| 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 "extensions/browser/api/web_request/web_request_api_helpers.h" | 5 #include "extensions/browser/api/web_request/web_request_api_helpers.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 const base::Time& extension_install_time, | 429 const base::Time& extension_install_time, |
| 430 bool cancel, | 430 bool cancel, |
| 431 std::unique_ptr<net::AuthCredentials>* auth_credentials) { | 431 std::unique_ptr<net::AuthCredentials>* auth_credentials) { |
| 432 EventResponseDelta* result = | 432 EventResponseDelta* result = |
| 433 new EventResponseDelta(extension_id, extension_install_time); | 433 new EventResponseDelta(extension_id, extension_install_time); |
| 434 result->cancel = cancel; | 434 result->cancel = cancel; |
| 435 result->auth_credentials.swap(*auth_credentials); | 435 result->auth_credentials.swap(*auth_credentials); |
| 436 return result; | 436 return result; |
| 437 } | 437 } |
| 438 | 438 |
| 439 void MergeCancelOfResponses( | 439 void MergeCancelOfResponses(const EventResponseDeltas& deltas, |
| 440 const EventResponseDeltas& deltas, | 440 bool* canceled, |
| 441 bool* canceled, | 441 const net::NetLogWithSource* net_log) { |
| 442 const net::BoundNetLog* net_log) { | |
| 443 for (EventResponseDeltas::const_iterator i = deltas.begin(); | 442 for (EventResponseDeltas::const_iterator i = deltas.begin(); |
| 444 i != deltas.end(); ++i) { | 443 i != deltas.end(); ++i) { |
| 445 if ((*i)->cancel) { | 444 if ((*i)->cancel) { |
| 446 *canceled = true; | 445 *canceled = true; |
| 447 net_log->AddEvent(net::NetLogEventType::CHROME_EXTENSION_ABORTED_REQUEST, | 446 net_log->AddEvent(net::NetLogEventType::CHROME_EXTENSION_ABORTED_REQUEST, |
| 448 CreateNetLogExtensionIdCallback(i->get())); | 447 CreateNetLogExtensionIdCallback(i->get())); |
| 449 break; | 448 break; |
| 450 } | 449 } |
| 451 } | 450 } |
| 452 } | 451 } |
| 453 | 452 |
| 454 // Helper function for MergeRedirectUrlOfResponses() that allows ignoring | 453 // Helper function for MergeRedirectUrlOfResponses() that allows ignoring |
| 455 // all redirects but those to data:// urls and about:blank. This is important | 454 // all redirects but those to data:// urls and about:blank. This is important |
| 456 // to treat these URLs as "cancel urls", i.e. URLs that extensions redirect | 455 // to treat these URLs as "cancel urls", i.e. URLs that extensions redirect |
| 457 // to if they want to express that they want to cancel a request. This reduces | 456 // to if they want to express that they want to cancel a request. This reduces |
| 458 // the number of conflicts that we need to flag, as canceling is considered | 457 // the number of conflicts that we need to flag, as canceling is considered |
| 459 // a higher precedence operation that redirects. | 458 // a higher precedence operation that redirects. |
| 460 // Returns whether a redirect occurred. | 459 // Returns whether a redirect occurred. |
| 461 static bool MergeRedirectUrlOfResponsesHelper( | 460 static bool MergeRedirectUrlOfResponsesHelper( |
| 462 const EventResponseDeltas& deltas, | 461 const EventResponseDeltas& deltas, |
| 463 GURL* new_url, | 462 GURL* new_url, |
| 464 extensions::WarningSet* conflicting_extensions, | 463 extensions::WarningSet* conflicting_extensions, |
| 465 const net::BoundNetLog* net_log, | 464 const net::NetLogWithSource* net_log, |
| 466 bool consider_only_cancel_scheme_urls) { | 465 bool consider_only_cancel_scheme_urls) { |
| 467 bool redirected = false; | 466 bool redirected = false; |
| 468 | 467 |
| 469 // Extension that determines the |new_url|. | 468 // Extension that determines the |new_url|. |
| 470 std::string winning_extension_id; | 469 std::string winning_extension_id; |
| 471 EventResponseDeltas::const_iterator delta; | 470 EventResponseDeltas::const_iterator delta; |
| 472 for (delta = deltas.begin(); delta != deltas.end(); ++delta) { | 471 for (delta = deltas.begin(); delta != deltas.end(); ++delta) { |
| 473 if ((*delta)->new_url.is_empty()) | 472 if ((*delta)->new_url.is_empty()) |
| 474 continue; | 473 continue; |
| 475 if (consider_only_cancel_scheme_urls && | 474 if (consider_only_cancel_scheme_urls && |
| (...skipping 17 matching lines...) Expand all Loading... |
| 493 (*delta)->new_url, | 492 (*delta)->new_url, |
| 494 *new_url)); | 493 *new_url)); |
| 495 net_log->AddEvent( | 494 net_log->AddEvent( |
| 496 net::NetLogEventType::CHROME_EXTENSION_IGNORED_DUE_TO_CONFLICT, | 495 net::NetLogEventType::CHROME_EXTENSION_IGNORED_DUE_TO_CONFLICT, |
| 497 CreateNetLogExtensionIdCallback(delta->get())); | 496 CreateNetLogExtensionIdCallback(delta->get())); |
| 498 } | 497 } |
| 499 } | 498 } |
| 500 return redirected; | 499 return redirected; |
| 501 } | 500 } |
| 502 | 501 |
| 503 void MergeRedirectUrlOfResponses( | 502 void MergeRedirectUrlOfResponses(const EventResponseDeltas& deltas, |
| 504 const EventResponseDeltas& deltas, | 503 GURL* new_url, |
| 505 GURL* new_url, | 504 extensions::WarningSet* conflicting_extensions, |
| 506 extensions::WarningSet* conflicting_extensions, | 505 const net::NetLogWithSource* net_log) { |
| 507 const net::BoundNetLog* net_log) { | |
| 508 | |
| 509 // First handle only redirects to data:// URLs and about:blank. These are a | 506 // First handle only redirects to data:// URLs and about:blank. These are a |
| 510 // special case as they represent a way of cancelling a request. | 507 // special case as they represent a way of cancelling a request. |
| 511 if (MergeRedirectUrlOfResponsesHelper( | 508 if (MergeRedirectUrlOfResponsesHelper( |
| 512 deltas, new_url, conflicting_extensions, net_log, true)) { | 509 deltas, new_url, conflicting_extensions, net_log, true)) { |
| 513 // If any extension cancelled a request by redirecting to a data:// URL or | 510 // If any extension cancelled a request by redirecting to a data:// URL or |
| 514 // about:blank, we don't consider the other redirects. | 511 // about:blank, we don't consider the other redirects. |
| 515 return; | 512 return; |
| 516 } | 513 } |
| 517 | 514 |
| 518 // Handle all other redirects. | 515 // Handle all other redirects. |
| 519 MergeRedirectUrlOfResponsesHelper( | 516 MergeRedirectUrlOfResponsesHelper( |
| 520 deltas, new_url, conflicting_extensions, net_log, false); | 517 deltas, new_url, conflicting_extensions, net_log, false); |
| 521 } | 518 } |
| 522 | 519 |
| 523 void MergeOnBeforeRequestResponses( | 520 void MergeOnBeforeRequestResponses( |
| 524 const EventResponseDeltas& deltas, | 521 const EventResponseDeltas& deltas, |
| 525 GURL* new_url, | 522 GURL* new_url, |
| 526 extensions::WarningSet* conflicting_extensions, | 523 extensions::WarningSet* conflicting_extensions, |
| 527 const net::BoundNetLog* net_log) { | 524 const net::NetLogWithSource* net_log) { |
| 528 MergeRedirectUrlOfResponses(deltas, new_url, conflicting_extensions, net_log); | 525 MergeRedirectUrlOfResponses(deltas, new_url, conflicting_extensions, net_log); |
| 529 } | 526 } |
| 530 | 527 |
| 531 static bool DoesRequestCookieMatchFilter( | 528 static bool DoesRequestCookieMatchFilter( |
| 532 const ParsedRequestCookie& cookie, | 529 const ParsedRequestCookie& cookie, |
| 533 RequestCookie* filter) { | 530 RequestCookie* filter) { |
| 534 if (!filter) return true; | 531 if (!filter) return true; |
| 535 if (filter->name.get() && cookie.first != *filter->name) return false; | 532 if (filter->name.get() && cookie.first != *filter->name) return false; |
| 536 if (filter->value.get() && cookie.second != *filter->value) return false; | 533 if (filter->value.get() && cookie.second != *filter->value) return false; |
| 537 return true; | 534 return true; |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 643 } | 640 } |
| 644 } | 641 } |
| 645 } | 642 } |
| 646 return modified; | 643 return modified; |
| 647 } | 644 } |
| 648 | 645 |
| 649 void MergeCookiesInOnBeforeSendHeadersResponses( | 646 void MergeCookiesInOnBeforeSendHeadersResponses( |
| 650 const EventResponseDeltas& deltas, | 647 const EventResponseDeltas& deltas, |
| 651 net::HttpRequestHeaders* request_headers, | 648 net::HttpRequestHeaders* request_headers, |
| 652 extensions::WarningSet* conflicting_extensions, | 649 extensions::WarningSet* conflicting_extensions, |
| 653 const net::BoundNetLog* net_log) { | 650 const net::NetLogWithSource* net_log) { |
| 654 // Skip all work if there are no registered cookie modifications. | 651 // Skip all work if there are no registered cookie modifications. |
| 655 bool cookie_modifications_exist = false; | 652 bool cookie_modifications_exist = false; |
| 656 EventResponseDeltas::const_iterator delta; | 653 EventResponseDeltas::const_iterator delta; |
| 657 for (delta = deltas.begin(); delta != deltas.end(); ++delta) { | 654 for (delta = deltas.begin(); delta != deltas.end(); ++delta) { |
| 658 cookie_modifications_exist |= | 655 cookie_modifications_exist |= |
| 659 !(*delta)->request_cookie_modifications.empty(); | 656 !(*delta)->request_cookie_modifications.empty(); |
| 660 } | 657 } |
| 661 if (!cookie_modifications_exist) | 658 if (!cookie_modifications_exist) |
| 662 return; | 659 return; |
| 663 | 660 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 716 return (*delta)->extension_id; | 713 return (*delta)->extension_id; |
| 717 } | 714 } |
| 718 } | 715 } |
| 719 return std::string(); | 716 return std::string(); |
| 720 } | 717 } |
| 721 | 718 |
| 722 void MergeOnBeforeSendHeadersResponses( | 719 void MergeOnBeforeSendHeadersResponses( |
| 723 const EventResponseDeltas& deltas, | 720 const EventResponseDeltas& deltas, |
| 724 net::HttpRequestHeaders* request_headers, | 721 net::HttpRequestHeaders* request_headers, |
| 725 extensions::WarningSet* conflicting_extensions, | 722 extensions::WarningSet* conflicting_extensions, |
| 726 const net::BoundNetLog* net_log) { | 723 const net::NetLogWithSource* net_log) { |
| 727 EventResponseDeltas::const_iterator delta; | 724 EventResponseDeltas::const_iterator delta; |
| 728 | 725 |
| 729 // Here we collect which headers we have removed or set to new values | 726 // Here we collect which headers we have removed or set to new values |
| 730 // so far due to extensions of higher precedence. | 727 // so far due to extensions of higher precedence. |
| 731 std::set<std::string> removed_headers; | 728 std::set<std::string> removed_headers; |
| 732 std::set<std::string> set_headers; | 729 std::set<std::string> set_headers; |
| 733 | 730 |
| 734 // We assume here that the deltas are sorted in decreasing extension | 731 // We assume here that the deltas are sorted in decreasing extension |
| 735 // precedence (i.e. decreasing extension installation time). | 732 // precedence (i.e. decreasing extension installation time). |
| 736 for (delta = deltas.begin(); delta != deltas.end(); ++delta) { | 733 for (delta = deltas.begin(); delta != deltas.end(); ++delta) { |
| (...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1023 } | 1020 } |
| 1024 } | 1021 } |
| 1025 return modified; | 1022 return modified; |
| 1026 } | 1023 } |
| 1027 | 1024 |
| 1028 void MergeCookiesInOnHeadersReceivedResponses( | 1025 void MergeCookiesInOnHeadersReceivedResponses( |
| 1029 const EventResponseDeltas& deltas, | 1026 const EventResponseDeltas& deltas, |
| 1030 const net::HttpResponseHeaders* original_response_headers, | 1027 const net::HttpResponseHeaders* original_response_headers, |
| 1031 scoped_refptr<net::HttpResponseHeaders>* override_response_headers, | 1028 scoped_refptr<net::HttpResponseHeaders>* override_response_headers, |
| 1032 extensions::WarningSet* conflicting_extensions, | 1029 extensions::WarningSet* conflicting_extensions, |
| 1033 const net::BoundNetLog* net_log) { | 1030 const net::NetLogWithSource* net_log) { |
| 1034 // Skip all work if there are no registered cookie modifications. | 1031 // Skip all work if there are no registered cookie modifications. |
| 1035 bool cookie_modifications_exist = false; | 1032 bool cookie_modifications_exist = false; |
| 1036 EventResponseDeltas::const_reverse_iterator delta; | 1033 EventResponseDeltas::const_reverse_iterator delta; |
| 1037 for (delta = deltas.rbegin(); delta != deltas.rend(); ++delta) { | 1034 for (delta = deltas.rbegin(); delta != deltas.rend(); ++delta) { |
| 1038 cookie_modifications_exist |= | 1035 cookie_modifications_exist |= |
| 1039 !(*delta)->response_cookie_modifications.empty(); | 1036 !(*delta)->response_cookie_modifications.empty(); |
| 1040 } | 1037 } |
| 1041 if (!cookie_modifications_exist) | 1038 if (!cookie_modifications_exist) |
| 1042 return; | 1039 return; |
| 1043 | 1040 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1079 } | 1076 } |
| 1080 return std::string(); | 1077 return std::string(); |
| 1081 } | 1078 } |
| 1082 | 1079 |
| 1083 void MergeOnHeadersReceivedResponses( | 1080 void MergeOnHeadersReceivedResponses( |
| 1084 const EventResponseDeltas& deltas, | 1081 const EventResponseDeltas& deltas, |
| 1085 const net::HttpResponseHeaders* original_response_headers, | 1082 const net::HttpResponseHeaders* original_response_headers, |
| 1086 scoped_refptr<net::HttpResponseHeaders>* override_response_headers, | 1083 scoped_refptr<net::HttpResponseHeaders>* override_response_headers, |
| 1087 GURL* allowed_unsafe_redirect_url, | 1084 GURL* allowed_unsafe_redirect_url, |
| 1088 extensions::WarningSet* conflicting_extensions, | 1085 extensions::WarningSet* conflicting_extensions, |
| 1089 const net::BoundNetLog* net_log) { | 1086 const net::NetLogWithSource* net_log) { |
| 1090 EventResponseDeltas::const_iterator delta; | 1087 EventResponseDeltas::const_iterator delta; |
| 1091 | 1088 |
| 1092 // Here we collect which headers we have removed or added so far due to | 1089 // Here we collect which headers we have removed or added so far due to |
| 1093 // extensions of higher precedence. Header keys are always stored as | 1090 // extensions of higher precedence. Header keys are always stored as |
| 1094 // lower case. | 1091 // lower case. |
| 1095 std::set<ResponseHeader> removed_headers; | 1092 std::set<ResponseHeader> removed_headers; |
| 1096 std::set<ResponseHeader> added_headers; | 1093 std::set<ResponseHeader> added_headers; |
| 1097 | 1094 |
| 1098 // We assume here that the deltas are sorted in decreasing extension | 1095 // We assume here that the deltas are sorted in decreasing extension |
| 1099 // precedence (i.e. decreasing extension installation time). | 1096 // precedence (i.e. decreasing extension installation time). |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1181 // Explicitly mark the URL as safe for redirection, to prevent the request | 1178 // Explicitly mark the URL as safe for redirection, to prevent the request |
| 1182 // from being blocked because of net::ERR_UNSAFE_REDIRECT. | 1179 // from being blocked because of net::ERR_UNSAFE_REDIRECT. |
| 1183 *allowed_unsafe_redirect_url = new_url; | 1180 *allowed_unsafe_redirect_url = new_url; |
| 1184 } | 1181 } |
| 1185 } | 1182 } |
| 1186 | 1183 |
| 1187 bool MergeOnAuthRequiredResponses( | 1184 bool MergeOnAuthRequiredResponses( |
| 1188 const EventResponseDeltas& deltas, | 1185 const EventResponseDeltas& deltas, |
| 1189 net::AuthCredentials* auth_credentials, | 1186 net::AuthCredentials* auth_credentials, |
| 1190 extensions::WarningSet* conflicting_extensions, | 1187 extensions::WarningSet* conflicting_extensions, |
| 1191 const net::BoundNetLog* net_log) { | 1188 const net::NetLogWithSource* net_log) { |
| 1192 CHECK(auth_credentials); | 1189 CHECK(auth_credentials); |
| 1193 bool credentials_set = false; | 1190 bool credentials_set = false; |
| 1194 std::string winning_extension_id; | 1191 std::string winning_extension_id; |
| 1195 | 1192 |
| 1196 for (EventResponseDeltas::const_iterator delta = deltas.begin(); | 1193 for (EventResponseDeltas::const_iterator delta = deltas.begin(); |
| 1197 delta != deltas.end(); | 1194 delta != deltas.end(); |
| 1198 ++delta) { | 1195 ++delta) { |
| 1199 if (!(*delta)->auth_credentials.get()) | 1196 if (!(*delta)->auth_credentials.get()) |
| 1200 continue; | 1197 continue; |
| 1201 bool different = | 1198 bool different = |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1315 for (size_t i = 0; i < kResourceTypeStringsLength; ++i) { | 1312 for (size_t i = 0; i < kResourceTypeStringsLength; ++i) { |
| 1316 if (type_str == kResourceTypeStrings[i]) { | 1313 if (type_str == kResourceTypeStrings[i]) { |
| 1317 found = true; | 1314 found = true; |
| 1318 types->push_back(kResourceTypeValues[i]); | 1315 types->push_back(kResourceTypeValues[i]); |
| 1319 } | 1316 } |
| 1320 } | 1317 } |
| 1321 return found; | 1318 return found; |
| 1322 } | 1319 } |
| 1323 | 1320 |
| 1324 } // namespace extension_web_request_api_helpers | 1321 } // namespace extension_web_request_api_helpers |
| OLD | NEW |