Chromium Code Reviews| 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 { | |
|
battre
2012/08/27 17:16:25
opt: StringMatchTest?
vabr (Chromium)
2012/08/28 17:59:09
Done.
| |
| 166 public: | |
| 167 MatchTest(const std::string& data, MatchType type); | |
| 168 ~MatchTest(); | |
| 169 | |
| 170 // Does |str| pass |*this| MatchTest? | |
| 171 bool Matches(const std::string& str) const; | |
|
battre
2012/08/27 17:16:25
nit: newline before private: (also below)
vabr (Chromium)
2012/08/28 17:59:09
Done.
| |
| 172 private: | |
| 173 const std::string data_; | |
| 174 const MatchType type_; | |
| 175 DISALLOW_COPY_AND_ASSIGN(MatchTest); | |
| 176 }; | |
| 177 | |
| 178 class TestGroup { | |
|
battre
2012/08/27 17:16:25
opt: HeaderMatchTest?
vabr (Chromium)
2012/08/28 17:59:09
Done.
| |
| 179 public: | |
| 180 // Takes ownership of both |name| and |value|. | |
|
battre
2012/08/27 17:16:25
actually this should be "Takes ownership of the co
vabr (Chromium)
2012/08/28 17:59:09
Done.
| |
| 181 TestGroup(ScopedVector<const MatchTest>* name, | |
| 182 ScopedVector<const MatchTest>* value); | |
| 183 ~TestGroup(); | |
| 184 // Does the header |name|: |value| match all tests in this test group? | |
| 185 bool Matches(const std::string& name, const std::string& value) const; | |
| 186 private: | |
| 187 // Tests to be passed by a header's name. | |
| 188 const ScopedVector<const MatchTest> name_; | |
| 189 // Tests to be passed by a header's value. | |
| 190 const ScopedVector<const MatchTest> value_; | |
| 191 DISALLOW_COPY_AND_ASSIGN(TestGroup); | |
| 192 }; | |
| 193 | |
| 194 WebRequestConditionAttributeResponseHeaders( | |
| 195 bool positive_test, ScopedVector<const TestGroup>* tests); | |
| 196 | |
| 197 // Gets the tests' description in |tests| and creates the corresponding | |
| 198 // TestGroup. Returns NULL on failure. | |
| 199 static scoped_ptr<const TestGroup> CreateTests( | |
| 200 const base::DictionaryValue* tests, | |
| 201 std::string* error); | |
| 202 // Helper to CreateTests. Never returns NULL, except for memory failures. | |
| 203 static scoped_ptr<const MatchTest> CreateMatchTest(const Value* content, | |
| 204 bool is_name_test, | |
| 205 MatchType match_type); | |
| 206 | |
| 207 // The condition is satisfied if there is a header and its value such that for | |
| 208 // some |i| the header passes all the tests from |tests_[i]|. | |
| 209 ScopedVector<const TestGroup> tests_; | |
| 210 | |
| 211 // True means that IsFulfilled() reports whether the condition is satisfied. | |
| 212 // False means that it reports whether the condition is NOT satisfied. | |
| 213 bool positive_test_; | |
| 214 | |
| 215 DISALLOW_COPY_AND_ASSIGN(WebRequestConditionAttributeResponseHeaders); | |
| 216 }; | |
| 217 | |
| 136 } // namespace extensions | 218 } // namespace extensions |
| 137 | 219 |
| 138 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDI TION_ATTRIBUTE_H_ | 220 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDI TION_ATTRIBUTE_H_ |
| OLD | NEW |