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