OLD | NEW |
---|---|
(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_WEBREQUEST_WEBREQUEST_CONDITIO N_H_ | |
6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDITIO N_H_ | |
7 #pragma once | |
8 | |
9 #include <string> | |
10 #include <vector> | |
11 | |
12 #include "base/basictypes.h" | |
13 #include "base/memory/linked_ptr.h" | |
14 #include "chrome/browser/extensions/api/declarative/url_matcher.h" | |
15 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit ion_attribute.h" | |
16 | |
17 namespace extensions { | |
18 | |
19 // Representation of a condition in the Declarative WebRequest API. A condition | |
20 // consists of several attributes. Each of these attributes needs to be | |
21 // fulfilled in order for the condition to be fulfilled. | |
22 // | |
23 // We distinguish between two types of conditions: | |
24 // - URL Matcher conditions are conditions that test the URL of a request. | |
25 // These are treated separately because we use a URLMatcher to efficiently | |
26 // test many of these conditions in parallel by using some advanced | |
27 // data structures. The URLMatcher tells us if all URL Matcher conditions | |
28 // are fulfilled for a WebRequestCondition. | |
29 // - All other conditions are represented as WebRequestConditionAttributes. | |
30 // These conditions are probed linearly (only if the URL Matcher found a hit). | |
31 // | |
32 // TODO(battre) Consider making the URLMatcher an owner of the | |
33 // URLMatcherConditionSet and only pass a pointer to URLMatcherConditionSet | |
34 // in url_matcher_condition_set(). This saves some copying in | |
35 // WebRequestConditionCollection::AppendURLMatcherConditionSets. | |
36 class WebRequestCondition { | |
37 public: | |
38 WebRequestCondition( | |
39 const URLMatcherConditionSet& url_matcher_conditions, | |
40 const WebRequestConditionAttributes& condition_attributes); | |
41 ~WebRequestCondition(); | |
42 | |
43 // Returns whether |request| is a match, given that the URLMatcher found | |
44 // a match for |url_matcher_conditions_|. | |
45 bool IsFulfilled(net::URLRequest* request) const; | |
46 | |
47 // Returns a URLMatcherConditionSet::ID which is the canonical representation | |
48 // for all URL patterns that need to be matched by this WebRequestCondition. | |
49 // This ID is registered in a URLMatcher that can inform us in case of a | |
50 // match. | |
51 URLMatcherConditionSet::ID url_matcher_condition_set_id() const { | |
52 return url_matcher_conditions_.id(); | |
53 } | |
54 | |
55 // Returns the set of conditions that are checked on the URL. This is the | |
56 // primary trigger for WebRequestCondition and therefore never empty. | |
57 // (If it was empty, the URLMatcher would never notify us about network | |
58 // requests which might fulfill the entire WebRequestCondition). | |
59 const URLMatcherConditionSet& url_matcher_condition_set() const { | |
60 return url_matcher_conditions_; | |
61 } | |
62 | |
63 // Factory method that instantiates a WebRequestCondition according to | |
64 // the description |condition| passed by the extension API. | |
65 static scoped_ptr<WebRequestCondition> Create( | |
66 URLMatcherConditionFactory* url_matcher_condition_factory, | |
67 const base::Value& condition, | |
68 std::string* error); | |
69 | |
70 private: | |
71 // Returns whether a condition attribute with name |condition_attribute_name| | |
72 // needs to be handled by the URLMatcher. | |
73 static bool IsURLMatcherConditionAttribute( | |
74 const std::string& condition_attribute_name); | |
75 | |
76 // Factory method of for URLMatcherConditions. | |
77 static URLMatcherCondition CreateURLMatcherCondition( | |
78 URLMatcherConditionFactory* url_matcher_condition_factory, | |
79 const std::string& condition_attribute_name, | |
80 const base::Value* value, | |
81 std::string* error); | |
82 | |
83 URLMatcherConditionSet url_matcher_conditions_; | |
84 WebRequestConditionAttributes condition_attributes_; | |
85 | |
86 DISALLOW_COPY_AND_ASSIGN(WebRequestCondition); | |
87 }; | |
88 | |
89 // This class stores a set of conditions that may be part of a WebRequestRule. | |
90 // If any condition is fulfilled, the WebRequestActions of the WebRequestRule | |
91 // can be triggered. | |
92 class WebRequestConditionCollection { | |
93 public: | |
94 typedef std::vector<linked_ptr<json_schema_compiler::any::Any> > AnyVector; | |
95 | |
96 explicit WebRequestConditionCollection( | |
97 const std::vector<linked_ptr<WebRequestCondition> >& conditions); | |
98 ~WebRequestConditionCollection(); | |
99 | |
100 const std::vector<linked_ptr<WebRequestCondition> >& conditions() const { | |
101 return conditions_; | |
102 } | |
103 | |
104 // Returns whether any condition in the condition collection is fulfilled | |
105 // based on a match |url_match| and the value of |request|. This function | |
106 // should be called for each URLMatcherConditionSet::ID that was found | |
107 // by the URLMatcher to ensure that the each trigger in |match_triggers_| is | |
108 // found. | |
109 bool IsFulfilled(URLMatcherConditionSet::ID url_match, | |
110 net::URLRequest* request) const; | |
111 | |
112 // Appends the URLMatcherConditionSet from all conditions to |condition_sets|. | |
113 void AppendURLMatcherConditionSets( | |
114 std::vector<URLMatcherConditionSet>* condition_sets) const; | |
Matt Perry
2012/03/26 20:21:20
I see why you chose this name, but it still confus
battre
2012/03/26 20:38:10
Done.
| |
115 | |
116 // Factory method that creates an WebRequestConditionCollection according | |
117 // to the JSON array |conditions| passed by the extension API. | |
118 // Sets |error| and returns NULL in case of an error. | |
119 static scoped_ptr<WebRequestConditionCollection> Create( | |
120 URLMatcherConditionFactory* url_matcher_condition_factory, | |
121 const AnyVector& conditions, | |
122 std::string* error); | |
123 | |
124 private: | |
125 typedef std::vector<linked_ptr<WebRequestCondition> > Collection; | |
126 Collection conditions_; | |
127 | |
128 typedef std::map<URLMatcherConditionSet::ID, WebRequestCondition*> | |
129 MatchTriggers; | |
130 MatchTriggers match_triggers_; | |
131 | |
132 DISALLOW_COPY_AND_ASSIGN(WebRequestConditionCollection); | |
133 }; | |
134 | |
135 } // namespace extensions | |
136 | |
137 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDI TION_H_ | |
OLD | NEW |