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 "net/url_request/url_request.h" |
| 9 |
| 10 namespace extensions { |
| 11 |
| 12 WebRequestRulesRegistry::WebRequestRulesRegistry() {} |
| 13 |
| 14 WebRequestRulesRegistry::~WebRequestRulesRegistry() {} |
| 15 |
| 16 std::set<WebRequestRule::GlobalRuleId> |
| 17 WebRequestRulesRegistry::GetMatches(net::URLRequest* request) { |
| 18 std::set<WebRequestRule::GlobalRuleId> result; |
| 19 |
| 20 // Figure out for which rules the URL match conditions were fulfilled. |
| 21 typedef std::set<URLMatcherConditionSet::ID> URLMatches; |
| 22 URLMatches url_matches = url_matcher_.MatchURL(request->url()); |
| 23 |
| 24 // Then we need to check for each of these, whether the other |
| 25 // WebRequestConditionAttributes are also fulfilled. |
| 26 for (URLMatches::iterator url_match = url_matches.begin(); |
| 27 url_match != url_matches.end(); ++url_match) { |
| 28 RuleTriggers::iterator rule_trigger = rule_triggers_.find(*url_match); |
| 29 CHECK(rule_trigger != rule_triggers_.end()); |
| 30 |
| 31 WebRequestRule* rule = rule_trigger->second; |
| 32 if (rule->conditions().IsFulfilled(*url_match, request)) |
| 33 result.insert(rule->id()); |
| 34 } |
| 35 |
| 36 return result; |
| 37 } |
| 38 |
| 39 std::string WebRequestRulesRegistry::AddRulesImpl( |
| 40 const std::string& extension_id, |
| 41 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules) { |
| 42 std::string error; |
| 43 RulesMap new_webrequest_rules; |
| 44 |
| 45 for (std::vector<linked_ptr<RulesRegistry::Rule> >::const_iterator rule = |
| 46 rules.begin(); rule != rules.end(); ++rule) { |
| 47 WebRequestRule::GlobalRuleId rule_id(extension_id, *(*rule)->id); |
| 48 DCHECK(webrequest_rules_.find(rule_id) == webrequest_rules_.end()); |
| 49 |
| 50 scoped_ptr<WebRequestRule> webrequest_rule( |
| 51 WebRequestRule::Create(url_matcher_.condition_factory(), extension_id, |
| 52 *rule, &error)); |
| 53 if (!error.empty()) { |
| 54 // We don't return here, because we want to clear temporary |
| 55 // condition sets in the url_matcher_. |
| 56 break; |
| 57 } |
| 58 |
| 59 new_webrequest_rules[rule_id] = make_linked_ptr(webrequest_rule.release()); |
| 60 } |
| 61 |
| 62 if (!error.empty()) { |
| 63 // Clean up temporary condition sets created during rule creation. |
| 64 url_matcher_.ClearUnusedConditionSets(); |
| 65 return error; |
| 66 } |
| 67 |
| 68 // Wohoo, everything worked fine. |
| 69 webrequest_rules_.insert(new_webrequest_rules.begin(), |
| 70 new_webrequest_rules.end()); |
| 71 |
| 72 // Create the triggers. |
| 73 for (RulesMap::iterator i = new_webrequest_rules.begin(); |
| 74 i != new_webrequest_rules.end(); ++i) { |
| 75 std::vector<URLMatcherConditionSet> url_condition_sets; |
| 76 const WebRequestConditionSet& conditions = i->second->conditions(); |
| 77 conditions.GetURLMatcherConditionSets(&url_condition_sets); |
| 78 for (std::vector<URLMatcherConditionSet>::iterator j = |
| 79 url_condition_sets.begin(); j != url_condition_sets.end(); ++j) { |
| 80 rule_triggers_[j->id()] = i->second.get(); |
| 81 } |
| 82 } |
| 83 |
| 84 // Register url patterns in url_matcher_. |
| 85 std::vector<URLMatcherConditionSet> all_new_condition_sets; |
| 86 for (RulesMap::iterator i = new_webrequest_rules.begin(); |
| 87 i != new_webrequest_rules.end(); ++i) { |
| 88 i->second->conditions().GetURLMatcherConditionSets(&all_new_condition_sets); |
| 89 } |
| 90 url_matcher_.AddConditionSets(all_new_condition_sets); |
| 91 |
| 92 return ""; |
| 93 } |
| 94 |
| 95 std::string WebRequestRulesRegistry::RemoveRulesImpl( |
| 96 const std::string& extension_id, |
| 97 const std::vector<std::string>& rule_identifiers) { |
| 98 return "TODO"; |
| 99 } |
| 100 |
| 101 std::string WebRequestRulesRegistry::RemoveAllRulesImpl( |
| 102 const std::string& extension_id) { |
| 103 return "TODO"; |
| 104 } |
| 105 |
| 106 content::BrowserThread::ID WebRequestRulesRegistry::GetOwnerThread() const { |
| 107 return content::BrowserThread::IO; |
| 108 } |
| 109 |
| 110 } // namespace extensions |
OLD | NEW |