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