1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDITION_ATTRIBUTE_H_
#define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDITION_ATTRIBUTE_H_
#include <string>
#include <vector>
#include "base/basictypes.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "chrome/browser/extensions/api/declarative_webrequest/request_stage.h"
#include "chrome/common/extensions/api/events.h"
#include "webkit/common/resource_type.h"
namespace base {
class Value;
}
namespace net {
class URLRequest;
}
namespace extensions {
class HeaderMatcher;
struct WebRequestData;
// Base class for all condition attributes of the declarative Web Request API
// except for condition attribute to test URLPatterns.
class WebRequestConditionAttribute
: public base::RefCounted<WebRequestConditionAttribute> {
public:
enum Type {
CONDITION_RESOURCE_TYPE,
CONDITION_CONTENT_TYPE,
CONDITION_RESPONSE_HEADERS,
CONDITION_THIRD_PARTY,
CONDITION_REQUEST_HEADERS,
CONDITION_STAGES
};
WebRequestConditionAttribute();
// Factory method that creates a WebRequestConditionAttribute for the JSON
// dictionary {|name|: |value|} passed by the extension API. Sets |error| and
// returns NULL if something fails.
// The ownership of |value| remains at the caller.
static scoped_refptr<const WebRequestConditionAttribute> Create(
const std::string& name,
const base::Value* value,
std::string* error);
// Returns a bit vector representing extensions::RequestStage. The bit vector
// contains a 1 for each request stage during which the condition attribute
// can be tested.
virtual int GetStages() const = 0;
// Returns whether the condition is fulfilled for this request.
virtual bool IsFulfilled(
const WebRequestData& request_data) const = 0;
virtual Type GetType() const = 0;
virtual std::string GetName() const = 0;
// Compares the Type of two WebRequestConditionAttributes, needs to be
// overridden for parameterized types.
virtual bool Equals(const WebRequestConditionAttribute* other) const;
protected:
friend class base::RefCounted<WebRequestConditionAttribute>;
virtual ~WebRequestConditionAttribute();
private:
DISALLOW_COPY_AND_ASSIGN(WebRequestConditionAttribute);
};
typedef std::vector<scoped_refptr<const WebRequestConditionAttribute> >
WebRequestConditionAttributes;
//
// The following are concrete condition attributes.
//
// Condition that checks whether a request is for a specific resource type.
class WebRequestConditionAttributeResourceType
: public WebRequestConditionAttribute {
public:
// Factory method, see WebRequestConditionAttribute::Create.
static scoped_refptr<const WebRequestConditionAttribute> Create(
const std::string& instance_type,
const base::Value* value,
std::string* error,
bool* bad_message);
// Implementation of WebRequestConditionAttribute:
virtual int GetStages() const OVERRIDE;
virtual bool IsFulfilled(
const WebRequestData& request_data) const OVERRIDE;
virtual Type GetType() const OVERRIDE;
virtual std::string GetName() const OVERRIDE;
virtual bool Equals(const WebRequestConditionAttribute* other) const OVERRIDE;
private:
explicit WebRequestConditionAttributeResourceType(
const std::vector<ResourceType::Type>& types);
virtual ~WebRequestConditionAttributeResourceType();
const std::vector<ResourceType::Type> types_;
DISALLOW_COPY_AND_ASSIGN(WebRequestConditionAttributeResourceType);
};
// Condition that checks whether a response's Content-Type header has a
// certain MIME media type.
class WebRequestConditionAttributeContentType
: public WebRequestConditionAttribute {
public:
// Factory method, see WebRequestConditionAttribute::Create.
static scoped_refptr<const WebRequestConditionAttribute> Create(
const std::string& name,
const base::Value* value,
std::string* error,
bool* bad_message);
// Implementation of WebRequestConditionAttribute:
virtual int GetStages() const OVERRIDE;
virtual bool IsFulfilled(
const WebRequestData& request_data) const OVERRIDE;
virtual Type GetType() const OVERRIDE;
virtual std::string GetName() const OVERRIDE;
virtual bool Equals(const WebRequestConditionAttribute* other) const OVERRIDE;
private:
explicit WebRequestConditionAttributeContentType(
const std::vector<std::string>& include_content_types,
bool inclusive);
virtual ~WebRequestConditionAttributeContentType();
const std::vector<std::string> content_types_;
const bool inclusive_;
DISALLOW_COPY_AND_ASSIGN(WebRequestConditionAttributeContentType);
};
// Condition attribute for matching against request headers. Uses HeaderMatcher
// to handle the actual tests, in connection with a boolean positiveness
// flag. If that flag is set to true, then IsFulfilled() returns true iff
// |header_matcher_| matches at least one header. Otherwise IsFulfilled()
// returns true iff the |header_matcher_| matches no header.
class WebRequestConditionAttributeRequestHeaders
: public WebRequestConditionAttribute {
public:
// Factory method, see WebRequestConditionAttribute::Create.
static scoped_refptr<const WebRequestConditionAttribute> Create(
const std::string& name,
const base::Value* value,
std::string* error,
bool* bad_message);
// Implementation of WebRequestConditionAttribute:
virtual int GetStages() const OVERRIDE;
virtual bool IsFulfilled(
const WebRequestData& request_data) const OVERRIDE;
virtual Type GetType() const OVERRIDE;
virtual std::string GetName() const OVERRIDE;
virtual bool Equals(const WebRequestConditionAttribute* other) const OVERRIDE;
private:
WebRequestConditionAttributeRequestHeaders(
scoped_ptr<const HeaderMatcher> header_matcher, bool positive);
virtual ~WebRequestConditionAttributeRequestHeaders();
const scoped_ptr<const HeaderMatcher> header_matcher_;
const bool positive_;
DISALLOW_COPY_AND_ASSIGN(WebRequestConditionAttributeRequestHeaders);
};
// Condition attribute for matching against response headers. Uses HeaderMatcher
// to handle the actual tests, in connection with a boolean positiveness
// flag. If that flag is set to true, then IsFulfilled() returns true iff
// |header_matcher_| matches at least one header. Otherwise IsFulfilled()
// returns true iff the |header_matcher_| matches no header.
class WebRequestConditionAttributeResponseHeaders
: public WebRequestConditionAttribute {
public:
// Factory method, see WebRequestConditionAttribute::Create.
static scoped_refptr<const WebRequestConditionAttribute> Create(
const std::string& name,
const base::Value* value,
std::string* error,
bool* bad_message);
// Implementation of WebRequestConditionAttribute:
virtual int GetStages() const OVERRIDE;
virtual bool IsFulfilled(
const WebRequestData& request_data) const OVERRIDE;
virtual Type GetType() const OVERRIDE;
virtual std::string GetName() const OVERRIDE;
virtual bool Equals(const WebRequestConditionAttribute* other) const OVERRIDE;
private:
WebRequestConditionAttributeResponseHeaders(
scoped_ptr<const HeaderMatcher> header_matcher, bool positive);
virtual ~WebRequestConditionAttributeResponseHeaders();
const scoped_ptr<const HeaderMatcher> header_matcher_;
const bool positive_;
DISALLOW_COPY_AND_ASSIGN(WebRequestConditionAttributeResponseHeaders);
};
// This condition tests whether the request origin is third-party.
class WebRequestConditionAttributeThirdParty
: public WebRequestConditionAttribute {
public:
// Factory method, see WebRequestConditionAttribute::Create.
static scoped_refptr<const WebRequestConditionAttribute> Create(
const std::string& name,
const base::Value* value,
std::string* error,
bool* bad_message);
// Implementation of WebRequestConditionAttribute:
virtual int GetStages() const OVERRIDE;
virtual bool IsFulfilled(
const WebRequestData& request_data) const OVERRIDE;
virtual Type GetType() const OVERRIDE;
virtual std::string GetName() const OVERRIDE;
virtual bool Equals(const WebRequestConditionAttribute* other) const OVERRIDE;
private:
explicit WebRequestConditionAttributeThirdParty(bool match_third_party);
virtual ~WebRequestConditionAttributeThirdParty();
const bool match_third_party_;
DISALLOW_COPY_AND_ASSIGN(WebRequestConditionAttributeThirdParty);
};
// This condition is used as a filter for request stages. It is true exactly in
// stages specified on construction.
class WebRequestConditionAttributeStages
: public WebRequestConditionAttribute {
public:
// Factory method, see WebRequestConditionAttribute::Create.
static scoped_refptr<const WebRequestConditionAttribute> Create(
const std::string& name,
const base::Value* value,
std::string* error,
bool* bad_message);
// Implementation of WebRequestConditionAttribute:
virtual int GetStages() const OVERRIDE;
virtual bool IsFulfilled(
const WebRequestData& request_data) const OVERRIDE;
virtual Type GetType() const OVERRIDE;
virtual std::string GetName() const OVERRIDE;
virtual bool Equals(const WebRequestConditionAttribute* other) const OVERRIDE;
private:
explicit WebRequestConditionAttributeStages(int allowed_stages);
virtual ~WebRequestConditionAttributeStages();
const int allowed_stages_; // Composition of RequestStage values.
DISALLOW_COPY_AND_ASSIGN(WebRequestConditionAttributeStages);
};
} // namespace extensions
#endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDITION_ATTRIBUTE_H_
|