Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "extensions/renderer/api_bindings_system.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/memory/ptr_util.h" | |
| 9 #include "base/values.h" | |
| 10 #include "extensions/renderer/api_binding.h" | |
| 11 #include "extensions/renderer/api_request_handler.h" | |
| 12 | |
| 13 namespace extensions { | |
| 14 | |
| 15 APIBindingsSystem::Request::Request() {} | |
| 16 APIBindingsSystem::Request::~Request() {} | |
| 17 | |
| 18 APIBindingsSystem::APIBindingsSystem( | |
| 19 std::unique_ptr<APIRequestHandler> request_handler, | |
| 20 const GetAPISchemaMethod& get_api_schema, | |
| 21 const SendRequestMethod& send_request) | |
| 22 : request_handler_(std::move(request_handler)), | |
| 23 get_api_schema_(get_api_schema), | |
| 24 send_request_(send_request) {} | |
| 25 | |
| 26 APIBindingsSystem::~APIBindingsSystem() {} | |
| 27 | |
| 28 v8::Local<v8::Object> APIBindingsSystem::CreateAPIInstance( | |
| 29 const std::string& api_name, | |
| 30 v8::Local<v8::Context> context, | |
| 31 v8::Isolate* isolate) { | |
| 32 std::unique_ptr<APIBinding>& binding = api_bindings_[api_name]; | |
| 33 if (!binding) | |
| 34 binding = CreateNewAPIBinding(api_name); | |
| 35 return binding->CreateInstance(context, isolate); | |
| 36 } | |
| 37 | |
| 38 std::unique_ptr<APIBinding> APIBindingsSystem::CreateNewAPIBinding( | |
| 39 const std::string& api_name) { | |
| 40 const base::DictionaryValue& api_schema = get_api_schema_.Run(api_name); | |
| 41 | |
| 42 const base::ListValue* function_definitions = nullptr; | |
| 43 CHECK(api_schema.GetList("functions", &function_definitions)); | |
| 44 const base::ListValue* type_definitions = nullptr; | |
| 45 // Type definitions might not exist for the given API. | |
| 46 api_schema.GetList("types", &type_definitions); | |
| 47 | |
| 48 return base::MakeUnique<APIBinding>( | |
| 49 api_name, *function_definitions, | |
| 50 type_definitions ? *type_definitions | |
| 51 : static_cast<const base::ListValue&>(base::ListValue()), | |
|
jbroman
2016/10/21 22:53:59
Is this cast necessary? I'd have expected it to co
Devlin
2016/10/22 00:49:15
I think this is one of those times that the compil
Devlin
2016/10/26 20:17:03
FYI, I decided option 3 here was best.
jbroman
2016/10/31 14:16:58
Ahh, I see what's going on here. Ordinarily this w
| |
| 52 base::Bind(&APIBindingsSystem::OnAPICall, base::Unretained(this)), | |
| 53 &type_reference_map_); | |
| 54 } | |
| 55 | |
| 56 void APIBindingsSystem::OnAPICall(const std::string& name, | |
| 57 std::unique_ptr<base::ListValue> arguments, | |
| 58 v8::Isolate* isolate, | |
| 59 v8::Local<v8::Context> context, | |
| 60 v8::Local<v8::Function> callback) { | |
| 61 auto request = base::MakeUnique<Request>(); | |
|
jbroman
2016/10/21 22:53:59
nit: Will the non-test |send_request_| actually ne
Devlin
2016/10/22 00:49:15
It's a little dependent on how everything shakes o
| |
| 62 if (!callback.IsEmpty()) { | |
| 63 request->request_id = | |
| 64 request_handler_->AddPendingRequest(isolate, callback, context); | |
| 65 } | |
| 66 request->arguments = std::move(arguments); | |
| 67 request->method_name = name; | |
| 68 | |
| 69 send_request_.Run(std::move(request)); | |
| 70 } | |
| 71 | |
| 72 } // namespace extensions | |
| OLD | NEW |