Chromium Code Reviews| 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 #include "chrome/browser/extensions/api/web_request/web_request_api.h" | 5 #include "chrome/browser/extensions/api/web_request/web_request_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 471 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 482 | 482 |
| 483 request_time_tracker_->LogRequestStartTime(request->identifier(), | 483 request_time_tracker_->LogRequestStartTime(request->identifier(), |
| 484 base::Time::Now(), | 484 base::Time::Now(), |
| 485 request->url(), | 485 request->url(), |
| 486 profile); | 486 profile); |
| 487 | 487 |
| 488 // Whether to initialized blocked_requests_. | 488 // Whether to initialized blocked_requests_. |
| 489 bool initialize_blocked_requests = false; | 489 bool initialize_blocked_requests = false; |
| 490 | 490 |
| 491 initialize_blocked_requests |= | 491 initialize_blocked_requests |= |
| 492 ProcessDeclarativeRules(request, extensions::ON_BEFORE_REQUEST, NULL); | 492 ProcessDeclarativeRules(profile, keys::kOnBeforeRequest, request, |
| 493 extensions::ON_BEFORE_REQUEST, NULL); | |
| 493 | 494 |
| 494 int extra_info_spec = 0; | 495 int extra_info_spec = 0; |
| 495 std::vector<const EventListener*> listeners = | 496 std::vector<const EventListener*> listeners = |
| 496 GetMatchingListeners(profile, extension_info_map, keys::kOnBeforeRequest, | 497 GetMatchingListeners(profile, extension_info_map, keys::kOnBeforeRequest, |
| 497 request, &extra_info_spec); | 498 request, &extra_info_spec); |
| 498 if (!listeners.empty() && | 499 if (!listeners.empty() && |
| 499 !GetAndSetSignaled(request->identifier(), kOnBeforeRequest)) { | 500 !GetAndSetSignaled(request->identifier(), kOnBeforeRequest)) { |
| 500 ListValue args; | 501 ListValue args; |
| 501 DictionaryValue* dict = new DictionaryValue(); | 502 DictionaryValue* dict = new DictionaryValue(); |
| 502 ExtractRequestInfo(request, dict); | 503 ExtractRequestInfo(request, dict); |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 530 net::URLRequest* request, | 531 net::URLRequest* request, |
| 531 const net::CompletionCallback& callback, | 532 const net::CompletionCallback& callback, |
| 532 net::HttpRequestHeaders* headers) { | 533 net::HttpRequestHeaders* headers) { |
| 533 // We hide events from the system context as well as sensitive requests. | 534 // We hide events from the system context as well as sensitive requests. |
| 534 if (!profile || helpers::HideRequestForURL(request->url())) | 535 if (!profile || helpers::HideRequestForURL(request->url())) |
| 535 return net::OK; | 536 return net::OK; |
| 536 | 537 |
| 537 bool initialize_blocked_requests = false; | 538 bool initialize_blocked_requests = false; |
| 538 | 539 |
| 539 initialize_blocked_requests |= | 540 initialize_blocked_requests |= |
| 540 ProcessDeclarativeRules(request, extensions::ON_BEFORE_SEND_HEADERS, | 541 ProcessDeclarativeRules(profile, keys::kOnBeforeSendHeaders, request, |
| 541 NULL); | 542 extensions::ON_BEFORE_SEND_HEADERS, NULL); |
| 542 | 543 |
| 543 int extra_info_spec = 0; | 544 int extra_info_spec = 0; |
| 544 std::vector<const EventListener*> listeners = | 545 std::vector<const EventListener*> listeners = |
| 545 GetMatchingListeners(profile, extension_info_map, | 546 GetMatchingListeners(profile, extension_info_map, |
| 546 keys::kOnBeforeSendHeaders, request, | 547 keys::kOnBeforeSendHeaders, request, |
| 547 &extra_info_spec); | 548 &extra_info_spec); |
| 548 if (!listeners.empty() && | 549 if (!listeners.empty() && |
| 549 !GetAndSetSignaled(request->identifier(), kOnBeforeSendHeaders)) { | 550 !GetAndSetSignaled(request->identifier(), kOnBeforeSendHeaders)) { |
| 550 ListValue args; | 551 ListValue args; |
| 551 DictionaryValue* dict = new DictionaryValue(); | 552 DictionaryValue* dict = new DictionaryValue(); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 614 const net::CompletionCallback& callback, | 615 const net::CompletionCallback& callback, |
| 615 net::HttpResponseHeaders* original_response_headers, | 616 net::HttpResponseHeaders* original_response_headers, |
| 616 scoped_refptr<net::HttpResponseHeaders>* override_response_headers) { | 617 scoped_refptr<net::HttpResponseHeaders>* override_response_headers) { |
| 617 // We hide events from the system context as well as sensitive requests. | 618 // We hide events from the system context as well as sensitive requests. |
| 618 if (!profile || helpers::HideRequestForURL(request->url())) | 619 if (!profile || helpers::HideRequestForURL(request->url())) |
| 619 return net::OK; | 620 return net::OK; |
| 620 | 621 |
| 621 bool initialize_blocked_requests = false; | 622 bool initialize_blocked_requests = false; |
| 622 | 623 |
| 623 initialize_blocked_requests |= | 624 initialize_blocked_requests |= |
| 624 ProcessDeclarativeRules(request, extensions::ON_HEADERS_RECEIVED, | 625 ProcessDeclarativeRules(profile, keys::kOnHeadersReceived, request, |
| 626 extensions::ON_HEADERS_RECEIVED, | |
| 625 original_response_headers); | 627 original_response_headers); |
| 626 | 628 |
| 627 int extra_info_spec = 0; | 629 int extra_info_spec = 0; |
| 628 std::vector<const EventListener*> listeners = | 630 std::vector<const EventListener*> listeners = |
| 629 GetMatchingListeners(profile, extension_info_map, | 631 GetMatchingListeners(profile, extension_info_map, |
| 630 keys::kOnHeadersReceived, request, | 632 keys::kOnHeadersReceived, request, |
| 631 &extra_info_spec); | 633 &extra_info_spec); |
| 632 | 634 |
| 633 if (!listeners.empty() && | 635 if (!listeners.empty() && |
| 634 !GetAndSetSignaled(request->identifier(), kOnHeadersReceived)) { | 636 !GetAndSetSignaled(request->identifier(), kOnHeadersReceived)) { |
| (...skipping 627 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1262 CHECK_GE(num_handlers_blocking, 0); | 1264 CHECK_GE(num_handlers_blocking, 0); |
| 1263 | 1265 |
| 1264 if (response) { | 1266 if (response) { |
| 1265 blocked_request.response_deltas.push_back( | 1267 blocked_request.response_deltas.push_back( |
| 1266 linked_ptr<helpers::EventResponseDelta>( | 1268 linked_ptr<helpers::EventResponseDelta>( |
| 1267 CalculateDelta(&blocked_request, response))); | 1269 CalculateDelta(&blocked_request, response))); |
| 1268 } | 1270 } |
| 1269 | 1271 |
| 1270 base::TimeDelta block_time = | 1272 base::TimeDelta block_time = |
| 1271 base::Time::Now() - blocked_request.blocking_time; | 1273 base::Time::Now() - blocked_request.blocking_time; |
| 1272 request_time_tracker_->IncrementExtensionBlockTime( | 1274 if (!extension_id.empty()) |
| 1273 extension_id, request_id, block_time); | 1275 request_time_tracker_->IncrementExtensionBlockTime( |
| 1276 extension_id, request_id, block_time); | |
|
battre
2012/06/15 22:56:49
should we add a UMA statistic to measure the netwo
Matt Perry
2012/06/18 21:01:01
Done.
| |
| 1274 | 1277 |
| 1275 if (num_handlers_blocking == 0) { | 1278 if (num_handlers_blocking == 0) { |
| 1276 ExecuteDeltas(profile, request_id, true); | 1279 ExecuteDeltas(profile, request_id, true); |
| 1277 } else { | 1280 } else { |
| 1278 // Update the URLRequest to indicate it is now blocked on a different | 1281 // Update the URLRequest to indicate it is now blocked on a different |
| 1279 // extension. | 1282 // extension. |
| 1280 std::set<EventListener>& listeners = listeners_[profile][event_name]; | 1283 std::set<EventListener>& listeners = listeners_[profile][event_name]; |
| 1281 | 1284 |
| 1282 for (std::set<EventListener>::iterator it = listeners.begin(); | 1285 for (std::set<EventListener>::iterator it = listeners.begin(); |
| 1283 it != listeners.end(); ++it) { | 1286 it != listeners.end(); ++it) { |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1392 blocked_requests_.erase(request_id); | 1395 blocked_requests_.erase(request_id); |
| 1393 if (call_callback) | 1396 if (call_callback) |
| 1394 callback.Run(response); | 1397 callback.Run(response); |
| 1395 } else { | 1398 } else { |
| 1396 blocked_requests_.erase(request_id); | 1399 blocked_requests_.erase(request_id); |
| 1397 } | 1400 } |
| 1398 return rv; | 1401 return rv; |
| 1399 } | 1402 } |
| 1400 | 1403 |
| 1401 bool ExtensionWebRequestEventRouter::ProcessDeclarativeRules( | 1404 bool ExtensionWebRequestEventRouter::ProcessDeclarativeRules( |
| 1405 void* profile, | |
| 1406 const std::string& event_name, | |
| 1402 net::URLRequest* request, | 1407 net::URLRequest* request, |
| 1403 extensions::RequestStages request_stage, | 1408 extensions::RequestStages request_stage, |
| 1404 net::HttpResponseHeaders* original_response_headers) { | 1409 net::HttpResponseHeaders* original_response_headers) { |
| 1405 if (!rules_registry_.get()) | 1410 if (!rules_registry_.get()) |
| 1406 return false; | 1411 return false; |
| 1407 | 1412 |
| 1413 if (!rules_registry_->IsReady()) { | |
|
battre
2012/06/15 22:56:49
nice! the architecture is really flexible.
Matt Perry
2012/06/18 21:01:01
Agreed, I was pleasantly surprised with how simple
| |
| 1414 // The rules registry is still loading. Block this request until it | |
| 1415 // finishes. | |
| 1416 EventResponse* response = NULL; | |
| 1417 rules_registry_->AddReadyCallback( | |
| 1418 base::Bind(&ExtensionWebRequestEventRouter::DecrementBlockCount, | |
| 1419 AsWeakPtr(), profile, std::string(), event_name, | |
| 1420 request->identifier(), response)); | |
| 1421 blocked_requests_[request->identifier()].num_handlers_blocking++; | |
|
Matt Perry
2012/06/18 21:01:01
BTW I had to add more initialization of blocked_re
| |
| 1422 return true; | |
| 1423 } | |
| 1424 | |
| 1408 base::Time start = base::Time::Now(); | 1425 base::Time start = base::Time::Now(); |
| 1409 | 1426 |
| 1410 // TODO(battre): Annotate deltas with extension IDs, so that we can | 1427 // TODO(battre): Annotate deltas with extension IDs, so that we can |
| 1411 // - Sort deltas by precedence | 1428 // - Sort deltas by precedence |
| 1412 // - Check whether extensions have host permissions. | 1429 // - Check whether extensions have host permissions. |
| 1413 extensions::WebRequestRule::OptionalRequestData optional_request_data; | 1430 extensions::WebRequestRule::OptionalRequestData optional_request_data; |
| 1414 optional_request_data.original_response_headers = | 1431 optional_request_data.original_response_headers = |
| 1415 original_response_headers; | 1432 original_response_headers; |
| 1416 std::list<linked_ptr<helpers::EventResponseDelta> > result = | 1433 std::list<linked_ptr<helpers::EventResponseDelta> > result = |
| 1417 rules_registry_->CreateDeltas(request, request_stage, | 1434 rules_registry_->CreateDeltas(request, request_stage, |
| (...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1751 } else if ((*it)->name().find("AdBlock") != std::string::npos) { | 1768 } else if ((*it)->name().find("AdBlock") != std::string::npos) { |
| 1752 adblock = true; | 1769 adblock = true; |
| 1753 } else { | 1770 } else { |
| 1754 other = true; | 1771 other = true; |
| 1755 } | 1772 } |
| 1756 } | 1773 } |
| 1757 } | 1774 } |
| 1758 | 1775 |
| 1759 host->Send(new ExtensionMsg_UsingWebRequestAPI(adblock, adblock_plus, other)); | 1776 host->Send(new ExtensionMsg_UsingWebRequestAPI(adblock, adblock_plus, other)); |
| 1760 } | 1777 } |
| OLD | NEW |