1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is mozilla.org code.
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1998
21 * the Initial Developer. All Rights Reserved.
24 * Stephen Mak <smak@sun.com>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
43 * Netscape Client Plugin API
44 * - Wrapper function to interface with the Netscape Navigator
46 * dp Suresh <dp@netscape.com>
48 *----------------------------------------------------------------------
50 * YOU WILL NOT NEED TO EDIT THIS FILE.
51 *----------------------------------------------------------------------
61 * Define PLUGIN_TRACE to have the wrapper functions print
62 * messages to stderr whenever they are called.
67 #define PLUGINDEBUGSTR(msg) fprintf(stderr, "%s\n", msg)
69 #define PLUGINDEBUGSTR(msg)
73 /***********************************************************************
77 ***********************************************************************/
79 static NPNetscapeFuncs gNetscapeFuncs; /* Netscape Function table */
82 /***********************************************************************
84 * Wrapper functions : plugin calling Netscape Navigator
86 * These functions let the plugin developer just call the APIs
87 * as documented and defined in npapi.h, without needing to know
88 * about the function table and call macros in npupp.h.
90 ***********************************************************************/
93 NPN_Version(int* plugin_major, int* plugin_minor,
94 int* netscape_major, int* netscape_minor)
96 *plugin_major = NP_VERSION_MAJOR;
97 *plugin_minor = NP_VERSION_MINOR;
99 /* Major version is in high byte */
100 *netscape_major = gNetscapeFuncs.version >> 8;
101 /* Minor version is in low byte */
102 *netscape_minor = gNetscapeFuncs.version & 0xFF;
106 NPN_GetValue(NPP instance, NPNVariable variable, void *r_value)
108 return CallNPN_GetValueProc(gNetscapeFuncs.getvalue,
109 instance, variable, r_value);
113 NPN_SetValue(NPP instance, NPPVariable variable, void *value)
115 return CallNPN_SetValueProc(gNetscapeFuncs.setvalue,
116 instance, variable, value);
120 NPN_GetURL(NPP instance, const char* url, const char* window)
122 return CallNPN_GetURLProc(gNetscapeFuncs.geturl, instance, url, window);
126 NPN_GetURLNotify(NPP instance, const char* url, const char* window, void* notifyData)
128 return CallNPN_GetURLNotifyProc(gNetscapeFuncs.geturlnotify, instance, url, window, notifyData);
132 NPN_PostURL(NPP instance, const char* url, const char* window,
133 uint32 len, const char* buf, NPBool file)
135 return CallNPN_PostURLProc(gNetscapeFuncs.posturl, instance,
136 url, window, len, buf, file);
140 NPN_PostURLNotify(NPP instance, const char* url, const char* window, uint32 len,
141 const char* buf, NPBool file, void* notifyData)
143 return CallNPN_PostURLNotifyProc(gNetscapeFuncs.posturlnotify,
144 instance, url, window, len, buf, file, notifyData);
148 NPN_RequestRead(NPStream* stream, NPByteRange* rangeList)
150 return CallNPN_RequestReadProc(gNetscapeFuncs.requestread,
155 NPN_NewStream(NPP instance, NPMIMEType type, const char *window,
156 NPStream** stream_ptr)
158 return CallNPN_NewStreamProc(gNetscapeFuncs.newstream, instance,
159 type, window, stream_ptr);
163 NPN_Write(NPP instance, NPStream* stream, int32 len, void* buffer)
165 return CallNPN_WriteProc(gNetscapeFuncs.write, instance,
166 stream, len, buffer);
170 NPN_DestroyStream(NPP instance, NPStream* stream, NPError reason)
172 return CallNPN_DestroyStreamProc(gNetscapeFuncs.destroystream,
173 instance, stream, reason);
177 NPN_Status(NPP instance, const char* message)
179 CallNPN_StatusProc(gNetscapeFuncs.status, instance, message);
183 NPN_UserAgent(NPP instance)
185 return CallNPN_UserAgentProc(gNetscapeFuncs.uagent, instance);
189 NPN_MemAlloc(uint32 size)
191 return CallNPN_MemAllocProc(gNetscapeFuncs.memalloc, size);
194 void NPN_MemFree(void* ptr)
196 CallNPN_MemFreeProc(gNetscapeFuncs.memfree, ptr);
199 uint32 NPN_MemFlush(uint32 size)
201 return CallNPN_MemFlushProc(gNetscapeFuncs.memflush, size);
204 void NPN_ReloadPlugins(NPBool reloadPages)
206 CallNPN_ReloadPluginsProc(gNetscapeFuncs.reloadplugins, reloadPages);
210 JRIEnv* NPN_GetJavaEnv()
212 return CallNPN_GetJavaEnvProc(gNetscapeFuncs.getJavaEnv);
215 jref NPN_GetJavaPeer(NPP instance)
217 return CallNPN_GetJavaPeerProc(gNetscapeFuncs.getJavaPeer,
223 NPN_InvalidateRect(NPP instance, NPRect *invalidRect)
225 CallNPN_InvalidateRectProc(gNetscapeFuncs.invalidaterect, instance,
230 NPN_InvalidateRegion(NPP instance, NPRegion invalidRegion)
232 CallNPN_InvalidateRegionProc(gNetscapeFuncs.invalidateregion, instance,
237 NPN_ForceRedraw(NPP instance)
239 CallNPN_ForceRedrawProc(gNetscapeFuncs.forceredraw, instance);
242 void NPN_PushPopupsEnabledState(NPP instance, NPBool enabled)
244 CallNPN_PushPopupsEnabledStateProc(gNetscapeFuncs.pushpopupsenabledstate,
248 void NPN_PopPopupsEnabledState(NPP instance)
250 CallNPN_PopPopupsEnabledStateProc(gNetscapeFuncs.poppopupsenabledstate,
254 NPIdentifier NPN_GetStringIdentifier(const NPUTF8 *name)
256 return CallNPN_GetStringIdentifierProc(gNetscapeFuncs.getstringidentifier,
260 void NPN_GetStringIdentifiers(const NPUTF8 **names, int32_t nameCount,
261 NPIdentifier *identifiers)
263 CallNPN_GetStringIdentifiersProc(gNetscapeFuncs.getstringidentifiers,
264 names, nameCount, identifiers);
267 NPIdentifier NPN_GetIntIdentifier(int32_t intid)
269 return CallNPN_GetIntIdentifierProc(gNetscapeFuncs.getintidentifier, intid);
272 bool NPN_IdentifierIsString(NPIdentifier identifier)
274 return CallNPN_IdentifierIsStringProc(gNetscapeFuncs.identifierisstring,
278 NPUTF8 *NPN_UTF8FromIdentifier(NPIdentifier identifier)
280 return CallNPN_UTF8FromIdentifierProc(gNetscapeFuncs.utf8fromidentifier,
284 int32_t NPN_IntFromIdentifier(NPIdentifier identifier)
286 return CallNPN_IntFromIdentifierProc(gNetscapeFuncs.intfromidentifier,
290 NPObject *NPN_CreateObject(NPP npp, NPClass *aClass)
292 return CallNPN_CreateObjectProc(gNetscapeFuncs.createobject, npp, aClass);
295 NPObject *NPN_RetainObject(NPObject *obj)
297 return CallNPN_RetainObjectProc(gNetscapeFuncs.retainobject, obj);
300 void NPN_ReleaseObject(NPObject *obj)
302 CallNPN_ReleaseObjectProc(gNetscapeFuncs.releaseobject, obj);
305 bool NPN_Invoke(NPP npp, NPObject* obj, NPIdentifier methodName,
306 const NPVariant *args, uint32_t argCount, NPVariant *result)
308 return CallNPN_InvokeProc(gNetscapeFuncs.invoke, npp, obj, methodName,
309 args, argCount, result);
312 bool NPN_InvokeDefault(NPP npp, NPObject* obj, const NPVariant *args,
313 uint32_t argCount, NPVariant *result)
315 return CallNPN_InvokeDefaultProc(gNetscapeFuncs.invokeDefault, npp, obj,
316 args, argCount, result);
319 bool NPN_Evaluate(NPP npp, NPObject* obj, NPString *script,
322 return CallNPN_EvaluateProc(gNetscapeFuncs.evaluate, npp, obj, script, result);
325 bool NPN_GetProperty(NPP npp, NPObject* obj, NPIdentifier propertyName,
328 return CallNPN_GetPropertyProc(gNetscapeFuncs.getproperty, npp, obj,
329 propertyName, result);
332 bool NPN_SetProperty(NPP npp, NPObject* obj, NPIdentifier propertyName,
333 const NPVariant *value)
335 return CallNPN_SetPropertyProc(gNetscapeFuncs.setproperty, npp, obj,
336 propertyName, value);
339 bool NPN_RemoveProperty(NPP npp, NPObject* obj, NPIdentifier propertyName)
341 return CallNPN_RemovePropertyProc(gNetscapeFuncs.removeproperty, npp, obj,
345 bool NPN_HasProperty(NPP npp, NPObject* obj, NPIdentifier propertyName)
347 return CallNPN_HasPropertyProc(gNetscapeFuncs.hasproperty, npp, obj,
351 bool NPN_HasMethod(NPP npp, NPObject* obj, NPIdentifier methodName)
353 return CallNPN_HasMethodProc(gNetscapeFuncs.hasmethod, npp, obj, methodName);
356 void NPN_ReleaseVariantValue(NPVariant *variant)
358 CallNPN_ReleaseVariantValueProc(gNetscapeFuncs.releasevariantvalue, variant);
361 void NPN_SetException(NPObject* obj, const NPUTF8 *message)
363 CallNPN_SetExceptionProc(gNetscapeFuncs.setexception, obj, message);
367 /***********************************************************************
369 * Wrapper functions : Netscape Navigator -> plugin
371 * These functions let the plugin developer just create the APIs
372 * as documented and defined in npapi.h, without needing to
373 * install those functions in the function table or worry about
374 * setting up globals for 68K plugins.
376 ***********************************************************************/
379 Private_New(NPMIMEType pluginType, NPP instance, uint16 mode,
380 int16 argc, char* argn[], char* argv[], NPSavedData* saved)
383 PLUGINDEBUGSTR("New");
384 ret = NPP_New(pluginType, instance, mode, argc, argn, argv, saved);
389 Private_Destroy(NPP instance, NPSavedData** save)
391 PLUGINDEBUGSTR("Destroy");
392 return NPP_Destroy(instance, save);
396 Private_SetWindow(NPP instance, NPWindow* window)
399 PLUGINDEBUGSTR("SetWindow");
400 err = NPP_SetWindow(instance, window);
405 Private_NewStream(NPP instance, NPMIMEType type, NPStream* stream,
406 NPBool seekable, uint16* stype)
409 PLUGINDEBUGSTR("NewStream");
410 err = NPP_NewStream(instance, type, stream, seekable, stype);
415 Private_WriteReady(NPP instance, NPStream* stream)
418 PLUGINDEBUGSTR("WriteReady");
419 result = NPP_WriteReady(instance, stream);
424 Private_Write(NPP instance, NPStream* stream, int32 offset, int32 len,
428 PLUGINDEBUGSTR("Write");
429 result = NPP_Write(instance, stream, offset, len, buffer);
434 Private_StreamAsFile(NPP instance, NPStream* stream, const char* fname)
436 PLUGINDEBUGSTR("StreamAsFile");
437 NPP_StreamAsFile(instance, stream, fname);
442 Private_DestroyStream(NPP instance, NPStream* stream, NPError reason)
445 PLUGINDEBUGSTR("DestroyStream");
446 err = NPP_DestroyStream(instance, stream, reason);
451 Private_URLNotify(NPP instance, const char* url,
452 NPReason reason, void* notifyData)
455 PLUGINDEBUGSTR("URLNotify");
456 NPP_URLNotify(instance, url, reason, notifyData);
462 Private_Print(NPP instance, NPPrint* platformPrint)
464 PLUGINDEBUGSTR("Print");
465 NPP_Print(instance, platformPrint);
470 Private_GetJavaClass(void)
472 jref clazz = NPP_GetJavaClass();
474 JRIEnv* env = NPN_GetJavaEnv();
475 return JRI_NewGlobalRef(env, clazz);
481 /***********************************************************************
483 * These functions are located automagically by netscape.
485 ***********************************************************************/
488 * NP_GetMIMEDescription
489 * - Netscape needs to know about this symbol
490 * - Netscape uses the return value to identify when an object instance
491 * of this plugin should be created.
494 NP_GetMIMEDescription(void)
496 return NPP_GetMIMEDescription();
500 * NP_GetValue [optional]
501 * - Netscape needs to know about this symbol.
502 * - Interfaces with plugin to get values for predefined variables
503 * that the navigator needs.
506 NP_GetValue(void* future, NPPVariable variable, void *value)
508 return NPP_GetValue(future, variable, value);
513 * - Netscape needs to know about this symbol.
514 * - It calls this function after looking up its symbol before it
515 * is about to create the first ever object of this kind.
518 * nsTable - The netscape function table. If developers just use these
519 * wrappers, they don't need to worry about all these function
523 * - This functions needs to fill the plugin function table
524 * pluginFuncs and return it. Netscape Navigator plugin
525 * library will use this function table to call the plugin.
529 NP_Initialize(NPNetscapeFuncs* nsTable, NPPluginFuncs* pluginFuncs)
531 NPError err = NPERR_NO_ERROR;
533 PLUGINDEBUGSTR("NP_Initialize");
535 /* validate input parameters */
537 if ((nsTable == NULL) || (pluginFuncs == NULL))
538 err = NPERR_INVALID_FUNCTABLE_ERROR;
541 * Check the major version passed in Netscape's function table.
542 * We won't load if the major version is newer than what we expect.
543 * Also check that the function tables passed in are big enough for
544 * all the functions we need (they could be bigger, if Netscape added
545 * new APIs, but that's OK with us -- we'll just ignore them).
549 if (err == NPERR_NO_ERROR) {
550 if ((nsTable->version >> 8) > NP_VERSION_MAJOR)
551 err = NPERR_INCOMPATIBLE_VERSION_ERROR;
552 if (nsTable->size < ((char *)&nsTable->posturlnotify - (char *)nsTable))
553 err = NPERR_INVALID_FUNCTABLE_ERROR;
554 if (pluginFuncs->size < sizeof(NPPluginFuncs))
555 err = NPERR_INVALID_FUNCTABLE_ERROR;
559 if (err == NPERR_NO_ERROR) {
561 * Copy all the fields of Netscape function table into our
562 * copy so we can call back into Netscape later. Note that
563 * we need to copy the fields one by one, rather than assigning
564 * the whole structure, because the Netscape function table
565 * could actually be bigger than what we expect.
567 gNetscapeFuncs.version = nsTable->version;
568 gNetscapeFuncs.size = nsTable->size;
569 gNetscapeFuncs.posturl = nsTable->posturl;
570 gNetscapeFuncs.geturl = nsTable->geturl;
571 gNetscapeFuncs.geturlnotify = nsTable->geturlnotify;
572 gNetscapeFuncs.requestread = nsTable->requestread;
573 gNetscapeFuncs.newstream = nsTable->newstream;
574 gNetscapeFuncs.write = nsTable->write;
575 gNetscapeFuncs.destroystream = nsTable->destroystream;
576 gNetscapeFuncs.status = nsTable->status;
577 gNetscapeFuncs.uagent = nsTable->uagent;
578 gNetscapeFuncs.memalloc = nsTable->memalloc;
579 gNetscapeFuncs.memfree = nsTable->memfree;
580 gNetscapeFuncs.memflush = nsTable->memflush;
581 gNetscapeFuncs.reloadplugins = nsTable->reloadplugins;
583 gNetscapeFuncs.getJavaEnv = nsTable->getJavaEnv;
584 gNetscapeFuncs.getJavaPeer = nsTable->getJavaPeer;
586 gNetscapeFuncs.getvalue = nsTable->getvalue;
587 gNetscapeFuncs.setvalue = nsTable->setvalue;
588 gNetscapeFuncs.posturlnotify = nsTable->posturlnotify;
590 if (nsTable->size >= ((char *)&nsTable->setexception - (char *)nsTable))
592 gNetscapeFuncs.invalidaterect = nsTable->invalidaterect;
593 gNetscapeFuncs.invalidateregion = nsTable->invalidateregion;
594 gNetscapeFuncs.forceredraw = nsTable->forceredraw;
595 gNetscapeFuncs.getstringidentifier = nsTable->getstringidentifier;
596 gNetscapeFuncs.getstringidentifiers = nsTable->getstringidentifiers;
597 gNetscapeFuncs.getintidentifier = nsTable->getintidentifier;
598 gNetscapeFuncs.identifierisstring = nsTable->identifierisstring;
599 gNetscapeFuncs.utf8fromidentifier = nsTable->utf8fromidentifier;
600 gNetscapeFuncs.intfromidentifier = nsTable->intfromidentifier;
601 gNetscapeFuncs.createobject = nsTable->createobject;
602 gNetscapeFuncs.retainobject = nsTable->retainobject;
603 gNetscapeFuncs.releaseobject = nsTable->releaseobject;
604 gNetscapeFuncs.invoke = nsTable->invoke;
605 gNetscapeFuncs.invokeDefault = nsTable->invokeDefault;
606 gNetscapeFuncs.evaluate = nsTable->evaluate;
607 gNetscapeFuncs.getproperty = nsTable->getproperty;
608 gNetscapeFuncs.setproperty = nsTable->setproperty;
609 gNetscapeFuncs.removeproperty = nsTable->removeproperty;
610 gNetscapeFuncs.hasproperty = nsTable->hasproperty;
611 gNetscapeFuncs.hasmethod = nsTable->hasmethod;
612 gNetscapeFuncs.releasevariantvalue = nsTable->releasevariantvalue;
613 gNetscapeFuncs.setexception = nsTable->setexception;
617 gNetscapeFuncs.invalidaterect = NULL;
618 gNetscapeFuncs.invalidateregion = NULL;
619 gNetscapeFuncs.forceredraw = NULL;
620 gNetscapeFuncs.getstringidentifier = NULL;
621 gNetscapeFuncs.getstringidentifiers = NULL;
622 gNetscapeFuncs.getintidentifier = NULL;
623 gNetscapeFuncs.identifierisstring = NULL;
624 gNetscapeFuncs.utf8fromidentifier = NULL;
625 gNetscapeFuncs.intfromidentifier = NULL;
626 gNetscapeFuncs.createobject = NULL;
627 gNetscapeFuncs.retainobject = NULL;
628 gNetscapeFuncs.releaseobject = NULL;
629 gNetscapeFuncs.invoke = NULL;
630 gNetscapeFuncs.invokeDefault = NULL;
631 gNetscapeFuncs.evaluate = NULL;
632 gNetscapeFuncs.getproperty = NULL;
633 gNetscapeFuncs.setproperty = NULL;
634 gNetscapeFuncs.removeproperty = NULL;
635 gNetscapeFuncs.hasproperty = NULL;
636 gNetscapeFuncs.releasevariantvalue = NULL;
637 gNetscapeFuncs.setexception = NULL;
640 ((char *)&nsTable->poppopupsenabledstate - (char *)nsTable))
642 gNetscapeFuncs.pushpopupsenabledstate = nsTable->pushpopupsenabledstate;
643 gNetscapeFuncs.poppopupsenabledstate = nsTable->poppopupsenabledstate;
647 gNetscapeFuncs.pushpopupsenabledstate = NULL;
648 gNetscapeFuncs.poppopupsenabledstate = NULL;
652 * Set up the plugin function table that Netscape will use to
653 * call us. Netscape needs to know about our version and size
654 * and have a UniversalProcPointer for every function we
657 pluginFuncs->version = (NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR;
658 pluginFuncs->size = sizeof(NPPluginFuncs);
659 pluginFuncs->newp = NewNPP_NewProc(Private_New);
660 pluginFuncs->destroy = NewNPP_DestroyProc(Private_Destroy);
661 pluginFuncs->setwindow = NewNPP_SetWindowProc(Private_SetWindow);
662 pluginFuncs->newstream = NewNPP_NewStreamProc(Private_NewStream);
663 pluginFuncs->destroystream = NewNPP_DestroyStreamProc(Private_DestroyStream);
664 pluginFuncs->asfile = NewNPP_StreamAsFileProc(Private_StreamAsFile);
665 pluginFuncs->writeready = NewNPP_WriteReadyProc(Private_WriteReady);
666 pluginFuncs->write = NewNPP_WriteProc(Private_Write);
667 pluginFuncs->print = NewNPP_PrintProc(Private_Print);
668 pluginFuncs->urlnotify = NewNPP_URLNotifyProc(Private_URLNotify);
669 pluginFuncs->event = NULL;
671 pluginFuncs->javaClass = Private_GetJavaClass();
673 // This function is supposedly loaded magically, but that doesn't
675 pluginFuncs->getvalue = NewNPP_GetValueProc(NP_GetValue);
677 err = NPP_Initialize();
684 * NP_Shutdown [optional]
685 * - Netscape needs to know about this symbol.
686 * - It calls this function after looking up its symbol after
687 * the last object of this kind has been destroyed.
693 PLUGINDEBUGSTR("NP_Shutdown");
695 return NPERR_NO_ERROR;