Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(273)

Side by Side Diff: components/url_matcher/url_matcher.cc

Issue 219613002: Add support for matching query parameters in URLMatcher (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Makes the changes backward compatible Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/url_matcher/url_matcher.h ('k') | components/url_matcher/url_matcher_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "components/url_matcher/url_matcher.h" 5 #include "components/url_matcher/url_matcher.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <iterator> 8 #include <iterator>
9 9
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 namespace { 142 namespace {
143 143
144 bool IsRegexCriterion(URLMatcherCondition::Criterion criterion) { 144 bool IsRegexCriterion(URLMatcherCondition::Criterion criterion) {
145 return criterion == URLMatcherCondition::URL_MATCHES; 145 return criterion == URLMatcherCondition::URL_MATCHES;
146 } 146 }
147 147
148 bool IsOriginAndPathRegexCriterion(URLMatcherCondition::Criterion criterion) { 148 bool IsOriginAndPathRegexCriterion(URLMatcherCondition::Criterion criterion) {
149 return criterion == URLMatcherCondition::ORIGIN_AND_PATH_MATCHES; 149 return criterion == URLMatcherCondition::ORIGIN_AND_PATH_MATCHES;
150 } 150 }
151 151
152 // These are symbols that are not contained in 7-bit ASCII used in GURLs.
153 const char kBeginningOfURL[] = {static_cast<char>(-1), 0};
154 const char kEndOfDomain[] = {static_cast<char>(-2), 0};
155 const char kEndOfPath[] = {static_cast<char>(-3), 0};
156 const char kBeginningOfQueryComponent[] = {static_cast<char>(-4), 0};
157 const char kEndOfURL[] = {static_cast<char>(-5), 0};
158 const char kQuerySeperator = '&';
Joao da Silva 2014/04/02 07:46:59 kQuerySeparator
kaliamoorthi 2014/04/02 10:56:53 Done.
159
160 // This function prepares the query string by replacing query separator with a
161 // magic value (|kBeginningOfQueryComponent|). When the boolean
162 // |prepend_beginning_of_query_component| is true the function prepends the
163 // query with the same magic. This is done to locate the start of a key value
164 // pair in the query string. The parameter |query| is passed by value
165 // intentionally, since it is locally modified.
166 std::string PrepareQuery(std::string query,
167 bool prepend_beginning_of_query_component) {
168 for (std::string::iterator it = query.begin(); it != query.end(); ++it) {
169 if (*it == kQuerySeperator)
170 *it = kBeginningOfQueryComponent[0];
171 }
172 return prepend_beginning_of_query_component
173 ? kBeginningOfQueryComponent + query
174 : query;
175 }
176
152 } // namespace 177 } // namespace
153 178
154 // 179 //
155 // URLMatcherCondition 180 // URLMatcherCondition
156 // 181 //
157 182
158 URLMatcherCondition::URLMatcherCondition() 183 URLMatcherCondition::URLMatcherCondition()
159 : criterion_(HOST_PREFIX), 184 : criterion_(HOST_PREFIX),
160 string_pattern_(NULL) {} 185 string_pattern_(NULL) {}
161 186
(...skipping 27 matching lines...) Expand all
189 return false; 214 return false;
190 } 215 }
191 216
192 bool URLMatcherCondition::IsFullURLCondition() const { 217 bool URLMatcherCondition::IsFullURLCondition() const {
193 // For these criteria the SubstringMatcher needs to be executed on the 218 // For these criteria the SubstringMatcher needs to be executed on the
194 // GURL that is canonicalized with 219 // GURL that is canonicalized with
195 // URLMatcherConditionFactory::CanonicalizeURLForFullSearches. 220 // URLMatcherConditionFactory::CanonicalizeURLForFullSearches.
196 switch (criterion_) { 221 switch (criterion_) {
197 case HOST_CONTAINS: 222 case HOST_CONTAINS:
198 case PATH_CONTAINS: 223 case PATH_CONTAINS:
199 case QUERY_CONTAINS:
200 case URL_PREFIX: 224 case URL_PREFIX:
201 case URL_SUFFIX: 225 case URL_SUFFIX:
202 case URL_CONTAINS: 226 case URL_CONTAINS:
203 case URL_EQUALS: 227 case URL_EQUALS:
204 return true; 228 return true;
205 default: 229 default:
206 break; 230 break;
207 } 231 }
208 return false; 232 return false;
209 } 233 }
(...skipping 16 matching lines...) Expand all
226 // a substring match on the raw URL. In case of a match, we need to verify 250 // a substring match on the raw URL. In case of a match, we need to verify
227 // that the match was found in the correct component of the URL. 251 // that the match was found in the correct component of the URL.
228 switch (criterion_) { 252 switch (criterion_) {
229 case HOST_CONTAINS: 253 case HOST_CONTAINS:
230 return url.host().find(string_pattern_->pattern()) != 254 return url.host().find(string_pattern_->pattern()) !=
231 std::string::npos; 255 std::string::npos;
232 case PATH_CONTAINS: 256 case PATH_CONTAINS:
233 return url.path().find(string_pattern_->pattern()) != 257 return url.path().find(string_pattern_->pattern()) !=
234 std::string::npos; 258 std::string::npos;
235 case QUERY_CONTAINS: 259 case QUERY_CONTAINS:
236 return url.query().find(string_pattern_->pattern()) != 260 return PrepareQuery(url.query(), false)
237 std::string::npos; 261 .find(string_pattern_->pattern()) != std::string::npos;
battre 2014/04/02 08:14:54 I think with the new syntax (CreateQueryContainsCo
kaliamoorthi 2014/04/02 10:56:53 I decorated the URL for component searches for ena
238 default: 262 default:
239 break; 263 break;
240 } 264 }
241 return true; 265 return true;
242 } 266 }
243 267
244 // 268 //
245 // URLMatcherConditionFactory 269 // URLMatcherConditionFactory
246 // 270 //
247 271
248 namespace {
249 // These are symbols that are not contained in 7-bit ASCII used in GURLs.
250 const char kBeginningOfURL[] = {static_cast<char>(-1), 0};
251 const char kEndOfDomain[] = {static_cast<char>(-2), 0};
252 const char kEndOfPath[] = {static_cast<char>(-3), 0};
253 const char kEndOfURL[] = {static_cast<char>(-4), 0};
254 } // namespace
255
256 URLMatcherConditionFactory::URLMatcherConditionFactory() : id_counter_(0) {} 272 URLMatcherConditionFactory::URLMatcherConditionFactory() : id_counter_(0) {}
257 273
258 URLMatcherConditionFactory::~URLMatcherConditionFactory() { 274 URLMatcherConditionFactory::~URLMatcherConditionFactory() {
259 STLDeleteElements(&substring_pattern_singletons_); 275 STLDeleteElements(&substring_pattern_singletons_);
260 STLDeleteElements(&regex_pattern_singletons_); 276 STLDeleteElements(&regex_pattern_singletons_);
261 STLDeleteElements(&origin_and_path_regex_pattern_singletons_); 277 STLDeleteElements(&origin_and_path_regex_pattern_singletons_);
262 } 278 }
263 279
264 std::string URLMatcherConditionFactory::CanonicalizeURLForComponentSearches( 280 std::string URLMatcherConditionFactory::CanonicalizeURLForComponentSearches(
265 const GURL& url) const { 281 const GURL& url) const {
266 return kBeginningOfURL + CanonicalizeHostname(url.host()) + kEndOfDomain + 282 return kBeginningOfURL + CanonicalizeHostname(url.host()) + kEndOfDomain +
267 url.path() + kEndOfPath + 283 url.path() + kEndOfPath +
268 (url.has_query() ? "?" + url.query() : std::string()) + kEndOfURL; 284 (url.has_query() ? PrepareQuery(url.query(), true) : std::string()) +
285 kEndOfURL;
269 } 286 }
270 287
271 URLMatcherCondition URLMatcherConditionFactory::CreateHostPrefixCondition( 288 URLMatcherCondition URLMatcherConditionFactory::CreateHostPrefixCondition(
272 const std::string& prefix) { 289 const std::string& prefix) {
273 return CreateCondition(URLMatcherCondition::HOST_PREFIX, 290 return CreateCondition(URLMatcherCondition::HOST_PREFIX,
274 kBeginningOfURL + CanonicalizeHostname(prefix)); 291 kBeginningOfURL + CanonicalizeHostname(prefix));
275 } 292 }
276 293
277 URLMatcherCondition URLMatcherConditionFactory::CreateHostSuffixCondition( 294 URLMatcherCondition URLMatcherConditionFactory::CreateHostSuffixCondition(
278 const std::string& suffix) { 295 const std::string& suffix) {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
310 URLMatcherCondition URLMatcherConditionFactory::CreatePathEqualsCondition( 327 URLMatcherCondition URLMatcherConditionFactory::CreatePathEqualsCondition(
311 const std::string& str) { 328 const std::string& str) {
312 return CreateCondition(URLMatcherCondition::PATH_EQUALS, 329 return CreateCondition(URLMatcherCondition::PATH_EQUALS,
313 kEndOfDomain + str + kEndOfPath); 330 kEndOfDomain + str + kEndOfPath);
314 } 331 }
315 332
316 URLMatcherCondition URLMatcherConditionFactory::CreateQueryPrefixCondition( 333 URLMatcherCondition URLMatcherConditionFactory::CreateQueryPrefixCondition(
317 const std::string& prefix) { 334 const std::string& prefix) {
318 std::string pattern; 335 std::string pattern;
319 if (!prefix.empty() && prefix[0] == '?') 336 if (!prefix.empty() && prefix[0] == '?')
320 pattern = kEndOfPath + prefix; 337 pattern = kEndOfPath + PrepareQuery(prefix.substr(1), true);
321 else 338 else
322 pattern = kEndOfPath + ('?' + prefix); 339 pattern = kEndOfPath + PrepareQuery(prefix, true);
323 340
324 return CreateCondition(URLMatcherCondition::QUERY_PREFIX, pattern); 341 return CreateCondition(URLMatcherCondition::QUERY_PREFIX, pattern);
325 } 342 }
326 343
327 URLMatcherCondition URLMatcherConditionFactory::CreateQuerySuffixCondition( 344 URLMatcherCondition URLMatcherConditionFactory::CreateQuerySuffixCondition(
328 const std::string& suffix) { 345 const std::string& suffix) {
329 if (!suffix.empty() && suffix[0] == '?') { 346 if (!suffix.empty() && suffix[0] == '?') {
330 return CreateQueryEqualsCondition(suffix); 347 return CreateQueryEqualsCondition(suffix);
331 } else { 348 } else {
332 return CreateCondition(URLMatcherCondition::QUERY_SUFFIX, 349 return CreateCondition(URLMatcherCondition::QUERY_SUFFIX,
333 suffix + kEndOfURL); 350 PrepareQuery(suffix, false) + kEndOfURL);
334 } 351 }
335 } 352 }
336 353
354 URLMatcherCondition
355 URLMatcherConditionFactory::CreateQueryContainsExactCondition(
356 const std::string& str) {
357 std::string pattern;
358 if (!str.empty() && str[0] == '?')
359 pattern = str.substr(1);
360 else
361 pattern = str;
362 return CreateCondition(URLMatcherCondition::QUERY_CONTAINS,
363 PrepareQuery(pattern, true));
364 }
365
337 URLMatcherCondition URLMatcherConditionFactory::CreateQueryContainsCondition( 366 URLMatcherCondition URLMatcherConditionFactory::CreateQueryContainsCondition(
338 const std::string& str) { 367 const std::string& str) {
368 std::string pattern;
339 if (!str.empty() && str[0] == '?') 369 if (!str.empty() && str[0] == '?')
340 return CreateQueryPrefixCondition(str); 370 pattern = str.substr(1);
341 else 371 else
342 return CreateCondition(URLMatcherCondition::QUERY_CONTAINS, str); 372 pattern = str;
373 return CreateCondition(URLMatcherCondition::QUERY_CONTAINS,
374 PrepareQuery(pattern, false));
343 } 375 }
344 376
345 URLMatcherCondition URLMatcherConditionFactory::CreateQueryEqualsCondition( 377 URLMatcherCondition URLMatcherConditionFactory::CreateQueryEqualsCondition(
346 const std::string& str) { 378 const std::string& str) {
347 std::string pattern; 379 std::string pattern;
348 if (!str.empty() && str[0] == '?') 380 if (!str.empty() && str[0] == '?')
349 pattern = kEndOfPath + str + kEndOfURL; 381 pattern = kEndOfPath + PrepareQuery(str.substr(1), true) + kEndOfURL;
350 else 382 else
351 pattern = kEndOfPath + ('?' + str) + kEndOfURL; 383 pattern = kEndOfPath + PrepareQuery(str, true) + kEndOfURL;
352 384
353 return CreateCondition(URLMatcherCondition::QUERY_EQUALS, pattern); 385 return CreateCondition(URLMatcherCondition::QUERY_EQUALS, pattern);
354 } 386 }
355 387
356 URLMatcherCondition 388 URLMatcherCondition
357 URLMatcherConditionFactory::CreateHostSuffixPathPrefixCondition( 389 URLMatcherConditionFactory::CreateHostSuffixPathPrefixCondition(
358 const std::string& host_suffix, 390 const std::string& host_suffix,
359 const std::string& path_prefix) { 391 const std::string& path_prefix) {
360 return CreateCondition(URLMatcherCondition::HOST_SUFFIX_PATH_PREFIX, 392 return CreateCondition(URLMatcherCondition::HOST_SUFFIX_PATH_PREFIX,
361 host_suffix + kEndOfDomain + path_prefix); 393 host_suffix + kEndOfDomain + path_prefix);
(...skipping 509 matching lines...) Expand 10 before | Expand all | Expand 10 after
871 903
872 void URLMatcher::UpdateInternalDatastructures() { 904 void URLMatcher::UpdateInternalDatastructures() {
873 UpdateSubstringSetMatcher(false); 905 UpdateSubstringSetMatcher(false);
874 UpdateSubstringSetMatcher(true); 906 UpdateSubstringSetMatcher(true);
875 UpdateRegexSetMatcher(); 907 UpdateRegexSetMatcher();
876 UpdateTriggers(); 908 UpdateTriggers();
877 UpdateConditionFactory(); 909 UpdateConditionFactory();
878 } 910 }
879 911
880 } // namespace url_matcher 912 } // namespace url_matcher
OLDNEW
« no previous file with comments | « components/url_matcher/url_matcher.h ('k') | components/url_matcher/url_matcher_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698