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/bind.h" | 9 #include "base/bind.h" |
10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
(...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 EventTypes event; | 340 EventTypes event; |
341 | 341 |
342 // The number of event handlers that we are awaiting a response from. | 342 // The number of event handlers that we are awaiting a response from. |
343 int num_handlers_blocking; | 343 int num_handlers_blocking; |
344 | 344 |
345 // Pointer to NetLog to report significant changes to the request for | 345 // Pointer to NetLog to report significant changes to the request for |
346 // debugging. | 346 // debugging. |
347 const net::BoundNetLog* net_log; | 347 const net::BoundNetLog* net_log; |
348 | 348 |
349 // The callback to call when we get a response from all event handlers. | 349 // The callback to call when we get a response from all event handlers. |
350 net::OldCompletionCallback* callback; | 350 net::CompletionCallback callback; |
351 | 351 |
352 // If non-empty, this contains the new URL that the request will redirect to. | 352 // If non-empty, this contains the new URL that the request will redirect to. |
353 // Only valid for OnBeforeRequest. | 353 // Only valid for OnBeforeRequest. |
354 GURL* new_url; | 354 GURL* new_url; |
355 | 355 |
356 // The request headers that will be issued along with this request. Only valid | 356 // The request headers that will be issued along with this request. Only valid |
357 // for OnBeforeSendHeaders. | 357 // for OnBeforeSendHeaders. |
358 net::HttpRequestHeaders* request_headers; | 358 net::HttpRequestHeaders* request_headers; |
359 | 359 |
360 // The response headers that were received from the server. Only valid for | 360 // The response headers that were received from the server. Only valid for |
(...skipping 17 matching lines...) Expand all Loading... |
378 base::Time blocking_time; | 378 base::Time blocking_time; |
379 | 379 |
380 // Changes requested by extensions. | 380 // Changes requested by extensions. |
381 helpers::EventResponseDeltas response_deltas; | 381 helpers::EventResponseDeltas response_deltas; |
382 | 382 |
383 BlockedRequest() | 383 BlockedRequest() |
384 : request(NULL), | 384 : request(NULL), |
385 event(kInvalidEvent), | 385 event(kInvalidEvent), |
386 num_handlers_blocking(0), | 386 num_handlers_blocking(0), |
387 net_log(NULL), | 387 net_log(NULL), |
388 callback(NULL), | |
389 new_url(NULL), | 388 new_url(NULL), |
390 request_headers(NULL), | 389 request_headers(NULL), |
391 override_response_headers(NULL), | 390 override_response_headers(NULL), |
392 auth_credentials(NULL) {} | 391 auth_credentials(NULL) {} |
393 }; | 392 }; |
394 | 393 |
395 bool ExtensionWebRequestEventRouter::RequestFilter::InitFromValue( | 394 bool ExtensionWebRequestEventRouter::RequestFilter::InitFromValue( |
396 const DictionaryValue& value, std::string* error) { | 395 const DictionaryValue& value, std::string* error) { |
397 for (DictionaryValue::key_iterator key = value.begin_keys(); | 396 for (DictionaryValue::key_iterator key = value.begin_keys(); |
398 key != value.end_keys(); ++key) { | 397 key != value.end_keys(); ++key) { |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
496 : request_time_tracker_(new ExtensionWebRequestTimeTracker) { | 495 : request_time_tracker_(new ExtensionWebRequestTimeTracker) { |
497 } | 496 } |
498 | 497 |
499 ExtensionWebRequestEventRouter::~ExtensionWebRequestEventRouter() { | 498 ExtensionWebRequestEventRouter::~ExtensionWebRequestEventRouter() { |
500 } | 499 } |
501 | 500 |
502 int ExtensionWebRequestEventRouter::OnBeforeRequest( | 501 int ExtensionWebRequestEventRouter::OnBeforeRequest( |
503 void* profile, | 502 void* profile, |
504 ExtensionInfoMap* extension_info_map, | 503 ExtensionInfoMap* extension_info_map, |
505 net::URLRequest* request, | 504 net::URLRequest* request, |
506 net::OldCompletionCallback* callback, | 505 const net::CompletionCallback& callback, |
507 GURL* new_url) { | 506 GURL* new_url) { |
508 // TODO(jochen): Figure out what to do with events from the system context. | 507 // TODO(jochen): Figure out what to do with events from the system context. |
509 if (!profile) | 508 if (!profile) |
510 return net::OK; | 509 return net::OK; |
511 | 510 |
512 if (IsPageLoad(request)) | 511 if (IsPageLoad(request)) |
513 NotifyPageLoad(); | 512 NotifyPageLoad(); |
514 | 513 |
515 if (!HasWebRequestScheme(request->url())) | 514 if (!HasWebRequestScheme(request->url())) |
516 return net::OK; | 515 return net::OK; |
(...skipping 25 matching lines...) Expand all Loading... |
542 blocked_requests_[request->identifier()].net_log = &request->net_log(); | 541 blocked_requests_[request->identifier()].net_log = &request->net_log(); |
543 return net::ERR_IO_PENDING; | 542 return net::ERR_IO_PENDING; |
544 } | 543 } |
545 return net::OK; | 544 return net::OK; |
546 } | 545 } |
547 | 546 |
548 int ExtensionWebRequestEventRouter::OnBeforeSendHeaders( | 547 int ExtensionWebRequestEventRouter::OnBeforeSendHeaders( |
549 void* profile, | 548 void* profile, |
550 ExtensionInfoMap* extension_info_map, | 549 ExtensionInfoMap* extension_info_map, |
551 net::URLRequest* request, | 550 net::URLRequest* request, |
552 net::OldCompletionCallback* callback, | 551 const net::CompletionCallback& callback, |
553 net::HttpRequestHeaders* headers) { | 552 net::HttpRequestHeaders* headers) { |
554 // TODO(jochen): Figure out what to do with events from the system context. | 553 // TODO(jochen): Figure out what to do with events from the system context. |
555 if (!profile) | 554 if (!profile) |
556 return net::OK; | 555 return net::OK; |
557 | 556 |
558 if (!HasWebRequestScheme(request->url())) | 557 if (!HasWebRequestScheme(request->url())) |
559 return net::OK; | 558 return net::OK; |
560 | 559 |
561 if (GetAndSetSignaled(request->identifier(), kOnBeforeSendHeaders)) | 560 if (GetAndSetSignaled(request->identifier(), kOnBeforeSendHeaders)) |
562 return net::OK; | 561 return net::OK; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
616 dict->Set(keys::kRequestHeadersKey, GetRequestHeadersList(headers)); | 615 dict->Set(keys::kRequestHeadersKey, GetRequestHeadersList(headers)); |
617 args.Append(dict); | 616 args.Append(dict); |
618 | 617 |
619 DispatchEvent(profile, request, listeners, args); | 618 DispatchEvent(profile, request, listeners, args); |
620 } | 619 } |
621 | 620 |
622 int ExtensionWebRequestEventRouter::OnHeadersReceived( | 621 int ExtensionWebRequestEventRouter::OnHeadersReceived( |
623 void* profile, | 622 void* profile, |
624 ExtensionInfoMap* extension_info_map, | 623 ExtensionInfoMap* extension_info_map, |
625 net::URLRequest* request, | 624 net::URLRequest* request, |
626 net::OldCompletionCallback* callback, | 625 const net::CompletionCallback& callback, |
627 net::HttpResponseHeaders* original_response_headers, | 626 net::HttpResponseHeaders* original_response_headers, |
628 scoped_refptr<net::HttpResponseHeaders>* override_response_headers) { | 627 scoped_refptr<net::HttpResponseHeaders>* override_response_headers) { |
629 if (!profile) | 628 if (!profile) |
630 return net::OK; | 629 return net::OK; |
631 | 630 |
632 if (!HasWebRequestScheme(request->url())) | 631 if (!HasWebRequestScheme(request->url())) |
633 return net::OK; | 632 return net::OK; |
634 | 633 |
635 int extra_info_spec = 0; | 634 int extra_info_spec = 0; |
636 std::vector<const EventListener*> listeners = | 635 std::vector<const EventListener*> listeners = |
(...skipping 617 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1254 std::set<std::string> conflicting_extensions; | 1253 std::set<std::string> conflicting_extensions; |
1255 helpers::EventLogEntries event_log_entries; | 1254 helpers::EventLogEntries event_log_entries; |
1256 | 1255 |
1257 bool canceled = false; | 1256 bool canceled = false; |
1258 helpers::MergeCancelOfResponses( | 1257 helpers::MergeCancelOfResponses( |
1259 blocked_request.response_deltas, | 1258 blocked_request.response_deltas, |
1260 &canceled, | 1259 &canceled, |
1261 &event_log_entries); | 1260 &event_log_entries); |
1262 | 1261 |
1263 if (blocked_request.event == kOnBeforeRequest) { | 1262 if (blocked_request.event == kOnBeforeRequest) { |
1264 CHECK(blocked_request.callback); | 1263 CHECK(!blocked_request.callback.is_null()); |
1265 helpers::MergeOnBeforeRequestResponses( | 1264 helpers::MergeOnBeforeRequestResponses( |
1266 blocked_request.response_deltas, | 1265 blocked_request.response_deltas, |
1267 blocked_request.new_url, | 1266 blocked_request.new_url, |
1268 &conflicting_extensions, | 1267 &conflicting_extensions, |
1269 &event_log_entries); | 1268 &event_log_entries); |
1270 } else if (blocked_request.event == kOnBeforeSendHeaders) { | 1269 } else if (blocked_request.event == kOnBeforeSendHeaders) { |
1271 CHECK(blocked_request.callback); | 1270 CHECK(!blocked_request.callback.is_null()); |
1272 helpers::MergeOnBeforeSendHeadersResponses( | 1271 helpers::MergeOnBeforeSendHeadersResponses( |
1273 blocked_request.response_deltas, | 1272 blocked_request.response_deltas, |
1274 blocked_request.request_headers, | 1273 blocked_request.request_headers, |
1275 &conflicting_extensions, | 1274 &conflicting_extensions, |
1276 &event_log_entries); | 1275 &event_log_entries); |
1277 } else if (blocked_request.event == kOnHeadersReceived) { | 1276 } else if (blocked_request.event == kOnHeadersReceived) { |
1278 CHECK(blocked_request.callback); | 1277 CHECK(!blocked_request.callback.is_null()); |
1279 helpers::MergeOnHeadersReceivedResponses( | 1278 helpers::MergeOnHeadersReceivedResponses( |
1280 blocked_request.response_deltas, | 1279 blocked_request.response_deltas, |
1281 blocked_request.original_response_headers.get(), | 1280 blocked_request.original_response_headers.get(), |
1282 blocked_request.override_response_headers, | 1281 blocked_request.override_response_headers, |
1283 &conflicting_extensions, | 1282 &conflicting_extensions, |
1284 &event_log_entries); | 1283 &event_log_entries); |
1285 } else if (blocked_request.event == kOnAuthRequired) { | 1284 } else if (blocked_request.event == kOnAuthRequired) { |
1286 CHECK(!blocked_request.callback); | 1285 CHECK(blocked_request.callback.is_null()); |
1287 CHECK(!blocked_request.auth_callback.is_null()); | 1286 CHECK(!blocked_request.auth_callback.is_null()); |
1288 credentials_set = helpers::MergeOnAuthRequiredResponses( | 1287 credentials_set = helpers::MergeOnAuthRequiredResponses( |
1289 blocked_request.response_deltas, | 1288 blocked_request.response_deltas, |
1290 blocked_request.auth_credentials, | 1289 blocked_request.auth_credentials, |
1291 &conflicting_extensions, | 1290 &conflicting_extensions, |
1292 &event_log_entries); | 1291 &event_log_entries); |
1293 } else { | 1292 } else { |
1294 NOTREACHED(); | 1293 NOTREACHED(); |
1295 } | 1294 } |
1296 | 1295 |
(...skipping 12 matching lines...) Expand all Loading... |
1309 ExtensionWarningSet::kNetworkConflict)); | 1308 ExtensionWarningSet::kNetworkConflict)); |
1310 } | 1309 } |
1311 | 1310 |
1312 if (canceled) { | 1311 if (canceled) { |
1313 request_time_tracker_->SetRequestCanceled(request_id); | 1312 request_time_tracker_->SetRequestCanceled(request_id); |
1314 } else if (blocked_request.new_url && | 1313 } else if (blocked_request.new_url && |
1315 !blocked_request.new_url->is_empty()) { | 1314 !blocked_request.new_url->is_empty()) { |
1316 request_time_tracker_->SetRequestRedirected(request_id); | 1315 request_time_tracker_->SetRequestRedirected(request_id); |
1317 } | 1316 } |
1318 | 1317 |
1319 if (blocked_request.callback) { | 1318 if (!blocked_request.callback.is_null()) { |
1320 // This triggers onErrorOccurred. | 1319 // This triggers onErrorOccurred. |
1321 int rv = canceled ? net::ERR_BLOCKED_BY_CLIENT : net::OK; | 1320 int rv = canceled ? net::ERR_BLOCKED_BY_CLIENT : net::OK; |
1322 net::OldCompletionCallback* callback = blocked_request.callback; | 1321 net::CompletionCallback callback = blocked_request.callback; |
1323 // Ensure that request is removed before callback because the callback | 1322 // Ensure that request is removed before callback because the callback |
1324 // might trigger the next event. | 1323 // might trigger the next event. |
1325 blocked_requests_.erase(request_id); | 1324 blocked_requests_.erase(request_id); |
1326 callback->Run(rv); | 1325 callback.Run(rv); |
1327 } else if (!blocked_request.auth_callback.is_null()) { | 1326 } else if (!blocked_request.auth_callback.is_null()) { |
1328 net::NetworkDelegate::AuthRequiredResponse response = | 1327 net::NetworkDelegate::AuthRequiredResponse response = |
1329 net::NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION; | 1328 net::NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION; |
1330 if (canceled) { | 1329 if (canceled) { |
1331 response = net::NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH; | 1330 response = net::NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH; |
1332 } else if (credentials_set) { | 1331 } else if (credentials_set) { |
1333 response = net::NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH; | 1332 response = net::NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH; |
1334 } | 1333 } |
1335 net::NetworkDelegate::AuthCallback callback = | 1334 net::NetworkDelegate::AuthCallback callback = |
1336 blocked_request.auth_callback; | 1335 blocked_request.auth_callback; |
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1662 } else if ((*it)->name().find("AdBlock") != std::string::npos) { | 1661 } else if ((*it)->name().find("AdBlock") != std::string::npos) { |
1663 adblock = true; | 1662 adblock = true; |
1664 } else { | 1663 } else { |
1665 other = true; | 1664 other = true; |
1666 } | 1665 } |
1667 } | 1666 } |
1668 } | 1667 } |
1669 | 1668 |
1670 host->Send(new ExtensionMsg_UsingWebRequestAPI(adblock, adblock_plus, other)); | 1669 host->Send(new ExtensionMsg_UsingWebRequestAPI(adblock, adblock_plus, other)); |
1671 } | 1670 } |
OLD | NEW |