Chromium Code Reviews| Index: extensions/common/matcher/url_matcher.cc |
| diff --git a/extensions/common/matcher/url_matcher.cc b/extensions/common/matcher/url_matcher.cc |
| index d7779a259206ccfb551d0d23bad92402e121a157..a44a20409654b2fb8588e903ef385d7abddf5048 100644 |
| --- a/extensions/common/matcher/url_matcher.cc |
| +++ b/extensions/common/matcher/url_matcher.cc |
| @@ -146,6 +146,10 @@ bool IsRegexCriterion(URLMatcherCondition::Criterion criterion) { |
| return criterion == URLMatcherCondition::URL_MATCHES; |
| } |
| +bool IsStrippedUrlRegexCriterion(URLMatcherCondition::Criterion criterion) { |
| + return criterion == URLMatcherCondition::STRIPPED_URL_MATCHES; |
| +} |
| + |
| } // namespace |
| // |
| @@ -209,6 +213,10 @@ bool URLMatcherCondition::IsRegexCondition() const { |
| return IsRegexCriterion(criterion_); |
| } |
| +bool URLMatcherCondition::IsStrippedUrlRegexCondition() const { |
| + return IsStrippedUrlRegexCriterion(criterion_); |
| +} |
| + |
| bool URLMatcherCondition::IsMatch( |
| const std::set<StringPattern::ID>& matching_patterns, |
| const GURL& url) const { |
| @@ -251,6 +259,7 @@ URLMatcherConditionFactory::URLMatcherConditionFactory() : id_counter_(0) {} |
| URLMatcherConditionFactory::~URLMatcherConditionFactory() { |
| STLDeleteElements(&substring_pattern_singletons_); |
| STLDeleteElements(®ex_pattern_singletons_); |
| + STLDeleteElements(&stripped_url_regex_pattern_singletons_); |
| } |
| std::string URLMatcherConditionFactory::CanonicalizeURLForComponentSearches( |
| @@ -380,12 +389,15 @@ std::string URLMatcherConditionFactory::CanonicalizeURLForFullSearches( |
| kEndOfURL; |
| } |
| -std::string URLMatcherConditionFactory::CanonicalizeURLForRegexSearches( |
| - const GURL& url) const { |
| +static std::string CanonicalizeURLForRegexSearchesHelper( |
|
Yoyo Zhou
2013/04/08 18:20:43
static function: should this be in the anonymous n
battre
2013/04/09 12:22:23
I considered this but concluded that it is more re
|
| + const GURL& url, |
| + bool clear_query) { |
| GURL::Replacements replacements; |
| replacements.ClearPassword(); |
| replacements.ClearUsername(); |
| replacements.ClearRef(); |
| + if (clear_query) |
| + replacements.ClearQuery(); |
| // Clear port if it is implicit from scheme. |
| if (url.has_port()) { |
| const std::string& port = url.scheme(); |
| @@ -397,6 +409,17 @@ std::string URLMatcherConditionFactory::CanonicalizeURLForRegexSearches( |
| return url.ReplaceComponents(replacements).spec(); |
| } |
| +std::string URLMatcherConditionFactory::CanonicalizeURLForRegexSearches( |
| + const GURL& url) const { |
| + return CanonicalizeURLForRegexSearchesHelper(url, false); |
| +} |
| + |
| +std::string |
| +URLMatcherConditionFactory::CanonicalizeURLForStrippedUrlRegexSearches( |
| + const GURL& url) const { |
| + return CanonicalizeURLForRegexSearchesHelper(url, true); |
| +} |
| + |
| URLMatcherCondition URLMatcherConditionFactory::CreateURLPrefixCondition( |
| const std::string& prefix) { |
| return CreateCondition(URLMatcherCondition::URL_PREFIX, |
| @@ -424,11 +447,17 @@ URLMatcherCondition URLMatcherConditionFactory::CreateURLMatchesCondition( |
| return CreateCondition(URLMatcherCondition::URL_MATCHES, regex); |
| } |
| +URLMatcherCondition |
| +URLMatcherConditionFactory::CreateStrippedURLMatchesCondition( |
| + const std::string& regex) { |
| + return CreateCondition(URLMatcherCondition::STRIPPED_URL_MATCHES, regex); |
| +} |
| + |
| void URLMatcherConditionFactory::ForgetUnusedPatterns( |
| const std::set<StringPattern::ID>& used_patterns) { |
| PatternSingletons::iterator i = substring_pattern_singletons_.begin(); |
| while (i != substring_pattern_singletons_.end()) { |
| - if (used_patterns.find((*i)->id()) != used_patterns.end()) { |
| + if (ContainsKey(used_patterns, (*i)->id())) { |
| ++i; |
| } else { |
| delete *i; |
| @@ -437,27 +466,41 @@ void URLMatcherConditionFactory::ForgetUnusedPatterns( |
| } |
| i = regex_pattern_singletons_.begin(); |
| while (i != regex_pattern_singletons_.end()) { |
| - if (used_patterns.find((*i)->id()) != used_patterns.end()) { |
| + if (ContainsKey(used_patterns, (*i)->id())) { |
| ++i; |
| } else { |
| delete *i; |
| regex_pattern_singletons_.erase(i++); |
| } |
| } |
| + i = stripped_url_regex_pattern_singletons_.begin(); |
| + while (i != stripped_url_regex_pattern_singletons_.end()) { |
| + if (ContainsKey(used_patterns, (*i)->id())) { |
| + ++i; |
| + } else { |
| + delete *i; |
| + stripped_url_regex_pattern_singletons_.erase(i++); |
| + } |
| + } |
| } |
| bool URLMatcherConditionFactory::IsEmpty() const { |
| return substring_pattern_singletons_.empty() && |
| - regex_pattern_singletons_.empty(); |
| + regex_pattern_singletons_.empty() && |
| + stripped_url_regex_pattern_singletons_.empty(); |
| } |
| URLMatcherCondition URLMatcherConditionFactory::CreateCondition( |
| URLMatcherCondition::Criterion criterion, |
| const std::string& pattern) { |
| StringPattern search_pattern(pattern, 0); |
| - PatternSingletons* pattern_singletons = |
| - IsRegexCriterion(criterion) ? ®ex_pattern_singletons_ |
| - : &substring_pattern_singletons_; |
| + PatternSingletons* pattern_singletons = NULL; |
| + if (IsRegexCriterion(criterion)) |
| + pattern_singletons = ®ex_pattern_singletons_; |
| + else if (IsStrippedUrlRegexCriterion(criterion)) |
| + pattern_singletons = &stripped_url_regex_pattern_singletons_; |
| + else |
| + pattern_singletons = &substring_pattern_singletons_; |
| PatternSingletons::const_iterator iter = |
| pattern_singletons->find(&search_pattern); |
| @@ -618,12 +661,23 @@ std::set<URLMatcherConditionSet::ID> URLMatcher::MatchURL( |
| // See URLMatcherConditionFactory for the canonicalization of URLs and the |
| // distinction between full url searches and url component searches. |
| std::set<StringPattern::ID> matches; |
| - full_url_matcher_.Match( |
| - condition_factory_.CanonicalizeURLForFullSearches(url), &matches); |
| - url_component_matcher_.Match( |
| - condition_factory_.CanonicalizeURLForComponentSearches(url), &matches); |
| - regex_set_matcher_.Match( |
| - condition_factory_.CanonicalizeURLForRegexSearches(url), &matches); |
| + if (!full_url_matcher_.IsEmpty()) { |
| + full_url_matcher_.Match( |
| + condition_factory_.CanonicalizeURLForFullSearches(url), &matches); |
| + } |
| + if (!url_component_matcher_.IsEmpty()) { |
| + url_component_matcher_.Match( |
| + condition_factory_.CanonicalizeURLForComponentSearches(url), &matches); |
| + } |
| + if (!regex_set_matcher_.IsEmpty()) { |
| + regex_set_matcher_.Match( |
| + condition_factory_.CanonicalizeURLForRegexSearches(url), &matches); |
| + } |
| + if (!stripped_url_regex_set_matcher_.IsEmpty()) { |
| + stripped_url_regex_set_matcher_.Match( |
| + condition_factory_.CanonicalizeURLForStrippedUrlRegexSearches(url), |
| + &matches); |
| + } |
| // Calculate all URLMatcherConditionSets for which all URLMatcherConditions |
| // were fulfilled. |
| @@ -659,6 +713,8 @@ bool URLMatcher::IsEmpty() const { |
| substring_match_triggers_.empty() && |
| full_url_matcher_.IsEmpty() && |
| url_component_matcher_.IsEmpty() && |
| + regex_set_matcher_.IsEmpty() && |
| + stripped_url_regex_set_matcher_.IsEmpty() && |
| registered_full_url_patterns_.empty() && |
| registered_url_component_patterns_.empty(); |
| } |
| @@ -683,6 +739,7 @@ void URLMatcher::UpdateSubstringSetMatcher(bool full_url_conditions) { |
| // If we are called to process Full URL searches, ignore others, and |
| // vice versa. (Regex conditions are updated in UpdateRegexSetMatcher.) |
| if (!condition_iter->IsRegexCondition() && |
| + !condition_iter->IsStrippedUrlRegexCondition() && |
| full_url_conditions == condition_iter->IsFullURLCondition()) |
| new_patterns.insert(condition_iter->string_pattern()); |
| } |
| @@ -722,6 +779,7 @@ void URLMatcher::UpdateSubstringSetMatcher(bool full_url_conditions) { |
| void URLMatcher::UpdateRegexSetMatcher() { |
| std::vector<const StringPattern*> new_patterns; |
| + std::vector<const StringPattern*> new_stripped_url_patterns; |
| for (URLMatcherConditionSets::const_iterator condition_set_iter = |
| url_matcher_condition_sets_.begin(); |
| @@ -734,6 +792,8 @@ void URLMatcher::UpdateRegexSetMatcher() { |
| ++condition_iter) { |
| if (condition_iter->IsRegexCondition()) |
| new_patterns.push_back(condition_iter->string_pattern()); |
| + else if (condition_iter->IsStrippedUrlRegexCondition()) |
| + new_stripped_url_patterns.push_back(condition_iter->string_pattern()); |
| } |
| } |
| @@ -741,6 +801,8 @@ void URLMatcher::UpdateRegexSetMatcher() { |
| // FilteredRE2 backend doesn't support incremental updates. |
| regex_set_matcher_.ClearPatterns(); |
| regex_set_matcher_.AddPatterns(new_patterns); |
| + stripped_url_regex_set_matcher_.ClearPatterns(); |
| + stripped_url_regex_set_matcher_.AddPatterns(new_stripped_url_patterns); |
| } |
| void URLMatcher::UpdateTriggers() { |