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 *----------------------------------------------------------------------
54 #include <sal/types.h> // just for SAL_DLLPUBLIC_EXPORT
64 * Define PLUGIN_TRACE to have the wrapper functions print
65 * messages to stderr whenever they are called.
70 #define PLUGINDEBUGSTR(msg) fprintf(stderr, "%s\n", msg)
72 #define PLUGINDEBUGSTR(msg)
76 /***********************************************************************
80 ***********************************************************************/
82 static NPNetscapeFuncs gNetscapeFuncs
; /* Netscape Function table */
85 /***********************************************************************
87 * Wrapper functions : plugin calling Netscape Navigator
89 * These functions let the plugin developer just call the APIs
90 * as documented and defined in npapi.h, without needing to know
91 * about the function table and call macros in npupp.h.
93 ***********************************************************************/
96 NPN_Version(int* plugin_major
, int* plugin_minor
,
97 int* netscape_major
, int* netscape_minor
)
99 *plugin_major
= NP_VERSION_MAJOR
;
100 *plugin_minor
= NP_VERSION_MINOR
;
102 /* Major version is in high byte */
103 *netscape_major
= gNetscapeFuncs
.version
>> 8;
104 /* Minor version is in low byte */
105 *netscape_minor
= gNetscapeFuncs
.version
& 0xFF;
109 NPN_GetValue(NPP instance
, NPNVariable variable
, void *r_value
)
111 return CallNPN_GetValueProc(gNetscapeFuncs
.getvalue
,
112 instance
, variable
, r_value
);
116 NPN_SetValue(NPP instance
, NPPVariable variable
, void *value
)
118 return CallNPN_SetValueProc(gNetscapeFuncs
.setvalue
,
119 instance
, variable
, value
);
123 NPN_GetURL(NPP instance
, const char* url
, const char* window
)
125 return CallNPN_GetURLProc(gNetscapeFuncs
.geturl
, instance
, url
, window
);
129 NPN_GetURLNotify(NPP instance
, const char* url
, const char* window
, void* notifyData
)
131 return CallNPN_GetURLNotifyProc(gNetscapeFuncs
.geturlnotify
, instance
, url
, window
, notifyData
);
135 NPN_PostURL(NPP instance
, const char* url
, const char* window
,
136 uint32_t len
, const char* buf
, NPBool file
)
138 return CallNPN_PostURLProc(gNetscapeFuncs
.posturl
, instance
,
139 url
, window
, len
, buf
, file
);
143 NPN_PostURLNotify(NPP instance
, const char* url
, const char* window
, uint32_t len
,
144 const char* buf
, NPBool file
, void* notifyData
)
146 return CallNPN_PostURLNotifyProc(gNetscapeFuncs
.posturlnotify
,
147 instance
, url
, window
, len
, buf
, file
, notifyData
);
151 NPN_RequestRead(NPStream
* stream
, NPByteRange
* rangeList
)
153 return CallNPN_RequestReadProc(gNetscapeFuncs
.requestread
,
158 NPN_NewStream(NPP instance
, NPMIMEType type
, const char *window
,
159 NPStream
** stream_ptr
)
161 return CallNPN_NewStreamProc(gNetscapeFuncs
.newstream
, instance
,
162 type
, window
, stream_ptr
);
166 NPN_Write(NPP instance
, NPStream
* stream
, int32_t len
, void* buffer
)
168 return CallNPN_WriteProc(gNetscapeFuncs
.write
, instance
,
169 stream
, len
, buffer
);
173 NPN_DestroyStream(NPP instance
, NPStream
* stream
, NPError reason
)
175 return CallNPN_DestroyStreamProc(gNetscapeFuncs
.destroystream
,
176 instance
, stream
, reason
);
180 NPN_Status(NPP instance
, const char* message
)
182 CallNPN_StatusProc(gNetscapeFuncs
.status
, instance
, message
);
186 NPN_UserAgent(NPP instance
)
188 return CallNPN_UserAgentProc(gNetscapeFuncs
.uagent
, instance
);
192 NPN_MemAlloc(uint32_t size
)
194 return CallNPN_MemAllocProc(gNetscapeFuncs
.memalloc
, size
);
197 void NPN_MemFree(void* ptr
)
199 CallNPN_MemFreeProc(gNetscapeFuncs
.memfree
, ptr
);
202 uint32_t NPN_MemFlush(uint32_t size
)
204 return CallNPN_MemFlushProc(gNetscapeFuncs
.memflush
, size
);
207 void NPN_ReloadPlugins(NPBool reloadPages
)
209 CallNPN_ReloadPluginsProc(gNetscapeFuncs
.reloadplugins
, reloadPages
);
213 JRIEnv
* NPN_GetJavaEnv()
215 return CallNPN_GetJavaEnvProc(gNetscapeFuncs
.getJavaEnv
);
218 jref
NPN_GetJavaPeer(NPP instance
)
220 return CallNPN_GetJavaPeerProc(gNetscapeFuncs
.getJavaPeer
,
226 NPN_InvalidateRect(NPP instance
, NPRect
*invalidRect
)
228 CallNPN_InvalidateRectProc(gNetscapeFuncs
.invalidaterect
, instance
,
233 NPN_InvalidateRegion(NPP instance
, NPRegion invalidRegion
)
235 CallNPN_InvalidateRegionProc(gNetscapeFuncs
.invalidateregion
, instance
,
240 NPN_ForceRedraw(NPP instance
)
242 CallNPN_ForceRedrawProc(gNetscapeFuncs
.forceredraw
, instance
);
245 /***********************************************************************
247 * Wrapper functions : Netscape Navigator -> plugin
249 * These functions let the plugin developer just create the APIs
250 * as documented and defined in npapi.h, without needing to
251 * install those functions in the function table or worry about
252 * setting up globals for 68K plugins.
254 ***********************************************************************/
257 Private_New(NPMIMEType pluginType
, NPP instance
, uint16_t mode
,
258 int16_t argc
, char* argn
[], char* argv
[], NPSavedData
* saved
)
261 PLUGINDEBUGSTR("New");
262 ret
= NPP_New(pluginType
, instance
, mode
, argc
, argn
, argv
, saved
);
267 Private_Destroy(NPP instance
, NPSavedData
** save
)
269 PLUGINDEBUGSTR("Destroy");
270 return NPP_Destroy(instance
, save
);
274 Private_SetWindow(NPP instance
, NPWindow
* window
)
277 PLUGINDEBUGSTR("SetWindow");
278 err
= NPP_SetWindow(instance
, window
);
283 Private_NewStream(NPP instance
, NPMIMEType type
, NPStream
* stream
,
284 NPBool seekable
, uint16_t* stype
)
287 PLUGINDEBUGSTR("NewStream");
288 err
= NPP_NewStream(instance
, type
, stream
, seekable
, stype
);
293 Private_WriteReady(NPP instance
, NPStream
* stream
)
296 PLUGINDEBUGSTR("WriteReady");
297 result
= NPP_WriteReady(instance
, stream
);
302 Private_Write(NPP instance
, NPStream
* stream
, int32_t offset
, int32_t len
,
306 PLUGINDEBUGSTR("Write");
307 result
= NPP_Write(instance
, stream
, offset
, len
, buffer
);
312 Private_StreamAsFile(NPP instance
, NPStream
* stream
, const char* fname
)
314 PLUGINDEBUGSTR("StreamAsFile");
315 NPP_StreamAsFile(instance
, stream
, fname
);
320 Private_DestroyStream(NPP instance
, NPStream
* stream
, NPError reason
)
323 PLUGINDEBUGSTR("DestroyStream");
324 err
= NPP_DestroyStream(instance
, stream
, reason
);
329 Private_URLNotify(NPP instance
, const char* url
,
330 NPReason reason
, void* notifyData
)
333 PLUGINDEBUGSTR("URLNotify");
334 NPP_URLNotify(instance
, url
, reason
, notifyData
);
340 Private_Print(NPP instance
, NPPrint
* platformPrint
)
342 PLUGINDEBUGSTR("Print");
343 NPP_Print(instance
, platformPrint
);
348 Private_GetJavaClass(void)
350 jref clazz
= NPP_GetJavaClass();
352 JRIEnv
* env
= NPN_GetJavaEnv();
353 return JRI_NewGlobalRef(env
, clazz
);
359 /***********************************************************************
361 * These functions are located automagically by netscape.
363 ***********************************************************************/
366 * NP_GetMIMEDescription
367 * - Netscape needs to know about this symbol
368 * - Netscape uses the return value to identify when an object instance
369 * of this plugin should be created.
371 SAL_DLLPUBLIC_EXPORT
char *
372 NP_GetMIMEDescription(void)
374 return (char *)NPP_GetMIMEDescription();
378 * NP_GetValue [optional]
379 * - Netscape needs to know about this symbol.
380 * - Interfaces with plugin to get values for predefined variables
381 * that the navigator needs.
383 SAL_DLLPUBLIC_EXPORT NPError
384 NP_GetValue(void* future
, NPPVariable variable
, void *value
)
386 return NPP_GetValue(future
, variable
, value
);
391 * - Netscape needs to know about this symbol.
392 * - It calls this function after looking up its symbol before it
393 * is about to create the first ever object of this kind.
396 * nsTable - The netscape function table. If developers just use these
397 * wrappers, they dont need to worry about all these function
401 * - This functions needs to fill the plugin function table
402 * pluginFuncs and return it. Netscape Navigator plugin
403 * library will use this function table to call the plugin.
406 SAL_DLLPUBLIC_EXPORT NPError
407 NP_Initialize(NPNetscapeFuncs
* nsTable
, NPPluginFuncs
* pluginFuncs
)
409 NPError err
= NPERR_NO_ERROR
;
411 PLUGINDEBUGSTR("NP_Initialize");
413 /* validate input parameters */
415 if ((nsTable
== NULL
) || (pluginFuncs
== NULL
))
416 err
= NPERR_INVALID_FUNCTABLE_ERROR
;
419 * Check the major version passed in Netscape's function table.
420 * We won't load if the major version is newer than what we expect.
421 * Also check that the function tables passed in are big enough for
422 * all the functions we need (they could be bigger, if Netscape added
423 * new APIs, but that's OK with us -- we'll just ignore them).
427 if (err
== NPERR_NO_ERROR
) {
428 if ((nsTable
->version
>> 8) > NP_VERSION_MAJOR
)
429 err
= NPERR_INCOMPATIBLE_VERSION_ERROR
;
430 if (nsTable
->size
< sizeof(NPNetscapeFuncs
))
431 err
= NPERR_INVALID_FUNCTABLE_ERROR
;
432 if (pluginFuncs
->size
< sizeof(NPPluginFuncs
))
433 err
= NPERR_INVALID_FUNCTABLE_ERROR
;
437 if (err
== NPERR_NO_ERROR
) {
439 * Copy all the fields of Netscape function table into our
440 * copy so we can call back into Netscape later. Note that
441 * we need to copy the fields one by one, rather than assigning
442 * the whole structure, because the Netscape function table
443 * could actually be bigger than what we expect.
445 gNetscapeFuncs
.version
= nsTable
->version
;
446 gNetscapeFuncs
.size
= nsTable
->size
;
447 gNetscapeFuncs
.posturl
= nsTable
->posturl
;
448 gNetscapeFuncs
.geturl
= nsTable
->geturl
;
449 gNetscapeFuncs
.geturlnotify
= nsTable
->geturlnotify
;
450 gNetscapeFuncs
.requestread
= nsTable
->requestread
;
451 gNetscapeFuncs
.newstream
= nsTable
->newstream
;
452 gNetscapeFuncs
.write
= nsTable
->write
;
453 gNetscapeFuncs
.destroystream
= nsTable
->destroystream
;
454 gNetscapeFuncs
.status
= nsTable
->status
;
455 gNetscapeFuncs
.uagent
= nsTable
->uagent
;
456 gNetscapeFuncs
.memalloc
= nsTable
->memalloc
;
457 gNetscapeFuncs
.memfree
= nsTable
->memfree
;
458 gNetscapeFuncs
.memflush
= nsTable
->memflush
;
459 gNetscapeFuncs
.reloadplugins
= nsTable
->reloadplugins
;
461 gNetscapeFuncs
.getJavaEnv
= nsTable
->getJavaEnv
;
462 gNetscapeFuncs
.getJavaPeer
= nsTable
->getJavaPeer
;
464 gNetscapeFuncs
.getvalue
= nsTable
->getvalue
;
467 * Set up the plugin function table that Netscape will use to
468 * call us. Netscape needs to know about our version and size
469 * and have a UniversalProcPointer for every function we
472 pluginFuncs
->version
= (NP_VERSION_MAJOR
<< 8) + NP_VERSION_MINOR
;
473 pluginFuncs
->size
= sizeof(NPPluginFuncs
);
474 pluginFuncs
->newp
= NewNPP_NewProc(Private_New
);
475 pluginFuncs
->destroy
= NewNPP_DestroyProc(Private_Destroy
);
476 pluginFuncs
->setwindow
= NewNPP_SetWindowProc(Private_SetWindow
);
477 pluginFuncs
->newstream
= NewNPP_NewStreamProc(Private_NewStream
);
478 pluginFuncs
->destroystream
= NewNPP_DestroyStreamProc(Private_DestroyStream
);
479 pluginFuncs
->asfile
= NewNPP_StreamAsFileProc(Private_StreamAsFile
);
480 pluginFuncs
->writeready
= NewNPP_WriteReadyProc(Private_WriteReady
);
481 pluginFuncs
->write
= NewNPP_WriteProc(Private_Write
);
482 pluginFuncs
->print
= NewNPP_PrintProc(Private_Print
);
483 pluginFuncs
->urlnotify
= NewNPP_URLNotifyProc(Private_URLNotify
);
484 pluginFuncs
->event
= NULL
;
486 pluginFuncs
->javaClass
= Private_GetJavaClass();
489 err
= NPP_Initialize();
496 * NP_Shutdown [optional]
497 * - Netscape needs to know about this symbol.
498 * - It calls this function after looking up its symbol after
499 * the last object of this kind has been destroyed.
502 SAL_DLLPUBLIC_EXPORT
void
505 PLUGINDEBUGSTR("NP_Shutdown");