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