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/declarative_webrequest/webrequest_condit
ion_attribute.h" | 5 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit
ion_attribute.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
11 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "chrome/browser/extensions/api/declarative_webrequest/request_stage.h" | 13 #include "chrome/browser/extensions/api/declarative_webrequest/request_stage.h" |
| 14 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit
ion.h" |
14 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta
nts.h" | 15 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta
nts.h" |
15 #include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h" | 16 #include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h" |
16 #include "content/public/browser/resource_request_info.h" | 17 #include "content/public/browser/resource_request_info.h" |
17 #include "extensions/common/error_utils.h" | 18 #include "extensions/common/error_utils.h" |
18 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
19 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 20 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
20 #include "net/base/static_cookie_policy.h" | 21 #include "net/base/static_cookie_policy.h" |
21 #include "net/http/http_request_headers.h" | 22 #include "net/http/http_request_headers.h" |
22 #include "net/http/http_util.h" | 23 #include "net/http/http_util.h" |
23 #include "net/url_request/url_request.h" | 24 #include "net/url_request/url_request.h" |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 new WebRequestConditionAttributeResourceType(passed_types)); | 145 new WebRequestConditionAttributeResourceType(passed_types)); |
145 } | 146 } |
146 | 147 |
147 int WebRequestConditionAttributeResourceType::GetStages() const { | 148 int WebRequestConditionAttributeResourceType::GetStages() const { |
148 return ON_BEFORE_REQUEST | ON_BEFORE_SEND_HEADERS | ON_SEND_HEADERS | | 149 return ON_BEFORE_REQUEST | ON_BEFORE_SEND_HEADERS | ON_SEND_HEADERS | |
149 ON_HEADERS_RECEIVED | ON_AUTH_REQUIRED | ON_BEFORE_REDIRECT | | 150 ON_HEADERS_RECEIVED | ON_AUTH_REQUIRED | ON_BEFORE_REDIRECT | |
150 ON_RESPONSE_STARTED | ON_COMPLETED | ON_ERROR; | 151 ON_RESPONSE_STARTED | ON_COMPLETED | ON_ERROR; |
151 } | 152 } |
152 | 153 |
153 bool WebRequestConditionAttributeResourceType::IsFulfilled( | 154 bool WebRequestConditionAttributeResourceType::IsFulfilled( |
154 const WebRequestRule::RequestData& request_data) const { | 155 const DeclarativeWebRequestData& request_data) const { |
155 if (!(request_data.stage & GetStages())) | 156 if (!(request_data.stage & GetStages())) |
156 return false; | 157 return false; |
157 const content::ResourceRequestInfo* info = | 158 const content::ResourceRequestInfo* info = |
158 content::ResourceRequestInfo::ForRequest(request_data.request); | 159 content::ResourceRequestInfo::ForRequest(request_data.request); |
159 if (!info) | 160 if (!info) |
160 return false; | 161 return false; |
161 return std::find(types_.begin(), types_.end(), info->GetResourceType()) != | 162 return std::find(types_.begin(), types_.end(), info->GetResourceType()) != |
162 types_.end(); | 163 types_.end(); |
163 } | 164 } |
164 | 165 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 return scoped_ptr<WebRequestConditionAttribute>( | 216 return scoped_ptr<WebRequestConditionAttribute>( |
216 new WebRequestConditionAttributeContentType( | 217 new WebRequestConditionAttributeContentType( |
217 content_types, name == keys::kContentTypeKey)); | 218 content_types, name == keys::kContentTypeKey)); |
218 } | 219 } |
219 | 220 |
220 int WebRequestConditionAttributeContentType::GetStages() const { | 221 int WebRequestConditionAttributeContentType::GetStages() const { |
221 return ON_HEADERS_RECEIVED; | 222 return ON_HEADERS_RECEIVED; |
222 } | 223 } |
223 | 224 |
224 bool WebRequestConditionAttributeContentType::IsFulfilled( | 225 bool WebRequestConditionAttributeContentType::IsFulfilled( |
225 const WebRequestRule::RequestData& request_data) const { | 226 const DeclarativeWebRequestData& request_data) const { |
226 if (!(request_data.stage & GetStages())) | 227 if (!(request_data.stage & GetStages())) |
227 return false; | 228 return false; |
228 std::string content_type; | 229 std::string content_type; |
229 request_data.original_response_headers->GetNormalizedHeader( | 230 request_data.original_response_headers->GetNormalizedHeader( |
230 net::HttpRequestHeaders::kContentType, &content_type); | 231 net::HttpRequestHeaders::kContentType, &content_type); |
231 std::string mime_type; | 232 std::string mime_type; |
232 std::string charset; | 233 std::string charset; |
233 bool had_charset = false; | 234 bool had_charset = false; |
234 net::HttpUtil::ParseContentType( | 235 net::HttpUtil::ParseContentType( |
235 content_type, &mime_type, &charset, &had_charset, NULL); | 236 content_type, &mime_type, &charset, &had_charset, NULL); |
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
560 | 561 |
561 int WebRequestConditionAttributeRequestHeaders::GetStages() const { | 562 int WebRequestConditionAttributeRequestHeaders::GetStages() const { |
562 // Currently we only allow matching against headers in the before-send-headers | 563 // Currently we only allow matching against headers in the before-send-headers |
563 // stage. The headers are accessible in other stages as well, but before | 564 // stage. The headers are accessible in other stages as well, but before |
564 // allowing to match against them in further stages, we should consider | 565 // allowing to match against them in further stages, we should consider |
565 // caching the match result. | 566 // caching the match result. |
566 return ON_BEFORE_SEND_HEADERS; | 567 return ON_BEFORE_SEND_HEADERS; |
567 } | 568 } |
568 | 569 |
569 bool WebRequestConditionAttributeRequestHeaders::IsFulfilled( | 570 bool WebRequestConditionAttributeRequestHeaders::IsFulfilled( |
570 const WebRequestRule::RequestData& request_data) const { | 571 const DeclarativeWebRequestData& request_data) const { |
571 if (!(request_data.stage & GetStages())) | 572 if (!(request_data.stage & GetStages())) |
572 return false; | 573 return false; |
573 | 574 |
574 const net::HttpRequestHeaders& headers = | 575 const net::HttpRequestHeaders& headers = |
575 request_data.request->extra_request_headers(); | 576 request_data.request->extra_request_headers(); |
576 | 577 |
577 bool passed = false; // Did some header pass TestNameValue? | 578 bool passed = false; // Did some header pass TestNameValue? |
578 net::HttpRequestHeaders::Iterator it(headers); | 579 net::HttpRequestHeaders::Iterator it(headers); |
579 while (!passed && it.GetNext()) | 580 while (!passed && it.GetNext()) |
580 passed |= header_matcher_->TestNameValue(it.name(), it.value()); | 581 passed |= header_matcher_->TestNameValue(it.name(), it.value()); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
624 return scoped_ptr<WebRequestConditionAttribute>( | 625 return scoped_ptr<WebRequestConditionAttribute>( |
625 new WebRequestConditionAttributeResponseHeaders( | 626 new WebRequestConditionAttributeResponseHeaders( |
626 header_matcher.Pass(), name == keys::kResponseHeadersKey)); | 627 header_matcher.Pass(), name == keys::kResponseHeadersKey)); |
627 } | 628 } |
628 | 629 |
629 int WebRequestConditionAttributeResponseHeaders::GetStages() const { | 630 int WebRequestConditionAttributeResponseHeaders::GetStages() const { |
630 return ON_HEADERS_RECEIVED; | 631 return ON_HEADERS_RECEIVED; |
631 } | 632 } |
632 | 633 |
633 bool WebRequestConditionAttributeResponseHeaders::IsFulfilled( | 634 bool WebRequestConditionAttributeResponseHeaders::IsFulfilled( |
634 const WebRequestRule::RequestData& request_data) const { | 635 const DeclarativeWebRequestData& request_data) const { |
635 if (!(request_data.stage & GetStages())) | 636 if (!(request_data.stage & GetStages())) |
636 return false; | 637 return false; |
637 | 638 |
638 const net::HttpResponseHeaders* headers = | 639 const net::HttpResponseHeaders* headers = |
639 request_data.original_response_headers; | 640 request_data.original_response_headers; |
640 if (headers == NULL) { | 641 if (headers == NULL) { |
641 // Each header of an empty set satisfies (the negation of) everything; | 642 // Each header of an empty set satisfies (the negation of) everything; |
642 // OTOH, there is no header to satisfy even the most permissive test. | 643 // OTOH, there is no header to satisfy even the most permissive test. |
643 return !positive_; | 644 return !positive_; |
644 } | 645 } |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
695 new WebRequestConditionAttributeThirdParty(third_party)); | 696 new WebRequestConditionAttributeThirdParty(third_party)); |
696 } | 697 } |
697 | 698 |
698 int WebRequestConditionAttributeThirdParty::GetStages() const { | 699 int WebRequestConditionAttributeThirdParty::GetStages() const { |
699 return ON_BEFORE_REQUEST | ON_BEFORE_SEND_HEADERS | ON_SEND_HEADERS | | 700 return ON_BEFORE_REQUEST | ON_BEFORE_SEND_HEADERS | ON_SEND_HEADERS | |
700 ON_HEADERS_RECEIVED | ON_AUTH_REQUIRED | ON_BEFORE_REDIRECT | | 701 ON_HEADERS_RECEIVED | ON_AUTH_REQUIRED | ON_BEFORE_REDIRECT | |
701 ON_RESPONSE_STARTED | ON_COMPLETED | ON_ERROR; | 702 ON_RESPONSE_STARTED | ON_COMPLETED | ON_ERROR; |
702 } | 703 } |
703 | 704 |
704 bool WebRequestConditionAttributeThirdParty::IsFulfilled( | 705 bool WebRequestConditionAttributeThirdParty::IsFulfilled( |
705 const WebRequestRule::RequestData& request_data) const { | 706 const DeclarativeWebRequestData& request_data) const { |
706 if (!(request_data.stage & GetStages())) | 707 if (!(request_data.stage & GetStages())) |
707 return false; | 708 return false; |
708 | 709 |
709 // Request is "1st party" if it gets cookies under 3rd party-blocking policy. | 710 // Request is "1st party" if it gets cookies under 3rd party-blocking policy. |
710 const net::StaticCookiePolicy block_third_party_policy( | 711 const net::StaticCookiePolicy block_third_party_policy( |
711 net::StaticCookiePolicy::BLOCK_ALL_THIRD_PARTY_COOKIES); | 712 net::StaticCookiePolicy::BLOCK_ALL_THIRD_PARTY_COOKIES); |
712 const int can_get_cookies = block_third_party_policy.CanGetCookies( | 713 const int can_get_cookies = block_third_party_policy.CanGetCookies( |
713 request_data.request->url(), | 714 request_data.request->url(), |
714 request_data.request->first_party_for_cookies()); | 715 request_data.request->first_party_for_cookies()); |
715 const bool is_first_party = (can_get_cookies == net::OK); | 716 const bool is_first_party = (can_get_cookies == net::OK); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
790 | 791 |
791 return scoped_ptr<WebRequestConditionAttribute>( | 792 return scoped_ptr<WebRequestConditionAttribute>( |
792 new WebRequestConditionAttributeStages(allowed_stages)); | 793 new WebRequestConditionAttributeStages(allowed_stages)); |
793 } | 794 } |
794 | 795 |
795 int WebRequestConditionAttributeStages::GetStages() const { | 796 int WebRequestConditionAttributeStages::GetStages() const { |
796 return allowed_stages_; | 797 return allowed_stages_; |
797 } | 798 } |
798 | 799 |
799 bool WebRequestConditionAttributeStages::IsFulfilled( | 800 bool WebRequestConditionAttributeStages::IsFulfilled( |
800 const WebRequestRule::RequestData& request_data) const { | 801 const DeclarativeWebRequestData& request_data) const { |
801 // Note: removing '!=' triggers warning C4800 on the VS compiler. | 802 // Note: removing '!=' triggers warning C4800 on the VS compiler. |
802 return (request_data.stage & GetStages()) != 0; | 803 return (request_data.stage & GetStages()) != 0; |
803 } | 804 } |
804 | 805 |
805 WebRequestConditionAttribute::Type | 806 WebRequestConditionAttribute::Type |
806 WebRequestConditionAttributeStages::GetType() const { | 807 WebRequestConditionAttributeStages::GetType() const { |
807 return CONDITION_STAGES; | 808 return CONDITION_STAGES; |
808 } | 809 } |
809 | 810 |
810 } // namespace extensions | 811 } // namespace extensions |
OLD | NEW |