diff --git a/shell/browser/electron_browser_client.cc b/shell/browser/electron_browser_client.cc index 913a8de8c2b8..b086e2dbb140 100644 --- a/shell/browser/electron_browser_client.cc +++ b/shell/browser/electron_browser_client.cc @@ -162,6 +162,7 @@ #include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_guest.h" #include "extensions/browser/process_manager.h" #include "extensions/browser/process_map.h" +#include "extensions/browser/service_worker/service_worker_host.h" #include "extensions/browser/url_loader_factory_manager.h" #include "extensions/common/api/mime_handler.mojom.h" #include "extensions/common/constants.h" @@ -1554,6 +1555,9 @@ void ElectronBrowserClient::ExposeInterfacesToRenderer( associated_registry->AddInterface( base::BindRepeating(&extensions::ExtensionsGuestView::CreateForExtensions, render_process_host->GetID())); + associated_registry->AddInterface( + base::BindRepeating(&extensions::ServiceWorkerHost::BindReceiver, + render_process_host->GetID())); #endif } diff --git a/spec/extensions-spec.ts b/spec/extensions-spec.ts index 5e048882497f..69bb5768c34e 100644 --- a/spec/extensions-spec.ts +++ b/spec/extensions-spec.ts @@ -711,5 +711,20 @@ describe('chrome extensions', () => { const scope = await registrationPromise; expect(scope).equals(extension.url); }); + + it('can run chrome extension APIs', async () => { + const customSession = session.fromPartition(`persist:${uuid.v4()}`); + const w = new BrowserWindow({ show: false, webPreferences: { session: customSession, nodeIntegration: true } }); + await customSession.loadExtension(path.join(fixtures, 'extensions', 'mv3-service-worker')); + + await w.loadURL(url); + + w.webContents.executeJavaScript('window.postMessage(\'fetch-confirmation\', \'*\')'); + + const [, , responseString] = await once(w.webContents, 'console-message'); + const { message } = JSON.parse(responseString); + + expect(message).to.equal('Hello from background.js'); + }); }); }); diff --git a/spec/fixtures/extensions/mv3-service-worker/background.js b/spec/fixtures/extensions/mv3-service-worker/background.js index c4d4a3003587..411f03f8fbbb 100644 --- a/spec/fixtures/extensions/mv3-service-worker/background.js +++ b/spec/fixtures/extensions/mv3-service-worker/background.js @@ -1 +1,7 @@ -console.log('service worker installed'); +/* global chrome */ + +chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => { + if (message === 'fetch-confirmation') { + sendResponse({ message: 'Hello from background.js' }); + } +}); diff --git a/spec/fixtures/extensions/mv3-service-worker/main.js b/spec/fixtures/extensions/mv3-service-worker/main.js new file mode 100644 index 000000000000..c7a37cd155bb --- /dev/null +++ b/spec/fixtures/extensions/mv3-service-worker/main.js @@ -0,0 +1,13 @@ +/* global chrome */ + +chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { + sendResponse(message); +}); + +window.addEventListener('message', (event) => { + if (event.data === 'fetch-confirmation') { + chrome.runtime.sendMessage('fetch-confirmation', response => { + console.log(JSON.stringify(response)); + }); + } +}, false); diff --git a/spec/fixtures/extensions/mv3-service-worker/manifest.json b/spec/fixtures/extensions/mv3-service-worker/manifest.json index bc81e3a6a510..e18a207e45cb 100644 --- a/spec/fixtures/extensions/mv3-service-worker/manifest.json +++ b/spec/fixtures/extensions/mv3-service-worker/manifest.json @@ -3,6 +3,17 @@ "description": "Test for extension service worker support.", "version": "1.0", "manifest_version": 3, + "content_scripts": [ + { + "matches": [ + "" + ], + "js": [ + "main.js" + ], + "run_at": "document_start" + } + ], "background": { "service_worker": "background.js" }