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