diff options
Diffstat (limited to 'toolkit/components/extensions')
5 files changed, 92 insertions, 4 deletions
diff --git a/toolkit/components/extensions/Extension.sys.mjs b/toolkit/components/extensions/Extension.sys.mjs index caff299eb6..e773745c65 100644 --- a/toolkit/components/extensions/Extension.sys.mjs +++ b/toolkit/components/extensions/Extension.sys.mjs @@ -1224,7 +1224,8 @@ export class ExtensionData { let { manifest } = this; if ( !manifest.background || - manifest.background.service_worker || + (manifest.background.service_worker && + WebExtensionPolicy.backgroundServiceWorkerEnabled) || this.manifestVersion > 2 ) { return false; diff --git a/toolkit/components/extensions/parent/ext-backgroundPage.js b/toolkit/components/extensions/parent/ext-backgroundPage.js index 725be65122..568d049b9d 100644 --- a/toolkit/components/extensions/parent/ext-backgroundPage.js +++ b/toolkit/components/extensions/parent/ext-backgroundPage.js @@ -264,7 +264,9 @@ this.backgroundPage = class extends ExtensionAPI { let { manifest } = extension; extension.backgroundState = BACKGROUND_STATE.STARTING; - this.isWorker = Boolean(manifest.background.service_worker); + this.isWorker = + !!manifest.background.service_worker && + WebExtensionPolicy.backgroundServiceWorkerEnabled; let BackgroundClass = this.isWorker ? BackgroundWorker : BackgroundPage; diff --git a/toolkit/components/extensions/parent/ext-runtime.js b/toolkit/components/extensions/parent/ext-runtime.js index cd18e0f0aa..2122e8faed 100644 --- a/toolkit/components/extensions/parent/ext-runtime.js +++ b/toolkit/components/extensions/parent/ext-runtime.js @@ -237,9 +237,12 @@ this.runtime = class extends ExtensionAPIPersistent { }, async internalWakeupBackground() { + const { background } = extension.manifest; if ( - extension.manifest.background && - !extension.manifest.background.service_worker && + background && + (background.page || background.scripts) && + // Note: if background.service_worker is specified, it takes + // precedence over page/scripts, and persistentBackground is false. !extension.persistentBackground ) { await extension.wakeupBackground(); diff --git a/toolkit/components/extensions/test/xpcshell/test_ext_background_script_and_service_worker.js b/toolkit/components/extensions/test/xpcshell/test_ext_background_script_and_service_worker.js new file mode 100644 index 0000000000..fc59b1810d --- /dev/null +++ b/toolkit/components/extensions/test/xpcshell/test_ext_background_script_and_service_worker.js @@ -0,0 +1,81 @@ +/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* vim: set sts=2 sw=2 et tw=80: */ +"use strict"; + +async function testExtensionWithBackground({ + with_scripts = false, + with_service_worker = false, + with_page = false, + expected_background_type, + expected_manifest_warnings = [], +}) { + let background = {}; + if (with_scripts) { + background.scripts = ["scripts.js"]; + } + if (with_service_worker) { + background.service_worker = "sw.js"; + } + if (with_page) { + background.page = "page.html"; + } + let extension = ExtensionTestUtils.loadExtension({ + manifest: { background }, + files: { + "scripts.js": () => { + browser.test.sendMessage("from_bg", "scripts"); + }, + "sw.js": () => { + browser.test.sendMessage("from_bg", "service_worker"); + }, + "page.html": `<!DOCTYPE html><script src="page.js"></script>`, + "page.js": () => { + browser.test.sendMessage("from_bg", "page"); + }, + }, + }); + ExtensionTestUtils.failOnSchemaWarnings(false); + await extension.startup(); + ExtensionTestUtils.failOnSchemaWarnings(true); + Assert.deepEqual( + extension.extension.warnings, + expected_manifest_warnings, + "Expected manifest warnings" + ); + info("Waiting for background to start"); + Assert.equal( + await extension.awaitMessage("from_bg"), + expected_background_type, + "Expected background type" + ); + await extension.unload(); +} + +add_task(async function test_page_and_scripts() { + await testExtensionWithBackground({ + with_page: true, + with_scripts: true, + // Should be expected_background_type: "scripts", not "page". + // https://github.com/w3c/webextensions/issues/282#issuecomment-1443332913 + // ... but changing that may potentially affect backcompat of existing + // Firefox add-ons. + expected_background_type: "page", + expected_manifest_warnings: [ + "Reading manifest: Warning processing background.scripts: An unexpected property was found in the WebExtension manifest.", + ], + }); +}); + +add_task( + { skip_if: () => WebExtensionPolicy.backgroundServiceWorkerEnabled }, + async function test_scripts_and_service_worker_when_sw_disabled() { + await testExtensionWithBackground({ + with_scripts: true, + with_service_worker: true, + expected_background_type: "scripts", + expected_manifest_warnings: [ + "Reading manifest: Warning processing background.service_worker: An unexpected property was found in the WebExtension manifest.", + ], + }); + } +); diff --git a/toolkit/components/extensions/test/xpcshell/xpcshell-common.ini b/toolkit/components/extensions/test/xpcshell/xpcshell-common.ini index f76456124d..81beee5810 100644 --- a/toolkit/components/extensions/test/xpcshell/xpcshell-common.ini +++ b/toolkit/components/extensions/test/xpcshell/xpcshell-common.ini @@ -25,6 +25,7 @@ skip-if = os == "android" # Bug 1700482 skip-if = os == "android" # Android does not use Places for history. [test_ext_background_private_browsing.js] [test_ext_background_runtime_connect_params.js] +[test_ext_background_script_and_service_worker.js] [test_ext_background_sub_windows.js] [test_ext_background_teardown.js] [test_ext_background_telemetry.js] |