| 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/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/stl_util.h" |
| 9 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
| 10 #include "base/values.h" | 11 #include "base/values.h" |
| 11 #include "chrome/browser/extensions/api/declarative_webrequest/request_stage.h" | 12 #include "chrome/browser/extensions/api/declarative_webrequest/request_stage.h" |
| 12 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit
ion_attribute.h" | 13 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit
ion_attribute.h" |
| 13 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta
nts.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/common/extensions/matcher/url_matcher.h" | 15 #include "chrome/common/extensions/matcher/url_matcher.h" |
| 16 #include "chrome/common/extensions/matcher/url_matcher_factory.h" | 16 #include "chrome/common/extensions/matcher/url_matcher_factory.h" |
| 17 #include "net/url_request/url_request.h" | 17 #include "net/url_request/url_request.h" |
| 18 | 18 |
| 19 namespace keys = extensions::declarative_webrequest_constants; | 19 namespace keys = extensions::declarative_webrequest_constants; |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 static extensions::URLMatcherConditionSet::ID g_next_id = 0; | 22 static extensions::URLMatcherConditionSet::ID g_next_id = 0; |
| 23 | 23 |
| 24 // TODO(battre): improve error messaging to give more meaningful messages | 24 // TODO(battre): improve error messaging to give more meaningful messages |
| (...skipping 17 matching lines...) Expand all Loading... |
| 42 // | 42 // |
| 43 // WebRequestCondition | 43 // WebRequestCondition |
| 44 // | 44 // |
| 45 | 45 |
| 46 WebRequestCondition::WebRequestCondition( | 46 WebRequestCondition::WebRequestCondition( |
| 47 scoped_refptr<URLMatcherConditionSet> url_matcher_conditions, | 47 scoped_refptr<URLMatcherConditionSet> url_matcher_conditions, |
| 48 const WebRequestConditionAttributes& condition_attributes) | 48 const WebRequestConditionAttributes& condition_attributes) |
| 49 : url_matcher_conditions_(url_matcher_conditions), | 49 : url_matcher_conditions_(url_matcher_conditions), |
| 50 condition_attributes_(condition_attributes), | 50 condition_attributes_(condition_attributes), |
| 51 applicable_request_stages_(~0) { | 51 applicable_request_stages_(~0) { |
| 52 CHECK(url_matcher_conditions.get()); | |
| 53 for (WebRequestConditionAttributes::const_iterator i = | 52 for (WebRequestConditionAttributes::const_iterator i = |
| 54 condition_attributes_.begin(); i != condition_attributes_.end(); ++i) { | 53 condition_attributes_.begin(); i != condition_attributes_.end(); ++i) { |
| 55 applicable_request_stages_ &= (*i)->GetStages(); | 54 applicable_request_stages_ &= (*i)->GetStages(); |
| 56 } | 55 } |
| 57 } | 56 } |
| 58 | 57 |
| 59 WebRequestCondition::~WebRequestCondition() {} | 58 WebRequestCondition::~WebRequestCondition() {} |
| 60 | 59 |
| 61 bool WebRequestCondition::IsFulfilled( | 60 bool WebRequestCondition::IsFulfilled( |
| 61 const std::set<URLMatcherConditionSet::ID>& url_matches, |
| 62 const WebRequestRule::RequestData& request_data) const { | 62 const WebRequestRule::RequestData& request_data) const { |
| 63 // All condition attributes must be fulfilled for a fulfilled condition. | |
| 64 if (!(request_data.stage & applicable_request_stages_)) { | 63 if (!(request_data.stage & applicable_request_stages_)) { |
| 65 // A condition that cannot be evaluated is considered as violated. | 64 // A condition that cannot be evaluated is considered as violated. |
| 66 return false; | 65 return false; |
| 67 } | 66 } |
| 68 | 67 |
| 68 // Check a UrlFilter attribute if present. |
| 69 if (url_matcher_conditions_.get() && |
| 70 !ContainsKey(url_matches, url_matcher_conditions_->id())) |
| 71 return false; |
| 72 |
| 73 // All condition attributes must be fulfilled for a fulfilled condition. |
| 69 for (WebRequestConditionAttributes::const_iterator i = | 74 for (WebRequestConditionAttributes::const_iterator i = |
| 70 condition_attributes_.begin(); i != condition_attributes_.end(); ++i) { | 75 condition_attributes_.begin(); i != condition_attributes_.end(); ++i) { |
| 71 if (!(*i)->IsFulfilled(request_data)) | 76 if (!(*i)->IsFulfilled(request_data)) |
| 72 return false; | 77 return false; |
| 73 } | 78 } |
| 74 return true; | 79 return true; |
| 75 } | 80 } |
| 76 | 81 |
| 77 // static | 82 // static |
| 78 scoped_ptr<WebRequestCondition> WebRequestCondition::Create( | 83 scoped_ptr<WebRequestCondition> WebRequestCondition::Create( |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 if (attribute.get()) | 130 if (attribute.get()) |
| 126 attributes.push_back(make_linked_ptr(attribute.release())); | 131 attributes.push_back(make_linked_ptr(attribute.release())); |
| 127 } else { | 132 } else { |
| 128 *error = base::StringPrintf(kUnknownConditionAttribute, | 133 *error = base::StringPrintf(kUnknownConditionAttribute, |
| 129 condition_attribute_name.c_str()); | 134 condition_attribute_name.c_str()); |
| 130 } | 135 } |
| 131 if (!error->empty()) | 136 if (!error->empty()) |
| 132 return scoped_ptr<WebRequestCondition>(NULL); | 137 return scoped_ptr<WebRequestCondition>(NULL); |
| 133 } | 138 } |
| 134 | 139 |
| 135 if (!url_matcher_condition_set) { | |
| 136 URLMatcherConditionSet::Conditions url_matcher_conditions; | |
| 137 url_matcher_conditions.insert( | |
| 138 url_matcher_condition_factory->CreateHostPrefixCondition("")); | |
| 139 url_matcher_condition_set = | |
| 140 new URLMatcherConditionSet(++g_next_id, url_matcher_conditions); | |
| 141 } | |
| 142 scoped_ptr<WebRequestCondition> result( | 140 scoped_ptr<WebRequestCondition> result( |
| 143 new WebRequestCondition(url_matcher_condition_set, attributes)); | 141 new WebRequestCondition(url_matcher_condition_set, attributes)); |
| 144 | 142 |
| 145 if (!result->stages()) { | 143 if (!result->stages()) { |
| 146 *error = kConditionCannotBeFulfilled; | 144 *error = kConditionCannotBeFulfilled; |
| 147 return scoped_ptr<WebRequestCondition>(NULL); | 145 return scoped_ptr<WebRequestCondition>(NULL); |
| 148 } | 146 } |
| 149 | 147 |
| 150 return result.Pass(); | 148 return result.Pass(); |
| 151 } | 149 } |
| 152 | 150 |
| 153 | |
| 154 // | 151 // |
| 155 // WebRequestConditionSet | 152 // WebRequestConditionSet |
| 156 // | 153 // |
| 157 | 154 |
| 158 WebRequestConditionSet::WebRequestConditionSet( | |
| 159 const WebRequestConditionSet::Conditions& conditions) | |
| 160 : conditions_(conditions) { | |
| 161 for (Conditions::iterator i = conditions_.begin(); i != conditions_.end(); | |
| 162 ++i) { | |
| 163 URLMatcherConditionSet::ID trigger_id = | |
| 164 (*i)->url_matcher_condition_set_id(); | |
| 165 match_triggers_[trigger_id] = i->get(); | |
| 166 } | |
| 167 } | |
| 168 | |
| 169 WebRequestConditionSet::~WebRequestConditionSet() {} | 155 WebRequestConditionSet::~WebRequestConditionSet() {} |
| 170 | 156 |
| 171 bool WebRequestConditionSet::IsFulfilled( | 157 bool WebRequestConditionSet::IsFulfilled( |
| 172 URLMatcherConditionSet::ID url_match, | 158 URLMatcherConditionSet::ID url_match_trigger, |
| 159 const std::set<URLMatcherConditionSet::ID>& url_matches, |
| 173 const WebRequestRule::RequestData& request_data) const { | 160 const WebRequestRule::RequestData& request_data) const { |
| 174 MatchTriggers::const_iterator trigger = match_triggers_.find(url_match); | 161 if (url_match_trigger == -1) { |
| 175 DCHECK(trigger != match_triggers_.end()); | 162 // Invalid trigger -- indication that we should only check conditions |
| 176 DCHECK_EQ(url_match, trigger->second->url_matcher_condition_set_id()); | 163 // without URL attributes. |
| 177 return trigger->second->IsFulfilled(request_data); | 164 for (std::vector<const WebRequestCondition*>::const_iterator it = |
| 165 conditions_without_urls_.begin(); |
| 166 it != conditions_without_urls_.end(); ++it) { |
| 167 if ((*it)->IsFulfilled(url_matches, request_data)) |
| 168 return true; |
| 169 } |
| 170 return false; |
| 171 } |
| 172 |
| 173 URLMatcherIdToCondition::const_iterator triggered = |
| 174 match_id_to_condition_.find(url_match_trigger); |
| 175 return (triggered != match_id_to_condition_.end() && |
| 176 triggered->second->IsFulfilled(url_matches, request_data)); |
| 178 } | 177 } |
| 179 | 178 |
| 180 void WebRequestConditionSet::GetURLMatcherConditionSets( | 179 void WebRequestConditionSet::GetURLMatcherConditionSets( |
| 181 URLMatcherConditionSet::Vector* condition_sets) const { | 180 URLMatcherConditionSet::Vector* condition_sets) const { |
| 182 for (Conditions::const_iterator i = conditions_.begin(); | 181 for (Conditions::const_iterator i = conditions_.begin(); |
| 183 i != conditions_.end(); ++i) { | 182 i != conditions_.end(); ++i) { |
| 184 condition_sets->push_back((*i)->url_matcher_condition_set()); | 183 scoped_refptr<URLMatcherConditionSet> set = |
| 184 (*i)->url_matcher_condition_set(); |
| 185 if (set.get()) |
| 186 condition_sets->push_back(set); |
| 185 } | 187 } |
| 186 } | 188 } |
| 187 | 189 |
| 188 // static | 190 // static |
| 189 scoped_ptr<WebRequestConditionSet> WebRequestConditionSet::Create( | 191 scoped_ptr<WebRequestConditionSet> WebRequestConditionSet::Create( |
| 190 URLMatcherConditionFactory* url_matcher_condition_factory, | 192 URLMatcherConditionFactory* url_matcher_condition_factory, |
| 191 const AnyVector& conditions, | 193 const AnyVector& conditions, |
| 192 std::string* error) { | 194 std::string* error) { |
| 193 WebRequestConditionSet::Conditions result; | 195 Conditions result; |
| 194 | 196 |
| 195 for (AnyVector::const_iterator i = conditions.begin(); | 197 for (AnyVector::const_iterator i = conditions.begin(); |
| 196 i != conditions.end(); ++i) { | 198 i != conditions.end(); ++i) { |
| 197 CHECK(i->get()); | 199 CHECK(i->get()); |
| 198 scoped_ptr<WebRequestCondition> condition = | 200 scoped_ptr<WebRequestCondition> condition = |
| 199 WebRequestCondition::Create(url_matcher_condition_factory, | 201 WebRequestCondition::Create(url_matcher_condition_factory, |
| 200 (*i)->value(), error); | 202 (*i)->value(), error); |
| 201 if (!error->empty()) | 203 if (!error->empty()) |
| 202 return scoped_ptr<WebRequestConditionSet>(NULL); | 204 return scoped_ptr<WebRequestConditionSet>(NULL); |
| 203 result.push_back(make_linked_ptr(condition.release())); | 205 result.push_back(make_linked_ptr(condition.release())); |
| 204 } | 206 } |
| 205 | 207 |
| 206 return scoped_ptr<WebRequestConditionSet>(new WebRequestConditionSet(result)); | 208 URLMatcherIdToCondition match_id_to_condition; |
| 209 std::vector<const WebRequestCondition*> conditions_without_urls; |
| 210 |
| 211 for (Conditions::const_iterator i = result.begin(); i != result.end(); ++i) { |
| 212 const URLMatcherConditionSet* set = (*i)->url_matcher_condition_set().get(); |
| 213 if (set) { |
| 214 URLMatcherConditionSet::ID id = set->id(); |
| 215 match_id_to_condition[id] = i->get(); |
| 216 } else { |
| 217 conditions_without_urls.push_back(i->get()); |
| 218 } |
| 219 } |
| 220 |
| 221 return make_scoped_ptr(new WebRequestConditionSet( |
| 222 result, match_id_to_condition, conditions_without_urls)); |
| 207 } | 223 } |
| 208 | 224 |
| 225 bool WebRequestConditionSet::HasConditionsWithoutUrls() const { |
| 226 return !conditions_without_urls_.empty(); |
| 227 } |
| 228 |
| 229 WebRequestConditionSet::WebRequestConditionSet( |
| 230 const Conditions& conditions, |
| 231 const URLMatcherIdToCondition& match_id_to_condition, |
| 232 const std::vector<const WebRequestCondition*>& conditions_without_urls) |
| 233 : match_id_to_condition_(match_id_to_condition), |
| 234 conditions_(conditions), |
| 235 conditions_without_urls_(conditions_without_urls) {} |
| 236 |
| 209 } // namespace extensions | 237 } // namespace extensions |
| OLD | NEW |