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

MozReview-Commit-ID: Gfg3Wv7JGI6

--HG--
extra : rebase_source : 08999bd99d095d1198a9cbaa3eb0c84b051f4473
This commit is contained in:
Shane Caraveo 2016-11-10 09:54:34 -08:00
Родитель 992470914d
Коммит 086959718c
4 изменённых файлов: 193 добавлений и 4 удалений

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

@ -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.

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

@ -28,12 +28,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),
@ -49,8 +58,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) {

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

@ -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,177 @@
<!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": {
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": {
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,
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>