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.
5 #ifndef EXTENSIONS_BROWSER_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDITION_ATTRIBUTE_H_
6 #define EXTENSIONS_BROWSER_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDITION_ATTRIBUTE_H_
11 #include "base/basictypes.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "content/public/common/resource_type.h"
15 #include "extensions/browser/api/declarative_webrequest/request_stage.h"
16 #include "extensions/common/api/events.h"
26 namespace extensions
{
29 struct WebRequestData
;
31 // Base class for all condition attributes of the declarative Web Request API
32 // except for condition attribute to test URLPatterns.
33 class WebRequestConditionAttribute
34 : public base::RefCounted
<WebRequestConditionAttribute
> {
37 CONDITION_RESOURCE_TYPE
,
38 CONDITION_CONTENT_TYPE
,
39 CONDITION_RESPONSE_HEADERS
,
40 CONDITION_THIRD_PARTY
,
41 CONDITION_REQUEST_HEADERS
,
45 WebRequestConditionAttribute();
47 // Factory method that creates a WebRequestConditionAttribute for the JSON
48 // dictionary {|name|: |value|} passed by the extension API. Sets |error| and
49 // returns NULL if something fails.
50 // The ownership of |value| remains at the caller.
51 static scoped_refptr
<const WebRequestConditionAttribute
> Create(
52 const std::string
& name
,
53 const base::Value
* value
,
56 // Returns a bit vector representing extensions::RequestStage. The bit vector
57 // contains a 1 for each request stage during which the condition attribute
59 virtual int GetStages() const = 0;
61 // Returns whether the condition is fulfilled for this request.
62 virtual bool IsFulfilled(
63 const WebRequestData
& request_data
) const = 0;
65 virtual Type
GetType() const = 0;
66 virtual std::string
GetName() const = 0;
68 // Compares the Type of two WebRequestConditionAttributes, needs to be
69 // overridden for parameterized types.
70 virtual bool Equals(const WebRequestConditionAttribute
* other
) const;
73 friend class base::RefCounted
<WebRequestConditionAttribute
>;
74 virtual ~WebRequestConditionAttribute();
77 DISALLOW_COPY_AND_ASSIGN(WebRequestConditionAttribute
);
80 typedef std::vector
<scoped_refptr
<const WebRequestConditionAttribute
> >
81 WebRequestConditionAttributes
;
84 // The following are concrete condition attributes.
87 // Condition that checks whether a request is for a specific resource type.
88 class WebRequestConditionAttributeResourceType
89 : public WebRequestConditionAttribute
{
91 // Factory method, see WebRequestConditionAttribute::Create.
92 static scoped_refptr
<const WebRequestConditionAttribute
> Create(
93 const std::string
& instance_type
,
94 const base::Value
* value
,
98 // Implementation of WebRequestConditionAttribute:
99 int GetStages() const override
;
100 bool IsFulfilled(const WebRequestData
& request_data
) const override
;
101 Type
GetType() const override
;
102 std::string
GetName() const override
;
103 bool Equals(const WebRequestConditionAttribute
* other
) const override
;
106 explicit WebRequestConditionAttributeResourceType(
107 const std::vector
<content::ResourceType
>& types
);
108 ~WebRequestConditionAttributeResourceType() override
;
110 const std::vector
<content::ResourceType
> types_
;
112 DISALLOW_COPY_AND_ASSIGN(WebRequestConditionAttributeResourceType
);
115 // Condition that checks whether a response's Content-Type header has a
116 // certain MIME media type.
117 class WebRequestConditionAttributeContentType
118 : public WebRequestConditionAttribute
{
120 // Factory method, see WebRequestConditionAttribute::Create.
121 static scoped_refptr
<const WebRequestConditionAttribute
> Create(
122 const std::string
& name
,
123 const base::Value
* value
,
127 // Implementation of WebRequestConditionAttribute:
128 int GetStages() const override
;
129 bool IsFulfilled(const WebRequestData
& request_data
) const override
;
130 Type
GetType() const override
;
131 std::string
GetName() const override
;
132 bool Equals(const WebRequestConditionAttribute
* other
) const override
;
135 explicit WebRequestConditionAttributeContentType(
136 const std::vector
<std::string
>& include_content_types
,
138 ~WebRequestConditionAttributeContentType() override
;
140 const std::vector
<std::string
> content_types_
;
141 const bool inclusive_
;
143 DISALLOW_COPY_AND_ASSIGN(WebRequestConditionAttributeContentType
);
146 // Condition attribute for matching against request headers. Uses HeaderMatcher
147 // to handle the actual tests, in connection with a boolean positiveness
148 // flag. If that flag is set to true, then IsFulfilled() returns true iff
149 // |header_matcher_| matches at least one header. Otherwise IsFulfilled()
150 // returns true iff the |header_matcher_| matches no header.
151 class WebRequestConditionAttributeRequestHeaders
152 : public WebRequestConditionAttribute
{
154 // Factory method, see WebRequestConditionAttribute::Create.
155 static scoped_refptr
<const WebRequestConditionAttribute
> Create(
156 const std::string
& name
,
157 const base::Value
* value
,
161 // Implementation of WebRequestConditionAttribute:
162 int GetStages() const override
;
163 bool IsFulfilled(const WebRequestData
& request_data
) const override
;
164 Type
GetType() const override
;
165 std::string
GetName() const override
;
166 bool Equals(const WebRequestConditionAttribute
* other
) const override
;
169 WebRequestConditionAttributeRequestHeaders(
170 scoped_ptr
<const HeaderMatcher
> header_matcher
, bool positive
);
171 ~WebRequestConditionAttributeRequestHeaders() override
;
173 const scoped_ptr
<const HeaderMatcher
> header_matcher_
;
174 const bool positive_
;
176 DISALLOW_COPY_AND_ASSIGN(WebRequestConditionAttributeRequestHeaders
);
179 // Condition attribute for matching against response headers. Uses HeaderMatcher
180 // to handle the actual tests, in connection with a boolean positiveness
181 // flag. If that flag is set to true, then IsFulfilled() returns true iff
182 // |header_matcher_| matches at least one header. Otherwise IsFulfilled()
183 // returns true iff the |header_matcher_| matches no header.
184 class WebRequestConditionAttributeResponseHeaders
185 : public WebRequestConditionAttribute
{
187 // Factory method, see WebRequestConditionAttribute::Create.
188 static scoped_refptr
<const WebRequestConditionAttribute
> Create(
189 const std::string
& name
,
190 const base::Value
* value
,
194 // Implementation of WebRequestConditionAttribute:
195 int GetStages() const override
;
196 bool IsFulfilled(const WebRequestData
& request_data
) const override
;
197 Type
GetType() const override
;
198 std::string
GetName() const override
;
199 bool Equals(const WebRequestConditionAttribute
* other
) const override
;
202 WebRequestConditionAttributeResponseHeaders(
203 scoped_ptr
<const HeaderMatcher
> header_matcher
, bool positive
);
204 ~WebRequestConditionAttributeResponseHeaders() override
;
206 const scoped_ptr
<const HeaderMatcher
> header_matcher_
;
207 const bool positive_
;
209 DISALLOW_COPY_AND_ASSIGN(WebRequestConditionAttributeResponseHeaders
);
212 // This condition tests whether the request origin is third-party.
213 class WebRequestConditionAttributeThirdParty
214 : public WebRequestConditionAttribute
{
216 // Factory method, see WebRequestConditionAttribute::Create.
217 static scoped_refptr
<const WebRequestConditionAttribute
> Create(
218 const std::string
& name
,
219 const base::Value
* value
,
223 // Implementation of WebRequestConditionAttribute:
224 int GetStages() const override
;
225 bool IsFulfilled(const WebRequestData
& request_data
) const override
;
226 Type
GetType() const override
;
227 std::string
GetName() const override
;
228 bool Equals(const WebRequestConditionAttribute
* other
) const override
;
231 explicit WebRequestConditionAttributeThirdParty(bool match_third_party
);
232 ~WebRequestConditionAttributeThirdParty() override
;
234 const bool match_third_party_
;
236 DISALLOW_COPY_AND_ASSIGN(WebRequestConditionAttributeThirdParty
);
239 // This condition is used as a filter for request stages. It is true exactly in
240 // stages specified on construction.
241 class WebRequestConditionAttributeStages
242 : public WebRequestConditionAttribute
{
244 // Factory method, see WebRequestConditionAttribute::Create.
245 static scoped_refptr
<const WebRequestConditionAttribute
> Create(
246 const std::string
& name
,
247 const base::Value
* value
,
251 // Implementation of WebRequestConditionAttribute:
252 int GetStages() const override
;
253 bool IsFulfilled(const WebRequestData
& request_data
) const override
;
254 Type
GetType() const override
;
255 std::string
GetName() const override
;
256 bool Equals(const WebRequestConditionAttribute
* other
) const override
;
259 explicit WebRequestConditionAttributeStages(int allowed_stages
);
260 ~WebRequestConditionAttributeStages() override
;
262 const int allowed_stages_
; // Composition of RequestStage values.
264 DISALLOW_COPY_AND_ASSIGN(WebRequestConditionAttributeStages
);
267 } // namespace extensions
269 #endif // EXTENSIONS_BROWSER_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDITION_ATTRIBUTE_H_