OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_ registry.h" | |
6 | |
7 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit ion.h" | |
8 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rule.h " | |
9 #include "net/url_request/url_request.h" | |
10 | |
11 namespace extensions { | |
12 namespace declarative_webrequest { | |
13 | |
14 WebRequestRulesRegistry::WebRequestRulesRegistry() {} | |
15 | |
16 WebRequestRulesRegistry::~WebRequestRulesRegistry() {} | |
17 | |
18 std::set<WebRequestRulesRegistry::GlobalRuleId> | |
19 WebRequestRulesRegistry::GetMatches(net::URLRequest* request) { | |
20 std::set<WebRequestRulesRegistry::GlobalRuleId> result; | |
21 | |
22 // Figure out for which rules, the URL match conditions were fulfilled. | |
23 // Then we need to check for each of these, whether the other | |
Matt Perry
2012/03/22 23:07:25
comment?
battre
2012/03/26 18:35:51
Done.
| |
24 // First filter by URLs. | |
25 typedef std::set<URLMatcherConditionSet::ID> UrlMatches; | |
Matt Perry
2012/03/22 01:34:43
nit: URLMatches for consistency
battre
2012/03/26 18:35:51
Done.
| |
26 UrlMatches url_matches = url_matcher_.MatchURL(request->url()); | |
27 | |
28 // Then filter by remaining conditions. | |
29 for (UrlMatches::iterator url_match = url_matches.begin(); | |
30 url_match != url_matches.end(); ++url_match) { | |
Matt Perry
2012/03/22 23:07:25
+1 indent (to line up with inside of parenthetical
battre
2012/03/26 18:35:51
Done.
| |
31 | |
32 RuleTriggers::iterator rule_trigger = rule_triggers_.find(*url_match); | |
33 CHECK(rule_trigger != rule_triggers_.end()); | |
34 | |
35 WebRequestRule* rule = rule_trigger->second; | |
36 if (rule->conditions_collection().IsFulfilled(url_matches, request)) { | |
Matt Perry
2012/03/22 23:07:25
It seems unnecessary to pass in all the url_matche
battre
2012/03/26 18:35:51
Good catch.
Done.
| |
37 DCHECK(rule_ids_.find(rule) != rule_ids_.end()); | |
38 result.insert(rule_ids_[rule]); | |
39 } | |
40 } | |
41 | |
42 return result; | |
43 } | |
44 | |
45 std::string WebRequestRulesRegistry::AddRulesImpl( | |
46 const std::string& extension_id, | |
47 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules) { | |
48 std::string error; | |
49 RulesMap new_webrequest_rules; | |
50 | |
51 typedef std::vector<linked_ptr<RulesRegistry::Rule> > RulesVector; | |
52 for (RulesVector::const_iterator rule = rules.begin(); rule != rules.end(); | |
53 ++rule) { | |
54 GlobalRuleId rule_id(extension_id, *(*rule)->id); | |
55 DCHECK(webrequest_rules_.find(rule_id) == webrequest_rules_.end()); | |
56 | |
57 scoped_ptr<WebRequestRule> webrequest_rule( | |
58 WebRequestRuleFactory::CreateRule(url_matcher_.condition_factory(), | |
59 *rule, &error)); | |
60 if (!error.empty()) { | |
61 // We don't return here, because we want to clear temporary | |
62 // condition sets in the url_matcher_. | |
63 break; | |
64 } | |
65 | |
66 new_webrequest_rules.insert( | |
67 make_pair(rule_id, make_linked_ptr(webrequest_rule.release()))); | |
68 } | |
69 | |
70 if (!error.empty()) { | |
71 // Clean up temporary condition sets created during rule creation. | |
72 url_matcher_.ClearUnusedConditionSets(); | |
73 return error; | |
74 } | |
75 | |
76 // Wohoo, everything worked fine. | |
77 webrequest_rules_.insert(new_webrequest_rules.begin(), | |
78 new_webrequest_rules.end()); | |
79 | |
80 // Build mapping of rule ID pointer to GlobalRuleId. | |
81 for (RulesMap::iterator i = new_webrequest_rules.begin(); | |
82 i != new_webrequest_rules.end(); ++i) { | |
83 rule_ids_[i->second.get()] = i->first; | |
84 } | |
85 | |
86 // Create the triggers. | |
87 for (RulesMap::iterator i = new_webrequest_rules.begin(); | |
88 i != new_webrequest_rules.end(); ++i) { | |
89 std::vector<URLMatcherConditionSet> condition_sets; | |
90 const WebRequestConditionCollection& conditions_collection = | |
91 i->second->conditions_collection(); | |
92 conditions_collection.AppendURLMatcherConditionSets(&condition_sets); | |
93 for (std::vector<URLMatcherConditionSet>::iterator j = | |
94 condition_sets.begin(); j != condition_sets.end(); ++j) { | |
95 rule_triggers_.insert(std::make_pair(j->id(), i->second.get())); | |
Matt Perry
2012/03/22 23:07:25
rule_triggers_[j->id()] = i->second.get(); ?
battre
2012/03/26 18:35:51
Possible, rule_triggers_[j->id()] first creates a
Matt Perry
2012/03/26 19:11:06
map[key] = value; is just more idiosyncratic. it's
battre
2012/03/26 20:38:10
Done.
| |
96 } | |
97 } | |
98 | |
99 // Register url patterns in url_matcher_. | |
100 std::vector<URLMatcherConditionSet> all_new_condition_sets; | |
101 for (RulesMap::iterator i = new_webrequest_rules.begin(); | |
102 i != new_webrequest_rules.end(); ++i) { | |
103 i->second->conditions_collection().AppendURLMatcherConditionSets( | |
104 &all_new_condition_sets); | |
105 } | |
106 url_matcher_.AddConditionSets(all_new_condition_sets); | |
107 | |
108 return ""; | |
109 } | |
110 | |
111 std::string WebRequestRulesRegistry::RemoveRulesImpl( | |
112 const std::string& extension_id, | |
113 const std::vector<std::string>& rule_identifiers) { | |
114 return "TODO"; | |
115 } | |
116 | |
117 std::string WebRequestRulesRegistry::RemoveAllRulesImpl( | |
118 const std::string& extension_id) { | |
119 return "TODO"; | |
120 } | |
121 | |
122 content::BrowserThread::ID WebRequestRulesRegistry::GetOwnerThread() const { | |
123 return content::BrowserThread::IO; | |
124 } | |
125 | |
126 } // namespace declarative_webrequest | |
127 } // namespace extensions | |
OLD | NEW |