Initialize Tizen 2.3
[framework/web/wrt-plugins-common.git] / src_wearable / standards / W3C / Widget / plugin_initializer.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /**
17  *
18  * @file       plugin_initializer.cpp
19  * @author     Grzegorz Krawczyk (g.krawczyk@samsung.com)
20  * @version    0.1
21  * @brief
22  */
23
24 #include <dpl/log/secure_log.h>
25
26 #include <Commons/plugin_initializer_def.h>
27 #include <Commons/WrtAccess/WrtAccess.h>
28 #include <ExportedApi.h>
29 #include <ObjectFactory.h>
30 #include <Plugin.h>
31 #include <PluginRegistration.h>
32
33 #include "JSWidget.h"
34 #include "JSPreferences.h"
35
36 #include "plugin_config.h"
37
38 #define OBJECT_WIDGET "widget"
39 #define INTERFACE_WIDGET_NAME "Widget"
40
41 using namespace WrtPlugins::W3C;
42 using namespace WrtDeviceApis;
43 using namespace WrtDeviceApis::Commons;
44 using namespace WrtPluginsApi;
45
46 namespace Options {
47 class_definition_options_t WidgetOptions = {
48     JS_CLASS,
49     CREATE_INSTANCE,
50     NONE_NOTICE,
51     IGNORED,
52     NULL,
53     NULL,
54     NULL
55 };
56
57 class_definition_options_t WidgetInterfaceOptions = {
58     JS_INTERFACE,
59     CREATE_INSTANCE,
60     NONE_NOTICE,
61     IGNORED, //ignored
62     NULL,
63     NULL,
64     NULL
65 };
66 }
67
68 //TODO: remove this after switch to wrt-plugin-installer
69 void on_widget_init_callback(feature_mapping_interface_t *mapping)
70 {
71     _D("[W3C\\widget] on_widget_init_callback");
72
73     WrtPlugins::W3C::WidgetDeclarations::getMappingInterface(mapping);
74 }
75 //...
76
77 void on_widget_start_callback(int widgetId)
78 {
79     _D("[W3C\\widget] on_widget_start_callback (%d)", widgetId);
80
81     Try
82     {
83         WrtAccessSingleton::Instance().initialize(widgetId);
84     }
85     Catch(Commons::Exception)
86     {
87         _E("Wrt wrapper registration failed");
88         return;
89     }
90 }
91
92 void on_widget_stop_callback(int widgetId)
93 {
94     _D("[W3C\\widget] on_widget_stop_callback (%d)", widgetId);
95     Try
96     {
97         WrtAccessSingleton::Instance().deinitialize(widgetId);
98     }
99     Catch(Commons::Exception)
100     {
101         _E("Wrt wrapper registration failed");
102         return;
103     }
104 }
105
106 void Register(PluginRegistration& r)
107 {
108     Plugin* plugin = new Plugin();
109
110     auto widgetObject = ObjectFactory::createObjectWithInterface(
111             OBJECT_WIDGET,
112             (js_class_template_getter) WrtPlugins::W3C::JSWidget::getClassRef,
113             INTERFACE_WIDGET_NAME,
114             reinterpret_cast<js_class_constructor_cb_t>(WrtPlugins::W3C::JSWidget::callAsConstructor));
115
116     plugin->AddObject(widgetObject);
117
118     r.Connect<OnWidgetStart>(on_widget_start_callback);
119     r.Connect<OnWidgetStop>(on_widget_stop_callback);
120
121     r.AddPlugin(*plugin);
122 }
123
124 void Unregister(PluginRegistration& r, Plugin* plugin)
125 {
126     r.DisconnectAll();
127     delete plugin;
128 }
129
130 void GetProvidedFeatures(feature_mapping_interface_t *mapping)
131 {
132     WrtPlugins::W3C::WidgetDeclarations::getMappingInterface(mapping);
133 }
134
135 ExportedApi dll_api={Register, Unregister, GetProvidedFeatures};
136
137 //TODO: remove this after switch to wrt-plugin-installer
138 PLUGIN_ON_WIDGET_START(on_widget_start_callback)
139 PLUGIN_ON_WIDGET_STOP(on_widget_stop_callback)
140 PLUGIN_ON_WIDGET_INIT(on_widget_init_callback)
141
142 PLUGIN_CLASS_MAP_BEGIN
143 PLUGIN_CLASS_MAP_ADD_INTERFACE(
144     WRT_JS_EXTENSION_OBJECT_GLOBAL,
145     INTERFACE_WIDGET_NAME,
146     (js_class_template_getter) WrtPlugins::W3C::JSWidget::getClassRef,
147     reinterpret_cast<js_class_constructor_cb_t>(WrtPlugins::W3C::JSWidget::
148                                                     callAsConstructor),
149     &Options::WidgetInterfaceOptions)
150 PLUGIN_CLASS_MAP_ADD_INTERFACE_PRODUCT(
151     WRT_JS_EXTENSION_OBJECT_GLOBAL,
152     OBJECT_WIDGET,
153     INTERFACE_WIDGET_NAME,
154     &Options::WidgetOptions)
155
156 PLUGIN_CLASS_MAP_END
157 //...
158
159 #undef OBJECT_WIDGET
160 #undef INTERFACE_WIDGET_NAME