OLD | NEW |
---|---|
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 Loading... | |
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 Loading... | |
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) { | |
176 if (details.tabId !== -1 || details.frameId >= 0) | |
battre
2015/12/15 13:32:55
// The event is considered expected if it happens
robwu
2015/12/15 14:13:30
Done, with a slightly different wording. It's not
| |
177 return false; | |
178 | |
179 // This only returns false if there is at least one expected event where the | |
180 // details match the given details. | |
battre
2015/12/15 13:32:55
I am struggling with the negations. Unexpected, a
robwu
2015/12/15 14:13:30
I've uploaded a new patchset that moves the negati
| |
181 return expectedEventData.every(function(exp) { | |
182 return name !== exp.event || | |
183 exp.details.tabId !== -1 || | |
184 exp.details.frameId !== -1 || | |
185 exp.details.method !== details.method || | |
186 exp.details.url !== details.url || | |
187 exp.details.type !== details.type; | |
188 }); | |
189 } | |
190 | |
173 function captureEvent(name, details, callback) { | 191 function captureEvent(name, details, callback) { |
192 // frameId should be -1 or positive, but is sometimes -2 (MSG_ROUTING_NONE). | |
193 // TODO(robwu): This will be resolved once crbug.com/432875 is resolved. | |
194 if (details.frameId === -2) | |
195 details.frameId = -1; | |
196 | |
174 // Ignore system-level requests like safebrowsing updates and favicon fetches | 197 // Ignore system-level requests like safebrowsing updates and favicon fetches |
175 // since they are unpredictable. | 198 // since they are unpredictable. |
176 if (details.tabId == -1 || details.type == "other" || | 199 if (details.type == "other" || |
200 isUnexpectedDetachedRequest(name, details) || | |
177 details.url.match(/\/favicon.ico$/) || | 201 details.url.match(/\/favicon.ico$/) || |
178 details.url.match(/https:\/\/dl.google.com/)) | 202 details.url.match(/https:\/\/dl.google.com/)) |
179 return; | 203 return; |
180 | 204 |
181 // Pull the extra per-event options out of the expected data. These let | 205 // Pull the extra per-event options out of the expected data. These let |
182 // us specify special return values per event. | 206 // us specify special return values per event. |
183 var currentIndex = capturedEventData.length; | 207 var currentIndex = capturedEventData.length; |
184 var extraOptions; | 208 var extraOptions; |
185 var retval; | 209 var retval; |
186 if (expectedEventData.length > currentIndex) { | 210 if (expectedEventData.length > currentIndex) { |
(...skipping 24 matching lines...) Expand all Loading... | |
211 // Subtract one to discount for {"-1": -1} mapping that always exists. | 235 // Subtract one to discount for {"-1": -1} mapping that always exists. |
212 // This gives the first frame the ID 0. | 236 // This gives the first frame the ID 0. |
213 frameIdMap[details.frameId] = Object.keys(frameIdMap).length - 1; | 237 frameIdMap[details.frameId] = Object.keys(frameIdMap).length - 1; |
214 } | 238 } |
215 details.frameId = frameIdMap[details.frameId]; | 239 details.frameId = frameIdMap[details.frameId]; |
216 details.parentFrameId = frameIdMap[details.parentFrameId]; | 240 details.parentFrameId = frameIdMap[details.parentFrameId]; |
217 | 241 |
218 // This assigns unique IDs to newly opened tabs. However, the new IDs are only | 242 // 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. | 243 // deterministic, if the order in which the tabs are opened is deterministic. |
220 if (!(details.tabId in tabIdMap)) { | 244 if (!(details.tabId in tabIdMap)) { |
221 tabIdMap[details.tabId] = Object.keys(tabIdMap).length; | 245 // Subtract one because the map is initialized with {"-1": -1}, and the |
246 // first tab has ID 0. | |
247 tabIdMap[details.tabId] = Object.keys(tabIdMap).length - 1; | |
222 } | 248 } |
223 details.tabId = tabIdMap[details.tabId]; | 249 details.tabId = tabIdMap[details.tabId]; |
224 | 250 |
225 delete details.requestId; | 251 delete details.requestId; |
226 delete details.timeStamp; | 252 delete details.timeStamp; |
227 if (details.requestHeaders) { | 253 if (details.requestHeaders) { |
228 details.requestHeadersValid = checkUserAgent(details.requestHeaders); | 254 details.requestHeadersValid = checkUserAgent(details.requestHeaders); |
229 delete details.requestHeaders; | 255 delete details.requestHeaders; |
230 } | 256 } |
231 if (details.responseHeaders) { | 257 if (details.responseHeaders) { |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
379 helper('onHeadersReceived'); | 405 helper('onHeadersReceived'); |
380 helper('onResponseStarted'); | 406 helper('onResponseStarted'); |
381 helper('onBeforeRedirect'); | 407 helper('onBeforeRedirect'); |
382 helper('onCompleted'); | 408 helper('onCompleted'); |
383 helper('onErrorOccurred'); | 409 helper('onErrorOccurred'); |
384 } | 410 } |
385 | 411 |
386 function resetDeclarativeRules() { | 412 function resetDeclarativeRules() { |
387 chrome.declarativeWebRequest.onRequest.removeRules(); | 413 chrome.declarativeWebRequest.onRequest.removeRules(); |
388 } | 414 } |
OLD | NEW |