Chromium Code Reviews| 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 <limits> | 7 #include <limits> |
| 8 | 8 |
| 9 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit ion.h" | 9 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit ion.h" |
| 10 #include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h" | 10 #include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h" |
| 11 #include "chrome/browser/extensions/api/web_request/web_request_permissions.h" | 11 #include "chrome/browser/extensions/api/web_request/web_request_permissions.h" |
| 12 #include "chrome/browser/extensions/extension_system.h" | 12 #include "chrome/browser/extensions/extension_system.h" |
| 13 #include "net/url_request/url_request.h" | 13 #include "net/url_request/url_request.h" |
| 14 | 14 |
| 15 namespace extensions { | 15 namespace extensions { |
| 16 | 16 |
| 17 WebRequestRulesRegistry::WebRequestRulesRegistry(Profile* profile, | 17 WebRequestRulesRegistry::WebRequestRulesRegistry(Profile* profile, |
| 18 Delegate* delegate) | 18 Delegate* delegate) |
| 19 : RulesRegistryWithCache(delegate) { | 19 : RulesRegistryWithCache(delegate) { |
| 20 if (profile) | 20 if (profile) |
| 21 extension_info_map_ = ExtensionSystem::Get(profile)->info_map(); | 21 extension_info_map_ = ExtensionSystem::Get(profile)->info_map(); |
| 22 } | 22 } |
| 23 | 23 |
| 24 std::set<WebRequestRule::GlobalRuleId> | 24 std::set<const WebRequestRule*> |
| 25 WebRequestRulesRegistry::GetMatches( | 25 WebRequestRulesRegistry::GetMatches( |
| 26 const WebRequestRule::RequestData& request_data) { | 26 const WebRequestRule::RequestData& request_data) { |
| 27 std::set<WebRequestRule::GlobalRuleId> result; | 27 // 1st phase -- add all rules with some conditions without UrlFilter |
| 28 // attributes. | |
| 29 std::set<const WebRequestRule*> result(rules_with_untriggered_conditions_); | |
| 28 | 30 |
| 29 // Figure out for which rules the URL match conditions were fulfilled. | 31 // 2nd phase -- add all rules with some conditions triggered by URL matches. |
| 30 typedef std::set<URLMatcherConditionSet::ID> URLMatches; | 32 typedef std::set<URLMatcherConditionSet::ID> URLMatches; |
| 31 URLMatches url_matches = url_matcher_.MatchURL(request_data.request->url()); | 33 URLMatches url_matches = url_matcher_.MatchURL(request_data.request->url()); |
| 34 for (URLMatches::const_iterator url_match = url_matches.begin(); | |
| 35 url_match != url_matches.end(); ++url_match) { | |
| 36 RuleTriggers::const_iterator rule_trigger = rule_triggers_.find(*url_match); | |
| 37 CHECK(rule_trigger != rule_triggers_.end()); | |
| 38 result.insert(rule_trigger->second); | |
| 39 } | |
| 32 | 40 |
| 33 // Then we need to check for each of these, whether the other | 41 // 3rd phase -- eliminate all rules with no satisfied condition (that may |
| 34 // WebRequestConditionAttributes are also fulfilled. | 42 // happen due to non-UrlFilter attributes). |
| 35 for (URLMatches::iterator url_match = url_matches.begin(); | 43 std::set<const WebRequestRule*>::iterator it = result.begin(); |
| 36 url_match != url_matches.end(); ++url_match) { | 44 while (it != result.end()) { |
| 37 RuleTriggers::iterator rule_trigger = rule_triggers_.find(*url_match); | 45 if ((*it)->conditions().IsFulfilled(url_matches, request_data)) |
| 38 CHECK(rule_trigger != rule_triggers_.end()); | 46 ++it; |
| 47 else | |
| 48 result.erase(it++); | |
| 49 } | |
|
battre
2013/01/07 17:36:27
I think this can be simplified by just iterating o
Jeffrey Yasskin
2013/01/07 20:47:41
Yes, that's cheaper. I had guessed that it would b
vabr (Chromium)
2013/01/10 15:12:22
Done.
| |
| 39 | 50 |
| 40 WebRequestRule* rule = rule_trigger->second; | |
| 41 if (rule->conditions().IsFulfilled(*url_match, request_data)) | |
| 42 result.insert(rule->id()); | |
| 43 } | |
| 44 return result; | 51 return result; |
| 45 } | 52 } |
| 46 | 53 |
| 47 std::list<LinkedPtrEventResponseDelta> WebRequestRulesRegistry::CreateDeltas( | 54 std::list<LinkedPtrEventResponseDelta> WebRequestRulesRegistry::CreateDeltas( |
| 48 const ExtensionInfoMap* extension_info_map, | 55 const ExtensionInfoMap* extension_info_map, |
| 49 const WebRequestRule::RequestData& request_data, | 56 const WebRequestRule::RequestData& request_data, |
| 50 bool crosses_incognito) { | 57 bool crosses_incognito) { |
| 51 if (webrequest_rules_.empty()) | 58 if (webrequest_rules_.empty()) |
| 52 return std::list<LinkedPtrEventResponseDelta>(); | 59 return std::list<LinkedPtrEventResponseDelta>(); |
| 53 | 60 |
| 54 std::set<WebRequestRule::GlobalRuleId> matches = | 61 std::set<const WebRequestRule*> matches = GetMatches(request_data); |
| 55 GetMatches(request_data); | |
| 56 | 62 |
| 57 // Sort all matching rules by their priority so that they can be processed | 63 // Sort all matching rules by their priority so that they can be processed |
| 58 // in decreasing order. | 64 // in decreasing order. |
| 59 typedef std::pair<WebRequestRule::Priority, WebRequestRule::GlobalRuleId> | 65 typedef std::pair<WebRequestRule::Priority, WebRequestRule::GlobalRuleId> |
| 60 PriorityRuleIdPair; | 66 PriorityRuleIdPair; |
| 61 std::vector<PriorityRuleIdPair> ordered_matches; | 67 std::vector<PriorityRuleIdPair> ordered_matches; |
| 62 ordered_matches.reserve(matches.size()); | 68 ordered_matches.reserve(matches.size()); |
| 63 for (std::set<WebRequestRule::GlobalRuleId>::iterator i = matches.begin(); | 69 for (std::set<const WebRequestRule*>::iterator i = matches.begin(); |
| 64 i != matches.end(); ++i) { | 70 i != matches.end(); ++i) { |
| 65 RulesMap::const_iterator rule = webrequest_rules_.find(*i); | 71 ordered_matches.push_back(make_pair((*i)->priority(), (*i)->id())); |
| 66 CHECK(rule != webrequest_rules_.end()); | |
| 67 ordered_matches.push_back(make_pair(rule->second->priority(), *i)); | |
| 68 } | 72 } |
| 69 // Sort from rbegin to rend in order to get descending priority order. | 73 // Sort from rbegin to rend in order to get descending priority order. |
| 70 std::sort(ordered_matches.rbegin(), ordered_matches.rend()); | 74 std::sort(ordered_matches.rbegin(), ordered_matches.rend()); |
| 71 | 75 |
| 72 // Build a map that maps each extension id to the minimum required priority | 76 // Build a map that maps each extension id to the minimum required priority |
| 73 // for rules of that extension. Initially, this priority is -infinite and | 77 // for rules of that extension. Initially, this priority is -infinite and |
| 74 // will be increased when the rules are processed and raise the bar via | 78 // will be increased when the rules are processed and raise the bar via |
| 75 // WebRequestIgnoreRulesActions. | 79 // WebRequestIgnoreRulesActions. |
| 76 typedef std::string ExtensionId; | 80 typedef std::string ExtensionId; |
| 77 typedef std::map<ExtensionId, WebRequestRule::Priority> MinPriorities; | 81 typedef std::map<ExtensionId, WebRequestRule::Priority> MinPriorities; |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 150 i != new_webrequest_rules.end(); ++i) { | 154 i != new_webrequest_rules.end(); ++i) { |
| 151 URLMatcherConditionSet::Vector url_condition_sets; | 155 URLMatcherConditionSet::Vector url_condition_sets; |
| 152 const WebRequestConditionSet& conditions = i->second->conditions(); | 156 const WebRequestConditionSet& conditions = i->second->conditions(); |
| 153 conditions.GetURLMatcherConditionSets(&url_condition_sets); | 157 conditions.GetURLMatcherConditionSets(&url_condition_sets); |
| 154 for (URLMatcherConditionSet::Vector::iterator j = | 158 for (URLMatcherConditionSet::Vector::iterator j = |
| 155 url_condition_sets.begin(); j != url_condition_sets.end(); ++j) { | 159 url_condition_sets.begin(); j != url_condition_sets.end(); ++j) { |
| 156 rule_triggers_[(*j)->id()] = i->second.get(); | 160 rule_triggers_[(*j)->id()] = i->second.get(); |
| 157 } | 161 } |
| 158 } | 162 } |
| 159 | 163 |
| 160 // Register url patterns in url_matcher_. | 164 // Register url patterns in |url_matcher_| and |
| 165 // |rules_with_untriggered_conditions_|. | |
| 161 URLMatcherConditionSet::Vector all_new_condition_sets; | 166 URLMatcherConditionSet::Vector all_new_condition_sets; |
| 162 for (RulesMap::iterator i = new_webrequest_rules.begin(); | 167 for (RulesMap::iterator i = new_webrequest_rules.begin(); |
| 163 i != new_webrequest_rules.end(); ++i) { | 168 i != new_webrequest_rules.end(); ++i) { |
| 164 i->second->conditions().GetURLMatcherConditionSets(&all_new_condition_sets); | 169 i->second->conditions().GetURLMatcherConditionSets(&all_new_condition_sets); |
| 170 if (i->second->conditions().HasConditionsWithoutUrls()) | |
| 171 rules_with_untriggered_conditions_.insert(i->second.get()); | |
| 165 } | 172 } |
| 166 url_matcher_.AddConditionSets(all_new_condition_sets); | 173 url_matcher_.AddConditionSets(all_new_condition_sets); |
| 167 | 174 |
| 168 ClearCacheOnNavigation(); | 175 ClearCacheOnNavigation(); |
| 169 | 176 |
| 170 return ""; | 177 return ""; |
| 171 } | 178 } |
| 172 | 179 |
| 173 std::string WebRequestRulesRegistry::RemoveRulesImpl( | 180 std::string WebRequestRulesRegistry::RemoveRulesImpl( |
| 174 const std::string& extension_id, | 181 const std::string& extension_id, |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 188 // Remove all triggers but collect their IDs. | 195 // Remove all triggers but collect their IDs. |
| 189 URLMatcherConditionSet::Vector condition_sets; | 196 URLMatcherConditionSet::Vector condition_sets; |
| 190 WebRequestRule* rule = webrequest_rules_entry->second.get(); | 197 WebRequestRule* rule = webrequest_rules_entry->second.get(); |
| 191 rule->conditions().GetURLMatcherConditionSets(&condition_sets); | 198 rule->conditions().GetURLMatcherConditionSets(&condition_sets); |
| 192 for (URLMatcherConditionSet::Vector::iterator j = condition_sets.begin(); | 199 for (URLMatcherConditionSet::Vector::iterator j = condition_sets.begin(); |
| 193 j != condition_sets.end(); ++j) { | 200 j != condition_sets.end(); ++j) { |
| 194 remove_from_url_matcher.push_back((*j)->id()); | 201 remove_from_url_matcher.push_back((*j)->id()); |
| 195 rule_triggers_.erase((*j)->id()); | 202 rule_triggers_.erase((*j)->id()); |
| 196 } | 203 } |
| 197 | 204 |
| 198 // Remove reference to actual rule. | 205 rules_with_untriggered_conditions_.erase(rule); |
| 206 | |
| 207 // Removes the owning references to (and thus deletes) the rule. | |
| 199 webrequest_rules_.erase(webrequest_rules_entry); | 208 webrequest_rules_.erase(webrequest_rules_entry); |
| 200 } | 209 } |
| 201 | 210 |
| 202 // Clear URLMatcher based on condition_set_ids that are not needed any more. | 211 // Clear URLMatcher based on condition_set_ids that are not needed any more. |
| 203 url_matcher_.RemoveConditionSets(remove_from_url_matcher); | 212 url_matcher_.RemoveConditionSets(remove_from_url_matcher); |
| 204 | 213 |
| 205 ClearCacheOnNavigation(); | 214 ClearCacheOnNavigation(); |
| 206 | 215 |
| 207 return ""; | 216 return ""; |
| 208 } | 217 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 240 return base::Time(); | 249 return base::Time(); |
| 241 | 250 |
| 242 return extension_info_map_->GetInstallTime(extension_id); | 251 return extension_info_map_->GetInstallTime(extension_id); |
| 243 } | 252 } |
| 244 | 253 |
| 245 void WebRequestRulesRegistry::ClearCacheOnNavigation() { | 254 void WebRequestRulesRegistry::ClearCacheOnNavigation() { |
| 246 extension_web_request_api_helpers::ClearCacheOnNavigation(); | 255 extension_web_request_api_helpers::ClearCacheOnNavigation(); |
| 247 } | 256 } |
| 248 | 257 |
| 249 } // namespace extensions | 258 } // namespace extensions |
| OLD | NEW |