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_ACTION_H_
6 #define EXTENSIONS_BROWSER_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_ACTION_H_
12 #include "base/compiler_specific.h"
13 #include "base/memory/linked_ptr.h"
14 #include "base/memory/ref_counted.h"
15 #include "extensions/browser/api/declarative/declarative_rule.h"
16 #include "extensions/browser/api/declarative_webrequest/request_stage.h"
17 #include "extensions/browser/api/web_request/web_request_api_helpers.h"
18 #include "extensions/common/api/events.h"
21 class WebRequestPermission
;
24 class DictionaryValue
;
29 namespace extension_web_request_api_helpers
{
30 struct EventResponseDelta
;
33 namespace extensions
{
36 struct WebRequestData
;
47 namespace extensions
{
49 typedef linked_ptr
<extension_web_request_api_helpers::EventResponseDelta
>
50 LinkedPtrEventResponseDelta
;
52 // Base class for all WebRequestActions of the declarative Web Request API.
53 class WebRequestAction
: public base::RefCounted
<WebRequestAction
> {
55 // Type identifiers for concrete WebRequestActions. If you add a new type,
56 // also update the unittest WebRequestActionTest.GetName, and add a
57 // WebRequestActionWithThreadsTest.Permission* unittest.
59 ACTION_CANCEL_REQUEST
,
60 ACTION_REDIRECT_REQUEST
,
61 ACTION_REDIRECT_TO_TRANSPARENT_IMAGE
,
62 ACTION_REDIRECT_TO_EMPTY_DOCUMENT
,
63 ACTION_REDIRECT_BY_REGEX_DOCUMENT
,
64 ACTION_SET_REQUEST_HEADER
,
65 ACTION_REMOVE_REQUEST_HEADER
,
66 ACTION_ADD_RESPONSE_HEADER
,
67 ACTION_REMOVE_RESPONSE_HEADER
,
69 ACTION_MODIFY_REQUEST_COOKIE
,
70 ACTION_MODIFY_RESPONSE_COOKIE
,
71 ACTION_SEND_MESSAGE_TO_EXTENSION
,
74 // Strategies for checking host permissions.
75 enum HostPermissionsStrategy
{
76 STRATEGY_NONE
, // Do not check host permissions.
77 STRATEGY_DEFAULT
, // Check for host permissions for all URLs
78 // before creating the delta.
79 STRATEGY_HOST
, // Check that host permissions match the URL
83 // Information necessary to decide how to apply a WebRequestAction
84 // inside a matching rule.
86 const InfoMap
* extension_info_map
;
87 const WebRequestData
& request_data
;
88 bool crosses_incognito
;
89 // Modified by each applied action:
90 std::list
<LinkedPtrEventResponseDelta
>* deltas
;
91 std::set
<std::string
>* ignored_tags
;
102 // Compares the Type of two WebRequestActions, needs to be overridden for
103 // parameterized types.
104 virtual bool Equals(const WebRequestAction
* other
) const;
106 // Return the JavaScript type name corresponding to type(). If there are
107 // more names, they are returned separated by a colon.
108 virtual std::string
GetName() const = 0;
110 int minimum_priority() const {
111 return minimum_priority_
;
114 HostPermissionsStrategy
host_permissions_strategy() const {
115 return host_permissions_strategy_
;
118 // Returns whether the specified extension has permission to execute this
119 // action on |request|. Checks the host permission if the host permissions
120 // strategy is STRATEGY_DEFAULT.
121 // |extension_info_map| may only be NULL for during testing, in which case
122 // host permissions are ignored. |crosses_incognito| specifies
123 // whether the request comes from a different profile than |extension_id|
124 // but was processed because the extension is in spanning mode.
125 virtual bool HasPermission(const InfoMap
* extension_info_map
,
126 const std::string
& extension_id
,
127 const net::URLRequest
* request
,
128 bool crosses_incognito
) const;
130 // Factory method that instantiates a concrete WebRequestAction
131 // implementation according to |json_action|, the representation of the
132 // WebRequestAction as received from the extension API.
133 // Sets |error| and returns NULL in case of a semantic error that cannot
134 // be caught by schema validation. Sets |bad_message| and returns NULL
135 // in case the input is syntactically unexpected.
136 static scoped_refptr
<const WebRequestAction
> Create(
137 content::BrowserContext
* browser_context
,
138 const Extension
* extension
,
139 const base::Value
& json_action
,
143 // Returns a description of the modification to the request caused by
145 virtual LinkedPtrEventResponseDelta
CreateDelta(
146 const WebRequestData
& request_data
,
147 const std::string
& extension_id
,
148 const base::Time
& extension_install_time
) const = 0;
150 // Applies this action to a request, recording the results into
151 // apply_info.deltas.
152 void Apply(const std::string
& extension_id
,
153 base::Time extension_install_time
,
154 ApplyInfo
* apply_info
) const;
157 friend class base::RefCounted
<WebRequestAction
>;
158 virtual ~WebRequestAction();
159 WebRequestAction(int stages
,
161 int minimum_priority
,
162 HostPermissionsStrategy strategy
);
165 // A bit vector representing a set of extensions::RequestStage during which
166 // the condition can be tested.
171 // The minimum priority of rules that may be evaluated after the rule
172 // containing this action.
173 const int minimum_priority_
;
175 // Defaults to STRATEGY_DEFAULT.
176 const HostPermissionsStrategy host_permissions_strategy_
;
179 typedef DeclarativeActionSet
<WebRequestAction
> WebRequestActionSet
;
182 // The following are concrete actions.
185 // Action that instructs to cancel a network request.
186 class WebRequestCancelAction
: public WebRequestAction
{
188 WebRequestCancelAction();
190 // Implementation of WebRequestAction:
191 std::string
GetName() const override
;
192 LinkedPtrEventResponseDelta
CreateDelta(
193 const WebRequestData
& request_data
,
194 const std::string
& extension_id
,
195 const base::Time
& extension_install_time
) const override
;
198 ~WebRequestCancelAction() override
;
199 DISALLOW_COPY_AND_ASSIGN(WebRequestCancelAction
);
202 // Action that instructs to redirect a network request.
203 class WebRequestRedirectAction
: public WebRequestAction
{
205 explicit WebRequestRedirectAction(const GURL
& redirect_url
);
207 // Implementation of WebRequestAction:
208 bool Equals(const WebRequestAction
* other
) const override
;
209 std::string
GetName() const override
;
210 LinkedPtrEventResponseDelta
CreateDelta(
211 const WebRequestData
& request_data
,
212 const std::string
& extension_id
,
213 const base::Time
& extension_install_time
) const override
;
216 ~WebRequestRedirectAction() override
;
218 GURL redirect_url_
; // Target to which the request shall be redirected.
220 DISALLOW_COPY_AND_ASSIGN(WebRequestRedirectAction
);
223 // Action that instructs to redirect a network request to a transparent image.
224 class WebRequestRedirectToTransparentImageAction
: public WebRequestAction
{
226 WebRequestRedirectToTransparentImageAction();
228 // Implementation of WebRequestAction:
229 std::string
GetName() const override
;
230 LinkedPtrEventResponseDelta
CreateDelta(
231 const WebRequestData
& request_data
,
232 const std::string
& extension_id
,
233 const base::Time
& extension_install_time
) const override
;
236 ~WebRequestRedirectToTransparentImageAction() override
;
237 DISALLOW_COPY_AND_ASSIGN(WebRequestRedirectToTransparentImageAction
);
241 // Action that instructs to redirect a network request to an empty document.
242 class WebRequestRedirectToEmptyDocumentAction
: public WebRequestAction
{
244 WebRequestRedirectToEmptyDocumentAction();
246 // Implementation of WebRequestAction:
247 std::string
GetName() const override
;
248 LinkedPtrEventResponseDelta
CreateDelta(
249 const WebRequestData
& request_data
,
250 const std::string
& extension_id
,
251 const base::Time
& extension_install_time
) const override
;
254 ~WebRequestRedirectToEmptyDocumentAction() override
;
255 DISALLOW_COPY_AND_ASSIGN(WebRequestRedirectToEmptyDocumentAction
);
258 // Action that instructs to redirect a network request.
259 class WebRequestRedirectByRegExAction
: public WebRequestAction
{
261 // The |to_pattern| has to be passed in RE2 syntax with the exception that
262 // capture groups are referenced in Perl style ($1, $2, ...).
263 explicit WebRequestRedirectByRegExAction(scoped_ptr
<re2::RE2
> from_pattern
,
264 const std::string
& to_pattern
);
266 // Conversion of capture group styles between Perl style ($1, $2, ...) and
267 // RE2 (\1, \2, ...).
268 static std::string
PerlToRe2Style(const std::string
& perl
);
270 // Implementation of WebRequestAction:
271 bool Equals(const WebRequestAction
* other
) const override
;
272 std::string
GetName() const override
;
273 LinkedPtrEventResponseDelta
CreateDelta(
274 const WebRequestData
& request_data
,
275 const std::string
& extension_id
,
276 const base::Time
& extension_install_time
) const override
;
279 ~WebRequestRedirectByRegExAction() override
;
281 scoped_ptr
<re2::RE2
> from_pattern_
;
282 std::string to_pattern_
;
284 DISALLOW_COPY_AND_ASSIGN(WebRequestRedirectByRegExAction
);
287 // Action that instructs to set a request header.
288 class WebRequestSetRequestHeaderAction
: public WebRequestAction
{
290 WebRequestSetRequestHeaderAction(const std::string
& name
,
291 const std::string
& value
);
293 // Implementation of WebRequestAction:
294 bool Equals(const WebRequestAction
* other
) const override
;
295 std::string
GetName() const override
;
296 LinkedPtrEventResponseDelta
CreateDelta(
297 const WebRequestData
& request_data
,
298 const std::string
& extension_id
,
299 const base::Time
& extension_install_time
) const override
;
302 ~WebRequestSetRequestHeaderAction() override
;
306 DISALLOW_COPY_AND_ASSIGN(WebRequestSetRequestHeaderAction
);
309 // Action that instructs to remove a request header.
310 class WebRequestRemoveRequestHeaderAction
: public WebRequestAction
{
312 explicit WebRequestRemoveRequestHeaderAction(const std::string
& name
);
314 // Implementation of WebRequestAction:
315 bool Equals(const WebRequestAction
* other
) const override
;
316 std::string
GetName() const override
;
317 LinkedPtrEventResponseDelta
CreateDelta(
318 const WebRequestData
& request_data
,
319 const std::string
& extension_id
,
320 const base::Time
& extension_install_time
) const override
;
323 ~WebRequestRemoveRequestHeaderAction() override
;
326 DISALLOW_COPY_AND_ASSIGN(WebRequestRemoveRequestHeaderAction
);
329 // Action that instructs to add a response header.
330 class WebRequestAddResponseHeaderAction
: public WebRequestAction
{
332 WebRequestAddResponseHeaderAction(const std::string
& name
,
333 const std::string
& value
);
335 // Implementation of WebRequestAction:
336 bool Equals(const WebRequestAction
* other
) const override
;
337 std::string
GetName() const override
;
338 LinkedPtrEventResponseDelta
CreateDelta(
339 const WebRequestData
& request_data
,
340 const std::string
& extension_id
,
341 const base::Time
& extension_install_time
) const override
;
344 ~WebRequestAddResponseHeaderAction() override
;
348 DISALLOW_COPY_AND_ASSIGN(WebRequestAddResponseHeaderAction
);
351 // Action that instructs to remove a response header.
352 class WebRequestRemoveResponseHeaderAction
: public WebRequestAction
{
354 explicit WebRequestRemoveResponseHeaderAction(const std::string
& name
,
355 const std::string
& value
,
358 // Implementation of WebRequestAction:
359 bool Equals(const WebRequestAction
* other
) const override
;
360 std::string
GetName() const override
;
361 LinkedPtrEventResponseDelta
CreateDelta(
362 const WebRequestData
& request_data
,
363 const std::string
& extension_id
,
364 const base::Time
& extension_install_time
) const override
;
367 ~WebRequestRemoveResponseHeaderAction() override
;
372 DISALLOW_COPY_AND_ASSIGN(WebRequestRemoveResponseHeaderAction
);
375 // Action that instructs to ignore rules below a certain priority.
376 class WebRequestIgnoreRulesAction
: public WebRequestAction
{
378 explicit WebRequestIgnoreRulesAction(int minimum_priority
,
379 const std::string
& ignore_tag
);
381 // Implementation of WebRequestAction:
382 bool Equals(const WebRequestAction
* other
) const override
;
383 std::string
GetName() const override
;
384 LinkedPtrEventResponseDelta
CreateDelta(
385 const WebRequestData
& request_data
,
386 const std::string
& extension_id
,
387 const base::Time
& extension_install_time
) const override
;
388 const std::string
& ignore_tag() const { return ignore_tag_
; }
391 ~WebRequestIgnoreRulesAction() override
;
393 // Rules are ignored if they have a tag matching |ignore_tag_| and
394 // |ignore_tag_| is non-empty.
395 std::string ignore_tag_
;
396 DISALLOW_COPY_AND_ASSIGN(WebRequestIgnoreRulesAction
);
399 // Action that instructs to modify (add, edit, remove) a request cookie.
400 class WebRequestRequestCookieAction
: public WebRequestAction
{
402 typedef extension_web_request_api_helpers::RequestCookieModification
403 RequestCookieModification
;
405 explicit WebRequestRequestCookieAction(
406 linked_ptr
<RequestCookieModification
> request_cookie_modification
);
408 // Implementation of WebRequestAction:
409 bool Equals(const WebRequestAction
* other
) const override
;
410 std::string
GetName() const override
;
411 LinkedPtrEventResponseDelta
CreateDelta(
412 const WebRequestData
& request_data
,
413 const std::string
& extension_id
,
414 const base::Time
& extension_install_time
) const override
;
417 ~WebRequestRequestCookieAction() override
;
419 linked_ptr
<RequestCookieModification
> request_cookie_modification_
;
420 DISALLOW_COPY_AND_ASSIGN(WebRequestRequestCookieAction
);
423 // Action that instructs to modify (add, edit, remove) a response cookie.
424 class WebRequestResponseCookieAction
: public WebRequestAction
{
426 typedef extension_web_request_api_helpers::ResponseCookieModification
427 ResponseCookieModification
;
429 explicit WebRequestResponseCookieAction(
430 linked_ptr
<ResponseCookieModification
> response_cookie_modification
);
432 // Implementation of WebRequestAction:
433 bool Equals(const WebRequestAction
* other
) const override
;
434 std::string
GetName() const override
;
435 LinkedPtrEventResponseDelta
CreateDelta(
436 const WebRequestData
& request_data
,
437 const std::string
& extension_id
,
438 const base::Time
& extension_install_time
) const override
;
441 ~WebRequestResponseCookieAction() override
;
443 linked_ptr
<ResponseCookieModification
> response_cookie_modification_
;
444 DISALLOW_COPY_AND_ASSIGN(WebRequestResponseCookieAction
);
447 // Action that triggers the chrome.declarativeWebRequest.onMessage event in
448 // the background/event/... pages of the extension.
449 class WebRequestSendMessageToExtensionAction
: public WebRequestAction
{
451 explicit WebRequestSendMessageToExtensionAction(const std::string
& message
);
453 // Implementation of WebRequestAction:
454 bool Equals(const WebRequestAction
* other
) const override
;
455 std::string
GetName() const override
;
456 LinkedPtrEventResponseDelta
CreateDelta(
457 const WebRequestData
& request_data
,
458 const std::string
& extension_id
,
459 const base::Time
& extension_install_time
) const override
;
462 ~WebRequestSendMessageToExtensionAction() override
;
464 std::string message_
;
465 DISALLOW_COPY_AND_ASSIGN(WebRequestSendMessageToExtensionAction
);
468 } // namespace extensions
470 #endif // EXTENSIONS_BROWSER_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_ACTION_H_