Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(217)

Side by Side Diff: chrome/browser/extensions/api/declarative/initializing_rules_registry.cc

Issue 9422003: Migrate Declarative API bindings to new JSON objects generated by JSON compiler (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix memory leaks Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/initializing_rules_registry. h" 5 #include "chrome/browser/extensions/api/declarative/initializing_rules_registry. h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/stringprintf.h" 8 #include "base/stringprintf.h"
9 #include "base/values.h" 9 #include "base/values.h"
10 #include "chrome/browser/extensions/api/declarative/declarative_api_constants.h"
11
12 namespace keys = extensions::declarative_api_constants;
13 10
14 namespace { 11 namespace {
15 std::string ToId(int identifier) { 12 std::string ToId(int identifier) {
16 return StringPrintf("_%d_", identifier); 13 return StringPrintf("_%d_", identifier);
17 } 14 }
18 } // namespace 15 } // namespace
19 16
20 namespace extensions { 17 namespace extensions {
21 18
22 InitializingRulesRegistry::InitializingRulesRegistry( 19 InitializingRulesRegistry::InitializingRulesRegistry(
23 scoped_ptr<RulesRegistry> delegate) 20 scoped_refptr<RulesRegistry> delegate)
24 : delegate_(delegate.Pass()), 21 : delegate_(delegate),
25 last_generated_rule_identifier_id_(0) { 22 last_generated_rule_identifier_id_(0) {
26 } 23 }
27 24
28 InitializingRulesRegistry::~InitializingRulesRegistry() {} 25 InitializingRulesRegistry::~InitializingRulesRegistry() {}
29 26
30 std::string InitializingRulesRegistry::AddRules( 27 std::string InitializingRulesRegistry::AddRules(
31 const std::string& extension_id, 28 const std::string& extension_id,
32 const std::vector<DictionaryValue*>& rules) { 29 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules) {
33 std::string error = CheckAndFillInOptionalRules(extension_id, rules); 30 std::string error = CheckAndFillInOptionalRules(extension_id, rules);
34 if (!error.empty()) 31 if (!error.empty())
35 return error; 32 return error;
36 FillInOptionalPriorities(rules); 33 FillInOptionalPriorities(rules);
37 return delegate_->AddRules(extension_id, rules); 34 return delegate_->AddRules(extension_id, rules);
38 } 35 }
39 36
40 std::string InitializingRulesRegistry::RemoveRules( 37 std::string InitializingRulesRegistry::RemoveRules(
41 const std::string& extension_id, 38 const std::string& extension_id,
42 const std::vector<std::string>& rule_identifiers) { 39 const std::vector<std::string>& rule_identifiers) {
43 std::string error = delegate_->RemoveRules(extension_id, rule_identifiers); 40 std::string error = delegate_->RemoveRules(extension_id, rule_identifiers);
44 if (!error.empty()) 41 if (!error.empty())
45 return error; 42 return error;
46 RemoveUsedRuleIdentifiers(extension_id, rule_identifiers); 43 RemoveUsedRuleIdentifiers(extension_id, rule_identifiers);
47 return ""; 44 return "";
48 } 45 }
49 46
50 std::string InitializingRulesRegistry::RemoveAllRules( 47 std::string InitializingRulesRegistry::RemoveAllRules(
51 const std::string& extension_id) { 48 const std::string& extension_id) {
52 std::string error = delegate_->RemoveAllRules(extension_id); 49 std::string error = delegate_->RemoveAllRules(extension_id);
53 if (!error.empty()) 50 if (!error.empty())
54 return error; 51 return error;
55 RemoveAllUsedRuleIdentifiers(extension_id); 52 RemoveAllUsedRuleIdentifiers(extension_id);
56 return ""; 53 return "";
57 } 54 }
58 55
59 std::string InitializingRulesRegistry::GetRules( 56 std::string InitializingRulesRegistry::GetRules(
60 const std::string& extension_id, 57 const std::string& extension_id,
61 const std::vector<std::string>& rule_identifiers, 58 const std::vector<std::string>& rule_identifiers,
62 std::vector<DictionaryValue*>* out) { 59 std::vector<linked_ptr<RulesRegistry::Rule> >* out) {
63 return delegate_->GetRules(extension_id, rule_identifiers, out); 60 return delegate_->GetRules(extension_id, rule_identifiers, out);
64 } 61 }
65 62
66 std::string InitializingRulesRegistry::GetAllRules( 63 std::string InitializingRulesRegistry::GetAllRules(
67 const std::string& extension_id, 64 const std::string& extension_id,
68 std::vector<DictionaryValue*>* out) { 65 std::vector<linked_ptr<RulesRegistry::Rule> >* out) {
69 return delegate_->GetAllRules(extension_id, out); 66 return delegate_->GetAllRules(extension_id, out);
70 } 67 }
71 68
72 void InitializingRulesRegistry::OnExtensionUnloaded( 69 void InitializingRulesRegistry::OnExtensionUnloaded(
73 const std::string& extension_id) { 70 const std::string& extension_id) {
74 delegate_->OnExtensionUnloaded(extension_id); 71 delegate_->OnExtensionUnloaded(extension_id);
75 } 72 }
76 73
74 content::BrowserThread::ID InitializingRulesRegistry::GetOwnerThread() const {
75 return delegate_->GetOwnerThread();
76 }
77
77 bool InitializingRulesRegistry::IsUniqueId( 78 bool InitializingRulesRegistry::IsUniqueId(
78 const std::string& extension_id, 79 const std::string& extension_id,
79 const std::string& rule_id) const { 80 const std::string& rule_id) const {
80 RuleIdentifiersMap::const_iterator identifiers = 81 RuleIdentifiersMap::const_iterator identifiers =
81 used_rule_identifiers_.find(extension_id); 82 used_rule_identifiers_.find(extension_id);
82 if (identifiers == used_rule_identifiers_.end()) 83 if (identifiers == used_rule_identifiers_.end())
83 return true; 84 return true;
84 return identifiers->second.find(rule_id) == identifiers->second.end(); 85 return identifiers->second.find(rule_id) == identifiers->second.end();
85 } 86 }
86 87
87 std::string InitializingRulesRegistry::GenerateUniqueId( 88 std::string InitializingRulesRegistry::GenerateUniqueId(
88 std::string extension_id) { 89 std::string extension_id) {
89 while (!IsUniqueId(extension_id, ToId(last_generated_rule_identifier_id_))) 90 while (!IsUniqueId(extension_id, ToId(last_generated_rule_identifier_id_)))
90 ++last_generated_rule_identifier_id_; 91 ++last_generated_rule_identifier_id_;
91 std::string new_id = ToId(last_generated_rule_identifier_id_); 92 return ToId(last_generated_rule_identifier_id_);
92 used_rule_identifiers_[extension_id].insert(new_id);
93 return new_id;
94 } 93 }
95 94
96 void InitializingRulesRegistry::RemoveUsedRuleIdentifiers( 95 void InitializingRulesRegistry::RemoveUsedRuleIdentifiers(
97 const std::string& extension_id, 96 const std::string& extension_id,
98 const std::vector<std::string>& identifiers) { 97 const std::vector<std::string>& identifiers) {
99 std::vector<std::string>::const_iterator i; 98 std::vector<std::string>::const_iterator i;
100 for (i = identifiers.begin(); i != identifiers.end(); ++i) 99 for (i = identifiers.begin(); i != identifiers.end(); ++i)
101 used_rule_identifiers_[extension_id].erase(*i); 100 used_rule_identifiers_[extension_id].erase(*i);
102 } 101 }
103 102
104 void InitializingRulesRegistry::RemoveAllUsedRuleIdentifiers( 103 void InitializingRulesRegistry::RemoveAllUsedRuleIdentifiers(
105 const std::string& extension_id) { 104 const std::string& extension_id) {
106 used_rule_identifiers_.erase(extension_id); 105 used_rule_identifiers_.erase(extension_id);
107 } 106 }
108 107
109 std::string InitializingRulesRegistry::CheckAndFillInOptionalRules( 108 std::string InitializingRulesRegistry::CheckAndFillInOptionalRules(
110 const std::string& extension_id, 109 const std::string& extension_id,
111 const std::vector<DictionaryValue*>& rules) { 110 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules) {
112 // IDs we have inserted, in case we need to rollback this operation. 111 // IDs we have inserted, in case we need to rollback this operation.
113 std::vector<std::string> rollback_log; 112 std::vector<std::string> rollback_log;
114 113
115 // First we insert all rules with existing identifier, so that generated 114 // First we insert all rules with existing identifier, so that generated
116 // identifiers cannot collide with identifiers passed by the caller. 115 // identifiers cannot collide with identifiers passed by the caller.
117 for (std::vector<DictionaryValue*>::const_iterator i = 116 for (std::vector<linked_ptr<RulesRegistry::Rule> >::const_iterator i =
118 rules.begin(); i != rules.end(); ++i) { 117 rules.begin(); i != rules.end(); ++i) {
119 DictionaryValue* rule = *i; 118 RulesRegistry::Rule* rule = i->get();
120 if (rule->HasKey(keys::kId)) { 119 if (rule->id.get()) {
121 std::string id; 120 std::string id = *(rule->id);
122 CHECK(rule->GetString(keys::kId, &id));
123 if (!IsUniqueId(extension_id, id)) { 121 if (!IsUniqueId(extension_id, id)) {
124 RemoveUsedRuleIdentifiers(extension_id, rollback_log); 122 RemoveUsedRuleIdentifiers(extension_id, rollback_log);
125 return "Id " + id + " was used multiple times."; 123 return "Id " + id + " was used multiple times.";
126 } 124 }
127 used_rule_identifiers_[extension_id].insert(id); 125 used_rule_identifiers_[extension_id].insert(id);
128 } 126 }
129 } 127 }
130 // Now we generate IDs in case they were not specificed in the rules. This 128 // Now we generate IDs in case they were not specificed in the rules. This
131 // cannot fail so we do not need to keep track of a rollback log. 129 // cannot fail so we do not need to keep track of a rollback log.
132 for (std::vector<DictionaryValue*>::const_iterator i = 130 for (std::vector<linked_ptr<RulesRegistry::Rule> >::const_iterator i =
133 rules.begin(); i != rules.end(); ++i) { 131 rules.begin(); i != rules.end(); ++i) {
134 DictionaryValue* rule = *i; 132 RulesRegistry::Rule* rule = i->get();
135 if (!rule->HasKey(keys::kId)) 133 if (!rule->id.get()) {
136 rule->SetString(keys::kId, GenerateUniqueId(extension_id)); 134 rule->id.reset(new std::string(GenerateUniqueId(extension_id)));
135 used_rule_identifiers_[extension_id].insert(*(rule->id));
136 }
137 } 137 }
138 return ""; 138 return "";
139 } 139 }
140 140
141 void InitializingRulesRegistry::FillInOptionalPriorities( 141 void InitializingRulesRegistry::FillInOptionalPriorities(
142 const std::vector<DictionaryValue*>& rules) { 142 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules) {
143 std::vector<DictionaryValue*>::const_iterator i; 143 std::vector<linked_ptr<RulesRegistry::Rule> >::const_iterator i;
144 for (i = rules.begin(); i != rules.end(); ++i) { 144 for (i = rules.begin(); i != rules.end(); ++i) {
145 DictionaryValue* rule = *i; 145 if (!(*i)->priority.get())
146 if (!rule->HasKey(keys::kPriority)) 146 (*i)->priority.reset(new int(DEFAULT_PRIORITY));
147 rule->SetInteger(keys::kPriority, 100);
148 } 147 }
149 } 148 }
150 149
151 } // namespace extensions 150 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698