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 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/ptr_util.h" | |
| 12 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
| 13 #include "url/gurl.h" | 14 #include "url/gurl.h" |
| 14 #include "url/url_canon.h" | 15 #include "url/url_canon.h" |
| 15 | 16 |
| 16 namespace url_matcher { | 17 namespace url_matcher { |
| 17 | 18 |
| 18 // This set of classes implement a mapping of URL Component Patterns, such as | 19 // This set of classes implement a mapping of URL Component Patterns, such as |
| 19 // host_prefix, host_suffix, host_equals, ..., etc., to StringPatterns | 20 // host_prefix, host_suffix, host_equals, ..., etc., to StringPatterns |
| 20 // for use in substring comparisons. | 21 // for use in substring comparisons. |
| 21 // | 22 // |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 255 const char kQueryComponentDelimiter[] = {static_cast<char>(-4), 0}; | 256 const char kQueryComponentDelimiter[] = {static_cast<char>(-4), 0}; |
| 256 const char kEndOfURL[] = {static_cast<char>(-5), 0}; | 257 const char kEndOfURL[] = {static_cast<char>(-5), 0}; |
| 257 | 258 |
| 258 // The delimiter for query parameters | 259 // The delimiter for query parameters |
| 259 const char kQuerySeparator = '&'; | 260 const char kQuerySeparator = '&'; |
| 260 } // namespace | 261 } // namespace |
| 261 | 262 |
| 262 URLMatcherConditionFactory::URLMatcherConditionFactory() : id_counter_(0) {} | 263 URLMatcherConditionFactory::URLMatcherConditionFactory() : id_counter_(0) {} |
| 263 | 264 |
| 264 URLMatcherConditionFactory::~URLMatcherConditionFactory() { | 265 URLMatcherConditionFactory::~URLMatcherConditionFactory() { |
| 265 base::STLDeleteElements(&substring_pattern_singletons_); | |
| 266 base::STLDeleteElements(®ex_pattern_singletons_); | |
| 267 base::STLDeleteElements(&origin_and_path_regex_pattern_singletons_); | |
| 268 } | 266 } |
| 269 | 267 |
| 270 std::string URLMatcherConditionFactory::CanonicalizeURLForComponentSearches( | 268 std::string URLMatcherConditionFactory::CanonicalizeURLForComponentSearches( |
| 271 const GURL& url) const { | 269 const GURL& url) const { |
| 272 return kBeginningOfURL + CanonicalizeHostname(url.host()) + kEndOfDomain + | 270 return kBeginningOfURL + CanonicalizeHostname(url.host()) + kEndOfDomain + |
| 273 url.path() + kEndOfPath + | 271 url.path() + kEndOfPath + |
| 274 (url.has_query() ? CanonicalizeQuery(url.query(), true, true) | 272 (url.has_query() ? CanonicalizeQuery(url.query(), true, true) |
| 275 : std::string()) + | 273 : std::string()) + |
| 276 kEndOfURL; | 274 kEndOfURL; |
| 277 } | 275 } |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 456 } | 454 } |
| 457 | 455 |
| 458 URLMatcherCondition | 456 URLMatcherCondition |
| 459 URLMatcherConditionFactory::CreateOriginAndPathMatchesCondition( | 457 URLMatcherConditionFactory::CreateOriginAndPathMatchesCondition( |
| 460 const std::string& regex) { | 458 const std::string& regex) { |
| 461 return CreateCondition(URLMatcherCondition::ORIGIN_AND_PATH_MATCHES, regex); | 459 return CreateCondition(URLMatcherCondition::ORIGIN_AND_PATH_MATCHES, regex); |
| 462 } | 460 } |
| 463 | 461 |
| 464 void URLMatcherConditionFactory::ForgetUnusedPatterns( | 462 void URLMatcherConditionFactory::ForgetUnusedPatterns( |
| 465 const std::set<StringPattern::ID>& used_patterns) { | 463 const std::set<StringPattern::ID>& used_patterns) { |
| 466 PatternSingletons::iterator i = substring_pattern_singletons_.begin(); | 464 auto i = substring_pattern_singletons_.begin(); |
| 467 while (i != substring_pattern_singletons_.end()) { | 465 while (i != substring_pattern_singletons_.end()) { |
| 468 if (base::ContainsKey(used_patterns, (*i)->id())) { | 466 if (base::ContainsKey(used_patterns, i->first->id())) |
| 469 ++i; | 467 ++i; |
| 470 } else { | 468 else |
| 471 delete *i; | |
| 472 substring_pattern_singletons_.erase(i++); | 469 substring_pattern_singletons_.erase(i++); |
| 473 } | |
| 474 } | 470 } |
| 471 | |
| 475 i = regex_pattern_singletons_.begin(); | 472 i = regex_pattern_singletons_.begin(); |
| 476 while (i != regex_pattern_singletons_.end()) { | 473 while (i != regex_pattern_singletons_.end()) { |
| 477 if (base::ContainsKey(used_patterns, (*i)->id())) { | 474 if (base::ContainsKey(used_patterns, i->first->id())) |
| 478 ++i; | 475 ++i; |
| 479 } else { | 476 else |
| 480 delete *i; | |
| 481 regex_pattern_singletons_.erase(i++); | 477 regex_pattern_singletons_.erase(i++); |
| 482 } | |
| 483 } | 478 } |
| 479 | |
| 484 i = origin_and_path_regex_pattern_singletons_.begin(); | 480 i = origin_and_path_regex_pattern_singletons_.begin(); |
| 485 while (i != origin_and_path_regex_pattern_singletons_.end()) { | 481 while (i != origin_and_path_regex_pattern_singletons_.end()) { |
| 486 if (base::ContainsKey(used_patterns, (*i)->id())) { | 482 if (base::ContainsKey(used_patterns, i->first->id())) |
| 487 ++i; | 483 ++i; |
| 488 } else { | 484 else |
| 489 delete *i; | |
| 490 origin_and_path_regex_pattern_singletons_.erase(i++); | 485 origin_and_path_regex_pattern_singletons_.erase(i++); |
| 491 } | |
| 492 } | 486 } |
| 493 } | 487 } |
| 494 | 488 |
| 495 bool URLMatcherConditionFactory::IsEmpty() const { | 489 bool URLMatcherConditionFactory::IsEmpty() const { |
| 496 return substring_pattern_singletons_.empty() && | 490 return substring_pattern_singletons_.empty() && |
| 497 regex_pattern_singletons_.empty() && | 491 regex_pattern_singletons_.empty() && |
| 498 origin_and_path_regex_pattern_singletons_.empty(); | 492 origin_and_path_regex_pattern_singletons_.empty(); |
| 499 } | 493 } |
| 500 | 494 |
| 501 URLMatcherCondition URLMatcherConditionFactory::CreateCondition( | 495 URLMatcherCondition URLMatcherConditionFactory::CreateCondition( |
| 502 URLMatcherCondition::Criterion criterion, | 496 URLMatcherCondition::Criterion criterion, |
| 503 const std::string& pattern) { | 497 const std::string& pattern) { |
| 504 StringPattern search_pattern(pattern, 0); | 498 StringPattern search_pattern(pattern, id_counter_++); |
|
battre
2016/10/24 11:32:16
why did you change this?
Avi (use Gerrit)
2016/10/24 14:38:06
Gah. It was a remnant of an earlier attempt to fix
| |
| 505 PatternSingletons* pattern_singletons = NULL; | 499 PatternSingletons* pattern_singletons = NULL; |
| 506 if (IsRegexCriterion(criterion)) | 500 if (IsRegexCriterion(criterion)) |
| 507 pattern_singletons = ®ex_pattern_singletons_; | 501 pattern_singletons = ®ex_pattern_singletons_; |
| 508 else if (IsOriginAndPathRegexCriterion(criterion)) | 502 else if (IsOriginAndPathRegexCriterion(criterion)) |
| 509 pattern_singletons = &origin_and_path_regex_pattern_singletons_; | 503 pattern_singletons = &origin_and_path_regex_pattern_singletons_; |
| 510 else | 504 else |
| 511 pattern_singletons = &substring_pattern_singletons_; | 505 pattern_singletons = &substring_pattern_singletons_; |
| 512 | 506 |
| 513 PatternSingletons::const_iterator iter = | 507 auto iter = pattern_singletons->find(&search_pattern); |
| 514 pattern_singletons->find(&search_pattern); | |
| 515 | 508 |
| 516 if (iter != pattern_singletons->end()) | 509 if (iter != pattern_singletons->end()) |
| 517 return URLMatcherCondition(criterion, *iter); | 510 return URLMatcherCondition(criterion, iter->first); |
| 518 | 511 |
| 519 StringPattern* new_pattern = new StringPattern(pattern, id_counter_++); | 512 StringPattern* new_pattern = new StringPattern(pattern, id_counter_++); |
| 520 pattern_singletons->insert(new_pattern); | 513 (*pattern_singletons)[new_pattern] = base::WrapUnique(new_pattern); |
| 521 return URLMatcherCondition(criterion, new_pattern); | 514 return URLMatcherCondition(criterion, new_pattern); |
| 522 } | 515 } |
| 523 | 516 |
| 524 std::string URLMatcherConditionFactory::CanonicalizeHostSuffix( | 517 std::string URLMatcherConditionFactory::CanonicalizeHostSuffix( |
| 525 const std::string& suffix) const { | 518 const std::string& suffix) const { |
| 526 if (suffix.empty()) | 519 if (suffix.empty()) |
| 527 return "."; | 520 return "."; |
| 528 return suffix.back() == '.' ? suffix : suffix + "."; | 521 return suffix.back() == '.' ? suffix : suffix + "."; |
| 529 } | 522 } |
| 530 | 523 |
| (...skipping 565 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1096 | 1089 |
| 1097 void URLMatcher::UpdateInternalDatastructures() { | 1090 void URLMatcher::UpdateInternalDatastructures() { |
| 1098 UpdateSubstringSetMatcher(false); | 1091 UpdateSubstringSetMatcher(false); |
| 1099 UpdateSubstringSetMatcher(true); | 1092 UpdateSubstringSetMatcher(true); |
| 1100 UpdateRegexSetMatcher(); | 1093 UpdateRegexSetMatcher(); |
| 1101 UpdateTriggers(); | 1094 UpdateTriggers(); |
| 1102 UpdateConditionFactory(); | 1095 UpdateConditionFactory(); |
| 1103 } | 1096 } |
| 1104 | 1097 |
| 1105 } // namespace url_matcher | 1098 } // namespace url_matcher |
| OLD | NEW |