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 APIRequestHandler::SendRequestMethod& send_request, |
19 const APIEventHandler::EventListenersChangedMethod& event_listeners_changed, | 19 const APIEventHandler::EventListenersChangedMethod& event_listeners_changed, |
20 APILastError last_error) | 20 APILastError last_error) |
21 : type_reference_map_(base::Bind(&APIBindingsSystem::InitializeType, | 21 : type_reference_map_(base::Bind(&APIBindingsSystem::InitializeType, |
22 base::Unretained(this))), | 22 base::Unretained(this))), |
23 request_handler_(call_js, std::move(last_error)), | 23 request_handler_(send_request, call_js, std::move(last_error)), |
24 event_handler_(call_js, event_listeners_changed), | 24 event_handler_(call_js, event_listeners_changed), |
25 call_js_(call_js), | 25 call_js_(call_js), |
26 call_js_sync_(call_js_sync), | 26 call_js_sync_(call_js_sync), |
27 get_api_schema_(get_api_schema), | 27 get_api_schema_(get_api_schema) {} |
28 send_request_(send_request) {} | |
29 | 28 |
30 APIBindingsSystem::~APIBindingsSystem() {} | 29 APIBindingsSystem::~APIBindingsSystem() {} |
31 | 30 |
32 v8::Local<v8::Object> APIBindingsSystem::CreateAPIInstance( | 31 v8::Local<v8::Object> APIBindingsSystem::CreateAPIInstance( |
33 const std::string& api_name, | 32 const std::string& api_name, |
34 v8::Local<v8::Context> context, | 33 v8::Local<v8::Context> context, |
35 v8::Isolate* isolate, | 34 v8::Isolate* isolate, |
36 const APIBinding::AvailabilityCallback& is_available, | 35 const APIBinding::AvailabilityCallback& is_available, |
37 v8::Local<v8::Object>* hooks_interface_out) { | 36 v8::Local<v8::Object>* hooks_interface_out) { |
38 std::unique_ptr<APIBinding>& binding = api_bindings_[api_name]; | 37 std::unique_ptr<APIBinding>& binding = api_bindings_[api_name]; |
(...skipping 25 matching lines...) Expand all Loading... |
64 auto iter = binding_hooks_.find(api_name); | 63 auto iter = binding_hooks_.find(api_name); |
65 if (iter != binding_hooks_.end()) { | 64 if (iter != binding_hooks_.end()) { |
66 hooks = std::move(iter->second); | 65 hooks = std::move(iter->second); |
67 binding_hooks_.erase(iter); | 66 binding_hooks_.erase(iter); |
68 } else { | 67 } else { |
69 hooks = base::MakeUnique<APIBindingHooks>(call_js_sync_); | 68 hooks = base::MakeUnique<APIBindingHooks>(call_js_sync_); |
70 } | 69 } |
71 | 70 |
72 return base::MakeUnique<APIBinding>( | 71 return base::MakeUnique<APIBinding>( |
73 api_name, function_definitions, type_definitions, event_definitions, | 72 api_name, function_definitions, type_definitions, event_definitions, |
74 property_definitions, send_request_, std::move(hooks), | 73 property_definitions, std::move(hooks), &type_reference_map_, |
75 &type_reference_map_, &request_handler_, &event_handler_); | 74 &request_handler_, &event_handler_); |
76 } | 75 } |
77 | 76 |
78 void APIBindingsSystem::InitializeType(const std::string& type_name) { | 77 void APIBindingsSystem::InitializeType(const std::string& type_name) { |
79 // In order to initialize the type, we just initialize the full binding. This | 78 // In order to initialize the type, we just initialize the full binding. This |
80 // seems like a lot of work, but in practice, trying to extract out only the | 79 // seems like a lot of work, but in practice, trying to extract out only the |
81 // types from the schema, and then update the reference map based on that, is | 80 // types from the schema, and then update the reference map based on that, is |
82 // close enough to the same cost. Additionally, this happens lazily on API | 81 // close enough to the same cost. Additionally, this happens lazily on API |
83 // use, and relatively few APIs specify types from another API. Finally, this | 82 // use, and relatively few APIs specify types from another API. Finally, this |
84 // will also go away if/when we generate all these specifications. | 83 // will also go away if/when we generate all these specifications. |
85 std::string::size_type dot = type_name.rfind('.'); | 84 std::string::size_type dot = type_name.rfind('.'); |
(...skipping 24 matching lines...) Expand all Loading... |
110 const std::string& api_name) { | 109 const std::string& api_name) { |
111 DCHECK(api_bindings_.empty()) | 110 DCHECK(api_bindings_.empty()) |
112 << "Hook registration must happen before creating any binding instances."; | 111 << "Hook registration must happen before creating any binding instances."; |
113 std::unique_ptr<APIBindingHooks>& hooks = binding_hooks_[api_name]; | 112 std::unique_ptr<APIBindingHooks>& hooks = binding_hooks_[api_name]; |
114 if (!hooks) | 113 if (!hooks) |
115 hooks = base::MakeUnique<APIBindingHooks>(call_js_sync_); | 114 hooks = base::MakeUnique<APIBindingHooks>(call_js_sync_); |
116 return hooks.get(); | 115 return hooks.get(); |
117 } | 116 } |
118 | 117 |
119 } // namespace extensions | 118 } // namespace extensions |
OLD | NEW |