Chromium Code Reviews| Index: chrome/browser/extensions/api/declarative_webrequest/webrequest_condition.cc |
| diff --git a/chrome/browser/extensions/api/declarative_webrequest/webrequest_condition.cc b/chrome/browser/extensions/api/declarative_webrequest/webrequest_condition.cc |
| index cf02de76c3c4fdb871b8eccd147e466f51255d95..626c81781b78b4f39b695f09680352aed068f40e 100644 |
| --- a/chrome/browser/extensions/api/declarative_webrequest/webrequest_condition.cc |
| +++ b/chrome/browser/extensions/api/declarative_webrequest/webrequest_condition.cc |
| @@ -33,6 +33,7 @@ const char kConditionExpectedString[] = |
| "Condition '%s' expected a string value"; |
| const char kVectorOfStringsExpected[] = |
| "Attribute '%s' expected a vector of strings"; |
| +const char kInvalidPortRanges[] = "Invalid port ranges"; |
| // String literals from the JavaScript API: |
| const char kHostContainsKey[] = "host_contains"; |
| @@ -179,6 +180,7 @@ scoped_ptr<WebRequestCondition> WebRequestCondition::Create( |
| WebRequestConditionAttributes attributes; |
| URLMatcherConditionSet::Conditions url_matcher_conditions; |
| scoped_ptr<URLMatcherSchemeFilter> url_matcher_schema_filter; |
| + scoped_ptr<URLMatcherPortFilter> url_matcher_port_filter; |
| for (base::DictionaryValue::Iterator iter(*condition_dict); |
| iter.HasNext(); iter.Advance()) { |
| @@ -201,6 +203,11 @@ scoped_ptr<WebRequestCondition> WebRequestCondition::Create( |
| &condition_attribute_value, error); |
| if (!error->empty()) |
| return scoped_ptr<WebRequestCondition>(NULL); |
| + } else if (condition_attribute_name == keys::kPortsKey) { |
| + url_matcher_port_filter = CreateURLMatcherPorts( |
| + &condition_attribute_value, error); |
| + if (!error->empty()) |
| + return scoped_ptr<WebRequestCondition>(NULL); |
| } else if (WebRequestConditionAttribute::IsKnownType( |
| condition_attribute_name)) { |
| scoped_ptr<WebRequestConditionAttribute> attribute = |
| @@ -229,7 +236,7 @@ scoped_ptr<WebRequestCondition> WebRequestCondition::Create( |
| scoped_refptr<URLMatcherConditionSet> url_matcher_condition_set( |
| new URLMatcherConditionSet(++g_next_id, url_matcher_conditions, |
| - url_matcher_schema_filter.Pass())); |
| + url_matcher_schema_filter.Pass(), url_matcher_port_filter.Pass())); |
| return scoped_ptr<WebRequestCondition>( |
| new WebRequestCondition(url_matcher_condition_set, attributes)); |
| } |
| @@ -270,6 +277,44 @@ scoped_ptr<URLMatcherSchemeFilter> WebRequestCondition::CreateURLMatcherScheme( |
| new URLMatcherSchemeFilter(schemas)); |
| } |
| +// static |
| +scoped_ptr<URLMatcherPortFilter> WebRequestCondition::CreateURLMatcherPorts( |
| + const base::Value* value, |
| + std::string* error) { |
| + std::vector<URLMatcherPortFilter::Range> ranges; |
| + const base::ListValue* value_list = NULL; |
| + if (!value->GetAsList(&value_list)) { |
| + *error = kInvalidPortRanges; |
| + return scoped_ptr<URLMatcherPortFilter>(NULL); |
| + } |
| + |
| + for (ListValue::const_iterator i = value_list->begin(); |
| + i != value_list->end(); ++i) { |
| + Value* entry = *i; |
| + if (entry->IsType(Value::TYPE_INTEGER)) { |
|
Matt Perry
2012/04/12 01:17:52
you could simplify this to:
int int_value;
ListVal
battre
2012/04/12 06:47:29
Done.
|
| + int port = 0; |
| + CHECK(entry->GetAsInteger(&port)); |
| + ranges.push_back(URLMatcherPortFilter::CreateRange(port)); |
| + } else if (entry->IsType(Value::TYPE_LIST)) { |
| + base::ListValue* range = NULL; |
| + int from = 0, to = 0; |
| + CHECK(entry->GetAsList(&range)); |
| + if (range->GetSize() != 2u || |
| + !range->GetInteger(0, &from) || |
| + !range->GetInteger(1, &to)) { |
| + *error = kInvalidPortRanges; |
| + return scoped_ptr<URLMatcherPortFilter>(NULL); |
| + } |
| + ranges.push_back(URLMatcherPortFilter::CreateRange(from, to)); |
| + } else { |
| + *error = kInvalidPortRanges; |
| + return scoped_ptr<URLMatcherPortFilter>(NULL); |
| + } |
| + } |
| + |
| + return scoped_ptr<URLMatcherPortFilter>(new URLMatcherPortFilter(ranges)); |
| +} |
| + |
| // |
| // WebRequestConditionSet |
| // |