Skip to content

Commit 6951252

Browse files
codebyterepull[bot]
authored andcommitted
fix: chrome.tabs.update return value (#39365)
fix: chrome.tabs.update return value
1 parent ae5bfd2 commit 6951252

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

shell/browser/extensions/api/tabs/tabs_api.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ void ZoomModeToZoomSettings(WebContentsZoomController::ZoomMode zoom_mode,
5757
break;
5858
}
5959
}
60+
61+
api::tabs::MutedInfo CreateMutedInfo(content::WebContents* contents) {
62+
DCHECK(contents);
63+
api::tabs::MutedInfo info;
64+
info.muted = contents->IsAudioMuted();
65+
info.reason = api::tabs::MUTED_INFO_REASON_USER;
66+
return info;
67+
}
6068
} // namespace
6169

6270
ExecuteCodeInTabFunction::ExecuteCodeInTabFunction() : execute_tab_id_(-1) {}
@@ -502,11 +510,17 @@ ExtensionFunction::ResponseValue TabsUpdateFunction::GetResult() {
502510

503511
auto* api_web_contents = electron::api::WebContents::From(web_contents_);
504512
tab.id = (api_web_contents ? api_web_contents->ID() : -1);
513+
505514
// TODO(nornagon): in Chrome, the tab URL is only available to extensions
506515
// that have the "tabs" (or "activeTab") permission. We should do the same
507516
// permission check here.
508517
tab.url = web_contents_->GetLastCommittedURL().spec();
509518

519+
if (api_web_contents)
520+
tab.active = api_web_contents->IsFocused();
521+
tab.muted_info = CreateMutedInfo(web_contents_);
522+
tab.audible = web_contents_->IsCurrentlyAudible();
523+
510524
return ArgumentList(tabs::Get::Results::Create(std::move(tab)));
511525
}
512526

spec/extensions-spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,23 @@ describe('chrome extensions', () => {
950950
expect(response.status).to.equal('reloaded');
951951
});
952952
});
953+
954+
it('update', async () => {
955+
await w.loadURL(url);
956+
957+
const message = { method: 'update', args: [{ muted: true }] };
958+
w.webContents.executeJavaScript(`window.postMessage('${JSON.stringify(message)}', '*')`);
959+
960+
const [,, responseString] = await once(w.webContents, 'console-message');
961+
const response = JSON.parse(responseString);
962+
963+
expect(response).to.have.property('mutedInfo').that.is.a('object');
964+
const { mutedInfo } = response;
965+
expect(mutedInfo).to.deep.eq({
966+
muted: true,
967+
reason: 'user'
968+
});
969+
});
953970
});
954971
});
955972
});

spec/fixtures/extensions/tabs-api-async/background.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ const handleRequest = (request, sender, sendResponse) => {
4242
chrome.tabs.reload(tabId).then(() => {
4343
sendResponse({ status: 'reloaded' });
4444
});
45+
break;
46+
}
47+
48+
case 'update': {
49+
const [params] = args;
50+
chrome.tabs.update(tabId, params).then(sendResponse);
51+
break;
4552
}
4653
}
4754
};

spec/fixtures/extensions/tabs-api-async/main.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ const testMap = {
3434
chrome.runtime.sendMessage({ method: 'reload' }, response => {
3535
console.log(JSON.stringify(response));
3636
});
37+
},
38+
update (params) {
39+
chrome.runtime.sendMessage({ method: 'update', args: [params] }, response => {
40+
console.log(JSON.stringify(response));
41+
});
3742
}
3843
};
3944

0 commit comments

Comments
 (0)