From 67523a47b46b21281dc32432da9ae1aaa51d1956 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Mon, 7 Aug 2023 10:03:45 +0200 Subject: [PATCH] fix: `chrome.tabs.update` return value (#39365) fix: chrome.tabs.update return value --- shell/browser/extensions/api/tabs/tabs_api.cc | 14 ++++++++++++++ spec/extensions-spec.ts | 17 +++++++++++++++++ .../extensions/tabs-api-async/background.js | 7 +++++++ spec/fixtures/extensions/tabs-api-async/main.js | 5 +++++ 4 files changed, 43 insertions(+) diff --git a/shell/browser/extensions/api/tabs/tabs_api.cc b/shell/browser/extensions/api/tabs/tabs_api.cc index 87920a6e1cb0..cde0d41b28cf 100644 --- a/shell/browser/extensions/api/tabs/tabs_api.cc +++ b/shell/browser/extensions/api/tabs/tabs_api.cc @@ -57,6 +57,14 @@ void ZoomModeToZoomSettings(WebContentsZoomController::ZoomMode zoom_mode, break; } } + +api::tabs::MutedInfo CreateMutedInfo(content::WebContents* contents) { + DCHECK(contents); + api::tabs::MutedInfo info; + info.muted = contents->IsAudioMuted(); + info.reason = api::tabs::MUTED_INFO_REASON_USER; + return info; +} } // namespace ExecuteCodeInTabFunction::ExecuteCodeInTabFunction() : execute_tab_id_(-1) {} @@ -502,11 +510,17 @@ ExtensionFunction::ResponseValue TabsUpdateFunction::GetResult() { auto* api_web_contents = electron::api::WebContents::From(web_contents_); tab.id = (api_web_contents ? api_web_contents->ID() : -1); + // TODO(nornagon): in Chrome, the tab URL is only available to extensions // that have the "tabs" (or "activeTab") permission. We should do the same // permission check here. tab.url = web_contents_->GetLastCommittedURL().spec(); + if (api_web_contents) + tab.active = api_web_contents->IsFocused(); + tab.muted_info = CreateMutedInfo(web_contents_); + tab.audible = web_contents_->IsCurrentlyAudible(); + return ArgumentList(tabs::Get::Results::Create(std::move(tab))); } diff --git a/spec/extensions-spec.ts b/spec/extensions-spec.ts index cc8b49c6c282..c55964c990d8 100644 --- a/spec/extensions-spec.ts +++ b/spec/extensions-spec.ts @@ -950,6 +950,23 @@ describe('chrome extensions', () => { expect(response.status).to.equal('reloaded'); }); }); + + it('update', async () => { + await w.loadURL(url); + + const message = { method: 'update', args: [{ muted: true }] }; + w.webContents.executeJavaScript(`window.postMessage('${JSON.stringify(message)}', '*')`); + + const [,, responseString] = await once(w.webContents, 'console-message'); + const response = JSON.parse(responseString); + + expect(response).to.have.property('mutedInfo').that.is.a('object'); + const { mutedInfo } = response; + expect(mutedInfo).to.deep.eq({ + muted: true, + reason: 'user' + }); + }); }); }); }); diff --git a/spec/fixtures/extensions/tabs-api-async/background.js b/spec/fixtures/extensions/tabs-api-async/background.js index 32290c36fb78..2e3eb8ebf5cd 100644 --- a/spec/fixtures/extensions/tabs-api-async/background.js +++ b/spec/fixtures/extensions/tabs-api-async/background.js @@ -42,6 +42,13 @@ const handleRequest = (request, sender, sendResponse) => { chrome.tabs.reload(tabId).then(() => { sendResponse({ status: 'reloaded' }); }); + break; + } + + case 'update': { + const [params] = args; + chrome.tabs.update(tabId, params).then(sendResponse); + break; } } }; diff --git a/spec/fixtures/extensions/tabs-api-async/main.js b/spec/fixtures/extensions/tabs-api-async/main.js index 8c23bb00d0a1..4360681115ab 100644 --- a/spec/fixtures/extensions/tabs-api-async/main.js +++ b/spec/fixtures/extensions/tabs-api-async/main.js @@ -34,6 +34,11 @@ const testMap = { chrome.runtime.sendMessage({ method: 'reload' }, response => { console.log(JSON.stringify(response)); }); + }, + update (params) { + chrome.runtime.sendMessage({ method: 'update', args: [params] }, response => { + console.log(JSON.stringify(response)); + }); } };