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/stringprintf.h" | 9 #include "base/stringprintf.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
11 #include "chrome/browser/extensions/api/declarative_webrequest/request_stage.h" | 11 #include "chrome/browser/extensions/api/declarative_webrequest/request_stage.h" |
12 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit
ion_attribute.h" | 12 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit
ion_attribute.h" |
13 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta
nts.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" | |
15 #include "chrome/common/extensions/matcher/url_matcher.h" | 14 #include "chrome/common/extensions/matcher/url_matcher.h" |
16 #include "chrome/common/extensions/matcher/url_matcher_factory.h" | 15 #include "chrome/common/extensions/matcher/url_matcher_factory.h" |
17 #include "net/url_request/url_request.h" | 16 #include "net/url_request/url_request.h" |
18 | 17 |
19 namespace keys = extensions::declarative_webrequest_constants; | 18 namespace keys = extensions::declarative_webrequest_constants; |
20 | 19 |
21 namespace { | 20 namespace { |
22 static extensions::URLMatcherConditionSet::ID g_next_id = 0; | 21 static extensions::URLMatcherConditionSet::ID g_next_id = 0; |
23 | 22 |
24 // TODO(battre): improve error messaging to give more meaningful messages | 23 // TODO(battre): improve error messaging to give more meaningful messages |
(...skipping 26 matching lines...) Expand all Loading... |
51 applicable_request_stages_(~0) { | 50 applicable_request_stages_(~0) { |
52 for (WebRequestConditionAttributes::const_iterator i = | 51 for (WebRequestConditionAttributes::const_iterator i = |
53 condition_attributes_.begin(); i != condition_attributes_.end(); ++i) { | 52 condition_attributes_.begin(); i != condition_attributes_.end(); ++i) { |
54 applicable_request_stages_ &= (*i)->GetStages(); | 53 applicable_request_stages_ &= (*i)->GetStages(); |
55 } | 54 } |
56 } | 55 } |
57 | 56 |
58 WebRequestCondition::~WebRequestCondition() {} | 57 WebRequestCondition::~WebRequestCondition() {} |
59 | 58 |
60 bool WebRequestCondition::IsFulfilled( | 59 bool WebRequestCondition::IsFulfilled( |
61 const std::set<URLMatcherConditionSet::ID>& url_matches, | 60 const DeclarativeWebRequestData& request_data) const { |
62 const WebRequestRule::RequestData& request_data) const { | |
63 // Check a UrlFilter attribute if present. | 61 // Check a UrlFilter attribute if present. |
64 if (url_matcher_conditions_.get() && | 62 if (url_matcher_conditions_.get() && |
65 !ContainsKey(url_matches, url_matcher_conditions_->id())) | 63 !ContainsKey(*request_data.url_matches, url_matcher_conditions_->id())) |
66 return false; | 64 return false; |
67 | 65 |
68 // All condition attributes must be fulfilled for a fulfilled condition. | 66 // All condition attributes must be fulfilled for a fulfilled condition. |
69 if (!(request_data.stage & applicable_request_stages_)) { | 67 if (!(request_data.stage & applicable_request_stages_)) { |
70 // A condition that cannot be evaluated is considered as violated. | 68 // A condition that cannot be evaluated is considered as violated. |
71 return false; | 69 return false; |
72 } | 70 } |
73 | 71 |
74 for (WebRequestConditionAttributes::const_iterator i = | 72 for (WebRequestConditionAttributes::const_iterator i = |
75 condition_attributes_.begin(); i != condition_attributes_.end(); ++i) { | 73 condition_attributes_.begin(); i != condition_attributes_.end(); ++i) { |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 new WebRequestCondition(url_matcher_condition_set, attributes)); | 139 new WebRequestCondition(url_matcher_condition_set, attributes)); |
142 | 140 |
143 if (!result->stages()) { | 141 if (!result->stages()) { |
144 *error = kConditionCannotBeFulfilled; | 142 *error = kConditionCannotBeFulfilled; |
145 return scoped_ptr<WebRequestCondition>(NULL); | 143 return scoped_ptr<WebRequestCondition>(NULL); |
146 } | 144 } |
147 | 145 |
148 return result.Pass(); | 146 return result.Pass(); |
149 } | 147 } |
150 | 148 |
151 // | |
152 // WebRequestConditionSet | |
153 // | |
154 | |
155 namespace { | |
156 | |
157 // Returns whether there is a condition among |conditions| which has no | |
158 // URLMatcherConditionSet assigned. | |
159 bool HasConditionWithoutUrlMatcher( | |
160 const WebRequestConditionSet::Conditions& conditions) { | |
161 for (WebRequestConditionSet::Conditions::const_iterator i = | |
162 conditions.begin(); | |
163 i != conditions.end(); ++i) { | |
164 if (!(*i)->url_matcher_condition_set().get()) | |
165 return true; | |
166 } | |
167 return false; | |
168 } | |
169 | |
170 } // namespace | |
171 | |
172 WebRequestConditionSet::WebRequestConditionSet( | |
173 const WebRequestConditionSet::Conditions& conditions) | |
174 : conditions_(conditions), | |
175 has_conditions_without_urls_(HasConditionWithoutUrlMatcher(conditions)) { | |
176 } | |
177 | |
178 WebRequestConditionSet::~WebRequestConditionSet() {} | |
179 | |
180 bool WebRequestConditionSet::IsFulfilled( | |
181 const std::set<URLMatcherConditionSet::ID>& url_matches, | |
182 const WebRequestRule::RequestData& request_data) const { | |
183 for (Conditions::const_iterator i = conditions_.begin(); | |
184 i != conditions_.end(); ++i) { | |
185 if ((*i)->IsFulfilled(url_matches, request_data)) | |
186 return true; | |
187 } | |
188 return false; | |
189 } | |
190 | |
191 void WebRequestConditionSet::GetURLMatcherConditionSets( | |
192 URLMatcherConditionSet::Vector* condition_sets) const { | |
193 for (Conditions::const_iterator i = conditions_.begin(); | |
194 i != conditions_.end(); ++i) { | |
195 scoped_refptr<URLMatcherConditionSet> set = | |
196 (*i)->url_matcher_condition_set(); | |
197 if (set.get()) | |
198 condition_sets->push_back(set); | |
199 } | |
200 } | |
201 | |
202 // static | |
203 scoped_ptr<WebRequestConditionSet> WebRequestConditionSet::Create( | |
204 URLMatcherConditionFactory* url_matcher_condition_factory, | |
205 const AnyVector& conditions, | |
206 std::string* error) { | |
207 WebRequestConditionSet::Conditions result; | |
208 | |
209 for (AnyVector::const_iterator i = conditions.begin(); | |
210 i != conditions.end(); ++i) { | |
211 CHECK(i->get()); | |
212 scoped_ptr<WebRequestCondition> condition = | |
213 WebRequestCondition::Create(url_matcher_condition_factory, | |
214 (*i)->value(), error); | |
215 if (!error->empty()) | |
216 return scoped_ptr<WebRequestConditionSet>(NULL); | |
217 result.push_back(make_linked_ptr(condition.release())); | |
218 } | |
219 | |
220 return scoped_ptr<WebRequestConditionSet>(new WebRequestConditionSet(result)); | |
221 } | |
222 | |
223 } // namespace extensions | 149 } // namespace extensions |
OLD | NEW |