From 1b18a3e79490ca8db24afc07d28d6f4b7101086a Mon Sep 17 00:00:00 2001 From: William Durand Date: Mon, 1 Aug 2022 15:02:21 +0000 Subject: [PATCH] Bug 1748315 - Part 2 - Pass `context.childManager` to `Schemas.checkParameters()`. r=rpl This is required to load and validate JSON schemas that use formatters like `relativeUrl`. Depends on D141750 Differential Revision: https://phabricator.services.mozilla.com/D141751 --- .../extensions/ExtensionProcessScript.jsm | 7 +- .../xpcshell/webidl-api/head_webidl_api.js | 9 +- .../test_ext_webidl_api_schema_formatters.js | 99 +++++++++++++++++++ .../test/xpcshell/webidl-api/xpcshell.ini | 1 + 4 files changed, 111 insertions(+), 5 deletions(-) create mode 100644 toolkit/components/extensions/test/xpcshell/webidl-api/test_ext_webidl_api_schema_formatters.js diff --git a/toolkit/components/extensions/ExtensionProcessScript.jsm b/toolkit/components/extensions/ExtensionProcessScript.jsm index dbb14ed9f765..f34e6c78edd8 100644 --- a/toolkit/components/extensions/ExtensionProcessScript.jsm +++ b/toolkit/components/extensions/ExtensionProcessScript.jsm @@ -510,7 +510,12 @@ var ExtensionAPIRequestHandler = { const { apiNamespace, apiName, args } = request; // Validate and normalize parameters, set the normalized args on the // mozIExtensionAPIRequest normalizedArgs property. - return lazy.Schemas.checkParameters(context, apiNamespace, apiName, args); + return lazy.Schemas.checkParameters( + context.childManager, + apiNamespace, + apiName, + args + ); }, }; diff --git a/toolkit/components/extensions/test/xpcshell/webidl-api/head_webidl_api.js b/toolkit/components/extensions/test/xpcshell/webidl-api/head_webidl_api.js index c75806a0765a..db603350eea2 100644 --- a/toolkit/components/extensions/test/xpcshell/webidl-api/head_webidl_api.js +++ b/toolkit/components/extensions/test/xpcshell/webidl-api/head_webidl_api.js @@ -87,7 +87,7 @@ function mockHandleAPIRequest(extPage, mockHandleAPIRequest) { "resource://gre/modules/ExtensionProcessScript.jsm" ); - mockFnText = `(() => { + mockFnText = `(() => { return (${mockFnText}); })();`; // eslint-disable-next-line no-eval @@ -137,6 +137,7 @@ function mockHandleAPIRequest(extPage, mockHandleAPIRequest) { * value was a promise) from the call to `backgroundScript` * - testError: the error raised (or rejected if the return value * value was a promise) from the call to `backgroundScript` + * - extension: the extension wrapper created by this helper. * @param {Function} mockAPIRequestHandler * Function to be used to mock mozIExtensionAPIRequestHandler.handleAPIRequest * for the purpose of the test. @@ -226,7 +227,7 @@ async function runExtensionAPITest( } } - async function runTestCaseInWorker(page) { + async function runTestCaseInWorker({ page, extension }) { info(`*** Run test case in an extension service worker`); const result = await page.spawn([], async () => { const { active } = await content.navigator.serviceWorker.ready; @@ -238,7 +239,7 @@ async function runExtensionAPITest( }); }); info(`*** Assert test case results got from extension service worker`); - await assertTestResult(result); + await assertTestResult({ ...result, extension }); } // NOTE: prefixing this with `function ` is needed because backgroundScript @@ -303,7 +304,7 @@ async function runExtensionAPITest( registerCleanupFunction(testCleanup); await mockHandleAPIRequest(page, mockAPIRequestHandler); - await runTestCaseInWorker(page); + await runTestCaseInWorker({ page, extension }); await testCleanup(); info(`End test case "${testDescription}"`); } diff --git a/toolkit/components/extensions/test/xpcshell/webidl-api/test_ext_webidl_api_schema_formatters.js b/toolkit/components/extensions/test/xpcshell/webidl-api/test_ext_webidl_api_schema_formatters.js new file mode 100644 index 000000000000..0f367af908a9 --- /dev/null +++ b/toolkit/components/extensions/test/xpcshell/webidl-api/test_ext_webidl_api_schema_formatters.js @@ -0,0 +1,99 @@ +/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* vim: set sts=2 sw=2 et tw=80: */ +"use strict"; + +const { ExtensionAPI } = ExtensionCommon; + +AddonTestUtils.init(this); +AddonTestUtils.createAppInfo( + "xpcshell@tests.mozilla.org", + "XPCShell", + "1", + "42" +); + +// Because the `mockExtensionAPI` is currently the only "mock" API that has +// WebIDL bindings, this is the only namespace we can use in our tests. There +// is no JSON schema for this namespace so we add one here that is tailored for +// our testing needs. +const API = class extends ExtensionAPI { + getAPI(context) { + return { + mockExtensionAPI: { + methodAsync: files => { + return files; + }, + }, + }; + } +}; + +const SCHEMA = [ + { + namespace: "mockExtensionAPI", + functions: [ + { + name: "methodAsync", + type: "function", + async: true, + parameters: [ + { + name: "files", + type: "array", + items: { $ref: "manifest.ExtensionURL" }, + }, + ], + }, + ], + }, +]; + +add_setup(async function() { + await AddonTestUtils.promiseStartupManager(); + + // The blob:-URL registered in `registerModules()` below gets loaded at: + // https://searchfox.org/mozilla-central/rev/0fec57c05d3996cc00c55a66f20dd5793a9bfb5d/toolkit/components/extensions/ExtensionCommon.jsm#1649 + Services.prefs.setBoolPref( + "security.allow_parent_unrestricted_js_loads", + true + ); + + ExtensionParent.apiManager.registerModules({ + mockExtensionAPI: { + schema: `data:,${JSON.stringify(SCHEMA)}`, + scopes: ["addon_parent"], + paths: [["mockExtensionAPI"]], + url: URL.createObjectURL( + new Blob([`this.mockExtensionAPI = ${API.toString()}`]) + ), + }, + }); +}); + +add_task(async function test_relative_urls() { + await runExtensionAPITest( + "should format arguments with the relativeUrl formatter", + { + backgroundScript() { + return browser.mockExtensionAPI.methodAsync([ + "script-1.js", + "script-2.js", + ]); + }, + mockAPIRequestHandler(policy, request) { + return this._handleAPIRequest_orig(policy, request); + }, + assertResults({ testResult, testError, extension }) { + Assert.deepEqual( + testResult, + [ + `moz-extension://${extension.uuid}/script-1.js`, + `moz-extension://${extension.uuid}/script-2.js`, + ], + "expected correct url" + ); + Assert.deepEqual(testError, undefined, "expected no error"); + }, + } + ); +}); diff --git a/toolkit/components/extensions/test/xpcshell/webidl-api/xpcshell.ini b/toolkit/components/extensions/test/xpcshell/webidl-api/xpcshell.ini index 013efc2cd9b6..3adc7a5248e1 100644 --- a/toolkit/components/extensions/test/xpcshell/webidl-api/xpcshell.ini +++ b/toolkit/components/extensions/test/xpcshell/webidl-api/xpcshell.ini @@ -28,4 +28,5 @@ skip-if = os == "android" && processor == "x86_64" && debug # Bug 1716308 [test_ext_webidl_api_request_handler.js] [test_ext_webidl_api_schema_errors.js] +[test_ext_webidl_api_schema_formatters.js] [test_ext_webidl_runtime_port.js]