| 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.h" | 5 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit
ion.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chrome/browser/extensions/api/declarative/url_matcher.h" |
| 12 #include "chrome/browser/extensions/api/declarative_webrequest/request_stages.h" | 13 #include "chrome/browser/extensions/api/declarative_webrequest/request_stages.h" |
| 13 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit
ion_attribute.h" | 14 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit
ion_attribute.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" |
| 16 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_helper
s.h" |
| 15 #include "net/url_request/url_request.h" | 17 #include "net/url_request/url_request.h" |
| 16 | 18 |
| 19 namespace helpers = extensions::declarative_webrequest_helpers; |
| 20 |
| 17 namespace { | 21 namespace { |
| 18 static extensions::URLMatcherConditionSet::ID g_next_id = 0; | 22 static extensions::URLMatcherConditionSet::ID g_next_id = 0; |
| 19 | 23 |
| 20 // TODO(battre): improve error messaging to give more meaningful messages | 24 // TODO(battre): improve error messaging to give more meaningful messages |
| 21 // to the extension developer. | 25 // to the extension developer. |
| 22 // Error messages: | 26 // Error messages: |
| 23 const char kExpectedDictionary[] = "A condition has to be a dictionary."; | 27 const char kExpectedDictionary[] = "A condition has to be a dictionary."; |
| 24 const char kConditionWithoutInstanceType[] = "A condition had no instanceType"; | 28 const char kConditionWithoutInstanceType[] = "A condition had no instanceType"; |
| 25 const char kExpectedOtherConditionType[] = "Expected a condition of type " | 29 const char kExpectedOtherConditionType[] = "Expected a condition of type " |
| 26 "experimental.webRequest.RequestMatcher"; | 30 "experimental.webRequest.RequestMatcher"; |
| 27 const char kUnknownConditionAttribute[] = "UnKnown condition attribute '%s'"; | 31 const char kUnknownConditionAttribute[] = "Unknown condition attribute '%s'"; |
| 28 const char kConditionExpectedString[] = | 32 const char kConditionExpectedString[] = |
| 29 "Condition '%s' expected a string value"; | 33 "Condition '%s' expected a string value"; |
| 34 const char kVectorOfStringsExpected[] = |
| 35 "Attribute '%s' expected a vector of strings"; |
| 30 | 36 |
| 31 // String literals from the JavaScript API: | 37 // String literals from the JavaScript API: |
| 32 const char kHostContainsKey[] = "host_contains"; | 38 const char kHostContainsKey[] = "host_contains"; |
| 33 const char kHostEqualsKey[] = "host_equals"; | 39 const char kHostEqualsKey[] = "host_equals"; |
| 34 const char kHostPrefixKey[] = "host_prefix"; | 40 const char kHostPrefixKey[] = "host_prefix"; |
| 35 const char kHostSuffixKey[] = "host_suffix"; | 41 const char kHostSuffixKey[] = "host_suffix"; |
| 36 const char kHostSuffixPathPrefixKey[] = "host_suffix_path_prefix"; | 42 const char kHostSuffixPathPrefixKey[] = "host_suffix_path_prefix"; |
| 37 const char kPathContainsKey[] = "path_contains"; | 43 const char kPathContainsKey[] = "path_contains"; |
| 38 const char kPathEqualsKey[] = "path_equals"; | 44 const char kPathEqualsKey[] = "path_equals"; |
| 39 const char kPathPrefixKey[] = "path_prefix"; | 45 const char kPathPrefixKey[] = "path_prefix"; |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 *error = kConditionWithoutInstanceType; | 171 *error = kConditionWithoutInstanceType; |
| 166 return scoped_ptr<WebRequestCondition>(NULL); | 172 return scoped_ptr<WebRequestCondition>(NULL); |
| 167 } | 173 } |
| 168 if (instance_type != keys::kRequestMatcherType) { | 174 if (instance_type != keys::kRequestMatcherType) { |
| 169 *error = kExpectedOtherConditionType; | 175 *error = kExpectedOtherConditionType; |
| 170 return scoped_ptr<WebRequestCondition>(NULL); | 176 return scoped_ptr<WebRequestCondition>(NULL); |
| 171 } | 177 } |
| 172 | 178 |
| 173 WebRequestConditionAttributes attributes; | 179 WebRequestConditionAttributes attributes; |
| 174 URLMatcherConditionSet::Conditions url_matcher_conditions; | 180 URLMatcherConditionSet::Conditions url_matcher_conditions; |
| 181 scoped_ptr<URLMatcherSchemeFilter> url_matcher_schema_filter; |
| 175 | 182 |
| 176 for (base::DictionaryValue::Iterator iter(*condition_dict); | 183 for (base::DictionaryValue::Iterator iter(*condition_dict); |
| 177 iter.HasNext(); iter.Advance()) { | 184 iter.HasNext(); iter.Advance()) { |
| 178 const std::string& condition_attribute_name = iter.key(); | 185 const std::string& condition_attribute_name = iter.key(); |
| 179 const Value& condition_attribute_value = iter.value(); | 186 const Value& condition_attribute_value = iter.value(); |
| 180 if (condition_attribute_name == keys::kInstanceTypeKey) { | 187 if (condition_attribute_name == keys::kInstanceTypeKey) { |
| 181 // Skip this. | 188 // Skip this. |
| 182 } else if (IsURLMatcherConditionAttribute(condition_attribute_name)) { | 189 } else if (IsURLMatcherConditionAttribute(condition_attribute_name)) { |
| 183 URLMatcherCondition url_matcher_condition = | 190 URLMatcherCondition url_matcher_condition = |
| 184 CreateURLMatcherCondition( | 191 CreateURLMatcherCondition( |
| 185 url_matcher_condition_factory, | 192 url_matcher_condition_factory, |
| 186 condition_attribute_name, | 193 condition_attribute_name, |
| 187 &condition_attribute_value, | 194 &condition_attribute_value, |
| 188 error); | 195 error); |
| 189 if (!error->empty()) | 196 if (!error->empty()) |
| 190 return scoped_ptr<WebRequestCondition>(NULL); | 197 return scoped_ptr<WebRequestCondition>(NULL); |
| 191 url_matcher_conditions.insert(url_matcher_condition); | 198 url_matcher_conditions.insert(url_matcher_condition); |
| 199 } else if (condition_attribute_name == keys::kSchemesKey) { |
| 200 url_matcher_schema_filter = CreateURLMatcherScheme( |
| 201 &condition_attribute_value, error); |
| 202 if (!error->empty()) |
| 203 return scoped_ptr<WebRequestCondition>(NULL); |
| 192 } else if (WebRequestConditionAttribute::IsKnownType( | 204 } else if (WebRequestConditionAttribute::IsKnownType( |
| 193 condition_attribute_name)) { | 205 condition_attribute_name)) { |
| 194 scoped_ptr<WebRequestConditionAttribute> attribute = | 206 scoped_ptr<WebRequestConditionAttribute> attribute = |
| 195 WebRequestConditionAttribute::Create( | 207 WebRequestConditionAttribute::Create( |
| 196 condition_attribute_name, | 208 condition_attribute_name, |
| 197 &condition_attribute_value, | 209 &condition_attribute_value, |
| 198 error); | 210 error); |
| 199 if (!error->empty()) | 211 if (!error->empty()) |
| 200 return scoped_ptr<WebRequestCondition>(NULL); | 212 return scoped_ptr<WebRequestCondition>(NULL); |
| 201 attributes.push_back(make_linked_ptr(attribute.release())); | 213 attributes.push_back(make_linked_ptr(attribute.release())); |
| 202 } else { | 214 } else { |
| 203 *error = base::StringPrintf(kUnknownConditionAttribute, | 215 *error = base::StringPrintf(kUnknownConditionAttribute, |
| 204 condition_attribute_name.c_str()); | 216 condition_attribute_name.c_str()); |
| 205 return scoped_ptr<WebRequestCondition>(NULL); | 217 return scoped_ptr<WebRequestCondition>(NULL); |
| 206 } | 218 } |
| 207 } | 219 } |
| 208 | 220 |
| 209 // As the URL is the preliminary matching criterion that triggers the tests | 221 // As the URL is the preliminary matching criterion that triggers the tests |
| 210 // for the remaining condition attributes, we insert an empty URL match if | 222 // for the remaining condition attributes, we insert an empty URL match if |
| 211 // no other url match conditions were specified. Such an empty URL is always | 223 // no other url match conditions were specified. Such an empty URL is always |
| 212 // matched. | 224 // matched. |
| 213 if (url_matcher_conditions.empty()) { | 225 if (url_matcher_conditions.empty()) { |
| 214 url_matcher_conditions.insert( | 226 url_matcher_conditions.insert( |
| 215 url_matcher_condition_factory->CreateHostPrefixCondition("")); | 227 url_matcher_condition_factory->CreateHostPrefixCondition("")); |
| 216 } | 228 } |
| 217 | 229 |
| 218 scoped_refptr<URLMatcherConditionSet> url_matcher_condition_set( | 230 scoped_refptr<URLMatcherConditionSet> url_matcher_condition_set( |
| 219 new URLMatcherConditionSet(++g_next_id, url_matcher_conditions)); | 231 new URLMatcherConditionSet(++g_next_id, url_matcher_conditions, |
| 232 url_matcher_schema_filter.Pass())); |
| 220 return scoped_ptr<WebRequestCondition>( | 233 return scoped_ptr<WebRequestCondition>( |
| 221 new WebRequestCondition(url_matcher_condition_set, attributes)); | 234 new WebRequestCondition(url_matcher_condition_set, attributes)); |
| 222 } | 235 } |
| 223 | 236 |
| 224 // static | 237 // static |
| 225 bool WebRequestCondition::IsURLMatcherConditionAttribute( | 238 bool WebRequestCondition::IsURLMatcherConditionAttribute( |
| 226 const std::string& condition_attribute_name) { | 239 const std::string& condition_attribute_name) { |
| 227 return g_url_matcher_condition_factory_methods.Get().Contains( | 240 return g_url_matcher_condition_factory_methods.Get().Contains( |
| 228 condition_attribute_name); | 241 condition_attribute_name); |
| 229 } | 242 } |
| 230 | 243 |
| 231 // static | 244 // static |
| 232 URLMatcherCondition WebRequestCondition::CreateURLMatcherCondition( | 245 URLMatcherCondition WebRequestCondition::CreateURLMatcherCondition( |
| 233 URLMatcherConditionFactory* url_matcher_condition_factory, | 246 URLMatcherConditionFactory* url_matcher_condition_factory, |
| 234 const std::string& condition_attribute_name, | 247 const std::string& condition_attribute_name, |
| 235 const base::Value* value, | 248 const base::Value* value, |
| 236 std::string* error) { | 249 std::string* error) { |
| 237 std::string str_value; | 250 std::string str_value; |
| 238 if (!value->GetAsString(&str_value)) { | 251 if (!value->GetAsString(&str_value)) { |
| 239 *error = base::StringPrintf(kConditionExpectedString, | 252 *error = base::StringPrintf(kConditionExpectedString, |
| 240 condition_attribute_name.c_str()); | 253 condition_attribute_name.c_str()); |
| 241 return URLMatcherCondition(); | 254 return URLMatcherCondition(); |
| 242 } | 255 } |
| 243 return g_url_matcher_condition_factory_methods.Get().Call( | 256 return g_url_matcher_condition_factory_methods.Get().Call( |
| 244 url_matcher_condition_factory, condition_attribute_name, str_value); | 257 url_matcher_condition_factory, condition_attribute_name, str_value); |
| 245 } | 258 } |
| 246 | 259 |
| 260 // static |
| 261 scoped_ptr<URLMatcherSchemeFilter> WebRequestCondition::CreateURLMatcherScheme( |
| 262 const base::Value* value, |
| 263 std::string* error) { |
| 264 std::vector<std::string> schemas; |
| 265 if (!helpers::GetAsStringVector(value, &schemas)) { |
| 266 *error = base::StringPrintf(kVectorOfStringsExpected, keys::kSchemesKey); |
| 267 return scoped_ptr<URLMatcherSchemeFilter>(NULL); |
| 268 } |
| 269 return scoped_ptr<URLMatcherSchemeFilter>( |
| 270 new URLMatcherSchemeFilter(schemas)); |
| 271 } |
| 247 | 272 |
| 248 // | 273 // |
| 249 // WebRequestConditionSet | 274 // WebRequestConditionSet |
| 250 // | 275 // |
| 251 | 276 |
| 252 WebRequestConditionSet::WebRequestConditionSet( | 277 WebRequestConditionSet::WebRequestConditionSet( |
| 253 const std::vector<linked_ptr<WebRequestCondition> >& conditions) | 278 const std::vector<linked_ptr<WebRequestCondition> >& conditions) |
| 254 : conditions_(conditions) { | 279 : conditions_(conditions) { |
| 255 for (Conditions::iterator i = conditions_.begin(); i != conditions_.end(); | 280 for (Conditions::iterator i = conditions_.begin(); i != conditions_.end(); |
| 256 ++i) { | 281 ++i) { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 (*i)->value(), error); | 320 (*i)->value(), error); |
| 296 if (!error->empty()) | 321 if (!error->empty()) |
| 297 return scoped_ptr<WebRequestConditionSet>(NULL); | 322 return scoped_ptr<WebRequestConditionSet>(NULL); |
| 298 result.push_back(make_linked_ptr(condition.release())); | 323 result.push_back(make_linked_ptr(condition.release())); |
| 299 } | 324 } |
| 300 | 325 |
| 301 return scoped_ptr<WebRequestConditionSet>(new WebRequestConditionSet(result)); | 326 return scoped_ptr<WebRequestConditionSet>(new WebRequestConditionSet(result)); |
| 302 } | 327 } |
| 303 | 328 |
| 304 } // namespace extensions | 329 } // namespace extensions |
| OLD | NEW |