Chromium Code Reviews| Index: chrome/browser/extensions/api/declarative/url_matcher.h |
| diff --git a/chrome/browser/extensions/api/declarative/url_matcher.h b/chrome/browser/extensions/api/declarative/url_matcher.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..74078f395dc3e67525a415e86ea48429b3f640ff |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/declarative/url_matcher.h |
| @@ -0,0 +1,168 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_URL_MATCHER_H_ |
| +#define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_URL_MATCHER_H_ |
| +#pragma once |
| + |
| +#include <set> |
| +#include <vector> |
| + |
| +#include "base/gtest_prod_util.h" |
| +#include "base/memory/linked_ptr.h" |
| +#include "chrome/browser/extensions/api/declarative/substring_set_matcher.h" |
| +#include "chrome/browser/extensions/api/declarative/url_component_patterns.h" |
| +#include "googleurl/src/gurl.h" |
| +#include "url_component_patterns.h" |
| + |
| +namespace base { |
| +class DictionaryValue; |
| +} |
| + |
| +namespace extensions { |
| + |
| +// This class represents a single URL matching condition, e.g. a match on the |
| +// host suffix or the containment of a string in the query component of a GURL. |
| +// |
| +// This is basically the next layer above UrlComponentPatterns. The difference |
| +// between a SubstringPattern created by UrlComponentPatterns is that also |
|
Matt Perry
2012/02/14 01:38:34
s/between/from
"is that this* also"
battre
2012/02/14 19:32:21
Done.
|
| +// supports matches to check whether {Host, Path, Query} of a URL contains |
| +// a string. |
| +class UrlMatcherCondition { |
|
Matt Perry
2012/02/14 01:38:34
URLMatcherCondition
battre
2012/02/14 19:32:21
Done.
|
| + public: |
| + enum Criterion { |
| + HOST_PREFIX, |
| + HOST_SUFFIX, |
| + HOST_CONTAINS, |
| + HOST_EQUALS, |
| + PATH_PREFIX, |
| + PATH_SUFFIX, |
| + PATH_CONTAINS, |
| + PATH_EQUALS, |
| + QUERY_PREFIX, |
| + QUERY_SUFFIX, |
| + QUERY_CONTAINS, |
| + QUERY_EQUALS, |
| + URL_PREFIX, |
| + URL_SUFFIX, |
| + URL_CONTAINS, |
| + URL_EQUALS, |
| + }; |
| + |
| + UrlMatcherCondition(); |
| + UrlMatcherCondition(const UrlMatcherCondition& rhs); |
| + UrlMatcherCondition& operator=(const UrlMatcherCondition& rhs); |
| + UrlMatcherCondition(Criterion criterion, std::string value); |
|
Matt Perry
2012/02/14 01:38:34
pass const-ref
battre
2012/02/14 19:32:21
Done.
|
| + |
| + Criterion criterion() const { return criterion_; } |
| + const std::string& value() const { return value_; } |
| + const SubstringPattern& substring_pattern() const { |
| + return substring_pattern_; |
| + } |
| + |
| + // Returns whether this UrlMatcherCondition needs to be executed on a |
| + // full URL rather than the individual components (see |
| + // UrlMatcherConditionSets). |
| + bool IsFullUrlCondition() const; |
| + |
| + // Returns whether this condition is fulfilled according to |
| + // |matching_substring_patterns| and |url|. |
| + bool IsMatch( |
| + const std::set<SubstringPattern::ID>& matching_substring_patterns, |
| + const GURL& url) const; |
| + |
| + // Initializes the SubstringPattern of this class, this is taken care of |
| + // by the UrlMatcher. |
| + void BuildSubstringPattern(UrlComponentPatterns* url_component_patterns); |
| + |
| + private: |
| + // |criterion_| and |value_| describe together what property a URL needs |
| + // to fulfill to be considered a match. |
| + Criterion criterion_; |
| + std::string value_; |
| + |
| + // This is the UrlMatcherCondition that is used in a SubstringSetMatcher. |
| + // It becomes valid after BuildSubstringPattern has been called. |
| + SubstringPattern substring_pattern_; |
| +}; |
| + |
| +// This class represents a set of conditions that all need to match on a |
| +// given URL in order to be considered a match. |
| +class UrlMatcherConditionSet { |
| + public: |
| + typedef int ID; |
| + typedef std::vector<UrlMatcherCondition> Conditions; |
| + |
| + UrlMatcherConditionSet(); |
| + UrlMatcherConditionSet(const UrlMatcherConditionSet& rhs); |
| + UrlMatcherConditionSet& operator=(const UrlMatcherConditionSet& rhs); |
| + UrlMatcherConditionSet(ID id, |
| + const std::vector<UrlMatcherCondition>& conditions); |
| + |
| + ID id() const { return id_; } |
| + const Conditions& conditions() const { return conditions_; } |
| + |
| + bool IsMatch( |
| + const std::set<SubstringPattern::ID>& matching_substring_patterns, |
| + const GURL& url) const; |
| + |
| + private: |
| + friend class UrlMatcher; |
| + |
| + Conditions& mutable_conditions() { return conditions_; } |
| + |
| + ID id_; |
| + Conditions conditions_; |
| +}; |
| + |
| +// This class allows matching one URL against a large set of |
| +// UrlMatcherConditionSets at the same time. |
| +class UrlMatcher { |
| + public: |
| + UrlMatcher(); |
| + |
| + void AddConditionSets(const std::vector<UrlMatcherConditionSet>& conditions); |
| + void RemoveConditionSets( |
| + const std::vector<UrlMatcherConditionSet::ID>& condition_ids); |
| + |
| + std::set<UrlMatcherConditionSet::ID> MatchUrl(const GURL& url); |
| + |
| + private: |
| + FRIEND_TEST_ALL_PREFIXES(UrlMatcherTest, FullTest); |
| + |
| + void AddConditionSet(const UrlMatcherConditionSet& new_condition_set); |
| + void RemoveConditionSet(UrlMatcherConditionSet::ID id); |
| + void UpdateSubstringSetMatcher( |
| + bool full_url_conditions, |
| + std::vector<SubstringPattern>* unregistered_patterns); |
| + void UpdateFrequenciesAndTriggers(); |
| + void RecreateTriggers(); |
| + |
| + // Maps a condition ID (as passed to AddConditions()) to the respective |
| + // UrlMatcherConditionSet. |
| + typedef std::map<UrlMatcherConditionSet::ID, UrlMatcherConditionSet> |
| + UrlMatcherConditionSets; |
| + UrlMatcherConditionSets url_matcher_condition_sets_; |
| + |
| + // Maps a SubstringPattern ID to the UrlMatcherConditions that need to |
| + // be triggered in case of a SubstringPatter match. |
| + std::map<SubstringPattern::ID, std::set<UrlMatcherConditionSet::ID> > |
| + substring_match_triggers_; |
| + |
| + std::map<SubstringPattern::ID, size_t> substring_pattern_frequencies_; |
| + |
| + SubstringSetMatcher full_url_matcher_; |
| + SubstringSetMatcher url_component_matcher_; |
| + std::set<SubstringPattern> registered_full_url_patterns_; |
| + std::set<SubstringPattern> registered_url_component_patterns_; |
| + |
| + UrlComponentPatterns url_component_patterns_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(UrlMatcher); |
| +}; |
| + |
| + |
| +} // namespace extensions |
| + |
| +#endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_URL_MATCHER_H_ |