Index: extensions/renderer/api_bindings_system.cc |
diff --git a/extensions/renderer/api_bindings_system.cc b/extensions/renderer/api_bindings_system.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..7b487be9b7e60e7409f727e6682c5dd20f714f4c |
--- /dev/null |
+++ b/extensions/renderer/api_bindings_system.cc |
@@ -0,0 +1,72 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "extensions/renderer/api_bindings_system.h" |
+ |
+#include "base/bind.h" |
+#include "base/memory/ptr_util.h" |
+#include "base/values.h" |
+#include "extensions/renderer/api_binding.h" |
+#include "extensions/renderer/api_request_handler.h" |
+ |
+namespace extensions { |
+ |
+APIBindingsSystem::Request::Request() {} |
+APIBindingsSystem::Request::~Request() {} |
+ |
+APIBindingsSystem::APIBindingsSystem( |
+ std::unique_ptr<APIRequestHandler> request_handler, |
+ const GetAPISchemaMethod& get_api_schema, |
+ const SendRequestMethod& send_request) |
+ : request_handler_(std::move(request_handler)), |
+ get_api_schema_(get_api_schema), |
+ send_request_(send_request) {} |
+ |
+APIBindingsSystem::~APIBindingsSystem() {} |
+ |
+v8::Local<v8::Object> APIBindingsSystem::CreateAPIInstance( |
+ const std::string& api_name, |
+ v8::Local<v8::Context> context, |
+ v8::Isolate* isolate) { |
+ std::unique_ptr<APIBinding>& binding = api_bindings_[api_name]; |
+ if (!binding) |
+ binding = CreateNewAPIBinding(api_name); |
+ return binding->CreateInstance(context, isolate); |
+} |
+ |
+std::unique_ptr<APIBinding> APIBindingsSystem::CreateNewAPIBinding( |
+ const std::string& api_name) { |
+ const base::DictionaryValue& api_schema = get_api_schema_.Run(api_name); |
+ |
+ const base::ListValue* function_definitions = nullptr; |
+ CHECK(api_schema.GetList("functions", &function_definitions)); |
+ const base::ListValue* type_definitions = nullptr; |
+ // Type definitions might not exist for the given API. |
+ api_schema.GetList("types", &type_definitions); |
+ |
+ return base::MakeUnique<APIBinding>( |
+ api_name, *function_definitions, |
+ type_definitions ? *type_definitions |
+ : 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
|
+ base::Bind(&APIBindingsSystem::OnAPICall, base::Unretained(this)), |
+ &type_reference_map_); |
+} |
+ |
+void APIBindingsSystem::OnAPICall(const std::string& name, |
+ std::unique_ptr<base::ListValue> arguments, |
+ v8::Isolate* isolate, |
+ v8::Local<v8::Context> context, |
+ v8::Local<v8::Function> callback) { |
+ 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
|
+ if (!callback.IsEmpty()) { |
+ request->request_id = |
+ request_handler_->AddPendingRequest(isolate, callback, context); |
+ } |
+ request->arguments = std::move(arguments); |
+ request->method_name = name; |
+ |
+ send_request_.Run(std::move(request)); |
+} |
+ |
+} // namespace extensions |