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