OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include "chrome/browser/extensions/extension_webrequest_api.h" | 5 #include "chrome/browser/extensions/extension_webrequest_api.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
11 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "chrome/browser/extensions/extension_event_router_forwarder.h" | 13 #include "chrome/browser/extensions/extension_event_router_forwarder.h" |
14 #include "chrome/browser/extensions/extension_webrequest_api_constants.h" | 14 #include "chrome/browser/extensions/extension_webrequest_api_constants.h" |
15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/common/extensions/extension.h" | 16 #include "chrome/common/extensions/extension.h" |
17 #include "chrome/common/extensions/extension_extent.h" | 17 #include "chrome/common/extensions/extension_extent.h" |
18 #include "chrome/common/extensions/url_pattern.h" | 18 #include "chrome/common/extensions/url_pattern.h" |
19 #include "chrome/common/url_constants.h" | |
19 #include "content/browser/browser_thread.h" | 20 #include "content/browser/browser_thread.h" |
20 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
21 #include "net/url_request/url_request.h" | 22 #include "net/url_request/url_request.h" |
22 #include "googleurl/src/gurl.h" | 23 #include "googleurl/src/gurl.h" |
23 | 24 |
24 namespace keys = extension_webrequest_api_constants; | 25 namespace keys = extension_webrequest_api_constants; |
25 | 26 |
26 namespace { | 27 namespace { |
27 | 28 |
28 // List of all the webRequest events. | 29 // List of all the webRequest events. |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
236 net::URLRequest* request, | 237 net::URLRequest* request, |
237 net::CompletionCallback* callback) { | 238 net::CompletionCallback* callback) { |
238 // TODO(jochen): Figure out what to do with events from the system context. | 239 // TODO(jochen): Figure out what to do with events from the system context. |
239 if (profile_id == Profile::kInvalidProfileId) | 240 if (profile_id == Profile::kInvalidProfileId) |
240 return false; | 241 return false; |
241 std::vector<const EventListener*> listeners = | 242 std::vector<const EventListener*> listeners = |
242 GetMatchingListeners(profile_id, keys::kOnBeforeRequest, request->url()); | 243 GetMatchingListeners(profile_id, keys::kOnBeforeRequest, request->url()); |
243 if (listeners.empty()) | 244 if (listeners.empty()) |
244 return false; | 245 return false; |
245 | 246 |
247 // Don't handle HTTP requests yet. We want to wait until we have headers | |
248 // available. Then we can dispatch the event. Remember the request, because | |
249 // we only have access to the ID at that point. | |
250 if (request->url().SchemeIs(chrome::kHttpScheme) || | |
251 request->url().SchemeIs(chrome::kHttpsScheme)) { | |
252 http_requests_[request->identifier()] = request; | |
willchan no longer on Chromium
2011/03/17 15:55:54
You insert into this map here. What happens if OnB
Matt Perry
2011/03/22 21:11:43
Yep... I added a tracking object so they're remove
| |
253 return false; | |
254 } | |
255 | |
256 return OnBeforeRequestCommon(profile_id, event_router, request, callback, | |
257 tab_id, window_id, resource_type, listeners); | |
willchan no longer on Chromium
2011/03/17 15:55:54
where are these variables coming from? looks like
Matt Perry
2011/03/22 21:11:43
oops. bad base revision messed this up.
| |
258 } | |
259 | |
260 bool ExtensionWebRequestEventRouter::OnBeforeHttpRequest( | |
261 ProfileId profile_id, | |
262 ExtensionEventRouterForwarder* event_router, | |
263 uint64 request_id, | |
264 net::HttpRequestHeaders* headers, | |
willchan no longer on Chromium
2011/03/17 15:55:54
Don't you need to pass this variable through to On
Matt Perry
2011/03/22 21:11:43
Yeah, I'm leaving that as a TODO for now. There's
| |
265 net::CompletionCallback* callback) { | |
266 // TODO(jochen): Figure out what to do with events from the system context. | |
267 if (profile_id == Profile::kInvalidProfileId) | |
268 return false; | |
269 | |
270 HttpRequestMap::iterator iter = http_requests_.find(request_id); | |
271 if (iter == http_requests_.end()) | |
willchan no longer on Chromium
2011/03/17 15:55:54
Isn't this a bug? Should this be LOG(DFATAL)?
| |
272 return false; | |
273 | |
274 net::URLRequest* request = iter->second; | |
275 http_requests_.erase(iter); | |
276 | |
277 int tab_id = -1; | |
278 int window_id = -1; | |
279 ResourceType::Type resource_type = ResourceType::LAST_TYPE; | |
280 ExtractRequestInfo(request, &tab_id, &window_id, &resource_type); | |
281 | |
282 std::vector<const EventListener*> listeners = | |
283 GetMatchingListeners(profile_id, keys::kOnBeforeRequest, request->url(), | |
284 tab_id, window_id, resource_type); | |
285 if (listeners.empty()) | |
286 return false; | |
287 | |
288 return OnBeforeRequestCommon(profile_id, event_router, request, callback, | |
289 tab_id, window_id, resource_type, listeners); | |
290 } | |
291 | |
292 bool ExtensionWebRequestEventRouter::OnBeforeRequestCommon( | |
293 ProfileId profile_id, | |
294 ExtensionEventRouterForwarder* event_router, | |
295 net::URLRequest* request, | |
296 net::CompletionCallback* callback, | |
297 int tab_id, | |
298 int window_id, | |
299 ResourceType::Type resource_type, | |
300 const std::vector<const EventListener*>& listeners) { | |
246 ListValue args; | 301 ListValue args; |
247 DictionaryValue* dict = new DictionaryValue(); | 302 DictionaryValue* dict = new DictionaryValue(); |
248 dict->SetString(keys::kUrlKey, request->url().spec()); | 303 dict->SetString(keys::kUrlKey, request->url().spec()); |
249 dict->SetString(keys::kMethodKey, request->method()); | 304 dict->SetString(keys::kMethodKey, request->method()); |
250 // TODO(mpcomplete): implement | 305 // TODO(mpcomplete): implement |
251 dict->SetInteger(keys::kTabIdKey, 0); | 306 dict->SetInteger(keys::kTabIdKey, 0); |
252 dict->SetString(keys::kRequestIdKey, | 307 dict->SetString(keys::kRequestIdKey, |
253 base::Uint64ToString(request->identifier())); | 308 base::Uint64ToString(request->identifier())); |
254 dict->SetString(keys::kTypeKey, "main_frame"); | 309 dict->SetString(keys::kTypeKey, "main_frame"); |
255 dict->SetInteger(keys::kTimeStampKey, 1); | 310 dict->SetInteger(keys::kTimeStampKey, 1); |
256 args.Append(dict); | 311 args.Append(dict); |
257 | 312 |
258 std::string json_args; | 313 std::string json_args; |
259 base::JSONWriter::Write(&args, false, &json_args); | 314 base::JSONWriter::Write(&args, false, &json_args); |
260 | 315 |
261 // TODO(mpcomplete): Consider consolidating common (extension_id,json_args) | 316 // TODO(mpcomplete): Consider consolidating common (extension_id,json_args) |
262 // pairs into a single message sent to a list of sub_event_names. | 317 // pairs into a single message sent to a list of sub_event_names. |
263 int num_handlers_blocking = 0; | 318 int num_handlers_blocking = 0; |
264 for (std::vector<const EventListener*>::iterator it = listeners.begin(); | 319 for (std::vector<const EventListener*>::const_iterator it = listeners.begin(); |
265 it != listeners.end(); ++it) { | 320 it != listeners.end(); ++it) { |
266 event_router->DispatchEventToExtension( | 321 event_router->DispatchEventToExtension( |
267 (*it)->extension_id, (*it)->sub_event_name, json_args, | 322 (*it)->extension_id, (*it)->sub_event_name, json_args, |
268 profile_id, true, GURL()); | 323 profile_id, true, GURL()); |
269 if ((*it)->extra_info_spec & ExtraInfoSpec::BLOCKING) { | 324 if (callback && (*it)->extra_info_spec & ExtraInfoSpec::BLOCKING) { |
270 (*it)->blocked_requests.insert(request->identifier()); | 325 (*it)->blocked_requests.insert(request->identifier()); |
271 ++num_handlers_blocking; | 326 ++num_handlers_blocking; |
272 } | 327 } |
273 } | 328 } |
274 | 329 |
275 if (num_handlers_blocking > 0) { | 330 if (num_handlers_blocking > 0) { |
276 CHECK(blocked_requests_.find(request->identifier()) == | 331 CHECK(blocked_requests_.find(request->identifier()) == |
277 blocked_requests_.end()); | 332 blocked_requests_.end()); |
278 blocked_requests_[request->identifier()].num_handlers_blocking = | 333 blocked_requests_[request->identifier()].num_handlers_blocking = |
279 num_handlers_blocking; | 334 num_handlers_blocking; |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
454 | 509 |
455 BrowserThread::PostTask( | 510 BrowserThread::PostTask( |
456 BrowserThread::IO, FROM_HERE, | 511 BrowserThread::IO, FROM_HERE, |
457 NewRunnableFunction( | 512 NewRunnableFunction( |
458 &EventHandledOnIOThread, | 513 &EventHandledOnIOThread, |
459 profile()->GetRuntimeId(), extension_id(), | 514 profile()->GetRuntimeId(), extension_id(), |
460 event_name, sub_event_name, request_id, cancel)); | 515 event_name, sub_event_name, request_id, cancel)); |
461 | 516 |
462 return true; | 517 return true; |
463 } | 518 } |
OLD | NEW |