Bug 1311576 fix webrequest filter for tabId and windowId, r=kmag

MozReview-Commit-ID: FzW53LXktWz

--HG--
extra : rebase_source : e19987aa7f2bfa711f07abebaf37b75464508177
This commit is contained in:
Shane Caraveo 2016-11-15 16:34:33 -08:00
Родитель 30f5c7c935
Коммит d981ec07ec
5 изменённых файлов: 200 добавлений и 10 удалений

Просмотреть файл

@ -76,7 +76,10 @@ extensions.on("page-shutdown", (type, context) => {
});
extensions.on("fill-browser-data", (type, browser, data) => {
data.tabId = browser ? TabManager.getBrowserId(browser) : -1;
let gBrowser = browser && browser.ownerGlobal.gBrowser;
let tab = gBrowser && gBrowser.getTabForBrowser(browser);
data.tabId = tab ? TabManager.getId(tab) : -1;
data.windowId = tab ? WindowManager.getId(tab.ownerGlobal) : -1;
});
/* eslint-enable mozilla/balanced-listeners */
@ -140,7 +143,7 @@ let tabListener = {
},
handleWindowOpen(window) {
if (window.arguments[0] instanceof window.XULElement) {
if (window.arguments && window.arguments[0] instanceof window.XULElement) {
// If the first window argument is a XUL element, it means the
// window is about to adopt a tab from another window to replace its
// initial tab.

Просмотреть файл

@ -27,12 +27,21 @@ function WebRequestEventManager(context, eventName) {
if (data.isSystemPrincipal) {
return;
}
let browserData = {};
extensions.emit("fill-browser-data", data.browser, browserData);
if (filter.tabId != null && browserData.tabId != filter.tabId) {
return;
}
if (filter.windowId != null && browserData.windowId != filter.windowId) {
return;
}
let data2 = {
requestId: data.requestId,
url: data.url,
originUrl: data.originUrl,
method: data.method,
tabId: browserData.tabId,
type: data.type,
timeStamp: Date.now(),
frameId: ExtensionManagement.getFrameId(data.windowId),
@ -48,8 +57,6 @@ function WebRequestEventManager(context, eventName) {
data2.ip = data.ip;
}
extensions.emit("fill-browser-data", data.browser, data2);
let optional = ["requestHeaders", "responseHeaders", "statusCode", "statusLine", "error", "redirectUrl",
"requestBody"];
for (let opt of optional) {

Просмотреть файл

@ -159,10 +159,8 @@ function background(events) {
if (expectedEvent) {
expected.events.shift();
} else {
expectedEvent = expected.optional_events[0] == name;
if (expectedEvent) {
expected.optional_events.shift();
}
// e10s vs. non-e10s errors can end with either onCompleted or onErrorOccurred
expectedEvent = expected.optional_events.includes(name);
}
browser.test.assertTrue(expectedEvent, `received ${name}`);
browser.test.assertEq(expected.type, details.type, "resource type is correct");
@ -177,8 +175,7 @@ function background(events) {
browser.test.assertEq("string", typeof expected.test.requestId, `requestid ${expected.test.requestId} is string`);
browser.test.assertEq("string", typeof details.requestId, `requestid ${details.requestId} is string`);
browser.test.assertEq("number", typeof parseInt(details.requestId, 10), "parsed requestid is number");
browser.test.assertNotEq(expected.test.requestId, details.requestId,
`last requestId ${expected.test.requestId} different from this one ${details.requestId}`);
browser.test.assertEq(expected.test.requestId, details.requestId, "redirects will keep the same requestId");
} else {
// Save any values we want to validate in later events.
expected.test.requestId = details.requestId;

Просмотреть файл

@ -96,6 +96,8 @@ skip-if = (os == 'android') # Bug 1258975 on android.
skip-if = os == 'android' # webrequest api unsupported (bug 1258975).
[test_ext_webrequest_basic.html]
skip-if = os == 'android' # webrequest api unsupported (bug 1258975).
[test_ext_webrequest_filter.html]
skip-if = os == 'android' # webrequest api unsupported (bug 1258975).
[test_ext_webrequest_suspend.html]
skip-if = os == 'android' # webrequest api unsupported (bug 1258975).
[test_ext_webrequest_upload.html]

Просмотреть файл

@ -0,0 +1,181 @@
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
<script type="text/javascript" src="head.js"></script>
<script type="text/javascript" src="head_webrequest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<script>
"use strict";
let windowData, testWindow;
add_task(function* setup() {
testWindow = window.open("about:blank", "_blank", "width=100,height=100");
yield waitForLoad(testWindow);
// Fetch the windowId and tabId we need to filter with WebRequest.
let extension = ExtensionTestUtils.loadExtension({
manifest: {
permissions: [
"tabs",
],
},
background() {
browser.windows.getCurrent({populate: true}).then(window => {
browser.test.log(`current window ${window.id} tabs: ${JSON.stringify(window.tabs.map(tab => [tab.id, tab.url]))}`);
browser.test.sendMessage("windowData", {windowId: window.id, tabId: window.tabs[0].id});
});
},
});
yield extension.startup();
windowData = yield extension.awaitMessage("windowData");
info(`window is ${JSON.stringify(windowData)}`);
yield extension.unload();
});
add_task(function* test_webRequest_filter_window() {
yield SpecialPowers.pushPrefEnv({
set: [["dom.serviceWorkers.testing.enabled", true]],
});
let events = {
"onBeforeRequest": [{urls: ["<all_urls>"], windowId: windowData.windowId}],
"onBeforeSendHeaders": [{urls: ["<all_urls>"], windowId: windowData.windowId}, ["requestHeaders"]],
"onSendHeaders": [{urls: ["<all_urls>"], windowId: windowData.windowId}, ["requestHeaders"]],
"onBeforeRedirect": [{urls: ["<all_urls>"], windowId: windowData.windowId}],
"onHeadersReceived": [{urls: ["<all_urls>"], windowId: windowData.windowId}, ["responseHeaders"]],
"onResponseStarted": [{urls: ["<all_urls>"], windowId: windowData.windowId}],
"onCompleted": [{urls: ["<all_urls>"], windowId: windowData.windowId}, ["responseHeaders"]],
"onErrorOccurred": [{urls: ["<all_urls>"], windowId: windowData.windowId}],
};
let expect = {
"file_image_bad.png": {
optional_events: ["onBeforeRedirect", "onBeforeRequest", "onBeforeSendHeaders", "onSendHeaders"],
type: "main_frame",
},
};
let extension = makeExtension(events);
yield extension.startup();
extension.sendMessage("set-expected", {expect, origin: location.href});
yield extension.awaitMessage("continue");
// We should not get events for a new window load.
let newWindow = window.open("file_image_good.png", "_blank", "width=100,height=100");
yield waitForLoad(newWindow);
newWindow.close();
// We should not get background events.
let registration = yield navigator.serviceWorker.register("webrequest_worker.js?test0", {scope: "."});
// We should get events for the reload.
testWindow.location = "file_image_bad.png";
yield extension.awaitMessage("done");
yield registration.unregister();
yield extension.unload();
});
add_task(function* test_webRequest_filter_tab() {
yield SpecialPowers.pushPrefEnv({
set: [["dom.serviceWorkers.testing.enabled", true]],
});
let events = {
"onBeforeRequest": [{urls: ["<all_urls>"], tabId: windowData.tabId}],
"onBeforeSendHeaders": [{urls: ["<all_urls>"], tabId: windowData.tabId}, ["requestHeaders"]],
"onSendHeaders": [{urls: ["<all_urls>"], tabId: windowData.tabId}, ["requestHeaders"]],
"onBeforeRedirect": [{urls: ["<all_urls>"], tabId: windowData.tabId}],
"onHeadersReceived": [{urls: ["<all_urls>"], tabId: windowData.tabId}, ["responseHeaders"]],
"onResponseStarted": [{urls: ["<all_urls>"], tabId: windowData.tabId}],
"onCompleted": [{urls: ["<all_urls>"], tabId: windowData.tabId}, ["responseHeaders"]],
"onErrorOccurred": [{urls: ["<all_urls>"], tabId: windowData.tabId}],
};
let expect = {
"file_image_good.png": {
optional_events: ["onBeforeRedirect", "onBeforeRequest", "onBeforeSendHeaders", "onSendHeaders"],
type: "main_frame",
cached: true,
},
};
let extension = makeExtension(events);
yield extension.startup();
extension.sendMessage("set-expected", {expect, origin: location.href});
yield extension.awaitMessage("continue");
// We should not get events for a new window load.
let newWindow = window.open("file_image_good.png", "_blank", "width=100,height=100");
yield waitForLoad(newWindow);
newWindow.close();
// We should not get background events.
let registration = yield navigator.serviceWorker.register("webrequest_worker.js?test1", {scope: "."});
// We should get events for the reload.
testWindow.location = "file_image_good.png";
yield extension.awaitMessage("done");
yield registration.unregister();
yield extension.unload();
});
add_task(function* test_webRequest_filter_background() {
yield SpecialPowers.pushPrefEnv({
set: [["dom.serviceWorkers.testing.enabled", true]],
});
let events = {
"onBeforeRequest": [{urls: ["<all_urls>"], tabId: -1}],
"onBeforeSendHeaders": [{urls: ["<all_urls>"], tabId: -1}, ["requestHeaders"]],
"onSendHeaders": [{urls: ["<all_urls>"], tabId: -1}, ["requestHeaders"]],
"onBeforeRedirect": [{urls: ["<all_urls>"], tabId: -1}],
"onHeadersReceived": [{urls: ["<all_urls>"], tabId: -1}, ["responseHeaders"]],
"onResponseStarted": [{urls: ["<all_urls>"], tabId: -1}],
"onCompleted": [{urls: ["<all_urls>"], tabId: -1}, ["responseHeaders"]],
"onErrorOccurred": [{urls: ["<all_urls>"], tabId: -1}],
};
let expect = {
"webrequest_worker.js": {
type: "script",
},
"example.txt": {
status: 404,
events: ["onBeforeRequest", "onBeforeSendHeaders", "onSendHeaders", "onHeadersReceived", "onResponseStarted"],
optional_events: ["onCompleted", "onErrorOccurred"],
type: "other",
origin: SimpleTest.getTestFileURL("webrequest_worker.js?test2"),
},
};
let extension = makeExtension(events);
yield extension.startup();
extension.sendMessage("set-expected", {expect, origin: location.href});
yield extension.awaitMessage("continue");
// We should not get events for a window.
testWindow.location = "file_image_bad.png";
// We should get events for the background page.
let registration = yield navigator.serviceWorker.register(SimpleTest.getTestFileURL("webrequest_worker.js?test2"), {scope: "."});
yield extension.awaitMessage("done");
yield registration.unregister();
yield extension.unload();
});
add_task(function* teardown() {
testWindow.close();
});
</script>
</head>
<body>
</body>
</html>