OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_RULES_RE
GISTRY_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_RULES_RE
GISTRY_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_RULES_RE
GISTRY_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_RULES_RE
GISTRY_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
9 #include <map> | 9 #include <map> |
10 #include <set> | 10 #include <set> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/time.h" | 13 #include "base/time.h" |
14 #include "base/memory/linked_ptr.h" | 14 #include "base/memory/linked_ptr.h" |
15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 16 #include "chrome/browser/extensions/api/declarative/declarative_rule.h" |
16 #include "chrome/browser/extensions/api/declarative/rules_registry_with_cache.h" | 17 #include "chrome/browser/extensions/api/declarative/rules_registry_with_cache.h" |
17 #include "chrome/browser/extensions/api/declarative_webrequest/request_stage.h" | 18 #include "chrome/browser/extensions/api/declarative_webrequest/request_stage.h" |
18 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rule.h
" | 19 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_action
.h" |
| 20 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit
ion.h" |
19 #include "chrome/browser/extensions/extension_info_map.h" | 21 #include "chrome/browser/extensions/extension_info_map.h" |
20 #include "chrome/common/extensions/matcher/url_matcher.h" | 22 #include "chrome/common/extensions/matcher/url_matcher.h" |
21 | 23 |
22 class Profile; | 24 class Profile; |
23 class WebRequestPermissions; | 25 class WebRequestPermissions; |
24 | 26 |
25 namespace extension_web_request_api_helpers { | 27 namespace extension_web_request_api_helpers { |
26 struct EventResponseDelta; | 28 struct EventResponseDelta; |
27 } | 29 } |
28 | 30 |
29 namespace net { | 31 namespace net { |
30 class URLRequest; | 32 class URLRequest; |
31 } | 33 } |
32 | 34 |
33 namespace extensions { | 35 namespace extensions { |
34 | 36 |
35 class RulesRegistryService; | 37 class RulesRegistryService; |
36 | 38 |
| 39 typedef linked_ptr<extension_web_request_api_helpers::EventResponseDelta> |
| 40 LinkedPtrEventResponseDelta; |
| 41 typedef DeclarativeRule<WebRequestCondition, WebRequestAction> WebRequestRule; |
| 42 |
37 // The WebRequestRulesRegistry is responsible for managing | 43 // The WebRequestRulesRegistry is responsible for managing |
38 // the internal representation of rules for the Declarative Web Request API. | 44 // the internal representation of rules for the Declarative Web Request API. |
39 // | 45 // |
40 // Here is the high level overview of this functionality: | 46 // Here is the high level overview of this functionality: |
41 // | 47 // |
42 // RulesRegistry::Rule consists of Conditions and Actions, these are | 48 // RulesRegistry::Rule consists of Conditions and Actions, these are |
43 // represented as a WebRequestRule with WebRequestConditions and | 49 // represented as a WebRequestRule with WebRequestConditions and |
44 // WebRequestRuleActions. | 50 // WebRequestRuleActions. |
45 // | 51 // |
46 // WebRequestConditions represent JSON dictionaries as the following: | 52 // WebRequestConditions represent JSON dictionaries as the following: |
(...skipping 15 matching lines...) Expand all Loading... |
62 // will respond with the URLMatcherConditionSet::ID. We can map this | 68 // will respond with the URLMatcherConditionSet::ID. We can map this |
63 // to the WebRequestRule and check whether also the other conditions (in this | 69 // to the WebRequestRule and check whether also the other conditions (in this |
64 // example 'scheme': 'http') are fulfilled. | 70 // example 'scheme': 'http') are fulfilled. |
65 class WebRequestRulesRegistry : public RulesRegistryWithCache { | 71 class WebRequestRulesRegistry : public RulesRegistryWithCache { |
66 public: | 72 public: |
67 WebRequestRulesRegistry(Profile* profile, Delegate* delegate); | 73 WebRequestRulesRegistry(Profile* profile, Delegate* delegate); |
68 | 74 |
69 // TODO(battre): This will become an implementation detail, because we need | 75 // TODO(battre): This will become an implementation detail, because we need |
70 // a way to also execute the actions of the rules. | 76 // a way to also execute the actions of the rules. |
71 std::set<WebRequestRule::GlobalRuleId> GetMatches( | 77 std::set<WebRequestRule::GlobalRuleId> GetMatches( |
72 const WebRequestRule::RequestData& request_data); | 78 const DeclarativeWebRequestData& request_data); |
73 | 79 |
74 // Returns which modifications should be executed on the network request | 80 // Returns which modifications should be executed on the network request |
75 // according to the rules registered in this registry. | 81 // according to the rules registered in this registry. |
76 std::list<LinkedPtrEventResponseDelta> CreateDeltas( | 82 std::list<LinkedPtrEventResponseDelta> CreateDeltas( |
77 const ExtensionInfoMap* extension_info_map, | 83 const ExtensionInfoMap* extension_info_map, |
78 const WebRequestRule::RequestData& request_data, | 84 const DeclarativeWebRequestData& request_data, |
79 bool crosses_incognito); | 85 bool crosses_incognito); |
80 | 86 |
81 // Implementation of RulesRegistryWithCache: | 87 // Implementation of RulesRegistryWithCache: |
82 virtual std::string AddRulesImpl( | 88 virtual std::string AddRulesImpl( |
83 const std::string& extension_id, | 89 const std::string& extension_id, |
84 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules) OVERRIDE; | 90 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules) OVERRIDE; |
85 virtual std::string RemoveRulesImpl( | 91 virtual std::string RemoveRulesImpl( |
86 const std::string& extension_id, | 92 const std::string& extension_id, |
87 const std::vector<std::string>& rule_identifiers) OVERRIDE; | 93 const std::vector<std::string>& rule_identifiers) OVERRIDE; |
88 virtual std::string RemoveAllRulesImpl( | 94 virtual std::string RemoveAllRulesImpl( |
89 const std::string& extension_id) OVERRIDE; | 95 const std::string& extension_id) OVERRIDE; |
90 virtual content::BrowserThread::ID GetOwnerThread() const OVERRIDE; | 96 virtual content::BrowserThread::ID GetOwnerThread() const OVERRIDE; |
91 | 97 |
92 // Returns true if this object retains no allocated data. Only for debugging. | 98 // Returns true if this object retains no allocated data. Only for debugging. |
93 bool IsEmpty() const; | 99 bool IsEmpty() const; |
94 | 100 |
95 protected: | 101 protected: |
96 virtual ~WebRequestRulesRegistry(); | 102 virtual ~WebRequestRulesRegistry(); |
97 | 103 |
98 // Virtual for testing: | 104 // Virtual for testing: |
99 virtual base::Time GetExtensionInstallationTime( | 105 virtual base::Time GetExtensionInstallationTime( |
100 const std::string& extension_id) const; | 106 const std::string& extension_id) const; |
101 virtual void ClearCacheOnNavigation(); | 107 virtual void ClearCacheOnNavigation(); |
102 | 108 |
103 private: | 109 private: |
| 110 // Checks whether the set of |conditions| and |actions| are consistent, |
| 111 // meaning for example that we do not allow combining an |action| that needs |
| 112 // to be executed before the |condition| can be fulfilled. |
| 113 // Returns true in case of consistency and MUST set |error| otherwise. |
| 114 static bool CheckConsistency(WebRequestConditionSet* conditions, |
| 115 WebRequestActionSet* actions, |
| 116 std::string* error); |
| 117 |
104 typedef std::map<URLMatcherConditionSet::ID, WebRequestRule*> RuleTriggers; | 118 typedef std::map<URLMatcherConditionSet::ID, WebRequestRule*> RuleTriggers; |
105 typedef std::map<WebRequestRule::GlobalRuleId, linked_ptr<WebRequestRule> > | 119 typedef std::map<WebRequestRule::GlobalRuleId, linked_ptr<WebRequestRule> > |
106 RulesMap; | 120 RulesMap; |
107 | 121 |
108 // Map that tells us which WebRequestRule may match under the condition that | 122 // Map that tells us which WebRequestRule may match under the condition that |
109 // the URLMatcherConditionSet::ID was returned by the |url_matcher_|. | 123 // the URLMatcherConditionSet::ID was returned by the |url_matcher_|. |
110 RuleTriggers rule_triggers_; | 124 RuleTriggers rule_triggers_; |
111 | 125 |
112 RulesMap webrequest_rules_; | 126 RulesMap webrequest_rules_; |
113 | 127 |
114 URLMatcher url_matcher_; | 128 URLMatcher url_matcher_; |
115 | 129 |
116 scoped_refptr<ExtensionInfoMap> extension_info_map_; | 130 scoped_refptr<ExtensionInfoMap> extension_info_map_; |
117 }; | 131 }; |
118 | 132 |
119 } // namespace extensions | 133 } // namespace extensions |
120 | 134 |
121 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_RULES
_REGISTRY_H_ | 135 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_RULES
_REGISTRY_H_ |
OLD | NEW |