blob: e2fb6d6ac5b048e671dfc6015edc708ee2d84f97 [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"
32#include "BackgroundPlugin.h"
Grace Klobafbe47c02009-05-14 17:31:45 -070033#include "android_npapi.h"
34
35NPNetscapeFuncs* browser;
36#define EXPORT __attribute__((visibility("default")))
37
Derek Sollenberger9119e7d2009-06-08 10:53:09 -040038NPError NPP_New(NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc,
Grace Klobafbe47c02009-05-14 17:31:45 -070039 char* argn[], char* argv[], NPSavedData* saved);
40NPError NPP_Destroy(NPP instance, NPSavedData** save);
41NPError NPP_SetWindow(NPP instance, NPWindow* window);
Derek Sollenberger9119e7d2009-06-08 10:53:09 -040042NPError NPP_NewStream(NPP instance, NPMIMEType type, NPStream* stream,
Grace Klobafbe47c02009-05-14 17:31:45 -070043 NPBool seekable, uint16* stype);
44NPError NPP_DestroyStream(NPP instance, NPStream* stream, NPReason reason);
45int32 NPP_WriteReady(NPP instance, NPStream* stream);
Derek Sollenberger9119e7d2009-06-08 10:53:09 -040046int32 NPP_Write(NPP instance, NPStream* stream, int32 offset, int32 len,
Grace Klobafbe47c02009-05-14 17:31:45 -070047 void* buffer);
48void NPP_StreamAsFile(NPP instance, NPStream* stream, const char* fname);
49void NPP_Print(NPP instance, NPPrint* platformPrint);
50int16 NPP_HandleEvent(NPP instance, void* event);
Derek Sollenberger9119e7d2009-06-08 10:53:09 -040051void NPP_URLNotify(NPP instance, const char* URL, NPReason reason,
Grace Klobafbe47c02009-05-14 17:31:45 -070052 void* notifyData);
53NPError NPP_GetValue(NPP instance, NPPVariable variable, void *value);
54NPError NPP_SetValue(NPP instance, NPNVariable variable, void *value);
55
56extern "C" {
57EXPORT NPError NP_Initialize(NPNetscapeFuncs* browserFuncs, NPPluginFuncs* pluginFuncs, void *java_env, void *application_context);
58EXPORT NPError NP_GetValue(NPP instance, NPPVariable variable, void *value);
59EXPORT const char* NP_GetMIMEDescription(void);
Derek Sollenberger9119e7d2009-06-08 10:53:09 -040060EXPORT void NP_Shutdown(void);
Grace Klobafbe47c02009-05-14 17:31:45 -070061};
62
63ANPAudioTrackInterfaceV0 gSoundI;
Mike Reed224adad2009-06-10 10:24:01 -040064ANPBitmapInterfaceV0 gBitmapI;
Grace Klobafbe47c02009-05-14 17:31:45 -070065ANPCanvasInterfaceV0 gCanvasI;
66ANPLogInterfaceV0 gLogI;
67ANPPaintInterfaceV0 gPaintI;
68ANPPathInterfaceV0 gPathI;
69ANPTypefaceInterfaceV0 gTypefaceI;
70
71#define ARRAY_COUNT(array) (sizeof(array) / sizeof(array[0]))
72
73NPError NP_Initialize(NPNetscapeFuncs* browserFuncs, NPPluginFuncs* pluginFuncs, void *java_env, void *application_context)
74{
75 // Make sure we have a function table equal or larger than we are built against.
76 if (browserFuncs->size < sizeof(NPNetscapeFuncs)) {
77 return NPERR_GENERIC_ERROR;
78 }
Derek Sollenberger9119e7d2009-06-08 10:53:09 -040079
Grace Klobafbe47c02009-05-14 17:31:45 -070080 // Copy the function table (structure)
81 browser = (NPNetscapeFuncs*) malloc(sizeof(NPNetscapeFuncs));
82 memcpy(browser, browserFuncs, sizeof(NPNetscapeFuncs));
Derek Sollenberger9119e7d2009-06-08 10:53:09 -040083
Grace Klobafbe47c02009-05-14 17:31:45 -070084 // Build the plugin function table
85 pluginFuncs->version = 11;
86 pluginFuncs->size = sizeof(pluginFuncs);
87 pluginFuncs->newp = NPP_New;
88 pluginFuncs->destroy = NPP_Destroy;
89 pluginFuncs->setwindow = NPP_SetWindow;
90 pluginFuncs->newstream = NPP_NewStream;
91 pluginFuncs->destroystream = NPP_DestroyStream;
92 pluginFuncs->asfile = NPP_StreamAsFile;
93 pluginFuncs->writeready = NPP_WriteReady;
94 pluginFuncs->write = (NPP_WriteProcPtr)NPP_Write;
95 pluginFuncs->print = NPP_Print;
96 pluginFuncs->event = NPP_HandleEvent;
97 pluginFuncs->urlnotify = NPP_URLNotify;
98 pluginFuncs->getvalue = NPP_GetValue;
99 pluginFuncs->setvalue = NPP_SetValue;
100
101 static const struct {
102 NPNVariable v;
103 uint32_t size;
104 ANPInterface* i;
105 } gPairs[] = {
Mike Reed224adad2009-06-10 10:24:01 -0400106 { kBitmapInterfaceV0_ANPGetValue, sizeof(gBitmapI), &gBitmapI },
Grace Klobafbe47c02009-05-14 17:31:45 -0700107 { kCanvasInterfaceV0_ANPGetValue, sizeof(gCanvasI), &gCanvasI },
Mike Reed224adad2009-06-10 10:24:01 -0400108 { kLogInterfaceV0_ANPGetValue, sizeof(gLogI), &gLogI },
Grace Klobafbe47c02009-05-14 17:31:45 -0700109 { kPaintInterfaceV0_ANPGetValue, sizeof(gPaintI), &gPaintI },
110 { kPathInterfaceV0_ANPGetValue, sizeof(gPathI), &gPathI },
111 { kTypefaceInterfaceV0_ANPGetValue, sizeof(gPaintI), &gTypefaceI },
112 { kAudioTrackInterfaceV0_ANPGetValue, sizeof(gSoundI), &gSoundI },
113 };
114 for (size_t i = 0; i < ARRAY_COUNT(gPairs); i++) {
115 gPairs[i].i->inSize = gPairs[i].size;
116 NPError err = browser->getvalue(NULL, gPairs[i].v, gPairs[i].i);
117 if (err) {
118 return err;
119 }
120 }
Derek Sollenberger9119e7d2009-06-08 10:53:09 -0400121
Grace Klobafbe47c02009-05-14 17:31:45 -0700122 return NPERR_NO_ERROR;
123}
124
125void NP_Shutdown(void)
126{
127
128}
129
Derek Sollenberger9119e7d2009-06-08 10:53:09 -0400130const char *NP_GetMIMEDescription(void)
Grace Klobafbe47c02009-05-14 17:31:45 -0700131{
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400132 return "application/x-testbrowserplugin:tst:Test plugin mimetype is application/x-testbrowserplugin";
Grace Klobafbe47c02009-05-14 17:31:45 -0700133}
134
135NPError NPP_New(NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc,
136 char* argn[], char* argv[], NPSavedData* saved)
137{
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400138
139 /* BEGIN: STANDARD PLUGIN FRAMEWORK */
Grace Klobafbe47c02009-05-14 17:31:45 -0700140 PluginObject *obj = NULL;
141
142 // Scripting functions appeared in NPAPI version 14
143 if (browser->version >= 14) {
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400144 instance->pdata = browser->createobject (instance, getPluginClass());
145 obj = static_cast<PluginObject*>(instance->pdata);
146 bzero(obj, sizeof(*obj));
Grace Klobafbe47c02009-05-14 17:31:45 -0700147 }
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400148 /* END: STANDARD PLUGIN FRAMEWORK */
Derek Sollenberger9119e7d2009-06-08 10:53:09 -0400149
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400150 // select the drawing model based on user input
Grace Klobafbe47c02009-05-14 17:31:45 -0700151 ANPDrawingModel model = kBitmap_ANPDrawingModel;
152
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400153 for (int i = 0; i < argc; i++) {
Grace Klobafbe47c02009-05-14 17:31:45 -0700154 if (!strcmp(argn[i], "DrawingModel")) {
155 if (!strcmp(argv[i], "Bitmap")) {
156 model = kBitmap_ANPDrawingModel;
157 }
158 if (!strcmp(argv[i], "Canvas")) {
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400159 //TODO support drawing on canvas instead of bitmap
Grace Klobafbe47c02009-05-14 17:31:45 -0700160 }
161 gLogI.log(instance, kDebug_ANPLogType, "------ %p DrawingModel is %d", instance, model);
162 break;
163 }
164 }
165
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400166 // comment this out to use the default model (bitmaps)
167 NPError err = browser->setvalue(instance, kRequestDrawingModel_ANPSetValue,
Grace Klobafbe47c02009-05-14 17:31:45 -0700168 reinterpret_cast<void*>(model));
169 if (err) {
170 gLogI.log(instance, kError_ANPLogType, "request model %d err %d", model, err);
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400171 return err;
Grace Klobafbe47c02009-05-14 17:31:45 -0700172 }
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400173
174 // select the pluginType
175 for (int i = 0; i < argc; i++) {
176 if (!strcmp(argn[i], "PluginType")) {
177 if (!strcmp(argv[i], "Animation")) {
178 obj->pluginType = kAnimation_PluginType;
179 obj->activePlugin = new BallAnimation(instance);
180 }
181 else if (!strcmp(argv[i], "Audio")) {
182 obj->pluginType = kAudio_PluginType;
183 //TODO add audio here
184 }
185 else if (!strcmp(argv[i], "Background")) {
186 obj->pluginType = kBackground_PluginType;
187 obj->activePlugin = new BackgroundPlugin(instance);
188 }
189 gLogI.log(instance, kDebug_ANPLogType, "------ %p PluginType is %d", instance, obj->pluginType);
190 break;
191 }
192 }
193
194 // if no pluginType is specified then default to Animation
195 if (!obj->pluginType) {
196 obj->pluginType = kAnimation_PluginType;
197 obj->activePlugin = new BallAnimation(instance);
198 }
199
200 return NPERR_NO_ERROR;
Grace Klobafbe47c02009-05-14 17:31:45 -0700201}
202
203NPError NPP_Destroy(NPP instance, NPSavedData** save)
204{
205 PluginObject *obj = (PluginObject*) instance->pdata;
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400206 delete obj->activePlugin;
Grace Klobafbe47c02009-05-14 17:31:45 -0700207 gSoundI.deleteTrack(obj->track);
208
209 return NPERR_NO_ERROR;
210}
211
Grace Klobafbe47c02009-05-14 17:31:45 -0700212NPError NPP_SetWindow(NPP instance, NPWindow* window)
213{
214 PluginObject *obj = (PluginObject*) instance->pdata;
Derek Sollenberger9119e7d2009-06-08 10:53:09 -0400215
Grace Klobafbe47c02009-05-14 17:31:45 -0700216 // Do nothing if browser didn't support NPN_CreateObject which would have created the PluginObject.
217 if (obj != NULL) {
218 obj->window = window;
219 }
Derek Sollenberger9119e7d2009-06-08 10:53:09 -0400220
Grace Klobafbe47c02009-05-14 17:31:45 -0700221 browser->invalidaterect(instance, NULL);
222
223 return NPERR_NO_ERROR;
224}
Derek Sollenberger9119e7d2009-06-08 10:53:09 -0400225
Grace Klobafbe47c02009-05-14 17:31:45 -0700226NPError NPP_NewStream(NPP instance, NPMIMEType type, NPStream* stream, NPBool seekable, uint16* stype)
227{
228 *stype = NP_ASFILEONLY;
229 return NPERR_NO_ERROR;
230}
231
232NPError NPP_DestroyStream(NPP instance, NPStream* stream, NPReason reason)
233{
234 return NPERR_NO_ERROR;
235}
236
237int32 NPP_WriteReady(NPP instance, NPStream* stream)
238{
239 return 0;
240}
241
242int32 NPP_Write(NPP instance, NPStream* stream, int32 offset, int32 len, void* buffer)
243{
244 return 0;
245}
246
247void NPP_StreamAsFile(NPP instance, NPStream* stream, const char* fname)
248{
249}
250
251void NPP_Print(NPP instance, NPPrint* platformPrint)
252{
Grace Klobafbe47c02009-05-14 17:31:45 -0700253}
254
255struct SoundPlay {
256 NPP instance;
257 ANPAudioTrack* track;
258 FILE* file;
259};
260
261static void audioCallback(ANPAudioEvent evt, void* user, ANPAudioBuffer* buffer) {
262 switch (evt) {
263 case kMoreData_ANPAudioEvent: {
264 SoundPlay* play = reinterpret_cast<SoundPlay*>(user);
265 size_t amount = fread(buffer->bufferData, 1, buffer->size, play->file);
266 buffer->size = amount;
267 if (amount == 0) {
268 gSoundI.stop(play->track);
269 fclose(play->file);
270 play->file = NULL;
271 // need to notify our main thread to delete the track now
272 }
273 break;
274 }
275 default:
276 break;
277 }
278}
279
280static ANPAudioTrack* createTrack(NPP instance, const char path[]) {
281 FILE* f = fopen(path, "r");
282 gLogI.log(instance, kWarning_ANPLogType, "--- path %s FILE %p", path, f);
283 if (NULL == f) {
284 return NULL;
285 }
286 SoundPlay* play = new SoundPlay;
287 play->file = f;
288 play->track = gSoundI.newTrack(44100, kPCM16Bit_ANPSampleFormat, 2, audioCallback, play);
289 if (NULL == play->track) {
290 fclose(f);
291 delete play;
292 return NULL;
293 }
294 return play->track;
295}
296
297int16 NPP_HandleEvent(NPP instance, void* event)
298{
299 PluginObject *obj = reinterpret_cast<PluginObject*>(instance->pdata);
300 const ANPEvent* evt = reinterpret_cast<const ANPEvent*>(event);
301
302 switch (evt->eventType) {
303 case kDraw_ANPEventType:
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400304
305 if (evt->data.draw.model == kBitmap_ANPDrawingModel) {
306
307 static ANPBitmapFormat currentFormat = -1;
308 if (evt->data.draw.data.bitmap.format != currentFormat) {
309 currentFormat = evt->data.draw.data.bitmap.format;
310 gLogI.log(instance, kDebug_ANPLogType, "---- %p Draw (bitmap)"
311 " clip=%d,%d,%d,%d format=%d", instance,
312 evt->data.draw.clip.left,
313 evt->data.draw.clip.top,
314 evt->data.draw.clip.right,
315 evt->data.draw.clip.bottom,
316 evt->data.draw.data.bitmap.format);
317 }
Grace Klobafbe47c02009-05-14 17:31:45 -0700318 }
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400319 break;
Grace Klobafbe47c02009-05-14 17:31:45 -0700320
321 case kKey_ANPEventType:
322 gLogI.log(instance, kDebug_ANPLogType, "---- %p Key action=%d"
323 " code=%d vcode=%d unichar=%d repeat=%d mods=%x", instance,
324 evt->data.key.action,
325 evt->data.key.nativeCode,
326 evt->data.key.virtualCode,
327 evt->data.key.unichar,
328 evt->data.key.repeatCount,
329 evt->data.key.modifiers);
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400330 break;
Derek Sollenberger9119e7d2009-06-08 10:53:09 -0400331
332 case kLifecycle_ANPEventType:
333 gLogI.log(instance, kDebug_ANPLogType, "---- %p Lifecycle action=%d",
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400334 instance, evt->data.lifecycle.action);
Mike Reed1e7c3312009-05-26 16:37:45 -0400335 break;
Grace Klobafbe47c02009-05-14 17:31:45 -0700336
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400337 case kTouch_ANPEventType:
Grace Klobafbe47c02009-05-14 17:31:45 -0700338 gLogI.log(instance, kDebug_ANPLogType, "---- %p Touch action=%d [%d %d]",
339 instance, evt->data.touch.action, evt->data.touch.x,
340 evt->data.touch.y);
341 if (kUp_ANPTouchAction == evt->data.touch.action) {
342 if (NULL == obj->track) {
343 obj->track = createTrack(instance, "/sdcard/sample.snd");
344 }
345 if (obj->track) {
346 gLogI.log(instance, kDebug_ANPLogType, "track %p %d",
347 obj->track, gSoundI.isStopped(obj->track));
348 if (gSoundI.isStopped(obj->track)) {
349 gSoundI.start(obj->track);
350 } else {
351 gSoundI.pause(obj->track);
352 }
353 }
354 }
355 return 1;
356
357 default:
358 break;
359 }
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400360
361 if(!obj->activePlugin) {
362 gLogI.log(instance, kError_ANPLogType, "the active plugin is null.");
363 return 0; // unknown or unhandled event
364 }
365 else {
366 return obj->activePlugin->handleEvent(evt);
367 }
Grace Klobafbe47c02009-05-14 17:31:45 -0700368}
369
370void NPP_URLNotify(NPP instance, const char* url, NPReason reason, void* notifyData)
371{
372
373}
374
375EXPORT NPError NP_GetValue(NPP instance, NPPVariable variable, void *value) {
376
377 if (variable == NPPVpluginNameString) {
378 const char **str = (const char **)value;
379 *str = "Test Plugin";
380 return NPERR_NO_ERROR;
381 }
Derek Sollenberger9119e7d2009-06-08 10:53:09 -0400382
Grace Klobafbe47c02009-05-14 17:31:45 -0700383 if (variable == NPPVpluginDescriptionString) {
384 const char **str = (const char **)value;
385 *str = "Description of Test Plugin";
386 return NPERR_NO_ERROR;
387 }
Derek Sollenberger9119e7d2009-06-08 10:53:09 -0400388
Grace Klobafbe47c02009-05-14 17:31:45 -0700389 return NPERR_GENERIC_ERROR;
390}
391
392NPError NPP_GetValue(NPP instance, NPPVariable variable, void *value)
393{
394 if (variable == NPPVpluginScriptableNPObject) {
395 void **v = (void **)value;
396 PluginObject *obj = (PluginObject*) instance->pdata;
Derek Sollenberger9119e7d2009-06-08 10:53:09 -0400397
Grace Klobafbe47c02009-05-14 17:31:45 -0700398 if (obj)
399 browser->retainobject((NPObject*)obj);
Derek Sollenberger9119e7d2009-06-08 10:53:09 -0400400
Grace Klobafbe47c02009-05-14 17:31:45 -0700401 *v = obj;
402 return NPERR_NO_ERROR;
403 }
Derek Sollenberger9119e7d2009-06-08 10:53:09 -0400404
Grace Klobafbe47c02009-05-14 17:31:45 -0700405 return NPERR_GENERIC_ERROR;
406}
407
408NPError NPP_SetValue(NPP instance, NPNVariable variable, void *value)
409{
410 return NPERR_GENERIC_ERROR;
411}
412