| 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/stl_util.h" |
| 10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 for (WebRequestConditionAttributes::const_iterator i = | 52 for (WebRequestConditionAttributes::const_iterator i = |
| 53 condition_attributes_.begin(); i != condition_attributes_.end(); ++i) { | 53 condition_attributes_.begin(); i != condition_attributes_.end(); ++i) { |
| 54 applicable_request_stages_ &= (*i)->GetStages(); | 54 applicable_request_stages_ &= (*i)->GetStages(); |
| 55 } | 55 } |
| 56 } | 56 } |
| 57 | 57 |
| 58 WebRequestCondition::~WebRequestCondition() {} | 58 WebRequestCondition::~WebRequestCondition() {} |
| 59 | 59 |
| 60 bool WebRequestCondition::IsFulfilled( | 60 bool WebRequestCondition::IsFulfilled( |
| 61 const std::set<URLMatcherConditionSet::ID>& url_matches, | 61 const std::set<URLMatcherConditionSet::ID>& url_matches, |
| 62 const WebRequestRule::RequestData& request_data) const { | 62 const DeclarativeWebRequestData& request_data) const { |
| 63 if (!(request_data.stage & applicable_request_stages_)) { | 63 if (!(request_data.stage & applicable_request_stages_)) { |
| 64 // A condition that cannot be evaluated is considered as violated. | 64 // A condition that cannot be evaluated is considered as violated. |
| 65 return false; | 65 return false; |
| 66 } | 66 } |
| 67 | 67 |
| 68 // Check a UrlFilter attribute if present. | 68 // Check a UrlFilter attribute if present. |
| 69 if (url_matcher_conditions_.get() && | 69 if (url_matcher_conditions_.get() && |
| 70 !ContainsKey(url_matches, url_matcher_conditions_->id())) | 70 !ContainsKey(url_matches, url_matcher_conditions_->id())) |
| 71 return false; | 71 return false; |
| 72 | 72 |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 new WebRequestCondition(url_matcher_condition_set, attributes)); | 141 new WebRequestCondition(url_matcher_condition_set, attributes)); |
| 142 | 142 |
| 143 if (!result->stages()) { | 143 if (!result->stages()) { |
| 144 *error = kConditionCannotBeFulfilled; | 144 *error = kConditionCannotBeFulfilled; |
| 145 return scoped_ptr<WebRequestCondition>(NULL); | 145 return scoped_ptr<WebRequestCondition>(NULL); |
| 146 } | 146 } |
| 147 | 147 |
| 148 return result.Pass(); | 148 return result.Pass(); |
| 149 } | 149 } |
| 150 | 150 |
| 151 // | |
| 152 // WebRequestConditionSet | |
| 153 // | |
| 154 | |
| 155 WebRequestConditionSet::~WebRequestConditionSet() {} | |
| 156 | |
| 157 bool WebRequestConditionSet::IsFulfilled( | |
| 158 URLMatcherConditionSet::ID url_match_trigger, | |
| 159 const std::set<URLMatcherConditionSet::ID>& url_matches, | |
| 160 const WebRequestRule::RequestData& request_data) const { | |
| 161 if (url_match_trigger == -1) { | |
| 162 // Invalid trigger -- indication that we should only check conditions | |
| 163 // without URL attributes. | |
| 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)); | |
| 177 } | |
| 178 | |
| 179 void WebRequestConditionSet::GetURLMatcherConditionSets( | |
| 180 URLMatcherConditionSet::Vector* condition_sets) const { | |
| 181 for (Conditions::const_iterator i = conditions_.begin(); | |
| 182 i != conditions_.end(); ++i) { | |
| 183 scoped_refptr<URLMatcherConditionSet> set = | |
| 184 (*i)->url_matcher_condition_set(); | |
| 185 if (set.get()) | |
| 186 condition_sets->push_back(set); | |
| 187 } | |
| 188 } | |
| 189 | |
| 190 // static | |
| 191 scoped_ptr<WebRequestConditionSet> WebRequestConditionSet::Create( | |
| 192 URLMatcherConditionFactory* url_matcher_condition_factory, | |
| 193 const AnyVector& conditions, | |
| 194 std::string* error) { | |
| 195 Conditions result; | |
| 196 | |
| 197 for (AnyVector::const_iterator i = conditions.begin(); | |
| 198 i != conditions.end(); ++i) { | |
| 199 CHECK(i->get()); | |
| 200 scoped_ptr<WebRequestCondition> condition = | |
| 201 WebRequestCondition::Create(url_matcher_condition_factory, | |
| 202 (*i)->value(), error); | |
| 203 if (!error->empty()) | |
| 204 return scoped_ptr<WebRequestConditionSet>(NULL); | |
| 205 result.push_back(make_linked_ptr(condition.release())); | |
| 206 } | |
| 207 | |
| 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)); | |
| 223 } | |
| 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 | |
| 237 } // namespace extensions | 151 } // namespace extensions |
| OLD | NEW |