fix: Chrome Extensions service worker host registration (#39290)
This commit is contained in:
parent
b2c62d6ad1
commit
c8f7a0e052
5 changed files with 50 additions and 1 deletions
|
@ -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<extensions::mojom::GuestView>(
|
||||
base::BindRepeating(&extensions::ExtensionsGuestView::CreateForExtensions,
|
||||
render_process_host->GetID()));
|
||||
associated_registry->AddInterface<extensions::mojom::ServiceWorkerHost>(
|
||||
base::BindRepeating(&extensions::ServiceWorkerHost::BindReceiver,
|
||||
render_process_host->GetID()));
|
||||
#endif
|
||||
}
|
||||
|
||||
|
|
|
@ -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');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -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' });
|
||||
}
|
||||
});
|
||||
|
|
13
spec/fixtures/extensions/mv3-service-worker/main.js
vendored
Normal file
13
spec/fixtures/extensions/mv3-service-worker/main.js
vendored
Normal file
|
@ -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);
|
|
@ -3,6 +3,17 @@
|
|||
"description": "Test for extension service worker support.",
|
||||
"version": "1.0",
|
||||
"manifest_version": 3,
|
||||
"content_scripts": [
|
||||
{
|
||||
"matches": [
|
||||
"<all_urls>"
|
||||
],
|
||||
"js": [
|
||||
"main.js"
|
||||
],
|
||||
"run_at": "document_start"
|
||||
}
|
||||
],
|
||||
"background": {
|
||||
"service_worker": "background.js"
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue