Tizen 2.0 Release
[framework/osp/web.git] / src / controls / js-bridge-plugin-tizen / FWebCtrl_JsBridgePlugin.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file                FWebCtrl_JsBridgePlugin.cpp
20  * @brief               The file contains the implmentation of npapi plugin callbacks.
21  *
22  * The file contains the implmentation of npapi plugin callbacks.
23  */
24
25 #define XP_TIZEN 1
26 #define XP_UNIX 1
27 #define MOZ_X11 1
28 #define ENABLE_TIZEN_JSBRIDGE_PLUGIN 1
29 #define HAS_SCRIPT_SUPPORT 1
30
31 #include <string.h>
32 #include "FWebCtrl_JsBridgePlugin.h"
33
34 #ifdef __PLUGIN_DEBUG_PRINT
35 #include <dlog.h>
36 #ifdef LOG_TAG
37 #undef LOG_TAG
38 #endif
39 #define LOG_TAG "JsBridgePlugin"
40 #define __PLUGIN_DEBUG_TRACE(format, ARG...)  \
41         { \
42                 LOGD(format "[%d]", ## ARG, __LINE__); \
43         }
44 #else
45 #define __PLUGIN_DEBUG_TRACE(format, ARG...);
46 #endif
47
48 static NPObject*
49 _CreateScriptPlayer(NPP instance, NPClass* pNPClass)
50 {
51
52         // set up our our instance data
53         requestToNativeScriptPlayer = g_pBrowserFuncs->getstringidentifier("requestToNative");
54         RuntimeObject* pObject = (RuntimeObject*) g_pBrowserFuncs->memalloc(sizeof(RuntimeObject));
55         if (pObject != NULL)
56         {
57                 pObject->instance = instance;
58                 instance->pdata = pObject;
59         }
60
61         return (NPObject*) pObject;
62 }
63
64 static void
65 _DestroyScriptPlayer(NPObject* pNPobj)
66 {
67         RuntimeObject* pObj = (RuntimeObject*) pNPobj;
68         if (pObj)
69         {
70                 pObj->instance->pdata = NULL;
71                 g_pBrowserFuncs->memfree(pObj);
72         }
73 }
74
75 static bool
76 _HasMethod(NPObject* pNPobj, NPIdentifier name)
77 {
78         return name == requestToNativeScriptPlayer;
79 }
80
81 static bool
82 _Invoke(NPObject* pNPobj, NPIdentifier name, const NPVariant* pArgs, uint32_t argCount, NPVariant* result)
83 {
84         if (pArgs->type == NPVariantType_String)
85         {
86                 char* pString = (char*) g_pBrowserFuncs->memalloc(pArgs->value.stringValue.UTF8Length + 1);
87
88                 if (pString != NULL)
89                 {
90                         memset(pString, 0x00, pArgs->value.stringValue.UTF8Length + 1);
91                         memcpy(pString, pArgs->value.stringValue.UTF8Characters, pArgs->value.stringValue.UTF8Length);
92
93                         RuntimeObject* pObj = (RuntimeObject*) pNPobj;
94                         g_pBrowserFuncs->setvalue(pObj->instance, NPPAppNotify, pString);
95                         g_pBrowserFuncs->memfree(pString);
96                 }
97         }
98
99         return true;
100 }
101
102 #ifdef OJI
103 JRIGlobalRef
104 GetJavaClassOfPlugin(void)
105 {
106         jref clazz = NULL; //NPP_GetJavaClass();
107         if (clazz)
108         {
109                 JRIEnv* pEnv = g_pBrowserFuncs->getJavaEnv();
110                 return JRI_NewGlobalRef(pEnv, clazz);
111         }
112         return NULL;
113 }
114 #endif
115
116 struct NPClass classScriptPlayer = {
117         NP_CLASS_STRUCT_VERSION,   /* uint32_t structVersion; */
118         _CreateScriptPlayer,       /* NPAllocateFunctionPtr allocate; */
119         _DestroyScriptPlayer,      /* NPDeallocateFunctionPtr deallocate; */
120         NULL,                      /* NPInvalidateFunctionPtr invalidate; */
121         _HasMethod,                /* NPHasMethodFunctionPtr hasMethod; */
122         _Invoke,                   /* NPInvokeFunctionPtr invoke; */
123         NULL,                      /* NPInvokeDefaultFunctionPtr invokeDefault; */
124         NULL,                      /* NPHasPropertyFunctionPtr hasProperty; */
125         NULL,                      /* NPGetPropertyFunctionPtr getProperty; */
126         NULL,                      /* NPSetPropertyFunctionPtr setProperty; */
127         NULL,                      /* NPRemovePropertyFunctionPtr removeProperty; */
128 };
129
130 NPError
131 NP_Initialize(NPNetscapeFuncs* pNsTable, NPPluginFuncs* pFuncs)
132 {
133
134         __PLUGIN_DEBUG_TRACE("%s", __FUNCTION__);
135         NPError err = NPERR_INVALID_PARAM;
136
137         if (pNsTable != NULL && pFuncs != NULL)
138         {
139                 g_pBrowserFuncs = pNsTable;
140
141                 //
142                 // Set up the plugin function table that Netscape will use to
143                 // call us.  Netscape needs to know about our version and size
144                 // and have a UniversalProcPointer for every function we
145                 // implement.
146                 //
147                 pFuncs->version = (NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR;
148                 pFuncs->size = sizeof(NPPluginFuncs);
149                 pFuncs->newp = Plugin_New;
150                 pFuncs->destroy = Plugin_Destroy;
151                 pFuncs->setwindow = Plugin_SetWindow;
152                 pFuncs->newstream = Plugin_NewStream;
153                 pFuncs->destroystream = Plugin_DestroyStream;
154                 pFuncs->asfile = Plugin_StreamAsFile;
155                 pFuncs->writeready = Plugin_WriteReady;
156                 pFuncs->write = Plugin_Write;
157                 pFuncs->print = Plugin_Print;
158                 pFuncs->event = Plugin_HandleEvent;
159                 pFuncs->urlnotify = Plugin_URLNotify;
160
161 #ifdef OJI
162                 pFuncs->javaClass = GetJavaClassOfPlugin();
163 #endif
164                 pFuncs->getvalue = Plugin_GetValue;
165                 pFuncs->setvalue = Plugin_SetValue;
166                 err = NPERR_NO_ERROR;
167         }
168         return err;
169 }
170
171 char*
172 NP_GetMIMEDescription()
173 {
174         __PLUGIN_DEBUG_TRACE("%s", __FUNCTION__);
175         return (char*) "application/x-tizen-jsbridge:jsb:TizenJsBridgePlugin";
176 }
177
178 NPError
179 NP_GetValue(void* pFuture, NPPVariable variable, void* pValue)
180 {
181         __PLUGIN_DEBUG_TRACE("%s", __FUNCTION__);
182         return Plugin_GetValue(static_cast< NPP_t* >(pFuture), variable, pValue);
183 }
184
185 NPError
186 NP_Shutdown(void)
187 {
188         __PLUGIN_DEBUG_TRACE("%s", __FUNCTION__);
189         return NPERR_NO_ERROR;
190 }
191
192 NPError
193 Plugin_New(NPMIMEType pluginType, NPP instance, uint16_t mode, int16_t argc, char* argn[], char* argv[], NPSavedData* saved)
194 {
195         __PLUGIN_DEBUG_TRACE("%s", __FUNCTION__);
196         if (instance != NULL)
197         {
198                 instance->pdata = NULL;
199         }
200         g_pBrowserFuncs->setvalue(instance, NPPVpluginWindowBool, (void*)false);
201         return NPERR_NO_ERROR;
202 }
203
204 NPError
205 Plugin_Destroy(NPP instance, NPSavedData** save)
206 {
207         __PLUGIN_DEBUG_TRACE("%s", __FUNCTION__);
208         NPObject* pObj = (NPObject*) instance->pdata;
209
210         if (pObj)
211         {
212                 g_pBrowserFuncs->releaseobject(pObj);
213         }
214         return NPERR_NO_ERROR;
215 }
216
217 NPError
218 Plugin_SetWindow(NPP instance, NPWindow* window)
219 {
220         __PLUGIN_DEBUG_TRACE("%s", __FUNCTION__);
221         return NPERR_NO_ERROR;
222 }
223
224 NPError
225 Plugin_NewStream(NPP instance, NPMIMEType type, NPStream* stream, NPBool seekable, uint16_t* stype)
226 {
227         return NPERR_NO_ERROR;
228 }
229
230 NPError
231 Plugin_DestroyStream(NPP instance, NPStream* stream, NPReason reason)
232 {
233         return NPERR_NO_ERROR;
234 }
235
236 int32_t
237 Plugin_WriteReady(NPP instance, NPStream* stream)
238 {
239         return 0;
240 }
241
242 int32_t
243 Plugin_Write(NPP instance, NPStream* stream, int32_t offset, int32_t len, void* buffer)
244 {
245         return 0;
246 }
247
248 void
249 Plugin_StreamAsFile(NPP instance, NPStream* stream, const char* fname)
250 {
251
252 }
253
254 void
255 Plugin_Print(NPP instance, NPPrint* platformPrint)
256 {
257
258 }
259
260 int16_t
261 Plugin_HandleEvent(NPP instance, void* event)
262 {
263         return 1;
264 }
265
266 void
267 Plugin_URLNotify(NPP instance, const char* URL, NPReason reason, void* notifyData)
268 {
269
270 }
271
272 NPError
273 Plugin_GetValue(NPP instance, NPPVariable variable, void* pValue)
274 {
275         __PLUGIN_DEBUG_TRACE("%s", __FUNCTION__);
276
277         NPObject* pNPObject = NULL;
278         NPError retErr = NPERR_INVALID_PARAM;
279
280         if (pValue != NULL)
281         {
282                 retErr = NPERR_NO_ERROR;
283                 switch (variable)
284                 {
285                 case NPPVpluginNeedsXEmbed:
286                 {
287                         *((bool*) pValue) = true;
288                         break;
289                 }
290
291                 case NPPVpluginNameString:
292                 {
293                         static char name[] = "x-tizen-jsbridge";
294                         *((char**) pValue) = name;
295                         break;
296                 }
297
298                 case NPPVpluginDescriptionString:
299                 {
300                         static char desc[] = "The Plugin for x-tizen-jsbridge";
301                         *((char**) pValue) = desc;
302                         break;
303                 }
304
305                 case NPPVpluginScriptableNPObject:
306                 {
307                         pNPObject = g_pBrowserFuncs->createobject(instance, &classScriptPlayer);
308                         g_pBrowserFuncs->retainobject(pNPObject);
309                         *((NPObject**) pValue) = pNPObject;
310                         break;
311                 }
312
313                 default:
314                 {
315                         retErr = NPERR_INVALID_PARAM;
316                         break;
317                 }
318                 }
319         }
320         return retErr;
321 }
322
323 NPError
324 Plugin_SetValue(NPP instance, NPNVariable variable, void* value)
325 {
326         return NPERR_NO_ERROR;
327 }