blob: e3ad4a739c819ae34d4a85b0f5946d51a6140c09 [file] [log] [blame]
Derek Sollenbergerdf8a3f312009-08-18 14:25:27 -04001/*
2 * Copyright 2009, 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 "android_npapi.h"
30#include "main.h"
31#include "PluginObject.h"
32#include "EventPlugin.h"
33
34NPNetscapeFuncs* browser;
35#define EXPORT __attribute__((visibility("default")))
36
37NPError NPP_New(NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc,
38 char* argn[], char* argv[], NPSavedData* saved);
39NPError NPP_Destroy(NPP instance, NPSavedData** save);
40NPError NPP_SetWindow(NPP instance, NPWindow* window);
41NPError NPP_NewStream(NPP instance, NPMIMEType type, NPStream* stream,
42 NPBool seekable, uint16* stype);
43NPError NPP_DestroyStream(NPP instance, NPStream* stream, NPReason reason);
44int32 NPP_WriteReady(NPP instance, NPStream* stream);
45int32 NPP_Write(NPP instance, NPStream* stream, int32 offset, int32 len,
46 void* buffer);
47void NPP_StreamAsFile(NPP instance, NPStream* stream, const char* fname);
48void NPP_Print(NPP instance, NPPrint* platformPrint);
49int16 NPP_HandleEvent(NPP instance, void* event);
50void NPP_URLNotify(NPP instance, const char* URL, NPReason reason,
51 void* notifyData);
52NPError NPP_GetValue(NPP instance, NPPVariable variable, void *value);
53NPError NPP_SetValue(NPP instance, NPNVariable variable, void *value);
54
55extern "C" {
56EXPORT NPError NP_Initialize(NPNetscapeFuncs* browserFuncs, NPPluginFuncs* pluginFuncs, void *java_env, void *application_context);
57EXPORT NPError NP_GetValue(NPP instance, NPPVariable variable, void *value);
58EXPORT const char* NP_GetMIMEDescription(void);
59EXPORT void NP_Shutdown(void);
60};
61
62ANPAudioTrackInterfaceV0 gSoundI;
63ANPBitmapInterfaceV0 gBitmapI;
64ANPCanvasInterfaceV0 gCanvasI;
65ANPLogInterfaceV0 gLogI;
66ANPPaintInterfaceV0 gPaintI;
67ANPPathInterfaceV0 gPathI;
Derek Sollenbergerdf8a3f312009-08-18 14:25:27 -040068ANPSystemInterfaceV0 gSystemI;
69ANPTypefaceInterfaceV0 gTypefaceI;
70ANPWindowInterfaceV0 gWindowI;
71
72#define ARRAY_COUNT(array) (sizeof(array) / sizeof(array[0]))
73
74NPError NP_Initialize(NPNetscapeFuncs* browserFuncs, NPPluginFuncs* pluginFuncs, void *java_env, void *application_context)
75{
76 // Make sure we have a function table equal or larger than we are built against.
77 if (browserFuncs->size < sizeof(NPNetscapeFuncs)) {
78 return NPERR_GENERIC_ERROR;
79 }
80
81 // Copy the function table (structure)
82 browser = (NPNetscapeFuncs*) malloc(sizeof(NPNetscapeFuncs));
83 memcpy(browser, browserFuncs, sizeof(NPNetscapeFuncs));
84
85 // Build the plugin function table
86 pluginFuncs->version = 11;
87 pluginFuncs->size = sizeof(pluginFuncs);
88 pluginFuncs->newp = NPP_New;
89 pluginFuncs->destroy = NPP_Destroy;
90 pluginFuncs->setwindow = NPP_SetWindow;
91 pluginFuncs->newstream = NPP_NewStream;
92 pluginFuncs->destroystream = NPP_DestroyStream;
93 pluginFuncs->asfile = NPP_StreamAsFile;
94 pluginFuncs->writeready = NPP_WriteReady;
95 pluginFuncs->write = (NPP_WriteProcPtr)NPP_Write;
96 pluginFuncs->print = NPP_Print;
97 pluginFuncs->event = NPP_HandleEvent;
98 pluginFuncs->urlnotify = NPP_URLNotify;
99 pluginFuncs->getvalue = NPP_GetValue;
100 pluginFuncs->setvalue = NPP_SetValue;
101
102 static const struct {
103 NPNVariable v;
104 uint32_t size;
105 ANPInterface* i;
106 } gPairs[] = {
Derek Sollenbergerdf8a3f312009-08-18 14:25:27 -0400107 { kCanvasInterfaceV0_ANPGetValue, sizeof(gCanvasI), &gCanvasI },
108 { kLogInterfaceV0_ANPGetValue, sizeof(gLogI), &gLogI },
109 { kPaintInterfaceV0_ANPGetValue, sizeof(gPaintI), &gPaintI },
Derek Sollenbergerdf8a3f312009-08-18 14:25:27 -0400110 { kTypefaceInterfaceV0_ANPGetValue, sizeof(gTypefaceI), &gTypefaceI },
Derek Sollenbergerdf8a3f312009-08-18 14:25:27 -0400111 };
112 for (size_t i = 0; i < ARRAY_COUNT(gPairs); i++) {
113 gPairs[i].i->inSize = gPairs[i].size;
114 NPError err = browser->getvalue(NULL, gPairs[i].v, gPairs[i].i);
115 if (err) {
116 return err;
117 }
118 }
119
120 return NPERR_NO_ERROR;
121}
122
123void NP_Shutdown(void)
124{
125
126}
127
128const char *NP_GetMIMEDescription(void)
129{
130 return "application/x-browsertestplugin:btp:Android Browser Test Plugin";
131}
132
133NPError NPP_New(NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc,
134 char* argn[], char* argv[], NPSavedData* saved)
135{
136
137
138 gLogI.log(instance, kDebug_ANPLogType, "creating plugin");
139
140 PluginObject *obj = NULL;
141
142 // Scripting functions appeared in NPAPI version 14
143 if (browser->version >= 14) {
144 instance->pdata = browser->createobject (instance, getPluginClass());
145 obj = static_cast<PluginObject*>(instance->pdata);
146 bzero(obj, sizeof(*obj));
147 } else {
148 return NPERR_GENERIC_ERROR;
149 }
150
151 // select the drawing model
Derek Sollenberger0b3a5d62009-09-08 18:31:40 -0400152 ANPDrawingModel model = kBitmap_ANPDrawingModel;
Derek Sollenbergerdf8a3f312009-08-18 14:25:27 -0400153
154 // notify the plugin API of the drawing model we wish to use. This must be
155 // done prior to creating certain subPlugin objects (e.g. surfaceViews)
156 NPError err = browser->setvalue(instance, kRequestDrawingModel_ANPSetValue,
157 reinterpret_cast<void*>(model));
158 if (err) {
159 gLogI.log(instance, kError_ANPLogType, "request model %d err %d", model, err);
160 return err;
161 }
162
163 // create the sub-plugin
164 obj->subPlugin = new EventPlugin(instance);
165
166 return NPERR_NO_ERROR;
167}
168
169NPError NPP_Destroy(NPP instance, NPSavedData** save)
170{
171 PluginObject *obj = (PluginObject*) instance->pdata;
172 delete obj->subPlugin;
173
174 return NPERR_NO_ERROR;
175}
176
177NPError NPP_SetWindow(NPP instance, NPWindow* window)
178{
179 PluginObject *obj = (PluginObject*) instance->pdata;
180
181 // Do nothing if browser didn't support NPN_CreateObject which would have created the PluginObject.
182 if (obj != NULL) {
183 obj->window = window;
184 }
185
186 return NPERR_NO_ERROR;
187}
188
189NPError NPP_NewStream(NPP instance, NPMIMEType type, NPStream* stream, NPBool seekable, uint16* stype)
190{
191 *stype = NP_ASFILEONLY;
192 return NPERR_NO_ERROR;
193}
194
195NPError NPP_DestroyStream(NPP instance, NPStream* stream, NPReason reason)
196{
197 return NPERR_NO_ERROR;
198}
199
200int32 NPP_WriteReady(NPP instance, NPStream* stream)
201{
202 return 0;
203}
204
205int32 NPP_Write(NPP instance, NPStream* stream, int32 offset, int32 len, void* buffer)
206{
207 return 0;
208}
209
210void NPP_StreamAsFile(NPP instance, NPStream* stream, const char* fname)
211{
212}
213
214void NPP_Print(NPP instance, NPPrint* platformPrint)
215{
216}
217
218int16 NPP_HandleEvent(NPP instance, void* event)
219{
220 PluginObject *obj = reinterpret_cast<PluginObject*>(instance->pdata);
221 const ANPEvent* evt = reinterpret_cast<const ANPEvent*>(event);
222
223 if(!obj->subPlugin) {
224 gLogI.log(instance, kError_ANPLogType, "the sub-plugin is null.");
225 return 0; // unknown or unhandled event
226 }
227 else {
228 return obj->subPlugin->handleEvent(evt);
229 }
230}
231
232void NPP_URLNotify(NPP instance, const char* url, NPReason reason, void* notifyData)
233{
234}
235
236EXPORT NPError NP_GetValue(NPP instance, NPPVariable variable, void *value) {
237
238 if (variable == NPPVpluginNameString) {
239 const char **str = (const char **)value;
240 *str = "Browser Test Plugin";
241 return NPERR_NO_ERROR;
242 }
243
244 if (variable == NPPVpluginDescriptionString) {
245 const char **str = (const char **)value;
246 *str = "Description of Browser Test Plugin";
247 return NPERR_NO_ERROR;
248 }
249
250 return NPERR_GENERIC_ERROR;
251}
252
253NPError NPP_GetValue(NPP instance, NPPVariable variable, void *value)
254{
255 if (variable == NPPVpluginScriptableNPObject) {
256 void **v = (void **)value;
257 PluginObject *obj = (PluginObject*) instance->pdata;
258
259 if (obj)
260 browser->retainobject((NPObject*)obj);
261
262 *v = obj;
263 return NPERR_NO_ERROR;
264 }
265
266 return NPERR_GENERIC_ERROR;
267}
268
269NPError NPP_SetValue(NPP instance, NPNVariable variable, void *value)
270{
271 return NPERR_GENERIC_ERROR;
272}
273