Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(407)

Side by Side Diff: chrome/test/data/extensions/api_test/webrequest/framework.js

Issue 1515703005: WebRequest API: add more resource types (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 var getURL = chrome.extension.getURL; 5 var getURL = chrome.extension.getURL;
6 var deepEq = chrome.test.checkDeepEq; 6 var deepEq = chrome.test.checkDeepEq;
7 var expectedEventData; 7 var expectedEventData;
8 var capturedEventData; 8 var capturedEventData;
9 var capturedUnexpectedData; 9 var capturedUnexpectedData;
10 var expectedEventOrder; 10 var expectedEventOrder;
(...skipping 22 matching lines...) Expand all
33 var ignoreUnexpected = false; 33 var ignoreUnexpected = false;
34 34
35 // This is a debugging aid to print all received events as well as the 35 // This is a debugging aid to print all received events as well as the
36 // information whether they were expected. 36 // information whether they were expected.
37 var logAllRequests = false; 37 var logAllRequests = false;
38 38
39 function runTests(tests) { 39 function runTests(tests) {
40 var waitForAboutBlank = function(_, info, tab) { 40 var waitForAboutBlank = function(_, info, tab) {
41 if (info.status == "complete" && tab.url == "about:blank") { 41 if (info.status == "complete" && tab.url == "about:blank") {
42 tabId = tab.id; 42 tabId = tab.id;
43 tabIdMap = {}; 43 tabIdMap = {"-1": -1};
44 tabIdMap[tabId] = 0; 44 tabIdMap[tabId] = 0;
45 chrome.tabs.onUpdated.removeListener(waitForAboutBlank); 45 chrome.tabs.onUpdated.removeListener(waitForAboutBlank);
46 chrome.test.getConfig(function(config) { 46 chrome.test.getConfig(function(config) {
47 testServerPort = config.testServer.port; 47 testServerPort = config.testServer.port;
48 chrome.test.runTests(tests); 48 chrome.test.runTests(tests);
49 }); 49 });
50 } 50 }
51 }; 51 };
52 chrome.tabs.onUpdated.addListener(waitForAboutBlank); 52 chrome.tabs.onUpdated.addListener(waitForAboutBlank);
53 chrome.tabs.create({url: "about:blank"}); 53 chrome.tabs.create({url: "about:blank"});
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 // Simple check to see that we have a User-Agent header, and that it contains 163 // Simple check to see that we have a User-Agent header, and that it contains
164 // an expected value. This is a basic check that the request headers are valid. 164 // an expected value. This is a basic check that the request headers are valid.
165 function checkUserAgent(headers) { 165 function checkUserAgent(headers) {
166 for (var i in headers) { 166 for (var i in headers) {
167 if (headers[i].name.toLowerCase() == "user-agent") 167 if (headers[i].name.toLowerCase() == "user-agent")
168 return headers[i].value.toLowerCase().indexOf("chrome") != -1; 168 return headers[i].value.toLowerCase().indexOf("chrome") != -1;
169 } 169 }
170 return false; 170 return false;
171 } 171 }
172 172
173 // Whether the request is missing a tabId and frameId and we're not expecting
174 // a request with the given details.
175 function isUnexpectedDetachedRequest(name, details) {
battre 2015/12/15 12:53:09 Could you add some more comments that explain what
robwu 2015/12/15 13:17:25 Done.
176 if (details.tabId !== -1 || details.frameId >= 0)
177 return false;
178
179 return expectedEventData.every(function(exp) {
180 return name !== exp.event ||
181 exp.details.tabId !== -1 ||
182 exp.details.frameId !== -1 ||
183 exp.details.method !== details.method ||
184 exp.details.url !== details.url ||
185 exp.details.type !== details.type;
186 });
187 }
188
173 function captureEvent(name, details, callback) { 189 function captureEvent(name, details, callback) {
190 // frameId should be -1 or positive, but is sometimes -2 (MSG_ROUTING_NONE).
191 // TODO(robwu): This will be resolved once crbug.com/432875 is resolved.
192 if (details.frameId === -2)
193 details.frameId = -1;
194
174 // Ignore system-level requests like safebrowsing updates and favicon fetches 195 // Ignore system-level requests like safebrowsing updates and favicon fetches
175 // since they are unpredictable. 196 // since they are unpredictable.
176 if (details.tabId == -1 || details.type == "other" || 197 if (details.type == "other" ||
198 isUnexpectedDetachedRequest(name, details) ||
177 details.url.match(/\/favicon.ico$/) || 199 details.url.match(/\/favicon.ico$/) ||
178 details.url.match(/https:\/\/dl.google.com/)) 200 details.url.match(/https:\/\/dl.google.com/))
179 return; 201 return;
180 202
181 // Pull the extra per-event options out of the expected data. These let 203 // Pull the extra per-event options out of the expected data. These let
182 // us specify special return values per event. 204 // us specify special return values per event.
183 var currentIndex = capturedEventData.length; 205 var currentIndex = capturedEventData.length;
184 var extraOptions; 206 var extraOptions;
185 var retval; 207 var retval;
186 if (expectedEventData.length > currentIndex) { 208 if (expectedEventData.length > currentIndex) {
(...skipping 24 matching lines...) Expand all
211 // Subtract one to discount for {"-1": -1} mapping that always exists. 233 // Subtract one to discount for {"-1": -1} mapping that always exists.
212 // This gives the first frame the ID 0. 234 // This gives the first frame the ID 0.
213 frameIdMap[details.frameId] = Object.keys(frameIdMap).length - 1; 235 frameIdMap[details.frameId] = Object.keys(frameIdMap).length - 1;
214 } 236 }
215 details.frameId = frameIdMap[details.frameId]; 237 details.frameId = frameIdMap[details.frameId];
216 details.parentFrameId = frameIdMap[details.parentFrameId]; 238 details.parentFrameId = frameIdMap[details.parentFrameId];
217 239
218 // This assigns unique IDs to newly opened tabs. However, the new IDs are only 240 // This assigns unique IDs to newly opened tabs. However, the new IDs are only
219 // deterministic, if the order in which the tabs are opened is deterministic. 241 // deterministic, if the order in which the tabs are opened is deterministic.
220 if (!(details.tabId in tabIdMap)) { 242 if (!(details.tabId in tabIdMap)) {
221 tabIdMap[details.tabId] = Object.keys(tabIdMap).length; 243 // Subtract one because the map is initialized with {"-1": -1}, and the
244 // first tab has ID 0.
245 tabIdMap[details.tabId] = Object.keys(tabIdMap).length - 1;
222 } 246 }
223 details.tabId = tabIdMap[details.tabId]; 247 details.tabId = tabIdMap[details.tabId];
224 248
225 delete details.requestId; 249 delete details.requestId;
226 delete details.timeStamp; 250 delete details.timeStamp;
227 if (details.requestHeaders) { 251 if (details.requestHeaders) {
228 details.requestHeadersValid = checkUserAgent(details.requestHeaders); 252 details.requestHeadersValid = checkUserAgent(details.requestHeaders);
229 delete details.requestHeaders; 253 delete details.requestHeaders;
230 } 254 }
231 if (details.responseHeaders) { 255 if (details.responseHeaders) {
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
379 helper('onHeadersReceived'); 403 helper('onHeadersReceived');
380 helper('onResponseStarted'); 404 helper('onResponseStarted');
381 helper('onBeforeRedirect'); 405 helper('onBeforeRedirect');
382 helper('onCompleted'); 406 helper('onCompleted');
383 helper('onErrorOccurred'); 407 helper('onErrorOccurred');
384 } 408 }
385 409
386 function resetDeclarativeRules() { 410 function resetDeclarativeRules() {
387 chrome.declarativeWebRequest.onRequest.removeRules(); 411 chrome.declarativeWebRequest.onRequest.removeRules();
388 } 412 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698