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/browser/extensions/api/declarative_webrequest/webrequest_consta
nts.h" |
15 #include "chrome/common/extensions/matcher/url_matcher.h" | 16 #include "chrome/common/extensions/matcher/url_matcher.h" |
16 #include "chrome/common/extensions/matcher/url_matcher_factory.h" | 17 #include "chrome/common/extensions/matcher/url_matcher_factory.h" |
17 #include "net/url_request/url_request.h" | 18 #include "net/url_request/url_request.h" |
18 | 19 |
(...skipping 23 matching lines...) Expand all Loading... |
42 // | 43 // |
43 // WebRequestCondition | 44 // WebRequestCondition |
44 // | 45 // |
45 | 46 |
46 WebRequestCondition::WebRequestCondition( | 47 WebRequestCondition::WebRequestCondition( |
47 scoped_refptr<URLMatcherConditionSet> url_matcher_conditions, | 48 scoped_refptr<URLMatcherConditionSet> url_matcher_conditions, |
48 const WebRequestConditionAttributes& condition_attributes) | 49 const WebRequestConditionAttributes& condition_attributes) |
49 : url_matcher_conditions_(url_matcher_conditions), | 50 : url_matcher_conditions_(url_matcher_conditions), |
50 condition_attributes_(condition_attributes), | 51 condition_attributes_(condition_attributes), |
51 applicable_request_stages_(~0) { | 52 applicable_request_stages_(~0) { |
52 CHECK(url_matcher_conditions.get()); | |
53 for (WebRequestConditionAttributes::const_iterator i = | 53 for (WebRequestConditionAttributes::const_iterator i = |
54 condition_attributes_.begin(); i != condition_attributes_.end(); ++i) { | 54 condition_attributes_.begin(); i != condition_attributes_.end(); ++i) { |
55 applicable_request_stages_ &= (*i)->GetStages(); | 55 applicable_request_stages_ &= (*i)->GetStages(); |
56 } | 56 } |
57 } | 57 } |
58 | 58 |
59 WebRequestCondition::~WebRequestCondition() {} | 59 WebRequestCondition::~WebRequestCondition() {} |
60 | 60 |
61 bool WebRequestCondition::IsFulfilled( | 61 bool WebRequestCondition::IsFulfilled( |
| 62 const std::set<URLMatcherConditionSet::ID>& url_matches, |
62 const WebRequestRule::RequestData& request_data) const { | 63 const WebRequestRule::RequestData& request_data) const { |
| 64 // Check a UrlFilter attribute if present. |
| 65 if (url_matcher_conditions_.get() && |
| 66 !ContainsKey(url_matches, url_matcher_conditions_->id())) |
| 67 return false; |
| 68 |
63 // All condition attributes must be fulfilled for a fulfilled condition. | 69 // All condition attributes must be fulfilled for a fulfilled condition. |
64 if (!(request_data.stage & applicable_request_stages_)) { | 70 if (!(request_data.stage & applicable_request_stages_)) { |
65 // A condition that cannot be evaluated is considered as violated. | 71 // A condition that cannot be evaluated is considered as violated. |
66 return false; | 72 return false; |
67 } | 73 } |
68 | 74 |
69 for (WebRequestConditionAttributes::const_iterator i = | 75 for (WebRequestConditionAttributes::const_iterator i = |
70 condition_attributes_.begin(); i != condition_attributes_.end(); ++i) { | 76 condition_attributes_.begin(); i != condition_attributes_.end(); ++i) { |
71 if (!(*i)->IsFulfilled(request_data)) | 77 if (!(*i)->IsFulfilled(request_data)) |
72 return false; | 78 return false; |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 if (attribute.get()) | 131 if (attribute.get()) |
126 attributes.push_back(make_linked_ptr(attribute.release())); | 132 attributes.push_back(make_linked_ptr(attribute.release())); |
127 } else { | 133 } else { |
128 *error = base::StringPrintf(kUnknownConditionAttribute, | 134 *error = base::StringPrintf(kUnknownConditionAttribute, |
129 condition_attribute_name.c_str()); | 135 condition_attribute_name.c_str()); |
130 } | 136 } |
131 if (!error->empty()) | 137 if (!error->empty()) |
132 return scoped_ptr<WebRequestCondition>(NULL); | 138 return scoped_ptr<WebRequestCondition>(NULL); |
133 } | 139 } |
134 | 140 |
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( | 141 scoped_ptr<WebRequestCondition> result( |
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 // | 152 // |
155 // WebRequestConditionSet | 153 // WebRequestConditionSet |
156 // | 154 // |
157 | 155 |
| 156 namespace { |
| 157 |
| 158 // Returns whether there is a condition among |conditions| which has no |
| 159 // URLMatcherConditionSet assigned. |
| 160 bool HasConditionWithoutUrlMatcher( |
| 161 const WebRequestConditionSet::Conditions& conditions) { |
| 162 for (WebRequestConditionSet::Conditions::const_iterator i = |
| 163 conditions.begin(); |
| 164 i != conditions.end(); ++i) { |
| 165 if (!(*i)->url_matcher_condition_set().get()) |
| 166 return true; |
| 167 } |
| 168 return false; |
| 169 } |
| 170 |
| 171 } // namespace |
| 172 |
158 WebRequestConditionSet::WebRequestConditionSet( | 173 WebRequestConditionSet::WebRequestConditionSet( |
159 const WebRequestConditionSet::Conditions& conditions) | 174 const WebRequestConditionSet::Conditions& conditions) |
160 : conditions_(conditions) { | 175 : conditions_(conditions), |
161 for (Conditions::iterator i = conditions_.begin(); i != conditions_.end(); | 176 has_conditions_without_urls_(HasConditionWithoutUrlMatcher(conditions)) { |
162 ++i) { | |
163 URLMatcherConditionSet::ID trigger_id = | |
164 (*i)->url_matcher_condition_set_id(); | |
165 match_triggers_[trigger_id] = i->get(); | |
166 } | |
167 } | 177 } |
168 | 178 |
169 WebRequestConditionSet::~WebRequestConditionSet() {} | 179 WebRequestConditionSet::~WebRequestConditionSet() {} |
170 | 180 |
171 bool WebRequestConditionSet::IsFulfilled( | 181 bool WebRequestConditionSet::IsFulfilled( |
172 URLMatcherConditionSet::ID url_match, | 182 const std::set<URLMatcherConditionSet::ID>& url_matches, |
173 const WebRequestRule::RequestData& request_data) const { | 183 const WebRequestRule::RequestData& request_data) const { |
174 MatchTriggers::const_iterator trigger = match_triggers_.find(url_match); | 184 for (Conditions::const_iterator i = conditions_.begin(); |
175 DCHECK(trigger != match_triggers_.end()); | 185 i != conditions_.end(); ++i) { |
176 DCHECK_EQ(url_match, trigger->second->url_matcher_condition_set_id()); | 186 if ((*i)->IsFulfilled(url_matches, request_data)) |
177 return trigger->second->IsFulfilled(request_data); | 187 return true; |
| 188 } |
| 189 return false; |
178 } | 190 } |
179 | 191 |
180 void WebRequestConditionSet::GetURLMatcherConditionSets( | 192 void WebRequestConditionSet::GetURLMatcherConditionSets( |
181 URLMatcherConditionSet::Vector* condition_sets) const { | 193 URLMatcherConditionSet::Vector* condition_sets) const { |
182 for (Conditions::const_iterator i = conditions_.begin(); | 194 for (Conditions::const_iterator i = conditions_.begin(); |
183 i != conditions_.end(); ++i) { | 195 i != conditions_.end(); ++i) { |
184 condition_sets->push_back((*i)->url_matcher_condition_set()); | 196 scoped_refptr<URLMatcherConditionSet> set = |
| 197 (*i)->url_matcher_condition_set(); |
| 198 if (set.get()) |
| 199 condition_sets->push_back(set); |
185 } | 200 } |
186 } | 201 } |
187 | 202 |
188 // static | 203 // static |
189 scoped_ptr<WebRequestConditionSet> WebRequestConditionSet::Create( | 204 scoped_ptr<WebRequestConditionSet> WebRequestConditionSet::Create( |
190 URLMatcherConditionFactory* url_matcher_condition_factory, | 205 URLMatcherConditionFactory* url_matcher_condition_factory, |
191 const AnyVector& conditions, | 206 const AnyVector& conditions, |
192 std::string* error) { | 207 std::string* error) { |
193 WebRequestConditionSet::Conditions result; | 208 WebRequestConditionSet::Conditions result; |
194 | 209 |
195 for (AnyVector::const_iterator i = conditions.begin(); | 210 for (AnyVector::const_iterator i = conditions.begin(); |
196 i != conditions.end(); ++i) { | 211 i != conditions.end(); ++i) { |
197 CHECK(i->get()); | 212 CHECK(i->get()); |
198 scoped_ptr<WebRequestCondition> condition = | 213 scoped_ptr<WebRequestCondition> condition = |
199 WebRequestCondition::Create(url_matcher_condition_factory, | 214 WebRequestCondition::Create(url_matcher_condition_factory, |
200 (*i)->value(), error); | 215 (*i)->value(), error); |
201 if (!error->empty()) | 216 if (!error->empty()) |
202 return scoped_ptr<WebRequestConditionSet>(NULL); | 217 return scoped_ptr<WebRequestConditionSet>(NULL); |
203 result.push_back(make_linked_ptr(condition.release())); | 218 result.push_back(make_linked_ptr(condition.release())); |
204 } | 219 } |
205 | 220 |
206 return scoped_ptr<WebRequestConditionSet>(new WebRequestConditionSet(result)); | 221 return scoped_ptr<WebRequestConditionSet>(new WebRequestConditionSet(result)); |
207 } | 222 } |
208 | 223 |
209 } // namespace extensions | 224 } // namespace extensions |
OLD | NEW |