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