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

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

Issue 9315010: RulesRegistry for declarative APIs. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merged with ToT for trybot testing Created 8 years, 10 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
(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/initializing_rules_registry. h"
6
7 #include "base/logging.h"
8 #include "base/stl_util.h"
9 #include "base/string_number_conversions.h"
10 #include "chrome/browser/extensions/api/declarative/declarative_api_constants.h"
11 #include "chrome/browser/extensions/api/declarative/rule_identifier.h"
12
13 namespace keys = extensions::declarative_api_constants;
14
15 namespace {
16 std::string ToId(int identifier) {
17 return "_" + base::IntToString(identifier) + "_";
not at google - send to devlin 2012/02/07 03:25:10 I think c++ will automatically cast int -> std::st
battre 2012/02/07 18:45:33 No.
not at google - send to devlin 2012/02/07 23:54:57 Yeah, that's not what I meant though, just uninden
18 }
19 }
20
21 namespace extensions {
22
23 InitializingRulesRegistry::InitializingRulesRegistry(
24 scoped_ptr<RulesRegistry> delegate)
25 : delegate_(delegate.Pass()),
not at google - send to devlin 2012/02/07 03:25:10 I don't think you need the .Pass().
battre 2012/02/07 18:45:33 I do: error: 'scoped_ptr<C>::scoped_ptr(scoped_ptr
not at google - send to devlin 2012/02/07 23:54:57 cool, good to know.
26 last_generated_rule_identifier_id_(0) {
27 }
28
29 InitializingRulesRegistry::~InitializingRulesRegistry() {}
30
31 bool InitializingRulesRegistry::AddRules(
32 const std::string& extension_id,
33 const std::vector<DictionaryValue*>& rules,
34 std::string* error) {
35 if (!CheckAndFillInOptionalRules(extension_id, rules, error))
36 return false;
37 FillInOptionalPriorities(rules);
38 return delegate_->AddRules(extension_id, rules, error);
39 }
40
41 bool InitializingRulesRegistry::RemoveRules(
42 const std::string& extension_id,
43 const std::vector<std::string>& rule_identifiers,
44 std::string* error) {
45 if (!delegate_->RemoveRules(extension_id, rule_identifiers, error))
46 return false;
47 RemoveUsedRuleIdentifiers(extension_id, rule_identifiers);
48 return true;
49 }
50
51 void InitializingRulesRegistry::GetRules(
52 const std::string& extension_id,
53 const std::vector<std::string>& rule_identifiers,
54 std::vector<DictionaryValue*>* out) {
55 delegate_->GetRules(extension_id, rule_identifiers, out);
56 }
57
58 void InitializingRulesRegistry::OnExtensionUnloaded(
59 const std::string& extension_id) {
60 delegate_->OnExtensionUnloaded(extension_id);
61 }
62
63 bool InitializingRulesRegistry::IsUniqueId(const RuleIdentifier& id) const {
64 return used_rule_identifiers_.find(id) == used_rule_identifiers_.end();
65 }
66
67 std::string InitializingRulesRegistry::GenerateUniqueId(
68 std::string extension_id) {
69 while (
70 !IsUniqueId(
71 RuleIdentifier(extension_id,
72 ToId(last_generated_rule_identifier_id_)))) {
not at google - send to devlin 2012/02/07 03:25:10 indentation doesn't need to be this whitespacey?
battre 2012/02/07 18:45:33 Hm, I am not a big fan of arbitrary line breaks. B
73 ++last_generated_rule_identifier_id_;
74 }
75 RuleIdentifier new_id(extension_id,
76 ToId(last_generated_rule_identifier_id_));
77 used_rule_identifiers_.insert(new_id);
78 return new_id.rule_id();
79 }
80
81 void InitializingRulesRegistry::RemoveUsedRuleIdentifiers(
82 const std::string& extension_id,
83 const std::vector<std::string>& identifiers) {
84 std::vector<std::string>::const_iterator i;
85 for (i = identifiers.begin(); i != identifiers.end(); ++i)
86 used_rule_identifiers_.erase(RuleIdentifier(extension_id, *i));
87 }
88
89 bool InitializingRulesRegistry::CheckAndFillInOptionalRules(
90 const std::string& extension_id,
91 const std::vector<DictionaryValue*>& rules,
92 std::string* error) {
93 // IDs we have inserted, in case we need to rollback this operation.
94 std::vector<std::string> rollback_log;
95
96 std::vector<DictionaryValue*>::const_iterator i;
97
98 // First we insert all rules with existing identifier, so that generated
99 // identifiers cannot collide with identifiers passed by the caller.
100 for (i = rules.begin(); i != rules.end(); ++i) {
101 DictionaryValue* rule = *i;
102 if (rule->HasKey(keys::kId)) {
103 std::string id;
104 CHECK(rule->GetString(keys::kId, &id));
105 RuleIdentifier rule_identifier(extension_id, id);
106 if (!IsUniqueId(rule_identifier)) {
107 *error = "Id " + id + " was used multiple times.";
108 RemoveUsedRuleIdentifiers(extension_id, rollback_log);
109 return false;
110 }
111 used_rule_identifiers_.insert(rule_identifier);
112 }
113 }
114 // Now we generate IDs in case they were not specificed in the rules. This
115 // cannot fail so we do not need to keep track of a rollback log.
116 for (i = rules.begin(); i != rules.end(); ++i) {
117 DictionaryValue* rule = *i;
118 if (!rule->HasKey(keys::kId))
119 rule->SetString(keys::kId, GenerateUniqueId(extension_id));
120 }
121 return true;
122 }
123
124 void InitializingRulesRegistry::FillInOptionalPriorities(
125 const std::vector<DictionaryValue*>& rules) {
126 std::vector<DictionaryValue*>::const_iterator i;
127 for (i = rules.begin(); i != rules.end(); ++i) {
128 DictionaryValue* rule = *i;
129 if (!rule->HasKey(keys::kPriority))
130 rule->SetInteger(keys::kPriority, 100);
131 }
132 }
133
134 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698