Home | History | Annotate | Download | only in declarative_webrequest
      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 CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_ACTION_H_
      6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_ACTION_H_
      7 
      8 #include <list>
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "base/compiler_specific.h"
     13 #include "base/memory/linked_ptr.h"
     14 #include "base/memory/ref_counted.h"
     15 #include "chrome/browser/extensions/api/declarative/declarative_rule.h"
     16 #include "chrome/browser/extensions/api/declarative_webrequest/request_stage.h"
     17 #include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h"
     18 #include "chrome/common/extensions/api/events.h"
     19 #include "url/gurl.h"
     20 
     21 class WebRequestPermission;
     22 
     23 namespace base {
     24 class DictionaryValue;
     25 class Time;
     26 class Value;
     27 }
     28 
     29 namespace extension_web_request_api_helpers {
     30 struct EventResponseDelta;
     31 }
     32 
     33 namespace extensions {
     34 class Extension;
     35 class InfoMap;
     36 struct WebRequestData;
     37 }
     38 
     39 namespace net {
     40 class URLRequest;
     41 }
     42 
     43 namespace re2 {
     44 class RE2;
     45 }
     46 
     47 namespace extensions {
     48 
     49 typedef linked_ptr<extension_web_request_api_helpers::EventResponseDelta>
     50     LinkedPtrEventResponseDelta;
     51 
     52 // Base class for all WebRequestActions of the declarative Web Request API.
     53 class WebRequestAction : public base::RefCounted<WebRequestAction> {
     54  public:
     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.
     58   enum Type {
     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,
     68     ACTION_IGNORE_RULES,
     69     ACTION_MODIFY_REQUEST_COOKIE,
     70     ACTION_MODIFY_RESPONSE_COOKIE,
     71     ACTION_SEND_MESSAGE_TO_EXTENSION,
     72   };
     73 
     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
     80                        // of the request.
     81   };
     82 
     83   // Information necessary to decide how to apply a WebRequestAction
     84   // inside a matching rule.
     85   struct ApplyInfo {
     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;
     92   };
     93 
     94   int stages() const {
     95     return stages_;
     96   }
     97 
     98   Type type() const {
     99     return type_;
    100   }
    101 
    102   // Compares the Type of two WebRequestActions, needs to be overridden for
    103   // parameterized types.
    104   virtual bool Equals(const WebRequestAction* other) const;
    105 
    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;
    109 
    110   int minimum_priority() const {
    111     return minimum_priority_;
    112   }
    113 
    114   HostPermissionsStrategy host_permissions_strategy() const {
    115     return host_permissions_strategy_;
    116   }
    117 
    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;
    129 
    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       const Extension* extension,
    138       const base::Value& json_action,
    139       std::string* error,
    140       bool* bad_message);
    141 
    142   // Returns a description of the modification to the request caused by
    143   // this action.
    144   virtual LinkedPtrEventResponseDelta CreateDelta(
    145       const WebRequestData& request_data,
    146       const std::string& extension_id,
    147       const base::Time& extension_install_time) const = 0;
    148 
    149   // Applies this action to a request, recording the results into
    150   // apply_info.deltas.
    151   void Apply(const std::string& extension_id,
    152              base::Time extension_install_time,
    153              ApplyInfo* apply_info) const;
    154 
    155  protected:
    156   friend class base::RefCounted<WebRequestAction>;
    157   virtual ~WebRequestAction();
    158   WebRequestAction(int stages,
    159                    Type type,
    160                    int minimum_priority,
    161                    HostPermissionsStrategy strategy);
    162 
    163  private:
    164   // A bit vector representing a set of extensions::RequestStage during which
    165   // the condition can be tested.
    166   const int stages_;
    167 
    168   const Type type_;
    169 
    170   // The minimum priority of rules that may be evaluated after the rule
    171   // containing this action.
    172   const int minimum_priority_;
    173 
    174   // Defaults to STRATEGY_DEFAULT.
    175   const HostPermissionsStrategy host_permissions_strategy_;
    176 };
    177 
    178 typedef DeclarativeActionSet<WebRequestAction> WebRequestActionSet;
    179 
    180 //
    181 // The following are concrete actions.
    182 //
    183 
    184 // Action that instructs to cancel a network request.
    185 class WebRequestCancelAction : public WebRequestAction {
    186  public:
    187   WebRequestCancelAction();
    188 
    189   // Implementation of WebRequestAction:
    190   virtual std::string GetName() const OVERRIDE;
    191   virtual LinkedPtrEventResponseDelta CreateDelta(
    192       const WebRequestData& request_data,
    193       const std::string& extension_id,
    194       const base::Time& extension_install_time) const OVERRIDE;
    195 
    196  private:
    197   virtual ~WebRequestCancelAction();
    198   DISALLOW_COPY_AND_ASSIGN(WebRequestCancelAction);
    199 };
    200 
    201 // Action that instructs to redirect a network request.
    202 class WebRequestRedirectAction : public WebRequestAction {
    203  public:
    204   explicit WebRequestRedirectAction(const GURL& redirect_url);
    205 
    206   // Implementation of WebRequestAction:
    207   virtual bool Equals(const WebRequestAction* other) const OVERRIDE;
    208   virtual std::string GetName() const OVERRIDE;
    209   virtual LinkedPtrEventResponseDelta CreateDelta(
    210       const WebRequestData& request_data,
    211       const std::string& extension_id,
    212       const base::Time& extension_install_time) const OVERRIDE;
    213 
    214  private:
    215   virtual ~WebRequestRedirectAction();
    216 
    217   GURL redirect_url_;  // Target to which the request shall be redirected.
    218 
    219   DISALLOW_COPY_AND_ASSIGN(WebRequestRedirectAction);
    220 };
    221 
    222 // Action that instructs to redirect a network request to a transparent image.
    223 class WebRequestRedirectToTransparentImageAction : public WebRequestAction {
    224  public:
    225   WebRequestRedirectToTransparentImageAction();
    226 
    227   // Implementation of WebRequestAction:
    228   virtual std::string GetName() const OVERRIDE;
    229   virtual LinkedPtrEventResponseDelta CreateDelta(
    230       const WebRequestData& request_data,
    231       const std::string& extension_id,
    232       const base::Time& extension_install_time) const OVERRIDE;
    233 
    234  private:
    235   virtual ~WebRequestRedirectToTransparentImageAction();
    236   DISALLOW_COPY_AND_ASSIGN(WebRequestRedirectToTransparentImageAction);
    237 };
    238 
    239 
    240 // Action that instructs to redirect a network request to an empty document.
    241 class WebRequestRedirectToEmptyDocumentAction : public WebRequestAction {
    242  public:
    243   WebRequestRedirectToEmptyDocumentAction();
    244 
    245   // Implementation of WebRequestAction:
    246   virtual std::string GetName() const OVERRIDE;
    247   virtual LinkedPtrEventResponseDelta CreateDelta(
    248       const WebRequestData& request_data,
    249       const std::string& extension_id,
    250       const base::Time& extension_install_time) const OVERRIDE;
    251 
    252  private:
    253   virtual ~WebRequestRedirectToEmptyDocumentAction();
    254   DISALLOW_COPY_AND_ASSIGN(WebRequestRedirectToEmptyDocumentAction);
    255 };
    256 
    257 // Action that instructs to redirect a network request.
    258 class WebRequestRedirectByRegExAction : public WebRequestAction {
    259  public:
    260   // The |to_pattern| has to be passed in RE2 syntax with the exception that
    261   // capture groups are referenced in Perl style ($1, $2, ...).
    262   explicit WebRequestRedirectByRegExAction(scoped_ptr<re2::RE2> from_pattern,
    263                                            const std::string& to_pattern);
    264 
    265   // Conversion of capture group styles between Perl style ($1, $2, ...) and
    266   // RE2 (\1, \2, ...).
    267   static std::string PerlToRe2Style(const std::string& perl);
    268 
    269   // Implementation of WebRequestAction:
    270   virtual bool Equals(const WebRequestAction* other) const OVERRIDE;
    271   virtual std::string GetName() const OVERRIDE;
    272   virtual LinkedPtrEventResponseDelta CreateDelta(
    273       const WebRequestData& request_data,
    274       const std::string& extension_id,
    275       const base::Time& extension_install_time) const OVERRIDE;
    276 
    277  private:
    278   virtual ~WebRequestRedirectByRegExAction();
    279 
    280   scoped_ptr<re2::RE2> from_pattern_;
    281   std::string to_pattern_;
    282 
    283   DISALLOW_COPY_AND_ASSIGN(WebRequestRedirectByRegExAction);
    284 };
    285 
    286 // Action that instructs to set a request header.
    287 class WebRequestSetRequestHeaderAction : public WebRequestAction {
    288  public:
    289   WebRequestSetRequestHeaderAction(const std::string& name,
    290                                    const std::string& value);
    291 
    292   // Implementation of WebRequestAction:
    293   virtual bool Equals(const WebRequestAction* other) const OVERRIDE;
    294   virtual std::string GetName() const OVERRIDE;
    295   virtual LinkedPtrEventResponseDelta CreateDelta(
    296       const WebRequestData& request_data,
    297       const std::string& extension_id,
    298       const base::Time& extension_install_time) const OVERRIDE;
    299 
    300  private:
    301   virtual ~WebRequestSetRequestHeaderAction();
    302 
    303   std::string name_;
    304   std::string value_;
    305   DISALLOW_COPY_AND_ASSIGN(WebRequestSetRequestHeaderAction);
    306 };
    307 
    308 // Action that instructs to remove a request header.
    309 class WebRequestRemoveRequestHeaderAction : public WebRequestAction {
    310  public:
    311   explicit WebRequestRemoveRequestHeaderAction(const std::string& name);
    312 
    313   // Implementation of WebRequestAction:
    314   virtual bool Equals(const WebRequestAction* other) const OVERRIDE;
    315   virtual std::string GetName() const OVERRIDE;
    316   virtual LinkedPtrEventResponseDelta CreateDelta(
    317       const WebRequestData& request_data,
    318       const std::string& extension_id,
    319       const base::Time& extension_install_time) const OVERRIDE;
    320 
    321  private:
    322   virtual ~WebRequestRemoveRequestHeaderAction();
    323 
    324   std::string name_;
    325   DISALLOW_COPY_AND_ASSIGN(WebRequestRemoveRequestHeaderAction);
    326 };
    327 
    328 // Action that instructs to add a response header.
    329 class WebRequestAddResponseHeaderAction : public WebRequestAction {
    330  public:
    331   WebRequestAddResponseHeaderAction(const std::string& name,
    332                                     const std::string& value);
    333 
    334   // Implementation of WebRequestAction:
    335   virtual bool Equals(const WebRequestAction* other) const OVERRIDE;
    336   virtual std::string GetName() const OVERRIDE;
    337   virtual LinkedPtrEventResponseDelta CreateDelta(
    338       const WebRequestData& request_data,
    339       const std::string& extension_id,
    340       const base::Time& extension_install_time) const OVERRIDE;
    341 
    342  private:
    343   virtual ~WebRequestAddResponseHeaderAction();
    344 
    345   std::string name_;
    346   std::string value_;
    347   DISALLOW_COPY_AND_ASSIGN(WebRequestAddResponseHeaderAction);
    348 };
    349 
    350 // Action that instructs to remove a response header.
    351 class WebRequestRemoveResponseHeaderAction : public WebRequestAction {
    352  public:
    353   explicit WebRequestRemoveResponseHeaderAction(const std::string& name,
    354                                                 const std::string& value,
    355                                                 bool has_value);
    356 
    357   // Implementation of WebRequestAction:
    358   virtual bool Equals(const WebRequestAction* other) const OVERRIDE;
    359   virtual std::string GetName() const OVERRIDE;
    360   virtual LinkedPtrEventResponseDelta CreateDelta(
    361       const WebRequestData& request_data,
    362       const std::string& extension_id,
    363       const base::Time& extension_install_time) const OVERRIDE;
    364 
    365  private:
    366   virtual ~WebRequestRemoveResponseHeaderAction();
    367 
    368   std::string name_;
    369   std::string value_;
    370   bool has_value_;
    371   DISALLOW_COPY_AND_ASSIGN(WebRequestRemoveResponseHeaderAction);
    372 };
    373 
    374 // Action that instructs to ignore rules below a certain priority.
    375 class WebRequestIgnoreRulesAction : public WebRequestAction {
    376  public:
    377   explicit WebRequestIgnoreRulesAction(int minimum_priority,
    378                                        const std::string& ignore_tag);
    379 
    380   // Implementation of WebRequestAction:
    381   virtual bool Equals(const WebRequestAction* other) const OVERRIDE;
    382   virtual std::string GetName() const OVERRIDE;
    383   virtual LinkedPtrEventResponseDelta CreateDelta(
    384       const WebRequestData& request_data,
    385       const std::string& extension_id,
    386       const base::Time& extension_install_time) const OVERRIDE;
    387   const std::string& ignore_tag() const { return ignore_tag_; }
    388 
    389  private:
    390   virtual ~WebRequestIgnoreRulesAction();
    391 
    392   // Rules are ignored if they have a tag matching |ignore_tag_| and
    393   // |ignore_tag_| is non-empty.
    394   std::string ignore_tag_;
    395   DISALLOW_COPY_AND_ASSIGN(WebRequestIgnoreRulesAction);
    396 };
    397 
    398 // Action that instructs to modify (add, edit, remove) a request cookie.
    399 class WebRequestRequestCookieAction : public WebRequestAction {
    400  public:
    401   typedef extension_web_request_api_helpers::RequestCookieModification
    402       RequestCookieModification;
    403 
    404   explicit WebRequestRequestCookieAction(
    405       linked_ptr<RequestCookieModification> request_cookie_modification);
    406 
    407   // Implementation of WebRequestAction:
    408   virtual bool Equals(const WebRequestAction* other) const OVERRIDE;
    409   virtual std::string GetName() const OVERRIDE;
    410   virtual LinkedPtrEventResponseDelta CreateDelta(
    411       const WebRequestData& request_data,
    412       const std::string& extension_id,
    413       const base::Time& extension_install_time) const OVERRIDE;
    414 
    415  private:
    416   virtual ~WebRequestRequestCookieAction();
    417 
    418   linked_ptr<RequestCookieModification> request_cookie_modification_;
    419   DISALLOW_COPY_AND_ASSIGN(WebRequestRequestCookieAction);
    420 };
    421 
    422 // Action that instructs to modify (add, edit, remove) a response cookie.
    423 class WebRequestResponseCookieAction : public WebRequestAction {
    424  public:
    425   typedef extension_web_request_api_helpers::ResponseCookieModification
    426       ResponseCookieModification;
    427 
    428   explicit WebRequestResponseCookieAction(
    429       linked_ptr<ResponseCookieModification> response_cookie_modification);
    430 
    431   // Implementation of WebRequestAction:
    432   virtual bool Equals(const WebRequestAction* other) const OVERRIDE;
    433   virtual std::string GetName() const OVERRIDE;
    434   virtual LinkedPtrEventResponseDelta CreateDelta(
    435       const WebRequestData& request_data,
    436       const std::string& extension_id,
    437       const base::Time& extension_install_time) const OVERRIDE;
    438 
    439  private:
    440   virtual ~WebRequestResponseCookieAction();
    441 
    442   linked_ptr<ResponseCookieModification> response_cookie_modification_;
    443   DISALLOW_COPY_AND_ASSIGN(WebRequestResponseCookieAction);
    444 };
    445 
    446 // Action that triggers the chrome.declarativeWebRequest.onMessage event in
    447 // the background/event/... pages of the extension.
    448 class WebRequestSendMessageToExtensionAction : public WebRequestAction {
    449  public:
    450   explicit WebRequestSendMessageToExtensionAction(const std::string& message);
    451 
    452   // Implementation of WebRequestAction:
    453   virtual bool Equals(const WebRequestAction* other) const OVERRIDE;
    454   virtual std::string GetName() const OVERRIDE;
    455   virtual LinkedPtrEventResponseDelta CreateDelta(
    456       const WebRequestData& request_data,
    457       const std::string& extension_id,
    458       const base::Time& extension_install_time) const OVERRIDE;
    459 
    460  private:
    461   virtual ~WebRequestSendMessageToExtensionAction();
    462 
    463   std::string message_;
    464   DISALLOW_COPY_AND_ASSIGN(WebRequestSendMessageToExtensionAction);
    465 };
    466 
    467 }  // namespace extensions
    468 
    469 #endif  // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_ACTION_H_
    470