| 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 <string> | 11 #include <string> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/gtest_prod_util.h" |
| 14 #include "base/time.h" | 15 #include "base/time.h" |
| 15 #include "base/memory/linked_ptr.h" | 16 #include "base/memory/linked_ptr.h" |
| 16 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
| 17 #include "chrome/browser/extensions/api/declarative/declarative_rule.h" | 18 #include "chrome/browser/extensions/api/declarative/declarative_rule.h" |
| 18 #include "chrome/browser/extensions/api/declarative/rules_registry_with_cache.h" | 19 #include "chrome/browser/extensions/api/declarative/rules_registry_with_cache.h" |
| 19 #include "chrome/browser/extensions/api/declarative_webrequest/request_stage.h" | 20 #include "chrome/browser/extensions/api/declarative_webrequest/request_stage.h" |
| 20 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_action
.h" | 21 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_action
.h" |
| 21 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit
ion.h" | 22 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit
ion.h" |
| 22 #include "chrome/browser/extensions/extension_info_map.h" | 23 #include "chrome/browser/extensions/extension_info_map.h" |
| 23 #include "extensions/common/matcher/url_matcher.h" | 24 #include "extensions/common/matcher/url_matcher.h" |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 bool IsEmpty() const; | 101 bool IsEmpty() const; |
| 101 | 102 |
| 102 protected: | 103 protected: |
| 103 virtual ~WebRequestRulesRegistry(); | 104 virtual ~WebRequestRulesRegistry(); |
| 104 | 105 |
| 105 // Virtual for testing: | 106 // Virtual for testing: |
| 106 virtual base::Time GetExtensionInstallationTime( | 107 virtual base::Time GetExtensionInstallationTime( |
| 107 const std::string& extension_id) const; | 108 const std::string& extension_id) const; |
| 108 virtual void ClearCacheOnNavigation(); | 109 virtual void ClearCacheOnNavigation(); |
| 109 | 110 |
| 111 void SetExtensionInfoMapForTesting( |
| 112 scoped_refptr<ExtensionInfoMap> extension_info_map) { |
| 113 extension_info_map_ = extension_info_map; |
| 114 } |
| 115 |
| 110 const std::set<const WebRequestRule*>& | 116 const std::set<const WebRequestRule*>& |
| 111 rules_with_untriggered_conditions_for_test() const { | 117 rules_with_untriggered_conditions_for_test() const { |
| 112 return rules_with_untriggered_conditions_; | 118 return rules_with_untriggered_conditions_; |
| 113 } | 119 } |
| 114 | 120 |
| 115 private: | 121 private: |
| 116 // Checks whether the set of |conditions| and |actions| are consistent, | 122 FRIEND_TEST_ALL_PREFIXES(WebRequestRulesRegistrySimpleTest, StageChecker); |
| 117 // meaning for example that we do not allow combining an |action| that needs | 123 FRIEND_TEST_ALL_PREFIXES(WebRequestRulesRegistrySimpleTest, |
| 118 // to be executed before the |condition| can be fulfilled. | 124 HostPermissionsChecker); |
| 119 // Returns true in case of consistency and MUST set |error| otherwise. | |
| 120 static bool CheckConsistency(const WebRequestConditionSet* conditions, | |
| 121 const WebRequestActionSet* actions, | |
| 122 std::string* error); | |
| 123 | 125 |
| 124 typedef std::map<URLMatcherConditionSet::ID, WebRequestRule*> RuleTriggers; | 126 typedef std::map<URLMatcherConditionSet::ID, WebRequestRule*> RuleTriggers; |
| 125 typedef std::map<WebRequestRule::GlobalRuleId, linked_ptr<WebRequestRule> > | 127 typedef std::map<WebRequestRule::GlobalRuleId, linked_ptr<WebRequestRule> > |
| 126 RulesMap; | 128 RulesMap; |
| 127 typedef std::set<URLMatcherConditionSet::ID> URLMatches; | 129 typedef std::set<URLMatcherConditionSet::ID> URLMatches; |
| 128 typedef std::set<const WebRequestRule*> RuleSet; | 130 typedef std::set<const WebRequestRule*> RuleSet; |
| 129 | 131 |
| 132 // This bundles all consistency checkers. Returns true in case of consistency |
| 133 // and MUST set |error| otherwise. |
| 134 static bool Checker(const Extension* extension, |
| 135 const WebRequestConditionSet* conditions, |
| 136 const WebRequestActionSet* actions, |
| 137 std::string* error); |
| 138 |
| 139 // Check that the |extension| has host permissions for all URLs if actions |
| 140 // requiring them are present. |
| 141 static bool HostPermissionsChecker(const Extension* extension, |
| 142 const WebRequestActionSet* actions, |
| 143 std::string* error); |
| 144 |
| 145 // Check that every action is applicable in the same request stage as at |
| 146 // least one condition. |
| 147 static bool StageChecker(const WebRequestConditionSet* conditions, |
| 148 const WebRequestActionSet* actions, |
| 149 std::string* error); |
| 150 |
| 130 // This is a helper function to GetMatches. Rules triggered by |url_matches| | 151 // This is a helper function to GetMatches. Rules triggered by |url_matches| |
| 131 // get added to |result| if one of their conditions is fulfilled. | 152 // get added to |result| if one of their conditions is fulfilled. |
| 132 // |request_data| gets passed to IsFulfilled of the rules' condition sets. | 153 // |request_data| gets passed to IsFulfilled of the rules' condition sets. |
| 133 void AddTriggeredRules(const URLMatches& url_matches, | 154 void AddTriggeredRules(const URLMatches& url_matches, |
| 134 const WebRequestCondition::MatchData& request_data, | 155 const WebRequestCondition::MatchData& request_data, |
| 135 RuleSet* result) const; | 156 RuleSet* result) const; |
| 136 | 157 |
| 137 // Map that tells us which WebRequestRule may match under the condition that | 158 // Map that tells us which WebRequestRule may match under the condition that |
| 138 // the URLMatcherConditionSet::ID was returned by the |url_matcher_|. | 159 // the URLMatcherConditionSet::ID was returned by the |url_matcher_|. |
| 139 RuleTriggers rule_triggers_; | 160 RuleTriggers rule_triggers_; |
| 140 | 161 |
| 141 // These rules contain condition sets with conditions without URL attributes. | 162 // These rules contain condition sets with conditions without URL attributes. |
| 142 // Such conditions are not triggered by URL matcher, so we need to test them | 163 // Such conditions are not triggered by URL matcher, so we need to test them |
| 143 // separately. | 164 // separately. |
| 144 std::set<const WebRequestRule*> rules_with_untriggered_conditions_; | 165 std::set<const WebRequestRule*> rules_with_untriggered_conditions_; |
| 145 | 166 |
| 146 RulesMap webrequest_rules_; | 167 RulesMap webrequest_rules_; |
| 147 | 168 |
| 148 URLMatcher url_matcher_; | 169 URLMatcher url_matcher_; |
| 149 | 170 |
| 150 void* profile_id_; | 171 void* profile_id_; |
| 151 scoped_refptr<ExtensionInfoMap> extension_info_map_; | 172 scoped_refptr<ExtensionInfoMap> extension_info_map_; |
| 152 | 173 |
| 153 DISALLOW_COPY_AND_ASSIGN(WebRequestRulesRegistry); | 174 DISALLOW_COPY_AND_ASSIGN(WebRequestRulesRegistry); |
| 154 }; | 175 }; |
| 155 | 176 |
| 156 } // namespace extensions | 177 } // namespace extensions |
| 157 | 178 |
| 158 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_RULES
_REGISTRY_H_ | 179 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_RULES
_REGISTRY_H_ |
| OLD | NEW |