4 * Netscape Client Plugin API
5 * - Wrapper function to interface with the Netscape Navigator
7 * dp Suresh <dp@netscape.com>
9 *----------------------------------------------------------------------
11 * YOU WILL NOT NEED TO EDIT THIS FILE.
12 *----------------------------------------------------------------------
22 * Define PLUGIN_TRACE to have the wrapper functions print
23 * messages to stderr whenever they are called.
28 #define PLUGINDEBUGSTR(msg) fprintf(stderr, "%s\n", msg)
30 #define PLUGINDEBUGSTR(msg)
34 /***********************************************************************
38 ***********************************************************************/
40 static NPNetscapeFuncs gNetscapeFuncs; /* Netscape Function table */
43 /***********************************************************************
45 * Wrapper functions : plugin calling Netscape Navigator
47 * These functions let the plugin developer just call the APIs
48 * as documented and defined in npapi.h, without needing to know
49 * about the function table and call macros in npupp.h.
51 ***********************************************************************/
54 NPN_Version(int* plugin_major, int* plugin_minor,
55 int* netscape_major, int* netscape_minor)
57 *plugin_major = NP_VERSION_MAJOR;
58 *plugin_minor = NP_VERSION_MINOR;
60 /* Major version is in high byte */
61 *netscape_major = gNetscapeFuncs.version >> 8;
62 /* Minor version is in low byte */
63 *netscape_minor = gNetscapeFuncs.version & 0xFF;
67 NPN_GetValue(NPP instance, NPNVariable variable, void *r_value)
69 return CallNPN_GetValueProc(gNetscapeFuncs.getvalue,
70 instance, variable, r_value);
74 NPN_GetURL(NPP instance, const char* url, const char* window)
76 return CallNPN_GetURLProc(gNetscapeFuncs.geturl, instance, url, window);
80 NPN_PostURL(NPP instance, const char* url, const char* window,
81 uint32 len, const char* buf, NPBool file)
83 return CallNPN_PostURLProc(gNetscapeFuncs.posturl, instance,
84 url, window, len, buf, file);
88 NPN_RequestRead(NPStream* stream, NPByteRange* rangeList)
90 return CallNPN_RequestReadProc(gNetscapeFuncs.requestread,
95 NPN_NewStream(NPP instance, NPMIMEType type, const char *window,
96 NPStream** stream_ptr)
98 return CallNPN_NewStreamProc(gNetscapeFuncs.newstream, instance,
99 type, window, stream_ptr);
103 NPN_Write(NPP instance, NPStream* stream, int32 len, void* buffer)
105 return CallNPN_WriteProc(gNetscapeFuncs.write, instance,
106 stream, len, buffer);
110 NPN_DestroyStream(NPP instance, NPStream* stream, NPError reason)
112 return CallNPN_DestroyStreamProc(gNetscapeFuncs.destroystream,
113 instance, stream, reason);
117 NPN_Status(NPP instance, const char* message)
119 CallNPN_StatusProc(gNetscapeFuncs.status, instance, message);
123 NPN_UserAgent(NPP instance)
125 return CallNPN_UserAgentProc(gNetscapeFuncs.uagent, instance);
129 NPN_MemAlloc(uint32 size)
131 return CallNPN_MemAllocProc(gNetscapeFuncs.memalloc, size);
134 void NPN_MemFree(void* ptr)
136 CallNPN_MemFreeProc(gNetscapeFuncs.memfree, ptr);
139 uint32 NPN_MemFlush(uint32 size)
141 return CallNPN_MemFlushProc(gNetscapeFuncs.memflush, size);
144 void NPN_ReloadPlugins(NPBool reloadPages)
146 CallNPN_ReloadPluginsProc(gNetscapeFuncs.reloadplugins, reloadPages);
149 JRIEnv* NPN_GetJavaEnv()
151 return CallNPN_GetJavaEnvProc(gNetscapeFuncs.getJavaEnv);
154 jref NPN_GetJavaPeer(NPP instance)
156 return CallNPN_GetJavaPeerProc(gNetscapeFuncs.getJavaPeer,
161 /***********************************************************************
163 * Wrapper functions : Netscape Navigator -> plugin
165 * These functions let the plugin developer just create the APIs
166 * as documented and defined in npapi.h, without needing to
167 * install those functions in the function table or worry about
168 * setting up globals for 68K plugins.
170 ***********************************************************************/
173 Private_New(NPMIMEType pluginType, NPP instance, uint16 mode,
174 int16 argc, char* argn[], char* argv[], NPSavedData* saved)
177 PLUGINDEBUGSTR("New");
178 ret = NPP_New(pluginType, instance, mode, argc, argn, argv, saved);
183 Private_Destroy(NPP instance, NPSavedData** save)
185 PLUGINDEBUGSTR("Destroy");
186 return NPP_Destroy(instance, save);
190 Private_SetWindow(NPP instance, NPWindow* window)
193 PLUGINDEBUGSTR("SetWindow");
194 err = NPP_SetWindow(instance, window);
199 Private_NewStream(NPP instance, NPMIMEType type, NPStream* stream,
200 NPBool seekable, uint16* stype)
203 PLUGINDEBUGSTR("NewStream");
204 err = NPP_NewStream(instance, type, stream, seekable, stype);
209 Private_WriteReady(NPP instance, NPStream* stream)
212 PLUGINDEBUGSTR("WriteReady");
213 result = NPP_WriteReady(instance, stream);
218 Private_Write(NPP instance, NPStream* stream, int32 offset, int32 len,
222 PLUGINDEBUGSTR("Write");
223 result = NPP_Write(instance, stream, offset, len, buffer);
228 Private_StreamAsFile(NPP instance, NPStream* stream, const char* fname)
230 PLUGINDEBUGSTR("StreamAsFile");
231 NPP_StreamAsFile(instance, stream, fname);
236 Private_DestroyStream(NPP instance, NPStream* stream, NPError reason)
239 PLUGINDEBUGSTR("DestroyStream");
240 err = NPP_DestroyStream(instance, stream, reason);
246 Private_Print(NPP instance, NPPrint* platformPrint)
248 PLUGINDEBUGSTR("Print");
249 NPP_Print(instance, platformPrint);
253 Private_GetJavaClass(void)
255 jref clazz = NPP_GetJavaClass();
257 JRIEnv* env = NPN_GetJavaEnv();
258 return JRI_NewGlobalRef(env, clazz);
263 /***********************************************************************
265 * These functions are located automagically by netscape.
267 ***********************************************************************/
270 * NP_GetMIMEDescription
271 * - Netscape needs to know about this symbol
272 * - Netscape uses the return value to identify when an object instance
273 * of this plugin should be created.
276 NP_GetMIMEDescription(void)
278 return NPP_GetMIMEDescription();
282 * NP_GetValue [optional]
283 * - Netscape needs to know about this symbol.
284 * - Interfaces with plugin to get values for predefined variables
285 * that the navigator needs.
288 NP_GetValue(void *future, NPPVariable variable, void *value)
290 return NPP_GetValue(future, variable, value);
295 * - Netscape needs to know about this symbol.
296 * - It calls this function after looking up its symbol before it
297 * is about to create the first ever object of this kind.
300 * nsTable - The netscape function table. If developers just use these
301 * wrappers, they dont need to worry about all these function
305 * - This functions needs to fill the plugin function table
306 * pluginFuncs and return it. Netscape Navigator plugin
307 * library will use this function table to call the plugin.
311 NP_Initialize(NPNetscapeFuncs* nsTable, NPPluginFuncs* pluginFuncs)
313 NPError err = NPERR_NO_ERROR;
315 PLUGINDEBUGSTR("NP_Initialize");
317 /* validate input parameters */
319 if ((nsTable == NULL) || (pluginFuncs == NULL))
320 err = NPERR_INVALID_FUNCTABLE_ERROR;
323 * Check the major version passed in Netscape's function table.
324 * We won't load if the major version is newer than what we expect.
325 * Also check that the function tables passed in are big enough for
326 * all the functions we need (they could be bigger, if Netscape added
327 * new APIs, but that's OK with us -- we'll just ignore them).
331 if (err == NPERR_NO_ERROR) {
332 if ((nsTable->version >> 8) > NP_VERSION_MAJOR)
333 err = NPERR_INCOMPATIBLE_VERSION_ERROR;
334 if (nsTable->size < sizeof(NPNetscapeFuncs))
335 err = NPERR_INVALID_FUNCTABLE_ERROR;
336 if (pluginFuncs->size < sizeof(NPPluginFuncs))
337 err = NPERR_INVALID_FUNCTABLE_ERROR;
341 if (err == NPERR_NO_ERROR) {
343 * Copy all the fields of Netscape function table into our
344 * copy so we can call back into Netscape later. Note that
345 * we need to copy the fields one by one, rather than assigning
346 * the whole structure, because the Netscape function table
347 * could actually be bigger than what we expect.
349 gNetscapeFuncs.version = nsTable->version;
350 gNetscapeFuncs.size = nsTable->size;
351 gNetscapeFuncs.posturl = nsTable->posturl;
352 gNetscapeFuncs.geturl = nsTable->geturl;
353 gNetscapeFuncs.requestread = nsTable->requestread;
354 gNetscapeFuncs.newstream = nsTable->newstream;
355 gNetscapeFuncs.write = nsTable->write;
356 gNetscapeFuncs.destroystream = nsTable->destroystream;
357 gNetscapeFuncs.status = nsTable->status;
358 gNetscapeFuncs.uagent = nsTable->uagent;
359 gNetscapeFuncs.memalloc = nsTable->memalloc;
360 gNetscapeFuncs.memfree = nsTable->memfree;
361 gNetscapeFuncs.memflush = nsTable->memflush;
362 gNetscapeFuncs.reloadplugins = nsTable->reloadplugins;
363 gNetscapeFuncs.getJavaEnv = nsTable->getJavaEnv;
364 gNetscapeFuncs.getJavaPeer = nsTable->getJavaPeer;
365 gNetscapeFuncs.getvalue = nsTable->getvalue;
368 * Set up the plugin function table that Netscape will use to
369 * call us. Netscape needs to know about our version and size
370 * and have a UniversalProcPointer for every function we
373 pluginFuncs->version = (NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR;
374 pluginFuncs->size = sizeof(NPPluginFuncs);
375 pluginFuncs->newp = NewNPP_NewProc(Private_New);
376 pluginFuncs->destroy = NewNPP_DestroyProc(Private_Destroy);
377 pluginFuncs->setwindow = NewNPP_SetWindowProc(Private_SetWindow);
378 pluginFuncs->newstream = NewNPP_NewStreamProc(Private_NewStream);
379 pluginFuncs->destroystream = NewNPP_DestroyStreamProc(Private_DestroyStream);
380 pluginFuncs->asfile = NewNPP_StreamAsFileProc(Private_StreamAsFile);
381 pluginFuncs->writeready = NewNPP_WriteReadyProc(Private_WriteReady);
382 pluginFuncs->write = NewNPP_WriteProc(Private_Write);
383 pluginFuncs->print = NewNPP_PrintProc(Private_Print);
384 pluginFuncs->event = NULL;
385 pluginFuncs->javaClass = Private_GetJavaClass();
387 err = NPP_Initialize();
394 * NP_Shutdown [optional]
395 * - Netscape needs to know about this symbol.
396 * - It calls this function after looking up its symbol after
397 * the last object of this kind has been destroyed.
403 PLUGINDEBUGSTR("NP_Shutdown");
405 return NPERR_NO_ERROR;