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

Side by Side Diff: chrome/browser/extensions/api/declarative/declarative_api.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: Fixed function naming 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
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/declarative_api.h" 5 #include "chrome/browser/extensions/api/declarative/declarative_api.h"
6 6
7 #include "base/bind.h"
7 #include "base/values.h" 8 #include "base/values.h"
8 #include "chrome/browser/extensions/api/declarative/declarative_api_constants.h" 9 #include "chrome/browser/extensions/api/declarative/declarative_api_constants.h"
9 #include "chrome/browser/extensions/api/declarative/rules_registry.h" 10 #include "chrome/browser/extensions/api/declarative/rules_registry.h"
10 #include "chrome/browser/extensions/api/declarative/rules_registry_service.h" 11 #include "chrome/browser/extensions/api/declarative/rules_registry_service.h"
11 #include "chrome/browser/extensions/extension_service.h" 12 #include "chrome/browser/extensions/extension_service.h"
12 #include "chrome/browser/profiles/profile.h" 13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/common/extensions/api/experimental.declarative.h"
15 #include "content/public/browser/browser_thread.h"
13 16
14 namespace keys = extensions::declarative_api_constants; 17 namespace keys = extensions::declarative_api_constants;
15 18
19 using namespace extensions::api::experimental_declarative;
20
16 namespace { 21 namespace {
17 22
18 // Adds all entries from |list| to |out|. Assumes that all entries of |list| 23 // Adds all entries from |list| to |out|. Assumes that all entries of |list|
19 // are strings. Returns true if successful. 24 // are strings. Returns true if successful.
20 bool AddAllStringValues(ListValue* list, std::vector<std::string>* out) { 25 bool AddAllStringValues(ListValue* list, std::vector<std::string>* out) {
21 for (ListValue::iterator i = list->begin(); i != list->end(); ++i) { 26 for (ListValue::iterator i = list->begin(); i != list->end(); ++i) {
22 std::string value; 27 std::string value;
23 if (!(*i)->GetAsString(&value)) 28 if (!(*i)->GetAsString(&value))
24 return false; 29 return false;
25 out->push_back(value); 30 out->push_back(value);
26 } 31 }
27 return true; 32 return true;
28 } 33 }
29 34
30 } // namespace 35 } // namespace
31 36
32 namespace extensions { 37 namespace extensions {
33 38
34 bool AddRulesFunction::RunImpl() { 39 RulesFunction::RulesFunction() : rules_registry_(NULL) {}
40
41 bool RulesFunction::RunImpl() {
35 std::string event_name; 42 std::string event_name;
36 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name)); 43 EXTENSION_FUNCTION_VALIDATE(!args_->GetString(0, &event_name));
not at google - send to devlin 2012/02/18 02:59:01 Shame to have to do this, I suppose you have no ch
battre 2012/02/20 10:17:36 Obviously. Done.
37
38 ListValue* rules_list = NULL;
39 EXTENSION_FUNCTION_VALIDATE(args_->GetList(1, &rules_list));
40
41 std::vector<DictionaryValue*> rules;
42 for (ListValue::iterator i = rules_list->begin();
43 i != rules_list->end();
44 ++i) {
45 DictionaryValue* rule = NULL;
46 EXTENSION_FUNCTION_VALIDATE((*i)->GetAsDictionary(&rule));
47 rules.push_back(rule);
48 }
49 44
50 RulesRegistryService* rules_registry_service = 45 RulesRegistryService* rules_registry_service =
51 profile()->GetExtensionService()->GetRulesRegistryService(); 46 profile()->GetExtensionService()->GetRulesRegistryService();
52 RulesRegistry* rules_registry = 47 rules_registry_ = rules_registry_service->GetRulesRegistry(event_name);
53 rules_registry_service->GetRulesRegistry(event_name); 48 if (!rules_registry_) {
54 if (!rules_registry) {
55 error_ = keys::kInvalidEventName; 49 error_ = keys::kInvalidEventName;
56 return false; 50 return false;
57 } 51 }
not at google - send to devlin 2012/02/18 02:59:01 This (and other examples of the pattern) are the s
battre 2012/02/20 10:17:36 not quite. EXTENSION_FUNCTION_VALIDATE sets bad_me
not at google - send to devlin 2012/02/20 11:50:51 Ah crap I think I put this comment on the wrong bl
battre 2012/02/28 22:29:18 I wanted to do this change after landing http://co
58 52
59 error_ = rules_registry->AddRules(extension_id(), rules); 53 if (rules_registry_->GetOwnerThread() == content::BrowserThread::UI) {
not at google - send to devlin 2012/02/18 02:59:01 I think this would be more clearly expressed as c
battre 2012/02/20 10:17:36 Done.
60 if (!error_.empty()) 54 RunImplOnCorrectThread();
61 return false; 55 SendResponseOnUIThread();
62 56 } else {
63 result_.reset(rules_list->DeepCopy()); 57 content::BrowserThread::PostTaskAndReply(
64 return true; 58 rules_registry_->GetOwnerThread(), FROM_HERE,
65 } 59 base::Bind(&RulesFunction::RunImplOnCorrectThread, this),
66 60 base::Bind(&RulesFunction::SendResponseOnUIThread, this));
67 bool RemoveRulesFunction::RunImpl() {
68 std::string event_name;
69 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name));
70
71 Value* rule_identifiers = NULL;
72 EXTENSION_FUNCTION_VALIDATE(args_->Get(1, &rule_identifiers));
73
74 RulesRegistryService* rules_registry_service =
75 profile()->GetExtensionService()->GetRulesRegistryService();
76 RulesRegistry* rules_registry =
77 rules_registry_service->GetRulesRegistry(event_name);
78 if (!rules_registry) {
79 error_ = keys::kInvalidEventName;
80 return false;
81 } 61 }
82 62
83 switch (rule_identifiers->GetType()) {
84 case Value::TYPE_NULL:
85 error_ = rules_registry->RemoveAllRules(extension_id());
86 break;
87 case Value::TYPE_LIST: {
88 std::vector<std::string> rule_identifiers_list;
89 EXTENSION_FUNCTION_VALIDATE(
90 AddAllStringValues(static_cast<ListValue*>(rule_identifiers),
91 &rule_identifiers_list));
92 error_ = rules_registry->RemoveRules(extension_id(),
93 rule_identifiers_list);
94 break;
95 }
96 default:
97 error_ = keys::kInvalidDatatype;
98 break;
99 }
100 return error_.empty();
101 }
102
103 bool GetRulesFunction::RunImpl() {
104 std::string event_name;
105 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &event_name));
106
107 Value* rule_identifiers = NULL;
108 EXTENSION_FUNCTION_VALIDATE(args_->Get(1, &rule_identifiers));
109
110 RulesRegistryService* rules_registry_service =
111 profile()->GetExtensionService()->GetRulesRegistryService();
112 RulesRegistry* rules_registry =
113 rules_registry_service->GetRulesRegistry(event_name);
114 if (!rules_registry) {
115 error_ = keys::kInvalidEventName;
116 return false;
117 }
118
119 std::vector<DictionaryValue*> rules;
120 switch (rule_identifiers->GetType()) {
121 case Value::TYPE_NULL:
122 error_ = rules_registry->GetAllRules(extension_id(), &rules);
123 break;
124 case Value::TYPE_LIST: {
125 std::vector<std::string> rule_identifiers_list;
126 EXTENSION_FUNCTION_VALIDATE(
127 AddAllStringValues(static_cast<ListValue*>(rule_identifiers),
128 &rule_identifiers_list));
129 error_ = rules_registry->GetRules(extension_id(), rule_identifiers_list,
130 &rules);
131 break;
132 }
133 default:
134 error_ = keys::kInvalidDatatype;
135 break;
136 }
137
138 if (!error_.empty())
139 return false;
140
141 scoped_ptr<ListValue> result(new ListValue);
142 for (std::vector<DictionaryValue*>::iterator i = rules.begin();
143 i != rules.end(); ++i)
144 result->Append(*i);
145 result_.reset(result.release());
146
147 return true; 63 return true;
148 } 64 }
149 65
66 void RulesFunction::SendResponseOnUIThread() {
67 SendResponse(error_.empty() && !bad_message_);
68 }
69
70 void AddRulesFunction::RunImplOnCorrectThread() {
71 scoped_ptr<AddRules::Params> params(AddRules::Params::Create(*args_));
72 if (!params.get()) {
73 bad_message_ = true;
74 return;
75 }
76
77 error_ = rules_registry_->AddRules(extension_id(), params->rules);
78
79 if (error_.empty())
80 result_.reset(AddRules::Result::Create(params->rules));
81 }
82
83 void RemoveRulesFunction::RunImplOnCorrectThread() {
84 scoped_ptr<RemoveRules::Params> params(RemoveRules::Params::Create(*args_));
85 if (!params.get()) {
86 bad_message_ = true;
87 return;
88 }
89
90 if (params->rule_identifiers.get()) {
91 error_ = rules_registry_->RemoveRules(extension_id(),
92 *params->rule_identifiers);
93 } else {
94 error_ = rules_registry_->RemoveAllRules(extension_id());
95 }
96 }
97
98 void GetRulesFunction::RunImplOnCorrectThread() {
99 scoped_ptr<RemoveRules::Params> params(RemoveRules::Params::Create(*args_));
100 if (!params.get()) {
101 bad_message_ = true;
102 return;
103 }
104
105 std::vector<linked_ptr<Rule> > rules;
106 if (params->rule_identifiers.get()) {
107 error_ = rules_registry_->GetRules(extension_id(),
108 *params->rule_identifiers,
109 &rules);
110 } else {
111 error_ = rules_registry_->GetAllRules(extension_id(), &rules);
112 }
113
114 if (error_.empty())
115 result_.reset(GetRules::Result::Create(rules));
116 }
117
150 } // namespace extensions 118 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698