1 /*
2  * npunix.c
3  *
4  * Netscape Client Plugin API
5  * - Wrapper function to interface with the Netscape Navigator
6  *
7  * dp Suresh <dp@netscape.com>
8  *
9  *----------------------------------------------------------------------
10  * PLUGIN DEVELOPERS:
11  *	YOU WILL NOT NEED TO EDIT THIS FILE.
12  *----------------------------------------------------------------------
13  *
14  * At the official Netscape site there's only the PluginSDK30b5 for unix:
15  *
16  * !!! I had to add the missing URLNotify stuff here. If you find an official
17  * !!! version of this file which has it, use it instead.
18  * !!!        April 2000, Bert Freudenberg
19  *
20  */
21 
22 #define XP_UNIX 1
23 
24 #include <stdio.h>
25 #include "npapi.h"
26 #include "npupp.h"
27 
28 /*
29  * Define PLUGIN_TRACE to have the wrapper functions print
30  * messages to stderr whenever they are called.
31  */
32 
33 #ifdef PLUGIN_TRACE
34 #include <stdio.h>
35 #define PLUGINDEBUGSTR(msg)	fprintf(stderr, "%s\n", msg)
36 #else
37 #define PLUGINDEBUGSTR(msg)
38 #endif
39 
40 
41 /***********************************************************************
42  *
43  * Globals
44  *
45  ***********************************************************************/
46 
47 static NPNetscapeFuncs   gNetscapeFuncs;	/* Netscape Function table */
48 
49 
50 /***********************************************************************
51  *
52  * Wrapper functions : plugin calling Netscape Navigator
53  *
54  * These functions let the plugin developer just call the APIs
55  * as documented and defined in npapi.h, without needing to know
56  * about the function table and call macros in npupp.h.
57  *
58  ***********************************************************************/
59 
60 void
NPN_Version(int * plugin_major,int * plugin_minor,int * netscape_major,int * netscape_minor)61 NPN_Version(int* plugin_major, int* plugin_minor,
62 	     int* netscape_major, int* netscape_minor)
63 {
64 	*plugin_major = NP_VERSION_MAJOR;
65 	*plugin_minor = NP_VERSION_MINOR;
66 
67 	/* Major version is in high byte */
68 	*netscape_major = gNetscapeFuncs.version >> 8;
69 	/* Minor version is in low byte */
70 	*netscape_minor = gNetscapeFuncs.version & 0xFF;
71 }
72 
73 NPError
NPN_GetValue(NPP instance,NPNVariable variable,void * r_value)74 NPN_GetValue(NPP instance, NPNVariable variable, void *r_value)
75 {
76 	return CallNPN_GetValueProc(gNetscapeFuncs.getvalue,
77 					instance, variable, r_value);
78 }
79 
80 NPError
NPN_GetURL(NPP instance,const char * url,const char * window)81 NPN_GetURL(NPP instance, const char* url, const char* window)
82 {
83 	return CallNPN_GetURLProc(gNetscapeFuncs.geturl, instance, url, window);
84 }
85 
86 NPError
NPN_GetURLNotify(NPP instance,const char * url,const char * window,void * notifyData)87 NPN_GetURLNotify(NPP instance, const char* url, const char* window, void* notifyData)
88 {
89 	int navMinorVers = gNetscapeFuncs.version & 0xFF;
90 	if (navMinorVers < NPVERS_HAS_NOTIFICATION)
91 	  return NPERR_INCOMPATIBLE_VERSION_ERROR;
92 	return CallNPN_GetURLNotifyProc(gNetscapeFuncs.geturlnotify, instance, url, window, notifyData);
93 }
94 
95 NPError
NPN_PostURL(NPP instance,const char * url,const char * window,uint32 len,const char * buf,NPBool file)96 NPN_PostURL(NPP instance, const char* url, const char* window,
97 	     uint32 len, const char* buf, NPBool file)
98 {
99 	return CallNPN_PostURLProc(gNetscapeFuncs.posturl, instance,
100 					url, window, len, buf, file);
101 }
102 
103 NPError
NPN_PostURLNotify(NPP instance,const char * url,const char * window,uint32 len,const char * buf,NPBool file,void * notifyData)104 NPN_PostURLNotify(NPP instance, const char* url, const char* window,
105 	     uint32 len, const char* buf, NPBool file, void* notifyData)
106 {
107 	int navMinorVers = gNetscapeFuncs.version & 0xFF;
108 	if (navMinorVers < NPVERS_HAS_NOTIFICATION)
109 	  return NPERR_INCOMPATIBLE_VERSION_ERROR;
110 	return CallNPN_PostURLNotifyProc(gNetscapeFuncs.posturlnotify, instance,
111 					 url, window, len, buf, file, notifyData);
112 }
113 
114 NPError
NPN_RequestRead(NPStream * stream,NPByteRange * rangeList)115 NPN_RequestRead(NPStream* stream, NPByteRange* rangeList)
116 {
117 	return CallNPN_RequestReadProc(gNetscapeFuncs.requestread,
118 					stream, rangeList);
119 }
120 
121 NPError
NPN_NewStream(NPP instance,NPMIMEType type,const char * window,NPStream ** stream_ptr)122 NPN_NewStream(NPP instance, NPMIMEType type, const char *window,
123 	      NPStream** stream_ptr)
124 {
125 	return CallNPN_NewStreamProc(gNetscapeFuncs.newstream, instance,
126 					type, window, stream_ptr);
127 }
128 
129 int32
NPN_Write(NPP instance,NPStream * stream,int32 len,void * buffer)130 NPN_Write(NPP instance, NPStream* stream, int32 len, void* buffer)
131 {
132 	return CallNPN_WriteProc(gNetscapeFuncs.write, instance,
133 					stream, len, buffer);
134 }
135 
136 NPError
NPN_DestroyStream(NPP instance,NPStream * stream,NPError reason)137 NPN_DestroyStream(NPP instance, NPStream* stream, NPError reason)
138 {
139 	return CallNPN_DestroyStreamProc(gNetscapeFuncs.destroystream,
140 						instance, stream, reason);
141 }
142 
143 void
NPN_Status(NPP instance,const char * message)144 NPN_Status(NPP instance, const char* message)
145 {
146 	CallNPN_StatusProc(gNetscapeFuncs.status, instance, message);
147 }
148 
149 const char*
NPN_UserAgent(NPP instance)150 NPN_UserAgent(NPP instance)
151 {
152 	return CallNPN_UserAgentProc(gNetscapeFuncs.uagent, instance);
153 }
154 
155 void*
NPN_MemAlloc(uint32 size)156 NPN_MemAlloc(uint32 size)
157 {
158 	return CallNPN_MemAllocProc(gNetscapeFuncs.memalloc, size);
159 }
160 
NPN_MemFree(void * ptr)161 void NPN_MemFree(void* ptr)
162 {
163 	CallNPN_MemFreeProc(gNetscapeFuncs.memfree, ptr);
164 }
165 
NPN_MemFlush(uint32 size)166 uint32 NPN_MemFlush(uint32 size)
167 {
168 	return CallNPN_MemFlushProc(gNetscapeFuncs.memflush, size);
169 }
170 
NPN_ReloadPlugins(NPBool reloadPages)171 void NPN_ReloadPlugins(NPBool reloadPages)
172 {
173 	CallNPN_ReloadPluginsProc(gNetscapeFuncs.reloadplugins, reloadPages);
174 }
175 
NPN_GetJavaEnv()176 JRIEnv* NPN_GetJavaEnv()
177 {
178 	return CallNPN_GetJavaEnvProc(gNetscapeFuncs.getJavaEnv);
179 }
180 
NPN_GetJavaPeer(NPP instance)181 jref NPN_GetJavaPeer(NPP instance)
182 {
183 	return CallNPN_GetJavaPeerProc(gNetscapeFuncs.getJavaPeer,
184 				       instance);
185 }
186 
187 
188 /***********************************************************************
189  *
190  * Wrapper functions : Netscape Navigator -> plugin
191  *
192  * These functions let the plugin developer just create the APIs
193  * as documented and defined in npapi.h, without needing to
194  * install those functions in the function table or worry about
195  * setting up globals for 68K plugins.
196  *
197  ***********************************************************************/
198 
199 NPError
Private_New(NPMIMEType pluginType,NPP instance,uint16 mode,int16 argc,char * argn[],char * argv[],NPSavedData * saved)200 Private_New(NPMIMEType pluginType, NPP instance, uint16 mode,
201 		int16 argc, char* argn[], char* argv[], NPSavedData* saved)
202 {
203 	NPError ret;
204 	PLUGINDEBUGSTR("New");
205 	ret = NPP_New(pluginType, instance, mode, argc, argn, argv, saved);
206 	return ret;
207 }
208 
209 NPError
Private_Destroy(NPP instance,NPSavedData ** save)210 Private_Destroy(NPP instance, NPSavedData** save)
211 {
212 	PLUGINDEBUGSTR("Destroy");
213 	return NPP_Destroy(instance, save);
214 }
215 
216 NPError
Private_SetWindow(NPP instance,NPWindow * window)217 Private_SetWindow(NPP instance, NPWindow* window)
218 {
219 	NPError err;
220 	PLUGINDEBUGSTR("SetWindow");
221 	err = NPP_SetWindow(instance, window);
222 	return err;
223 }
224 
225 NPError
Private_NewStream(NPP instance,NPMIMEType type,NPStream * stream,NPBool seekable,uint16 * stype)226 Private_NewStream(NPP instance, NPMIMEType type, NPStream* stream,
227 			NPBool seekable, uint16* stype)
228 {
229 	NPError err;
230 	PLUGINDEBUGSTR("NewStream");
231 	err = NPP_NewStream(instance, type, stream, seekable, stype);
232 	return err;
233 }
234 
235 int32
Private_WriteReady(NPP instance,NPStream * stream)236 Private_WriteReady(NPP instance, NPStream* stream)
237 {
238 	unsigned int result;
239 	PLUGINDEBUGSTR("WriteReady");
240 	result = NPP_WriteReady(instance, stream);
241 	return result;
242 }
243 
244 int32
Private_Write(NPP instance,NPStream * stream,int32 offset,int32 len,void * buffer)245 Private_Write(NPP instance, NPStream* stream, int32 offset, int32 len,
246 		void* buffer)
247 {
248 	unsigned int result;
249 	PLUGINDEBUGSTR("Write");
250 	result = NPP_Write(instance, stream, offset, len, buffer);
251 	return result;
252 }
253 
254 void
Private_StreamAsFile(NPP instance,NPStream * stream,const char * fname)255 Private_StreamAsFile(NPP instance, NPStream* stream, const char* fname)
256 {
257 	PLUGINDEBUGSTR("StreamAsFile");
258 	NPP_StreamAsFile(instance, stream, fname);
259 }
260 
261 
262 void
Private_URLNotify(NPP instance,const char * url,NPReason reason,void * notifyData)263 Private_URLNotify(NPP instance, const char* url, NPReason reason, void* notifyData)
264 {
265 	PLUGINDEBUGSTR("URLNotify");
266 	NPP_URLNotify(instance, url, reason, notifyData);
267 }
268 
269 NPError
Private_DestroyStream(NPP instance,NPStream * stream,NPError reason)270 Private_DestroyStream(NPP instance, NPStream* stream, NPError reason)
271 {
272 	NPError err;
273 	PLUGINDEBUGSTR("DestroyStream");
274 	err = NPP_DestroyStream(instance, stream, reason);
275 	return err;
276 }
277 
278 
279 void
Private_Print(NPP instance,NPPrint * platformPrint)280 Private_Print(NPP instance, NPPrint* platformPrint)
281 {
282 	PLUGINDEBUGSTR("Print");
283 	NPP_Print(instance, platformPrint);
284 }
285 
286 JRIGlobalRef
Private_GetJavaClass(void)287 Private_GetJavaClass(void)
288 {
289     jref clazz = NPP_GetJavaClass();
290     if (clazz) {
291 	JRIEnv* env = NPN_GetJavaEnv();
292 	return JRI_NewGlobalRef(env, clazz);
293     }
294     return NULL;
295 }
296 
297 /***********************************************************************
298  *
299  * These functions are located automagically by netscape.
300  *
301  ***********************************************************************/
302 
303 /*
304  * NP_GetMIMEDescription
305  *	- Netscape needs to know about this symbol
306  *	- Netscape uses the return value to identify when an object instance
307  *	  of this plugin should be created.
308  */
309 char *
NP_GetMIMEDescription(void)310 NP_GetMIMEDescription(void)
311 {
312 	return NPP_GetMIMEDescription();
313 }
314 
315 /*
316  * NP_GetValue [optional]
317  *	- Netscape needs to know about this symbol.
318  *	- Interfaces with plugin to get values for predefined variables
319  *	  that the navigator needs.
320  */
321 NPError
NP_GetValue(void * future,NPPVariable variable,void * value)322 NP_GetValue(void *future, NPPVariable variable, void *value)
323 {
324 	return NPP_GetValue(future, variable, value);
325 }
326 
327 /*
328  * NP_Initialize
329  *	- Netscape needs to know about this symbol.
330  *	- It calls this function after looking up its symbol before it
331  *	  is about to create the first ever object of this kind.
332  *
333  * PARAMETERS
334  *    nsTable	- The netscape function table. If developers just use these
335  *		  wrappers, they dont need to worry about all these function
336  *		  tables.
337  * RETURN
338  *    pluginFuncs
339  *		- This functions needs to fill the plugin function table
340  *		  pluginFuncs and return it. Netscape Navigator plugin
341  *		  library will use this function table to call the plugin.
342  *
343  */
344 NPError
NP_Initialize(NPNetscapeFuncs * nsTable,NPPluginFuncs * pluginFuncs)345 NP_Initialize(NPNetscapeFuncs* nsTable, NPPluginFuncs* pluginFuncs)
346 {
347 	NPError err = NPERR_NO_ERROR;
348 
349 	PLUGINDEBUGSTR("NP_Initialize");
350 
351 	/* validate input parameters */
352 
353 	if ((nsTable == NULL) || (pluginFuncs == NULL))
354 		err = NPERR_INVALID_FUNCTABLE_ERROR;
355 
356 	/*
357 	 * Check the major version passed in Netscape's function table.
358 	 * We won't load if the major version is newer than what we expect.
359 	 * Also check that the function tables passed in are big enough for
360 	 * all the functions we need (they could be bigger, if Netscape added
361 	 * new APIs, but that's OK with us -- we'll just ignore them).
362 	 *
363 	 */
364 
365 	if (err == NPERR_NO_ERROR) {
366 		if ((nsTable->version >> 8) > NP_VERSION_MAJOR)
367 			err = NPERR_INCOMPATIBLE_VERSION_ERROR;
368 		if (nsTable->size < sizeof(NPNetscapeFuncs))
369 			err = NPERR_INVALID_FUNCTABLE_ERROR;
370 		if (pluginFuncs->size < sizeof(NPPluginFuncs))
371 			err = NPERR_INVALID_FUNCTABLE_ERROR;
372 	}
373 
374 
375 	if (err == NPERR_NO_ERROR) {
376 		/*
377 		 * Copy all the fields of Netscape function table into our
378 		 * copy so we can call back into Netscape later.  Note that
379 		 * we need to copy the fields one by one, rather than assigning
380 		 * the whole structure, because the Netscape function table
381 		 * could actually be bigger than what we expect.
382 		 */
383 		gNetscapeFuncs.version       = nsTable->version;
384 		gNetscapeFuncs.size          = nsTable->size;
385 		gNetscapeFuncs.posturl       = nsTable->posturl;
386 		gNetscapeFuncs.geturl        = nsTable->geturl;
387 		gNetscapeFuncs.requestread   = nsTable->requestread;
388 		gNetscapeFuncs.newstream     = nsTable->newstream;
389 		gNetscapeFuncs.write         = nsTable->write;
390 		gNetscapeFuncs.destroystream = nsTable->destroystream;
391 		gNetscapeFuncs.status        = nsTable->status;
392 		gNetscapeFuncs.uagent        = nsTable->uagent;
393 		gNetscapeFuncs.memalloc      = nsTable->memalloc;
394 		gNetscapeFuncs.memfree       = nsTable->memfree;
395 		gNetscapeFuncs.memflush      = nsTable->memflush;
396 		gNetscapeFuncs.reloadplugins = nsTable->reloadplugins;
397 		gNetscapeFuncs.getJavaEnv    = nsTable->getJavaEnv;
398 		gNetscapeFuncs.getJavaPeer   = nsTable->getJavaPeer;
399 		gNetscapeFuncs.getvalue      = nsTable->getvalue;
400 
401 		if ((nsTable->version & 0xFF) >= NPVERS_HAS_NOTIFICATION) {
402 		  gNetscapeFuncs.posturlnotify = nsTable->posturlnotify;
403 		  gNetscapeFuncs.geturlnotify  = nsTable->geturlnotify;
404 		}
405 
406 		/*
407 		 * Set up the plugin function table that Netscape will use to
408 		 * call us.  Netscape needs to know about our version and size
409 		 * and have a UniversalProcPointer for every function we
410 		 * implement.
411 		 */
412 		pluginFuncs->version    = (NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR;
413 		pluginFuncs->size       = sizeof(NPPluginFuncs);
414 		pluginFuncs->newp       = NewNPP_NewProc(Private_New);
415 		pluginFuncs->destroy    = NewNPP_DestroyProc(Private_Destroy);
416 		pluginFuncs->setwindow  = NewNPP_SetWindowProc(Private_SetWindow);
417 		pluginFuncs->newstream  = NewNPP_NewStreamProc(Private_NewStream);
418 		pluginFuncs->destroystream = NewNPP_DestroyStreamProc(Private_DestroyStream);
419 		pluginFuncs->asfile     = NewNPP_StreamAsFileProc(Private_StreamAsFile);
420 		pluginFuncs->urlnotify  = NewNPP_URLNotifyProc(Private_URLNotify);
421 		pluginFuncs->writeready = NewNPP_WriteReadyProc(Private_WriteReady);
422 		pluginFuncs->write      = NewNPP_WriteProc(Private_Write);
423 		pluginFuncs->print      = NewNPP_PrintProc(Private_Print);
424 		pluginFuncs->event      = NULL;
425  		pluginFuncs->javaClass	= Private_GetJavaClass();
426 
427 		err = NPP_Initialize();
428 	}
429 
430 	return err;
431 }
432 
433 /*
434  * NP_Shutdown [optional]
435  *	- Netscape needs to know about this symbol.
436  *	- It calls this function after looking up its symbol after
437  *	  the last object of this kind has been destroyed.
438  *
439  */
440 NPError
NP_Shutdown(void)441 NP_Shutdown(void)
442 {
443  	PLUGINDEBUGSTR("NP_Shutdown");
444 	NPP_Shutdown();
445 	return NPERR_NO_ERROR;
446 }
447