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::AvailabilityCallback& is_available, |
18 const APIRequestHandler::SendRequestMethod& send_request, | 19 const APIRequestHandler::SendRequestMethod& send_request, |
19 const APIEventHandler::EventListenersChangedMethod& event_listeners_changed, | 20 const APIEventHandler::EventListenersChangedMethod& event_listeners_changed, |
20 APILastError last_error) | 21 APILastError last_error) |
21 : type_reference_map_(base::Bind(&APIBindingsSystem::InitializeType, | 22 : type_reference_map_(base::Bind(&APIBindingsSystem::InitializeType, |
22 base::Unretained(this))), | 23 base::Unretained(this))), |
23 request_handler_(send_request, call_js, std::move(last_error)), | 24 request_handler_(send_request, call_js, std::move(last_error)), |
24 event_handler_(call_js, event_listeners_changed), | 25 event_handler_(call_js, event_listeners_changed), |
25 call_js_(call_js), | 26 call_js_(call_js), |
26 call_js_sync_(call_js_sync), | 27 call_js_sync_(call_js_sync), |
27 get_api_schema_(get_api_schema) {} | 28 get_api_schema_(get_api_schema), |
| 29 is_available_(is_available) {} |
28 | 30 |
29 APIBindingsSystem::~APIBindingsSystem() {} | 31 APIBindingsSystem::~APIBindingsSystem() {} |
30 | 32 |
31 v8::Local<v8::Object> APIBindingsSystem::CreateAPIInstance( | 33 v8::Local<v8::Object> APIBindingsSystem::CreateAPIInstance( |
32 const std::string& api_name, | 34 const std::string& api_name, |
33 v8::Local<v8::Context> context, | 35 v8::Local<v8::Context> context, |
34 const APIBinding::AvailabilityCallback& is_available, | |
35 APIBindingHooks** hooks_out) { | 36 APIBindingHooks** hooks_out) { |
36 std::unique_ptr<APIBinding>& binding = api_bindings_[api_name]; | 37 std::unique_ptr<APIBinding>& binding = api_bindings_[api_name]; |
37 if (!binding) | 38 if (!binding) |
38 binding = CreateNewAPIBinding(api_name); | 39 binding = CreateNewAPIBinding(api_name); |
39 if (hooks_out) | 40 if (hooks_out) |
40 *hooks_out = binding->hooks(); | 41 *hooks_out = binding->hooks(); |
41 return binding->CreateInstance(context, is_available); | 42 return binding->CreateInstance(context); |
42 } | 43 } |
43 | 44 |
44 std::unique_ptr<APIBinding> APIBindingsSystem::CreateNewAPIBinding( | 45 std::unique_ptr<APIBinding> APIBindingsSystem::CreateNewAPIBinding( |
45 const std::string& api_name) { | 46 const std::string& api_name) { |
46 const base::DictionaryValue& api_schema = get_api_schema_.Run(api_name); | 47 const base::DictionaryValue& api_schema = get_api_schema_.Run(api_name); |
47 | 48 |
48 const base::ListValue* function_definitions = nullptr; | 49 const base::ListValue* function_definitions = nullptr; |
49 api_schema.GetList("functions", &function_definitions); | 50 api_schema.GetList("functions", &function_definitions); |
50 const base::ListValue* type_definitions = nullptr; | 51 const base::ListValue* type_definitions = nullptr; |
51 api_schema.GetList("types", &type_definitions); | 52 api_schema.GetList("types", &type_definitions); |
(...skipping 12 matching lines...) Expand all Loading... |
64 hooks = std::move(iter->second); | 65 hooks = std::move(iter->second); |
65 binding_hooks_.erase(iter); | 66 binding_hooks_.erase(iter); |
66 } else { | 67 } else { |
67 hooks = base::MakeUnique<APIBindingHooks>(api_name, call_js_sync_); | 68 hooks = base::MakeUnique<APIBindingHooks>(api_name, call_js_sync_); |
68 } | 69 } |
69 | 70 |
70 return base::MakeUnique<APIBinding>( | 71 return base::MakeUnique<APIBinding>( |
71 api_name, function_definitions, type_definitions, event_definitions, | 72 api_name, function_definitions, type_definitions, event_definitions, |
72 property_definitions, | 73 property_definitions, |
73 base::Bind(&APIBindingsSystem::CreateCustomType, base::Unretained(this)), | 74 base::Bind(&APIBindingsSystem::CreateCustomType, base::Unretained(this)), |
74 std::move(hooks), &type_reference_map_, &request_handler_, | 75 is_available_, std::move(hooks), &type_reference_map_, &request_handler_, |
75 &event_handler_); | 76 &event_handler_); |
76 } | 77 } |
77 | 78 |
78 void APIBindingsSystem::InitializeType(const std::string& type_name) { | 79 void APIBindingsSystem::InitializeType(const std::string& type_name) { |
79 // In order to initialize the type, we just initialize the full binding. This | 80 // 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 | 81 // 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 | 82 // 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 | 83 // 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 | 84 // use, and relatively few APIs specify types from another API. Finally, this |
84 // will also go away if/when we generate all these specifications. | 85 // will also go away if/when we generate all these specifications. |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 const std::string& property_name, | 136 const std::string& property_name, |
136 const base::ListValue* property_values) { | 137 const base::ListValue* property_values) { |
137 auto iter = custom_types_.find(type_name); | 138 auto iter = custom_types_.find(type_name); |
138 DCHECK(iter != custom_types_.end()) << "Custom type not found: " << type_name; | 139 DCHECK(iter != custom_types_.end()) << "Custom type not found: " << type_name; |
139 return iter->second.Run(isolate, property_name, property_values, | 140 return iter->second.Run(isolate, property_name, property_values, |
140 &request_handler_, &event_handler_, | 141 &request_handler_, &event_handler_, |
141 &type_reference_map_); | 142 &type_reference_map_); |
142 } | 143 } |
143 | 144 |
144 } // namespace extensions | 145 } // namespace extensions |
OLD | NEW |