| 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::APIBindingsSystem( | 14 APIBindingsSystem::APIBindingsSystem( |
| 15 const binding::RunJSFunction& call_js, | 15 const binding::RunJSFunction& call_js, |
| 16 const binding::RunJSFunctionSync& call_js_sync, | 16 const binding::RunJSFunctionSync& call_js_sync, |
| 17 const GetAPISchemaMethod& get_api_schema, | 17 const GetAPISchemaMethod& get_api_schema, |
| 18 const APIBinding::SendRequestMethod& send_request, | 18 const APIBinding::SendRequestMethod& send_request, |
| 19 const APIEventHandler::EventListenersChangedMethod& event_listeners_changed) | 19 const APIEventHandler::EventListenersChangedMethod& event_listeners_changed, |
| 20 APILastError last_error) |
| 20 : type_reference_map_(base::Bind(&APIBindingsSystem::InitializeType, | 21 : type_reference_map_(base::Bind(&APIBindingsSystem::InitializeType, |
| 21 base::Unretained(this))), | 22 base::Unretained(this))), |
| 22 request_handler_(call_js), | 23 request_handler_(call_js, std::move(last_error)), |
| 23 event_handler_(call_js, event_listeners_changed), | 24 event_handler_(call_js, event_listeners_changed), |
| 24 call_js_(call_js), | 25 call_js_(call_js), |
| 25 call_js_sync_(call_js_sync), | 26 call_js_sync_(call_js_sync), |
| 26 get_api_schema_(get_api_schema), | 27 get_api_schema_(get_api_schema), |
| 27 send_request_(send_request) {} | 28 send_request_(send_request) {} |
| 28 | 29 |
| 29 APIBindingsSystem::~APIBindingsSystem() {} | 30 APIBindingsSystem::~APIBindingsSystem() {} |
| 30 | 31 |
| 31 v8::Local<v8::Object> APIBindingsSystem::CreateAPIInstance( | 32 v8::Local<v8::Object> APIBindingsSystem::CreateAPIInstance( |
| 32 const std::string& api_name, | 33 const std::string& api_name, |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 DCHECK_LT(dot, type_name.size() - 1); | 86 DCHECK_LT(dot, type_name.size() - 1); |
| 86 std::string api_name = type_name.substr(0, dot); | 87 std::string api_name = type_name.substr(0, dot); |
| 87 // If we've already instantiated the binding, the type should have been in | 88 // If we've already instantiated the binding, the type should have been in |
| 88 // there. | 89 // there. |
| 89 DCHECK(api_bindings_.find(api_name) == api_bindings_.end()); | 90 DCHECK(api_bindings_.find(api_name) == api_bindings_.end()); |
| 90 | 91 |
| 91 api_bindings_[api_name] = CreateNewAPIBinding(api_name); | 92 api_bindings_[api_name] = CreateNewAPIBinding(api_name); |
| 92 } | 93 } |
| 93 | 94 |
| 94 void APIBindingsSystem::CompleteRequest(int request_id, | 95 void APIBindingsSystem::CompleteRequest(int request_id, |
| 95 const base::ListValue& response) { | 96 const base::ListValue& response, |
| 96 request_handler_.CompleteRequest(request_id, response); | 97 const std::string& error) { |
| 98 request_handler_.CompleteRequest(request_id, response, error); |
| 97 } | 99 } |
| 98 | 100 |
| 99 void APIBindingsSystem::FireEventInContext(const std::string& event_name, | 101 void APIBindingsSystem::FireEventInContext(const std::string& event_name, |
| 100 v8::Local<v8::Context> context, | 102 v8::Local<v8::Context> context, |
| 101 const base::ListValue& response) { | 103 const base::ListValue& response) { |
| 102 event_handler_.FireEventInContext(event_name, context, response); | 104 event_handler_.FireEventInContext(event_name, context, response); |
| 103 } | 105 } |
| 104 | 106 |
| 105 APIBindingHooks* APIBindingsSystem::GetHooksForAPI( | 107 APIBindingHooks* APIBindingsSystem::GetHooksForAPI( |
| 106 const std::string& api_name) { | 108 const std::string& api_name) { |
| 107 DCHECK(api_bindings_.empty()) | 109 DCHECK(api_bindings_.empty()) |
| 108 << "Hook registration must happen before creating any binding instances."; | 110 << "Hook registration must happen before creating any binding instances."; |
| 109 std::unique_ptr<APIBindingHooks>& hooks = binding_hooks_[api_name]; | 111 std::unique_ptr<APIBindingHooks>& hooks = binding_hooks_[api_name]; |
| 110 if (!hooks) | 112 if (!hooks) |
| 111 hooks = base::MakeUnique<APIBindingHooks>(call_js_sync_); | 113 hooks = base::MakeUnique<APIBindingHooks>(call_js_sync_); |
| 112 return hooks.get(); | 114 return hooks.get(); |
| 113 } | 115 } |
| 114 | 116 |
| 115 } // namespace extensions | 117 } // namespace extensions |
| OLD | NEW |