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 27 matching lines...) Expand all Loading... |
52 CHECK(url_matcher_conditions.get()); | 51 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( |
62 const WebRequestRule::RequestData& request_data) const { | 61 const DeclarativeWebRequestData& request_data) const { |
63 // All condition attributes must be fulfilled for a fulfilled condition. | 62 // 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 |
69 for (WebRequestConditionAttributes::const_iterator i = | 68 for (WebRequestConditionAttributes::const_iterator i = |
70 condition_attributes_.begin(); i != condition_attributes_.end(); ++i) { | 69 condition_attributes_.begin(); i != condition_attributes_.end(); ++i) { |
71 if (!(*i)->IsFulfilled(request_data)) | 70 if (!(*i)->IsFulfilled(request_data)) |
72 return false; | 71 return false; |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 new WebRequestCondition(url_matcher_condition_set, attributes)); | 142 new WebRequestCondition(url_matcher_condition_set, attributes)); |
144 | 143 |
145 if (!result->stages()) { | 144 if (!result->stages()) { |
146 *error = kConditionCannotBeFulfilled; | 145 *error = kConditionCannotBeFulfilled; |
147 return scoped_ptr<WebRequestCondition>(NULL); | 146 return scoped_ptr<WebRequestCondition>(NULL); |
148 } | 147 } |
149 | 148 |
150 return result.Pass(); | 149 return result.Pass(); |
151 } | 150 } |
152 | 151 |
153 | |
154 // | |
155 // WebRequestConditionSet | |
156 // | |
157 | |
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() {} | |
170 | |
171 bool WebRequestConditionSet::IsFulfilled( | |
172 URLMatcherConditionSet::ID url_match, | |
173 const WebRequestRule::RequestData& request_data) const { | |
174 MatchTriggers::const_iterator trigger = match_triggers_.find(url_match); | |
175 DCHECK(trigger != match_triggers_.end()); | |
176 DCHECK_EQ(url_match, trigger->second->url_matcher_condition_set_id()); | |
177 return trigger->second->IsFulfilled(request_data); | |
178 } | |
179 | |
180 void WebRequestConditionSet::GetURLMatcherConditionSets( | |
181 URLMatcherConditionSet::Vector* condition_sets) const { | |
182 for (Conditions::const_iterator i = conditions_.begin(); | |
183 i != conditions_.end(); ++i) { | |
184 condition_sets->push_back((*i)->url_matcher_condition_set()); | |
185 } | |
186 } | |
187 | |
188 // static | |
189 scoped_ptr<WebRequestConditionSet> WebRequestConditionSet::Create( | |
190 URLMatcherConditionFactory* url_matcher_condition_factory, | |
191 const AnyVector& conditions, | |
192 std::string* error) { | |
193 WebRequestConditionSet::Conditions result; | |
194 | |
195 for (AnyVector::const_iterator i = conditions.begin(); | |
196 i != conditions.end(); ++i) { | |
197 CHECK(i->get()); | |
198 scoped_ptr<WebRequestCondition> condition = | |
199 WebRequestCondition::Create(url_matcher_condition_factory, | |
200 (*i)->value(), error); | |
201 if (!error->empty()) | |
202 return scoped_ptr<WebRequestConditionSet>(NULL); | |
203 result.push_back(make_linked_ptr(condition.release())); | |
204 } | |
205 | |
206 return scoped_ptr<WebRequestConditionSet>(new WebRequestConditionSet(result)); | |
207 } | |
208 | |
209 } // namespace extensions | 152 } // namespace extensions |
OLD | NEW |