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 virtual int GetStages() const OVERRIDE
;
100 virtual bool IsFulfilled(
101 const WebRequestData
& request_data
) const OVERRIDE
;
102 virtual Type
GetType() const OVERRIDE
;
103 virtual std::string
GetName() const OVERRIDE
;
104 virtual bool Equals(const WebRequestConditionAttribute
* other
) const OVERRIDE
;
107 explicit WebRequestConditionAttributeResourceType(
108 const std::vector
<content::ResourceType
>& types
);
109 virtual ~WebRequestConditionAttributeResourceType();
111 const std::vector
<content::ResourceType
> types_
;
113 DISALLOW_COPY_AND_ASSIGN(WebRequestConditionAttributeResourceType
);
116 // Condition that checks whether a response's Content-Type header has a
117 // certain MIME media type.
118 class WebRequestConditionAttributeContentType
119 : public WebRequestConditionAttribute
{
121 // Factory method, see WebRequestConditionAttribute::Create.
122 static scoped_refptr
<const WebRequestConditionAttribute
> Create(
123 const std::string
& name
,
124 const base::Value
* value
,
128 // Implementation of WebRequestConditionAttribute:
129 virtual int GetStages() const OVERRIDE
;
130 virtual bool IsFulfilled(
131 const WebRequestData
& request_data
) const OVERRIDE
;
132 virtual Type
GetType() const OVERRIDE
;
133 virtual std::string
GetName() const OVERRIDE
;
134 virtual bool Equals(const WebRequestConditionAttribute
* other
) const OVERRIDE
;
137 explicit WebRequestConditionAttributeContentType(
138 const std::vector
<std::string
>& include_content_types
,
140 virtual ~WebRequestConditionAttributeContentType();
142 const std::vector
<std::string
> content_types_
;
143 const bool inclusive_
;
145 DISALLOW_COPY_AND_ASSIGN(WebRequestConditionAttributeContentType
);
148 // Condition attribute for matching against request headers. Uses HeaderMatcher
149 // to handle the actual tests, in connection with a boolean positiveness
150 // flag. If that flag is set to true, then IsFulfilled() returns true iff
151 // |header_matcher_| matches at least one header. Otherwise IsFulfilled()
152 // returns true iff the |header_matcher_| matches no header.
153 class WebRequestConditionAttributeRequestHeaders
154 : public WebRequestConditionAttribute
{
156 // Factory method, see WebRequestConditionAttribute::Create.
157 static scoped_refptr
<const WebRequestConditionAttribute
> Create(
158 const std::string
& name
,
159 const base::Value
* value
,
163 // Implementation of WebRequestConditionAttribute:
164 virtual int GetStages() const OVERRIDE
;
165 virtual bool IsFulfilled(
166 const WebRequestData
& request_data
) const OVERRIDE
;
167 virtual Type
GetType() const OVERRIDE
;
168 virtual std::string
GetName() const OVERRIDE
;
169 virtual bool Equals(const WebRequestConditionAttribute
* other
) const OVERRIDE
;
172 WebRequestConditionAttributeRequestHeaders(
173 scoped_ptr
<const HeaderMatcher
> header_matcher
, bool positive
);
174 virtual ~WebRequestConditionAttributeRequestHeaders();
176 const scoped_ptr
<const HeaderMatcher
> header_matcher_
;
177 const bool positive_
;
179 DISALLOW_COPY_AND_ASSIGN(WebRequestConditionAttributeRequestHeaders
);
182 // Condition attribute for matching against response headers. Uses HeaderMatcher
183 // to handle the actual tests, in connection with a boolean positiveness
184 // flag. If that flag is set to true, then IsFulfilled() returns true iff
185 // |header_matcher_| matches at least one header. Otherwise IsFulfilled()
186 // returns true iff the |header_matcher_| matches no header.
187 class WebRequestConditionAttributeResponseHeaders
188 : public WebRequestConditionAttribute
{
190 // Factory method, see WebRequestConditionAttribute::Create.
191 static scoped_refptr
<const WebRequestConditionAttribute
> Create(
192 const std::string
& name
,
193 const base::Value
* value
,
197 // Implementation of WebRequestConditionAttribute:
198 virtual int GetStages() const OVERRIDE
;
199 virtual bool IsFulfilled(
200 const WebRequestData
& request_data
) const OVERRIDE
;
201 virtual Type
GetType() const OVERRIDE
;
202 virtual std::string
GetName() const OVERRIDE
;
203 virtual bool Equals(const WebRequestConditionAttribute
* other
) const OVERRIDE
;
206 WebRequestConditionAttributeResponseHeaders(
207 scoped_ptr
<const HeaderMatcher
> header_matcher
, bool positive
);
208 virtual ~WebRequestConditionAttributeResponseHeaders();
210 const scoped_ptr
<const HeaderMatcher
> header_matcher_
;
211 const bool positive_
;
213 DISALLOW_COPY_AND_ASSIGN(WebRequestConditionAttributeResponseHeaders
);
216 // This condition tests whether the request origin is third-party.
217 class WebRequestConditionAttributeThirdParty
218 : public WebRequestConditionAttribute
{
220 // Factory method, see WebRequestConditionAttribute::Create.
221 static scoped_refptr
<const WebRequestConditionAttribute
> Create(
222 const std::string
& name
,
223 const base::Value
* value
,
227 // Implementation of WebRequestConditionAttribute:
228 virtual int GetStages() const OVERRIDE
;
229 virtual bool IsFulfilled(
230 const WebRequestData
& request_data
) const OVERRIDE
;
231 virtual Type
GetType() const OVERRIDE
;
232 virtual std::string
GetName() const OVERRIDE
;
233 virtual bool Equals(const WebRequestConditionAttribute
* other
) const OVERRIDE
;
236 explicit WebRequestConditionAttributeThirdParty(bool match_third_party
);
237 virtual ~WebRequestConditionAttributeThirdParty();
239 const bool match_third_party_
;
241 DISALLOW_COPY_AND_ASSIGN(WebRequestConditionAttributeThirdParty
);
244 // This condition is used as a filter for request stages. It is true exactly in
245 // stages specified on construction.
246 class WebRequestConditionAttributeStages
247 : public WebRequestConditionAttribute
{
249 // Factory method, see WebRequestConditionAttribute::Create.
250 static scoped_refptr
<const WebRequestConditionAttribute
> Create(
251 const std::string
& name
,
252 const base::Value
* value
,
256 // Implementation of WebRequestConditionAttribute:
257 virtual int GetStages() const OVERRIDE
;
258 virtual bool IsFulfilled(
259 const WebRequestData
& request_data
) const OVERRIDE
;
260 virtual Type
GetType() const OVERRIDE
;
261 virtual std::string
GetName() const OVERRIDE
;
262 virtual bool Equals(const WebRequestConditionAttribute
* other
) const OVERRIDE
;
265 explicit WebRequestConditionAttributeStages(int allowed_stages
);
266 virtual ~WebRequestConditionAttributeStages();
268 const int allowed_stages_
; // Composition of RequestStage values.
270 DISALLOW_COPY_AND_ASSIGN(WebRequestConditionAttributeStages
);
273 } // namespace extensions
275 #endif // EXTENSIONS_BROWSER_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDITION_ATTRIBUTE_H_