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 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 const char kQuerySeparator = '&'; | |
| 254 } // namespace | 256 } // namespace |
| 255 | 257 |
| 256 URLMatcherConditionFactory::URLMatcherConditionFactory() : id_counter_(0) {} | 258 URLMatcherConditionFactory::URLMatcherConditionFactory() : id_counter_(0) {} |
| 257 | 259 |
| 258 URLMatcherConditionFactory::~URLMatcherConditionFactory() { | 260 URLMatcherConditionFactory::~URLMatcherConditionFactory() { |
| 259 STLDeleteElements(&substring_pattern_singletons_); | 261 STLDeleteElements(&substring_pattern_singletons_); |
| 260 STLDeleteElements(®ex_pattern_singletons_); | 262 STLDeleteElements(®ex_pattern_singletons_); |
| 261 STLDeleteElements(&origin_and_path_regex_pattern_singletons_); | 263 STLDeleteElements(&origin_and_path_regex_pattern_singletons_); |
| 262 } | 264 } |
| 263 | 265 |
| 264 std::string URLMatcherConditionFactory::CanonicalizeURLForComponentSearches( | 266 std::string URLMatcherConditionFactory::CanonicalizeURLForComponentSearches( |
| 265 const GURL& url) const { | 267 const GURL& url) const { |
| 266 return kBeginningOfURL + CanonicalizeHostname(url.host()) + kEndOfDomain + | 268 return kBeginningOfURL + CanonicalizeHostname(url.host()) + kEndOfDomain + |
| 267 url.path() + kEndOfPath + | 269 url.path() + kEndOfPath + |
| 268 (url.has_query() ? "?" + url.query() : std::string()) + kEndOfURL; | 270 (url.has_query() ? CanonicalizeQuery(url.query(), true, true) |
| 271 : std::string()) + | |
| 272 kEndOfURL; | |
| 269 } | 273 } |
| 270 | 274 |
| 271 URLMatcherCondition URLMatcherConditionFactory::CreateHostPrefixCondition( | 275 URLMatcherCondition URLMatcherConditionFactory::CreateHostPrefixCondition( |
| 272 const std::string& prefix) { | 276 const std::string& prefix) { |
| 273 return CreateCondition(URLMatcherCondition::HOST_PREFIX, | 277 return CreateCondition(URLMatcherCondition::HOST_PREFIX, |
| 274 kBeginningOfURL + CanonicalizeHostname(prefix)); | 278 kBeginningOfURL + CanonicalizeHostname(prefix)); |
| 275 } | 279 } |
| 276 | 280 |
| 277 URLMatcherCondition URLMatcherConditionFactory::CreateHostSuffixCondition( | 281 URLMatcherCondition URLMatcherConditionFactory::CreateHostSuffixCondition( |
| 278 const std::string& suffix) { | 282 const std::string& suffix) { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 310 URLMatcherCondition URLMatcherConditionFactory::CreatePathEqualsCondition( | 314 URLMatcherCondition URLMatcherConditionFactory::CreatePathEqualsCondition( |
| 311 const std::string& str) { | 315 const std::string& str) { |
| 312 return CreateCondition(URLMatcherCondition::PATH_EQUALS, | 316 return CreateCondition(URLMatcherCondition::PATH_EQUALS, |
| 313 kEndOfDomain + str + kEndOfPath); | 317 kEndOfDomain + str + kEndOfPath); |
| 314 } | 318 } |
| 315 | 319 |
| 316 URLMatcherCondition URLMatcherConditionFactory::CreateQueryPrefixCondition( | 320 URLMatcherCondition URLMatcherConditionFactory::CreateQueryPrefixCondition( |
| 317 const std::string& prefix) { | 321 const std::string& prefix) { |
| 318 std::string pattern; | 322 std::string pattern; |
| 319 if (!prefix.empty() && prefix[0] == '?') | 323 if (!prefix.empty() && prefix[0] == '?') |
| 320 pattern = kEndOfPath + prefix; | 324 pattern = kEndOfPath + CanonicalizeQuery(prefix.substr(1), true, false); |
| 321 else | 325 else |
| 322 pattern = kEndOfPath + ('?' + prefix); | 326 pattern = kEndOfPath + CanonicalizeQuery(prefix, true, false); |
| 323 | 327 |
| 324 return CreateCondition(URLMatcherCondition::QUERY_PREFIX, pattern); | 328 return CreateCondition(URLMatcherCondition::QUERY_PREFIX, pattern); |
| 325 } | 329 } |
| 326 | 330 |
| 327 URLMatcherCondition URLMatcherConditionFactory::CreateQuerySuffixCondition( | 331 URLMatcherCondition URLMatcherConditionFactory::CreateQuerySuffixCondition( |
| 328 const std::string& suffix) { | 332 const std::string& suffix) { |
| 329 if (!suffix.empty() && suffix[0] == '?') { | 333 if (!suffix.empty() && suffix[0] == '?') { |
| 330 return CreateQueryEqualsCondition(suffix); | 334 return CreateQueryEqualsCondition(suffix); |
| 331 } else { | 335 } else { |
| 332 return CreateCondition(URLMatcherCondition::QUERY_SUFFIX, | 336 return CreateCondition(URLMatcherCondition::QUERY_SUFFIX, |
| 333 suffix + kEndOfURL); | 337 CanonicalizeQuery(suffix, false, true) + kEndOfURL); |
| 334 } | 338 } |
| 335 } | 339 } |
| 336 | 340 |
| 337 URLMatcherCondition URLMatcherConditionFactory::CreateQueryContainsCondition( | 341 URLMatcherCondition URLMatcherConditionFactory::CreateQueryContainsCondition( |
| 338 const std::string& str) { | 342 const std::string& str) { |
| 339 if (!str.empty() && str[0] == '?') | 343 if (!str.empty() && str[0] == '?') |
| 340 return CreateQueryPrefixCondition(str); | 344 return CreateQueryPrefixCondition(str); |
| 341 else | 345 else |
| 342 return CreateCondition(URLMatcherCondition::QUERY_CONTAINS, str); | 346 return CreateCondition(URLMatcherCondition::QUERY_CONTAINS, str); |
| 343 } | 347 } |
| 344 | 348 |
| 345 URLMatcherCondition URLMatcherConditionFactory::CreateQueryEqualsCondition( | 349 URLMatcherCondition URLMatcherConditionFactory::CreateQueryEqualsCondition( |
| 346 const std::string& str) { | 350 const std::string& str) { |
| 347 std::string pattern; | 351 std::string pattern; |
| 348 if (!str.empty() && str[0] == '?') | 352 if (!str.empty() && str[0] == '?') |
| 349 pattern = kEndOfPath + str + kEndOfURL; | 353 pattern = |
| 354 kEndOfPath + CanonicalizeQuery(str.substr(1), true, true) + kEndOfURL; | |
| 350 else | 355 else |
| 351 pattern = kEndOfPath + ('?' + str) + kEndOfURL; | 356 pattern = kEndOfPath + CanonicalizeQuery(str, true, true) + kEndOfURL; |
| 352 | 357 |
| 353 return CreateCondition(URLMatcherCondition::QUERY_EQUALS, pattern); | 358 return CreateCondition(URLMatcherCondition::QUERY_EQUALS, pattern); |
| 354 } | 359 } |
| 355 | 360 |
| 356 URLMatcherCondition | 361 URLMatcherCondition |
| 357 URLMatcherConditionFactory::CreateHostSuffixPathPrefixCondition( | 362 URLMatcherConditionFactory::CreateHostSuffixPathPrefixCondition( |
| 358 const std::string& host_suffix, | 363 const std::string& host_suffix, |
| 359 const std::string& path_prefix) { | 364 const std::string& path_prefix) { |
| 360 return CreateCondition(URLMatcherCondition::HOST_SUFFIX_PATH_PREFIX, | 365 return CreateCondition(URLMatcherCondition::HOST_SUFFIX_PATH_PREFIX, |
| 361 host_suffix + kEndOfDomain + path_prefix); | 366 host_suffix + kEndOfDomain + path_prefix); |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 515 } | 520 } |
| 516 | 521 |
| 517 std::string URLMatcherConditionFactory::CanonicalizeHostname( | 522 std::string URLMatcherConditionFactory::CanonicalizeHostname( |
| 518 const std::string& hostname) const { | 523 const std::string& hostname) const { |
| 519 if (!hostname.empty() && hostname[0] == '.') | 524 if (!hostname.empty() && hostname[0] == '.') |
| 520 return hostname; | 525 return hostname; |
| 521 else | 526 else |
| 522 return "." + hostname; | 527 return "." + hostname; |
| 523 } | 528 } |
| 524 | 529 |
| 530 // This function prepares the query string by replacing query separator with a | |
| 531 // magic value (|kQueryComponentDelimiter|). When the boolean | |
| 532 // |prepend_beginning_of_query_component| is true the function prepends the | |
| 533 // query with the same magic. This is done to locate the start of a key value | |
| 534 // pair in the query string. The parameter |query| is passed by value | |
| 535 // intentionally, since it is locally modified. | |
| 536 std::string URLMatcherConditionFactory::CanonicalizeQuery( | |
| 537 std::string query, | |
| 538 bool prepend_beginning_of_query_component, | |
| 539 bool append_end_of_query_component) const { | |
| 540 for (std::string::iterator it = query.begin(); it != query.end(); ++it) { | |
| 541 if (*it == kQuerySeparator) | |
| 542 *it = kQueryComponentDelimiter[0]; | |
| 543 } | |
| 544 if (prepend_beginning_of_query_component) | |
| 545 query = kQueryComponentDelimiter + query; | |
| 546 if (append_end_of_query_component) | |
| 547 query += kQueryComponentDelimiter; | |
| 548 return query; | |
| 549 } | |
| 550 | |
| 525 bool URLMatcherConditionFactory::StringPatternPointerCompare::operator()( | 551 bool URLMatcherConditionFactory::StringPatternPointerCompare::operator()( |
| 526 StringPattern* lhs, | 552 StringPattern* lhs, |
| 527 StringPattern* rhs) const { | 553 StringPattern* rhs) const { |
| 528 if (lhs == NULL && rhs != NULL) return true; | 554 if (lhs == NULL && rhs != NULL) return true; |
| 529 if (lhs != NULL && rhs != NULL) | 555 if (lhs != NULL && rhs != NULL) |
| 530 return lhs->pattern() < rhs->pattern(); | 556 return lhs->pattern() < rhs->pattern(); |
| 531 // Either both are NULL or only rhs is NULL. | 557 // Either both are NULL or only rhs is NULL. |
| 532 return false; | 558 return false; |
| 533 } | 559 } |
| 534 | 560 |
| 535 // | 561 // |
| 562 // URLQueryElementMatcherCondition | |
| 563 // | |
| 564 | |
| 565 URLQueryElementMatcherCondition::URLQueryElementMatcherCondition( | |
| 566 const std::string& key, | |
| 567 const std::string& value, | |
| 568 QueryValueMatchType query_value_match_type, | |
| 569 QueryElementType query_element_type, | |
| 570 Type match_type, | |
| 571 URLMatcherConditionFactory* factory) { | |
| 572 match_type_ = match_type; | |
| 573 | |
| 574 if (query_element_type == ELEMENT_TYPE_KEY_VALUE) { | |
| 575 key_ = kQueryComponentDelimiter + key + std::string("="); | |
| 576 value_ = value; | |
| 577 } else { | |
| 578 key_ = kQueryComponentDelimiter + key; | |
| 579 value_ = std::string(); | |
| 580 } | |
| 581 | |
| 582 if (query_value_match_type == QUERY_VALUE_MATCH_EXACT) | |
| 583 value_ += kQueryComponentDelimiter; | |
| 584 | |
| 585 // If |value_| is empty no need to find the |key_| and verify if the value | |
| 586 // matches. Simply checking the presence of key is sufficient, which is done | |
| 587 // by MATCH_ANY | |
| 588 if (value_.empty()) | |
| 589 match_type_ = MATCH_ANY; | |
| 590 | |
| 591 URLMatcherCondition condition; | |
| 592 // If |match_type_| is MATCH_ANY, then we could simply look for the | |
| 593 // combination of |key_| + |value_|, which can be efficiently done by | |
| 594 // SubstringMatcher | |
| 595 if (match_type_ == MATCH_ANY) | |
| 596 condition = factory->CreateQueryContainsCondition(key_ + value_); | |
| 597 else | |
| 598 condition = factory->CreateQueryContainsCondition(key_); | |
| 599 string_pattern_ = condition.string_pattern(); | |
| 600 | |
| 601 key_length_ = key_.length(); | |
| 602 value_length_ = value_.length(); | |
| 603 } | |
| 604 | |
| 605 URLQueryElementMatcherCondition::~URLQueryElementMatcherCondition() {} | |
| 606 | |
| 607 bool URLQueryElementMatcherCondition::operator<( | |
| 608 const URLQueryElementMatcherCondition& rhs) const { | |
| 609 if (match_type_ < rhs.match_type_) | |
| 610 return true; | |
| 611 if (match_type_ > rhs.match_type_) | |
| 612 return false; | |
|
battre
2014/04/08 10:51:21
The previous lines can be simplified to
if (matche
kaliamoorthi
2014/04/08 12:14:24
Done.
| |
| 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 Loading... | |
| 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 all query elements | |
| 768 // are not found, no need to verify match that is expected to take more | |
| 769 // cycles. | |
|
battre
2014/04/08 10:51:21
I am not sure that this performance speedup justif
battre
2014/04/08 11:20:20
You can ignore this as discussed. It would make th
| |
| 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 Loading... | |
| 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 Loading... | |
| 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 Loading... | |
| 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 Loading... | |
| 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 Loading... | |
| 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 |
| OLD | NEW |