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 |
(...skipping 12 matching lines...) Expand all Loading... | |
23 | 23 |
24 namespace extensions { | 24 namespace extensions { |
25 | 25 |
26 WebRequestRulesRegistry::WebRequestRulesRegistry(Profile* profile, | 26 WebRequestRulesRegistry::WebRequestRulesRegistry(Profile* profile, |
27 Delegate* delegate) | 27 Delegate* delegate) |
28 : RulesRegistryWithCache(delegate) { | 28 : RulesRegistryWithCache(delegate) { |
29 if (profile) | 29 if (profile) |
30 extension_info_map_ = ExtensionSystem::Get(profile)->info_map(); | 30 extension_info_map_ = ExtensionSystem::Get(profile)->info_map(); |
31 } | 31 } |
32 | 32 |
33 std::set<const WebRequestRule*> | 33 std::set<const WebRequestRule*> WebRequestRulesRegistry::GetMatches( |
34 WebRequestRulesRegistry::GetMatches( | 34 DeclarativeWebRequestData* request_data) { |
battre
2013/01/23 18:56:34
What do you think this:
struct WebRequestData {
Jeffrey Yasskin
2013/01/24 00:39:17
I don't have a strong opinion here, but I don't th
vabr (Chromium)
2013/01/24 18:24:08
I thought about this, and did not dare to suggest
| |
35 const DeclarativeWebRequestData& request_data) { | 35 RuleSet result; |
36 typedef std::set<const WebRequestRule*> RuleSet; | |
37 typedef std::set<URLMatcherConditionSet::ID> URLMatches; | |
38 | 36 |
39 RuleSet result; | 37 request_data->url_match_ids = |
40 URLMatches url_matches = url_matcher_.MatchURL(request_data.request->url()); | 38 url_matcher_.MatchURL(request_data->request->url()); |
39 request_data->first_party_url_match_ids = | |
40 url_matcher_.MatchURL(request_data->request->first_party_for_cookies()); | |
41 | 41 |
42 // 1st phase -- add all rules with some conditions without UrlFilter | 42 // 1st phase -- add all rules with some conditions without UrlFilter |
43 // attributes. | 43 // attributes. |
44 for (RuleSet::const_iterator it = rules_with_untriggered_conditions_.begin(); | 44 for (RuleSet::const_iterator it = rules_with_untriggered_conditions_.begin(); |
45 it != rules_with_untriggered_conditions_.end(); ++it) { | 45 it != rules_with_untriggered_conditions_.end(); ++it) { |
46 if ((*it)->conditions().IsFulfilled(-1, url_matches, request_data)) | 46 if ((*it)->conditions().IsFulfilled(-1, *request_data)) |
47 result.insert(*it); | 47 result.insert(*it); |
48 } | 48 } |
49 | 49 |
50 // 2nd phase -- add all rules with some conditions triggered by URL matches. | 50 // 2nd phase -- add all rules with some conditions triggered by URL matches. |
51 for (URLMatches::const_iterator url_match = url_matches.begin(); | 51 AddTriggeredRules(request_data->url_match_ids, *request_data, &result); |
52 url_match != url_matches.end(); ++url_match) { | 52 AddTriggeredRules(request_data->first_party_url_match_ids, |
53 RuleTriggers::const_iterator rule_trigger = rule_triggers_.find(*url_match); | 53 *request_data, &result); |
54 CHECK(rule_trigger != rule_triggers_.end()); | |
55 if (!ContainsKey(result, rule_trigger->second) && | |
56 rule_trigger->second->conditions().IsFulfilled(*url_match, url_matches, | |
57 request_data)) | |
58 result.insert(rule_trigger->second); | |
59 } | |
60 | 54 |
61 return result; | 55 return result; |
62 } | 56 } |
63 | 57 |
64 std::list<LinkedPtrEventResponseDelta> WebRequestRulesRegistry::CreateDeltas( | 58 std::list<LinkedPtrEventResponseDelta> WebRequestRulesRegistry::CreateDeltas( |
65 const ExtensionInfoMap* extension_info_map, | 59 const ExtensionInfoMap* extension_info_map, |
66 const DeclarativeWebRequestData& request_data, | 60 DeclarativeWebRequestData* request_data, |
67 bool crosses_incognito) { | 61 bool crosses_incognito) { |
68 if (webrequest_rules_.empty()) | 62 if (webrequest_rules_.empty()) |
69 return std::list<LinkedPtrEventResponseDelta>(); | 63 return std::list<LinkedPtrEventResponseDelta>(); |
70 | 64 |
71 std::set<const WebRequestRule*> matches = GetMatches(request_data); | 65 std::set<const WebRequestRule*> matches = GetMatches(request_data); |
72 | 66 |
73 // Sort all matching rules by their priority so that they can be processed | 67 // Sort all matching rules by their priority so that they can be processed |
74 // in decreasing order. | 68 // in decreasing order. |
75 typedef std::pair<WebRequestRule::Priority, WebRequestRule::GlobalRuleId> | 69 typedef std::pair<WebRequestRule::Priority, WebRequestRule::GlobalRuleId> |
76 PriorityRuleIdPair; | 70 PriorityRuleIdPair; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
109 | 103 |
110 // Skip rule if a previous rule of this extension instructed to ignore | 104 // Skip rule if a previous rule of this extension instructed to ignore |
111 // all rules with a lower priority than min_priorities[extension_id]. | 105 // all rules with a lower priority than min_priorities[extension_id]. |
112 int current_min_priority = min_priorities[extension_id]; | 106 int current_min_priority = min_priorities[extension_id]; |
113 if (priority_of_rule < current_min_priority) | 107 if (priority_of_rule < current_min_priority) |
114 continue; | 108 continue; |
115 | 109 |
116 | 110 |
117 std::list<LinkedPtrEventResponseDelta> rule_result; | 111 std::list<LinkedPtrEventResponseDelta> rule_result; |
118 WebRequestAction::ApplyInfo apply_info = { | 112 WebRequestAction::ApplyInfo apply_info = { |
119 extension_info_map, request_data, crosses_incognito, &rule_result | 113 extension_info_map, *request_data, crosses_incognito, &rule_result |
120 }; | 114 }; |
121 rule->Apply(&apply_info); | 115 rule->Apply(&apply_info); |
122 result.splice(result.begin(), rule_result); | 116 result.splice(result.begin(), rule_result); |
123 | 117 |
124 min_priorities[extension_id] = std::max(current_min_priority, | 118 min_priorities[extension_id] = std::max(current_min_priority, |
125 rule->GetMinimumPriority()); | 119 rule->GetMinimumPriority()); |
126 } | 120 } |
127 return result; | 121 return result; |
128 } | 122 } |
129 | 123 |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
293 found_matching_condition = true; | 287 found_matching_condition = true; |
294 } | 288 } |
295 if (!found_matching_condition) { | 289 if (!found_matching_condition) { |
296 *error = kActionCannotBeExecuted; | 290 *error = kActionCannotBeExecuted; |
297 return false; | 291 return false; |
298 } | 292 } |
299 } | 293 } |
300 return true; | 294 return true; |
301 } | 295 } |
302 | 296 |
297 void WebRequestRulesRegistry::AddTriggeredRules( | |
298 const URLMatches& url_matches, | |
299 const DeclarativeWebRequestData& request_data, | |
300 RuleSet* result) { | |
301 for (URLMatches::const_iterator url_match = url_matches.begin(); | |
302 url_match != url_matches.end(); ++url_match) { | |
303 RuleTriggers::const_iterator rule_trigger = rule_triggers_.find(*url_match); | |
304 CHECK(rule_trigger != rule_triggers_.end()); | |
305 if (!ContainsKey(*result, rule_trigger->second) && | |
306 rule_trigger->second->conditions().IsFulfilled(*url_match, | |
307 request_data)) | |
308 result->insert(rule_trigger->second); | |
309 } | |
310 } | |
311 | |
303 } // namespace extensions | 312 } // namespace extensions |
OLD | NEW |