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

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

Issue 9315010: RulesRegistry for declarative APIs. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed remaining comments 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/test_rules_registry.h"
6
7 #include <algorithm>
8
9 #include "base/logging.h"
10 #include "base/stl_util.h"
11 #include "chrome/browser/extensions/api/declarative/declarative_api_constants.h"
12
13 namespace keys = extensions::declarative_api_constants;
14
15 namespace {
16
17 std::string GetRuleId(DictionaryValue* rule) {
18 std::string rule_id;
19 CHECK(rule->GetString(keys::kId, &rule_id));
20 return rule_id;
21 }
22
23 } // namespace
24
25 namespace extensions {
26
27 TestRulesRegistry::TestRulesRegistry() {
28 }
29
30 TestRulesRegistry::~TestRulesRegistry() {
31 STLDeleteValues(&rules_);
32 }
33
34 bool TestRulesRegistry::AddRules(
35 const std::string& extension_id,
36 const std::vector<DictionaryValue*>& rules,
37 std::string* error) {
38 for (std::vector<DictionaryValue*>::const_iterator i =
39 rules.begin(); i != rules.end(); ++i) {
40 std::string rule_id = GetRuleId(*i);
41 // TODO: relax this (check first and abort with returning false).
42 CHECK(rules_.find(rule_id) == rules_.end());
43 rules_[rule_id] = (*i)->DeepCopy();
44 }
45 return true;
46 }
47
48 bool TestRulesRegistry::RemoveRules(
49 const std::string& extension_id,
50 const std::vector<std::string>& rule_identifiers,
51 std::string* error) {
52 if (rule_identifiers.empty()) {
53 STLDeleteValues(&rules_);
not at google - send to devlin 2012/02/07 03:25:10 See comment in the API file I made about this (app
battre 2012/02/07 18:45:33 Done.
54 } else {
55 for (std::vector<std::string>::const_iterator i =
56 rule_identifiers.begin(); i != rule_identifiers.end(); ++i) {
57 std::map<std::string, DictionaryValue*>::iterator entry = rules_.find(*i);
58 // TODO: relax this (check first and abort with returning false).
59 CHECK(entry != rules_.end());
60 delete entry->second;
61 rules_.erase(entry);
62 }
63 }
64 return true;
65 }
66
67 void TestRulesRegistry::GetRules(
68 const std::string& extension_id,
69 const std::vector<std::string>& rule_identifiers,
70 std::vector<DictionaryValue*>* out) {
71 if (rule_identifiers.empty()) {
72 for (std::map<std::string, DictionaryValue*>::const_iterator i =
73 rules_.begin(); i != rules_.end(); ++i)
74 out->push_back(i->second->DeepCopy());
75 } else {
76 std::vector<std::string>::const_iterator i;
not at google - send to devlin 2012/02/07 03:25:10 same thing with the iterator here
battre 2012/02/07 18:45:33 Done.
77 for (i = rule_identifiers.begin(); i != rule_identifiers.end(); ++i) {
78 std::map<std::string, DictionaryValue*>::iterator entry = rules_.find(*i);
79 // TODO: relax this (check first and abort with returning false).
80 CHECK(entry != rules_.end());
81 out->push_back(entry->second->DeepCopy());
82 }
83 }
84 }
85
86 void TestRulesRegistry::OnExtensionUnloaded(const std::string& extension_id) {
87 std::vector<std::string> no_rule_identifiers;
88 std::string error;
89 if (!RemoveRules(extension_id, no_rule_identifiers, &error))
90 LOG(ERROR) << error;
91 }
92
93 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698