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

Side by Side Diff: chrome/browser/extensions/api/declarative_webrequest/webrequest_condition_attribute.h

Issue 10874029: Adding condition attributes for response headers to Declarative WebRequest (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 3 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
« no previous file with comments | « no previous file | chrome/browser/extensions/api/declarative_webrequest/webrequest_condition_attribute.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 StringMatchTest {
166 public:
167 StringMatchTest(const std::string& data, MatchType type);
168 ~StringMatchTest();
169
170 // Does |str| pass |*this| StringMatchTest?
171 bool Matches(const std::string& str) const;
172
173 private:
174 const std::string data_;
175 const MatchType type_;
176 DISALLOW_COPY_AND_ASSIGN(StringMatchTest);
177 };
178
179 class HeaderMatchTest {
180 public:
181 // Takes ownership of the content of both |name| and |value|.
182 HeaderMatchTest(ScopedVector<const StringMatchTest>* name,
183 ScopedVector<const StringMatchTest>* value);
184 ~HeaderMatchTest();
185 // Does the header |name|: |value| match all tests in this header test?
186 bool Matches(const std::string& name, const std::string& value) const;
187
188 private:
189 // Tests to be passed by a header's name.
190 const ScopedVector<const StringMatchTest> name_;
191 // Tests to be passed by a header's value.
192 const ScopedVector<const StringMatchTest> value_;
193 DISALLOW_COPY_AND_ASSIGN(HeaderMatchTest);
194 };
195
196 WebRequestConditionAttributeResponseHeaders(
197 bool positive_test, ScopedVector<const HeaderMatchTest>* tests);
198
199 // Gets the tests' description in |tests| and creates the corresponding
200 // HeaderMatchTest. Returns NULL on failure.
201 static scoped_ptr<const HeaderMatchTest> CreateTests(
202 const base::DictionaryValue* tests,
203 std::string* error);
204 // Helper to CreateTests. Never returns NULL, except for memory failures.
205 static scoped_ptr<const StringMatchTest> CreateMatchTest(const Value* content,
206 bool is_name_test,
207 MatchType match_type);
208
209 // The condition is satisfied if there is a header and its value such that for
210 // some |i| the header passes all the tests from |tests_[i]|.
211 ScopedVector<const HeaderMatchTest> tests_;
212
213 // True means that IsFulfilled() reports whether the condition is satisfied.
214 // False means that it reports whether the condition is NOT satisfied.
215 bool positive_test_;
216
217 DISALLOW_COPY_AND_ASSIGN(WebRequestConditionAttributeResponseHeaders);
218 };
219
136 } // namespace extensions 220 } // namespace extensions
137 221
138 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDI TION_ATTRIBUTE_H_ 222 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDI TION_ATTRIBUTE_H_
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/extensions/api/declarative_webrequest/webrequest_condition_attribute.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698