Index: chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_registry.cc |
diff --git a/chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_registry.cc b/chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_registry.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..47bb0f962dabb004671d268b9b2a3b5e306ee869 |
--- /dev/null |
+++ b/chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_registry.cc |
@@ -0,0 +1,127 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_registry.h" |
+ |
+#include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condition.h" |
+#include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rule.h" |
+#include "net/url_request/url_request.h" |
+ |
+namespace extensions { |
+namespace declarative_webrequest { |
+ |
+WebRequestRulesRegistry::WebRequestRulesRegistry() {} |
+ |
+WebRequestRulesRegistry::~WebRequestRulesRegistry() {} |
+ |
+std::set<WebRequestRulesRegistry::GlobalRuleId> |
+WebRequestRulesRegistry::GetMatches(net::URLRequest* request) { |
+ std::set<WebRequestRulesRegistry::GlobalRuleId> result; |
+ |
+ // Figure out for which rules, the URL match conditions were fulfilled. |
+ // 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.
|
+ // First filter by URLs. |
+ 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.
|
+ UrlMatches url_matches = url_matcher_.MatchURL(request->url()); |
+ |
+ // Then filter by remaining conditions. |
+ for (UrlMatches::iterator url_match = url_matches.begin(); |
+ 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.
|
+ |
+ RuleTriggers::iterator rule_trigger = rule_triggers_.find(*url_match); |
+ CHECK(rule_trigger != rule_triggers_.end()); |
+ |
+ WebRequestRule* rule = rule_trigger->second; |
+ 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.
|
+ DCHECK(rule_ids_.find(rule) != rule_ids_.end()); |
+ result.insert(rule_ids_[rule]); |
+ } |
+ } |
+ |
+ return result; |
+} |
+ |
+std::string WebRequestRulesRegistry::AddRulesImpl( |
+ const std::string& extension_id, |
+ const std::vector<linked_ptr<RulesRegistry::Rule> >& rules) { |
+ std::string error; |
+ RulesMap new_webrequest_rules; |
+ |
+ typedef std::vector<linked_ptr<RulesRegistry::Rule> > RulesVector; |
+ for (RulesVector::const_iterator rule = rules.begin(); rule != rules.end(); |
+ ++rule) { |
+ GlobalRuleId rule_id(extension_id, *(*rule)->id); |
+ DCHECK(webrequest_rules_.find(rule_id) == webrequest_rules_.end()); |
+ |
+ scoped_ptr<WebRequestRule> webrequest_rule( |
+ WebRequestRuleFactory::CreateRule(url_matcher_.condition_factory(), |
+ *rule, &error)); |
+ if (!error.empty()) { |
+ // We don't return here, because we want to clear temporary |
+ // condition sets in the url_matcher_. |
+ break; |
+ } |
+ |
+ new_webrequest_rules.insert( |
+ make_pair(rule_id, make_linked_ptr(webrequest_rule.release()))); |
+ } |
+ |
+ if (!error.empty()) { |
+ // Clean up temporary condition sets created during rule creation. |
+ url_matcher_.ClearUnusedConditionSets(); |
+ return error; |
+ } |
+ |
+ // Wohoo, everything worked fine. |
+ webrequest_rules_.insert(new_webrequest_rules.begin(), |
+ new_webrequest_rules.end()); |
+ |
+ // Build mapping of rule ID pointer to GlobalRuleId. |
+ for (RulesMap::iterator i = new_webrequest_rules.begin(); |
+ i != new_webrequest_rules.end(); ++i) { |
+ rule_ids_[i->second.get()] = i->first; |
+ } |
+ |
+ // Create the triggers. |
+ for (RulesMap::iterator i = new_webrequest_rules.begin(); |
+ i != new_webrequest_rules.end(); ++i) { |
+ std::vector<URLMatcherConditionSet> condition_sets; |
+ const WebRequestConditionCollection& conditions_collection = |
+ i->second->conditions_collection(); |
+ conditions_collection.AppendURLMatcherConditionSets(&condition_sets); |
+ for (std::vector<URLMatcherConditionSet>::iterator j = |
+ condition_sets.begin(); j != condition_sets.end(); ++j) { |
+ 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.
|
+ } |
+ } |
+ |
+ // Register url patterns in url_matcher_. |
+ std::vector<URLMatcherConditionSet> all_new_condition_sets; |
+ for (RulesMap::iterator i = new_webrequest_rules.begin(); |
+ i != new_webrequest_rules.end(); ++i) { |
+ i->second->conditions_collection().AppendURLMatcherConditionSets( |
+ &all_new_condition_sets); |
+ } |
+ url_matcher_.AddConditionSets(all_new_condition_sets); |
+ |
+ return ""; |
+} |
+ |
+std::string WebRequestRulesRegistry::RemoveRulesImpl( |
+ const std::string& extension_id, |
+ const std::vector<std::string>& rule_identifiers) { |
+ return "TODO"; |
+} |
+ |
+std::string WebRequestRulesRegistry::RemoveAllRulesImpl( |
+ const std::string& extension_id) { |
+ return "TODO"; |
+} |
+ |
+content::BrowserThread::ID WebRequestRulesRegistry::GetOwnerThread() const { |
+ return content::BrowserThread::IO; |
+} |
+ |
+} // namespace declarative_webrequest |
+} // namespace extensions |