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_RULES_REGISTRY_H_
      6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_RULES_REGISTRY_H_
      7 
      8 #include <list>
      9 #include <map>
     10 #include <set>
     11 #include <string>
     12 #include <vector>
     13 
     14 #include "base/gtest_prod_util.h"
     15 #include "base/memory/linked_ptr.h"
     16 #include "base/memory/ref_counted.h"
     17 #include "base/memory/scoped_ptr.h"
     18 #include "base/time/time.h"
     19 #include "chrome/browser/extensions/api/declarative/declarative_rule.h"
     20 #include "chrome/browser/extensions/api/declarative/rules_registry_with_cache.h"
     21 #include "chrome/browser/extensions/api/declarative_webrequest/request_stage.h"
     22 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_action.h"
     23 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condition.h"
     24 #include "chrome/browser/extensions/extension_info_map.h"
     25 #include "extensions/common/matcher/url_matcher.h"
     26 
     27 class Profile;
     28 class WebRequestPermissions;
     29 
     30 namespace extension_web_request_api_helpers {
     31 struct EventResponseDelta;
     32 }
     33 
     34 namespace net {
     35 class URLRequest;
     36 }
     37 
     38 namespace extensions {
     39 
     40 class RulesRegistryService;
     41 
     42 typedef linked_ptr<extension_web_request_api_helpers::EventResponseDelta>
     43     LinkedPtrEventResponseDelta;
     44 typedef DeclarativeRule<WebRequestCondition, WebRequestAction> WebRequestRule;
     45 
     46 // The WebRequestRulesRegistry is responsible for managing
     47 // the internal representation of rules for the Declarative Web Request API.
     48 //
     49 // Here is the high level overview of this functionality:
     50 //
     51 // RulesRegistry::Rule consists of Conditions and Actions, these are
     52 // represented as a WebRequestRule with WebRequestConditions and
     53 // WebRequestRuleActions.
     54 //
     55 // WebRequestConditions represent JSON dictionaries as the following:
     56 // {
     57 //   'instanceType': 'URLMatcher',
     58 //   'host_suffix': 'example.com',
     59 //   'path_prefix': '/query',
     60 //   'scheme': 'http'
     61 // }
     62 //
     63 // The evaluation of URL related condition attributes (host_suffix, path_prefix)
     64 // is delegated to a URLMatcher, because this is capable of evaluating many
     65 // of such URL related condition attributes in parallel.
     66 //
     67 // For this, the URLRequestCondition has a URLMatcherConditionSet, which
     68 // represents the {'host_suffix': 'example.com', 'path_prefix': '/query'} part.
     69 // We will then ask the URLMatcher, whether a given URL
     70 // "http://www.example.com/query/" has any matches, and the URLMatcher
     71 // will respond with the URLMatcherConditionSet::ID. We can map this
     72 // to the WebRequestRule and check whether also the other conditions (in this
     73 // example 'scheme': 'http') are fulfilled.
     74 class WebRequestRulesRegistry : public RulesRegistryWithCache {
     75  public:
     76   // For testing, |ui_part| can be NULL. In that case it constructs the
     77   // registry with storage functionality suspended.
     78   WebRequestRulesRegistry(
     79       Profile* profile,
     80       scoped_ptr<RulesRegistryWithCache::RuleStorageOnUI>* ui_part);
     81 
     82   // TODO(battre): This will become an implementation detail, because we need
     83   // a way to also execute the actions of the rules.
     84   std::set<const WebRequestRule*> GetMatches(
     85       const WebRequestData& request_data_without_ids) const;
     86 
     87   // Returns which modifications should be executed on the network request
     88   // according to the rules registered in this registry.
     89   std::list<LinkedPtrEventResponseDelta> CreateDeltas(
     90       const ExtensionInfoMap* extension_info_map,
     91       const WebRequestData& request_data,
     92       bool crosses_incognito);
     93 
     94   // Implementation of RulesRegistryWithCache:
     95   virtual std::string AddRulesImpl(
     96       const std::string& extension_id,
     97       const std::vector<linked_ptr<RulesRegistry::Rule> >& rules) OVERRIDE;
     98   virtual std::string RemoveRulesImpl(
     99       const std::string& extension_id,
    100       const std::vector<std::string>& rule_identifiers) OVERRIDE;
    101   virtual std::string RemoveAllRulesImpl(
    102       const std::string& extension_id) OVERRIDE;
    103 
    104   // Returns true if this object retains no allocated data. Only for debugging.
    105   bool IsEmpty() const;
    106 
    107  protected:
    108   virtual ~WebRequestRulesRegistry();
    109 
    110   // Virtual for testing:
    111   virtual base::Time GetExtensionInstallationTime(
    112       const std::string& extension_id) const;
    113   virtual void ClearCacheOnNavigation();
    114 
    115   void SetExtensionInfoMapForTesting(
    116       scoped_refptr<ExtensionInfoMap> extension_info_map) {
    117     extension_info_map_ = extension_info_map;
    118   }
    119 
    120   const std::set<const WebRequestRule*>&
    121   rules_with_untriggered_conditions_for_test() const {
    122     return rules_with_untriggered_conditions_;
    123   }
    124 
    125  private:
    126   FRIEND_TEST_ALL_PREFIXES(WebRequestRulesRegistrySimpleTest, StageChecker);
    127   FRIEND_TEST_ALL_PREFIXES(WebRequestRulesRegistrySimpleTest,
    128                            HostPermissionsChecker);
    129 
    130   typedef std::map<URLMatcherConditionSet::ID, WebRequestRule*> RuleTriggers;
    131   typedef std::map<WebRequestRule::RuleId, linked_ptr<WebRequestRule> >
    132       RulesMap;
    133   typedef std::set<URLMatcherConditionSet::ID> URLMatches;
    134   typedef std::set<const WebRequestRule*> RuleSet;
    135 
    136   // This bundles all consistency checkers. Returns true in case of consistency
    137   // and MUST set |error| otherwise.
    138   static bool Checker(const Extension* extension,
    139                       const WebRequestConditionSet* conditions,
    140                       const WebRequestActionSet* actions,
    141                       std::string* error);
    142 
    143   // Check that the |extension| has host permissions for all URLs if actions
    144   // requiring them are present.
    145   static bool HostPermissionsChecker(const Extension* extension,
    146                                      const WebRequestActionSet* actions,
    147                                      std::string* error);
    148 
    149   // Check that every action is applicable in the same request stage as at
    150   // least one condition.
    151   static bool StageChecker(const WebRequestConditionSet* conditions,
    152                            const WebRequestActionSet* actions,
    153                            std::string* error);
    154 
    155   // Helper for RemoveRulesImpl and RemoveAllRulesImpl. Call this before
    156   // deleting |rule| from one of the maps in |webrequest_rules_|. It will erase
    157   // the rule from |rule_triggers_| and |rules_with_untriggered_conditions_|,
    158   // and add every of the rule's URLMatcherConditionSet to
    159   // |remove_from_url_matcher|, so that the caller can remove them from the
    160   // matcher later.
    161   void CleanUpAfterRule(
    162       const WebRequestRule* rule,
    163       std::vector<URLMatcherConditionSet::ID>* remove_from_url_matcher);
    164 
    165   // This is a helper function to GetMatches. Rules triggered by |url_matches|
    166   // get added to |result| if one of their conditions is fulfilled.
    167   // |request_data| gets passed to IsFulfilled of the rules' condition sets.
    168   void AddTriggeredRules(const URLMatches& url_matches,
    169                          const WebRequestCondition::MatchData& request_data,
    170                          RuleSet* result) const;
    171 
    172   // Map that tells us which WebRequestRule may match under the condition that
    173   // the URLMatcherConditionSet::ID was returned by the |url_matcher_|.
    174   RuleTriggers rule_triggers_;
    175 
    176   // These rules contain condition sets with conditions without URL attributes.
    177   // Such conditions are not triggered by URL matcher, so we need to test them
    178   // separately.
    179   std::set<const WebRequestRule*> rules_with_untriggered_conditions_;
    180 
    181   std::map<WebRequestRule::ExtensionId, RulesMap> webrequest_rules_;
    182 
    183   URLMatcher url_matcher_;
    184 
    185   void* profile_id_;
    186   scoped_refptr<ExtensionInfoMap> extension_info_map_;
    187 
    188   DISALLOW_COPY_AND_ASSIGN(WebRequestRulesRegistry);
    189 };
    190 
    191 }  // namespace extensions
    192 
    193 #endif  // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_RULES_REGISTRY_H_
    194