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 *----------------------------------------------------------------------
50 * Define PLUGIN_TRACE to have the wrapper functions print
51 * messages to stderr whenever they are called.
56 #define PLUGINDEBUGSTR(msg) fprintf(stderr, "%s\n", msg)
58 #define PLUGINDEBUGSTR(msg)
62 /***********************************************************************
66 ***********************************************************************/
68 static NPNetscapeFuncs gNetscapeFuncs; /* Netscape Function table */
71 /***********************************************************************
73 * Wrapper functions : plugin calling Netscape Navigator
75 * These functions let the plugin developer just call the APIs
76 * as documented and defined in npapi.h, without needing to know
77 * about the function table and call macros in npupp.h.
79 ***********************************************************************/
82 NPN_Version(int* plugin_major, int* plugin_minor,
83 int* netscape_major, int* netscape_minor)
85 *plugin_major = NP_VERSION_MAJOR;
86 *plugin_minor = NP_VERSION_MINOR;
88 /* Major version is in high byte */
89 *netscape_major = gNetscapeFuncs.version >> 8;
90 /* Minor version is in low byte */
91 *netscape_minor = gNetscapeFuncs.version & 0xFF;
95 NPN_GetValue(NPP instance, NPNVariable variable, void *r_value)
97 return CallNPN_GetValueProc(gNetscapeFuncs.getvalue,
98 instance, variable, r_value);
102 NPN_SetValue(NPP instance, NPPVariable variable, void *value)
104 return CallNPN_SetValueProc(gNetscapeFuncs.setvalue,
105 instance, variable, value);
109 NPN_GetURL(NPP instance, const char* url, const char* window)
111 return CallNPN_GetURLProc(gNetscapeFuncs.geturl, instance, url, window);
115 NPN_GetURLNotify(NPP instance, const char* url, const char* window, void* notifyData)
117 return CallNPN_GetURLNotifyProc(gNetscapeFuncs.geturlnotify, instance, url, window, notifyData);
121 NPN_PostURL(NPP instance, const char* url, const char* window,
122 uint32 len, const char* buf, NPBool file)
124 return CallNPN_PostURLProc(gNetscapeFuncs.posturl, instance,
125 url, window, len, buf, file);
129 NPN_PostURLNotify(NPP instance, const char* url, const char* window, uint32 len,
130 const char* buf, NPBool file, void* notifyData)
132 return CallNPN_PostURLNotifyProc(gNetscapeFuncs.posturlnotify,
133 instance, url, window, len, buf, file, notifyData);
137 NPN_RequestRead(NPStream* stream, NPByteRange* rangeList)
139 return CallNPN_RequestReadProc(gNetscapeFuncs.requestread,
144 NPN_NewStream(NPP instance, NPMIMEType type, const char *window,
145 NPStream** stream_ptr)
147 return CallNPN_NewStreamProc(gNetscapeFuncs.newstream, instance,
148 type, window, stream_ptr);
152 NPN_Write(NPP instance, NPStream* stream, int32 len, void* buffer)
154 return CallNPN_WriteProc(gNetscapeFuncs.write, instance,
155 stream, len, buffer);
159 NPN_DestroyStream(NPP instance, NPStream* stream, NPError reason)
161 return CallNPN_DestroyStreamProc(gNetscapeFuncs.destroystream,
162 instance, stream, reason);
166 NPN_Status(NPP instance, const char* message)
168 CallNPN_StatusProc(gNetscapeFuncs.status, instance, message);
172 NPN_UserAgent(NPP instance)
174 return CallNPN_UserAgentProc(gNetscapeFuncs.uagent, instance);
178 NPN_MemAlloc(uint32 size)
180 return CallNPN_MemAllocProc(gNetscapeFuncs.memalloc, size);
183 void NPN_MemFree(void* ptr)
185 CallNPN_MemFreeProc(gNetscapeFuncs.memfree, ptr);
188 uint32 NPN_MemFlush(uint32 size)
190 return CallNPN_MemFlushProc(gNetscapeFuncs.memflush, size);
193 void NPN_ReloadPlugins(NPBool reloadPages)
195 CallNPN_ReloadPluginsProc(gNetscapeFuncs.reloadplugins, reloadPages);
198 JRIEnv* NPN_GetJavaEnv()
200 return CallNPN_GetJavaEnvProc(gNetscapeFuncs.getJavaEnv);
203 jref NPN_GetJavaPeer(NPP instance)
205 return CallNPN_GetJavaPeerProc(gNetscapeFuncs.getJavaPeer,
210 NPN_InvalidateRect(NPP instance, NPRect *invalidRect)
212 CallNPN_InvalidateRectProc(gNetscapeFuncs.invalidaterect, instance,
217 NPN_InvalidateRegion(NPP instance, NPRegion invalidRegion)
219 CallNPN_InvalidateRegionProc(gNetscapeFuncs.invalidateregion, instance,
224 NPN_ForceRedraw(NPP instance)
226 CallNPN_ForceRedrawProc(gNetscapeFuncs.forceredraw, instance);
229 NPIdentifier NPN_GetStringIdentifier(const NPUTF8 *name)
231 int navMinorVers = gNetscapeFuncs.version & 0xFF;
232 if( navMinorVers >= 14 )
234 return CallNPN_GetStringIdentifierProc( gNetscapeFuncs.getstringidentifier, name);
239 void NPN_GetStringIdentifiers(const NPUTF8 **names, int32_t nameCount, NPIdentifier *identifiers)
241 int navMinorVers = gNetscapeFuncs.version & 0xFF;
242 if( navMinorVers >= 14 )
244 CallNPN_GetStringIdentifiersProc( gNetscapeFuncs.getstringidentifiers, names, nameCount, identifiers);
248 NPIdentifier NPN_GetIntIdentifier(int32_t intid)
250 int navMinorVers = gNetscapeFuncs.version & 0xFF;
251 if( navMinorVers >= 14 )
253 return CallNPN_GetIntIdentifierProc( gNetscapeFuncs.getintidentifier, intid);
258 bool NPN_IdentifierIsString(NPIdentifier identifier)
260 int navMinorVers = gNetscapeFuncs.version & 0xFF;
261 if( navMinorVers >= 14 )
263 return CallNPN_IdentifierIsStringProc( gNetscapeFuncs.identifierisstring, identifier);
268 NPUTF8 *NPN_UTF8FromIdentifier(NPIdentifier identifier)
270 int navMinorVers = gNetscapeFuncs.version & 0xFF;
271 if( navMinorVers >= 14 )
273 return CallNPN_UTF8FromIdentifierProc( gNetscapeFuncs.utf8fromidentifier, identifier);
278 int32_t NPN_IntFromIdentifier(NPIdentifier identifier)
280 int navMinorVers = gNetscapeFuncs.version & 0xFF;
281 if( navMinorVers >= 14 )
283 return CallNPN_IntFromIdentifierProc( gNetscapeFuncs.intfromidentifier, identifier);
288 NPObject *NPN_CreateObject(NPP instance, NPClass *aClass)
290 int navMinorVers = gNetscapeFuncs.version & 0xFF;
291 if( navMinorVers >= 14 )
293 return CallNPN_CreateObjectProc( gNetscapeFuncs.createobject, instance, aClass);
298 NPObject *NPN_RetainObject(NPObject *npobj)
300 int navMinorVers = gNetscapeFuncs.version & 0xFF;
301 if( navMinorVers >= 14 )
303 return CallNPN_RetainObjectProc( gNetscapeFuncs.retainobject, npobj);
308 void NPN_ReleaseObject(NPObject *npobj)
310 int navMinorVers = gNetscapeFuncs.version & 0xFF;
311 if( navMinorVers >= 14 )
313 CallNPN_ReleaseObjectProc( gNetscapeFuncs.releaseobject, npobj);
317 bool NPN_Invoke(NPP instance, NPObject *npobj, NPIdentifier methodName, const NPVariant *args, uint32_t argCount, NPVariant *result)
319 int navMinorVers = gNetscapeFuncs.version & 0xFF;
320 if( navMinorVers >= 14 )
322 return CallNPN_InvokeProc( gNetscapeFuncs.invoke, instance, npobj, methodName, args, argCount, result);
327 bool NPN_InvokeDefault(NPP instance, NPObject *npobj, const NPVariant *args, uint32_t argCount, NPVariant *result)
329 int navMinorVers = gNetscapeFuncs.version & 0xFF;
330 if( navMinorVers >= 14 )
332 return CallNPN_InvokeDefaultProc( gNetscapeFuncs.invokeDefault, instance, npobj, args, argCount, result);
337 bool NPN_Evaluate(NPP instance, NPObject *npobj, NPString *script, NPVariant *result)
339 int navMinorVers = gNetscapeFuncs.version & 0xFF;
340 if( navMinorVers >= 14 )
342 return CallNPN_EvaluateProc( gNetscapeFuncs.evaluate, instance, npobj, script, result);
347 bool NPN_GetProperty(NPP instance, NPObject *npobj, NPIdentifier propertyName, NPVariant *result)
349 int navMinorVers = gNetscapeFuncs.version & 0xFF;
350 if( navMinorVers >= 14 )
352 return CallNPN_GetPropertyProc( gNetscapeFuncs.getproperty, instance, npobj, propertyName, result);
357 bool NPN_SetProperty(NPP instance, NPObject *npobj, NPIdentifier propertyName, const NPVariant *value)
359 int navMinorVers = gNetscapeFuncs.version & 0xFF;
360 if( navMinorVers >= 14 )
362 return CallNPN_SetPropertyProc( gNetscapeFuncs.setproperty, instance, npobj, propertyName, value);
367 bool NPN_RemoveProperty(NPP instance, NPObject *npobj, NPIdentifier propertyName)
369 int navMinorVers = gNetscapeFuncs.version & 0xFF;
370 if( navMinorVers >= 14 )
372 return CallNPN_RemovePropertyProc( gNetscapeFuncs.removeproperty, instance, npobj, propertyName);
377 bool NPN_HasProperty(NPP instance, NPObject *npobj, NPIdentifier propertyName)
379 int navMinorVers = gNetscapeFuncs.version & 0xFF;
380 if( navMinorVers >= 14 )
382 return CallNPN_HasPropertyProc( gNetscapeFuncs.hasproperty, instance, npobj, propertyName);
387 bool NPN_HasMethod(NPP instance, NPObject *npobj, NPIdentifier methodName)
389 int navMinorVers = gNetscapeFuncs.version & 0xFF;
390 if( navMinorVers >= 14 )
392 return CallNPN_HasMethodProc( gNetscapeFuncs.hasmethod, instance, npobj, methodName);
397 void NPN_ReleaseVariantValue(NPVariant *variant)
399 int navMinorVers = gNetscapeFuncs.version & 0xFF;
400 if( navMinorVers >= 14 )
402 CallNPN_ReleaseVariantValueProc( gNetscapeFuncs.releasevariantvalue, variant);
406 void NPN_SetException(NPObject *npobj, const NPUTF8 *message)
408 int navMinorVers = gNetscapeFuncs.version & 0xFF;
409 if( navMinorVers >= 14 )
411 CallNPN_SetExceptionProc( gNetscapeFuncs.setexception, npobj, message);
416 /***********************************************************************
418 * Wrapper functions : Netscape Navigator -> plugin
420 * These functions let the plugin developer just create the APIs
421 * as documented and defined in npapi.h, without needing to
422 * install those functions in the function table or worry about
423 * setting up globals for 68K plugins.
425 ***********************************************************************/
428 Private_New(NPMIMEType pluginType, NPP instance, uint16 mode,
429 int16 argc, char* argn[], char* argv[], NPSavedData* saved)
432 PLUGINDEBUGSTR("New");
433 ret = NPP_New(pluginType, instance, mode, argc, argn, argv, saved);
438 Private_Destroy(NPP instance, NPSavedData** save)
440 PLUGINDEBUGSTR("Destroy");
441 return NPP_Destroy(instance, save);
445 Private_SetWindow(NPP instance, NPWindow* window)
448 PLUGINDEBUGSTR("SetWindow");
449 err = NPP_SetWindow(instance, window);
454 Private_NewStream(NPP instance, NPMIMEType type, NPStream* stream,
455 NPBool seekable, uint16* stype)
458 PLUGINDEBUGSTR("NewStream");
459 err = NPP_NewStream(instance, type, stream, seekable, stype);
464 Private_WriteReady(NPP instance, NPStream* stream)
467 PLUGINDEBUGSTR("WriteReady");
468 result = NPP_WriteReady(instance, stream);
473 Private_Write(NPP instance, NPStream* stream, int32 offset, int32 len,
477 PLUGINDEBUGSTR("Write");
478 result = NPP_Write(instance, stream, offset, len, buffer);
483 Private_StreamAsFile(NPP instance, NPStream* stream, const char* fname)
485 PLUGINDEBUGSTR("StreamAsFile");
486 NPP_StreamAsFile(instance, stream, fname);
491 Private_DestroyStream(NPP instance, NPStream* stream, NPError reason)
494 PLUGINDEBUGSTR("DestroyStream");
495 err = NPP_DestroyStream(instance, stream, reason);
500 Private_URLNotify(NPP instance, const char* url,
501 NPReason reason, void* notifyData)
504 PLUGINDEBUGSTR("URLNotify");
505 NPP_URLNotify(instance, url, reason, notifyData);
511 Private_Print(NPP instance, NPPrint* platformPrint)
513 PLUGINDEBUGSTR("Print");
514 NPP_Print(instance, platformPrint);
518 Private_GetValue(NPP instance, NPPVariable variable, void *r_value)
520 PLUGINDEBUGSTR("GetValue");
521 return NPP_GetValue(instance, variable, r_value);
525 Private_SetValue(NPP instance, NPPVariable variable, void *r_value)
527 PLUGINDEBUGSTR("SetValue");
528 return NPERR_NO_ERROR; //NPP_SetValue(instance, variable, r_value);
532 Private_GetJavaClass(void)
534 jref clazz = NPP_GetJavaClass();
536 JRIEnv* env = NPN_GetJavaEnv();
537 return JRI_NewGlobalRef(env, clazz);
542 /***********************************************************************
544 * These functions are located automagically by netscape.
546 ***********************************************************************/
549 * NP_GetMIMEDescription
550 * - Netscape needs to know about this symbol
551 * - Netscape uses the return value to identify when an object instance
552 * of this plugin should be created.
555 NP_GetMIMEDescription(void)
557 return NPP_GetMIMEDescription();
561 * NP_GetValue [optional]
562 * - Netscape needs to know about this symbol.
563 * - Interfaces with plugin to get values for predefined variables
564 * that the navigator needs.
567 NP_GetValue(void *future, NPPVariable variable, void *value)
569 return NPP_GetValue(future, variable, value);
574 * - Netscape needs to know about this symbol.
575 * - It calls this function after looking up its symbol before it
576 * is about to create the first ever object of this kind.
579 * nsTable - The netscape function table. If developers just use these
580 * wrappers, they dont need to worry about all these function
584 * - This functions needs to fill the plugin function table
585 * pluginFuncs and return it. Netscape Navigator plugin
586 * library will use this function table to call the plugin.
590 NP_Initialize(NPNetscapeFuncs* nsTable, NPPluginFuncs* pluginFuncs)
592 NPError err = NPERR_NO_ERROR;
594 PLUGINDEBUGSTR("NP_Initialize");
596 /* validate input parameters */
598 if ((nsTable == NULL) || (pluginFuncs == NULL))
599 err = NPERR_INVALID_FUNCTABLE_ERROR;
602 * Check the major version passed in Netscape's function table.
603 * We won't load if the major version is newer than what we expect.
604 * Also check that the function tables passed in are big enough for
605 * all the functions we need (they could be bigger, if Netscape added
606 * new APIs, but that's OK with us -- we'll just ignore them).
610 if (err == NPERR_NO_ERROR) {
611 if ((nsTable->version >> 8) > NP_VERSION_MAJOR)
612 err = NPERR_INCOMPATIBLE_VERSION_ERROR;
613 if (nsTable->size < sizeof(NPNetscapeFuncs))
614 err = NPERR_INVALID_FUNCTABLE_ERROR;
615 if (pluginFuncs->size < sizeof(NPPluginFuncs))
616 err = NPERR_INVALID_FUNCTABLE_ERROR;
619 if (err == NPERR_NO_ERROR) {
621 * Copy all the fields of Netscape function table into our
622 * copy so we can call back into Netscape later. Note that
623 * we need to copy the fields one by one, rather than assigning
624 * the whole structure, because the Netscape function table
625 * could actually be bigger than what we expect.
627 int navMinorVers = nsTable->version & 0xFF;
629 gNetscapeFuncs.version = nsTable->version;
630 gNetscapeFuncs.size = nsTable->size;
631 gNetscapeFuncs.posturl = nsTable->posturl;
632 gNetscapeFuncs.geturl = nsTable->geturl;
633 gNetscapeFuncs.requestread = nsTable->requestread;
634 gNetscapeFuncs.newstream = nsTable->newstream;
635 gNetscapeFuncs.write = nsTable->write;
636 gNetscapeFuncs.destroystream = nsTable->destroystream;
637 gNetscapeFuncs.status = nsTable->status;
638 gNetscapeFuncs.uagent = nsTable->uagent;
639 gNetscapeFuncs.memalloc = nsTable->memalloc;
640 gNetscapeFuncs.memfree = nsTable->memfree;
641 gNetscapeFuncs.memflush = nsTable->memflush;
642 gNetscapeFuncs.reloadplugins = nsTable->reloadplugins;
643 if( navMinorVers >= NPVERS_HAS_LIVECONNECT )
645 gNetscapeFuncs.getJavaEnv = nsTable->getJavaEnv;
646 gNetscapeFuncs.getJavaPeer = nsTable->getJavaPeer;
648 if( navMinorVers >= NPVERS_HAS_NOTIFICATION )
650 gNetscapeFuncs.geturlnotify = nsTable->geturlnotify;
651 gNetscapeFuncs.posturlnotify = nsTable->posturlnotify;
653 gNetscapeFuncs.getvalue = nsTable->getvalue;
654 gNetscapeFuncs.setvalue = nsTable->setvalue;
655 gNetscapeFuncs.invalidaterect = nsTable->invalidaterect;
656 gNetscapeFuncs.invalidateregion = nsTable->invalidateregion;
657 gNetscapeFuncs.forceredraw = nsTable->forceredraw;
658 if( navMinorVers >= 14 )
661 gNetscapeFuncs.getstringidentifier = nsTable->getstringidentifier;
662 gNetscapeFuncs.getstringidentifiers = nsTable->getstringidentifiers;
663 gNetscapeFuncs.getintidentifier = nsTable->getintidentifier;
664 gNetscapeFuncs.identifierisstring = nsTable->identifierisstring;
665 gNetscapeFuncs.utf8fromidentifier = nsTable->utf8fromidentifier;
666 gNetscapeFuncs.intfromidentifier = nsTable->intfromidentifier;
667 gNetscapeFuncs.createobject = nsTable->createobject;
668 gNetscapeFuncs.retainobject = nsTable->retainobject;
669 gNetscapeFuncs.releaseobject = nsTable->releaseobject;
670 gNetscapeFuncs.invoke = nsTable->invoke;
671 gNetscapeFuncs.invokeDefault = nsTable->invokeDefault;
672 gNetscapeFuncs.evaluate = nsTable->evaluate;
673 gNetscapeFuncs.getproperty = nsTable->getproperty;
674 gNetscapeFuncs.setproperty = nsTable->setproperty;
675 gNetscapeFuncs.removeproperty = nsTable->removeproperty;
676 gNetscapeFuncs.hasproperty = nsTable->hasproperty;
677 gNetscapeFuncs.hasmethod = nsTable->hasmethod;
678 gNetscapeFuncs.releasevariantvalue = nsTable->releasevariantvalue;
679 gNetscapeFuncs.setexception = nsTable->setexception;
683 * Set up the plugin function table that Netscape will use to
684 * call us. Netscape needs to know about our version and size
685 * and have a UniversalProcPointer for every function we
688 pluginFuncs->version = (NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR;
689 pluginFuncs->size = sizeof(NPPluginFuncs);
690 pluginFuncs->newp = NewNPP_NewProc(Private_New);
691 pluginFuncs->destroy = NewNPP_DestroyProc(Private_Destroy);
692 pluginFuncs->setwindow = NewNPP_SetWindowProc(Private_SetWindow);
693 pluginFuncs->newstream = NewNPP_NewStreamProc(Private_NewStream);
694 pluginFuncs->destroystream = NewNPP_DestroyStreamProc(Private_DestroyStream);
695 pluginFuncs->asfile = NewNPP_StreamAsFileProc(Private_StreamAsFile);
696 pluginFuncs->writeready = NewNPP_WriteReadyProc(Private_WriteReady);
697 pluginFuncs->write = NewNPP_WriteProc(Private_Write);
698 pluginFuncs->print = NewNPP_PrintProc(Private_Print);
699 pluginFuncs->event = NULL;
700 pluginFuncs->getvalue = NewNPP_GetValueProc(Private_GetValue);
701 if( navMinorVers >= NPVERS_HAS_NOTIFICATION )
703 pluginFuncs->urlnotify = NewNPP_URLNotifyProc(PLUGIN_TO_HOST_GLUE(urlnotify, Private_URLNotify));
706 if( navMinorVers >= NPVERS_HAS_LIVECONNECT )
708 pluginFuncs->javaClass = (JRIGlobalRef) Private_GetJavaClass();
711 pluginFuncs->javaClass = NULL;
714 err = NPP_Initialize();
721 * NP_Shutdown [optional]
722 * - Netscape needs to know about this symbol.
723 * - It calls this function after looking up its symbol after
724 * the last object of this kind has been destroyed.
730 PLUGINDEBUGSTR("NP_Shutdown");
732 return NPERR_NO_ERROR;