Bug 1456995 - make browser_devtools_serviceworker_interception.js compatible with parent-intercept r=asuth

- Listen for observer topics in the parent-process when in parent intercept mode
- Remove an extra postMessage call (and the corresponding message handler)

Differential Revision: https://phabricator.services.mozilla.com/D44513

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Perry Jiang 2019-09-03 17:44:22 +00:00
parent 418b7275dd
commit a1ec5b43d3
2 changed files with 50 additions and 55 deletions

View File

@ -11,9 +11,11 @@ const crossHelloDoc = CROSS_URI + "hello.html";
const sw = BASE_URI + "fetch.js";
// XXXtt: We should be able to move this check to chrome process after we move
// the interception logic to chrome process.
async function checkObserverInContent(aInput) {
const isParentInterceptEnabled = Cc["@mozilla.org/serviceworkers/manager;1"]
.getService(Ci.nsIServiceWorkerManager)
.isParentInterceptEnabled();
async function checkObserver(aInput) {
let interceptedChannel = null;
// We always get two channels which receive the "http-on-stop-request"
@ -69,22 +71,26 @@ async function checkObserverInContent(aInput) {
{ start: tc.handleFetchEventStartTime, end: tc.handleFetchEventEndTime },
];
if (aInput.swPresent) {
serviceWorkerTimings.reduce((aPreviousTimings, aCurrentTimings) => {
ok(aPreviousTimings.start !== 0, "Start time check.");
ok(
aPreviousTimings.start <= aCurrentTimings.start,
"Start time order check."
);
ok(
aPreviousTimings.end <= aCurrentTimings.end,
"End time order check."
);
ok(
aCurrentTimings.start <= aCurrentTimings.end,
"Start time should be smaller than end time."
);
return aCurrentTimings;
});
// TODO: remove this condition (but keep the if statement's body) when
// bug 1577829 is resolved.
if (!isParentInterceptEnabled) {
serviceWorkerTimings.reduce((aPreviousTimings, aCurrentTimings) => {
ok(aPreviousTimings.start !== 0, "Start time check.");
ok(
aPreviousTimings.start <= aCurrentTimings.start,
"Start time order check."
);
ok(
aPreviousTimings.end <= aCurrentTimings.end,
"End time order check."
);
ok(
aCurrentTimings.start <= aCurrentTimings.end,
"Start time should be smaller than end time."
);
return aCurrentTimings;
});
}
} else {
serviceWorkerTimings.forEach(aTimings => {
is(aTimings.start, 0, "SW timings should be 0.");
@ -151,6 +157,27 @@ async function contentFetch(aURL) {
await content.window.fetch(aURL);
}
// The observer topics are fired in the parent process in parent-intercept
// and the content process in child-intercept. This function will handle running
// the check in the correct process. Note that it will block until the observers
// are notified.
async function fetchAndCheckObservers(
aFetchBrowser,
aObserverBrowser,
aTestCase
) {
let promise = null;
if (isParentInterceptEnabled) {
promise = checkObserver(aTestCase);
} else {
promise = ContentTask.spawn(aObserverBrowser, aTestCase, checkObserver);
}
await ContentTask.spawn(aFetchBrowser, aTestCase.url, contentFetch);
await promise;
}
async function registerSWAndWaitForActive(aServiceWorker) {
let swr = await content.navigator.serviceWorker.register(aServiceWorker, {
scope: "empty.html",
@ -168,8 +195,6 @@ async function registerSWAndWaitForActive(aServiceWorker) {
});
});
await swr.active.postMessage("claim");
await new Promise(resolve => {
if (content.navigator.serviceWorker.controller) {
return resolve();
@ -246,43 +271,19 @@ add_task(async function test_serivce_worker_interception() {
];
info("Test 1: Verify simple fetch");
let promise = ContentTask.spawn(
tabBrowser_observer,
testcases[0],
checkObserverInContent
);
await ContentTask.spawn(tabBrowser, testcases[0].url, contentFetch);
await promise;
await fetchAndCheckObservers(tabBrowser, tabBrowser_observer, testcases[0]);
info("Register a service worker");
await ContentTask.spawn(tabBrowser, sw, registerSWAndWaitForActive);
info("Test 2: Verify simple hijack");
promise = ContentTask.spawn(
tabBrowser_observer,
testcases[1],
checkObserverInContent
);
await ContentTask.spawn(tabBrowser, testcases[1].url, contentFetch);
await promise;
await fetchAndCheckObservers(tabBrowser, tabBrowser_observer, testcases[1]);
info("Test 3: Verify fetch without using http cache");
promise = ContentTask.spawn(
tabBrowser_observer,
testcases[2],
checkObserverInContent
);
await ContentTask.spawn(tabBrowser, testcases[2].url, contentFetch);
await promise;
await fetchAndCheckObservers(tabBrowser, tabBrowser_observer, testcases[2]);
info("Test 4: make a internal redirect");
promise = ContentTask.spawn(
tabBrowser_observer,
testcases[3],
checkObserverInContent
);
await ContentTask.spawn(tabBrowser, testcases[3].url, contentFetch);
await promise;
await fetchAndCheckObservers(tabBrowser, tabBrowser_observer, testcases[3]);
info("Clean up");
await ContentTask.spawn(tabBrowser, undefined, unregisterSW);

View File

@ -28,12 +28,6 @@ addEventListener("fetch", function(event) {
}
});
addEventListener("message", function(event) {
if (event.data === "claim") {
event.waitUntil(clients.claim());
}
});
addEventListener("activate", function(event) {
event.waitUntil(clients.claim());
});