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" |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 auto iter = binding_hooks_.find(api_name); | 63 auto iter = binding_hooks_.find(api_name); |
64 if (iter != binding_hooks_.end()) { | 64 if (iter != binding_hooks_.end()) { |
65 hooks = std::move(iter->second); | 65 hooks = std::move(iter->second); |
66 binding_hooks_.erase(iter); | 66 binding_hooks_.erase(iter); |
67 } else { | 67 } else { |
68 hooks = base::MakeUnique<APIBindingHooks>(call_js_sync_); | 68 hooks = base::MakeUnique<APIBindingHooks>(call_js_sync_); |
69 } | 69 } |
70 | 70 |
71 return base::MakeUnique<APIBinding>( | 71 return base::MakeUnique<APIBinding>( |
72 api_name, function_definitions, type_definitions, event_definitions, | 72 api_name, function_definitions, type_definitions, event_definitions, |
73 property_definitions, std::move(hooks), &type_reference_map_, | 73 property_definitions, |
74 &request_handler_, &event_handler_); | 74 base::Bind(&APIBindingsSystem::CreateCustomType, base::Unretained(this)), |
| 75 std::move(hooks), &type_reference_map_, &request_handler_, |
| 76 &event_handler_); |
75 } | 77 } |
76 | 78 |
77 void APIBindingsSystem::InitializeType(const std::string& type_name) { | 79 void APIBindingsSystem::InitializeType(const std::string& type_name) { |
78 // 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 |
79 // 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 |
80 // 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 |
81 // 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 |
82 // 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 |
83 // will also go away if/when we generate all these specifications. | 85 // will also go away if/when we generate all these specifications. |
84 std::string::size_type dot = type_name.rfind('.'); | 86 std::string::size_type dot = type_name.rfind('.'); |
(...skipping 23 matching lines...) Expand all Loading... |
108 APIBindingHooks* APIBindingsSystem::GetHooksForAPI( | 110 APIBindingHooks* APIBindingsSystem::GetHooksForAPI( |
109 const std::string& api_name) { | 111 const std::string& api_name) { |
110 DCHECK(api_bindings_.empty()) | 112 DCHECK(api_bindings_.empty()) |
111 << "Hook registration must happen before creating any binding instances."; | 113 << "Hook registration must happen before creating any binding instances."; |
112 std::unique_ptr<APIBindingHooks>& hooks = binding_hooks_[api_name]; | 114 std::unique_ptr<APIBindingHooks>& hooks = binding_hooks_[api_name]; |
113 if (!hooks) | 115 if (!hooks) |
114 hooks = base::MakeUnique<APIBindingHooks>(call_js_sync_); | 116 hooks = base::MakeUnique<APIBindingHooks>(call_js_sync_); |
115 return hooks.get(); | 117 return hooks.get(); |
116 } | 118 } |
117 | 119 |
| 120 void APIBindingsSystem::RegisterCustomType(const std::string& type_name, |
| 121 const CustomTypeHandler& function) { |
| 122 DCHECK(custom_types_.find(type_name) == custom_types_.end()) |
| 123 << "Custom type already registered: " << type_name; |
| 124 custom_types_[type_name] = function; |
| 125 } |
| 126 |
| 127 v8::Local<v8::Object> APIBindingsSystem::CreateCustomType( |
| 128 v8::Local<v8::Context> context, |
| 129 const std::string& type_name, |
| 130 const std::string& property_name) { |
| 131 auto iter = custom_types_.find(type_name); |
| 132 DCHECK(iter != custom_types_.end()) << "Custom type not found: " << type_name; |
| 133 return iter->second.Run(context, property_name, &request_handler_, |
| 134 &type_reference_map_); |
| 135 } |
| 136 |
118 } // namespace extensions | 137 } // namespace extensions |
OLD | NEW |