OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "extensions/renderer/api_binding.h" | 5 #include "extensions/renderer/api_binding.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 weak_factory_(this) { | 167 weak_factory_(this) { |
168 // TODO(devlin): It might make sense to instantiate the object_template_ | 168 // TODO(devlin): It might make sense to instantiate the object_template_ |
169 // directly here, which would avoid the need to hold on to | 169 // directly here, which would avoid the need to hold on to |
170 // |property_definitions_| and |enums_|. However, there are *some* cases where | 170 // |property_definitions_| and |enums_|. However, there are *some* cases where |
171 // we don't immediately stamp out an API from the template following | 171 // we don't immediately stamp out an API from the template following |
172 // construction. | 172 // construction. |
173 | 173 |
174 if (function_definitions) { | 174 if (function_definitions) { |
175 for (const auto& func : *function_definitions) { | 175 for (const auto& func : *function_definitions) { |
176 const base::DictionaryValue* func_dict = nullptr; | 176 const base::DictionaryValue* func_dict = nullptr; |
177 CHECK(func->GetAsDictionary(&func_dict)); | 177 CHECK(func.GetAsDictionary(&func_dict)); |
178 std::string name; | 178 std::string name; |
179 CHECK(func_dict->GetString("name", &name)); | 179 CHECK(func_dict->GetString("name", &name)); |
180 | 180 |
181 const base::ListValue* params = nullptr; | 181 const base::ListValue* params = nullptr; |
182 CHECK(func_dict->GetList("parameters", ¶ms)); | 182 CHECK(func_dict->GetList("parameters", ¶ms)); |
183 auto signature = base::MakeUnique<APISignature>(*params); | 183 auto signature = base::MakeUnique<APISignature>(*params); |
184 std::string full_name = | 184 std::string full_name = |
185 base::StringPrintf("%s.%s", api_name_.c_str(), name.c_str()); | 185 base::StringPrintf("%s.%s", api_name_.c_str(), name.c_str()); |
186 methods_[name] = base::MakeUnique<MethodData>(full_name, signature.get()); | 186 methods_[name] = base::MakeUnique<MethodData>(full_name, signature.get()); |
187 type_refs->AddAPIMethodSignature(full_name, std::move(signature)); | 187 type_refs->AddAPIMethodSignature(full_name, std::move(signature)); |
188 } | 188 } |
189 } | 189 } |
190 | 190 |
191 if (type_definitions) { | 191 if (type_definitions) { |
192 for (const auto& type : *type_definitions) { | 192 for (const auto& type : *type_definitions) { |
193 const base::DictionaryValue* type_dict = nullptr; | 193 const base::DictionaryValue* type_dict = nullptr; |
194 CHECK(type->GetAsDictionary(&type_dict)); | 194 CHECK(type.GetAsDictionary(&type_dict)); |
195 std::string id; | 195 std::string id; |
196 CHECK(type_dict->GetString("id", &id)); | 196 CHECK(type_dict->GetString("id", &id)); |
197 auto argument_spec = base::MakeUnique<ArgumentSpec>(*type_dict); | 197 auto argument_spec = base::MakeUnique<ArgumentSpec>(*type_dict); |
198 const std::set<std::string>& enum_values = argument_spec->enum_values(); | 198 const std::set<std::string>& enum_values = argument_spec->enum_values(); |
199 if (!enum_values.empty()) { | 199 if (!enum_values.empty()) { |
200 // Type names may be prefixed by the api name. If so, remove the prefix. | 200 // Type names may be prefixed by the api name. If so, remove the prefix. |
201 base::Optional<std::string> stripped_id; | 201 base::Optional<std::string> stripped_id; |
202 if (base::StartsWith(id, api_name_, base::CompareCase::SENSITIVE)) | 202 if (base::StartsWith(id, api_name_, base::CompareCase::SENSITIVE)) |
203 stripped_id = id.substr(api_name_.size() + 1); // +1 for trailing '.' | 203 stripped_id = id.substr(api_name_.size() + 1); // +1 for trailing '.' |
204 std::vector<EnumEntry>& entries = | 204 std::vector<EnumEntry>& entries = |
205 enums_[stripped_id ? *stripped_id : id]; | 205 enums_[stripped_id ? *stripped_id : id]; |
206 entries.reserve(enum_values.size()); | 206 entries.reserve(enum_values.size()); |
207 for (const auto& enum_value : enum_values) { | 207 for (const auto& enum_value : enum_values) { |
208 entries.push_back( | 208 entries.push_back( |
209 std::make_pair(enum_value, GetJSEnumEntryName(enum_value))); | 209 std::make_pair(enum_value, GetJSEnumEntryName(enum_value))); |
210 } | 210 } |
211 } | 211 } |
212 type_refs->AddSpec(id, std::move(argument_spec)); | 212 type_refs->AddSpec(id, std::move(argument_spec)); |
213 // Some types, like storage.StorageArea, have functions associated with | 213 // Some types, like storage.StorageArea, have functions associated with |
214 // them. Cache the function signatures in the type map. | 214 // them. Cache the function signatures in the type map. |
215 const base::ListValue* type_functions = nullptr; | 215 const base::ListValue* type_functions = nullptr; |
216 if (type_dict->GetList("functions", &type_functions)) { | 216 if (type_dict->GetList("functions", &type_functions)) { |
217 for (const auto& func : *type_functions) { | 217 for (const auto& func : *type_functions) { |
218 const base::DictionaryValue* func_dict = nullptr; | 218 const base::DictionaryValue* func_dict = nullptr; |
219 CHECK(func->GetAsDictionary(&func_dict)); | 219 CHECK(func.GetAsDictionary(&func_dict)); |
220 std::string function_name; | 220 std::string function_name; |
221 CHECK(func_dict->GetString("name", &function_name)); | 221 CHECK(func_dict->GetString("name", &function_name)); |
222 | 222 |
223 const base::ListValue* params = nullptr; | 223 const base::ListValue* params = nullptr; |
224 CHECK(func_dict->GetList("parameters", ¶ms)); | 224 CHECK(func_dict->GetList("parameters", ¶ms)); |
225 type_refs->AddTypeMethodSignature( | 225 type_refs->AddTypeMethodSignature( |
226 base::StringPrintf("%s.%s", id.c_str(), function_name.c_str()), | 226 base::StringPrintf("%s.%s", id.c_str(), function_name.c_str()), |
227 base::MakeUnique<APISignature>(*params)); | 227 base::MakeUnique<APISignature>(*params)); |
228 } | 228 } |
229 } | 229 } |
230 } | 230 } |
231 } | 231 } |
232 | 232 |
233 if (event_definitions) { | 233 if (event_definitions) { |
234 events_.reserve(event_definitions->GetSize()); | 234 events_.reserve(event_definitions->GetSize()); |
235 for (const auto& event : *event_definitions) { | 235 for (const auto& event : *event_definitions) { |
236 const base::DictionaryValue* event_dict = nullptr; | 236 const base::DictionaryValue* event_dict = nullptr; |
237 CHECK(event->GetAsDictionary(&event_dict)); | 237 CHECK(event.GetAsDictionary(&event_dict)); |
238 std::string name; | 238 std::string name; |
239 CHECK(event_dict->GetString("name", &name)); | 239 CHECK(event_dict->GetString("name", &name)); |
240 std::string full_name = | 240 std::string full_name = |
241 base::StringPrintf("%s.%s", api_name_.c_str(), name.c_str()); | 241 base::StringPrintf("%s.%s", api_name_.c_str(), name.c_str()); |
242 const base::ListValue* filters = nullptr; | 242 const base::ListValue* filters = nullptr; |
243 bool supports_filters = | 243 bool supports_filters = |
244 event_dict->GetList("filters", &filters) && !filters->empty(); | 244 event_dict->GetList("filters", &filters) && !filters->empty(); |
245 | 245 |
246 std::vector<std::string> rule_actions; | 246 std::vector<std::string> rule_actions; |
247 std::vector<std::string> rule_conditions; | 247 std::vector<std::string> rule_conditions; |
248 const base::DictionaryValue* options = nullptr; | 248 const base::DictionaryValue* options = nullptr; |
249 bool supports_rules = false; | 249 bool supports_rules = false; |
250 if (event_dict->GetDictionary("options", &options) && | 250 if (event_dict->GetDictionary("options", &options) && |
251 options->GetBoolean("supportsRules", &supports_rules) && | 251 options->GetBoolean("supportsRules", &supports_rules) && |
252 supports_rules) { | 252 supports_rules) { |
253 bool supports_listeners = false; | 253 bool supports_listeners = false; |
254 DCHECK(options->GetBoolean("supportsListeners", &supports_listeners)); | 254 DCHECK(options->GetBoolean("supportsListeners", &supports_listeners)); |
255 DCHECK(!supports_listeners) | 255 DCHECK(!supports_listeners) |
256 << "Events cannot support rules and listeners."; | 256 << "Events cannot support rules and listeners."; |
257 auto get_values = [options](base::StringPiece name, | 257 auto get_values = [options](base::StringPiece name, |
258 std::vector<std::string>* out_value) { | 258 std::vector<std::string>* out_value) { |
259 const base::ListValue* list = nullptr; | 259 const base::ListValue* list = nullptr; |
260 CHECK(options->GetList(name, &list)); | 260 CHECK(options->GetList(name, &list)); |
261 for (const auto& entry : *list) { | 261 for (const auto& entry : *list) { |
262 DCHECK(entry->is_string()); | 262 DCHECK(entry.is_string()); |
263 out_value->push_back(entry->GetString()); | 263 out_value->push_back(entry.GetString()); |
264 } | 264 } |
265 }; | 265 }; |
266 get_values("actions", &rule_actions); | 266 get_values("actions", &rule_actions); |
267 get_values("conditions", &rule_conditions); | 267 get_values("conditions", &rule_conditions); |
268 } | 268 } |
269 | 269 |
270 events_.push_back(base::MakeUnique<EventData>( | 270 events_.push_back(base::MakeUnique<EventData>( |
271 std::move(name), std::move(full_name), supports_filters, | 271 std::move(name), std::move(full_name), supports_filters, |
272 supports_rules, std::move(rule_actions), std::move(rule_conditions), | 272 supports_rules, std::move(rule_actions), std::move(rule_conditions), |
273 this)); | 273 this)); |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
529 if (invalid_invocation) { | 529 if (invalid_invocation) { |
530 arguments->ThrowTypeError("Invalid invocation"); | 530 arguments->ThrowTypeError("Invalid invocation"); |
531 return; | 531 return; |
532 } | 532 } |
533 | 533 |
534 request_handler_->StartRequest(context, name, std::move(converted_arguments), | 534 request_handler_->StartRequest(context, name, std::move(converted_arguments), |
535 callback, custom_callback); | 535 callback, custom_callback); |
536 } | 536 } |
537 | 537 |
538 } // namespace extensions | 538 } // namespace extensions |
OLD | NEW |