2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "extension/extension_adapter.h"
21 #include "common/logger.h"
25 ExtensionAdapter::ExtensionAdapter()
26 : next_xw_extension_(1),
27 next_xw_instance_(1) {
30 ExtensionAdapter::~ExtensionAdapter() {
33 ExtensionAdapter* ExtensionAdapter::GetInstance() {
34 static ExtensionAdapter self;
38 XW_Extension ExtensionAdapter::GetNextXWExtension() {
39 return next_xw_extension_++;
42 XW_Instance ExtensionAdapter::GetNextXWInstance() {
43 return next_xw_instance_++;
46 void ExtensionAdapter::RegisterExtension(Extension* extension) {
47 XW_Extension xw_extension = extension->xw_extension_;
48 if (!(xw_extension > 0 && xw_extension < next_xw_extension_)) {
49 LOGGER(WARN) << "xw_extension (" << xw_extension << ") is invalid.";
52 if (extension_map_.find(xw_extension) == extension_map_.end())
53 extension_map_[xw_extension] = extension;
56 void ExtensionAdapter::UnregisterExtension(Extension* extension) {
57 XW_Extension xw_extension = extension->xw_extension_;
58 if (!(xw_extension > 0 && xw_extension < next_xw_extension_)) {
59 LOGGER(WARN) << "xw_extension (" << xw_extension << ") is invalid.";
62 if (extension_map_.find(xw_extension) != extension_map_.end())
63 extension_map_.erase(xw_extension);
66 void ExtensionAdapter::RegisterInstance(ExtensionInstance* instance) {
67 XW_Instance xw_instance = instance->xw_instance_;
68 if (!(xw_instance > 0 && xw_instance < next_xw_instance_)) {
69 LOGGER(WARN) << "xw_instance (" << xw_instance << ") is invalid.";
72 if (instance_map_.find(xw_instance) == instance_map_.end())
73 instance_map_[xw_instance] = instance;
76 void ExtensionAdapter::UnregisterInstance(ExtensionInstance* instance) {
77 XW_Instance xw_instance = instance->xw_instance_;
78 if (!(xw_instance > 0 && xw_instance < next_xw_instance_)) {
79 LOGGER(WARN) << "xw_instance (" << xw_instance << ") is invalid.";
82 if (instance_map_.find(xw_instance) != instance_map_.end())
83 instance_map_.erase(xw_instance);
86 const void* ExtensionAdapter::GetInterface(const char* name) {
87 if (!strcmp(name, XW_CORE_INTERFACE_1)) {
88 static const XW_CoreInterface_1 coreInterface1 = {
91 CoreRegisterInstanceCallbacks,
92 CoreRegisterShutdownCallback,
96 return &coreInterface1;
99 if (!strcmp(name, XW_MESSAGING_INTERFACE_1)) {
100 static const XW_MessagingInterface_1 messagingInterface1 = {
104 return &messagingInterface1;
107 if (!strcmp(name, XW_INTERNAL_SYNC_MESSAGING_INTERFACE_1)) {
108 static const XW_Internal_SyncMessagingInterface_1
109 syncMessagingInterface1 = {
110 SyncMessagingRegister,
111 SyncMessagingSetSyncReply
113 return &syncMessagingInterface1;
116 if (!strcmp(name, XW_INTERNAL_ENTRY_POINTS_INTERFACE_1)) {
117 static const XW_Internal_EntryPointsInterface_1 entryPointsInterface1 = {
118 EntryPointsSetExtraJSEntryPoints
120 return &entryPointsInterface1;
123 if (!strcmp(name, XW_INTERNAL_RUNTIME_INTERFACE_1)) {
124 static const XW_Internal_RuntimeInterface_1 runtimeInterface1 = {
125 RuntimeGetStringVariable
127 return &runtimeInterface1;
130 if (!strcmp(name, XW_INTERNAL_PERMISSIONS_INTERFACE_1)) {
131 static const XW_Internal_PermissionsInterface_1 permissionsInterface1 = {
132 PermissionsCheckAPIAccessControl,
133 PermissionsRegisterPermissions
135 return &permissionsInterface1;
138 LOGGER(WARN) << "Interface '" << name << "' is not supported.";
142 Extension* ExtensionAdapter::GetExtension(XW_Extension xw_extension) {
143 ExtensionAdapter* adapter = ExtensionAdapter::GetInstance();
144 ExtensionMap::iterator it = adapter->extension_map_.find(xw_extension);
145 if (it == adapter->extension_map_.end())
150 ExtensionInstance* ExtensionAdapter::GetExtensionInstance(
151 XW_Instance xw_instance) {
152 ExtensionAdapter* adapter = ExtensionAdapter::GetInstance();
153 InstanceMap::iterator it = adapter->instance_map_.find(xw_instance);
154 if (it == adapter->instance_map_.end())
159 #define CHECK(x, xw) \
161 LOGGER(WARN) << "Ignoring call. Invalid " << #xw << " = " << xw; \
165 #define RETURN_IF_INITIALIZED(x) \
166 if (x->initialized_) \
169 void ExtensionAdapter::CoreSetExtensionName(XW_Extension xw_extension,
171 Extension* extension = GetExtension(xw_extension);
172 CHECK(extension, xw_extension);
173 RETURN_IF_INITIALIZED(extension);
174 extension->name_ = name;
177 void ExtensionAdapter::CoreSetJavaScriptAPI(XW_Extension xw_extension,
178 const char* javascript_api) {
179 Extension* extension = GetExtension(xw_extension);
180 CHECK(extension, xw_extension);
181 RETURN_IF_INITIALIZED(extension);
182 extension->javascript_api_ = javascript_api;
185 void ExtensionAdapter::CoreRegisterInstanceCallbacks(XW_Extension xw_extension,
186 XW_CreatedInstanceCallback created,
187 XW_DestroyedInstanceCallback destroyed) {
188 Extension* extension = GetExtension(xw_extension);
189 CHECK(extension, xw_extension);
190 RETURN_IF_INITIALIZED(extension);
191 extension->created_instance_callback_ = created;
192 extension->destroyed_instance_callback_ = destroyed;
195 void ExtensionAdapter::CoreRegisterShutdownCallback(XW_Extension xw_extension,
196 XW_ShutdownCallback shutdown) {
197 Extension* extension = GetExtension(xw_extension);
198 CHECK(extension, xw_extension);
199 RETURN_IF_INITIALIZED(extension);
200 extension->shutdown_callback_ = shutdown;
203 void ExtensionAdapter::CoreSetInstanceData(XW_Instance xw_instance,
205 ExtensionInstance* instance = GetExtensionInstance(xw_instance);
206 CHECK(instance, xw_instance);
207 instance->instance_data_ = data;
210 void* ExtensionAdapter::CoreGetInstanceData(XW_Instance xw_instance) {
211 ExtensionInstance* instance = GetExtensionInstance(xw_instance);
213 return instance->instance_data_;
218 void ExtensionAdapter::MessagingRegister(XW_Extension xw_extension,
219 XW_HandleMessageCallback handle_message) {
220 Extension* extension = GetExtension(xw_extension);
221 CHECK(extension, xw_extension);
222 RETURN_IF_INITIALIZED(extension);
223 extension->handle_msg_callback_ = handle_message;
226 void ExtensionAdapter::MessagingPostMessage(XW_Instance xw_instance,
227 const char* message) {
228 ExtensionInstance* instance = GetExtensionInstance(xw_instance);
229 CHECK(instance, xw_instance);
230 instance->PostMessageToJS(message);
233 void ExtensionAdapter::SyncMessagingRegister(XW_Extension xw_extension,
234 XW_HandleSyncMessageCallback handle_sync_message) {
235 Extension* extension = GetExtension(xw_extension);
236 CHECK(extension, xw_extension);
237 RETURN_IF_INITIALIZED(extension);
238 extension->handle_sync_msg_callback_ = handle_sync_message;
241 void ExtensionAdapter::SyncMessagingSetSyncReply(XW_Instance xw_instance,
243 ExtensionInstance* instance = GetExtensionInstance(xw_instance);
244 CHECK(instance, xw_instance);
245 instance->SyncReplyToJS(reply);
248 void ExtensionAdapter::EntryPointsSetExtraJSEntryPoints(
249 XW_Extension xw_extension, const char** entry_points) {
250 Extension* extension = GetExtension(xw_extension);
251 CHECK(extension, xw_extension);
252 RETURN_IF_INITIALIZED(extension);
254 for (int i=0; entry_points[i]; ++i) {
255 extension->entry_points_.push_back(std::string(entry_points[i]));
259 void ExtensionAdapter::RuntimeGetStringVariable(XW_Extension xw_extension,
262 unsigned int value_len) {
263 Extension* extension = GetExtension(xw_extension);
264 CHECK(extension, xw_extension);
265 extension->GetRuntimeVariable(key, value, value_len);
268 int ExtensionAdapter::PermissionsCheckAPIAccessControl(
269 XW_Extension xw_extension, const char* api_name) {
270 Extension* extension = GetExtension(xw_extension);
272 return extension->CheckAPIAccessControl(api_name);
277 int ExtensionAdapter::PermissionsRegisterPermissions(XW_Extension xw_extension,
278 const char* perm_table) {
279 Extension* extension = GetExtension(xw_extension);
281 return extension->RegisterPermissions(perm_table);
287 #undef RETURN_IF_INITIALIZED