Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / extensions / browser / api / declarative_webrequest / webrequest_condition_attribute.h
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.
4
5 #ifndef EXTENSIONS_BROWSER_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDITION_ATTRIBUTE_H_
6 #define EXTENSIONS_BROWSER_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDITION_ATTRIBUTE_H_
7
8 #include <string>
9 #include <vector>
10
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"
17
18 namespace base {
19 class Value;
20 }
21
22 namespace net {
23 class URLRequest;
24 }
25
26 namespace extensions {
27
28 class HeaderMatcher;
29 struct WebRequestData;
30
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> {
35  public:
36   enum Type {
37     CONDITION_RESOURCE_TYPE,
38     CONDITION_CONTENT_TYPE,
39     CONDITION_RESPONSE_HEADERS,
40     CONDITION_THIRD_PARTY,
41     CONDITION_REQUEST_HEADERS,
42     CONDITION_STAGES
43   };
44
45   WebRequestConditionAttribute();
46
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,
54       std::string* error);
55
56   // Returns a bit vector representing extensions::RequestStage. The bit vector
57   // contains a 1 for each request stage during which the condition attribute
58   // can be tested.
59   virtual int GetStages() const = 0;
60
61   // Returns whether the condition is fulfilled for this request.
62   virtual bool IsFulfilled(
63       const WebRequestData& request_data) const = 0;
64
65   virtual Type GetType() const = 0;
66   virtual std::string GetName() const = 0;
67
68   // Compares the Type of two WebRequestConditionAttributes, needs to be
69   // overridden for parameterized types.
70   virtual bool Equals(const WebRequestConditionAttribute* other) const;
71
72  protected:
73   friend class base::RefCounted<WebRequestConditionAttribute>;
74   virtual ~WebRequestConditionAttribute();
75
76  private:
77   DISALLOW_COPY_AND_ASSIGN(WebRequestConditionAttribute);
78 };
79
80 typedef std::vector<scoped_refptr<const WebRequestConditionAttribute> >
81     WebRequestConditionAttributes;
82
83 //
84 // The following are concrete condition attributes.
85 //
86
87 // Condition that checks whether a request is for a specific resource type.
88 class WebRequestConditionAttributeResourceType
89     : public WebRequestConditionAttribute {
90  public:
91   // Factory method, see WebRequestConditionAttribute::Create.
92   static scoped_refptr<const WebRequestConditionAttribute> Create(
93       const std::string& instance_type,
94       const base::Value* value,
95       std::string* error,
96       bool* bad_message);
97
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;
105
106  private:
107   explicit WebRequestConditionAttributeResourceType(
108       const std::vector<content::ResourceType>& types);
109   virtual ~WebRequestConditionAttributeResourceType();
110
111   const std::vector<content::ResourceType> types_;
112
113   DISALLOW_COPY_AND_ASSIGN(WebRequestConditionAttributeResourceType);
114 };
115
116 // Condition that checks whether a response's Content-Type header has a
117 // certain MIME media type.
118 class WebRequestConditionAttributeContentType
119     : public WebRequestConditionAttribute {
120  public:
121   // Factory method, see WebRequestConditionAttribute::Create.
122   static scoped_refptr<const WebRequestConditionAttribute> Create(
123       const std::string& name,
124       const base::Value* value,
125       std::string* error,
126       bool* bad_message);
127
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;
135
136  private:
137   explicit WebRequestConditionAttributeContentType(
138       const std::vector<std::string>& include_content_types,
139       bool inclusive);
140   virtual ~WebRequestConditionAttributeContentType();
141
142   const std::vector<std::string> content_types_;
143   const bool inclusive_;
144
145   DISALLOW_COPY_AND_ASSIGN(WebRequestConditionAttributeContentType);
146 };
147
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 {
155  public:
156   // Factory method, see WebRequestConditionAttribute::Create.
157   static scoped_refptr<const WebRequestConditionAttribute> Create(
158       const std::string& name,
159       const base::Value* value,
160       std::string* error,
161       bool* bad_message);
162
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;
170
171  private:
172   WebRequestConditionAttributeRequestHeaders(
173       scoped_ptr<const HeaderMatcher> header_matcher, bool positive);
174   virtual ~WebRequestConditionAttributeRequestHeaders();
175
176   const scoped_ptr<const HeaderMatcher> header_matcher_;
177   const bool positive_;
178
179   DISALLOW_COPY_AND_ASSIGN(WebRequestConditionAttributeRequestHeaders);
180 };
181
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 {
189  public:
190   // Factory method, see WebRequestConditionAttribute::Create.
191   static scoped_refptr<const WebRequestConditionAttribute> Create(
192       const std::string& name,
193       const base::Value* value,
194       std::string* error,
195       bool* bad_message);
196
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;
204
205  private:
206   WebRequestConditionAttributeResponseHeaders(
207       scoped_ptr<const HeaderMatcher> header_matcher, bool positive);
208   virtual ~WebRequestConditionAttributeResponseHeaders();
209
210   const scoped_ptr<const HeaderMatcher> header_matcher_;
211   const bool positive_;
212
213   DISALLOW_COPY_AND_ASSIGN(WebRequestConditionAttributeResponseHeaders);
214 };
215
216 // This condition tests whether the request origin is third-party.
217 class WebRequestConditionAttributeThirdParty
218     : public WebRequestConditionAttribute {
219  public:
220   // Factory method, see WebRequestConditionAttribute::Create.
221   static scoped_refptr<const WebRequestConditionAttribute> Create(
222       const std::string& name,
223       const base::Value* value,
224       std::string* error,
225       bool* bad_message);
226
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;
234
235  private:
236   explicit WebRequestConditionAttributeThirdParty(bool match_third_party);
237   virtual ~WebRequestConditionAttributeThirdParty();
238
239   const bool match_third_party_;
240
241   DISALLOW_COPY_AND_ASSIGN(WebRequestConditionAttributeThirdParty);
242 };
243
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 {
248  public:
249   // Factory method, see WebRequestConditionAttribute::Create.
250   static scoped_refptr<const WebRequestConditionAttribute> Create(
251       const std::string& name,
252       const base::Value* value,
253       std::string* error,
254       bool* bad_message);
255
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;
263
264  private:
265   explicit WebRequestConditionAttributeStages(int allowed_stages);
266   virtual ~WebRequestConditionAttributeStages();
267
268   const int allowed_stages_;  // Composition of RequestStage values.
269
270   DISALLOW_COPY_AND_ASSIGN(WebRequestConditionAttributeStages);
271 };
272
273 }  // namespace extensions
274
275 #endif  // EXTENSIONS_BROWSER_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDITION_ATTRIBUTE_H_