- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / declarative_webrequest / webrequest_rules_registry.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 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.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 RulesRegistry {
75  public:
76   // |cache_delegate| can be NULL. In that case it constructs the registry with
77   // storage functionality suspended.
78   WebRequestRulesRegistry(Profile* profile,
79                           RulesCacheDelegate* cache_delegate);
80
81   // TODO(battre): This will become an implementation detail, because we need
82   // a way to also execute the actions of the rules.
83   std::set<const WebRequestRule*> GetMatches(
84       const WebRequestData& request_data_without_ids) const;
85
86   // Returns which modifications should be executed on the network request
87   // according to the rules registered in this registry.
88   std::list<LinkedPtrEventResponseDelta> CreateDeltas(
89       const ExtensionInfoMap* extension_info_map,
90       const WebRequestData& request_data,
91       bool crosses_incognito);
92
93   // Implementation of RulesRegistry:
94   virtual std::string AddRulesImpl(
95       const std::string& extension_id,
96       const std::vector<linked_ptr<RulesRegistry::Rule> >& rules) OVERRIDE;
97   virtual std::string RemoveRulesImpl(
98       const std::string& extension_id,
99       const std::vector<std::string>& rule_identifiers) OVERRIDE;
100   virtual std::string RemoveAllRulesImpl(
101       const std::string& extension_id) OVERRIDE;
102
103   // Returns true if this object retains no allocated data. Only for debugging.
104   bool IsEmpty() const;
105
106  protected:
107   virtual ~WebRequestRulesRegistry();
108
109   // Virtual for testing:
110   virtual base::Time GetExtensionInstallationTime(
111       const std::string& extension_id) const;
112   virtual void ClearCacheOnNavigation();
113
114   void SetExtensionInfoMapForTesting(
115       scoped_refptr<ExtensionInfoMap> extension_info_map) {
116     extension_info_map_ = extension_info_map;
117   }
118
119   const std::set<const WebRequestRule*>&
120   rules_with_untriggered_conditions_for_test() const {
121     return rules_with_untriggered_conditions_;
122   }
123
124  private:
125   FRIEND_TEST_ALL_PREFIXES(WebRequestRulesRegistrySimpleTest, StageChecker);
126   FRIEND_TEST_ALL_PREFIXES(WebRequestRulesRegistrySimpleTest,
127                            HostPermissionsChecker);
128
129   typedef std::map<URLMatcherConditionSet::ID, WebRequestRule*> RuleTriggers;
130   typedef std::map<WebRequestRule::RuleId, linked_ptr<WebRequestRule> >
131       RulesMap;
132   typedef std::set<URLMatcherConditionSet::ID> URLMatches;
133   typedef std::set<const WebRequestRule*> RuleSet;
134
135   // This bundles all consistency checkers. Returns true in case of consistency
136   // and MUST set |error| otherwise.
137   static bool Checker(const Extension* extension,
138                       const WebRequestConditionSet* conditions,
139                       const WebRequestActionSet* actions,
140                       std::string* error);
141
142   // Check that the |extension| has host permissions for all URLs if actions
143   // requiring them are present.
144   static bool HostPermissionsChecker(const Extension* extension,
145                                      const WebRequestActionSet* actions,
146                                      std::string* error);
147
148   // Check that every action is applicable in the same request stage as at
149   // least one condition.
150   static bool StageChecker(const WebRequestConditionSet* conditions,
151                            const WebRequestActionSet* actions,
152                            std::string* error);
153
154   // Helper for RemoveRulesImpl and RemoveAllRulesImpl. Call this before
155   // deleting |rule| from one of the maps in |webrequest_rules_|. It will erase
156   // the rule from |rule_triggers_| and |rules_with_untriggered_conditions_|,
157   // and add every of the rule's URLMatcherConditionSet to
158   // |remove_from_url_matcher|, so that the caller can remove them from the
159   // matcher later.
160   void CleanUpAfterRule(
161       const WebRequestRule* rule,
162       std::vector<URLMatcherConditionSet::ID>* remove_from_url_matcher);
163
164   // This is a helper function to GetMatches. Rules triggered by |url_matches|
165   // get added to |result| if one of their conditions is fulfilled.
166   // |request_data| gets passed to IsFulfilled of the rules' condition sets.
167   void AddTriggeredRules(const URLMatches& url_matches,
168                          const WebRequestCondition::MatchData& request_data,
169                          RuleSet* result) const;
170
171   // Map that tells us which WebRequestRule may match under the condition that
172   // the URLMatcherConditionSet::ID was returned by the |url_matcher_|.
173   RuleTriggers rule_triggers_;
174
175   // These rules contain condition sets with conditions without URL attributes.
176   // Such conditions are not triggered by URL matcher, so we need to test them
177   // separately.
178   std::set<const WebRequestRule*> rules_with_untriggered_conditions_;
179
180   std::map<WebRequestRule::ExtensionId, RulesMap> webrequest_rules_;
181
182   URLMatcher url_matcher_;
183
184   void* profile_id_;
185   scoped_refptr<ExtensionInfoMap> extension_info_map_;
186
187   DISALLOW_COPY_AND_ASSIGN(WebRequestRulesRegistry);
188 };
189
190 }  // namespace extensions
191
192 #endif  // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_RULES_REGISTRY_H_