| 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_hooks.h" | 10 #include "extensions/renderer/api_binding_hooks.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(const binding::RunJSFunction& call_js, | 17 APIBindingsSystem::APIBindingsSystem( |
| 18 const GetAPISchemaMethod& get_api_schema, | 18 const binding::RunJSFunction& call_js, |
| 19 const SendRequestMethod& send_request) | 19 const binding::RunJSFunctionSync& call_js_sync, |
| 20 const GetAPISchemaMethod& get_api_schema, |
| 21 const SendRequestMethod& send_request) |
| 20 : request_handler_(call_js), | 22 : request_handler_(call_js), |
| 21 event_handler_(call_js), | 23 event_handler_(call_js), |
| 22 call_js_(call_js), | 24 call_js_(call_js), |
| 25 call_js_sync_(call_js_sync), |
| 23 get_api_schema_(get_api_schema), | 26 get_api_schema_(get_api_schema), |
| 24 send_request_(send_request) {} | 27 send_request_(send_request) {} |
| 25 | 28 |
| 26 APIBindingsSystem::~APIBindingsSystem() {} | 29 APIBindingsSystem::~APIBindingsSystem() {} |
| 27 | 30 |
| 28 v8::Local<v8::Object> APIBindingsSystem::CreateAPIInstance( | 31 v8::Local<v8::Object> APIBindingsSystem::CreateAPIInstance( |
| 29 const std::string& api_name, | 32 const std::string& api_name, |
| 30 v8::Local<v8::Context> context, | 33 v8::Local<v8::Context> context, |
| 31 v8::Isolate* isolate, | 34 v8::Isolate* isolate, |
| 32 const APIBinding::AvailabilityCallback& is_available, | 35 const APIBinding::AvailabilityCallback& is_available, |
| (...skipping 21 matching lines...) Expand all Loading... |
| 54 // Find the hooks for the API. If none exist, an empty set will be created so | 57 // Find the hooks for the API. If none exist, an empty set will be created so |
| 55 // we can use JS custom bindings. | 58 // we can use JS custom bindings. |
| 56 // TODO(devlin): Once all legacy custom bindings are converted, we don't have | 59 // TODO(devlin): Once all legacy custom bindings are converted, we don't have |
| 57 // to unconditionally pass in binding hooks. | 60 // to unconditionally pass in binding hooks. |
| 58 std::unique_ptr<APIBindingHooks> hooks; | 61 std::unique_ptr<APIBindingHooks> hooks; |
| 59 auto iter = binding_hooks_.find(api_name); | 62 auto iter = binding_hooks_.find(api_name); |
| 60 if (iter != binding_hooks_.end()) { | 63 if (iter != binding_hooks_.end()) { |
| 61 hooks = std::move(iter->second); | 64 hooks = std::move(iter->second); |
| 62 binding_hooks_.erase(iter); | 65 binding_hooks_.erase(iter); |
| 63 } else { | 66 } else { |
| 64 hooks = base::MakeUnique<APIBindingHooks>(call_js_); | 67 hooks = base::MakeUnique<APIBindingHooks>(call_js_sync_); |
| 65 } | 68 } |
| 66 | 69 |
| 67 return base::MakeUnique<APIBinding>( | 70 return base::MakeUnique<APIBinding>( |
| 68 api_name, function_definitions, type_definitions, event_definitions, | 71 api_name, function_definitions, type_definitions, event_definitions, |
| 69 base::Bind(&APIBindingsSystem::OnAPICall, base::Unretained(this)), | 72 base::Bind(&APIBindingsSystem::OnAPICall, base::Unretained(this)), |
| 70 std::move(hooks), &type_reference_map_); | 73 std::move(hooks), &type_reference_map_); |
| 71 } | 74 } |
| 72 | 75 |
| 73 void APIBindingsSystem::CompleteRequest(int request_id, | 76 void APIBindingsSystem::CompleteRequest(int request_id, |
| 74 const base::ListValue& response) { | 77 const base::ListValue& response) { |
| 75 request_handler_.CompleteRequest(request_id, response); | 78 request_handler_.CompleteRequest(request_id, response); |
| 76 } | 79 } |
| 77 | 80 |
| 78 void APIBindingsSystem::FireEventInContext(const std::string& event_name, | 81 void APIBindingsSystem::FireEventInContext(const std::string& event_name, |
| 79 v8::Local<v8::Context> context, | 82 v8::Local<v8::Context> context, |
| 80 const base::ListValue& response) { | 83 const base::ListValue& response) { |
| 81 event_handler_.FireEventInContext(event_name, context, response); | 84 event_handler_.FireEventInContext(event_name, context, response); |
| 82 } | 85 } |
| 83 | 86 |
| 84 APIBindingHooks* APIBindingsSystem::GetHooksForAPI( | 87 APIBindingHooks* APIBindingsSystem::GetHooksForAPI( |
| 85 const std::string& api_name) { | 88 const std::string& api_name) { |
| 86 DCHECK(api_bindings_.empty()) | 89 DCHECK(api_bindings_.empty()) |
| 87 << "Hook registration must happen before creating any binding instances."; | 90 << "Hook registration must happen before creating any binding instances."; |
| 88 std::unique_ptr<APIBindingHooks>& hooks = binding_hooks_[api_name]; | 91 std::unique_ptr<APIBindingHooks>& hooks = binding_hooks_[api_name]; |
| 89 if (!hooks) | 92 if (!hooks) |
| 90 hooks = base::MakeUnique<APIBindingHooks>(call_js_); | 93 hooks = base::MakeUnique<APIBindingHooks>(call_js_sync_); |
| 91 return hooks.get(); | 94 return hooks.get(); |
| 92 } | 95 } |
| 93 | 96 |
| 94 void APIBindingsSystem::OnAPICall(const std::string& name, | 97 void APIBindingsSystem::OnAPICall(const std::string& name, |
| 95 std::unique_ptr<base::ListValue> arguments, | 98 std::unique_ptr<base::ListValue> arguments, |
| 96 v8::Isolate* isolate, | 99 v8::Isolate* isolate, |
| 97 v8::Local<v8::Context> context, | 100 v8::Local<v8::Context> context, |
| 98 v8::Local<v8::Function> callback) { | 101 v8::Local<v8::Function> callback) { |
| 99 auto request = base::MakeUnique<Request>(); | 102 auto request = base::MakeUnique<Request>(); |
| 100 if (!callback.IsEmpty()) { | 103 if (!callback.IsEmpty()) { |
| 101 request->request_id = | 104 request->request_id = |
| 102 request_handler_.AddPendingRequest(isolate, callback, context); | 105 request_handler_.AddPendingRequest(isolate, callback, context); |
| 103 request->has_callback = true; | 106 request->has_callback = true; |
| 104 } | 107 } |
| 105 // TODO(devlin): Query and curry user gestures around. | 108 // TODO(devlin): Query and curry user gestures around. |
| 106 request->has_user_gesture = false; | 109 request->has_user_gesture = false; |
| 107 request->arguments = std::move(arguments); | 110 request->arguments = std::move(arguments); |
| 108 request->method_name = name; | 111 request->method_name = name; |
| 109 | 112 |
| 110 send_request_.Run(std::move(request), context); | 113 send_request_.Run(std::move(request), context); |
| 111 } | 114 } |
| 112 | 115 |
| 113 } // namespace extensions | 116 } // namespace extensions |
| OLD | NEW |