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_consta
nts.h" | 14 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta
nts.h" |
15 #include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h" | 15 #include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h" |
16 #include "chrome/common/extensions/extension_error_utils.h" | |
17 #include "content/public/browser/resource_request_info.h" | 16 #include "content/public/browser/resource_request_info.h" |
| 17 #include "extensions/common/error_utils.h" |
18 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
19 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 19 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
20 #include "net/base/static_cookie_policy.h" | 20 #include "net/base/static_cookie_policy.h" |
| 21 #include "net/http/http_request_headers.h" |
21 #include "net/http/http_util.h" | 22 #include "net/http/http_util.h" |
22 #include "net/http/http_request_headers.h" | |
23 #include "net/url_request/url_request.h" | 23 #include "net/url_request/url_request.h" |
24 | 24 |
25 using base::CaseInsensitiveCompareASCII; | 25 using base::CaseInsensitiveCompareASCII; |
26 using base::DictionaryValue; | 26 using base::DictionaryValue; |
27 using base::ListValue; | 27 using base::ListValue; |
28 using base::StringValue; | 28 using base::StringValue; |
29 using base::Value; | 29 using base::Value; |
30 | 30 |
31 namespace { | 31 namespace { |
32 // Error messages. | 32 // Error messages. |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 } else if (WebRequestConditionAttributeResponseHeaders::IsMatchingType( | 80 } else if (WebRequestConditionAttributeResponseHeaders::IsMatchingType( |
81 name)) { | 81 name)) { |
82 return WebRequestConditionAttributeResponseHeaders::Create( | 82 return WebRequestConditionAttributeResponseHeaders::Create( |
83 name, value, error); | 83 name, value, error); |
84 } else if (WebRequestConditionAttributeThirdParty::IsMatchingType(name)) { | 84 } else if (WebRequestConditionAttributeThirdParty::IsMatchingType(name)) { |
85 return WebRequestConditionAttributeThirdParty::Create(name, value, error); | 85 return WebRequestConditionAttributeThirdParty::Create(name, value, error); |
86 } else if (WebRequestConditionAttributeStages::IsMatchingType(name)) { | 86 } else if (WebRequestConditionAttributeStages::IsMatchingType(name)) { |
87 return WebRequestConditionAttributeStages::Create(name, value, error); | 87 return WebRequestConditionAttributeStages::Create(name, value, error); |
88 } | 88 } |
89 | 89 |
90 *error = ExtensionErrorUtils::FormatErrorMessage(kUnknownConditionAttribute, | 90 *error = ErrorUtils::FormatErrorMessage(kUnknownConditionAttribute, |
91 name); | 91 name); |
92 return scoped_ptr<WebRequestConditionAttribute>(NULL); | 92 return scoped_ptr<WebRequestConditionAttribute>(NULL); |
93 } | 93 } |
94 | 94 |
95 // | 95 // |
96 // WebRequestConditionAttributeResourceType | 96 // WebRequestConditionAttributeResourceType |
97 // | 97 // |
98 | 98 |
99 WebRequestConditionAttributeResourceType:: | 99 WebRequestConditionAttributeResourceType:: |
100 WebRequestConditionAttributeResourceType( | 100 WebRequestConditionAttributeResourceType( |
(...skipping 12 matching lines...) Expand all Loading... |
113 // static | 113 // static |
114 scoped_ptr<WebRequestConditionAttribute> | 114 scoped_ptr<WebRequestConditionAttribute> |
115 WebRequestConditionAttributeResourceType::Create( | 115 WebRequestConditionAttributeResourceType::Create( |
116 const std::string& name, | 116 const std::string& name, |
117 const base::Value* value, | 117 const base::Value* value, |
118 std::string* error) { | 118 std::string* error) { |
119 DCHECK(IsMatchingType(name)); | 119 DCHECK(IsMatchingType(name)); |
120 | 120 |
121 const ListValue* value_as_list = NULL; | 121 const ListValue* value_as_list = NULL; |
122 if (!value->GetAsList(&value_as_list)) { | 122 if (!value->GetAsList(&value_as_list)) { |
123 *error = ExtensionErrorUtils::FormatErrorMessage(kInvalidValue, | 123 *error = ErrorUtils::FormatErrorMessage(kInvalidValue, |
124 keys::kResourceTypeKey); | 124 keys::kResourceTypeKey); |
125 return scoped_ptr<WebRequestConditionAttribute>(NULL); | 125 return scoped_ptr<WebRequestConditionAttribute>(NULL); |
126 } | 126 } |
127 | 127 |
128 size_t number_types = value_as_list->GetSize(); | 128 size_t number_types = value_as_list->GetSize(); |
129 | 129 |
130 std::vector<ResourceType::Type> passed_types(number_types); | 130 std::vector<ResourceType::Type> passed_types(number_types); |
131 for (size_t i = 0; i < number_types; ++i) { | 131 for (size_t i = 0; i < number_types; ++i) { |
132 std::string resource_type_string; | 132 std::string resource_type_string; |
133 ResourceType::Type type = ResourceType::LAST_TYPE; | 133 ResourceType::Type type = ResourceType::LAST_TYPE; |
134 if (!value_as_list->GetString(i, &resource_type_string) || | 134 if (!value_as_list->GetString(i, &resource_type_string) || |
135 !helpers::ParseResourceType(resource_type_string, &type)) { | 135 !helpers::ParseResourceType(resource_type_string, &type)) { |
136 *error = ExtensionErrorUtils::FormatErrorMessage(kInvalidValue, | 136 *error = ErrorUtils::FormatErrorMessage(kInvalidValue, |
137 keys::kResourceTypeKey); | 137 keys::kResourceTypeKey); |
138 return scoped_ptr<WebRequestConditionAttribute>(NULL); | 138 return scoped_ptr<WebRequestConditionAttribute>(NULL); |
139 } | 139 } |
140 passed_types.push_back(type); | 140 passed_types.push_back(type); |
141 } | 141 } |
142 | 142 |
143 return scoped_ptr<WebRequestConditionAttribute>( | 143 return scoped_ptr<WebRequestConditionAttribute>( |
144 new WebRequestConditionAttributeResourceType(passed_types)); | 144 new WebRequestConditionAttributeResourceType(passed_types)); |
145 } | 145 } |
146 | 146 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 // static | 191 // static |
192 scoped_ptr<WebRequestConditionAttribute> | 192 scoped_ptr<WebRequestConditionAttribute> |
193 WebRequestConditionAttributeContentType::Create( | 193 WebRequestConditionAttributeContentType::Create( |
194 const std::string& name, | 194 const std::string& name, |
195 const base::Value* value, | 195 const base::Value* value, |
196 std::string* error) { | 196 std::string* error) { |
197 DCHECK(IsMatchingType(name)); | 197 DCHECK(IsMatchingType(name)); |
198 | 198 |
199 const ListValue* value_as_list = NULL; | 199 const ListValue* value_as_list = NULL; |
200 if (!value->GetAsList(&value_as_list)) { | 200 if (!value->GetAsList(&value_as_list)) { |
201 *error = ExtensionErrorUtils::FormatErrorMessage(kInvalidValue, name); | 201 *error = ErrorUtils::FormatErrorMessage(kInvalidValue, name); |
202 return scoped_ptr<WebRequestConditionAttribute>(NULL); | 202 return scoped_ptr<WebRequestConditionAttribute>(NULL); |
203 } | 203 } |
204 std::vector<std::string> content_types; | 204 std::vector<std::string> content_types; |
205 for (ListValue::const_iterator it = value_as_list->begin(); | 205 for (ListValue::const_iterator it = value_as_list->begin(); |
206 it != value_as_list->end(); ++it) { | 206 it != value_as_list->end(); ++it) { |
207 std::string content_type; | 207 std::string content_type; |
208 if (!(*it)->GetAsString(&content_type)) { | 208 if (!(*it)->GetAsString(&content_type)) { |
209 *error = ExtensionErrorUtils::FormatErrorMessage(kInvalidValue, name); | 209 *error = ErrorUtils::FormatErrorMessage(kInvalidValue, name); |
210 return scoped_ptr<WebRequestConditionAttribute>(NULL); | 210 return scoped_ptr<WebRequestConditionAttribute>(NULL); |
211 } | 211 } |
212 content_types.push_back(content_type); | 212 content_types.push_back(content_type); |
213 } | 213 } |
214 | 214 |
215 return scoped_ptr<WebRequestConditionAttribute>( | 215 return scoped_ptr<WebRequestConditionAttribute>( |
216 new WebRequestConditionAttributeContentType( | 216 new WebRequestConditionAttributeContentType( |
217 content_types, name == keys::kContentTypeKey)); | 217 content_types, name == keys::kContentTypeKey)); |
218 } | 218 } |
219 | 219 |
(...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
520 } | 520 } |
521 | 521 |
522 namespace { | 522 namespace { |
523 | 523 |
524 scoped_ptr<const HeaderMatcher> PrepareHeaderMatcher( | 524 scoped_ptr<const HeaderMatcher> PrepareHeaderMatcher( |
525 const std::string& name, | 525 const std::string& name, |
526 const base::Value* value, | 526 const base::Value* value, |
527 std::string* error) { | 527 std::string* error) { |
528 const ListValue* value_as_list = NULL; | 528 const ListValue* value_as_list = NULL; |
529 if (!value->GetAsList(&value_as_list)) { | 529 if (!value->GetAsList(&value_as_list)) { |
530 *error = ExtensionErrorUtils::FormatErrorMessage(kInvalidValue, name); | 530 *error = ErrorUtils::FormatErrorMessage(kInvalidValue, name); |
531 return scoped_ptr<const HeaderMatcher>(NULL); | 531 return scoped_ptr<const HeaderMatcher>(NULL); |
532 } | 532 } |
533 | 533 |
534 scoped_ptr<const HeaderMatcher> header_matcher( | 534 scoped_ptr<const HeaderMatcher> header_matcher( |
535 HeaderMatcher::Create(value_as_list)); | 535 HeaderMatcher::Create(value_as_list)); |
536 if (header_matcher.get() == NULL) | 536 if (header_matcher.get() == NULL) |
537 *error = ExtensionErrorUtils::FormatErrorMessage(kInvalidValue, name); | 537 *error = ErrorUtils::FormatErrorMessage(kInvalidValue, name); |
538 return header_matcher.Pass(); | 538 return header_matcher.Pass(); |
539 } | 539 } |
540 | 540 |
541 } // namespace | 541 } // namespace |
542 | 542 |
543 // static | 543 // static |
544 scoped_ptr<WebRequestConditionAttribute> | 544 scoped_ptr<WebRequestConditionAttribute> |
545 WebRequestConditionAttributeRequestHeaders::Create( | 545 WebRequestConditionAttributeRequestHeaders::Create( |
546 const std::string& name, | 546 const std::string& name, |
547 const base::Value* value, | 547 const base::Value* value, |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
679 // static | 679 // static |
680 scoped_ptr<WebRequestConditionAttribute> | 680 scoped_ptr<WebRequestConditionAttribute> |
681 WebRequestConditionAttributeThirdParty::Create( | 681 WebRequestConditionAttributeThirdParty::Create( |
682 const std::string& name, | 682 const std::string& name, |
683 const base::Value* value, | 683 const base::Value* value, |
684 std::string* error) { | 684 std::string* error) { |
685 DCHECK(IsMatchingType(name)); | 685 DCHECK(IsMatchingType(name)); |
686 | 686 |
687 bool third_party = false; // Dummy value, gets overwritten. | 687 bool third_party = false; // Dummy value, gets overwritten. |
688 if (!value->GetAsBoolean(&third_party)) { | 688 if (!value->GetAsBoolean(&third_party)) { |
689 *error = ExtensionErrorUtils::FormatErrorMessage(kInvalidValue, | 689 *error = ErrorUtils::FormatErrorMessage(kInvalidValue, |
690 keys::kThirdPartyKey); | 690 keys::kThirdPartyKey); |
691 return scoped_ptr<WebRequestConditionAttribute>(NULL); | 691 return scoped_ptr<WebRequestConditionAttribute>(NULL); |
692 } | 692 } |
693 | 693 |
694 return scoped_ptr<WebRequestConditionAttribute>( | 694 return scoped_ptr<WebRequestConditionAttribute>( |
695 new WebRequestConditionAttributeThirdParty(third_party)); | 695 new WebRequestConditionAttributeThirdParty(third_party)); |
696 } | 696 } |
697 | 697 |
698 int WebRequestConditionAttributeThirdParty::GetStages() const { | 698 int WebRequestConditionAttributeThirdParty::GetStages() const { |
699 return ON_BEFORE_REQUEST | ON_BEFORE_SEND_HEADERS | ON_SEND_HEADERS | | 699 return ON_BEFORE_REQUEST | ON_BEFORE_SEND_HEADERS | ON_SEND_HEADERS | |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
776 | 776 |
777 // static | 777 // static |
778 scoped_ptr<WebRequestConditionAttribute> | 778 scoped_ptr<WebRequestConditionAttribute> |
779 WebRequestConditionAttributeStages::Create(const std::string& name, | 779 WebRequestConditionAttributeStages::Create(const std::string& name, |
780 const Value* value, | 780 const Value* value, |
781 std::string* error) { | 781 std::string* error) { |
782 DCHECK(IsMatchingType(name)); | 782 DCHECK(IsMatchingType(name)); |
783 | 783 |
784 int allowed_stages = 0; | 784 int allowed_stages = 0; |
785 if (!ParseListOfStages(*value, &allowed_stages)) { | 785 if (!ParseListOfStages(*value, &allowed_stages)) { |
786 *error = ExtensionErrorUtils::FormatErrorMessage(kInvalidValue, | 786 *error = ErrorUtils::FormatErrorMessage(kInvalidValue, |
787 keys::kStagesKey); | 787 keys::kStagesKey); |
788 return scoped_ptr<WebRequestConditionAttribute>(NULL); | 788 return scoped_ptr<WebRequestConditionAttribute>(NULL); |
789 } | 789 } |
790 | 790 |
791 return scoped_ptr<WebRequestConditionAttribute>( | 791 return scoped_ptr<WebRequestConditionAttribute>( |
792 new WebRequestConditionAttributeStages(allowed_stages)); | 792 new WebRequestConditionAttributeStages(allowed_stages)); |
793 } | 793 } |
794 | 794 |
795 int WebRequestConditionAttributeStages::GetStages() const { | 795 int WebRequestConditionAttributeStages::GetStages() const { |
796 return allowed_stages_; | 796 return allowed_stages_; |
797 } | 797 } |
798 | 798 |
799 bool WebRequestConditionAttributeStages::IsFulfilled( | 799 bool WebRequestConditionAttributeStages::IsFulfilled( |
800 const WebRequestRule::RequestData& request_data) const { | 800 const WebRequestRule::RequestData& request_data) const { |
801 // Note: removing '!=' triggers warning C4800 on the VS compiler. | 801 // Note: removing '!=' triggers warning C4800 on the VS compiler. |
802 return (request_data.stage & GetStages()) != 0; | 802 return (request_data.stage & GetStages()) != 0; |
803 } | 803 } |
804 | 804 |
805 WebRequestConditionAttribute::Type | 805 WebRequestConditionAttribute::Type |
806 WebRequestConditionAttributeStages::GetType() const { | 806 WebRequestConditionAttributeStages::GetType() const { |
807 return CONDITION_STAGES; | 807 return CONDITION_STAGES; |
808 } | 808 } |
809 | 809 |
810 } // namespace extensions | 810 } // namespace extensions |
OLD | NEW |