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