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 const WebRequestData& request_data_without_ids) const { |
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 WebRequestDataWithMatchIds request_data(&request_data_without_ids); |
40 URLMatches url_matches = url_matcher_.MatchURL(request_data.request->url()); | 38 request_data.url_match_ids = url_matcher_.MatchURL( |
| 39 request_data.data->request->url()); |
| 40 request_data.first_party_url_match_ids = url_matcher_.MatchURL( |
| 41 request_data.data->request->first_party_for_cookies()); |
41 | 42 |
42 // 1st phase -- add all rules with some conditions without UrlFilter | 43 // 1st phase -- add all rules with some conditions without UrlFilter |
43 // attributes. | 44 // attributes. |
44 for (RuleSet::const_iterator it = rules_with_untriggered_conditions_.begin(); | 45 for (RuleSet::const_iterator it = rules_with_untriggered_conditions_.begin(); |
45 it != rules_with_untriggered_conditions_.end(); ++it) { | 46 it != rules_with_untriggered_conditions_.end(); ++it) { |
46 if ((*it)->conditions().IsFulfilled(-1, url_matches, request_data)) | 47 if ((*it)->conditions().IsFulfilled(-1, request_data)) |
47 result.insert(*it); | 48 result.insert(*it); |
48 } | 49 } |
49 | 50 |
50 // 2nd phase -- add all rules with some conditions triggered by URL matches. | 51 // 2nd phase -- add all rules with some conditions triggered by URL matches. |
51 for (URLMatches::const_iterator url_match = url_matches.begin(); | 52 AddTriggeredRules(request_data.url_match_ids, request_data, &result); |
52 url_match != url_matches.end(); ++url_match) { | 53 AddTriggeredRules(request_data.first_party_url_match_ids, |
53 RuleTriggers::const_iterator rule_trigger = rule_triggers_.find(*url_match); | 54 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 | 55 |
61 return result; | 56 return result; |
62 } | 57 } |
63 | 58 |
64 std::list<LinkedPtrEventResponseDelta> WebRequestRulesRegistry::CreateDeltas( | 59 std::list<LinkedPtrEventResponseDelta> WebRequestRulesRegistry::CreateDeltas( |
65 const ExtensionInfoMap* extension_info_map, | 60 const ExtensionInfoMap* extension_info_map, |
66 const DeclarativeWebRequestData& request_data, | 61 const WebRequestData& request_data, |
67 bool crosses_incognito) { | 62 bool crosses_incognito) { |
68 if (webrequest_rules_.empty()) | 63 if (webrequest_rules_.empty()) |
69 return std::list<LinkedPtrEventResponseDelta>(); | 64 return std::list<LinkedPtrEventResponseDelta>(); |
70 | 65 |
71 std::set<const WebRequestRule*> matches = GetMatches(request_data); | 66 std::set<const WebRequestRule*> matches = GetMatches(request_data); |
72 | 67 |
73 // Sort all matching rules by their priority so that they can be processed | 68 // Sort all matching rules by their priority so that they can be processed |
74 // in decreasing order. | 69 // in decreasing order. |
75 typedef std::pair<WebRequestRule::Priority, WebRequestRule::GlobalRuleId> | 70 typedef std::pair<WebRequestRule::Priority, WebRequestRule::GlobalRuleId> |
76 PriorityRuleIdPair; | 71 PriorityRuleIdPair; |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 found_matching_condition = true; | 288 found_matching_condition = true; |
294 } | 289 } |
295 if (!found_matching_condition) { | 290 if (!found_matching_condition) { |
296 *error = kActionCannotBeExecuted; | 291 *error = kActionCannotBeExecuted; |
297 return false; | 292 return false; |
298 } | 293 } |
299 } | 294 } |
300 return true; | 295 return true; |
301 } | 296 } |
302 | 297 |
| 298 void WebRequestRulesRegistry::AddTriggeredRules( |
| 299 const URLMatches& url_matches, |
| 300 const WebRequestCondition::MatchData& request_data, |
| 301 RuleSet* result) const { |
| 302 for (URLMatches::const_iterator url_match = url_matches.begin(); |
| 303 url_match != url_matches.end(); ++url_match) { |
| 304 RuleTriggers::const_iterator rule_trigger = rule_triggers_.find(*url_match); |
| 305 CHECK(rule_trigger != rule_triggers_.end()); |
| 306 if (!ContainsKey(*result, rule_trigger->second) && |
| 307 rule_trigger->second->conditions().IsFulfilled(*url_match, |
| 308 request_data)) |
| 309 result->insert(rule_trigger->second); |
| 310 } |
| 311 } |
| 312 |
303 } // namespace extensions | 313 } // namespace extensions |
OLD | NEW |