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

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

Issue 10923002: Extracting header testing code from WebRequestConditionAttributeResponseHeaders. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Correction of a negated condition and more unit-tests 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 "base/memory/scoped_vector.h"
15 #include "chrome/browser/extensions/api/declarative_webrequest/request_stage.h" 15 #include "chrome/browser/extensions/api/declarative_webrequest/request_stage.h"
16 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rule.h " 16 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rule.h "
17 #include "chrome/common/extensions/api/events.h" 17 #include "chrome/common/extensions/api/events.h"
18 #include "webkit/glue/resource_type.h" 18 #include "webkit/glue/resource_type.h"
19 19
20 namespace base { 20 namespace base {
21 class Value; 21 class Value;
22 } 22 }
23 23
24 namespace net { 24 namespace net {
25 class URLRequest; 25 class URLRequest;
26 } 26 }
27 27
28 namespace extensions { 28 namespace extensions {
29 29
30 class HeaderMatcher;
31
30 // Base class for all condition attributes of the declarative Web Request API 32 // Base class for all condition attributes of the declarative Web Request API
31 // except for condition attribute to test URLPatterns. 33 // except for condition attribute to test URLPatterns.
32 class WebRequestConditionAttribute { 34 class WebRequestConditionAttribute {
33 public: 35 public:
34 enum Type { 36 enum Type {
35 CONDITION_RESOURCE_TYPE, 37 CONDITION_RESOURCE_TYPE,
36 CONDITION_CONTENT_TYPE, 38 CONDITION_CONTENT_TYPE,
37 CONDITION_RESPONSE_HEADERS 39 CONDITION_RESPONSE_HEADERS
38 }; 40 };
39 41
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 explicit WebRequestConditionAttributeContentType( 131 explicit WebRequestConditionAttributeContentType(
130 const std::vector<std::string>& include_content_types, 132 const std::vector<std::string>& include_content_types,
131 bool inclusive); 133 bool inclusive);
132 134
133 std::vector<std::string> content_types_; 135 std::vector<std::string> content_types_;
134 bool inclusive_; 136 bool inclusive_;
135 137
136 DISALLOW_COPY_AND_ASSIGN(WebRequestConditionAttributeContentType); 138 DISALLOW_COPY_AND_ASSIGN(WebRequestConditionAttributeContentType);
137 }; 139 };
138 140
139 // Condition that performs matches against response headers' names and values. 141 // Condition attribute for matching against response headers. Uses HeaderMatcher
140 // In the comments below there is a distinction between when this condition is 142 // to handle the actual tests, in connection with a boolean positiveness
141 // "satisfied" and when it is "fulfilled". See the comments at |tests_| for 143 // flag. If that flag is set to true, then IsFulfilled() returns true iff
142 // "satisfied" and the comment at |positive_test_| for "fulfilled". 144 // |header_matcher_| matches at least one header. Otherwise IsFulfilled()
145 // returns true iff the |header_matcher_| matches no header.
143 class WebRequestConditionAttributeResponseHeaders 146 class WebRequestConditionAttributeResponseHeaders
144 : public WebRequestConditionAttribute { 147 : public WebRequestConditionAttribute {
145 public: 148 public:
146 virtual ~WebRequestConditionAttributeResponseHeaders(); 149 virtual ~WebRequestConditionAttributeResponseHeaders();
147 150
148 static bool IsMatchingType(const std::string& instance_type); 151 static bool IsMatchingType(const std::string& instance_type);
149 152
150 // Factory method, see WebRequestConditionAttribute::Create. 153 // Factory method, see WebRequestConditionAttribute::Create.
151 static scoped_ptr<WebRequestConditionAttribute> Create( 154 static scoped_ptr<WebRequestConditionAttribute> Create(
152 const std::string& name, 155 const std::string& name,
153 const base::Value* value, 156 const base::Value* value,
154 std::string* error); 157 std::string* error);
155 158
156 // Implementation of WebRequestConditionAttribute: 159 // Implementation of WebRequestConditionAttribute:
157 virtual int GetStages() const OVERRIDE; 160 virtual int GetStages() const OVERRIDE;
158 virtual bool IsFulfilled( 161 virtual bool IsFulfilled(
159 const WebRequestRule::RequestData& request_data) const OVERRIDE; 162 const WebRequestRule::RequestData& request_data) const OVERRIDE;
160 virtual Type GetType() const OVERRIDE; 163 virtual Type GetType() const OVERRIDE;
161 164
162 private: 165 private:
163 enum MatchType { kPrefix, kSuffix, kEquals, kContains }; 166 WebRequestConditionAttributeResponseHeaders(
167 scoped_ptr<const HeaderMatcher>* header_matcher, bool positive);
164 168
165 class StringMatchTest { 169 const scoped_ptr<const HeaderMatcher> header_matcher_;
166 public: 170 const bool positive_;
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> CreateHeaderMatchTest(
202 const base::DictionaryValue* tests,
203 std::string* error);
204 // Helper to CreateHeaderMatchTest. Never returns NULL, except for memory
205 // failures.
206 static scoped_ptr<const StringMatchTest> CreateStringMatchTest(
207 const Value* content,
208 bool is_name_test,
209 MatchType match_type);
210
211 // The condition is satisfied if there is a header and its value such that for
212 // some |i| the header passes all the tests from |tests_[i]|.
213 ScopedVector<const HeaderMatchTest> tests_;
214
215 // True means that IsFulfilled() reports whether the condition is satisfied.
216 // False means that it reports whether the condition is NOT satisfied.
217 bool positive_test_;
218 171
219 DISALLOW_COPY_AND_ASSIGN(WebRequestConditionAttributeResponseHeaders); 172 DISALLOW_COPY_AND_ASSIGN(WebRequestConditionAttributeResponseHeaders);
220 }; 173 };
221 174
222 } // namespace extensions 175 } // namespace extensions
223 176
224 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDI TION_ATTRIBUTE_H_ 177 #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