| 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" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 // Error messages: | 26 // Error messages: |
| 27 const char kExpectedDictionary[] = "A condition has to be a dictionary."; | 27 const char kExpectedDictionary[] = "A condition has to be a dictionary."; |
| 28 const char kConditionWithoutInstanceType[] = "A condition had no instanceType"; | 28 const char kConditionWithoutInstanceType[] = "A condition had no instanceType"; |
| 29 const char kExpectedOtherConditionType[] = "Expected a condition of type " | 29 const char kExpectedOtherConditionType[] = "Expected a condition of type " |
| 30 "experimental.webRequest.RequestMatcher"; | 30 "experimental.webRequest.RequestMatcher"; |
| 31 const char kUnknownConditionAttribute[] = "Unknown condition attribute '%s'"; | 31 const char kUnknownConditionAttribute[] = "Unknown condition attribute '%s'"; |
| 32 const char kConditionExpectedString[] = | 32 const char kConditionExpectedString[] = |
| 33 "Condition '%s' expected a string value"; | 33 "Condition '%s' expected a string value"; |
| 34 const char kVectorOfStringsExpected[] = | 34 const char kVectorOfStringsExpected[] = |
| 35 "Attribute '%s' expected a vector of strings"; | 35 "Attribute '%s' expected a vector of strings"; |
| 36 const char kInvalidPortRanges[] = "Invalid port ranges"; |
| 36 | 37 |
| 37 // String literals from the JavaScript API: | 38 // String literals from the JavaScript API: |
| 38 const char kHostContainsKey[] = "host_contains"; | 39 const char kHostContainsKey[] = "host_contains"; |
| 39 const char kHostEqualsKey[] = "host_equals"; | 40 const char kHostEqualsKey[] = "host_equals"; |
| 40 const char kHostPrefixKey[] = "host_prefix"; | 41 const char kHostPrefixKey[] = "host_prefix"; |
| 41 const char kHostSuffixKey[] = "host_suffix"; | 42 const char kHostSuffixKey[] = "host_suffix"; |
| 42 const char kHostSuffixPathPrefixKey[] = "host_suffix_path_prefix"; | 43 const char kHostSuffixPathPrefixKey[] = "host_suffix_path_prefix"; |
| 43 const char kPathContainsKey[] = "path_contains"; | 44 const char kPathContainsKey[] = "path_contains"; |
| 44 const char kPathEqualsKey[] = "path_equals"; | 45 const char kPathEqualsKey[] = "path_equals"; |
| 45 const char kPathPrefixKey[] = "path_prefix"; | 46 const char kPathPrefixKey[] = "path_prefix"; |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 return scoped_ptr<WebRequestCondition>(NULL); | 173 return scoped_ptr<WebRequestCondition>(NULL); |
| 173 } | 174 } |
| 174 if (instance_type != keys::kRequestMatcherType) { | 175 if (instance_type != keys::kRequestMatcherType) { |
| 175 *error = kExpectedOtherConditionType; | 176 *error = kExpectedOtherConditionType; |
| 176 return scoped_ptr<WebRequestCondition>(NULL); | 177 return scoped_ptr<WebRequestCondition>(NULL); |
| 177 } | 178 } |
| 178 | 179 |
| 179 WebRequestConditionAttributes attributes; | 180 WebRequestConditionAttributes attributes; |
| 180 URLMatcherConditionSet::Conditions url_matcher_conditions; | 181 URLMatcherConditionSet::Conditions url_matcher_conditions; |
| 181 scoped_ptr<URLMatcherSchemeFilter> url_matcher_schema_filter; | 182 scoped_ptr<URLMatcherSchemeFilter> url_matcher_schema_filter; |
| 183 scoped_ptr<URLMatcherPortFilter> url_matcher_port_filter; |
| 182 | 184 |
| 183 for (base::DictionaryValue::Iterator iter(*condition_dict); | 185 for (base::DictionaryValue::Iterator iter(*condition_dict); |
| 184 iter.HasNext(); iter.Advance()) { | 186 iter.HasNext(); iter.Advance()) { |
| 185 const std::string& condition_attribute_name = iter.key(); | 187 const std::string& condition_attribute_name = iter.key(); |
| 186 const Value& condition_attribute_value = iter.value(); | 188 const Value& condition_attribute_value = iter.value(); |
| 187 if (condition_attribute_name == keys::kInstanceTypeKey) { | 189 if (condition_attribute_name == keys::kInstanceTypeKey) { |
| 188 // Skip this. | 190 // Skip this. |
| 189 } else if (IsURLMatcherConditionAttribute(condition_attribute_name)) { | 191 } else if (IsURLMatcherConditionAttribute(condition_attribute_name)) { |
| 190 URLMatcherCondition url_matcher_condition = | 192 URLMatcherCondition url_matcher_condition = |
| 191 CreateURLMatcherCondition( | 193 CreateURLMatcherCondition( |
| 192 url_matcher_condition_factory, | 194 url_matcher_condition_factory, |
| 193 condition_attribute_name, | 195 condition_attribute_name, |
| 194 &condition_attribute_value, | 196 &condition_attribute_value, |
| 195 error); | 197 error); |
| 196 if (!error->empty()) | 198 if (!error->empty()) |
| 197 return scoped_ptr<WebRequestCondition>(NULL); | 199 return scoped_ptr<WebRequestCondition>(NULL); |
| 198 url_matcher_conditions.insert(url_matcher_condition); | 200 url_matcher_conditions.insert(url_matcher_condition); |
| 199 } else if (condition_attribute_name == keys::kSchemesKey) { | 201 } else if (condition_attribute_name == keys::kSchemesKey) { |
| 200 url_matcher_schema_filter = CreateURLMatcherScheme( | 202 url_matcher_schema_filter = CreateURLMatcherScheme( |
| 201 &condition_attribute_value, error); | 203 &condition_attribute_value, error); |
| 202 if (!error->empty()) | 204 if (!error->empty()) |
| 203 return scoped_ptr<WebRequestCondition>(NULL); | 205 return scoped_ptr<WebRequestCondition>(NULL); |
| 206 } else if (condition_attribute_name == keys::kPortsKey) { |
| 207 url_matcher_port_filter = CreateURLMatcherPorts( |
| 208 &condition_attribute_value, error); |
| 209 if (!error->empty()) |
| 210 return scoped_ptr<WebRequestCondition>(NULL); |
| 204 } else if (WebRequestConditionAttribute::IsKnownType( | 211 } else if (WebRequestConditionAttribute::IsKnownType( |
| 205 condition_attribute_name)) { | 212 condition_attribute_name)) { |
| 206 scoped_ptr<WebRequestConditionAttribute> attribute = | 213 scoped_ptr<WebRequestConditionAttribute> attribute = |
| 207 WebRequestConditionAttribute::Create( | 214 WebRequestConditionAttribute::Create( |
| 208 condition_attribute_name, | 215 condition_attribute_name, |
| 209 &condition_attribute_value, | 216 &condition_attribute_value, |
| 210 error); | 217 error); |
| 211 if (!error->empty()) | 218 if (!error->empty()) |
| 212 return scoped_ptr<WebRequestCondition>(NULL); | 219 return scoped_ptr<WebRequestCondition>(NULL); |
| 213 attributes.push_back(make_linked_ptr(attribute.release())); | 220 attributes.push_back(make_linked_ptr(attribute.release())); |
| 214 } else { | 221 } else { |
| 215 *error = base::StringPrintf(kUnknownConditionAttribute, | 222 *error = base::StringPrintf(kUnknownConditionAttribute, |
| 216 condition_attribute_name.c_str()); | 223 condition_attribute_name.c_str()); |
| 217 return scoped_ptr<WebRequestCondition>(NULL); | 224 return scoped_ptr<WebRequestCondition>(NULL); |
| 218 } | 225 } |
| 219 } | 226 } |
| 220 | 227 |
| 221 // As the URL is the preliminary matching criterion that triggers the tests | 228 // As the URL is the preliminary matching criterion that triggers the tests |
| 222 // for the remaining condition attributes, we insert an empty URL match if | 229 // for the remaining condition attributes, we insert an empty URL match if |
| 223 // no other url match conditions were specified. Such an empty URL is always | 230 // no other url match conditions were specified. Such an empty URL is always |
| 224 // matched. | 231 // matched. |
| 225 if (url_matcher_conditions.empty()) { | 232 if (url_matcher_conditions.empty()) { |
| 226 url_matcher_conditions.insert( | 233 url_matcher_conditions.insert( |
| 227 url_matcher_condition_factory->CreateHostPrefixCondition("")); | 234 url_matcher_condition_factory->CreateHostPrefixCondition("")); |
| 228 } | 235 } |
| 229 | 236 |
| 230 scoped_refptr<URLMatcherConditionSet> url_matcher_condition_set( | 237 scoped_refptr<URLMatcherConditionSet> url_matcher_condition_set( |
| 231 new URLMatcherConditionSet(++g_next_id, url_matcher_conditions, | 238 new URLMatcherConditionSet(++g_next_id, url_matcher_conditions, |
| 232 url_matcher_schema_filter.Pass())); | 239 url_matcher_schema_filter.Pass(), url_matcher_port_filter.Pass())); |
| 233 return scoped_ptr<WebRequestCondition>( | 240 return scoped_ptr<WebRequestCondition>( |
| 234 new WebRequestCondition(url_matcher_condition_set, attributes)); | 241 new WebRequestCondition(url_matcher_condition_set, attributes)); |
| 235 } | 242 } |
| 236 | 243 |
| 237 // static | 244 // static |
| 238 bool WebRequestCondition::IsURLMatcherConditionAttribute( | 245 bool WebRequestCondition::IsURLMatcherConditionAttribute( |
| 239 const std::string& condition_attribute_name) { | 246 const std::string& condition_attribute_name) { |
| 240 return g_url_matcher_condition_factory_methods.Get().Contains( | 247 return g_url_matcher_condition_factory_methods.Get().Contains( |
| 241 condition_attribute_name); | 248 condition_attribute_name); |
| 242 } | 249 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 263 std::string* error) { | 270 std::string* error) { |
| 264 std::vector<std::string> schemas; | 271 std::vector<std::string> schemas; |
| 265 if (!helpers::GetAsStringVector(value, &schemas)) { | 272 if (!helpers::GetAsStringVector(value, &schemas)) { |
| 266 *error = base::StringPrintf(kVectorOfStringsExpected, keys::kSchemesKey); | 273 *error = base::StringPrintf(kVectorOfStringsExpected, keys::kSchemesKey); |
| 267 return scoped_ptr<URLMatcherSchemeFilter>(NULL); | 274 return scoped_ptr<URLMatcherSchemeFilter>(NULL); |
| 268 } | 275 } |
| 269 return scoped_ptr<URLMatcherSchemeFilter>( | 276 return scoped_ptr<URLMatcherSchemeFilter>( |
| 270 new URLMatcherSchemeFilter(schemas)); | 277 new URLMatcherSchemeFilter(schemas)); |
| 271 } | 278 } |
| 272 | 279 |
| 280 // static |
| 281 scoped_ptr<URLMatcherPortFilter> WebRequestCondition::CreateURLMatcherPorts( |
| 282 const base::Value* value, |
| 283 std::string* error) { |
| 284 std::vector<URLMatcherPortFilter::Range> ranges; |
| 285 const base::ListValue* value_list = NULL; |
| 286 if (!value->GetAsList(&value_list)) { |
| 287 *error = kInvalidPortRanges; |
| 288 return scoped_ptr<URLMatcherPortFilter>(NULL); |
| 289 } |
| 290 |
| 291 for (ListValue::const_iterator i = value_list->begin(); |
| 292 i != value_list->end(); ++i) { |
| 293 Value* entry = *i; |
| 294 int port = 0; |
| 295 base::ListValue* range = NULL; |
| 296 if (entry->GetAsInteger(&port)) { |
| 297 ranges.push_back(URLMatcherPortFilter::CreateRange(port)); |
| 298 } else if (entry->GetAsList(&range)) { |
| 299 int from = 0, to = 0; |
| 300 if (range->GetSize() != 2u || |
| 301 !range->GetInteger(0, &from) || |
| 302 !range->GetInteger(1, &to)) { |
| 303 *error = kInvalidPortRanges; |
| 304 return scoped_ptr<URLMatcherPortFilter>(NULL); |
| 305 } |
| 306 ranges.push_back(URLMatcherPortFilter::CreateRange(from, to)); |
| 307 } else { |
| 308 *error = kInvalidPortRanges; |
| 309 return scoped_ptr<URLMatcherPortFilter>(NULL); |
| 310 } |
| 311 } |
| 312 |
| 313 return scoped_ptr<URLMatcherPortFilter>(new URLMatcherPortFilter(ranges)); |
| 314 } |
| 315 |
| 273 // | 316 // |
| 274 // WebRequestConditionSet | 317 // WebRequestConditionSet |
| 275 // | 318 // |
| 276 | 319 |
| 277 WebRequestConditionSet::WebRequestConditionSet( | 320 WebRequestConditionSet::WebRequestConditionSet( |
| 278 const std::vector<linked_ptr<WebRequestCondition> >& conditions) | 321 const std::vector<linked_ptr<WebRequestCondition> >& conditions) |
| 279 : conditions_(conditions) { | 322 : conditions_(conditions) { |
| 280 for (Conditions::iterator i = conditions_.begin(); i != conditions_.end(); | 323 for (Conditions::iterator i = conditions_.begin(); i != conditions_.end(); |
| 281 ++i) { | 324 ++i) { |
| 282 URLMatcherConditionSet::ID trigger_id = | 325 URLMatcherConditionSet::ID trigger_id = |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 320 (*i)->value(), error); | 363 (*i)->value(), error); |
| 321 if (!error->empty()) | 364 if (!error->empty()) |
| 322 return scoped_ptr<WebRequestConditionSet>(NULL); | 365 return scoped_ptr<WebRequestConditionSet>(NULL); |
| 323 result.push_back(make_linked_ptr(condition.release())); | 366 result.push_back(make_linked_ptr(condition.release())); |
| 324 } | 367 } |
| 325 | 368 |
| 326 return scoped_ptr<WebRequestConditionSet>(new WebRequestConditionSet(result)); | 369 return scoped_ptr<WebRequestConditionSet>(new WebRequestConditionSet(result)); |
| 327 } | 370 } |
| 328 | 371 |
| 329 } // namespace extensions | 372 } // namespace extensions |
| OLD | NEW |