| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/storage_area.h" | 5 #include "extensions/renderer/storage_area.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "base/values.h" | |
| 10 #include "extensions/common/api/storage.h" | 9 #include "extensions/common/api/storage.h" |
| 11 #include "extensions/common/extension_api.h" | |
| 12 #include "extensions/renderer/api_request_handler.h" | 10 #include "extensions/renderer/api_request_handler.h" |
| 13 #include "extensions/renderer/api_signature.h" | 11 #include "extensions/renderer/api_signature.h" |
| 12 #include "extensions/renderer/api_type_reference_map.h" |
| 14 #include "gin/arguments.h" | 13 #include "gin/arguments.h" |
| 15 #include "gin/handle.h" | 14 #include "gin/handle.h" |
| 16 #include "gin/object_template_builder.h" | 15 #include "gin/object_template_builder.h" |
| 17 #include "gin/wrappable.h" | 16 #include "gin/wrappable.h" |
| 18 | 17 |
| 19 namespace extensions { | 18 namespace extensions { |
| 20 | 19 |
| 21 namespace { | 20 namespace { |
| 22 | 21 |
| 23 #define DEFINE_STORAGE_AREA_HANDLERS() \ | 22 #define DEFINE_STORAGE_AREA_HANDLERS() \ |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 | 185 |
| 187 std::vector<v8::Local<v8::Value>> argument_list; | 186 std::vector<v8::Local<v8::Value>> argument_list; |
| 188 if (arguments->Length() > 0) { | 187 if (arguments->Length() > 0) { |
| 189 // Just copying handles should never fail. | 188 // Just copying handles should never fail. |
| 190 CHECK(arguments->GetRemaining(&argument_list)); | 189 CHECK(arguments->GetRemaining(&argument_list)); |
| 191 } | 190 } |
| 192 | 191 |
| 193 std::unique_ptr<base::ListValue> converted_arguments; | 192 std::unique_ptr<base::ListValue> converted_arguments; |
| 194 v8::Local<v8::Function> callback; | 193 v8::Local<v8::Function> callback; |
| 195 std::string error; | 194 std::string error; |
| 196 if (!GetFunctionSchema("storage", "storage.StorageArea", method_name) | 195 const APISignature* signature = type_refs_->GetTypeMethodSignature( |
| 197 .ParseArgumentsToJSON(context, argument_list, *type_refs_, | 196 base::StringPrintf("%s.%s", "storage.StorageArea", method_name.c_str())); |
| 198 &converted_arguments, &callback, &error)) { | 197 DCHECK(signature); |
| 198 if (!signature->ParseArgumentsToJSON(context, argument_list, *type_refs_, |
| 199 &converted_arguments, &callback, |
| 200 &error)) { |
| 199 arguments->ThrowTypeError("Invalid invocation"); | 201 arguments->ThrowTypeError("Invalid invocation"); |
| 200 return; | 202 return; |
| 201 } | 203 } |
| 202 | 204 |
| 203 converted_arguments->Insert(0u, base::MakeUnique<base::Value>(name_)); | 205 converted_arguments->Insert(0u, base::MakeUnique<base::Value>(name_)); |
| 204 request_handler_->StartRequest(context, "storage." + method_name, | 206 request_handler_->StartRequest(context, "storage." + method_name, |
| 205 std::move(converted_arguments), callback, | 207 std::move(converted_arguments), callback, |
| 206 v8::Local<v8::Function>()); | 208 v8::Local<v8::Function>()); |
| 207 } | 209 } |
| 208 | 210 |
| 209 const APISignature& StorageArea::GetFunctionSchema( | |
| 210 base::StringPiece api_name, | |
| 211 base::StringPiece type_name, | |
| 212 base::StringPiece function_name) { | |
| 213 std::string full_name = base::StringPrintf( | |
| 214 "%s.%s.%s", api_name.data(), type_name.data(), function_name.data()); | |
| 215 auto iter = signatures_.find(full_name); | |
| 216 if (iter != signatures_.end()) | |
| 217 return *iter->second; | |
| 218 | |
| 219 const base::DictionaryValue* full_schema = | |
| 220 ExtensionAPI::GetSharedInstance()->GetSchema(api_name.as_string()); | |
| 221 const base::ListValue* types = nullptr; | |
| 222 CHECK(full_schema->GetList("types", &types)); | |
| 223 const base::DictionaryValue* type_schema = nullptr; | |
| 224 for (const auto& type : *types) { | |
| 225 const base::DictionaryValue* type_dict = nullptr; | |
| 226 CHECK(type->GetAsDictionary(&type_dict)); | |
| 227 std::string id; | |
| 228 CHECK(type_dict->GetString("id", &id)); | |
| 229 if (id == type_name) { | |
| 230 type_schema = type_dict; | |
| 231 break; | |
| 232 } | |
| 233 } | |
| 234 CHECK(type_schema); | |
| 235 const base::ListValue* type_functions = nullptr; | |
| 236 CHECK(type_schema->GetList("functions", &type_functions)); | |
| 237 const base::ListValue* parameters = nullptr; | |
| 238 for (const auto& function : *type_functions) { | |
| 239 const base::DictionaryValue* function_dict = nullptr; | |
| 240 CHECK(function->GetAsDictionary(&function_dict)); | |
| 241 std::string name; | |
| 242 CHECK(function_dict->GetString("name", &name)); | |
| 243 if (name == function_name) { | |
| 244 CHECK(function_dict->GetList("parameters", ¶meters)); | |
| 245 break; | |
| 246 } | |
| 247 } | |
| 248 CHECK(parameters); | |
| 249 auto signature = base::MakeUnique<APISignature>(*parameters); | |
| 250 const auto* raw_signature = signature.get(); | |
| 251 signatures_[full_name] = std::move(signature); | |
| 252 return *raw_signature; | |
| 253 } | |
| 254 | |
| 255 } // namespace extensions | 211 } // namespace extensions |
| OLD | NEW |