From 1ee3f7ba6ba87432879c85228c696ccf42665b46 Mon Sep 17 00:00:00 2001 From: Blake Kaplan Date: Fri, 21 Dec 2018 21:51:40 +0000 Subject: [PATCH] Bug 1515989 - Fix this test to use the service worker manager in the parent. r=asuth I would have loved to get rid of ServiceWorkerContainer.getScopeForUrl in favor of just doing things in the chrome script, but it's a pain to make it work in both the parent-intercept and non- cases. Once we get rid of non-parent-intercept, we should just remove this test-only API. Differential Revision: https://phabricator.services.mozilla.com/D15218 --HG-- extra : moz-landing-system : lando --- dom/serviceworkers/test/test_scopes.html | 48 ++++++++++++++++++++---- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/dom/serviceworkers/test/test_scopes.html b/dom/serviceworkers/test/test_scopes.html index 2d8116f8373b..39e828cb89c2 100644 --- a/dom/serviceworkers/test/test_scopes.html +++ b/dom/serviceworkers/test/test_scopes.html @@ -66,7 +66,39 @@ function testScopes() { return new Promise(function(resolve, reject) { - var getScope = navigator.serviceWorker.getScopeForUrl.bind(navigator.serviceWorker); + function chromeScriptSource() { + let swm = Cc["@mozilla.org/serviceworkers/manager;1"] + .getService(Ci.nsIServiceWorkerManager); + let secMan = Cc["@mozilla.org/scriptsecuritymanager;1"] + .getService(Ci.nsIScriptSecurityManager); + addMessageListener("getScope", (msg) => { + let principal = secMan.createCodebasePrincipalFromOrigin(msg.principal); + try { + return { scope: swm.getScopeForUrl(principal, msg.path) }; + } catch (e) { + return { exception: e.message }; + } + }); + } + + let getScope; + let parent_intercept_enabled = + SpecialPowers.getBoolPref("dom.serviceWorkers.parent_intercept"); + + if (parent_intercept_enabled) { + let chromeScript = SpecialPowers.loadChromeScript(chromeScriptSource); + let docPrincipal = SpecialPowers.wrap(document).nodePrincipal.URI.spec; + + getScope = (path) => { + let rv = chromeScript.sendSyncMessage("getScope", { principal: docPrincipal, path })[0][0]; + if (rv.exception) + throw rv.exception; + return rv.scope; + }; + } else { + getScope = navigator.serviceWorker.getScopeForUrl.bind(navigator.serviceWorker); + } + var base = new URL(".", document.baseURI); function p(s) { @@ -82,13 +114,13 @@ } } - ok(getScope(p("sub.html")) === p("sub"), "Scope should match"); - ok(getScope(p("sub/dir.html")) === p("sub/dir.html"), "Scope should match"); - ok(getScope(p("sub/dir")) === p("sub/dir"), "Scope should match"); - ok(getScope(p("sub/dir/foo")) === p("sub/dir/"), "Scope should match"); - ok(getScope(p("sub/dir/afoo")) === p("sub/dir/a"), "Scope should match"); - ok(getScope(p("star*wars")) === p("star*"), "Scope should match"); - ok(getScope(p("scope/some_file.html")) === p("scope/"), "Scope should match"); + is(getScope(p("sub.html")), p("sub"), "Scope should match"); + is(getScope(p("sub/dir.html")), p("sub/dir.html"), "Scope should match"); + is(getScope(p("sub/dir")), p("sub/dir"), "Scope should match"); + is(getScope(p("sub/dir/foo")), p("sub/dir/"), "Scope should match"); + is(getScope(p("sub/dir/afoo")), p("sub/dir/a"), "Scope should match"); + is(getScope(p("star*wars")), p("star*"), "Scope should match"); + is(getScope(p("scope/some_file.html")), p("scope/"), "Scope should match"); fail("index.html"); fail("sua.html"); fail("star/a.html");