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 |
| 12 namespace extensions { |
| 13 |
| 14 APIBindingsSystem::Request::Request() {} |
| 15 APIBindingsSystem::Request::~Request() {} |
| 16 |
| 17 APIBindingsSystem::APIBindingsSystem( |
| 18 const APIRequestHandler::CallJSFunction& call_js, |
| 19 const GetAPISchemaMethod& get_api_schema, |
| 20 const SendRequestMethod& send_request) |
| 21 : request_handler_(call_js), |
| 22 get_api_schema_(get_api_schema), |
| 23 send_request_(send_request) {} |
| 24 |
| 25 APIBindingsSystem::~APIBindingsSystem() {} |
| 26 |
| 27 v8::Local<v8::Object> APIBindingsSystem::CreateAPIInstance( |
| 28 const std::string& api_name, |
| 29 v8::Local<v8::Context> context, |
| 30 v8::Isolate* isolate) { |
| 31 std::unique_ptr<APIBinding>& binding = api_bindings_[api_name]; |
| 32 if (!binding) |
| 33 binding = CreateNewAPIBinding(api_name); |
| 34 return binding->CreateInstance(context, isolate); |
| 35 } |
| 36 |
| 37 std::unique_ptr<APIBinding> APIBindingsSystem::CreateNewAPIBinding( |
| 38 const std::string& api_name) { |
| 39 const base::DictionaryValue& api_schema = get_api_schema_.Run(api_name); |
| 40 |
| 41 const base::ListValue* function_definitions = nullptr; |
| 42 CHECK(api_schema.GetList("functions", &function_definitions)); |
| 43 const base::ListValue* type_definitions = nullptr; |
| 44 // Type definitions might not exist for the given API. |
| 45 api_schema.GetList("types", &type_definitions); |
| 46 |
| 47 return base::MakeUnique<APIBinding>( |
| 48 api_name, *function_definitions, type_definitions, |
| 49 base::Bind(&APIBindingsSystem::OnAPICall, base::Unretained(this)), |
| 50 &type_reference_map_); |
| 51 } |
| 52 |
| 53 void APIBindingsSystem::CompleteRequest(const std::string& request_id, |
| 54 const base::ListValue& response) { |
| 55 request_handler_.CompleteRequest(request_id, response); |
| 56 } |
| 57 |
| 58 void APIBindingsSystem::OnAPICall(const std::string& name, |
| 59 std::unique_ptr<base::ListValue> arguments, |
| 60 v8::Isolate* isolate, |
| 61 v8::Local<v8::Context> context, |
| 62 v8::Local<v8::Function> callback) { |
| 63 auto request = base::MakeUnique<Request>(); |
| 64 if (!callback.IsEmpty()) { |
| 65 request->request_id = |
| 66 request_handler_.AddPendingRequest(isolate, callback, context); |
| 67 } |
| 68 request->arguments = std::move(arguments); |
| 69 request->method_name = name; |
| 70 |
| 71 send_request_.Run(std::move(request)); |
| 72 } |
| 73 |
| 74 } // namespace extensions |
OLD | NEW |