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)
61 #define PLUGIN_TO_HOST_GLUE(name, fp) (fp)
62 #define HOST_TO_PLUGIN_GLUE(name, fp) (fp)
64 /***********************************************************************
68 ***********************************************************************/
70 static NPNetscapeFuncs gNetscapeFuncs; /* Netscape Function table */
73 /***********************************************************************
75 * Wrapper functions : plugin calling Netscape Navigator
77 * These functions let the plugin developer just call the APIs
78 * as documented and defined in npapi.h, without needing to know
79 * about the function table and call macros in npupp.h.
81 ***********************************************************************/
84 NPN_Version(int* plugin_major, int* plugin_minor,
85 int* netscape_major, int* netscape_minor)
87 *plugin_major = NP_VERSION_MAJOR;
88 *plugin_minor = NP_VERSION_MINOR;
90 /* Major version is in high byte */
91 *netscape_major = gNetscapeFuncs.version >> 8;
92 /* Minor version is in low byte */
93 *netscape_minor = gNetscapeFuncs.version & 0xFF;
97 NPN_GetValue(NPP instance, NPNVariable variable, void *r_value)
99 return CallNPN_GetValueProc(gNetscapeFuncs.getvalue,
100 instance, variable, r_value);
104 NPN_SetValue(NPP instance, NPPVariable variable, void *value)
106 return CallNPN_SetValueProc(gNetscapeFuncs.setvalue,
107 instance, variable, value);
111 NPN_GetURL(NPP instance, const char* url, const char* window)
113 return CallNPN_GetURLProc(gNetscapeFuncs.geturl, instance, url, window);
117 NPN_GetURLNotify(NPP instance, const char* url, const char* window, void* notifyData)
119 return CallNPN_GetURLNotifyProc(gNetscapeFuncs.geturlnotify, instance, url, window, notifyData);
123 NPN_PostURL(NPP instance, const char* url, const char* window,
124 uint32 len, const char* buf, NPBool file)
126 return CallNPN_PostURLProc(gNetscapeFuncs.posturl, instance,
127 url, window, len, buf, file);
131 NPN_PostURLNotify(NPP instance, const char* url, const char* window, uint32 len,
132 const char* buf, NPBool file, void* notifyData)
134 return CallNPN_PostURLNotifyProc(gNetscapeFuncs.posturlnotify,
135 instance, url, window, len, buf, file, notifyData);
139 NPN_RequestRead(NPStream* stream, NPByteRange* rangeList)
141 return CallNPN_RequestReadProc(gNetscapeFuncs.requestread,
146 NPN_NewStream(NPP instance, NPMIMEType type, const char *window,
147 NPStream** stream_ptr)
149 return CallNPN_NewStreamProc(gNetscapeFuncs.newstream, instance,
150 type, window, stream_ptr);
154 NPN_Write(NPP instance, NPStream* stream, int32 len, void* buffer)
156 return CallNPN_WriteProc(gNetscapeFuncs.write, instance,
157 stream, len, buffer);
161 NPN_DestroyStream(NPP instance, NPStream* stream, NPError reason)
163 return CallNPN_DestroyStreamProc(gNetscapeFuncs.destroystream,
164 instance, stream, reason);
168 NPN_Status(NPP instance, const char* message)
170 CallNPN_StatusProc(gNetscapeFuncs.status, instance, message);
174 NPN_UserAgent(NPP instance)
176 return CallNPN_UserAgentProc(gNetscapeFuncs.uagent, instance);
180 NPN_MemAlloc(uint32 size)
182 return CallNPN_MemAllocProc(gNetscapeFuncs.memalloc, size);
185 void NPN_MemFree(void* ptr)
187 CallNPN_MemFreeProc(gNetscapeFuncs.memfree, ptr);
190 uint32 NPN_MemFlush(uint32 size)
192 return CallNPN_MemFlushProc(gNetscapeFuncs.memflush, size);
195 void NPN_ReloadPlugins(NPBool reloadPages)
197 CallNPN_ReloadPluginsProc(gNetscapeFuncs.reloadplugins, reloadPages);
200 JRIEnv* NPN_GetJavaEnv()
202 return CallNPN_GetJavaEnvProc(gNetscapeFuncs.getJavaEnv);
205 jref NPN_GetJavaPeer(NPP instance)
207 return CallNPN_GetJavaPeerProc(gNetscapeFuncs.getJavaPeer,
212 NPN_InvalidateRect(NPP instance, NPRect *invalidRect)
214 CallNPN_InvalidateRectProc(gNetscapeFuncs.invalidaterect, instance,
219 NPN_InvalidateRegion(NPP instance, NPRegion invalidRegion)
221 CallNPN_InvalidateRegionProc(gNetscapeFuncs.invalidateregion, instance,
226 NPN_ForceRedraw(NPP instance)
228 CallNPN_ForceRedrawProc(gNetscapeFuncs.forceredraw, instance);
231 NPIdentifier NPN_GetStringIdentifier(const NPUTF8 *name)
233 int navMinorVers = gNetscapeFuncs.version & 0xFF;
234 if( navMinorVers >= 14 )
236 return CallNPN_GetStringIdentifierProc( gNetscapeFuncs.getstringidentifier, name);
241 void NPN_GetStringIdentifiers(const NPUTF8 **names, int32_t nameCount, NPIdentifier *identifiers)
243 int navMinorVers = gNetscapeFuncs.version & 0xFF;
244 if( navMinorVers >= 14 )
246 CallNPN_GetStringIdentifiersProc( gNetscapeFuncs.getstringidentifiers, names, nameCount, identifiers);
250 NPIdentifier NPN_GetIntIdentifier(int32_t intid)
252 int navMinorVers = gNetscapeFuncs.version & 0xFF;
253 if( navMinorVers >= 14 )
255 return CallNPN_GetIntIdentifierProc( gNetscapeFuncs.getintidentifier, intid);
260 bool NPN_IdentifierIsString(NPIdentifier identifier)
262 int navMinorVers = gNetscapeFuncs.version & 0xFF;
263 if( navMinorVers >= 14 )
265 return CallNPN_IdentifierIsStringProc( gNetscapeFuncs.identifierisstring, identifier);
270 NPUTF8 *NPN_UTF8FromIdentifier(NPIdentifier identifier)
272 int navMinorVers = gNetscapeFuncs.version & 0xFF;
273 if( navMinorVers >= 14 )
275 return CallNPN_UTF8FromIdentifierProc( gNetscapeFuncs.utf8fromidentifier, identifier);
280 int32_t NPN_IntFromIdentifier(NPIdentifier identifier)
282 int navMinorVers = gNetscapeFuncs.version & 0xFF;
283 if( navMinorVers >= 14 )
285 return CallNPN_IntFromIdentifierProc( gNetscapeFuncs.intfromidentifier, identifier);
290 NPObject *NPN_CreateObject(NPP instance, NPClass *aClass)
292 int navMinorVers = gNetscapeFuncs.version & 0xFF;
293 if( navMinorVers >= 14 )
295 return CallNPN_CreateObjectProc( gNetscapeFuncs.createobject, instance, aClass);
300 NPObject *NPN_RetainObject(NPObject *npobj)
302 int navMinorVers = gNetscapeFuncs.version & 0xFF;
303 if( navMinorVers >= 14 )
305 return CallNPN_RetainObjectProc( gNetscapeFuncs.retainobject, npobj);
310 void NPN_ReleaseObject(NPObject *npobj)
312 int navMinorVers = gNetscapeFuncs.version & 0xFF;
313 if( navMinorVers >= 14 )
315 CallNPN_ReleaseObjectProc( gNetscapeFuncs.releaseobject, npobj);
319 bool NPN_Invoke(NPP instance, NPObject *npobj, NPIdentifier methodName, const NPVariant *args, uint32_t argCount, NPVariant *result)
321 int navMinorVers = gNetscapeFuncs.version & 0xFF;
322 if( navMinorVers >= 14 )
324 return CallNPN_InvokeProc( gNetscapeFuncs.invoke, instance, npobj, methodName, args, argCount, result);
329 bool NPN_InvokeDefault(NPP instance, NPObject *npobj, const NPVariant *args, uint32_t argCount, NPVariant *result)
331 int navMinorVers = gNetscapeFuncs.version & 0xFF;
332 if( navMinorVers >= 14 )
334 return CallNPN_InvokeDefaultProc( gNetscapeFuncs.invokeDefault, instance, npobj, args, argCount, result);
339 bool NPN_Evaluate(NPP instance, NPObject *npobj, NPString *script, NPVariant *result)
341 int navMinorVers = gNetscapeFuncs.version & 0xFF;
342 if( navMinorVers >= 14 )
344 return CallNPN_EvaluateProc( gNetscapeFuncs.evaluate, instance, npobj, script, result);
349 bool NPN_GetProperty(NPP instance, NPObject *npobj, NPIdentifier propertyName, NPVariant *result)
351 int navMinorVers = gNetscapeFuncs.version & 0xFF;
352 if( navMinorVers >= 14 )
354 return CallNPN_GetPropertyProc( gNetscapeFuncs.getproperty, instance, npobj, propertyName, result);
359 bool NPN_SetProperty(NPP instance, NPObject *npobj, NPIdentifier propertyName, const NPVariant *value)
361 int navMinorVers = gNetscapeFuncs.version & 0xFF;
362 if( navMinorVers >= 14 )
364 return CallNPN_SetPropertyProc( gNetscapeFuncs.setproperty, instance, npobj, propertyName, value);
369 bool NPN_RemoveProperty(NPP instance, NPObject *npobj, NPIdentifier propertyName)
371 int navMinorVers = gNetscapeFuncs.version & 0xFF;
372 if( navMinorVers >= 14 )
374 return CallNPN_RemovePropertyProc( gNetscapeFuncs.removeproperty, instance, npobj, propertyName);
379 bool NPN_HasProperty(NPP instance, NPObject *npobj, NPIdentifier propertyName)
381 int navMinorVers = gNetscapeFuncs.version & 0xFF;
382 if( navMinorVers >= 14 )
384 return CallNPN_HasPropertyProc( gNetscapeFuncs.hasproperty, instance, npobj, propertyName);
389 bool NPN_HasMethod(NPP instance, NPObject *npobj, NPIdentifier methodName)
391 int navMinorVers = gNetscapeFuncs.version & 0xFF;
392 if( navMinorVers >= 14 )
394 return CallNPN_HasMethodProc( gNetscapeFuncs.hasmethod, instance, npobj, methodName);
399 void NPN_ReleaseVariantValue(NPVariant *variant)
401 int navMinorVers = gNetscapeFuncs.version & 0xFF;
402 if( navMinorVers >= 14 )
404 CallNPN_ReleaseVariantValueProc( gNetscapeFuncs.releasevariantvalue, variant);
408 void NPN_SetException(NPObject *npobj, const NPUTF8 *message)
410 int navMinorVers = gNetscapeFuncs.version & 0xFF;
411 if( navMinorVers >= 14 )
413 CallNPN_SetExceptionProc( gNetscapeFuncs.setexception, npobj, message);
418 /***********************************************************************
420 * Wrapper functions : Netscape Navigator -> plugin
422 * These functions let the plugin developer just create the APIs
423 * as documented and defined in npapi.h, without needing to
424 * install those functions in the function table or worry about
425 * setting up globals for 68K plugins.
427 ***********************************************************************/
430 Private_New(NPMIMEType pluginType, NPP instance, uint16 mode,
431 int16 argc, char* argn[], char* argv[], NPSavedData* saved)
434 PLUGINDEBUGSTR("New");
435 ret = NPP_New(pluginType, instance, mode, argc, argn, argv, saved);
440 Private_Destroy(NPP instance, NPSavedData** save)
442 PLUGINDEBUGSTR("Destroy");
443 return NPP_Destroy(instance, save);
447 Private_SetWindow(NPP instance, NPWindow* window)
450 PLUGINDEBUGSTR("SetWindow");
451 err = NPP_SetWindow(instance, window);
456 Private_NewStream(NPP instance, NPMIMEType type, NPStream* stream,
457 NPBool seekable, uint16* stype)
460 PLUGINDEBUGSTR("NewStream");
461 err = NPP_NewStream(instance, type, stream, seekable, stype);
466 Private_WriteReady(NPP instance, NPStream* stream)
469 PLUGINDEBUGSTR("WriteReady");
470 result = NPP_WriteReady(instance, stream);
475 Private_Write(NPP instance, NPStream* stream, int32 offset, int32 len,
479 PLUGINDEBUGSTR("Write");
480 result = NPP_Write(instance, stream, offset, len, buffer);
485 Private_StreamAsFile(NPP instance, NPStream* stream, const char* fname)
487 PLUGINDEBUGSTR("StreamAsFile");
488 NPP_StreamAsFile(instance, stream, fname);
493 Private_DestroyStream(NPP instance, NPStream* stream, NPError reason)
496 PLUGINDEBUGSTR("DestroyStream");
497 err = NPP_DestroyStream(instance, stream, reason);
502 Private_URLNotify(NPP instance, const char* url,
503 NPReason reason, void* notifyData)
506 PLUGINDEBUGSTR("URLNotify");
507 NPP_URLNotify(instance, url, reason, notifyData);
513 Private_Print(NPP instance, NPPrint* platformPrint)
515 PLUGINDEBUGSTR("Print");
516 NPP_Print(instance, platformPrint);
520 Private_GetValue(NPP instance, NPPVariable variable, void *r_value)
522 PLUGINDEBUGSTR("GetValue");
523 return NPP_GetValue(instance, variable, r_value);
527 Private_SetValue(NPP instance, NPPVariable variable, void *r_value)
529 PLUGINDEBUGSTR("SetValue");
530 return NPERR_NO_ERROR; //NPP_SetValue(instance, variable, r_value);
534 Private_GetJavaClass(void)
536 jref clazz = NPP_GetJavaClass();
538 JRIEnv* env = NPN_GetJavaEnv();
539 return JRI_NewGlobalRef(env, clazz);
544 /***********************************************************************
546 * These functions are located automagically by netscape.
548 ***********************************************************************/
551 * NP_GetMIMEDescription
552 * - Netscape needs to know about this symbol
553 * - Netscape uses the return value to identify when an object instance
554 * of this plugin should be created.
557 NP_GetMIMEDescription(void)
559 return NPP_GetMIMEDescription();
563 * NP_GetValue [optional]
564 * - Netscape needs to know about this symbol.
565 * - Interfaces with plugin to get values for predefined variables
566 * that the navigator needs.
569 NP_GetValue(void *future, NPPVariable variable, void *value)
571 return NPP_GetValue(future, variable, value);
576 * - Netscape needs to know about this symbol.
577 * - It calls this function after looking up its symbol before it
578 * is about to create the first ever object of this kind.
581 * nsTable - The netscape function table. If developers just use these
582 * wrappers, they dont need to worry about all these function
586 * - This functions needs to fill the plugin function table
587 * pluginFuncs and return it. Netscape Navigator plugin
588 * library will use this function table to call the plugin.
592 NP_Initialize(NPNetscapeFuncs* nsTable, NPPluginFuncs* pluginFuncs)
594 NPError err = NPERR_NO_ERROR;
596 PLUGINDEBUGSTR("NP_Initialize");
598 /* validate input parameters */
600 if ((nsTable == NULL) || (pluginFuncs == NULL))
601 err = NPERR_INVALID_FUNCTABLE_ERROR;
604 * Check the major version passed in Netscape's function table.
605 * We won't load if the major version is newer than what we expect.
606 * Also check that the function tables passed in are big enough for
607 * all the functions we need (they could be bigger, if Netscape added
608 * new APIs, but that's OK with us -- we'll just ignore them).
612 if (err == NPERR_NO_ERROR) {
613 if ((nsTable->version >> 8) > NP_VERSION_MAJOR)
614 err = NPERR_INCOMPATIBLE_VERSION_ERROR;
615 if (nsTable->size < sizeof(NPNetscapeFuncs))
616 err = NPERR_INVALID_FUNCTABLE_ERROR;
617 if (pluginFuncs->size < sizeof(NPPluginFuncs))
618 err = NPERR_INVALID_FUNCTABLE_ERROR;
621 if (err == NPERR_NO_ERROR) {
623 * Copy all the fields of Netscape function table into our
624 * copy so we can call back into Netscape later. Note that
625 * we need to copy the fields one by one, rather than assigning
626 * the whole structure, because the Netscape function table
627 * could actually be bigger than what we expect.
629 int navMinorVers = nsTable->version & 0xFF;
631 gNetscapeFuncs.version = nsTable->version;
632 gNetscapeFuncs.size = nsTable->size;
633 gNetscapeFuncs.posturl = nsTable->posturl;
634 gNetscapeFuncs.geturl = nsTable->geturl;
635 gNetscapeFuncs.requestread = nsTable->requestread;
636 gNetscapeFuncs.newstream = nsTable->newstream;
637 gNetscapeFuncs.write = nsTable->write;
638 gNetscapeFuncs.destroystream = nsTable->destroystream;
639 gNetscapeFuncs.status = nsTable->status;
640 gNetscapeFuncs.uagent = nsTable->uagent;
641 gNetscapeFuncs.memalloc = nsTable->memalloc;
642 gNetscapeFuncs.memfree = nsTable->memfree;
643 gNetscapeFuncs.memflush = nsTable->memflush;
644 gNetscapeFuncs.reloadplugins = nsTable->reloadplugins;
645 if( navMinorVers >= NPVERS_HAS_LIVECONNECT )
647 gNetscapeFuncs.getJavaEnv = nsTable->getJavaEnv;
648 gNetscapeFuncs.getJavaPeer = nsTable->getJavaPeer;
650 if( navMinorVers >= NPVERS_HAS_NOTIFICATION )
652 gNetscapeFuncs.geturlnotify = nsTable->geturlnotify;
653 gNetscapeFuncs.posturlnotify = nsTable->posturlnotify;
655 gNetscapeFuncs.getvalue = nsTable->getvalue;
656 gNetscapeFuncs.setvalue = nsTable->setvalue;
657 gNetscapeFuncs.invalidaterect = nsTable->invalidaterect;
658 gNetscapeFuncs.invalidateregion = nsTable->invalidateregion;
659 gNetscapeFuncs.forceredraw = nsTable->forceredraw;
660 if( navMinorVers >= 14 )
663 gNetscapeFuncs.getstringidentifier = nsTable->getstringidentifier;
664 gNetscapeFuncs.getstringidentifiers = nsTable->getstringidentifiers;
665 gNetscapeFuncs.getintidentifier = nsTable->getintidentifier;
666 gNetscapeFuncs.identifierisstring = nsTable->identifierisstring;
667 gNetscapeFuncs.utf8fromidentifier = nsTable->utf8fromidentifier;
668 gNetscapeFuncs.intfromidentifier = nsTable->intfromidentifier;
669 gNetscapeFuncs.createobject = nsTable->createobject;
670 gNetscapeFuncs.retainobject = nsTable->retainobject;
671 gNetscapeFuncs.releaseobject = nsTable->releaseobject;
672 gNetscapeFuncs.invoke = nsTable->invoke;
673 gNetscapeFuncs.invokeDefault = nsTable->invokeDefault;
674 gNetscapeFuncs.evaluate = nsTable->evaluate;
675 gNetscapeFuncs.getproperty = nsTable->getproperty;
676 gNetscapeFuncs.setproperty = nsTable->setproperty;
677 gNetscapeFuncs.removeproperty = nsTable->removeproperty;
678 gNetscapeFuncs.hasproperty = nsTable->hasproperty;
679 gNetscapeFuncs.hasmethod = nsTable->hasmethod;
680 gNetscapeFuncs.releasevariantvalue = nsTable->releasevariantvalue;
681 gNetscapeFuncs.setexception = nsTable->setexception;
685 * Set up the plugin function table that Netscape will use to
686 * call us. Netscape needs to know about our version and size
687 * and have a UniversalProcPointer for every function we
690 pluginFuncs->version = (NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR;
691 pluginFuncs->size = sizeof(NPPluginFuncs);
692 pluginFuncs->newp = NewNPP_NewProc(Private_New);
693 pluginFuncs->destroy = NewNPP_DestroyProc(Private_Destroy);
694 pluginFuncs->setwindow = NewNPP_SetWindowProc(Private_SetWindow);
695 pluginFuncs->newstream = NewNPP_NewStreamProc(Private_NewStream);
696 pluginFuncs->destroystream = NewNPP_DestroyStreamProc(Private_DestroyStream);
697 pluginFuncs->asfile = NewNPP_StreamAsFileProc(Private_StreamAsFile);
698 pluginFuncs->writeready = NewNPP_WriteReadyProc(Private_WriteReady);
699 pluginFuncs->write = NewNPP_WriteProc(Private_Write);
700 pluginFuncs->print = NewNPP_PrintProc(Private_Print);
701 pluginFuncs->event = NULL;
702 pluginFuncs->getvalue = NewNPP_GetValueProc(Private_GetValue);
703 if( navMinorVers >= NPVERS_HAS_NOTIFICATION )
705 pluginFuncs->urlnotify = NewNPP_URLNotifyProc(PLUGIN_TO_HOST_GLUE(urlnotify, Private_URLNotify));
708 if( navMinorVers >= NPVERS_HAS_LIVECONNECT )
710 pluginFuncs->javaClass = (JRIGlobalRef) Private_GetJavaClass();
713 pluginFuncs->javaClass = NULL;
716 err = NPP_Initialize();
723 * NP_Shutdown [optional]
724 * - Netscape needs to know about this symbol.
725 * - It calls this function after looking up its symbol after
726 * the last object of this kind has been destroyed.
732 PLUGINDEBUGSTR("NP_Shutdown");
734 return NPERR_NO_ERROR;