Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 189 return false; | 189 return false; |
| 190 } | 190 } |
| 191 | 191 |
| 192 bool URLMatcherCondition::IsFullURLCondition() const { | 192 bool URLMatcherCondition::IsFullURLCondition() const { |
| 193 // For these criteria the SubstringMatcher needs to be executed on the | 193 // For these criteria the SubstringMatcher needs to be executed on the |
| 194 // GURL that is canonicalized with | 194 // GURL that is canonicalized with |
| 195 // URLMatcherConditionFactory::CanonicalizeURLForFullSearches. | 195 // URLMatcherConditionFactory::CanonicalizeURLForFullSearches. |
| 196 switch (criterion_) { | 196 switch (criterion_) { |
| 197 case HOST_CONTAINS: | 197 case HOST_CONTAINS: |
| 198 case PATH_CONTAINS: | 198 case PATH_CONTAINS: |
| 199 case QUERY_CONTAINS: | |
| 200 case URL_PREFIX: | 199 case URL_PREFIX: |
| 201 case URL_SUFFIX: | 200 case URL_SUFFIX: |
| 202 case URL_CONTAINS: | 201 case URL_CONTAINS: |
| 203 case URL_EQUALS: | 202 case URL_EQUALS: |
| 204 return true; | 203 return true; |
| 205 default: | 204 default: |
| 206 break; | 205 break; |
| 207 } | 206 } |
| 208 return false; | 207 return false; |
| 209 } | 208 } |
| 210 | 209 |
| 211 bool URLMatcherCondition::IsRegexCondition() const { | 210 bool URLMatcherCondition::IsRegexCondition() const { |
| 212 return IsRegexCriterion(criterion_); | 211 return IsRegexCriterion(criterion_); |
| 213 } | 212 } |
| 214 | 213 |
| 215 bool URLMatcherCondition::IsOriginAndPathRegexCondition() const { | 214 bool URLMatcherCondition::IsOriginAndPathRegexCondition() const { |
| 216 return IsOriginAndPathRegexCriterion(criterion_); | 215 return IsOriginAndPathRegexCriterion(criterion_); |
| 217 } | 216 } |
| 218 | 217 |
| 219 bool URLMatcherCondition::IsMatch( | 218 bool URLMatcherCondition::IsMatch( |
| 220 const std::set<StringPattern::ID>& matching_patterns, | 219 const std::set<StringPattern::ID>& matching_patterns, |
| 221 const GURL& url) const { | 220 const GURL& url) const { |
| 222 DCHECK(string_pattern_); | 221 DCHECK(string_pattern_); |
| 223 if (!ContainsKey(matching_patterns, string_pattern_->id())) | 222 if (!ContainsKey(matching_patterns, string_pattern_->id())) |
| 224 return false; | 223 return false; |
| 225 // The criteria HOST_CONTAINS, PATH_CONTAINS, QUERY_CONTAINS are based on | 224 // The criteria HOST_CONTAINS, PATH_CONTAINS are based on a substring match on |
| 226 // a substring match on the raw URL. In case of a match, we need to verify | 225 // the raw URL. In case of a match, we need to verify that the match was found |
| 227 // that the match was found in the correct component of the URL. | 226 // in the correct component of the URL. |
| 228 switch (criterion_) { | 227 switch (criterion_) { |
| 229 case HOST_CONTAINS: | 228 case HOST_CONTAINS: |
| 230 return url.host().find(string_pattern_->pattern()) != | 229 return url.host().find(string_pattern_->pattern()) != |
| 231 std::string::npos; | 230 std::string::npos; |
| 232 case PATH_CONTAINS: | 231 case PATH_CONTAINS: |
| 233 return url.path().find(string_pattern_->pattern()) != | 232 return url.path().find(string_pattern_->pattern()) != |
| 234 std::string::npos; | 233 std::string::npos; |
| 235 case QUERY_CONTAINS: | |
| 236 return url.query().find(string_pattern_->pattern()) != | |
| 237 std::string::npos; | |
| 238 default: | 234 default: |
| 239 break; | 235 break; |
| 240 } | 236 } |
| 241 return true; | 237 return true; |
| 242 } | 238 } |
| 243 | 239 |
| 244 // | 240 // |
| 245 // URLMatcherConditionFactory | 241 // URLMatcherConditionFactory |
| 246 // | 242 // |
| 247 | 243 |
| 248 namespace { | 244 namespace { |
| 249 // These are symbols that are not contained in 7-bit ASCII used in GURLs. | 245 // These are symbols that are not contained in 7-bit ASCII used in GURLs. |
| 250 const char kBeginningOfURL[] = {static_cast<char>(-1), 0}; | 246 const char kBeginningOfURL[] = {static_cast<char>(-1), 0}; |
| 251 const char kEndOfDomain[] = {static_cast<char>(-2), 0}; | 247 const char kEndOfDomain[] = {static_cast<char>(-2), 0}; |
| 252 const char kEndOfPath[] = {static_cast<char>(-3), 0}; | 248 const char kEndOfPath[] = {static_cast<char>(-3), 0}; |
| 253 const char kEndOfURL[] = {static_cast<char>(-4), 0}; | 249 const char kBeginningOfQueryComponent[] = {static_cast<char>(-4), 0}; |
| 250 const char kEndOfURL[] = {static_cast<char>(-5), 0}; | |
| 251 const char kQuerySeperator = '&'; | |
| 252 | |
| 253 std::string PrepareQuery(std::string query, bool prepend_query_start = true) { | |
|
battre
2014/04/01 08:23:00
Please add a description of the function.
Please
Joao da Silva
2014/04/01 09:18:47
Default parameters are not allowed by the style gu
battre
2014/04/01 09:29:40
I thought so as well and then removed the comment.
kaliamoorthi
2014/04/01 13:11:17
Done.
kaliamoorthi
2014/04/01 13:11:17
I have removed the default parameter anyway.
| |
| 254 for (std::string::iterator it = query.begin(); it != query.end(); ++it) { | |
| 255 if (*it == kQuerySeperator) | |
| 256 *it = kBeginningOfQueryComponent[0]; | |
| 257 } | |
| 258 return prepend_query_start ? kBeginningOfQueryComponent + query : query; | |
| 259 } | |
| 254 } // namespace | 260 } // namespace |
| 255 | 261 |
| 256 URLMatcherConditionFactory::URLMatcherConditionFactory() : id_counter_(0) {} | 262 URLMatcherConditionFactory::URLMatcherConditionFactory() : id_counter_(0) {} |
| 257 | 263 |
| 258 URLMatcherConditionFactory::~URLMatcherConditionFactory() { | 264 URLMatcherConditionFactory::~URLMatcherConditionFactory() { |
| 259 STLDeleteElements(&substring_pattern_singletons_); | 265 STLDeleteElements(&substring_pattern_singletons_); |
| 260 STLDeleteElements(®ex_pattern_singletons_); | 266 STLDeleteElements(®ex_pattern_singletons_); |
| 261 STLDeleteElements(&origin_and_path_regex_pattern_singletons_); | 267 STLDeleteElements(&origin_and_path_regex_pattern_singletons_); |
| 262 } | 268 } |
| 263 | 269 |
| 264 std::string URLMatcherConditionFactory::CanonicalizeURLForComponentSearches( | 270 std::string URLMatcherConditionFactory::CanonicalizeURLForComponentSearches( |
| 265 const GURL& url) const { | 271 const GURL& url) const { |
| 266 return kBeginningOfURL + CanonicalizeHostname(url.host()) + kEndOfDomain + | 272 return kBeginningOfURL + CanonicalizeHostname(url.host()) + kEndOfDomain + |
| 267 url.path() + kEndOfPath + | 273 url.path() + kEndOfPath + |
| 268 (url.has_query() ? "?" + url.query() : std::string()) + kEndOfURL; | 274 (url.has_query() ? PrepareQuery(url.query()) : std::string()) + |
| 275 kEndOfURL; | |
| 269 } | 276 } |
| 270 | 277 |
| 271 URLMatcherCondition URLMatcherConditionFactory::CreateHostPrefixCondition( | 278 URLMatcherCondition URLMatcherConditionFactory::CreateHostPrefixCondition( |
| 272 const std::string& prefix) { | 279 const std::string& prefix) { |
| 273 return CreateCondition(URLMatcherCondition::HOST_PREFIX, | 280 return CreateCondition(URLMatcherCondition::HOST_PREFIX, |
| 274 kBeginningOfURL + CanonicalizeHostname(prefix)); | 281 kBeginningOfURL + CanonicalizeHostname(prefix)); |
| 275 } | 282 } |
| 276 | 283 |
| 277 URLMatcherCondition URLMatcherConditionFactory::CreateHostSuffixCondition( | 284 URLMatcherCondition URLMatcherConditionFactory::CreateHostSuffixCondition( |
| 278 const std::string& suffix) { | 285 const std::string& suffix) { |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 309 | 316 |
| 310 URLMatcherCondition URLMatcherConditionFactory::CreatePathEqualsCondition( | 317 URLMatcherCondition URLMatcherConditionFactory::CreatePathEqualsCondition( |
| 311 const std::string& str) { | 318 const std::string& str) { |
| 312 return CreateCondition(URLMatcherCondition::PATH_EQUALS, | 319 return CreateCondition(URLMatcherCondition::PATH_EQUALS, |
| 313 kEndOfDomain + str + kEndOfPath); | 320 kEndOfDomain + str + kEndOfPath); |
| 314 } | 321 } |
| 315 | 322 |
| 316 URLMatcherCondition URLMatcherConditionFactory::CreateQueryPrefixCondition( | 323 URLMatcherCondition URLMatcherConditionFactory::CreateQueryPrefixCondition( |
| 317 const std::string& prefix) { | 324 const std::string& prefix) { |
| 318 std::string pattern; | 325 std::string pattern; |
| 319 if (!prefix.empty() && prefix[0] == '?') | 326 pattern = |
|
battre
2014/04/01 08:23:00
Either the assignment should become part of the co
kaliamoorthi
2014/04/01 13:11:17
I have reintroduced the if else.
| |
| 320 pattern = kEndOfPath + prefix; | 327 kEndOfPath + PrepareQuery((!prefix.empty() && prefix[0] == '?') |
| 321 else | 328 ? prefix.substr(1, prefix.length() - 1) |
|
battre
2014/04/01 08:23:00
nit: prefix.substr(1) should be sufficient, right?
kaliamoorthi
2014/04/01 13:11:17
That simplifies the expression. Fixed it.
| |
| 322 pattern = kEndOfPath + ('?' + prefix); | 329 : prefix); |
| 323 | 330 |
| 324 return CreateCondition(URLMatcherCondition::QUERY_PREFIX, pattern); | 331 return CreateCondition(URLMatcherCondition::QUERY_PREFIX, pattern); |
| 325 } | 332 } |
| 326 | 333 |
| 327 URLMatcherCondition URLMatcherConditionFactory::CreateQuerySuffixCondition( | 334 URLMatcherCondition URLMatcherConditionFactory::CreateQuerySuffixCondition( |
| 328 const std::string& suffix) { | 335 const std::string& suffix) { |
| 329 if (!suffix.empty() && suffix[0] == '?') { | 336 if (!suffix.empty() && suffix[0] == '?') { |
| 330 return CreateQueryEqualsCondition(suffix); | 337 return CreateQueryEqualsCondition(suffix); |
| 331 } else { | 338 } else { |
| 332 return CreateCondition(URLMatcherCondition::QUERY_SUFFIX, | 339 return CreateCondition(URLMatcherCondition::QUERY_SUFFIX, |
| 333 suffix + kEndOfURL); | 340 PrepareQuery(suffix, false) + kEndOfURL); |
| 334 } | 341 } |
| 335 } | 342 } |
| 336 | 343 |
| 337 URLMatcherCondition URLMatcherConditionFactory::CreateQueryContainsCondition( | 344 URLMatcherCondition URLMatcherConditionFactory::CreateQueryContainsCondition( |
| 338 const std::string& str) { | 345 const std::string& str) { |
| 339 if (!str.empty() && str[0] == '?') | 346 std::string query_condition; |
| 340 return CreateQueryPrefixCondition(str); | 347 query_condition = |
| 341 else | 348 (!str.empty() && str[0] == '?') ? str.substr(1, str.length() - 1) : str; |
|
battre
2014/04/01 08:23:00
Can you re-introduce the if statement (also below)
kaliamoorthi
2014/04/01 13:11:17
Done.
| |
| 342 return CreateCondition(URLMatcherCondition::QUERY_CONTAINS, str); | 349 return CreateCondition(URLMatcherCondition::QUERY_CONTAINS, |
| 350 PrepareQuery(query_condition)); | |
| 343 } | 351 } |
| 344 | 352 |
| 345 URLMatcherCondition URLMatcherConditionFactory::CreateQueryEqualsCondition( | 353 URLMatcherCondition URLMatcherConditionFactory::CreateQueryEqualsCondition( |
| 346 const std::string& str) { | 354 const std::string& str) { |
| 347 std::string pattern; | 355 std::string pattern; |
| 348 if (!str.empty() && str[0] == '?') | 356 pattern = kEndOfPath + PrepareQuery((!str.empty() && str[0] == '?') |
| 349 pattern = kEndOfPath + str + kEndOfURL; | 357 ? str.substr(1, str.length() - 1) |
| 350 else | 358 : str) + |
| 351 pattern = kEndOfPath + ('?' + str) + kEndOfURL; | 359 kEndOfURL; |
| 352 | 360 |
| 353 return CreateCondition(URLMatcherCondition::QUERY_EQUALS, pattern); | 361 return CreateCondition(URLMatcherCondition::QUERY_EQUALS, pattern); |
| 354 } | 362 } |
| 355 | 363 |
| 356 URLMatcherCondition | 364 URLMatcherCondition |
| 357 URLMatcherConditionFactory::CreateHostSuffixPathPrefixCondition( | 365 URLMatcherConditionFactory::CreateHostSuffixPathPrefixCondition( |
| 358 const std::string& host_suffix, | 366 const std::string& host_suffix, |
| 359 const std::string& path_prefix) { | 367 const std::string& path_prefix) { |
| 360 return CreateCondition(URLMatcherCondition::HOST_SUFFIX_PATH_PREFIX, | 368 return CreateCondition(URLMatcherCondition::HOST_SUFFIX_PATH_PREFIX, |
| 361 host_suffix + kEndOfDomain + path_prefix); | 369 host_suffix + kEndOfDomain + path_prefix); |
| (...skipping 509 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 871 | 879 |
| 872 void URLMatcher::UpdateInternalDatastructures() { | 880 void URLMatcher::UpdateInternalDatastructures() { |
| 873 UpdateSubstringSetMatcher(false); | 881 UpdateSubstringSetMatcher(false); |
| 874 UpdateSubstringSetMatcher(true); | 882 UpdateSubstringSetMatcher(true); |
| 875 UpdateRegexSetMatcher(); | 883 UpdateRegexSetMatcher(); |
| 876 UpdateTriggers(); | 884 UpdateTriggers(); |
| 877 UpdateConditionFactory(); | 885 UpdateConditionFactory(); |
| 878 } | 886 } |
| 879 | 887 |
| 880 } // namespace url_matcher | 888 } // namespace url_matcher |
| OLD | NEW |