OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDITIO N_ATTRIBUTE_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDITIO N_ATTRIBUTE_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDITIO N_ATTRIBUTE_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDITIO N_ATTRIBUTE_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/memory/linked_ptr.h" | 12 #include "base/memory/linked_ptr.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/memory/scoped_vector.h" | |
14 #include "chrome/browser/extensions/api/declarative_webrequest/request_stage.h" | 15 #include "chrome/browser/extensions/api/declarative_webrequest/request_stage.h" |
15 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rule.h " | 16 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rule.h " |
16 #include "chrome/common/extensions/api/events.h" | 17 #include "chrome/common/extensions/api/events.h" |
17 #include "webkit/glue/resource_type.h" | 18 #include "webkit/glue/resource_type.h" |
18 | 19 |
19 namespace base { | 20 namespace base { |
20 class Value; | 21 class Value; |
21 } | 22 } |
22 | 23 |
23 namespace net { | 24 namespace net { |
24 class URLRequest; | 25 class URLRequest; |
25 } | 26 } |
26 | 27 |
27 namespace extensions { | 28 namespace extensions { |
28 | 29 |
29 // Base class for all condition attributes of the declarative Web Request API | 30 // Base class for all condition attributes of the declarative Web Request API |
30 // except for condition attribute to test URLPatterns. | 31 // except for condition attribute to test URLPatterns. |
31 class WebRequestConditionAttribute { | 32 class WebRequestConditionAttribute { |
32 public: | 33 public: |
33 enum Type { | 34 enum Type { |
34 CONDITION_RESOURCE_TYPE, | 35 CONDITION_RESOURCE_TYPE, |
35 CONDITION_CONTENT_TYPE | 36 CONDITION_CONTENT_TYPE, |
37 CONDITION_REQUEST_HEADERS | |
36 }; | 38 }; |
37 | 39 |
38 WebRequestConditionAttribute(); | 40 WebRequestConditionAttribute(); |
39 virtual ~WebRequestConditionAttribute(); | 41 virtual ~WebRequestConditionAttribute(); |
40 | 42 |
41 // Factory method that creates a WebRequestConditionAttribute for the JSON | 43 // Factory method that creates a WebRequestConditionAttribute for the JSON |
42 // dictionary {|name|: |value|} passed by the extension API. Sets |error| and | 44 // dictionary {|name|: |value|} passed by the extension API. Sets |error| and |
43 // returns NULL if something fails. | 45 // returns NULL if something fails. |
44 // The ownership of |value| remains at the caller. | 46 // The ownership of |value| remains at the caller. |
45 static scoped_ptr<WebRequestConditionAttribute> Create( | 47 static scoped_ptr<WebRequestConditionAttribute> Create( |
46 const std::string& name, | 48 const std::string& name, |
47 const base::Value* value, | 49 const base::Value* value, |
48 std::string* error); | 50 std::string* error); |
49 | 51 |
50 // Returns a bit vector representing extensions::RequestStage. The bit vector | 52 // Returns a bit vector representing extensions::RequestStage. The bit vector |
51 // contains a 1 for each request stage during which the condition attribute | 53 // contains a 1 for each request stage during which the condition attribute |
52 // can be tested. | 54 // can be tested. |
53 virtual int GetStages() const = 0; | 55 virtual int GetStages() const = 0; |
54 | 56 |
55 // Returns whether the condition is fulfilled for this request. | 57 // Returns whether the condition is fulfilled for this request. |
56 virtual bool IsFulfilled(const WebRequestRule::RequestData& request_data) = 0; | 58 virtual bool IsFulfilled( |
59 const WebRequestRule::RequestData& request_data) const = 0; | |
57 | 60 |
58 virtual Type GetType() const = 0; | 61 virtual Type GetType() const = 0; |
59 | 62 |
60 // Returns whether condition attributes of type |instance_type| are known | 63 // Returns whether condition attributes of type |instance_type| are known |
61 // and can be instantiated by Create(). | 64 // and can be instantiated by Create(). |
62 static bool IsKnownType(const std::string& instance_type); | 65 static bool IsKnownType(const std::string& instance_type); |
63 | 66 |
64 private: | 67 private: |
65 DISALLOW_COPY_AND_ASSIGN(WebRequestConditionAttribute); | 68 DISALLOW_COPY_AND_ASSIGN(WebRequestConditionAttribute); |
66 }; | 69 }; |
(...skipping 14 matching lines...) Expand all Loading... | |
81 static bool IsMatchingType(const std::string& instance_type); | 84 static bool IsMatchingType(const std::string& instance_type); |
82 | 85 |
83 // Factory method, see WebRequestConditionAttribute::Create. | 86 // Factory method, see WebRequestConditionAttribute::Create. |
84 static scoped_ptr<WebRequestConditionAttribute> Create( | 87 static scoped_ptr<WebRequestConditionAttribute> Create( |
85 const std::string& name, | 88 const std::string& name, |
86 const base::Value* value, | 89 const base::Value* value, |
87 std::string* error); | 90 std::string* error); |
88 | 91 |
89 // Implementation of WebRequestConditionAttribute: | 92 // Implementation of WebRequestConditionAttribute: |
90 virtual int GetStages() const OVERRIDE; | 93 virtual int GetStages() const OVERRIDE; |
91 virtual bool IsFulfilled(const WebRequestRule::RequestData& request_data) | 94 virtual bool IsFulfilled( |
92 OVERRIDE; | 95 const WebRequestRule::RequestData& request_data) const OVERRIDE; |
93 virtual Type GetType() const OVERRIDE; | 96 virtual Type GetType() const OVERRIDE; |
94 | 97 |
95 private: | 98 private: |
96 explicit WebRequestConditionAttributeResourceType( | 99 explicit WebRequestConditionAttributeResourceType( |
97 const std::vector<ResourceType::Type>& types); | 100 const std::vector<ResourceType::Type>& types); |
98 | 101 |
99 std::vector<ResourceType::Type> types_; | 102 std::vector<ResourceType::Type> types_; |
100 | 103 |
101 DISALLOW_COPY_AND_ASSIGN(WebRequestConditionAttributeResourceType); | 104 DISALLOW_COPY_AND_ASSIGN(WebRequestConditionAttributeResourceType); |
102 }; | 105 }; |
103 | 106 |
104 // Condition that checks whether a response's Content-Type header has a | 107 // Condition that checks whether a response's Content-Type header has a |
105 // certain MIME media type. | 108 // certain MIME media type. |
106 class WebRequestConditionAttributeContentType | 109 class WebRequestConditionAttributeContentType |
107 : public WebRequestConditionAttribute { | 110 : public WebRequestConditionAttribute { |
108 public: | 111 public: |
109 virtual ~WebRequestConditionAttributeContentType(); | 112 virtual ~WebRequestConditionAttributeContentType(); |
110 | 113 |
111 static bool IsMatchingType(const std::string& instance_type); | 114 static bool IsMatchingType(const std::string& instance_type); |
112 | 115 |
113 // Factory method, see WebRequestConditionAttribute::Create. | 116 // Factory method, see WebRequestConditionAttribute::Create. |
114 static scoped_ptr<WebRequestConditionAttribute> Create( | 117 static scoped_ptr<WebRequestConditionAttribute> Create( |
115 const std::string& name, | 118 const std::string& name, |
116 const base::Value* value, | 119 const base::Value* value, |
117 std::string* error); | 120 std::string* error); |
118 | 121 |
119 // Implementation of WebRequestConditionAttribute: | 122 // Implementation of WebRequestConditionAttribute: |
120 virtual int GetStages() const OVERRIDE; | 123 virtual int GetStages() const OVERRIDE; |
121 virtual bool IsFulfilled(const WebRequestRule::RequestData& request_data) | 124 virtual bool IsFulfilled( |
122 OVERRIDE; | 125 const WebRequestRule::RequestData& request_data) const OVERRIDE; |
123 virtual Type GetType() const OVERRIDE; | 126 virtual Type GetType() const OVERRIDE; |
124 | 127 |
125 private: | 128 private: |
126 explicit WebRequestConditionAttributeContentType( | 129 explicit WebRequestConditionAttributeContentType( |
127 const std::vector<std::string>& include_content_types, | 130 const std::vector<std::string>& include_content_types, |
128 bool inclusive); | 131 bool inclusive); |
129 | 132 |
130 std::vector<std::string> content_types_; | 133 std::vector<std::string> content_types_; |
131 bool inclusive_; | 134 bool inclusive_; |
132 | 135 |
133 DISALLOW_COPY_AND_ASSIGN(WebRequestConditionAttributeContentType); | 136 DISALLOW_COPY_AND_ASSIGN(WebRequestConditionAttributeContentType); |
134 }; | 137 }; |
135 | 138 |
139 // Condition that performs matches against response headers' names and values. | |
140 // In the comments below there is a distinction between when this condition is | |
141 // "satisfied" and when it is "fulfilled". See the comments at |tests_| for | |
142 // "satisfied" and the comment at |positive_test_| for "fulfilled". | |
143 class WebRequestConditionAttributeResponseHeaders | |
144 : public WebRequestConditionAttribute { | |
145 public: | |
146 virtual ~WebRequestConditionAttributeResponseHeaders(); | |
147 | |
148 static bool IsMatchingType(const std::string& instance_type); | |
149 | |
150 // Factory method, see WebRequestConditionAttribute::Create. | |
151 static scoped_ptr<WebRequestConditionAttribute> Create( | |
152 const std::string& name, | |
153 const base::Value* value, | |
154 std::string* error); | |
155 | |
156 // Implementation of WebRequestConditionAttribute: | |
157 virtual int GetStages() const OVERRIDE; | |
158 virtual bool IsFulfilled( | |
159 const WebRequestRule::RequestData& request_data) const OVERRIDE; | |
160 virtual Type GetType() const OVERRIDE; | |
161 | |
162 private: | |
163 enum MatchType { kPrefix, kSuffix, kEquals, kContains }; | |
164 | |
165 class MatchTest { | |
166 public: | |
167 // Takes ownership of |data|, which must not be NULL. | |
168 MatchTest(scoped_ptr<const std::string> data, MatchType type); | |
battre
2012/08/24 16:24:41
Please pass |data| just as a const ref. The cost t
vabr (Chromium)
2012/08/27 12:11:50
Done.
To document this decision:
pros of the scope
| |
169 ~MatchTest(); | |
170 const std::string& data() const { | |
171 return *(data_.get()); | |
172 } | |
173 // Does |str| pass |*this| MatchTest? | |
174 bool Matches(const std::string& str) const; | |
175 private: | |
176 const scoped_ptr<const std::string> data_; | |
177 const MatchType type_; | |
178 DISALLOW_COPY_AND_ASSIGN(MatchTest); | |
179 }; | |
180 | |
181 class TestGroup { | |
182 public: | |
183 TestGroup(ScopedVector<const MatchTest>* name, | |
battre
2012/08/24 16:24:41
Please add comment, that ownership is taken over.
vabr (Chromium)
2012/08/27 12:11:50
Done.
| |
184 ScopedVector<const MatchTest>* value); | |
185 ~TestGroup(); | |
186 // Does the header |name|: |value| match all tests in this test group? | |
187 bool Matches(const std::string& name, const std::string& value) const; | |
188 private: | |
189 // Tests to be passed by a header's name. | |
190 const ScopedVector<const MatchTest> name_; | |
191 // Tests to be passed by a header's value. | |
192 const ScopedVector<const MatchTest> value_; | |
193 DISALLOW_COPY_AND_ASSIGN(TestGroup); | |
194 }; | |
195 | |
196 explicit WebRequestConditionAttributeResponseHeaders(bool positive_test); | |
197 | |
198 // Gets the tests' description in |tests| and creates the corresponding | |
199 // TestGroup. Returns NULL on failure. | |
200 static scoped_ptr<const TestGroup> GetTests( | |
battre
2012/08/24 16:24:41
nit: s/Get/Create/?
vabr (Chromium)
2012/08/27 12:11:50
Done.
| |
201 const base::DictionaryValue* tests, | |
202 std::string* error); | |
203 // Helper to GetTests. Never returns NULL, except for memory failures. | |
204 static scoped_ptr<const MatchTest> GetMatchTest(const Value* content, | |
205 bool is_name_test, | |
206 MatchType match_type); | |
battre
2012/08/24 16:24:41
nit: s/Get/Create/ (also in line 203)
vabr (Chromium)
2012/08/27 12:11:50
Done.
| |
207 | |
208 // The condition is satisfied if there is a header and its value such that for | |
209 // some |i| the header passes all the tests from |tests_[i]|. | |
210 ScopedVector<const TestGroup> tests_; | |
211 | |
212 // True means that IsFulfilled() reports whether the condition is satisfied. | |
213 // False means that it reports whether the condition is NOT satisfied. | |
214 bool positive_test_; | |
215 | |
216 DISALLOW_COPY_AND_ASSIGN(WebRequestConditionAttributeResponseHeaders); | |
217 }; | |
218 | |
136 } // namespace extensions | 219 } // namespace extensions |
137 | 220 |
138 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDI TION_ATTRIBUTE_H_ | 221 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDI TION_ATTRIBUTE_H_ |
OLD | NEW |