| 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 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_
registry.h" | 5 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_
registry.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> | 8 #include <limits> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 12 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit
ion.h" | 12 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit
ion.h" |
| 13 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_
checker.h" |
| 13 #include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h" | 14 #include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h" |
| 14 #include "chrome/browser/extensions/api/web_request/web_request_permissions.h" | 15 #include "chrome/browser/extensions/api/web_request/web_request_permissions.h" |
| 15 #include "chrome/browser/extensions/extension_system.h" | 16 #include "chrome/browser/extensions/extension_system.h" |
| 16 #include "net/url_request/url_request.h" | 17 #include "net/url_request/url_request.h" |
| 17 | 18 |
| 18 namespace { | |
| 19 const char kActionCannotBeExecuted[] = "An action can never be executed " | |
| 20 "because there are is no time in the request life-cycle during which the " | |
| 21 "conditions can be checked and the action can possibly be executed."; | |
| 22 } // namespace | |
| 23 | |
| 24 namespace extensions { | 19 namespace extensions { |
| 25 | 20 |
| 26 WebRequestRulesRegistry::WebRequestRulesRegistry(Profile* profile, | 21 WebRequestRulesRegistry::WebRequestRulesRegistry(Profile* profile, |
| 27 Delegate* delegate) | 22 Delegate* delegate) |
| 28 : RulesRegistryWithCache(delegate), | 23 : RulesRegistryWithCache(delegate), |
| 29 profile_id_(profile) { | 24 profile_id_(profile) { |
| 30 if (profile) | 25 if (profile) |
| 31 extension_info_map_ = ExtensionSystem::Get(profile)->info_map(); | 26 extension_info_map_ = ExtensionSystem::Get(profile)->info_map(); |
| 32 } | 27 } |
| 33 | 28 |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 } | 133 } |
| 139 | 134 |
| 140 std::string WebRequestRulesRegistry::AddRulesImpl( | 135 std::string WebRequestRulesRegistry::AddRulesImpl( |
| 141 const std::string& extension_id, | 136 const std::string& extension_id, |
| 142 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules) { | 137 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules) { |
| 143 base::Time extension_installation_time = | 138 base::Time extension_installation_time = |
| 144 GetExtensionInstallationTime(extension_id); | 139 GetExtensionInstallationTime(extension_id); |
| 145 | 140 |
| 146 std::string error; | 141 std::string error; |
| 147 RulesMap new_webrequest_rules; | 142 RulesMap new_webrequest_rules; |
| 143 const Extension* extension = |
| 144 extension_info_map_->extensions().GetByID(extension_id); |
| 145 WebRequestRulesChecker checker(extension); |
| 148 | 146 |
| 149 for (std::vector<linked_ptr<RulesRegistry::Rule> >::const_iterator rule = | 147 for (std::vector<linked_ptr<RulesRegistry::Rule> >::const_iterator rule = |
| 150 rules.begin(); rule != rules.end(); ++rule) { | 148 rules.begin(); rule != rules.end(); ++rule) { |
| 151 WebRequestRule::GlobalRuleId rule_id(extension_id, *(*rule)->id); | 149 WebRequestRule::GlobalRuleId rule_id(extension_id, *(*rule)->id); |
| 152 DCHECK(webrequest_rules_.find(rule_id) == webrequest_rules_.end()); | 150 DCHECK(webrequest_rules_.find(rule_id) == webrequest_rules_.end()); |
| 153 | 151 |
| 154 scoped_ptr<WebRequestRule> webrequest_rule( | 152 scoped_ptr<WebRequestRule> webrequest_rule( |
| 155 WebRequestRule::Create(url_matcher_.condition_factory(), extension_id, | 153 WebRequestRule::Create(url_matcher_.condition_factory(), extension_id, |
| 156 extension_installation_time, *rule, | 154 extension_installation_time, *rule, |
| 157 &CheckConsistency, &error)); | 155 checker.GetChecker(), &error)); |
| 158 if (!error.empty()) { | 156 if (!error.empty()) { |
| 159 // We don't return here, because we want to clear temporary | 157 // We don't return here, because we want to clear temporary |
| 160 // condition sets in the url_matcher_. | 158 // condition sets in the url_matcher_. |
| 161 break; | 159 break; |
| 162 } | 160 } |
| 163 | 161 |
| 164 new_webrequest_rules[rule_id] = make_linked_ptr(webrequest_rule.release()); | 162 new_webrequest_rules[rule_id] = make_linked_ptr(webrequest_rule.release()); |
| 165 } | 163 } |
| 166 | 164 |
| 167 if (!error.empty()) { | 165 if (!error.empty()) { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 193 i != new_webrequest_rules.end(); ++i) { | 191 i != new_webrequest_rules.end(); ++i) { |
| 194 i->second->conditions().GetURLMatcherConditionSets(&all_new_condition_sets); | 192 i->second->conditions().GetURLMatcherConditionSets(&all_new_condition_sets); |
| 195 if (i->second->conditions().HasConditionsWithoutUrls()) | 193 if (i->second->conditions().HasConditionsWithoutUrls()) |
| 196 rules_with_untriggered_conditions_.insert(i->second.get()); | 194 rules_with_untriggered_conditions_.insert(i->second.get()); |
| 197 } | 195 } |
| 198 url_matcher_.AddConditionSets(all_new_condition_sets); | 196 url_matcher_.AddConditionSets(all_new_condition_sets); |
| 199 | 197 |
| 200 ClearCacheOnNavigation(); | 198 ClearCacheOnNavigation(); |
| 201 | 199 |
| 202 if (profile_id_ && !webrequest_rules_.empty()) { | 200 if (profile_id_ && !webrequest_rules_.empty()) { |
| 203 const Extension* extension = | |
| 204 extension_info_map_->extensions().GetByID(extension_id); | |
| 205 | |
| 206 BrowserThread::PostTask( | 201 BrowserThread::PostTask( |
| 207 BrowserThread::UI, FROM_HERE, | 202 BrowserThread::UI, FROM_HERE, |
| 208 base::Bind(&extension_web_request_api_helpers::NotifyWebRequestAPIUsed, | 203 base::Bind(&extension_web_request_api_helpers::NotifyWebRequestAPIUsed, |
| 209 profile_id_, make_scoped_refptr(extension))); | 204 profile_id_, make_scoped_refptr(extension))); |
| 210 } | 205 } |
| 211 | 206 |
| 212 return std::string(); | 207 return std::string(); |
| 213 } | 208 } |
| 214 | 209 |
| 215 std::string WebRequestRulesRegistry::RemoveRulesImpl( | 210 std::string WebRequestRulesRegistry::RemoveRulesImpl( |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 if (!extension_info_map_.get()) // May be NULL during testing. | 278 if (!extension_info_map_.get()) // May be NULL during testing. |
| 284 return base::Time(); | 279 return base::Time(); |
| 285 | 280 |
| 286 return extension_info_map_->GetInstallTime(extension_id); | 281 return extension_info_map_->GetInstallTime(extension_id); |
| 287 } | 282 } |
| 288 | 283 |
| 289 void WebRequestRulesRegistry::ClearCacheOnNavigation() { | 284 void WebRequestRulesRegistry::ClearCacheOnNavigation() { |
| 290 extension_web_request_api_helpers::ClearCacheOnNavigation(); | 285 extension_web_request_api_helpers::ClearCacheOnNavigation(); |
| 291 } | 286 } |
| 292 | 287 |
| 293 // static | |
| 294 bool WebRequestRulesRegistry::CheckConsistency( | |
| 295 const WebRequestConditionSet* conditions, | |
| 296 const WebRequestActionSet* actions, | |
| 297 std::string* error) { | |
| 298 // Actions and conditions can be checked and executed in specific phases | |
| 299 // of each web request. We consider a rule inconsistent if there is an action | |
| 300 // that cannot be triggered by any condition. | |
| 301 for (WebRequestActionSet::Actions::const_iterator action_iter = | |
| 302 actions->actions().begin(); | |
| 303 action_iter != actions->actions().end(); | |
| 304 ++action_iter) { | |
| 305 bool found_matching_condition = false; | |
| 306 for (WebRequestConditionSet::Conditions::const_iterator condition_iter = | |
| 307 conditions->conditions().begin(); | |
| 308 condition_iter != conditions->conditions().end() && | |
| 309 !found_matching_condition; | |
| 310 ++condition_iter) { | |
| 311 // Test the intersection of bit masks, this is intentionally & and not &&. | |
| 312 if ((*action_iter)->GetStages() & (*condition_iter)->stages()) | |
| 313 found_matching_condition = true; | |
| 314 } | |
| 315 if (!found_matching_condition) { | |
| 316 *error = kActionCannotBeExecuted; | |
| 317 return false; | |
| 318 } | |
| 319 } | |
| 320 return true; | |
| 321 } | |
| 322 | |
| 323 void WebRequestRulesRegistry::AddTriggeredRules( | 288 void WebRequestRulesRegistry::AddTriggeredRules( |
| 324 const URLMatches& url_matches, | 289 const URLMatches& url_matches, |
| 325 const WebRequestCondition::MatchData& request_data, | 290 const WebRequestCondition::MatchData& request_data, |
| 326 RuleSet* result) const { | 291 RuleSet* result) const { |
| 327 for (URLMatches::const_iterator url_match = url_matches.begin(); | 292 for (URLMatches::const_iterator url_match = url_matches.begin(); |
| 328 url_match != url_matches.end(); ++url_match) { | 293 url_match != url_matches.end(); ++url_match) { |
| 329 RuleTriggers::const_iterator rule_trigger = rule_triggers_.find(*url_match); | 294 RuleTriggers::const_iterator rule_trigger = rule_triggers_.find(*url_match); |
| 330 CHECK(rule_trigger != rule_triggers_.end()); | 295 CHECK(rule_trigger != rule_triggers_.end()); |
| 331 if (!ContainsKey(*result, rule_trigger->second) && | 296 if (!ContainsKey(*result, rule_trigger->second) && |
| 332 rule_trigger->second->conditions().IsFulfilled(*url_match, | 297 rule_trigger->second->conditions().IsFulfilled(*url_match, |
| 333 request_data)) | 298 request_data)) |
| 334 result->insert(rule_trigger->second); | 299 result->insert(rule_trigger->second); |
| 335 } | 300 } |
| 336 } | 301 } |
| 337 | 302 |
| 338 } // namespace extensions | 303 } // namespace extensions |
| OLD | NEW |