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 *----------------------------------------------------------------------
49 * Define PLUGIN_TRACE to have the wrapper functions print
50 * messages to stderr whenever they are called.
55 #define PLUGINDEBUGSTR(msg) fprintf(stderr, "%s\n", msg)
57 #define PLUGINDEBUGSTR(msg)
60 /***********************************************************************
64 ***********************************************************************/
66 static NPNetscapeFuncs gNetscapeFuncs; /* Netscape Function table */
69 /***********************************************************************
71 * Wrapper functions : plugin calling Netscape Navigator
73 * These functions let the plugin developer just call the APIs
74 * as documented and defined in npapi.h, without needing to know
75 * about the function table and call macros in npupp.h.
77 ***********************************************************************/
80 NPN_Version(int* plugin_major, int* plugin_minor,
81 int* netscape_major, int* netscape_minor)
83 *plugin_major = NP_VERSION_MAJOR;
84 *plugin_minor = NP_VERSION_MINOR;
86 /* Major version is in high byte */
87 *netscape_major = gNetscapeFuncs.version >> 8;
88 /* Minor version is in low byte */
89 *netscape_minor = gNetscapeFuncs.version & 0xFF;
93 NPN_GetValue(NPP instance, NPNVariable variable, void *r_value)
95 return CallNPN_GetValueProc(gNetscapeFuncs.getvalue,
96 instance, variable, r_value);
100 NPN_SetValue(NPP instance, NPPVariable variable, void *value)
102 return CallNPN_SetValueProc(gNetscapeFuncs.setvalue,
103 instance, variable, value);
107 NPN_GetURL(NPP instance, const char* url, const char* window)
109 return CallNPN_GetURLProc(gNetscapeFuncs.geturl, instance, url, window);
113 NPN_GetURLNotify(NPP instance, const char* url, const char* window, void* notifyData)
115 return CallNPN_GetURLNotifyProc(gNetscapeFuncs.geturlnotify, instance, url, window, notifyData);
119 NPN_PostURL(NPP instance, const char* url, const char* window,
120 uint32 len, const char* buf, NPBool file)
122 return CallNPN_PostURLProc(gNetscapeFuncs.posturl, instance,
123 url, window, len, buf, file);
127 NPN_PostURLNotify(NPP instance, const char* url, const char* window, uint32 len,
128 const char* buf, NPBool file, void* notifyData)
130 return CallNPN_PostURLNotifyProc(gNetscapeFuncs.posturlnotify,
131 instance, url, window, len, buf, file, notifyData);
135 NPN_RequestRead(NPStream* stream, NPByteRange* rangeList)
137 return CallNPN_RequestReadProc(gNetscapeFuncs.requestread,
142 NPN_NewStream(NPP instance, NPMIMEType type, const char *window,
143 NPStream** stream_ptr)
145 return CallNPN_NewStreamProc(gNetscapeFuncs.newstream, instance,
146 type, window, stream_ptr);
150 NPN_Write(NPP instance, NPStream* stream, int32 len, void* buffer)
152 return CallNPN_WriteProc(gNetscapeFuncs.write, instance,
153 stream, len, buffer);
157 NPN_DestroyStream(NPP instance, NPStream* stream, NPError reason)
159 return CallNPN_DestroyStreamProc(gNetscapeFuncs.destroystream,
160 instance, stream, reason);
164 NPN_Status(NPP instance, const char* message)
166 CallNPN_StatusProc(gNetscapeFuncs.status, instance, message);
170 NPN_UserAgent(NPP instance)
172 return CallNPN_UserAgentProc(gNetscapeFuncs.uagent, instance);
176 NPN_MemAlloc(uint32 size)
178 return CallNPN_MemAllocProc(gNetscapeFuncs.memalloc, size);
181 void NPN_MemFree(void* ptr)
183 CallNPN_MemFreeProc(gNetscapeFuncs.memfree, ptr);
186 uint32 NPN_MemFlush(uint32 size)
188 return CallNPN_MemFlushProc(gNetscapeFuncs.memflush, size);
191 void NPN_ReloadPlugins(NPBool reloadPages)
193 CallNPN_ReloadPluginsProc(gNetscapeFuncs.reloadplugins, reloadPages);
196 JRIEnv* NPN_GetJavaEnv()
198 return CallNPN_GetJavaEnvProc(gNetscapeFuncs.getJavaEnv);
201 jref NPN_GetJavaPeer(NPP instance)
203 return CallNPN_GetJavaPeerProc(gNetscapeFuncs.getJavaPeer,
208 NPN_InvalidateRect(NPP instance, NPRect *invalidRect)
210 CallNPN_InvalidateRectProc(gNetscapeFuncs.invalidaterect, instance,
215 NPN_InvalidateRegion(NPP instance, NPRegion invalidRegion)
217 CallNPN_InvalidateRegionProc(gNetscapeFuncs.invalidateregion, instance,
222 NPN_ForceRedraw(NPP instance)
224 CallNPN_ForceRedrawProc(gNetscapeFuncs.forceredraw, instance);
227 NPIdentifier NPN_GetStringIdentifier(const NPUTF8 *name)
229 int navMinorVers = gNetscapeFuncs.version & 0xFF;
230 if( navMinorVers >= 14 )
232 return CallNPN_GetStringIdentifierProc( gNetscapeFuncs.getstringidentifier, name);
237 void NPN_GetStringIdentifiers(const NPUTF8 **names, int32_t nameCount, NPIdentifier *identifiers)
239 int navMinorVers = gNetscapeFuncs.version & 0xFF;
240 if( navMinorVers >= 14 )
242 CallNPN_GetStringIdentifiersProc( gNetscapeFuncs.getstringidentifiers, names, nameCount, identifiers);
246 NPIdentifier NPN_GetIntIdentifier(int32_t intid)
248 int navMinorVers = gNetscapeFuncs.version & 0xFF;
249 if( navMinorVers >= 14 )
251 return CallNPN_GetIntIdentifierProc( gNetscapeFuncs.getintidentifier, intid);
256 bool NPN_IdentifierIsString(NPIdentifier identifier)
258 int navMinorVers = gNetscapeFuncs.version & 0xFF;
259 if( navMinorVers >= 14 )
261 return CallNPN_IdentifierIsStringProc( gNetscapeFuncs.identifierisstring, identifier);
266 NPUTF8 *NPN_UTF8FromIdentifier(NPIdentifier identifier)
268 int navMinorVers = gNetscapeFuncs.version & 0xFF;
269 if( navMinorVers >= 14 )
271 return CallNPN_UTF8FromIdentifierProc( gNetscapeFuncs.utf8fromidentifier, identifier);
276 int32_t NPN_IntFromIdentifier(NPIdentifier identifier)
278 int navMinorVers = gNetscapeFuncs.version & 0xFF;
279 if( navMinorVers >= 14 )
281 return CallNPN_IntFromIdentifierProc( gNetscapeFuncs.intfromidentifier, identifier);
286 NPObject *NPN_CreateObject(NPP instance, NPClass *aClass)
288 int navMinorVers = gNetscapeFuncs.version & 0xFF;
289 if( navMinorVers >= 14 )
291 return CallNPN_CreateObjectProc( gNetscapeFuncs.createobject, instance, aClass);
296 NPObject *NPN_RetainObject(NPObject *npobj)
298 int navMinorVers = gNetscapeFuncs.version & 0xFF;
299 if( navMinorVers >= 14 )
301 return CallNPN_RetainObjectProc( gNetscapeFuncs.retainobject, npobj);
306 void NPN_ReleaseObject(NPObject *npobj)
308 int navMinorVers = gNetscapeFuncs.version & 0xFF;
309 if( navMinorVers >= 14 )
311 CallNPN_ReleaseObjectProc( gNetscapeFuncs.releaseobject, npobj);
315 bool NPN_Invoke(NPP instance, NPObject *npobj, NPIdentifier methodName, const NPVariant *args, uint32_t argCount, NPVariant *result)
317 int navMinorVers = gNetscapeFuncs.version & 0xFF;
318 if( navMinorVers >= 14 )
320 return CallNPN_InvokeProc( gNetscapeFuncs.invoke, instance, npobj, methodName, args, argCount, result);
325 bool NPN_InvokeDefault(NPP instance, NPObject *npobj, const NPVariant *args, uint32_t argCount, NPVariant *result)
327 int navMinorVers = gNetscapeFuncs.version & 0xFF;
328 if( navMinorVers >= 14 )
330 return CallNPN_InvokeDefaultProc( gNetscapeFuncs.invokeDefault, instance, npobj, args, argCount, result);
335 bool NPN_Evaluate(NPP instance, NPObject *npobj, NPString *script, NPVariant *result)
337 int navMinorVers = gNetscapeFuncs.version & 0xFF;
338 if( navMinorVers >= 14 )
340 return CallNPN_EvaluateProc( gNetscapeFuncs.evaluate, instance, npobj, script, result);
345 bool NPN_GetProperty(NPP instance, NPObject *npobj, NPIdentifier propertyName, NPVariant *result)
347 int navMinorVers = gNetscapeFuncs.version & 0xFF;
348 if( navMinorVers >= 14 )
350 return CallNPN_GetPropertyProc( gNetscapeFuncs.getproperty, instance, npobj, propertyName, result);
355 bool NPN_SetProperty(NPP instance, NPObject *npobj, NPIdentifier propertyName, const NPVariant *value)
357 int navMinorVers = gNetscapeFuncs.version & 0xFF;
358 if( navMinorVers >= 14 )
360 return CallNPN_SetPropertyProc( gNetscapeFuncs.setproperty, instance, npobj, propertyName, value);
365 bool NPN_RemoveProperty(NPP instance, NPObject *npobj, NPIdentifier propertyName)
367 int navMinorVers = gNetscapeFuncs.version & 0xFF;
368 if( navMinorVers >= 14 )
370 return CallNPN_RemovePropertyProc( gNetscapeFuncs.removeproperty, instance, npobj, propertyName);
375 bool NPN_HasProperty(NPP instance, NPObject *npobj, NPIdentifier propertyName)
377 int navMinorVers = gNetscapeFuncs.version & 0xFF;
378 if( navMinorVers >= 14 )
380 return CallNPN_HasPropertyProc( gNetscapeFuncs.hasproperty, instance, npobj, propertyName);
385 bool NPN_HasMethod(NPP instance, NPObject *npobj, NPIdentifier methodName)
387 int navMinorVers = gNetscapeFuncs.version & 0xFF;
388 if( navMinorVers >= 14 )
390 return CallNPN_HasMethodProc( gNetscapeFuncs.hasmethod, instance, npobj, methodName);
395 void NPN_ReleaseVariantValue(NPVariant *variant)
397 int navMinorVers = gNetscapeFuncs.version & 0xFF;
398 if( navMinorVers >= 14 )
400 CallNPN_ReleaseVariantValueProc( gNetscapeFuncs.releasevariantvalue, variant);
404 void NPN_SetException(NPObject *npobj, const NPUTF8 *message)
406 int navMinorVers = gNetscapeFuncs.version & 0xFF;
407 if( navMinorVers >= 14 )
409 CallNPN_SetExceptionProc( gNetscapeFuncs.setexception, npobj, message);
414 /***********************************************************************
416 * Wrapper functions : Netscape Navigator -> plugin
418 * These functions let the plugin developer just create the APIs
419 * as documented and defined in npapi.h, without needing to
420 * install those functions in the function table or worry about
421 * setting up globals for 68K plugins.
423 ***********************************************************************/
424 NPError Private_New(NPMIMEType pluginType, NPP instance, uint16 mode,
425 int16 argc, char* argn[], char* argv[], NPSavedData* saved);
426 NPError Private_Destroy(NPP instance, NPSavedData** save);
427 NPError Private_SetWindow(NPP instance, NPWindow* window);
428 NPError Private_NewStream(NPP instance, NPMIMEType type, NPStream* stream,
429 NPBool seekable, uint16* stype);
430 int32 Private_WriteReady(NPP instance, NPStream* stream);
431 int32 Private_Write(NPP instance, NPStream* stream, int32 offset, int32 len,
433 void Private_StreamAsFile(NPP instance, NPStream* stream, const char* fname);
434 NPError Private_DestroyStream(NPP instance, NPStream* stream, NPError reason);
435 void Private_URLNotify(NPP instance, const char* url, NPReason reason, void* notifyData);
436 void Private_Print(NPP instance, NPPrint* platformPrint);
437 NPError Private_GetValue(NPP instance, NPPVariable variable, void *r_value);
438 NPError Private_SetValue(NPP instance, NPPVariable variable, void *r_value);
439 JRIGlobalRef Private_GetJavaClass(void);
444 Private_New(NPMIMEType pluginType, NPP instance, uint16 mode,
445 int16 argc, char* argn[], char* argv[], NPSavedData* saved)
448 PLUGINDEBUGSTR("New");
449 ret = NPP_New(pluginType, instance, mode, argc, argn, argv, saved);
454 Private_Destroy(NPP instance, NPSavedData** save)
456 PLUGINDEBUGSTR("Destroy");
457 return NPP_Destroy(instance, save);
461 Private_SetWindow(NPP instance, NPWindow* window)
464 PLUGINDEBUGSTR("SetWindow");
465 err = NPP_SetWindow(instance, window);
470 Private_NewStream(NPP instance, NPMIMEType type, NPStream* stream,
471 NPBool seekable, uint16* stype)
474 PLUGINDEBUGSTR("NewStream");
475 err = NPP_NewStream(instance, type, stream, seekable, stype);
480 Private_WriteReady(NPP instance, NPStream* stream)
483 PLUGINDEBUGSTR("WriteReady");
484 result = NPP_WriteReady(instance, stream);
489 Private_Write(NPP instance, NPStream* stream, int32 offset, int32 len,
493 PLUGINDEBUGSTR("Write");
494 result = NPP_Write(instance, stream, offset, len, buffer);
499 Private_StreamAsFile(NPP instance, NPStream* stream, const char* fname)
501 PLUGINDEBUGSTR("StreamAsFile");
502 NPP_StreamAsFile(instance, stream, fname);
506 Private_DestroyStream(NPP instance, NPStream* stream, NPError reason)
509 PLUGINDEBUGSTR("DestroyStream");
510 err = NPP_DestroyStream(instance, stream, reason);
515 Private_URLNotify(NPP instance, const char* url,
516 NPReason reason, void* notifyData)
518 PLUGINDEBUGSTR("URLNotify");
519 NPP_URLNotify(instance, url, reason, notifyData);
523 Private_Print(NPP instance, NPPrint* platformPrint)
525 PLUGINDEBUGSTR("Print");
526 NPP_Print(instance, platformPrint);
530 Private_GetValue(NPP instance, NPPVariable variable, void *r_value)
532 PLUGINDEBUGSTR("GetValue");
533 return NPP_GetValue(instance, variable, r_value);
537 Private_SetValue(NPP instance, NPPVariable variable, void *r_value)
539 PLUGINDEBUGSTR("SetValue");
540 return NPP_SetValue(instance, variable, r_value);
544 Private_GetJavaClass(void)
546 jref clazz = NPP_GetJavaClass();
548 JRIEnv* env = NPN_GetJavaEnv();
549 return JRI_NewGlobalRef(env, clazz);
554 /***********************************************************************
556 * These functions are located automagically by netscape.
558 ***********************************************************************/
561 * NP_GetMIMEDescription
562 * - Netscape needs to know about this symbol
563 * - Netscape uses the return value to identify when an object instance
564 * of this plugin should be created.
567 NP_GetMIMEDescription(void)
569 return NPP_GetMIMEDescription();
573 * NP_GetValue [optional]
574 * - Netscape needs to know about this symbol.
575 * - Interfaces with plugin to get values for predefined variables
576 * that the navigator needs.
579 NP_GetValue(void *future, NPPVariable variable, void *value)
581 return NPP_GetValue(future, variable, value);
586 * - Netscape needs to know about this symbol.
587 * - It calls this function after looking up its symbol before it
588 * is about to create the first ever object of this kind.
591 * nsTable - The netscape function table. If developers just use these
592 * wrappers, they dont need to worry about all these function
596 * - This functions needs to fill the plugin function table
597 * pluginFuncs and return it. Netscape Navigator plugin
598 * library will use this function table to call the plugin.
602 NP_Initialize(NPNetscapeFuncs* nsTable, NPPluginFuncs* pluginFuncs)
604 NPError err = NPERR_NO_ERROR;
606 PLUGINDEBUGSTR("NP_Initialize");
608 /* validate input parameters */
610 if ((nsTable == NULL) || (pluginFuncs == NULL))
611 err = NPERR_INVALID_FUNCTABLE_ERROR;
614 * Check the major version passed in Netscape's function table.
615 * We won't load if the major version is newer than what we expect.
616 * Also check that the function tables passed in are big enough for
617 * all the functions we need (they could be bigger, if Netscape added
618 * new APIs, but that's OK with us -- we'll just ignore them).
622 if (err == NPERR_NO_ERROR) {
623 if ((nsTable->version >> 8) > NP_VERSION_MAJOR)
624 err = NPERR_INCOMPATIBLE_VERSION_ERROR;
625 if (nsTable->size < sizeof(NPNetscapeFuncs))
626 err = NPERR_INVALID_FUNCTABLE_ERROR;
627 if (pluginFuncs->size < sizeof(NPPluginFuncs))
628 err = NPERR_INVALID_FUNCTABLE_ERROR;
631 if (err == NPERR_NO_ERROR) {
633 * Copy all the fields of Netscape function table into our
634 * copy so we can call back into Netscape later. Note that
635 * we need to copy the fields one by one, rather than assigning
636 * the whole structure, because the Netscape function table
637 * could actually be bigger than what we expect.
639 int navMinorVers = nsTable->version & 0xFF;
641 gNetscapeFuncs.version = nsTable->version;
642 gNetscapeFuncs.size = nsTable->size;
643 gNetscapeFuncs.posturl = nsTable->posturl;
644 gNetscapeFuncs.geturl = nsTable->geturl;
645 gNetscapeFuncs.requestread = nsTable->requestread;
646 gNetscapeFuncs.newstream = nsTable->newstream;
647 gNetscapeFuncs.write = nsTable->write;
648 gNetscapeFuncs.destroystream = nsTable->destroystream;
649 gNetscapeFuncs.status = nsTable->status;
650 gNetscapeFuncs.uagent = nsTable->uagent;
651 gNetscapeFuncs.memalloc = nsTable->memalloc;
652 gNetscapeFuncs.memfree = nsTable->memfree;
653 gNetscapeFuncs.memflush = nsTable->memflush;
654 gNetscapeFuncs.reloadplugins = nsTable->reloadplugins;
655 if( navMinorVers >= NPVERS_HAS_LIVECONNECT )
657 gNetscapeFuncs.getJavaEnv = nsTable->getJavaEnv;
658 gNetscapeFuncs.getJavaPeer = nsTable->getJavaPeer;
660 if( navMinorVers >= NPVERS_HAS_NOTIFICATION )
662 gNetscapeFuncs.geturlnotify = nsTable->geturlnotify;
663 gNetscapeFuncs.posturlnotify = nsTable->posturlnotify;
665 gNetscapeFuncs.getvalue = nsTable->getvalue;
666 gNetscapeFuncs.setvalue = nsTable->setvalue;
667 gNetscapeFuncs.invalidaterect = nsTable->invalidaterect;
668 gNetscapeFuncs.invalidateregion = nsTable->invalidateregion;
669 gNetscapeFuncs.forceredraw = nsTable->forceredraw;
670 if( navMinorVers >= 14 )
673 gNetscapeFuncs.getstringidentifier = nsTable->getstringidentifier;
674 gNetscapeFuncs.getstringidentifiers = nsTable->getstringidentifiers;
675 gNetscapeFuncs.getintidentifier = nsTable->getintidentifier;
676 gNetscapeFuncs.identifierisstring = nsTable->identifierisstring;
677 gNetscapeFuncs.utf8fromidentifier = nsTable->utf8fromidentifier;
678 gNetscapeFuncs.intfromidentifier = nsTable->intfromidentifier;
679 gNetscapeFuncs.createobject = nsTable->createobject;
680 gNetscapeFuncs.retainobject = nsTable->retainobject;
681 gNetscapeFuncs.releaseobject = nsTable->releaseobject;
682 gNetscapeFuncs.invoke = nsTable->invoke;
683 gNetscapeFuncs.invokeDefault = nsTable->invokeDefault;
684 gNetscapeFuncs.evaluate = nsTable->evaluate;
685 gNetscapeFuncs.getproperty = nsTable->getproperty;
686 gNetscapeFuncs.setproperty = nsTable->setproperty;
687 gNetscapeFuncs.removeproperty = nsTable->removeproperty;
688 gNetscapeFuncs.hasproperty = nsTable->hasproperty;
689 gNetscapeFuncs.hasmethod = nsTable->hasmethod;
690 gNetscapeFuncs.releasevariantvalue = nsTable->releasevariantvalue;
691 gNetscapeFuncs.setexception = nsTable->setexception;
695 * Set up the plugin function table that Netscape will use to
696 * call us. Netscape needs to know about our version and size
697 * and have a UniversalProcPointer for every function we
700 pluginFuncs->version = (NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR;
701 pluginFuncs->size = sizeof(NPPluginFuncs);
702 pluginFuncs->newp = NewNPP_NewProc(Private_New);
703 pluginFuncs->destroy = NewNPP_DestroyProc(Private_Destroy);
704 pluginFuncs->setwindow = NewNPP_SetWindowProc(Private_SetWindow);
705 pluginFuncs->newstream = NewNPP_NewStreamProc(Private_NewStream);
706 pluginFuncs->destroystream = NewNPP_DestroyStreamProc(Private_DestroyStream);
707 pluginFuncs->asfile = NewNPP_StreamAsFileProc(Private_StreamAsFile);
708 pluginFuncs->writeready = NewNPP_WriteReadyProc(Private_WriteReady);
709 pluginFuncs->write = NewNPP_WriteProc(Private_Write);
710 pluginFuncs->print = NewNPP_PrintProc(Private_Print);
711 pluginFuncs->event = NULL;
712 pluginFuncs->getvalue = NewNPP_GetValueProc(Private_GetValue);
713 pluginFuncs->setvalue = NewNPP_SetValueProc(Private_SetValue);
714 if( navMinorVers >= NPVERS_HAS_NOTIFICATION )
716 pluginFuncs->urlnotify = NewNPP_URLNotifyProc(Private_URLNotify);
719 if( navMinorVers >= NPVERS_HAS_LIVECONNECT )
721 pluginFuncs->javaClass = (JRIGlobalRef) Private_GetJavaClass();
724 pluginFuncs->javaClass = NULL;
727 err = NPP_Initialize();
734 * NP_Shutdown [optional]
735 * - Netscape needs to know about this symbol.
736 * - It calls this function after looking up its symbol after
737 * the last object of this kind has been destroyed.
743 PLUGINDEBUGSTR("NP_Shutdown");
745 return NPERR_NO_ERROR;