1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * The contents of this file are subject to the Mozilla Public
4 * License Version 1.1 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of
6 * the License at http://www.mozilla.org/MPL/
8 * Software distributed under the License is distributed on an "AS
9 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
10 * implied. See the License for the specific language governing
11 * rights and limitations under the License.
13 * The Original Code is mozilla.org code.
15 * The Initial Developer of the Original Code is Netscape
16 * Communications Corporation. Portions created by Netscape are
17 * Copyright (C) 1998 Netscape Communications Corporation. All
21 * Stephen Mak <smak@sun.com>
27 * Netscape Client Plugin API
28 * - Wrapper function to interface with the Netscape Navigator
30 * dp Suresh <dp@netscape.com>
32 *----------------------------------------------------------------------
34 * YOU WILL NOT NEED TO EDIT THIS FILE.
35 * TO NETSCAPE DEVELOPERS:
36 * OF COURSE I WILL NEED TO EDIT THIS FILE, YOU BORKED IT ALL AROUND YOU
37 * IGNORANT FOOLS -- sam
38 *----------------------------------------------------------------------
48 #ifdef HAVE_NPFUNCTIONS_H
49 #include <npfunctions.h>
55 * Define PLUGIN_TRACE to have the wrapper functions print
56 * messages to stderr whenever they are called.
61 #define PLUGINDEBUGSTR(msg) fprintf(stderr, "%s\n", msg)
63 #define PLUGINDEBUGSTR(msg)
66 /***********************************************************************
70 ***********************************************************************/
72 static NPNetscapeFuncs gNetscapeFuncs; /* Netscape Function table */
75 /***********************************************************************
77 * Wrapper functions : plugin calling Netscape Navigator
79 * These functions let the plugin developer just call the APIs
80 * as documented and defined in npapi.h, without needing to know
81 * about the function table and call macros in npupp.h.
83 ***********************************************************************/
86 NPN_Version(int* plugin_major, int* plugin_minor,
87 int* netscape_major, int* netscape_minor)
89 *plugin_major = NP_VERSION_MAJOR;
90 *plugin_minor = NP_VERSION_MINOR;
92 /* Major version is in high byte */
93 *netscape_major = gNetscapeFuncs.version >> 8;
94 /* Minor version is in low byte */
95 *netscape_minor = gNetscapeFuncs.version & 0xFF;
99 NPN_GetValue(NPP instance, NPNVariable variable, void *r_value)
101 return CallNPN_GetValueProc(gNetscapeFuncs.getvalue,
102 instance, variable, r_value);
106 NPN_SetValue(NPP instance, NPPVariable variable, void *value)
108 return CallNPN_SetValueProc(gNetscapeFuncs.setvalue,
109 instance, variable, value);
113 NPN_GetURL(NPP instance, const char* url, const char* window)
115 return CallNPN_GetURLProc(gNetscapeFuncs.geturl, instance, url, window);
119 NPN_GetURLNotify(NPP instance, const char* url, const char* window, void* notifyData)
121 return CallNPN_GetURLNotifyProc(gNetscapeFuncs.geturlnotify, instance, url, window, notifyData);
125 NPN_PostURL(NPP instance, const char* url, const char* window,
126 uint32 len, const char* buf, NPBool file)
128 return CallNPN_PostURLProc(gNetscapeFuncs.posturl, instance,
129 url, window, len, buf, file);
133 NPN_PostURLNotify(NPP instance, const char* url, const char* window, uint32 len,
134 const char* buf, NPBool file, void* notifyData)
136 return CallNPN_PostURLNotifyProc(gNetscapeFuncs.posturlnotify,
137 instance, url, window, len, buf, file, notifyData);
141 NPN_RequestRead(NPStream* stream, NPByteRange* rangeList)
143 return CallNPN_RequestReadProc(gNetscapeFuncs.requestread,
148 NPN_NewStream(NPP instance, NPMIMEType type, const char *window,
149 NPStream** stream_ptr)
151 return CallNPN_NewStreamProc(gNetscapeFuncs.newstream, instance,
152 type, window, stream_ptr);
156 NPN_Write(NPP instance, NPStream* stream, int32 len, void* buffer)
158 return CallNPN_WriteProc(gNetscapeFuncs.write, instance,
159 stream, len, buffer);
163 NPN_DestroyStream(NPP instance, NPStream* stream, NPError reason)
165 return CallNPN_DestroyStreamProc(gNetscapeFuncs.destroystream,
166 instance, stream, reason);
170 NPN_Status(NPP instance, const char* message)
172 CallNPN_StatusProc(gNetscapeFuncs.status, instance, message);
176 NPN_UserAgent(NPP instance)
178 return CallNPN_UserAgentProc(gNetscapeFuncs.uagent, instance);
182 NPN_MemAlloc(uint32 size)
184 return CallNPN_MemAllocProc(gNetscapeFuncs.memalloc, size);
187 void NPN_MemFree(void* ptr)
189 CallNPN_MemFreeProc(gNetscapeFuncs.memfree, ptr);
192 uint32 NPN_MemFlush(uint32 size)
194 return CallNPN_MemFlushProc(gNetscapeFuncs.memflush, size);
197 void NPN_ReloadPlugins(NPBool reloadPages)
199 CallNPN_ReloadPluginsProc(gNetscapeFuncs.reloadplugins, reloadPages);
202 JRIEnv* NPN_GetJavaEnv()
204 return CallNPN_GetJavaEnvProc(gNetscapeFuncs.getJavaEnv);
207 jref NPN_GetJavaPeer(NPP instance)
209 return CallNPN_GetJavaPeerProc(gNetscapeFuncs.getJavaPeer,
214 NPN_InvalidateRect(NPP instance, NPRect *invalidRect)
216 CallNPN_InvalidateRectProc(gNetscapeFuncs.invalidaterect, instance,
221 NPN_InvalidateRegion(NPP instance, NPRegion invalidRegion)
223 CallNPN_InvalidateRegionProc(gNetscapeFuncs.invalidateregion, instance,
228 NPN_ForceRedraw(NPP instance)
230 CallNPN_ForceRedrawProc(gNetscapeFuncs.forceredraw, instance);
233 NPIdentifier NPN_GetStringIdentifier(const NPUTF8 *name)
235 int navMinorVers = gNetscapeFuncs.version & 0xFF;
236 if( navMinorVers >= 14 )
238 return CallNPN_GetStringIdentifierProc( gNetscapeFuncs.getstringidentifier, name);
243 void NPN_GetStringIdentifiers(const NPUTF8 **names, int32_t nameCount, NPIdentifier *identifiers)
245 int navMinorVers = gNetscapeFuncs.version & 0xFF;
246 if( navMinorVers >= 14 )
248 CallNPN_GetStringIdentifiersProc( gNetscapeFuncs.getstringidentifiers, names, nameCount, identifiers);
252 NPIdentifier NPN_GetIntIdentifier(int32_t intid)
254 int navMinorVers = gNetscapeFuncs.version & 0xFF;
255 if( navMinorVers >= 14 )
257 return CallNPN_GetIntIdentifierProc( gNetscapeFuncs.getintidentifier, intid);
262 bool NPN_IdentifierIsString(NPIdentifier identifier)
264 int navMinorVers = gNetscapeFuncs.version & 0xFF;
265 if( navMinorVers >= 14 )
267 return CallNPN_IdentifierIsStringProc( gNetscapeFuncs.identifierisstring, identifier);
272 NPUTF8 *NPN_UTF8FromIdentifier(NPIdentifier identifier)
274 int navMinorVers = gNetscapeFuncs.version & 0xFF;
275 if( navMinorVers >= 14 )
277 return CallNPN_UTF8FromIdentifierProc( gNetscapeFuncs.utf8fromidentifier, identifier);
282 int32_t NPN_IntFromIdentifier(NPIdentifier identifier)
284 int navMinorVers = gNetscapeFuncs.version & 0xFF;
285 if( navMinorVers >= 14 )
287 return CallNPN_IntFromIdentifierProc( gNetscapeFuncs.intfromidentifier, identifier);
292 NPObject *NPN_CreateObject(NPP instance, NPClass *aClass)
294 int navMinorVers = gNetscapeFuncs.version & 0xFF;
295 if( navMinorVers >= 14 )
297 return CallNPN_CreateObjectProc( gNetscapeFuncs.createobject, instance, aClass);
302 NPObject *NPN_RetainObject(NPObject *npobj)
304 int navMinorVers = gNetscapeFuncs.version & 0xFF;
305 if( navMinorVers >= 14 )
307 return CallNPN_RetainObjectProc( gNetscapeFuncs.retainobject, npobj);
312 void NPN_ReleaseObject(NPObject *npobj)
314 int navMinorVers = gNetscapeFuncs.version & 0xFF;
315 if( navMinorVers >= 14 )
317 CallNPN_ReleaseObjectProc( gNetscapeFuncs.releaseobject, npobj);
321 bool NPN_Invoke(NPP instance, NPObject *npobj, NPIdentifier methodName, const NPVariant *args, uint32_t argCount, NPVariant *result)
323 int navMinorVers = gNetscapeFuncs.version & 0xFF;
324 if( navMinorVers >= 14 )
326 return CallNPN_InvokeProc( gNetscapeFuncs.invoke, instance, npobj, methodName, args, argCount, result);
331 bool NPN_InvokeDefault(NPP instance, NPObject *npobj, const NPVariant *args, uint32_t argCount, NPVariant *result)
333 int navMinorVers = gNetscapeFuncs.version & 0xFF;
334 if( navMinorVers >= 14 )
336 return CallNPN_InvokeDefaultProc( gNetscapeFuncs.invokeDefault, instance, npobj, args, argCount, result);
341 bool NPN_Evaluate(NPP instance, NPObject *npobj, NPString *script, NPVariant *result)
343 int navMinorVers = gNetscapeFuncs.version & 0xFF;
344 if( navMinorVers >= 14 )
346 return CallNPN_EvaluateProc( gNetscapeFuncs.evaluate, instance, npobj, script, result);
351 bool NPN_GetProperty(NPP instance, NPObject *npobj, NPIdentifier propertyName, NPVariant *result)
353 int navMinorVers = gNetscapeFuncs.version & 0xFF;
354 if( navMinorVers >= 14 )
356 return CallNPN_GetPropertyProc( gNetscapeFuncs.getproperty, instance, npobj, propertyName, result);
361 bool NPN_SetProperty(NPP instance, NPObject *npobj, NPIdentifier propertyName, const NPVariant *value)
363 int navMinorVers = gNetscapeFuncs.version & 0xFF;
364 if( navMinorVers >= 14 )
366 return CallNPN_SetPropertyProc( gNetscapeFuncs.setproperty, instance, npobj, propertyName, value);
371 bool NPN_RemoveProperty(NPP instance, NPObject *npobj, NPIdentifier propertyName)
373 int navMinorVers = gNetscapeFuncs.version & 0xFF;
374 if( navMinorVers >= 14 )
376 return CallNPN_RemovePropertyProc( gNetscapeFuncs.removeproperty, instance, npobj, propertyName);
381 bool NPN_HasProperty(NPP instance, NPObject *npobj, NPIdentifier propertyName)
383 int navMinorVers = gNetscapeFuncs.version & 0xFF;
384 if( navMinorVers >= 14 )
386 return CallNPN_HasPropertyProc( gNetscapeFuncs.hasproperty, instance, npobj, propertyName);
391 bool NPN_HasMethod(NPP instance, NPObject *npobj, NPIdentifier methodName)
393 int navMinorVers = gNetscapeFuncs.version & 0xFF;
394 if( navMinorVers >= 14 )
396 return CallNPN_HasMethodProc( gNetscapeFuncs.hasmethod, instance, npobj, methodName);
401 void NPN_ReleaseVariantValue(NPVariant *variant)
403 int navMinorVers = gNetscapeFuncs.version & 0xFF;
404 if( navMinorVers >= 14 )
406 CallNPN_ReleaseVariantValueProc( gNetscapeFuncs.releasevariantvalue, variant);
410 void NPN_SetException(NPObject *npobj, const NPUTF8 *message)
412 int navMinorVers = gNetscapeFuncs.version & 0xFF;
413 if( navMinorVers >= 14 )
415 CallNPN_SetExceptionProc( gNetscapeFuncs.setexception, npobj, message);
420 /***********************************************************************
422 * Wrapper functions : Netscape Navigator -> plugin
424 * These functions let the plugin developer just create the APIs
425 * as documented and defined in npapi.h, without needing to
426 * install those functions in the function table or worry about
427 * setting up globals for 68K plugins.
429 ***********************************************************************/
430 NPError Private_New(NPMIMEType pluginType, NPP instance, uint16 mode,
431 int16 argc, char* argn[], char* argv[], NPSavedData* saved);
432 NPError Private_Destroy(NPP instance, NPSavedData** save);
433 NPError Private_SetWindow(NPP instance, NPWindow* window);
434 NPError Private_NewStream(NPP instance, NPMIMEType type, NPStream* stream,
435 NPBool seekable, uint16* stype);
436 int32 Private_WriteReady(NPP instance, NPStream* stream);
437 int32 Private_Write(NPP instance, NPStream* stream, int32 offset, int32 len,
439 void Private_StreamAsFile(NPP instance, NPStream* stream, const char* fname);
440 NPError Private_DestroyStream(NPP instance, NPStream* stream, NPError reason);
441 void Private_URLNotify(NPP instance, const char* url, NPReason reason, void* notifyData);
442 void Private_Print(NPP instance, NPPrint* platformPrint);
443 NPError Private_GetValue(NPP instance, NPPVariable variable, void *r_value);
444 NPError Private_SetValue(NPP instance, NPPVariable variable, void *r_value);
445 JRIGlobalRef Private_GetJavaClass(void);
450 Private_New(NPMIMEType pluginType, NPP instance, uint16 mode,
451 int16 argc, char* argn[], char* argv[], NPSavedData* saved)
454 PLUGINDEBUGSTR("New");
455 ret = NPP_New(pluginType, instance, mode, argc, argn, argv, saved);
460 Private_Destroy(NPP instance, NPSavedData** save)
462 PLUGINDEBUGSTR("Destroy");
463 return NPP_Destroy(instance, save);
467 Private_SetWindow(NPP instance, NPWindow* window)
470 PLUGINDEBUGSTR("SetWindow");
471 err = NPP_SetWindow(instance, window);
476 Private_NewStream(NPP instance, NPMIMEType type, NPStream* stream,
477 NPBool seekable, uint16* stype)
480 PLUGINDEBUGSTR("NewStream");
481 err = NPP_NewStream(instance, type, stream, seekable, stype);
486 Private_WriteReady(NPP instance, NPStream* stream)
489 PLUGINDEBUGSTR("WriteReady");
490 result = NPP_WriteReady(instance, stream);
495 Private_Write(NPP instance, NPStream* stream, int32 offset, int32 len,
499 PLUGINDEBUGSTR("Write");
500 result = NPP_Write(instance, stream, offset, len, buffer);
505 Private_StreamAsFile(NPP instance, NPStream* stream, const char* fname)
507 PLUGINDEBUGSTR("StreamAsFile");
508 NPP_StreamAsFile(instance, stream, fname);
512 Private_DestroyStream(NPP instance, NPStream* stream, NPError reason)
515 PLUGINDEBUGSTR("DestroyStream");
516 err = NPP_DestroyStream(instance, stream, reason);
521 Private_URLNotify(NPP instance, const char* url,
522 NPReason reason, void* notifyData)
524 PLUGINDEBUGSTR("URLNotify");
525 NPP_URLNotify(instance, url, reason, notifyData);
529 Private_Print(NPP instance, NPPrint* platformPrint)
531 PLUGINDEBUGSTR("Print");
532 NPP_Print(instance, platformPrint);
536 Private_GetValue(NPP instance, NPPVariable variable, void *r_value)
538 PLUGINDEBUGSTR("GetValue");
539 return NPP_GetValue(instance, variable, r_value);
543 Private_SetValue(NPP instance, NPPVariable variable, void *r_value)
545 PLUGINDEBUGSTR("SetValue");
546 return NPP_SetValue(instance, variable, r_value);
550 Private_GetJavaClass(void)
552 jref clazz = NPP_GetJavaClass();
554 JRIEnv* env = NPN_GetJavaEnv();
555 return JRI_NewGlobalRef(env, clazz);
560 /***********************************************************************
562 * These functions are located automagically by netscape.
564 ***********************************************************************/
567 * NP_GetMIMEDescription
568 * - Netscape needs to know about this symbol
569 * - Netscape uses the return value to identify when an object instance
570 * of this plugin should be created.
573 NP_GetMIMEDescription(void)
575 return NPP_GetMIMEDescription();
579 * NP_GetValue [optional]
580 * - Netscape needs to know about this symbol.
581 * - Interfaces with plugin to get values for predefined variables
582 * that the navigator needs.
585 NP_GetValue(void *future, NPPVariable variable, void *value)
587 return NPP_GetValue(future, variable, value);
592 * - Netscape needs to know about this symbol.
593 * - It calls this function after looking up its symbol before it
594 * is about to create the first ever object of this kind.
597 * nsTable - The netscape function table. If developers just use these
598 * wrappers, they dont need to worry about all these function
602 * - This functions needs to fill the plugin function table
603 * pluginFuncs and return it. Netscape Navigator plugin
604 * library will use this function table to call the plugin.
608 NP_Initialize(NPNetscapeFuncs* nsTable, NPPluginFuncs* pluginFuncs)
610 NPError err = NPERR_NO_ERROR;
612 PLUGINDEBUGSTR("NP_Initialize");
614 /* validate input parameters */
616 if ((nsTable == NULL) || (pluginFuncs == NULL))
617 err = NPERR_INVALID_FUNCTABLE_ERROR;
620 * Check the major version passed in Netscape's function table.
621 * We won't load if the major version is newer than what we expect.
622 * Also check that the function tables passed in are big enough for
623 * all the functions we need (they could be bigger, if Netscape added
624 * new APIs, but that's OK with us -- we'll just ignore them).
628 if (err == NPERR_NO_ERROR) {
629 if ((nsTable->version >> 8) > NP_VERSION_MAJOR)
630 err = NPERR_INCOMPATIBLE_VERSION_ERROR;
631 if (nsTable->size < sizeof(NPNetscapeFuncs))
632 err = NPERR_INVALID_FUNCTABLE_ERROR;
633 if (pluginFuncs->size < sizeof(NPPluginFuncs))
634 err = NPERR_INVALID_FUNCTABLE_ERROR;
637 if (err == NPERR_NO_ERROR) {
639 * Copy all the fields of Netscape function table into our
640 * copy so we can call back into Netscape later. Note that
641 * we need to copy the fields one by one, rather than assigning
642 * the whole structure, because the Netscape function table
643 * could actually be bigger than what we expect.
645 int navMinorVers = nsTable->version & 0xFF;
647 gNetscapeFuncs.version = nsTable->version;
648 gNetscapeFuncs.size = nsTable->size;
649 gNetscapeFuncs.posturl = nsTable->posturl;
650 gNetscapeFuncs.geturl = nsTable->geturl;
651 gNetscapeFuncs.requestread = nsTable->requestread;
652 gNetscapeFuncs.newstream = nsTable->newstream;
653 gNetscapeFuncs.write = nsTable->write;
654 gNetscapeFuncs.destroystream = nsTable->destroystream;
655 gNetscapeFuncs.status = nsTable->status;
656 gNetscapeFuncs.uagent = nsTable->uagent;
657 gNetscapeFuncs.memalloc = nsTable->memalloc;
658 gNetscapeFuncs.memfree = nsTable->memfree;
659 gNetscapeFuncs.memflush = nsTable->memflush;
660 gNetscapeFuncs.reloadplugins = nsTable->reloadplugins;
661 if( navMinorVers >= NPVERS_HAS_LIVECONNECT )
663 gNetscapeFuncs.getJavaEnv = nsTable->getJavaEnv;
664 gNetscapeFuncs.getJavaPeer = nsTable->getJavaPeer;
666 if( navMinorVers >= NPVERS_HAS_NOTIFICATION )
668 gNetscapeFuncs.geturlnotify = nsTable->geturlnotify;
669 gNetscapeFuncs.posturlnotify = nsTable->posturlnotify;
671 gNetscapeFuncs.getvalue = nsTable->getvalue;
672 gNetscapeFuncs.setvalue = nsTable->setvalue;
673 gNetscapeFuncs.invalidaterect = nsTable->invalidaterect;
674 gNetscapeFuncs.invalidateregion = nsTable->invalidateregion;
675 gNetscapeFuncs.forceredraw = nsTable->forceredraw;
676 if( navMinorVers >= 14 )
679 gNetscapeFuncs.getstringidentifier = nsTable->getstringidentifier;
680 gNetscapeFuncs.getstringidentifiers = nsTable->getstringidentifiers;
681 gNetscapeFuncs.getintidentifier = nsTable->getintidentifier;
682 gNetscapeFuncs.identifierisstring = nsTable->identifierisstring;
683 gNetscapeFuncs.utf8fromidentifier = nsTable->utf8fromidentifier;
684 gNetscapeFuncs.intfromidentifier = nsTable->intfromidentifier;
685 gNetscapeFuncs.createobject = nsTable->createobject;
686 gNetscapeFuncs.retainobject = nsTable->retainobject;
687 gNetscapeFuncs.releaseobject = nsTable->releaseobject;
688 gNetscapeFuncs.invoke = nsTable->invoke;
689 gNetscapeFuncs.invokeDefault = nsTable->invokeDefault;
690 gNetscapeFuncs.evaluate = nsTable->evaluate;
691 gNetscapeFuncs.getproperty = nsTable->getproperty;
692 gNetscapeFuncs.setproperty = nsTable->setproperty;
693 gNetscapeFuncs.removeproperty = nsTable->removeproperty;
694 gNetscapeFuncs.hasproperty = nsTable->hasproperty;
695 gNetscapeFuncs.hasmethod = nsTable->hasmethod;
696 gNetscapeFuncs.releasevariantvalue = nsTable->releasevariantvalue;
697 gNetscapeFuncs.setexception = nsTable->setexception;
701 * Set up the plugin function table that Netscape will use to
702 * call us. Netscape needs to know about our version and size
703 * and have a UniversalProcPointer for every function we
706 pluginFuncs->version = (NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR;
707 pluginFuncs->size = sizeof(NPPluginFuncs);
708 pluginFuncs->newp = NewNPP_NewProc(Private_New);
709 pluginFuncs->destroy = NewNPP_DestroyProc(Private_Destroy);
710 pluginFuncs->setwindow = NewNPP_SetWindowProc(Private_SetWindow);
711 pluginFuncs->newstream = NewNPP_NewStreamProc(Private_NewStream);
712 pluginFuncs->destroystream = NewNPP_DestroyStreamProc(Private_DestroyStream);
713 pluginFuncs->asfile = NewNPP_StreamAsFileProc(Private_StreamAsFile);
714 pluginFuncs->writeready = NewNPP_WriteReadyProc(Private_WriteReady);
715 pluginFuncs->write = NewNPP_WriteProc(Private_Write);
716 pluginFuncs->print = NewNPP_PrintProc(Private_Print);
717 pluginFuncs->event = NULL;
718 pluginFuncs->getvalue = NewNPP_GetValueProc(Private_GetValue);
719 pluginFuncs->setvalue = NewNPP_SetValueProc(Private_SetValue);
720 if( navMinorVers >= NPVERS_HAS_NOTIFICATION )
722 pluginFuncs->urlnotify = NewNPP_URLNotifyProc(Private_URLNotify);
725 if( navMinorVers >= NPVERS_HAS_LIVECONNECT )
727 pluginFuncs->javaClass = (JRIGlobalRef) Private_GetJavaClass();
730 pluginFuncs->javaClass = NULL;
733 err = NPP_Initialize();
740 * NP_Shutdown [optional]
741 * - Netscape needs to know about this symbol.
742 * - It calls this function after looking up its symbol after
743 * the last object of this kind has been destroyed.
749 PLUGINDEBUGSTR("NP_Shutdown");
751 return NPERR_NO_ERROR;