5f3efd5e3f8e1a4ae46605bc7f5c165d246289c0
[platform/framework/web/tizen-extensions-crosswalk.git] / common / extension_adapter.cc
1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "common/extension_adapter.h"
6
7 #include <iostream>
8 #include "common/XW_Extension_EntryPoints.h"
9
10 namespace {
11
12 XW_Extension g_extension = 0;
13
14 const XW_CoreInterface* g_core = NULL;
15 const XW_MessagingInterface* g_messaging = NULL;
16 const XW_Internal_SyncMessagingInterface* g_sync_messaging = NULL;
17 const XW_Internal_EntryPointsInterface* g_entry_points = NULL;
18
19 }  // namespace
20
21 namespace internal {
22
23 int32_t InitializeExtension(XW_Extension extension,
24                             XW_GetInterface get_interface,
25                             const char* name,
26                             const char* api,
27                             const char** entry_points,
28                             XW_CreatedInstanceCallback created,
29                             XW_DestroyedInstanceCallback destroyed,
30                             XW_HandleMessageCallback handle_message,
31                             XW_HandleSyncMessageCallback handle_sync_message) {
32   if (g_extension != 0) {
33     std::cerr << "Can't initialize same extension multiple times!\n";
34     return XW_ERROR;
35   }
36
37   g_extension = extension;
38
39   g_core = reinterpret_cast<const XW_CoreInterface*>(
40       get_interface(XW_CORE_INTERFACE));
41   if (!g_core) {
42     std::cerr << "Can't initialize extension: error getting Core interface.\n";
43     return XW_ERROR;
44   }
45   g_core->SetExtensionName(extension, name);
46   g_core->SetJavaScriptAPI(extension, api);
47   g_core->RegisterInstanceCallbacks(extension, created, destroyed);
48
49   g_messaging = reinterpret_cast<const XW_MessagingInterface*>(
50       get_interface(XW_MESSAGING_INTERFACE));
51   if (!g_messaging) {
52     std::cerr <<
53         "Can't initialize extension: error getting Messaging interface.\n";
54     return XW_ERROR;
55   }
56   g_messaging->Register(extension, handle_message);
57
58   g_sync_messaging =
59       reinterpret_cast<const XW_Internal_SyncMessagingInterface*>(
60           get_interface(XW_INTERNAL_SYNC_MESSAGING_INTERFACE));
61   if (!g_sync_messaging) {
62     std::cerr <<
63         "Can't initialize extension: error getting Messaging interface.\n";
64     return XW_ERROR;
65   }
66   g_sync_messaging->Register(extension, handle_sync_message);
67
68   g_entry_points = reinterpret_cast<const XW_Internal_EntryPointsInterface*>(
69       get_interface(XW_INTERNAL_ENTRY_POINTS_INTERFACE));
70   if (!g_entry_points) {
71     std::cerr << "NOTE: Entry points interface not available in this version "
72               << "of Crosswalk, ignoring entry point data for extensions.\n";
73   } else {
74     g_entry_points->SetExtraJSEntryPoints(extension, entry_points);
75   }
76
77   return XW_OK;
78 }
79
80 void PostMessage(XW_Instance instance, const char* message) {
81   g_messaging->PostMessage(instance, message);
82 }
83
84 void SetSyncReply(XW_Instance instance, const char* reply) {
85   g_sync_messaging->SetSyncReply(instance, reply);
86 }
87
88 }  // namespace internal