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

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: Fixes nit from Dominic 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 232 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 243
244 // 244 //
245 // URLMatcherConditionFactory 245 // URLMatcherConditionFactory
246 // 246 //
247 247
248 namespace { 248 namespace {
249 // These are symbols that are not contained in 7-bit ASCII used in GURLs. 249 // These are symbols that are not contained in 7-bit ASCII used in GURLs.
250 const char kBeginningOfURL[] = {static_cast<char>(-1), 0}; 250 const char kBeginningOfURL[] = {static_cast<char>(-1), 0};
251 const char kEndOfDomain[] = {static_cast<char>(-2), 0}; 251 const char kEndOfDomain[] = {static_cast<char>(-2), 0};
252 const char kEndOfPath[] = {static_cast<char>(-3), 0}; 252 const char kEndOfPath[] = {static_cast<char>(-3), 0};
253 const char kEndOfURL[] = {static_cast<char>(-4), 0}; 253 const char kQueryComponentDelimiter[] = {static_cast<char>(-4), 0};
254 const char kEndOfURL[] = {static_cast<char>(-5), 0};
255
256 // The delimiter for query parameters
257 const char kQuerySeparator = '&';
254 } // namespace 258 } // namespace
255 259
256 URLMatcherConditionFactory::URLMatcherConditionFactory() : id_counter_(0) {} 260 URLMatcherConditionFactory::URLMatcherConditionFactory() : id_counter_(0) {}
257 261
258 URLMatcherConditionFactory::~URLMatcherConditionFactory() { 262 URLMatcherConditionFactory::~URLMatcherConditionFactory() {
259 STLDeleteElements(&substring_pattern_singletons_); 263 STLDeleteElements(&substring_pattern_singletons_);
260 STLDeleteElements(&regex_pattern_singletons_); 264 STLDeleteElements(&regex_pattern_singletons_);
261 STLDeleteElements(&origin_and_path_regex_pattern_singletons_); 265 STLDeleteElements(&origin_and_path_regex_pattern_singletons_);
262 } 266 }
263 267
264 std::string URLMatcherConditionFactory::CanonicalizeURLForComponentSearches( 268 std::string URLMatcherConditionFactory::CanonicalizeURLForComponentSearches(
265 const GURL& url) const { 269 const GURL& url) const {
266 return kBeginningOfURL + CanonicalizeHostname(url.host()) + kEndOfDomain + 270 return kBeginningOfURL + CanonicalizeHostname(url.host()) + kEndOfDomain +
267 url.path() + kEndOfPath + 271 url.path() + kEndOfPath +
268 (url.has_query() ? "?" + url.query() : std::string()) + kEndOfURL; 272 (url.has_query() ? CanonicalizeQuery(url.query(), true, true)
273 : std::string()) +
274 kEndOfURL;
269 } 275 }
270 276
271 URLMatcherCondition URLMatcherConditionFactory::CreateHostPrefixCondition( 277 URLMatcherCondition URLMatcherConditionFactory::CreateHostPrefixCondition(
272 const std::string& prefix) { 278 const std::string& prefix) {
273 return CreateCondition(URLMatcherCondition::HOST_PREFIX, 279 return CreateCondition(URLMatcherCondition::HOST_PREFIX,
274 kBeginningOfURL + CanonicalizeHostname(prefix)); 280 kBeginningOfURL + CanonicalizeHostname(prefix));
275 } 281 }
276 282
277 URLMatcherCondition URLMatcherConditionFactory::CreateHostSuffixCondition( 283 URLMatcherCondition URLMatcherConditionFactory::CreateHostSuffixCondition(
278 const std::string& suffix) { 284 const std::string& suffix) {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
310 URLMatcherCondition URLMatcherConditionFactory::CreatePathEqualsCondition( 316 URLMatcherCondition URLMatcherConditionFactory::CreatePathEqualsCondition(
311 const std::string& str) { 317 const std::string& str) {
312 return CreateCondition(URLMatcherCondition::PATH_EQUALS, 318 return CreateCondition(URLMatcherCondition::PATH_EQUALS,
313 kEndOfDomain + str + kEndOfPath); 319 kEndOfDomain + str + kEndOfPath);
314 } 320 }
315 321
316 URLMatcherCondition URLMatcherConditionFactory::CreateQueryPrefixCondition( 322 URLMatcherCondition URLMatcherConditionFactory::CreateQueryPrefixCondition(
317 const std::string& prefix) { 323 const std::string& prefix) {
318 std::string pattern; 324 std::string pattern;
319 if (!prefix.empty() && prefix[0] == '?') 325 if (!prefix.empty() && prefix[0] == '?')
320 pattern = kEndOfPath + prefix; 326 pattern = kEndOfPath + CanonicalizeQuery(prefix.substr(1), true, false);
321 else 327 else
322 pattern = kEndOfPath + ('?' + prefix); 328 pattern = kEndOfPath + CanonicalizeQuery(prefix, true, false);
323 329
324 return CreateCondition(URLMatcherCondition::QUERY_PREFIX, pattern); 330 return CreateCondition(URLMatcherCondition::QUERY_PREFIX, pattern);
325 } 331 }
326 332
327 URLMatcherCondition URLMatcherConditionFactory::CreateQuerySuffixCondition( 333 URLMatcherCondition URLMatcherConditionFactory::CreateQuerySuffixCondition(
328 const std::string& suffix) { 334 const std::string& suffix) {
329 if (!suffix.empty() && suffix[0] == '?') { 335 if (!suffix.empty() && suffix[0] == '?') {
330 return CreateQueryEqualsCondition(suffix); 336 return CreateQueryEqualsCondition(suffix);
331 } else { 337 } else {
332 return CreateCondition(URLMatcherCondition::QUERY_SUFFIX, 338 return CreateCondition(URLMatcherCondition::QUERY_SUFFIX,
333 suffix + kEndOfURL); 339 CanonicalizeQuery(suffix, false, true) + kEndOfURL);
334 } 340 }
335 } 341 }
336 342
337 URLMatcherCondition URLMatcherConditionFactory::CreateQueryContainsCondition( 343 URLMatcherCondition URLMatcherConditionFactory::CreateQueryContainsCondition(
338 const std::string& str) { 344 const std::string& str) {
339 if (!str.empty() && str[0] == '?') 345 if (!str.empty() && str[0] == '?')
340 return CreateQueryPrefixCondition(str); 346 return CreateQueryPrefixCondition(str);
341 else 347 else
342 return CreateCondition(URLMatcherCondition::QUERY_CONTAINS, str); 348 return CreateCondition(URLMatcherCondition::QUERY_CONTAINS, str);
343 } 349 }
344 350
345 URLMatcherCondition URLMatcherConditionFactory::CreateQueryEqualsCondition( 351 URLMatcherCondition URLMatcherConditionFactory::CreateQueryEqualsCondition(
346 const std::string& str) { 352 const std::string& str) {
347 std::string pattern; 353 std::string pattern;
348 if (!str.empty() && str[0] == '?') 354 if (!str.empty() && str[0] == '?')
349 pattern = kEndOfPath + str + kEndOfURL; 355 pattern =
356 kEndOfPath + CanonicalizeQuery(str.substr(1), true, true) + kEndOfURL;
350 else 357 else
351 pattern = kEndOfPath + ('?' + str) + kEndOfURL; 358 pattern = kEndOfPath + CanonicalizeQuery(str, true, true) + kEndOfURL;
352 359
353 return CreateCondition(URLMatcherCondition::QUERY_EQUALS, pattern); 360 return CreateCondition(URLMatcherCondition::QUERY_EQUALS, pattern);
354 } 361 }
355 362
356 URLMatcherCondition 363 URLMatcherCondition
357 URLMatcherConditionFactory::CreateHostSuffixPathPrefixCondition( 364 URLMatcherConditionFactory::CreateHostSuffixPathPrefixCondition(
358 const std::string& host_suffix, 365 const std::string& host_suffix,
359 const std::string& path_prefix) { 366 const std::string& path_prefix) {
360 return CreateCondition(URLMatcherCondition::HOST_SUFFIX_PATH_PREFIX, 367 return CreateCondition(URLMatcherCondition::HOST_SUFFIX_PATH_PREFIX,
361 host_suffix + kEndOfDomain + path_prefix); 368 host_suffix + kEndOfDomain + path_prefix);
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
515 } 522 }
516 523
517 std::string URLMatcherConditionFactory::CanonicalizeHostname( 524 std::string URLMatcherConditionFactory::CanonicalizeHostname(
518 const std::string& hostname) const { 525 const std::string& hostname) const {
519 if (!hostname.empty() && hostname[0] == '.') 526 if (!hostname.empty() && hostname[0] == '.')
520 return hostname; 527 return hostname;
521 else 528 else
522 return "." + hostname; 529 return "." + hostname;
523 } 530 }
524 531
532 // This function prepares the query string by replacing query separator with a
533 // magic value (|kQueryComponentDelimiter|). When the boolean
534 // |prepend_beginning_of_query_component| is true the function prepends the
535 // query with the same magic. This is done to locate the start of a key value
536 // pair in the query string. The parameter |query| is passed by value
537 // intentionally, since it is locally modified.
538 std::string URLMatcherConditionFactory::CanonicalizeQuery(
539 std::string query,
540 bool prepend_beginning_of_query_component,
541 bool append_end_of_query_component) const {
542 for (std::string::iterator it = query.begin(); it != query.end(); ++it) {
543 if (*it == kQuerySeparator)
544 *it = kQueryComponentDelimiter[0];
545 }
546 if (prepend_beginning_of_query_component)
547 query = kQueryComponentDelimiter + query;
548 if (append_end_of_query_component)
549 query += kQueryComponentDelimiter;
550 return query;
551 }
552
525 bool URLMatcherConditionFactory::StringPatternPointerCompare::operator()( 553 bool URLMatcherConditionFactory::StringPatternPointerCompare::operator()(
526 StringPattern* lhs, 554 StringPattern* lhs,
527 StringPattern* rhs) const { 555 StringPattern* rhs) const {
528 if (lhs == NULL && rhs != NULL) return true; 556 if (lhs == NULL && rhs != NULL) return true;
529 if (lhs != NULL && rhs != NULL) 557 if (lhs != NULL && rhs != NULL)
530 return lhs->pattern() < rhs->pattern(); 558 return lhs->pattern() < rhs->pattern();
531 // Either both are NULL or only rhs is NULL. 559 // Either both are NULL or only rhs is NULL.
532 return false; 560 return false;
533 } 561 }
534 562
535 // 563 //
564 // URLQueryElementMatcherCondition
565 //
566
567 URLQueryElementMatcherCondition::URLQueryElementMatcherCondition(
568 const std::string& key,
569 const std::string& value,
570 QueryValueMatchType query_value_match_type,
571 QueryElementType query_element_type,
572 Type match_type,
573 URLMatcherConditionFactory* factory) {
574 match_type_ = match_type;
575
576 if (query_element_type == ELEMENT_TYPE_KEY_VALUE) {
577 key_ = kQueryComponentDelimiter + key + "=";
578 value_ = value;
579 } else {
580 key_ = kQueryComponentDelimiter + key;
581 value_ = std::string();
582 }
583
584 if (query_value_match_type == QUERY_VALUE_MATCH_EXACT)
585 value_ += kQueryComponentDelimiter;
586
587 // If |value_| is empty no need to find the |key_| and verify if the value
588 // matches. Simply checking the presence of key is sufficient, which is done
589 // by MATCH_ANY
590 if (value_.empty())
591 match_type_ = MATCH_ANY;
592
593 URLMatcherCondition condition;
594 // If |match_type_| is MATCH_ANY, then we could simply look for the
595 // combination of |key_| + |value_|, which can be efficiently done by
596 // SubstringMatcher
597 if (match_type_ == MATCH_ANY)
598 condition = factory->CreateQueryContainsCondition(key_ + value_);
599 else
600 condition = factory->CreateQueryContainsCondition(key_);
601 string_pattern_ = condition.string_pattern();
602
603 key_length_ = key_.length();
604 value_length_ = value_.length();
605 }
606
607 URLQueryElementMatcherCondition::~URLQueryElementMatcherCondition() {}
608
609 bool URLQueryElementMatcherCondition::operator<(
610 const URLQueryElementMatcherCondition& rhs) const {
611 if (match_type_ != rhs.match_type_)
612 return match_type_ < rhs.match_type_;
613 if (string_pattern_ != NULL && rhs.string_pattern_ != NULL)
614 return *string_pattern_ < *rhs.string_pattern_;
615 if (string_pattern_ == NULL && rhs.string_pattern_ != NULL)
616 return true;
617 // Either string_pattern_ != NULL && rhs.string_pattern_ == NULL,
618 // or both are NULL.
619 return false;
620 }
621
622 bool URLQueryElementMatcherCondition::IsMatch(
623 const std::string& url_for_component_searches) const {
624 switch (match_type_) {
625 case MATCH_ANY: {
626 // For MATCH_ANY, no additional verification step is needed. We can trust
627 // the SubstringMatcher to do the verification.
628 return true;
629 }
630 case MATCH_ALL: {
631 size_t start = 0;
632 int found = 0;
633 size_t offset;
634 while ((offset = url_for_component_searches.find(key_, start)) !=
635 std::string::npos) {
636 if (url_for_component_searches.compare(
637 offset + key_length_, value_length_, value_) != 0) {
638 return false;
639 } else {
640 ++found;
641 }
642 start = offset + key_length_ + value_length_ - 1;
643 }
644 return !!found;
645 }
646 case MATCH_FIRST: {
647 size_t offset = url_for_component_searches.find(key_);
648 return url_for_component_searches.compare(
649 offset + key_length_, value_length_, value_) == 0;
650 }
651 case MATCH_LAST: {
652 size_t offset = url_for_component_searches.rfind(key_);
653 return url_for_component_searches.compare(
654 offset + key_length_, value_length_, value_) == 0;
655 }
656 }
657 NOTREACHED();
658 return false;
659 }
660
661 //
536 // URLMatcherSchemeFilter 662 // URLMatcherSchemeFilter
537 // 663 //
538 664
539 URLMatcherSchemeFilter::URLMatcherSchemeFilter(const std::string& filter) 665 URLMatcherSchemeFilter::URLMatcherSchemeFilter(const std::string& filter)
540 : filters_(1) { 666 : filters_(1) {
541 filters_.push_back(filter); 667 filters_.push_back(filter);
542 } 668 }
543 669
544 URLMatcherSchemeFilter::URLMatcherSchemeFilter( 670 URLMatcherSchemeFilter::URLMatcherSchemeFilter(
545 const std::vector<std::string>& filters) 671 const std::vector<std::string>& filters)
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
598 URLMatcherConditionSet::URLMatcherConditionSet( 724 URLMatcherConditionSet::URLMatcherConditionSet(
599 ID id, 725 ID id,
600 const Conditions& conditions, 726 const Conditions& conditions,
601 scoped_ptr<URLMatcherSchemeFilter> scheme_filter, 727 scoped_ptr<URLMatcherSchemeFilter> scheme_filter,
602 scoped_ptr<URLMatcherPortFilter> port_filter) 728 scoped_ptr<URLMatcherPortFilter> port_filter)
603 : id_(id), 729 : id_(id),
604 conditions_(conditions), 730 conditions_(conditions),
605 scheme_filter_(scheme_filter.Pass()), 731 scheme_filter_(scheme_filter.Pass()),
606 port_filter_(port_filter.Pass()) {} 732 port_filter_(port_filter.Pass()) {}
607 733
734 URLMatcherConditionSet::URLMatcherConditionSet(
735 ID id,
736 const Conditions& conditions,
737 const QueryConditions& query_conditions,
738 scoped_ptr<URLMatcherSchemeFilter> scheme_filter,
739 scoped_ptr<URLMatcherPortFilter> port_filter)
740 : id_(id),
741 conditions_(conditions),
742 query_conditions_(query_conditions),
743 scheme_filter_(scheme_filter.Pass()),
744 port_filter_(port_filter.Pass()) {}
745
608 bool URLMatcherConditionSet::IsMatch( 746 bool URLMatcherConditionSet::IsMatch(
609 const std::set<StringPattern::ID>& matching_patterns, 747 const std::set<StringPattern::ID>& matching_patterns,
610 const GURL& url) const { 748 const GURL& url) const {
749 return IsMatch(matching_patterns, url, std::string());
750 }
751
752 bool URLMatcherConditionSet::IsMatch(
753 const std::set<StringPattern::ID>& matching_patterns,
754 const GURL& url,
755 const std::string& url_for_component_searches) const {
611 for (Conditions::const_iterator i = conditions_.begin(); 756 for (Conditions::const_iterator i = conditions_.begin();
612 i != conditions_.end(); ++i) { 757 i != conditions_.end(); ++i) {
613 if (!i->IsMatch(matching_patterns, url)) 758 if (!i->IsMatch(matching_patterns, url))
614 return false; 759 return false;
615 } 760 }
616 if (scheme_filter_.get() && !scheme_filter_->IsMatch(url)) 761 if (scheme_filter_.get() && !scheme_filter_->IsMatch(url))
617 return false; 762 return false;
618 if (port_filter_.get() && !port_filter_->IsMatch(url)) 763 if (port_filter_.get() && !port_filter_->IsMatch(url))
619 return false; 764 return false;
765 if (query_conditions_.empty())
766 return true;
767 // The loop is duplicated below for performance reasons. If not all query
768 // elements are found, no need to verify match that is expected to take more
769 // cycles.
770 for (QueryConditions::const_iterator i = query_conditions_.begin();
771 i != query_conditions_.end();
772 ++i) {
773 if (!ContainsKey(matching_patterns, i->string_pattern()->id()))
774 return false;
775 }
776 for (QueryConditions::const_iterator i = query_conditions_.begin();
777 i != query_conditions_.end();
778 ++i) {
779 if (!i->IsMatch(url_for_component_searches))
780 return false;
781 }
620 return true; 782 return true;
621 } 783 }
622 784
623 // 785 //
624 // URLMatcher 786 // URLMatcher
625 // 787 //
626 788
627 URLMatcher::URLMatcher() {} 789 URLMatcher::URLMatcher() {}
628 790
629 URLMatcher::~URLMatcher() {} 791 URLMatcher::~URLMatcher() {}
(...skipping 23 matching lines...) Expand all
653 void URLMatcher::ClearUnusedConditionSets() { 815 void URLMatcher::ClearUnusedConditionSets() {
654 UpdateConditionFactory(); 816 UpdateConditionFactory();
655 } 817 }
656 818
657 std::set<URLMatcherConditionSet::ID> URLMatcher::MatchURL( 819 std::set<URLMatcherConditionSet::ID> URLMatcher::MatchURL(
658 const GURL& url) const { 820 const GURL& url) const {
659 // Find all IDs of StringPatterns that match |url|. 821 // Find all IDs of StringPatterns that match |url|.
660 // See URLMatcherConditionFactory for the canonicalization of URLs and the 822 // See URLMatcherConditionFactory for the canonicalization of URLs and the
661 // distinction between full url searches and url component searches. 823 // distinction between full url searches and url component searches.
662 std::set<StringPattern::ID> matches; 824 std::set<StringPattern::ID> matches;
825 std::string url_for_component_searches;
826
663 if (!full_url_matcher_.IsEmpty()) { 827 if (!full_url_matcher_.IsEmpty()) {
664 full_url_matcher_.Match( 828 full_url_matcher_.Match(
665 condition_factory_.CanonicalizeURLForFullSearches(url), &matches); 829 condition_factory_.CanonicalizeURLForFullSearches(url), &matches);
666 } 830 }
667 if (!url_component_matcher_.IsEmpty()) { 831 if (!url_component_matcher_.IsEmpty()) {
668 url_component_matcher_.Match( 832 url_for_component_searches =
669 condition_factory_.CanonicalizeURLForComponentSearches(url), &matches); 833 condition_factory_.CanonicalizeURLForComponentSearches(url);
834 url_component_matcher_.Match(url_for_component_searches, &matches);
670 } 835 }
671 if (!regex_set_matcher_.IsEmpty()) { 836 if (!regex_set_matcher_.IsEmpty()) {
672 regex_set_matcher_.Match( 837 regex_set_matcher_.Match(
673 condition_factory_.CanonicalizeURLForRegexSearches(url), &matches); 838 condition_factory_.CanonicalizeURLForRegexSearches(url), &matches);
674 } 839 }
675 if (!origin_and_path_regex_set_matcher_.IsEmpty()) { 840 if (!origin_and_path_regex_set_matcher_.IsEmpty()) {
676 origin_and_path_regex_set_matcher_.Match( 841 origin_and_path_regex_set_matcher_.Match(
677 condition_factory_.CanonicalizeURLForOriginAndPathRegexSearches(url), 842 condition_factory_.CanonicalizeURLForOriginAndPathRegexSearches(url),
678 &matches); 843 &matches);
679 } 844 }
(...skipping 11 matching lines...) Expand all
691 substring_match_triggers_.find(*i); 856 substring_match_triggers_.find(*i);
692 if (triggered_condition_sets_iter == substring_match_triggers_.end()) 857 if (triggered_condition_sets_iter == substring_match_triggers_.end())
693 continue; // Not all substring matches are triggers for a condition set. 858 continue; // Not all substring matches are triggers for a condition set.
694 const std::set<URLMatcherConditionSet::ID>& condition_sets = 859 const std::set<URLMatcherConditionSet::ID>& condition_sets =
695 triggered_condition_sets_iter->second; 860 triggered_condition_sets_iter->second;
696 for (std::set<URLMatcherConditionSet::ID>::const_iterator j = 861 for (std::set<URLMatcherConditionSet::ID>::const_iterator j =
697 condition_sets.begin(); j != condition_sets.end(); ++j) { 862 condition_sets.begin(); j != condition_sets.end(); ++j) {
698 URLMatcherConditionSets::const_iterator condition_set_iter = 863 URLMatcherConditionSets::const_iterator condition_set_iter =
699 url_matcher_condition_sets_.find(*j); 864 url_matcher_condition_sets_.find(*j);
700 DCHECK(condition_set_iter != url_matcher_condition_sets_.end()); 865 DCHECK(condition_set_iter != url_matcher_condition_sets_.end());
701 if (condition_set_iter->second->IsMatch(matches, url)) 866 if (condition_set_iter->second->IsMatch(
867 matches, url, url_for_component_searches))
702 result.insert(*j); 868 result.insert(*j);
703 } 869 }
704 } 870 }
705 871
706 return result; 872 return result;
707 } 873 }
708 874
709 bool URLMatcher::IsEmpty() const { 875 bool URLMatcher::IsEmpty() const {
710 return condition_factory_.IsEmpty() && 876 return condition_factory_.IsEmpty() &&
711 url_matcher_condition_sets_.empty() && 877 url_matcher_condition_sets_.empty() &&
(...skipping 23 matching lines...) Expand all
735 for (URLMatcherConditionSet::Conditions::const_iterator condition_iter = 901 for (URLMatcherConditionSet::Conditions::const_iterator condition_iter =
736 conditions.begin(); condition_iter != conditions.end(); 902 conditions.begin(); condition_iter != conditions.end();
737 ++condition_iter) { 903 ++condition_iter) {
738 // If we are called to process Full URL searches, ignore others, and 904 // If we are called to process Full URL searches, ignore others, and
739 // vice versa. (Regex conditions are updated in UpdateRegexSetMatcher.) 905 // vice versa. (Regex conditions are updated in UpdateRegexSetMatcher.)
740 if (!condition_iter->IsRegexCondition() && 906 if (!condition_iter->IsRegexCondition() &&
741 !condition_iter->IsOriginAndPathRegexCondition() && 907 !condition_iter->IsOriginAndPathRegexCondition() &&
742 full_url_conditions == condition_iter->IsFullURLCondition()) 908 full_url_conditions == condition_iter->IsFullURLCondition())
743 new_patterns.insert(condition_iter->string_pattern()); 909 new_patterns.insert(condition_iter->string_pattern());
744 } 910 }
911
912 if (full_url_conditions)
913 continue;
914
915 const URLMatcherConditionSet::QueryConditions& query_conditions =
916 condition_set_iter->second->query_conditions();
917 for (URLMatcherConditionSet::QueryConditions::const_iterator
918 query_condition_iter = query_conditions.begin();
919 query_condition_iter != query_conditions.end();
920 ++query_condition_iter) {
921 new_patterns.insert(query_condition_iter->string_pattern());
922 }
745 } 923 }
746 924
747 // This is the set of patterns that were registered before this function 925 // This is the set of patterns that were registered before this function
748 // is called. 926 // is called.
749 std::set<const StringPattern*>& registered_patterns = 927 std::set<const StringPattern*>& registered_patterns =
750 full_url_conditions ? registered_full_url_patterns_ 928 full_url_conditions ? registered_full_url_patterns_
751 : registered_url_component_patterns_; 929 : registered_url_component_patterns_;
752 930
753 // Add all patterns that are in new_patterns but not in registered_patterns. 931 // Add all patterns that are in new_patterns but not in registered_patterns.
754 std::vector<const StringPattern*> patterns_to_register = 932 std::vector<const StringPattern*> patterns_to_register =
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
810 condition_set_iter != url_matcher_condition_sets_.end(); 988 condition_set_iter != url_matcher_condition_sets_.end();
811 ++condition_set_iter) { 989 ++condition_set_iter) {
812 const URLMatcherConditionSet::Conditions& conditions = 990 const URLMatcherConditionSet::Conditions& conditions =
813 condition_set_iter->second->conditions(); 991 condition_set_iter->second->conditions();
814 for (URLMatcherConditionSet::Conditions::const_iterator condition_iter = 992 for (URLMatcherConditionSet::Conditions::const_iterator condition_iter =
815 conditions.begin(); condition_iter != conditions.end(); 993 conditions.begin(); condition_iter != conditions.end();
816 ++condition_iter) { 994 ++condition_iter) {
817 const StringPattern* pattern = condition_iter->string_pattern(); 995 const StringPattern* pattern = condition_iter->string_pattern();
818 substring_pattern_frequencies[pattern->id()]++; 996 substring_pattern_frequencies[pattern->id()]++;
819 } 997 }
998
999 const URLMatcherConditionSet::QueryConditions& query_conditions =
1000 condition_set_iter->second->query_conditions();
1001 for (URLMatcherConditionSet::QueryConditions::const_iterator
1002 query_condition_iter = query_conditions.begin();
1003 query_condition_iter != query_conditions.end();
1004 ++query_condition_iter) {
1005 const StringPattern* pattern = query_condition_iter->string_pattern();
1006 substring_pattern_frequencies[pattern->id()]++;
1007 }
820 } 1008 }
821 1009
822 // Update trigger conditions: Determine for each URLMatcherConditionSet which 1010 // Update trigger conditions: Determine for each URLMatcherConditionSet which
823 // URLMatcherCondition contains a StringPattern that occurs least 1011 // URLMatcherCondition contains a StringPattern that occurs least
824 // frequently in this URLMatcher. We assume that this condition is very 1012 // frequently in this URLMatcher. We assume that this condition is very
825 // specific and occurs rarely in URLs. If a match occurs for this 1013 // specific and occurs rarely in URLs. If a match occurs for this
826 // URLMatcherCondition, we want to test all other URLMatcherCondition in the 1014 // URLMatcherCondition, we want to test all other URLMatcherCondition in the
827 // respective URLMatcherConditionSet as well to see whether the entire 1015 // respective URLMatcherConditionSet as well to see whether the entire
828 // URLMatcherConditionSet is considered matching. 1016 // URLMatcherConditionSet is considered matching.
829 substring_match_triggers_.clear(); 1017 substring_match_triggers_.clear();
(...skipping 11 matching lines...) Expand all
841 // We skip the first element in the following loop. 1029 // We skip the first element in the following loop.
842 ++condition_iter; 1030 ++condition_iter;
843 for (; condition_iter != conditions.end(); ++condition_iter) { 1031 for (; condition_iter != conditions.end(); ++condition_iter) {
844 StringPattern::ID current_id = 1032 StringPattern::ID current_id =
845 condition_iter->string_pattern()->id(); 1033 condition_iter->string_pattern()->id();
846 if (substring_pattern_frequencies[trigger] > 1034 if (substring_pattern_frequencies[trigger] >
847 substring_pattern_frequencies[current_id]) { 1035 substring_pattern_frequencies[current_id]) {
848 trigger = current_id; 1036 trigger = current_id;
849 } 1037 }
850 } 1038 }
1039
1040 const URLMatcherConditionSet::QueryConditions& query_conditions =
1041 condition_set_iter->second->query_conditions();
1042 for (URLMatcherConditionSet::QueryConditions::const_iterator
1043 query_condition_iter = query_conditions.begin();
1044 query_condition_iter != query_conditions.end();
1045 ++query_condition_iter) {
1046 StringPattern::ID current_id =
1047 query_condition_iter->string_pattern()->id();
1048 if (substring_pattern_frequencies[trigger] >
1049 substring_pattern_frequencies[current_id]) {
1050 trigger = current_id;
1051 }
1052 }
1053
851 substring_match_triggers_[trigger].insert(condition_set_iter->second->id()); 1054 substring_match_triggers_[trigger].insert(condition_set_iter->second->id());
852 } 1055 }
853 } 1056 }
854 1057
855 void URLMatcher::UpdateConditionFactory() { 1058 void URLMatcher::UpdateConditionFactory() {
856 std::set<StringPattern::ID> used_patterns; 1059 std::set<StringPattern::ID> used_patterns;
857 for (URLMatcherConditionSets::const_iterator condition_set_iter = 1060 for (URLMatcherConditionSets::const_iterator condition_set_iter =
858 url_matcher_condition_sets_.begin(); 1061 url_matcher_condition_sets_.begin();
859 condition_set_iter != url_matcher_condition_sets_.end(); 1062 condition_set_iter != url_matcher_condition_sets_.end();
860 ++condition_set_iter) { 1063 ++condition_set_iter) {
861 const URLMatcherConditionSet::Conditions& conditions = 1064 const URLMatcherConditionSet::Conditions& conditions =
862 condition_set_iter->second->conditions(); 1065 condition_set_iter->second->conditions();
863 for (URLMatcherConditionSet::Conditions::const_iterator condition_iter = 1066 for (URLMatcherConditionSet::Conditions::const_iterator condition_iter =
864 conditions.begin(); condition_iter != conditions.end(); 1067 conditions.begin(); condition_iter != conditions.end();
865 ++condition_iter) { 1068 ++condition_iter) {
866 used_patterns.insert(condition_iter->string_pattern()->id()); 1069 used_patterns.insert(condition_iter->string_pattern()->id());
867 } 1070 }
1071 const URLMatcherConditionSet::QueryConditions& query_conditions =
1072 condition_set_iter->second->query_conditions();
1073 for (URLMatcherConditionSet::QueryConditions::const_iterator
1074 query_condition_iter = query_conditions.begin();
1075 query_condition_iter != query_conditions.end();
1076 ++query_condition_iter) {
1077 used_patterns.insert(query_condition_iter->string_pattern()->id());
1078 }
868 } 1079 }
869 condition_factory_.ForgetUnusedPatterns(used_patterns); 1080 condition_factory_.ForgetUnusedPatterns(used_patterns);
870 } 1081 }
871 1082
872 void URLMatcher::UpdateInternalDatastructures() { 1083 void URLMatcher::UpdateInternalDatastructures() {
873 UpdateSubstringSetMatcher(false); 1084 UpdateSubstringSetMatcher(false);
874 UpdateSubstringSetMatcher(true); 1085 UpdateSubstringSetMatcher(true);
875 UpdateRegexSetMatcher(); 1086 UpdateRegexSetMatcher();
876 UpdateTriggers(); 1087 UpdateTriggers();
877 UpdateConditionFactory(); 1088 UpdateConditionFactory();
878 } 1089 }
879 1090
880 } // namespace url_matcher 1091 } // 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