blob: 23c7f011f9c450da12563901a57250b25f5633fb [file] [log] [blame]
Grace Klobafbe47c02009-05-14 17:31:45 -07001/*
2 * Copyright 2008, The Android Open Source Project
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include <stdlib.h>
27#include <string.h>
28#include <stdio.h>
29#include "main.h"
30#include "PluginObject.h"
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -040031#include "AnimationPlugin.h"
Derek Sollenbergerf42e2f42009-06-26 11:42:46 -040032#include "AudioPlugin.h"
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -040033#include "BackgroundPlugin.h"
Derek Sollenbergerdb398a72009-06-29 13:53:04 -040034#include "FormPlugin.h"
Grace Klobafbe47c02009-05-14 17:31:45 -070035#include "android_npapi.h"
36
37NPNetscapeFuncs* browser;
38#define EXPORT __attribute__((visibility("default")))
39
Derek Sollenberger9119e7d2009-06-08 10:53:09 -040040NPError NPP_New(NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc,
Grace Klobafbe47c02009-05-14 17:31:45 -070041 char* argn[], char* argv[], NPSavedData* saved);
42NPError NPP_Destroy(NPP instance, NPSavedData** save);
43NPError NPP_SetWindow(NPP instance, NPWindow* window);
Derek Sollenberger9119e7d2009-06-08 10:53:09 -040044NPError NPP_NewStream(NPP instance, NPMIMEType type, NPStream* stream,
Grace Klobafbe47c02009-05-14 17:31:45 -070045 NPBool seekable, uint16* stype);
46NPError NPP_DestroyStream(NPP instance, NPStream* stream, NPReason reason);
47int32 NPP_WriteReady(NPP instance, NPStream* stream);
Derek Sollenberger9119e7d2009-06-08 10:53:09 -040048int32 NPP_Write(NPP instance, NPStream* stream, int32 offset, int32 len,
Grace Klobafbe47c02009-05-14 17:31:45 -070049 void* buffer);
50void NPP_StreamAsFile(NPP instance, NPStream* stream, const char* fname);
51void NPP_Print(NPP instance, NPPrint* platformPrint);
52int16 NPP_HandleEvent(NPP instance, void* event);
Derek Sollenberger9119e7d2009-06-08 10:53:09 -040053void NPP_URLNotify(NPP instance, const char* URL, NPReason reason,
Grace Klobafbe47c02009-05-14 17:31:45 -070054 void* notifyData);
55NPError NPP_GetValue(NPP instance, NPPVariable variable, void *value);
56NPError NPP_SetValue(NPP instance, NPNVariable variable, void *value);
57
58extern "C" {
59EXPORT NPError NP_Initialize(NPNetscapeFuncs* browserFuncs, NPPluginFuncs* pluginFuncs, void *java_env, void *application_context);
60EXPORT NPError NP_GetValue(NPP instance, NPPVariable variable, void *value);
61EXPORT const char* NP_GetMIMEDescription(void);
Derek Sollenberger9119e7d2009-06-08 10:53:09 -040062EXPORT void NP_Shutdown(void);
Grace Klobafbe47c02009-05-14 17:31:45 -070063};
64
65ANPAudioTrackInterfaceV0 gSoundI;
Mike Reed224adad2009-06-10 10:24:01 -040066ANPBitmapInterfaceV0 gBitmapI;
Grace Klobafbe47c02009-05-14 17:31:45 -070067ANPCanvasInterfaceV0 gCanvasI;
68ANPLogInterfaceV0 gLogI;
69ANPPaintInterfaceV0 gPaintI;
70ANPPathInterfaceV0 gPathI;
71ANPTypefaceInterfaceV0 gTypefaceI;
Derek Sollenberger5b011e32009-06-22 11:39:40 -040072ANPWindowInterfaceV0 gWindowI;
Grace Klobafbe47c02009-05-14 17:31:45 -070073
74#define ARRAY_COUNT(array) (sizeof(array) / sizeof(array[0]))
Derek Sollenberger5b011e32009-06-22 11:39:40 -040075#define DEBUG_PLUGIN_EVENTS 0
Grace Klobafbe47c02009-05-14 17:31:45 -070076
77NPError NP_Initialize(NPNetscapeFuncs* browserFuncs, NPPluginFuncs* pluginFuncs, void *java_env, void *application_context)
78{
79 // Make sure we have a function table equal or larger than we are built against.
80 if (browserFuncs->size < sizeof(NPNetscapeFuncs)) {
81 return NPERR_GENERIC_ERROR;
82 }
Derek Sollenberger9119e7d2009-06-08 10:53:09 -040083
Grace Klobafbe47c02009-05-14 17:31:45 -070084 // Copy the function table (structure)
85 browser = (NPNetscapeFuncs*) malloc(sizeof(NPNetscapeFuncs));
86 memcpy(browser, browserFuncs, sizeof(NPNetscapeFuncs));
Derek Sollenberger9119e7d2009-06-08 10:53:09 -040087
Grace Klobafbe47c02009-05-14 17:31:45 -070088 // Build the plugin function table
89 pluginFuncs->version = 11;
90 pluginFuncs->size = sizeof(pluginFuncs);
91 pluginFuncs->newp = NPP_New;
92 pluginFuncs->destroy = NPP_Destroy;
93 pluginFuncs->setwindow = NPP_SetWindow;
94 pluginFuncs->newstream = NPP_NewStream;
95 pluginFuncs->destroystream = NPP_DestroyStream;
96 pluginFuncs->asfile = NPP_StreamAsFile;
97 pluginFuncs->writeready = NPP_WriteReady;
98 pluginFuncs->write = (NPP_WriteProcPtr)NPP_Write;
99 pluginFuncs->print = NPP_Print;
100 pluginFuncs->event = NPP_HandleEvent;
101 pluginFuncs->urlnotify = NPP_URLNotify;
102 pluginFuncs->getvalue = NPP_GetValue;
103 pluginFuncs->setvalue = NPP_SetValue;
104
105 static const struct {
106 NPNVariable v;
107 uint32_t size;
108 ANPInterface* i;
109 } gPairs[] = {
Mike Reed224adad2009-06-10 10:24:01 -0400110 { kBitmapInterfaceV0_ANPGetValue, sizeof(gBitmapI), &gBitmapI },
Grace Klobafbe47c02009-05-14 17:31:45 -0700111 { kCanvasInterfaceV0_ANPGetValue, sizeof(gCanvasI), &gCanvasI },
Mike Reed224adad2009-06-10 10:24:01 -0400112 { kLogInterfaceV0_ANPGetValue, sizeof(gLogI), &gLogI },
Grace Klobafbe47c02009-05-14 17:31:45 -0700113 { kPaintInterfaceV0_ANPGetValue, sizeof(gPaintI), &gPaintI },
114 { kPathInterfaceV0_ANPGetValue, sizeof(gPathI), &gPathI },
115 { kTypefaceInterfaceV0_ANPGetValue, sizeof(gPaintI), &gTypefaceI },
116 { kAudioTrackInterfaceV0_ANPGetValue, sizeof(gSoundI), &gSoundI },
Derek Sollenberger5b011e32009-06-22 11:39:40 -0400117 { kWindowInterfaceV0_ANPGetValue, sizeof(gWindowI), &gWindowI },
Grace Klobafbe47c02009-05-14 17:31:45 -0700118 };
119 for (size_t i = 0; i < ARRAY_COUNT(gPairs); i++) {
120 gPairs[i].i->inSize = gPairs[i].size;
121 NPError err = browser->getvalue(NULL, gPairs[i].v, gPairs[i].i);
122 if (err) {
123 return err;
124 }
125 }
Derek Sollenberger9119e7d2009-06-08 10:53:09 -0400126
Grace Klobafbe47c02009-05-14 17:31:45 -0700127 return NPERR_NO_ERROR;
128}
129
130void NP_Shutdown(void)
131{
132
133}
134
Derek Sollenberger9119e7d2009-06-08 10:53:09 -0400135const char *NP_GetMIMEDescription(void)
Grace Klobafbe47c02009-05-14 17:31:45 -0700136{
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400137 return "application/x-testbrowserplugin:tst:Test plugin mimetype is application/x-testbrowserplugin";
Grace Klobafbe47c02009-05-14 17:31:45 -0700138}
139
140NPError NPP_New(NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc,
141 char* argn[], char* argv[], NPSavedData* saved)
142{
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400143
144 /* BEGIN: STANDARD PLUGIN FRAMEWORK */
Grace Klobafbe47c02009-05-14 17:31:45 -0700145 PluginObject *obj = NULL;
146
147 // Scripting functions appeared in NPAPI version 14
148 if (browser->version >= 14) {
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400149 instance->pdata = browser->createobject (instance, getPluginClass());
150 obj = static_cast<PluginObject*>(instance->pdata);
151 bzero(obj, sizeof(*obj));
Grace Klobafbe47c02009-05-14 17:31:45 -0700152 }
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400153 /* END: STANDARD PLUGIN FRAMEWORK */
Derek Sollenberger9119e7d2009-06-08 10:53:09 -0400154
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400155 // select the drawing model based on user input
Grace Klobafbe47c02009-05-14 17:31:45 -0700156 ANPDrawingModel model = kBitmap_ANPDrawingModel;
157
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400158 for (int i = 0; i < argc; i++) {
Grace Klobafbe47c02009-05-14 17:31:45 -0700159 if (!strcmp(argn[i], "DrawingModel")) {
160 if (!strcmp(argv[i], "Bitmap")) {
161 model = kBitmap_ANPDrawingModel;
162 }
163 if (!strcmp(argv[i], "Canvas")) {
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400164 //TODO support drawing on canvas instead of bitmap
Grace Klobafbe47c02009-05-14 17:31:45 -0700165 }
166 gLogI.log(instance, kDebug_ANPLogType, "------ %p DrawingModel is %d", instance, model);
167 break;
168 }
169 }
170
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400171 // comment this out to use the default model (bitmaps)
172 NPError err = browser->setvalue(instance, kRequestDrawingModel_ANPSetValue,
Grace Klobafbe47c02009-05-14 17:31:45 -0700173 reinterpret_cast<void*>(model));
174 if (err) {
175 gLogI.log(instance, kError_ANPLogType, "request model %d err %d", model, err);
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400176 return err;
Grace Klobafbe47c02009-05-14 17:31:45 -0700177 }
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400178
179 // select the pluginType
180 for (int i = 0; i < argc; i++) {
181 if (!strcmp(argn[i], "PluginType")) {
182 if (!strcmp(argv[i], "Animation")) {
183 obj->pluginType = kAnimation_PluginType;
184 obj->activePlugin = new BallAnimation(instance);
185 }
186 else if (!strcmp(argv[i], "Audio")) {
187 obj->pluginType = kAudio_PluginType;
Derek Sollenbergerf42e2f42009-06-26 11:42:46 -0400188 obj->activePlugin = new AudioPlugin(instance);
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400189 }
190 else if (!strcmp(argv[i], "Background")) {
191 obj->pluginType = kBackground_PluginType;
192 obj->activePlugin = new BackgroundPlugin(instance);
193 }
Derek Sollenbergerdb398a72009-06-29 13:53:04 -0400194 else if (!strcmp(argv[i], "Form")) {
195 obj->pluginType = kForm_PluginType;
196 obj->activePlugin = new FormPlugin(instance);
197 }
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400198 gLogI.log(instance, kDebug_ANPLogType, "------ %p PluginType is %d", instance, obj->pluginType);
199 break;
200 }
201 }
202
203 // if no pluginType is specified then default to Animation
204 if (!obj->pluginType) {
205 obj->pluginType = kAnimation_PluginType;
206 obj->activePlugin = new BallAnimation(instance);
207 }
208
209 return NPERR_NO_ERROR;
Grace Klobafbe47c02009-05-14 17:31:45 -0700210}
211
212NPError NPP_Destroy(NPP instance, NPSavedData** save)
213{
214 PluginObject *obj = (PluginObject*) instance->pdata;
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400215 delete obj->activePlugin;
Grace Klobafbe47c02009-05-14 17:31:45 -0700216
217 return NPERR_NO_ERROR;
218}
219
Grace Klobafbe47c02009-05-14 17:31:45 -0700220NPError NPP_SetWindow(NPP instance, NPWindow* window)
221{
222 PluginObject *obj = (PluginObject*) instance->pdata;
Derek Sollenberger9119e7d2009-06-08 10:53:09 -0400223
Grace Klobafbe47c02009-05-14 17:31:45 -0700224 // Do nothing if browser didn't support NPN_CreateObject which would have created the PluginObject.
225 if (obj != NULL) {
226 obj->window = window;
227 }
Derek Sollenberger9119e7d2009-06-08 10:53:09 -0400228
Grace Klobafbe47c02009-05-14 17:31:45 -0700229 browser->invalidaterect(instance, NULL);
230
231 return NPERR_NO_ERROR;
232}
Derek Sollenberger9119e7d2009-06-08 10:53:09 -0400233
Grace Klobafbe47c02009-05-14 17:31:45 -0700234NPError NPP_NewStream(NPP instance, NPMIMEType type, NPStream* stream, NPBool seekable, uint16* stype)
235{
236 *stype = NP_ASFILEONLY;
237 return NPERR_NO_ERROR;
238}
239
240NPError NPP_DestroyStream(NPP instance, NPStream* stream, NPReason reason)
241{
242 return NPERR_NO_ERROR;
243}
244
245int32 NPP_WriteReady(NPP instance, NPStream* stream)
246{
247 return 0;
248}
249
250int32 NPP_Write(NPP instance, NPStream* stream, int32 offset, int32 len, void* buffer)
251{
252 return 0;
253}
254
255void NPP_StreamAsFile(NPP instance, NPStream* stream, const char* fname)
256{
257}
258
259void NPP_Print(NPP instance, NPPrint* platformPrint)
260{
Grace Klobafbe47c02009-05-14 17:31:45 -0700261}
262
Grace Klobafbe47c02009-05-14 17:31:45 -0700263int16 NPP_HandleEvent(NPP instance, void* event)
264{
265 PluginObject *obj = reinterpret_cast<PluginObject*>(instance->pdata);
266 const ANPEvent* evt = reinterpret_cast<const ANPEvent*>(event);
267
Derek Sollenberger5b011e32009-06-22 11:39:40 -0400268#if DEBUG_PLUGIN_EVENTS
Grace Klobafbe47c02009-05-14 17:31:45 -0700269 switch (evt->eventType) {
270 case kDraw_ANPEventType:
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400271
272 if (evt->data.draw.model == kBitmap_ANPDrawingModel) {
273
274 static ANPBitmapFormat currentFormat = -1;
275 if (evt->data.draw.data.bitmap.format != currentFormat) {
276 currentFormat = evt->data.draw.data.bitmap.format;
277 gLogI.log(instance, kDebug_ANPLogType, "---- %p Draw (bitmap)"
278 " clip=%d,%d,%d,%d format=%d", instance,
279 evt->data.draw.clip.left,
280 evt->data.draw.clip.top,
281 evt->data.draw.clip.right,
282 evt->data.draw.clip.bottom,
283 evt->data.draw.data.bitmap.format);
284 }
Grace Klobafbe47c02009-05-14 17:31:45 -0700285 }
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400286 break;
Grace Klobafbe47c02009-05-14 17:31:45 -0700287
288 case kKey_ANPEventType:
289 gLogI.log(instance, kDebug_ANPLogType, "---- %p Key action=%d"
290 " code=%d vcode=%d unichar=%d repeat=%d mods=%x", instance,
291 evt->data.key.action,
292 evt->data.key.nativeCode,
293 evt->data.key.virtualCode,
294 evt->data.key.unichar,
295 evt->data.key.repeatCount,
296 evt->data.key.modifiers);
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400297 break;
Derek Sollenberger9119e7d2009-06-08 10:53:09 -0400298
299 case kLifecycle_ANPEventType:
300 gLogI.log(instance, kDebug_ANPLogType, "---- %p Lifecycle action=%d",
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400301 instance, evt->data.lifecycle.action);
Mike Reed1e7c3312009-05-26 16:37:45 -0400302 break;
Grace Klobafbe47c02009-05-14 17:31:45 -0700303
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400304 case kTouch_ANPEventType:
Grace Klobafbe47c02009-05-14 17:31:45 -0700305 gLogI.log(instance, kDebug_ANPLogType, "---- %p Touch action=%d [%d %d]",
306 instance, evt->data.touch.action, evt->data.touch.x,
307 evt->data.touch.y);
Derek Sollenberger5b011e32009-06-22 11:39:40 -0400308 break;
309
Derek Sollenbergerdb398a72009-06-29 13:53:04 -0400310 case kMouse_ANPEventType:
311 gLogI.log(instance, kDebug_ANPLogType, "---- %p Mouse action=%d [%d %d]",
312 instance, evt->data.mouse.action, evt->data.mouse.x,
313 evt->data.mouse.y);
314 break;
315
Derek Sollenberger5b011e32009-06-22 11:39:40 -0400316 case kVisibleRect_ANPEventType:
317 gLogI.log(instance, kDebug_ANPLogType, "---- %p VisibleRect [%d %d %d %d]",
Derek Sollenbergerf42e2f42009-06-26 11:42:46 -0400318 instance, evt->data.visibleRect.rect.left, evt->data.visibleRect.rect.top,
319 evt->data.visibleRect.rect.right, evt->data.visibleRect.rect.bottom);
Derek Sollenberger5b011e32009-06-22 11:39:40 -0400320 break;
Grace Klobafbe47c02009-05-14 17:31:45 -0700321
322 default:
Derek Sollenberger5b011e32009-06-22 11:39:40 -0400323 gLogI.log(instance, kError_ANPLogType, "---- %p Unknown Event [%d]",
324 instance, evt->eventType);
Grace Klobafbe47c02009-05-14 17:31:45 -0700325 break;
326 }
Derek Sollenberger5b011e32009-06-22 11:39:40 -0400327#endif
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400328
329 if(!obj->activePlugin) {
330 gLogI.log(instance, kError_ANPLogType, "the active plugin is null.");
331 return 0; // unknown or unhandled event
332 }
333 else {
334 return obj->activePlugin->handleEvent(evt);
335 }
Grace Klobafbe47c02009-05-14 17:31:45 -0700336}
337
338void NPP_URLNotify(NPP instance, const char* url, NPReason reason, void* notifyData)
339{
340
341}
342
343EXPORT NPError NP_GetValue(NPP instance, NPPVariable variable, void *value) {
344
345 if (variable == NPPVpluginNameString) {
346 const char **str = (const char **)value;
347 *str = "Test Plugin";
348 return NPERR_NO_ERROR;
349 }
Derek Sollenberger9119e7d2009-06-08 10:53:09 -0400350
Grace Klobafbe47c02009-05-14 17:31:45 -0700351 if (variable == NPPVpluginDescriptionString) {
352 const char **str = (const char **)value;
353 *str = "Description of Test Plugin";
354 return NPERR_NO_ERROR;
355 }
Derek Sollenberger9119e7d2009-06-08 10:53:09 -0400356
Grace Klobafbe47c02009-05-14 17:31:45 -0700357 return NPERR_GENERIC_ERROR;
358}
359
360NPError NPP_GetValue(NPP instance, NPPVariable variable, void *value)
361{
362 if (variable == NPPVpluginScriptableNPObject) {
363 void **v = (void **)value;
364 PluginObject *obj = (PluginObject*) instance->pdata;
Derek Sollenberger9119e7d2009-06-08 10:53:09 -0400365
Grace Klobafbe47c02009-05-14 17:31:45 -0700366 if (obj)
367 browser->retainobject((NPObject*)obj);
Derek Sollenberger9119e7d2009-06-08 10:53:09 -0400368
Grace Klobafbe47c02009-05-14 17:31:45 -0700369 *v = obj;
370 return NPERR_NO_ERROR;
371 }
Derek Sollenberger9119e7d2009-06-08 10:53:09 -0400372
Grace Klobafbe47c02009-05-14 17:31:45 -0700373 return NPERR_GENERIC_ERROR;
374}
375
376NPError NPP_SetValue(NPP instance, NPNVariable variable, void *value)
377{
378 return NPERR_GENERIC_ERROR;
379}
380