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

Side by Side Diff: chrome/browser/extensions/api/declarative/url_matcher.h

Issue 9390018: Implementation of a Matching strategy for URLs in the Declarative WebRequest API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 10 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_URL_MATCHER_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_URL_MATCHER_H_
7 #pragma once
8
9 #include <set>
10 #include <vector>
11
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/linked_ptr.h"
14 #include "chrome/browser/extensions/api/declarative/substring_set_matcher.h"
15 #include "chrome/browser/extensions/api/declarative/url_component_patterns.h"
16 #include "googleurl/src/gurl.h"
17 #include "url_component_patterns.h"
18
19 namespace base {
20 class DictionaryValue;
21 }
22
23 namespace extensions {
24
25 // This class represents a single URL matching condition, e.g. a match on the
26 // host suffix or the containment of a string in the query component of a GURL.
27 //
28 // This is basically the next layer above UrlComponentPatterns. The difference
29 // 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.
30 // supports matches to check whether {Host, Path, Query} of a URL contains
31 // a string.
32 class UrlMatcherCondition {
Matt Perry 2012/02/14 01:38:34 URLMatcherCondition
battre 2012/02/14 19:32:21 Done.
33 public:
34 enum Criterion {
35 HOST_PREFIX,
36 HOST_SUFFIX,
37 HOST_CONTAINS,
38 HOST_EQUALS,
39 PATH_PREFIX,
40 PATH_SUFFIX,
41 PATH_CONTAINS,
42 PATH_EQUALS,
43 QUERY_PREFIX,
44 QUERY_SUFFIX,
45 QUERY_CONTAINS,
46 QUERY_EQUALS,
47 URL_PREFIX,
48 URL_SUFFIX,
49 URL_CONTAINS,
50 URL_EQUALS,
51 };
52
53 UrlMatcherCondition();
54 UrlMatcherCondition(const UrlMatcherCondition& rhs);
55 UrlMatcherCondition& operator=(const UrlMatcherCondition& rhs);
56 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.
57
58 Criterion criterion() const { return criterion_; }
59 const std::string& value() const { return value_; }
60 const SubstringPattern& substring_pattern() const {
61 return substring_pattern_;
62 }
63
64 // Returns whether this UrlMatcherCondition needs to be executed on a
65 // full URL rather than the individual components (see
66 // UrlMatcherConditionSets).
67 bool IsFullUrlCondition() const;
68
69 // Returns whether this condition is fulfilled according to
70 // |matching_substring_patterns| and |url|.
71 bool IsMatch(
72 const std::set<SubstringPattern::ID>& matching_substring_patterns,
73 const GURL& url) const;
74
75 // Initializes the SubstringPattern of this class, this is taken care of
76 // by the UrlMatcher.
77 void BuildSubstringPattern(UrlComponentPatterns* url_component_patterns);
78
79 private:
80 // |criterion_| and |value_| describe together what property a URL needs
81 // to fulfill to be considered a match.
82 Criterion criterion_;
83 std::string value_;
84
85 // This is the UrlMatcherCondition that is used in a SubstringSetMatcher.
86 // It becomes valid after BuildSubstringPattern has been called.
87 SubstringPattern substring_pattern_;
88 };
89
90 // This class represents a set of conditions that all need to match on a
91 // given URL in order to be considered a match.
92 class UrlMatcherConditionSet {
93 public:
94 typedef int ID;
95 typedef std::vector<UrlMatcherCondition> Conditions;
96
97 UrlMatcherConditionSet();
98 UrlMatcherConditionSet(const UrlMatcherConditionSet& rhs);
99 UrlMatcherConditionSet& operator=(const UrlMatcherConditionSet& rhs);
100 UrlMatcherConditionSet(ID id,
101 const std::vector<UrlMatcherCondition>& conditions);
102
103 ID id() const { return id_; }
104 const Conditions& conditions() const { return conditions_; }
105
106 bool IsMatch(
107 const std::set<SubstringPattern::ID>& matching_substring_patterns,
108 const GURL& url) const;
109
110 private:
111 friend class UrlMatcher;
112
113 Conditions& mutable_conditions() { return conditions_; }
114
115 ID id_;
116 Conditions conditions_;
117 };
118
119 // This class allows matching one URL against a large set of
120 // UrlMatcherConditionSets at the same time.
121 class UrlMatcher {
122 public:
123 UrlMatcher();
124
125 void AddConditionSets(const std::vector<UrlMatcherConditionSet>& conditions);
126 void RemoveConditionSets(
127 const std::vector<UrlMatcherConditionSet::ID>& condition_ids);
128
129 std::set<UrlMatcherConditionSet::ID> MatchUrl(const GURL& url);
130
131 private:
132 FRIEND_TEST_ALL_PREFIXES(UrlMatcherTest, FullTest);
133
134 void AddConditionSet(const UrlMatcherConditionSet& new_condition_set);
135 void RemoveConditionSet(UrlMatcherConditionSet::ID id);
136 void UpdateSubstringSetMatcher(
137 bool full_url_conditions,
138 std::vector<SubstringPattern>* unregistered_patterns);
139 void UpdateFrequenciesAndTriggers();
140 void RecreateTriggers();
141
142 // Maps a condition ID (as passed to AddConditions()) to the respective
143 // UrlMatcherConditionSet.
144 typedef std::map<UrlMatcherConditionSet::ID, UrlMatcherConditionSet>
145 UrlMatcherConditionSets;
146 UrlMatcherConditionSets url_matcher_condition_sets_;
147
148 // Maps a SubstringPattern ID to the UrlMatcherConditions that need to
149 // be triggered in case of a SubstringPatter match.
150 std::map<SubstringPattern::ID, std::set<UrlMatcherConditionSet::ID> >
151 substring_match_triggers_;
152
153 std::map<SubstringPattern::ID, size_t> substring_pattern_frequencies_;
154
155 SubstringSetMatcher full_url_matcher_;
156 SubstringSetMatcher url_component_matcher_;
157 std::set<SubstringPattern> registered_full_url_patterns_;
158 std::set<SubstringPattern> registered_url_component_patterns_;
159
160 UrlComponentPatterns url_component_patterns_;
161
162 DISALLOW_COPY_AND_ASSIGN(UrlMatcher);
163 };
164
165
166 } // namespace extensions
167
168 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_URL_MATCHER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698