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/rules_registry_with_cache.h" | 5 #include "chrome/browser/extensions/api/declarative/rules_registry_with_cache.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/stringprintf.h" | 8 #include "base/stringprintf.h" |
9 | 9 |
10 namespace { | 10 namespace { |
(...skipping 11 matching lines...) Expand all Loading... |
22 | 22 |
23 std::string RulesRegistryWithCache::AddRules( | 23 std::string RulesRegistryWithCache::AddRules( |
24 const std::string& extension_id, | 24 const std::string& extension_id, |
25 const std::vector<linked_ptr<Rule> >& rules) { | 25 const std::vector<linked_ptr<Rule> >& rules) { |
26 DCHECK(content::BrowserThread::CurrentlyOn(GetOwnerThread())); | 26 DCHECK(content::BrowserThread::CurrentlyOn(GetOwnerThread())); |
27 | 27 |
28 // Verify that all rule IDs are new. | 28 // Verify that all rule IDs are new. |
29 for (std::vector<linked_ptr<Rule> >::const_iterator i = | 29 for (std::vector<linked_ptr<Rule> >::const_iterator i = |
30 rules.begin(); i != rules.end(); ++i) { | 30 rules.begin(); i != rules.end(); ++i) { |
31 const RuleId& rule_id = *((*i)->id); | 31 const RuleId& rule_id = *((*i)->id); |
32 RulesDictionaryKey key = make_pair(extension_id, rule_id); | 32 RulesDictionaryKey key(extension_id, rule_id); |
33 if (rules_.find(key) != rules_.end()) | 33 if (rules_.find(key) != rules_.end()) |
34 return StringPrintf(kDuplicateRuleId, rule_id.c_str()); | 34 return StringPrintf(kDuplicateRuleId, rule_id.c_str()); |
35 } | 35 } |
36 | 36 |
37 std::string error = AddRulesImpl(extension_id, rules); | 37 std::string error = AddRulesImpl(extension_id, rules); |
38 | 38 |
39 if (!error.empty()) | 39 if (!error.empty()) |
40 return error; | 40 return error; |
41 | 41 |
42 // Commit all rules into |rules_| on success. | 42 // Commit all rules into |rules_| on success. |
43 for (std::vector<linked_ptr<Rule> >::const_iterator i = | 43 for (std::vector<linked_ptr<Rule> >::const_iterator i = |
44 rules.begin(); i != rules.end(); ++i) { | 44 rules.begin(); i != rules.end(); ++i) { |
45 const RuleId& rule_id = *((*i)->id); | 45 const RuleId& rule_id = *((*i)->id); |
46 RulesDictionaryKey key = make_pair(extension_id, rule_id); | 46 RulesDictionaryKey key(extension_id, rule_id); |
47 rules_[key] = *i; | 47 rules_[key] = *i; |
48 } | 48 } |
49 return kSuccess; | 49 return kSuccess; |
50 } | 50 } |
51 | 51 |
52 std::string RulesRegistryWithCache::RemoveRules( | 52 std::string RulesRegistryWithCache::RemoveRules( |
53 const std::string& extension_id, | 53 const std::string& extension_id, |
54 const std::vector<std::string>& rule_identifiers) { | 54 const std::vector<std::string>& rule_identifiers) { |
55 DCHECK(content::BrowserThread::CurrentlyOn(GetOwnerThread())); | 55 DCHECK(content::BrowserThread::CurrentlyOn(GetOwnerThread())); |
56 | 56 |
57 std::string error = RemoveRulesImpl(extension_id, rule_identifiers); | 57 std::string error = RemoveRulesImpl(extension_id, rule_identifiers); |
58 | 58 |
59 if (!error.empty()) | 59 if (!error.empty()) |
60 return error; | 60 return error; |
61 | 61 |
62 // Commit removal of rules from |rules_| on success. | 62 // Commit removal of rules from |rules_| on success. |
63 for (std::vector<std::string>::const_iterator i = | 63 for (std::vector<std::string>::const_iterator i = |
64 rule_identifiers.begin(); i != rule_identifiers.end(); ++i) { | 64 rule_identifiers.begin(); i != rule_identifiers.end(); ++i) { |
65 RulesDictionaryKey lookup_key = make_pair(extension_id, *i); | 65 RulesDictionaryKey lookup_key(extension_id, *i); |
66 rules_.erase(lookup_key); | 66 rules_.erase(lookup_key); |
67 } | 67 } |
68 return kSuccess; | 68 return kSuccess; |
69 } | 69 } |
70 | 70 |
71 std::string RulesRegistryWithCache::RemoveAllRules( | 71 std::string RulesRegistryWithCache::RemoveAllRules( |
72 const std::string& extension_id) { | 72 const std::string& extension_id) { |
73 DCHECK(content::BrowserThread::CurrentlyOn(GetOwnerThread())); | 73 DCHECK(content::BrowserThread::CurrentlyOn(GetOwnerThread())); |
74 | 74 |
75 std::string error = RemoveAllRulesImpl(extension_id); | 75 std::string error = RemoveAllRulesImpl(extension_id); |
(...skipping 13 matching lines...) Expand all Loading... |
89 } | 89 } |
90 | 90 |
91 std::string RulesRegistryWithCache::GetRules( | 91 std::string RulesRegistryWithCache::GetRules( |
92 const std::string& extension_id, | 92 const std::string& extension_id, |
93 const std::vector<std::string>& rule_identifiers, | 93 const std::vector<std::string>& rule_identifiers, |
94 std::vector<linked_ptr<RulesRegistry::Rule> >* out) { | 94 std::vector<linked_ptr<RulesRegistry::Rule> >* out) { |
95 DCHECK(content::BrowserThread::CurrentlyOn(GetOwnerThread())); | 95 DCHECK(content::BrowserThread::CurrentlyOn(GetOwnerThread())); |
96 | 96 |
97 for (std::vector<std::string>::const_iterator i = rule_identifiers.begin(); | 97 for (std::vector<std::string>::const_iterator i = rule_identifiers.begin(); |
98 i != rule_identifiers.end(); ++i) { | 98 i != rule_identifiers.end(); ++i) { |
99 RulesDictionaryKey lookup_key = make_pair(extension_id, *i); | 99 RulesDictionaryKey lookup_key(extension_id, *i); |
100 RulesDictionary::iterator entry = rules_.find(lookup_key); | 100 RulesDictionary::iterator entry = rules_.find(lookup_key); |
101 if (entry != rules_.end()) | 101 if (entry != rules_.end()) |
102 out->push_back(entry->second); | 102 out->push_back(entry->second); |
103 } | 103 } |
104 return kSuccess; | 104 return kSuccess; |
105 } | 105 } |
106 | 106 |
107 std::string RulesRegistryWithCache::GetAllRules( | 107 std::string RulesRegistryWithCache::GetAllRules( |
108 const std::string& extension_id, | 108 const std::string& extension_id, |
109 std::vector<linked_ptr<RulesRegistry::Rule> >* out) { | 109 std::vector<linked_ptr<RulesRegistry::Rule> >* out) { |
(...skipping 10 matching lines...) Expand all Loading... |
120 | 120 |
121 void RulesRegistryWithCache::OnExtensionUnloaded( | 121 void RulesRegistryWithCache::OnExtensionUnloaded( |
122 const std::string& extension_id) { | 122 const std::string& extension_id) { |
123 DCHECK(content::BrowserThread::CurrentlyOn(GetOwnerThread())); | 123 DCHECK(content::BrowserThread::CurrentlyOn(GetOwnerThread())); |
124 std::string error = RemoveAllRules(extension_id); | 124 std::string error = RemoveAllRules(extension_id); |
125 if (!error.empty()) | 125 if (!error.empty()) |
126 LOG(ERROR) << error; | 126 LOG(ERROR) << error; |
127 } | 127 } |
128 | 128 |
129 } // namespace extensions | 129 } // namespace extensions |
OLD | NEW |