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/stringprintf.h" | 10 #include "base/stringprintf.h" |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
137 WebRequestConditionAttributeResourceType::GetType() const { | 137 WebRequestConditionAttributeResourceType::GetType() const { |
138 return CONDITION_RESOURCE_TYPE; | 138 return CONDITION_RESOURCE_TYPE; |
139 } | 139 } |
140 | 140 |
141 // | 141 // |
142 // WebRequestConditionAttributeContentType | 142 // WebRequestConditionAttributeContentType |
143 // | 143 // |
144 | 144 |
145 WebRequestConditionAttributeContentType:: | 145 WebRequestConditionAttributeContentType:: |
146 WebRequestConditionAttributeContentType( | 146 WebRequestConditionAttributeContentType( |
147 const std::vector<std::string>& content_types) | 147 const std::vector<std::string>& content_types, |
148 : content_types_(content_types) {} | 148 bool inclusive) |
149 : content_types_(content_types), inclusive_(inclusive) {} | |
battre
2012/08/07 08:45:09
nit: I think the inclusive_ should go into the nex
Yoyo Zhou
2012/08/07 19:32:35
I think the style guide can be construed either wa
| |
149 | 150 |
150 WebRequestConditionAttributeContentType:: | 151 WebRequestConditionAttributeContentType:: |
151 ~WebRequestConditionAttributeContentType() {} | 152 ~WebRequestConditionAttributeContentType() {} |
152 | 153 |
153 // static | 154 // static |
154 bool WebRequestConditionAttributeContentType::IsMatchingType( | 155 bool WebRequestConditionAttributeContentType::IsMatchingType( |
155 const std::string& instance_type) { | 156 const std::string& instance_type) { |
156 return instance_type == keys::kContentTypeKey; | 157 return instance_type == keys::kContentTypeKey || |
158 instance_type == keys::kExcludeContentTypeKey; | |
157 } | 159 } |
158 | 160 |
159 // static | 161 // static |
160 scoped_ptr<WebRequestConditionAttribute> | 162 scoped_ptr<WebRequestConditionAttribute> |
161 WebRequestConditionAttributeContentType::Create( | 163 WebRequestConditionAttributeContentType::Create( |
162 const std::string& name, | 164 const std::string& name, |
163 const base::Value* value, | 165 const base::Value* value, |
164 std::string* error) { | 166 std::string* error) { |
165 std::vector<std::string> content_types; | 167 DCHECK(IsMatchingType(name)); |
166 | 168 |
167 const ListValue* value_as_list = NULL; | 169 const ListValue* value_as_list = NULL; |
168 if (!value->GetAsList(&value_as_list)) { | 170 if (!value->GetAsList(&value_as_list)) { |
169 *error = ExtensionErrorUtils::FormatErrorMessage(kInvalidValue, | 171 *error = ExtensionErrorUtils::FormatErrorMessage(kInvalidValue, name); |
170 keys::kContentTypeKey); | |
171 return scoped_ptr<WebRequestConditionAttribute>(NULL); | 172 return scoped_ptr<WebRequestConditionAttribute>(NULL); |
172 } | 173 } |
173 | 174 std::vector<std::string> content_types; |
174 for (ListValue::const_iterator it = value_as_list->begin(); | 175 for (ListValue::const_iterator it = value_as_list->begin(); |
175 it != value_as_list->end(); ++it) { | 176 it != value_as_list->end(); ++it) { |
176 std::string content_type; | 177 std::string content_type; |
177 if (!(*it)->GetAsString(&content_type)) { | 178 if (!(*it)->GetAsString(&content_type)) { |
178 *error = ExtensionErrorUtils::FormatErrorMessage(kInvalidValue, | 179 *error = ExtensionErrorUtils::FormatErrorMessage(kInvalidValue, name); |
179 keys::kContentTypeKey); | |
180 return scoped_ptr<WebRequestConditionAttribute>(NULL); | 180 return scoped_ptr<WebRequestConditionAttribute>(NULL); |
181 } | 181 } |
182 content_types.push_back(content_type); | 182 content_types.push_back(content_type); |
183 } | 183 } |
184 | |
184 return scoped_ptr<WebRequestConditionAttribute>( | 185 return scoped_ptr<WebRequestConditionAttribute>( |
185 new WebRequestConditionAttributeContentType(content_types)); | 186 new WebRequestConditionAttributeContentType( |
187 content_types, name == keys::kContentTypeKey)); | |
186 } | 188 } |
187 | 189 |
188 int WebRequestConditionAttributeContentType::GetStages() const { | 190 int WebRequestConditionAttributeContentType::GetStages() const { |
189 return ON_HEADERS_RECEIVED; | 191 return ON_HEADERS_RECEIVED; |
190 } | 192 } |
191 | 193 |
192 bool WebRequestConditionAttributeContentType::IsFulfilled( | 194 bool WebRequestConditionAttributeContentType::IsFulfilled( |
193 const WebRequestRule::RequestData& request_data) { | 195 const WebRequestRule::RequestData& request_data) { |
194 if (!(request_data.stage & GetStages())) | 196 if (!(request_data.stage & GetStages())) |
195 return false; | 197 return false; |
196 std::string content_type; | 198 std::string content_type; |
197 request_data.original_response_headers->GetNormalizedHeader( | 199 request_data.original_response_headers->GetNormalizedHeader( |
198 net::HttpRequestHeaders::kContentType, &content_type); | 200 net::HttpRequestHeaders::kContentType, &content_type); |
199 std::string mime_type; | 201 std::string mime_type; |
200 std::string charset; | 202 std::string charset; |
201 bool had_charset = false; | 203 bool had_charset = false; |
202 net::HttpUtil::ParseContentType( | 204 net::HttpUtil::ParseContentType( |
203 content_type, &mime_type, &charset, &had_charset, NULL); | 205 content_type, &mime_type, &charset, &had_charset, NULL); |
204 | 206 |
205 return std::find(content_types_.begin(), content_types_.end(), | 207 if (inclusive_) { |
206 mime_type) != content_types_.end(); | 208 return std::find(content_types_.begin(), content_types_.end(), |
209 mime_type) != content_types_.end(); | |
210 } else { | |
211 return std::find(content_types_.begin(), content_types_.end(), | |
212 mime_type) == content_types_.end(); | |
213 } | |
207 } | 214 } |
208 | 215 |
209 WebRequestConditionAttribute::Type | 216 WebRequestConditionAttribute::Type |
210 WebRequestConditionAttributeContentType::GetType() const { | 217 WebRequestConditionAttributeContentType::GetType() const { |
211 return CONDITION_CONTENT_TYPE; | 218 return CONDITION_CONTENT_TYPE; |
212 } | 219 } |
213 | 220 |
214 } // namespace extensions | 221 } // namespace extensions |
OLD | NEW |