Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_registry.cc

Issue 11569007: Refactoring how conditions without URL attributes are handled. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rewritten GetMatches and added the trigger ID to Set::IsFulfilled Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 <limits> 8 #include <limits>
9 #include <utility>
8 10
9 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit ion.h" 11 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit ion.h"
10 #include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h" 12 #include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h"
11 #include "chrome/browser/extensions/api/web_request/web_request_permissions.h" 13 #include "chrome/browser/extensions/api/web_request/web_request_permissions.h"
12 #include "chrome/browser/extensions/extension_system.h" 14 #include "chrome/browser/extensions/extension_system.h"
13 #include "net/url_request/url_request.h" 15 #include "net/url_request/url_request.h"
14 16
15 namespace extensions { 17 namespace extensions {
16 18
17 WebRequestRulesRegistry::WebRequestRulesRegistry(Profile* profile, 19 WebRequestRulesRegistry::WebRequestRulesRegistry(Profile* profile,
18 Delegate* delegate) 20 Delegate* delegate)
19 : RulesRegistryWithCache(delegate) { 21 : RulesRegistryWithCache(delegate) {
20 if (profile) 22 if (profile)
21 extension_info_map_ = ExtensionSystem::Get(profile)->info_map(); 23 extension_info_map_ = ExtensionSystem::Get(profile)->info_map();
22 } 24 }
23 25
24 std::set<WebRequestRule::GlobalRuleId> 26 std::set<const WebRequestRule*>
25 WebRequestRulesRegistry::GetMatches( 27 WebRequestRulesRegistry::GetMatches(
26 const WebRequestRule::RequestData& request_data) { 28 const WebRequestRule::RequestData& request_data) {
27 std::set<WebRequestRule::GlobalRuleId> result; 29 typedef std::set<const WebRequestRule*> RuleSet;
30 typedef std::set<URLMatcherConditionSet::ID> URLMatches;
28 31
29 // Figure out for which rules the URL match conditions were fulfilled. 32 RuleSet not_fulfilled;
30 typedef std::set<URLMatcherConditionSet::ID> URLMatches; 33 RuleSet result;
31 URLMatches url_matches = url_matcher_.MatchURL(request_data.request->url()); 34 URLMatches url_matches = url_matcher_.MatchURL(request_data.request->url());
32 35
33 // Then we need to check for each of these, whether the other 36 // 1st phase -- add all rules with some conditions without UrlFilter
34 // WebRequestConditionAttributes are also fulfilled. 37 // attributes.
35 for (URLMatches::iterator url_match = url_matches.begin(); 38 for (RuleSet::const_iterator it = rules_with_untriggered_conditions_.begin();
39 it != rules_with_untriggered_conditions_.end(); ++it) {
40 if ((*it)->conditions().IsFulfilled(-1, url_matches, request_data))
41 result.insert(*it);
42 else
43 not_fulfilled.insert(*it);
44 }
45
46 // 2nd phase -- add all rules with some conditions triggered by URL matches.
47 for (URLMatches::const_iterator url_match = url_matches.begin();
36 url_match != url_matches.end(); ++url_match) { 48 url_match != url_matches.end(); ++url_match) {
37 RuleTriggers::iterator rule_trigger = rule_triggers_.find(*url_match); 49 RuleTriggers::const_iterator rule_trigger = rule_triggers_.find(*url_match);
38 CHECK(rule_trigger != rule_triggers_.end()); 50 CHECK(rule_trigger != rule_triggers_.end());
51 if (not_fulfilled.find(rule_trigger->second) == not_fulfilled.end() &&
Jeffrey Yasskin 2013/01/10 22:00:57 Use ContainsKey from base/stl_util.h instead of fi
Jeffrey Yasskin 2013/01/10 22:00:57 Is this right? Say there's a rule with conditions:
battre 2013/01/11 17:41:55 I think the test we want here is: if (!ContainsKey
vabr (Chromium) 2013/01/15 10:24:57 Thanks, Jeffrey, for pointing this out, this was a
52 rule_trigger->second->conditions().IsFulfilled(
53 *url_match, url_matches, request_data))
54 result.insert(rule_trigger->second);
55 }
39 56
40 WebRequestRule* rule = rule_trigger->second;
41 if (rule->conditions().IsFulfilled(*url_match, request_data))
42 result.insert(rule->id());
43 }
44 return result; 57 return result;
45 } 58 }
46 59
47 std::list<LinkedPtrEventResponseDelta> WebRequestRulesRegistry::CreateDeltas( 60 std::list<LinkedPtrEventResponseDelta> WebRequestRulesRegistry::CreateDeltas(
48 const ExtensionInfoMap* extension_info_map, 61 const ExtensionInfoMap* extension_info_map,
49 const WebRequestRule::RequestData& request_data, 62 const WebRequestRule::RequestData& request_data,
50 bool crosses_incognito) { 63 bool crosses_incognito) {
51 if (webrequest_rules_.empty()) 64 if (webrequest_rules_.empty())
52 return std::list<LinkedPtrEventResponseDelta>(); 65 return std::list<LinkedPtrEventResponseDelta>();
53 66
54 std::set<WebRequestRule::GlobalRuleId> matches = 67 std::set<const WebRequestRule*> matches = GetMatches(request_data);
55 GetMatches(request_data);
56 68
57 // Sort all matching rules by their priority so that they can be processed 69 // Sort all matching rules by their priority so that they can be processed
58 // in decreasing order. 70 // in decreasing order.
59 typedef std::pair<WebRequestRule::Priority, WebRequestRule::GlobalRuleId> 71 typedef std::pair<WebRequestRule::Priority, WebRequestRule::GlobalRuleId>
60 PriorityRuleIdPair; 72 PriorityRuleIdPair;
61 std::vector<PriorityRuleIdPair> ordered_matches; 73 std::vector<PriorityRuleIdPair> ordered_matches;
62 ordered_matches.reserve(matches.size()); 74 ordered_matches.reserve(matches.size());
63 for (std::set<WebRequestRule::GlobalRuleId>::iterator i = matches.begin(); 75 for (std::set<const WebRequestRule*>::iterator i = matches.begin();
64 i != matches.end(); ++i) { 76 i != matches.end(); ++i) {
65 RulesMap::const_iterator rule = webrequest_rules_.find(*i); 77 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 } 78 }
69 // Sort from rbegin to rend in order to get descending priority order. 79 // Sort from rbegin to rend in order to get descending priority order.
70 std::sort(ordered_matches.rbegin(), ordered_matches.rend()); 80 std::sort(ordered_matches.rbegin(), ordered_matches.rend());
71 81
72 // Build a map that maps each extension id to the minimum required priority 82 // 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 83 // 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 84 // will be increased when the rules are processed and raise the bar via
75 // WebRequestIgnoreRulesActions. 85 // WebRequestIgnoreRulesActions.
76 typedef std::string ExtensionId; 86 typedef std::string ExtensionId;
77 typedef std::map<ExtensionId, WebRequestRule::Priority> MinPriorities; 87 typedef std::map<ExtensionId, WebRequestRule::Priority> MinPriorities;
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 i != new_webrequest_rules.end(); ++i) { 160 i != new_webrequest_rules.end(); ++i) {
151 URLMatcherConditionSet::Vector url_condition_sets; 161 URLMatcherConditionSet::Vector url_condition_sets;
152 const WebRequestConditionSet& conditions = i->second->conditions(); 162 const WebRequestConditionSet& conditions = i->second->conditions();
153 conditions.GetURLMatcherConditionSets(&url_condition_sets); 163 conditions.GetURLMatcherConditionSets(&url_condition_sets);
154 for (URLMatcherConditionSet::Vector::iterator j = 164 for (URLMatcherConditionSet::Vector::iterator j =
155 url_condition_sets.begin(); j != url_condition_sets.end(); ++j) { 165 url_condition_sets.begin(); j != url_condition_sets.end(); ++j) {
156 rule_triggers_[(*j)->id()] = i->second.get(); 166 rule_triggers_[(*j)->id()] = i->second.get();
157 } 167 }
158 } 168 }
159 169
160 // Register url patterns in url_matcher_. 170 // Register url patterns in |url_matcher_| and
171 // |rules_with_untriggered_conditions_|.
161 URLMatcherConditionSet::Vector all_new_condition_sets; 172 URLMatcherConditionSet::Vector all_new_condition_sets;
162 for (RulesMap::iterator i = new_webrequest_rules.begin(); 173 for (RulesMap::iterator i = new_webrequest_rules.begin();
163 i != new_webrequest_rules.end(); ++i) { 174 i != new_webrequest_rules.end(); ++i) {
164 i->second->conditions().GetURLMatcherConditionSets(&all_new_condition_sets); 175 i->second->conditions().GetURLMatcherConditionSets(&all_new_condition_sets);
176 if (i->second->conditions().HasConditionsWithoutUrls())
177 rules_with_untriggered_conditions_.insert(i->second.get());
165 } 178 }
166 url_matcher_.AddConditionSets(all_new_condition_sets); 179 url_matcher_.AddConditionSets(all_new_condition_sets);
167 180
168 ClearCacheOnNavigation(); 181 ClearCacheOnNavigation();
169 182
170 return ""; 183 return "";
171 } 184 }
172 185
173 std::string WebRequestRulesRegistry::RemoveRulesImpl( 186 std::string WebRequestRulesRegistry::RemoveRulesImpl(
174 const std::string& extension_id, 187 const std::string& extension_id,
(...skipping 13 matching lines...) Expand all
188 // Remove all triggers but collect their IDs. 201 // Remove all triggers but collect their IDs.
189 URLMatcherConditionSet::Vector condition_sets; 202 URLMatcherConditionSet::Vector condition_sets;
190 WebRequestRule* rule = webrequest_rules_entry->second.get(); 203 WebRequestRule* rule = webrequest_rules_entry->second.get();
191 rule->conditions().GetURLMatcherConditionSets(&condition_sets); 204 rule->conditions().GetURLMatcherConditionSets(&condition_sets);
192 for (URLMatcherConditionSet::Vector::iterator j = condition_sets.begin(); 205 for (URLMatcherConditionSet::Vector::iterator j = condition_sets.begin();
193 j != condition_sets.end(); ++j) { 206 j != condition_sets.end(); ++j) {
194 remove_from_url_matcher.push_back((*j)->id()); 207 remove_from_url_matcher.push_back((*j)->id());
195 rule_triggers_.erase((*j)->id()); 208 rule_triggers_.erase((*j)->id());
196 } 209 }
197 210
198 // Remove reference to actual rule. 211 rules_with_untriggered_conditions_.erase(rule);
212
213 // Removes the owning references to (and thus deletes) the rule.
199 webrequest_rules_.erase(webrequest_rules_entry); 214 webrequest_rules_.erase(webrequest_rules_entry);
200 } 215 }
201 216
202 // Clear URLMatcher based on condition_set_ids that are not needed any more. 217 // Clear URLMatcher based on condition_set_ids that are not needed any more.
203 url_matcher_.RemoveConditionSets(remove_from_url_matcher); 218 url_matcher_.RemoveConditionSets(remove_from_url_matcher);
204 219
205 ClearCacheOnNavigation(); 220 ClearCacheOnNavigation();
206 221
207 return ""; 222 return "";
208 } 223 }
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 return base::Time(); 255 return base::Time();
241 256
242 return extension_info_map_->GetInstallTime(extension_id); 257 return extension_info_map_->GetInstallTime(extension_id);
243 } 258 }
244 259
245 void WebRequestRulesRegistry::ClearCacheOnNavigation() { 260 void WebRequestRulesRegistry::ClearCacheOnNavigation() {
246 extension_web_request_api_helpers::ClearCacheOnNavigation(); 261 extension_web_request_api_helpers::ClearCacheOnNavigation();
247 } 262 }
248 263
249 } // namespace extensions 264 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698