]> git.sesse.net Git - vlc/blob - projects/mozilla/support/npmac.cpp
Clean up references to UPP types, plugin/host glue.
[vlc] / projects / mozilla / support / npmac.cpp
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  *
3  * Safari/Mozilla/Firefox plugin for VLC
4  * Copyright (C) 2009, Jean-Paul Saman <jpsaman@videolan.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
22 //
23 // npmac.cpp
24 //
25 //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
26
27 #include "config.h"
28
29 #include <string.h>
30
31 #include <Processes.h>
32 #include <Gestalt.h>
33 #include <CodeFragments.h>
34 #include <Timer.h>
35 #include <Resources.h>
36 #include <ToolUtils.h>
37
38 #define XP_MACOSX 1
39 #undef TARGET_RT_MAC_CFM
40
41 //
42 // A4Stuff.h contains the definition of EnterCodeResource and
43 // EnterCodeResource, used for setting up the code resource’s
44 // globals for 68K (analagous to the function SetCurrentA5
45 // defined by the toolbox).
46 //
47 // A4Stuff does not exist as of CW 7. Define them to nothing.
48 //
49
50 #if (defined(__MWERKS__) && (__MWERKS__ >= 0x2400)) || defined(__GNUC__)
51     #define EnterCodeResource()
52     #define ExitCodeResource()
53 #else
54     #include <A4Stuff.h>
55 #endif
56
57 #include "npapi.h"
58
59 //
60 // The Mixed Mode procInfos defined in npupp.h assume Think C-
61 // style calling conventions.  These conventions are used by
62 // Metrowerks with the exception of pointer return types, which
63 // in Metrowerks 68K are returned in A0, instead of the standard
64 // D0. Thus, since NPN_MemAlloc and NPN_UserAgent return pointers,
65 // Mixed Mode will return the values to a 68K plugin in D0, but
66 // a 68K plugin compiled by Metrowerks will expect the result in
67 // A0.  The following pragma forces Metrowerks to use D0 instead.
68 //
69 #ifdef __MWERKS__
70 #ifndef powerc
71 #pragma pointers_in_D0
72 #endif
73 #endif
74
75 #ifdef XP_UNIX
76 #undef XP_UNIX
77 #endif
78
79 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
80 #include "npupp.h" 
81 // e.g. CALL_NPN(CallNPN_GetURLNotify, x, y, z) -> CallNPN_GetURLNotify(x, y, z);
82 #define CALL_NPN(__CallNPNFunc__, ...) (__CallNPNFunc__(__VA_ARGS__))
83 #else
84 #include "npfunctions.h"
85 #define CALL_NPN(unused, FN, ...) ((*FN)(__VA_ARGS__))
86 #endif
87
88 #include "../vlcshell.h"
89
90 #ifdef __MWERKS__
91 #ifndef powerc
92 #pragma pointers_in_A0
93 #endif
94 #endif
95
96 // The following fix for static initializers (which fixes a previous
97 // incompatibility with some parts of PowerPlant, was submitted by
98 // Jan Ulbrich.
99 #ifdef __MWERKS__
100     #ifdef __cplusplus
101     extern "C" {
102     #endif
103         #ifndef powerc
104             extern void __InitCode__(void);
105         #else
106             extern void __sinit(void);
107             #define __InitCode__ __sinit
108         #endif
109         extern void __destroy_global_chain(void);
110     #ifdef __cplusplus
111     }
112     #endif // __cplusplus
113 #endif // __MWERKS__
114
115 //
116 // Define PLUGIN_TRACE to 1 to have the wrapper functions emit
117 // DebugStr messages whenever they are called.
118 //
119 #define PLUGIN_TRACE 0
120
121 #if PLUGIN_TRACE
122 #define PLUGINDEBUGSTR(msg)     ::DebugStr(msg)
123 #else
124 #define PLUGINDEBUGSTR(msg) {}
125 #endif
126
127 #if defined(XP_MACOSX) && defined(__POWERPC__) && (!defined(TARGET_RT_MAC_CFM))
128
129 // glue for mapping outgoing Macho function pointers to TVectors
130 struct TFPtoTVGlue {
131     void* glue[2];
132 };
133
134 static struct {
135     TFPtoTVGlue     newp;
136     TFPtoTVGlue     destroy;
137     TFPtoTVGlue     setwindow;
138     TFPtoTVGlue     newstream;
139     TFPtoTVGlue     destroystream;
140     TFPtoTVGlue     asfile;
141     TFPtoTVGlue     writeready;
142     TFPtoTVGlue     write;
143     TFPtoTVGlue     print;
144     TFPtoTVGlue     event;
145     TFPtoTVGlue     urlnotify;
146     TFPtoTVGlue     getvalue;
147     TFPtoTVGlue     setvalue;
148
149     TFPtoTVGlue     shutdown;
150 } gPluginFuncsGlueTable;
151
152 static inline void* SetupFPtoTVGlue(TFPtoTVGlue* functionGlue, void* fp)
153 {
154     functionGlue->glue[0] = fp;
155     functionGlue->glue[1] = 0;
156     return functionGlue;
157 }
158
159 #define PLUGIN_TO_HOST_GLUE(name, fp) (SetupFPtoTVGlue(&gPluginFuncsGlueTable.name, (void*)fp))
160
161 // glue for mapping netscape TVectors to Macho function pointers
162 struct TTVtoFPGlue {
163     uint32_t glue[6];
164 };
165
166 static struct {
167     TTVtoFPGlue             geturl;
168     TTVtoFPGlue             posturl;
169     TTVtoFPGlue             requestread;
170     TTVtoFPGlue             newstream;
171     TTVtoFPGlue             write;
172     TTVtoFPGlue             destroystream;
173     TTVtoFPGlue             status;
174     TTVtoFPGlue             uagent;
175     TTVtoFPGlue             memalloc;
176     TTVtoFPGlue             memfree;
177     TTVtoFPGlue             memflush;
178     TTVtoFPGlue             reloadplugins;
179     TTVtoFPGlue             getJavaEnv;
180     TTVtoFPGlue             getJavaPeer;
181     TTVtoFPGlue             geturlnotify;
182     TTVtoFPGlue             posturlnotify;
183     TTVtoFPGlue             getvalue;
184     TTVtoFPGlue             setvalue;
185     TTVtoFPGlue             invalidaterect;
186     TTVtoFPGlue             invalidateregion;
187     TTVtoFPGlue             forceredraw;
188     // NPRuntime support
189     TTVtoFPGlue             getstringidentifier;
190     TTVtoFPGlue             getstringidentifiers;
191     TTVtoFPGlue             getintidentifier;
192     TTVtoFPGlue             identifierisstring;
193     TTVtoFPGlue             utf8fromidentifier;
194     TTVtoFPGlue             intfromidentifier;
195     TTVtoFPGlue             createobject;
196     TTVtoFPGlue             retainobject;
197     TTVtoFPGlue             releaseobject;
198     TTVtoFPGlue             invoke;
199     TTVtoFPGlue             invokeDefault;
200     TTVtoFPGlue             evaluate;
201     TTVtoFPGlue             getproperty;
202     TTVtoFPGlue             setproperty;
203     TTVtoFPGlue             removeproperty;
204     TTVtoFPGlue             hasproperty;
205     TTVtoFPGlue             hasmethod;
206     TTVtoFPGlue             releasevariantvalue;
207     TTVtoFPGlue             setexception;
208 } gNetscapeFuncsGlueTable;
209
210 static void* SetupTVtoFPGlue(TTVtoFPGlue* functionGlue, void* tvp)
211 {
212     static const TTVtoFPGlue glueTemplate = { 0x3D800000, 0x618C0000, 0x800C0000, 0x804C0004, 0x7C0903A6, 0x4E800420 };
213
214     memcpy(functionGlue, &glueTemplate, sizeof(TTVtoFPGlue));
215     functionGlue->glue[0] |= ((UInt32)tvp >> 16);
216     functionGlue->glue[1] |= ((UInt32)tvp & 0xFFFF);
217     ::MakeDataExecutable(functionGlue, sizeof(TTVtoFPGlue));
218     return functionGlue;
219 }
220
221 #define HOST_TO_PLUGIN_GLUE(name, fp) ((UniversalProcPtr)(SetupTVtoFPGlue(&gNetscapeFuncsGlueTable.name, (void*)fp)))
222
223 #else
224
225 #define PLUGIN_TO_HOST_GLUE(name, fp) (fp)
226 #define HOST_TO_PLUGIN_GLUE(name, fp) (fp)
227
228 #endif /* XP_MACOSX */
229
230 #pragma mark -
231
232 //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
233 //
234 // Globals
235 //
236 //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
237
238 #if !TARGET_API_MAC_CARBON
239 QDGlobals*      gQDPtr;             // Pointer to Netscape’s QuickDraw globals
240 #endif
241 short           gResFile;           // Refnum of the plugin’s resource file
242 NPNetscapeFuncs    gNetscapeFuncs;      // Function table for procs in Netscape called by plugin
243
244 //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
245 //
246 // Wrapper functions for all calls from the plugin to Netscape.
247 // These functions let the plugin developer just call the APIs
248 // as documented and defined in npapi.h, without needing to know
249 // about the function table and call macros in npupp.h.
250 //
251 //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
252
253 void NPN_Version(int* plugin_major, int* plugin_minor, int* netscape_major, int* netscape_minor)
254 {
255     *plugin_major = NP_VERSION_MAJOR;
256     *plugin_minor = NP_VERSION_MINOR;
257     *netscape_major = gNetscapeFuncs.version >> 8;      // Major version is in high byte
258     *netscape_minor = gNetscapeFuncs.version & 0xFF;    // Minor version is in low byte
259 }
260
261 NPError NPN_GetURLNotify(NPP instance, const char* url, const char* window, void* notifyData)
262 {
263     int navMinorVers = gNetscapeFuncs.version & 0xFF;
264     NPError err;
265
266     if( navMinorVers >= NPVERS_HAS_NOTIFICATION )
267     {
268         err = CALL_NPN(CallNPN_GetURLNotifyProc, gNetscapeFuncs.geturlnotify, instance, url, window, notifyData);
269     }
270     else
271     {
272         err = NPERR_INCOMPATIBLE_VERSION_ERROR;
273     }
274     return err;
275 }
276
277 NPError NPN_GetURL(NPP instance, const char* url, const char* window)
278 {
279     return CALL_NPN(CallNPN_GetURLProc, gNetscapeFuncs.geturl, instance, url, window);
280 }
281
282 NPError NPN_PostURLNotify(NPP instance, const char* url, const char* window, uint32_t len, const char* buf, NPBool file, void* notifyData)
283 {
284     int navMinorVers = gNetscapeFuncs.version & 0xFF;
285     NPError err;
286
287     if( navMinorVers >= NPVERS_HAS_NOTIFICATION )
288     {
289         err = CALL_NPN(CallNPN_PostURLNotifyProc, gNetscapeFuncs.posturlnotify, instance, url,
290                                                         window, len, buf, file, notifyData);
291     }
292     else
293     {
294         err = NPERR_INCOMPATIBLE_VERSION_ERROR;
295     }
296     return err;
297 }
298
299 NPError NPN_PostURL(NPP instance, const char* url, const char* window, uint32_t len, const char* buf, NPBool file)
300 {
301     return CALL_NPN(CallNPN_PostURLProc, gNetscapeFuncs.posturl, instance, url, window, len, buf, file);
302 }
303
304 NPError NPN_RequestRead(NPStream* stream, NPByteRange* rangeList)
305 {
306     return CALL_NPN(CallNPN_RequestReadProc, gNetscapeFuncs.requestread, stream, rangeList);
307 }
308
309 NPError NPN_NewStream(NPP instance, NPMIMEType type, const char* window, NPStream** stream)
310 {
311     int navMinorVers = gNetscapeFuncs.version & 0xFF;
312     NPError err;
313
314     if( navMinorVers >= NPVERS_HAS_STREAMOUTPUT )
315     {
316         err = CALL_NPN(CallNPN_NewStreamProc, gNetscapeFuncs.newstream, instance, type, window, stream);
317     }
318     else
319     {
320         err = NPERR_INCOMPATIBLE_VERSION_ERROR;
321     }
322     return err;
323 }
324
325 int32_t NPN_Write(NPP instance, NPStream* stream, int32_t len, void* buffer)
326 {
327     int navMinorVers = gNetscapeFuncs.version & 0xFF;
328     NPError err;
329
330     if( navMinorVers >= NPVERS_HAS_STREAMOUTPUT )
331     {
332         err = CALL_NPN(CallNPN_WriteProc, gNetscapeFuncs.write, instance, stream, len, buffer);
333     }
334     else
335     {
336         err = NPERR_INCOMPATIBLE_VERSION_ERROR;
337     }
338     return err;
339 }
340
341 NPError    NPN_DestroyStream(NPP instance, NPStream* stream, NPError reason)
342 {
343     int navMinorVers = gNetscapeFuncs.version & 0xFF;
344     NPError err;
345
346     if( navMinorVers >= NPVERS_HAS_STREAMOUTPUT )
347     {
348         err = CALL_NPN(CallNPN_DestroyStreamProc, gNetscapeFuncs.destroystream, instance, stream, reason);
349     }
350     else
351     {
352         err = NPERR_INCOMPATIBLE_VERSION_ERROR;
353     }
354     return err;
355 }
356
357 void NPN_Status(NPP instance, const char* message)
358 {
359     CALL_NPN(CallNPN_StatusProc, gNetscapeFuncs.status, instance, message);
360 }
361
362 const char* NPN_UserAgent(NPP instance)
363 {
364     return CALL_NPN(CallNPN_UserAgentProc, gNetscapeFuncs.uagent, instance);
365 }
366
367 void* NPN_MemAlloc(uint32_t size)
368 {
369     return CALL_NPN(CallNPN_MemAllocProc, gNetscapeFuncs.memalloc, size);
370 }
371
372 void NPN_MemFree(void* ptr)
373 {
374     CALL_NPN(CallNPN_MemFreeProc, gNetscapeFuncs.memfree, ptr);
375 }
376
377 uint32_t NPN_MemFlush(uint32_t size)
378 {
379     return CALL_NPN(CallNPN_MemFlushProc, gNetscapeFuncs.memflush, size);
380 }
381
382 void NPN_ReloadPlugins(NPBool reloadPages)
383 {
384     CALL_NPN(CallNPN_ReloadPluginsProc, gNetscapeFuncs.reloadplugins, reloadPages);
385 }
386
387 #ifdef OJI
388 JRIEnv* NPN_GetJavaEnv(void)
389 {
390     return CallNPN_GetJavaEnvProc( gNetscapeFuncs.getJavaEnv );
391 }
392
393 jobject  NPN_GetJavaPeer(NPP instance)
394 {
395     return CallNPN_GetJavaPeerProc( gNetscapeFuncs.getJavaPeer, instance );
396 }
397 #endif
398
399 NPError NPN_GetValue(NPP instance, NPNVariable variable, void *value)
400 {
401     return CALL_NPN(CallNPN_GetValueProc, gNetscapeFuncs.getvalue, instance, variable, value);
402 }
403
404 NPError NPN_SetValue(NPP instance, NPPVariable variable, void *value)
405 {
406     return CALL_NPN(CallNPN_SetValueProc, gNetscapeFuncs.setvalue, instance, variable, value);
407 }
408
409 void NPN_InvalidateRect(NPP instance, NPRect *rect)
410 {
411     CALL_NPN(CallNPN_InvalidateRectProc, gNetscapeFuncs.invalidaterect, instance, rect);
412 }
413
414 void NPN_InvalidateRegion(NPP instance, NPRegion region)
415 {
416     CALL_NPN(CallNPN_InvalidateRegionProc, gNetscapeFuncs.invalidateregion, instance, region);
417 }
418
419 void NPN_ForceRedraw(NPP instance)
420 {
421     CALL_NPN(CallNPN_ForceRedrawProc, gNetscapeFuncs.forceredraw, instance);
422 }
423
424 NPIdentifier NPN_GetStringIdentifier(const NPUTF8 *name)
425 {
426     int navMinorVers = gNetscapeFuncs.version & 0xFF;
427     if( navMinorVers >= 14 )
428     {
429         return CALL_NPN(CallNPN_GetStringIdentifierProc, gNetscapeFuncs.getstringidentifier, name);
430     }
431     return NULL;
432 }
433
434 void NPN_GetStringIdentifiers(const NPUTF8 **names, int32_t nameCount, NPIdentifier *identifiers)
435 {
436     int navMinorVers = gNetscapeFuncs.version & 0xFF;
437     if( navMinorVers >= 14 )
438     {
439         CALL_NPN(CallNPN_GetStringIdentifiersProc, gNetscapeFuncs.getstringidentifiers, names, nameCount, identifiers);
440     }
441 }
442
443 NPIdentifier NPN_GetIntIdentifier(int32_t intid)
444 {
445     int navMinorVers = gNetscapeFuncs.version & 0xFF;
446     if( navMinorVers >= 14 )
447     {
448         return CALL_NPN(CallNPN_GetIntIdentifierProc, gNetscapeFuncs.getintidentifier, intid);
449     }
450     return NULL;
451 }
452
453 bool NPN_IdentifierIsString(NPIdentifier identifier)
454 {
455     int navMinorVers = gNetscapeFuncs.version & 0xFF;
456     if( navMinorVers >= 14 )
457     {
458         return CALL_NPN(CallNPN_IdentifierIsStringProc, gNetscapeFuncs.identifierisstring, identifier);
459     }
460     return false;
461 }
462
463 NPUTF8 *NPN_UTF8FromIdentifier(NPIdentifier identifier)
464 {
465     int navMinorVers = gNetscapeFuncs.version & 0xFF;
466     if( navMinorVers >= 14 )
467     {
468         return CALL_NPN(CallNPN_UTF8FromIdentifierProc, gNetscapeFuncs.utf8fromidentifier, identifier);
469     }
470     return NULL;
471 }
472
473 int32_t NPN_IntFromIdentifier(NPIdentifier identifier)
474 {
475     int navMinorVers = gNetscapeFuncs.version & 0xFF;
476     if( navMinorVers >= 14 )
477     {
478         return CALL_NPN(CallNPN_IntFromIdentifierProc, gNetscapeFuncs.intfromidentifier, identifier);
479     }
480     return 0;
481 }
482
483 NPObject *NPN_CreateObject(NPP instance, NPClass *aClass)
484 {
485     int navMinorVers = gNetscapeFuncs.version & 0xFF;
486     if( navMinorVers >= 14 )
487     {
488         return CALL_NPN(CallNPN_CreateObjectProc, gNetscapeFuncs.createobject, instance, aClass);
489     }
490     return NULL;
491 }
492
493 NPObject *NPN_RetainObject(NPObject *npobj)
494 {
495     int navMinorVers = gNetscapeFuncs.version & 0xFF;
496     if( navMinorVers >= 14 )
497     {
498         return CALL_NPN(CallNPN_RetainObjectProc, gNetscapeFuncs.retainobject, npobj);
499     }
500     return NULL;
501 }
502
503 void NPN_ReleaseObject(NPObject *npobj)
504 {
505     int navMinorVers = gNetscapeFuncs.version & 0xFF;
506     if( navMinorVers >= 14 )
507     {
508         CALL_NPN(CallNPN_ReleaseObjectProc, gNetscapeFuncs.releaseobject, npobj);
509     }
510 }
511
512 bool NPN_Invoke(NPP instance, NPObject *npobj, NPIdentifier methodName, const NPVariant *args, uint32_t argCount, NPVariant *result)
513 {
514     int navMinorVers = gNetscapeFuncs.version & 0xFF;
515     if( navMinorVers >= 14 )
516     {
517         return CALL_NPN(CallNPN_InvokeProc, gNetscapeFuncs.invoke, instance, npobj, methodName, args, argCount, result);
518     }
519     return false;
520 }
521
522 bool NPN_InvokeDefault(NPP instance, NPObject *npobj, const NPVariant *args, uint32_t argCount, NPVariant *result)
523 {
524     int navMinorVers = gNetscapeFuncs.version & 0xFF;
525     if( navMinorVers >= 14 )
526     {
527         return CALL_NPN(CallNPN_InvokeDefaultProc, gNetscapeFuncs.invokeDefault, instance, npobj, args, argCount, result);
528     }
529     return false;
530 }
531
532 bool NPN_Evaluate(NPP instance, NPObject *npobj, NPString *script, NPVariant *result)
533 {
534     int navMinorVers = gNetscapeFuncs.version & 0xFF;
535     if( navMinorVers >= 14 )
536     {
537         return CALL_NPN(CallNPN_EvaluateProc, gNetscapeFuncs.evaluate, instance, npobj, script, result);
538     }
539     return false;
540 }
541
542 bool NPN_GetProperty(NPP instance, NPObject *npobj, NPIdentifier propertyName, NPVariant *result)
543 {
544     int navMinorVers = gNetscapeFuncs.version & 0xFF;
545     if( navMinorVers >= 14 )
546     {
547         return CALL_NPN(CallNPN_GetPropertyProc, gNetscapeFuncs.getproperty, instance, npobj, propertyName, result);
548     }
549     return false;
550 }
551
552 bool NPN_SetProperty(NPP instance, NPObject *npobj, NPIdentifier propertyName, const NPVariant *value)
553 {
554     int navMinorVers = gNetscapeFuncs.version & 0xFF;
555     if( navMinorVers >= 14 )
556     {
557         return CALL_NPN(CallNPN_SetPropertyProc, gNetscapeFuncs.setproperty, instance, npobj, propertyName, value);
558     }
559     return false;
560 }
561
562 bool NPN_RemoveProperty(NPP instance, NPObject *npobj, NPIdentifier propertyName)
563 {
564     int navMinorVers = gNetscapeFuncs.version & 0xFF;
565     if( navMinorVers >= 14 )
566     {
567         return CALL_NPN(CallNPN_RemovePropertyProc, gNetscapeFuncs.removeproperty, instance, npobj, propertyName);
568     }
569     return false;
570 }
571
572 bool NPN_HasProperty(NPP instance, NPObject *npobj, NPIdentifier propertyName)
573 {
574     int navMinorVers = gNetscapeFuncs.version & 0xFF;
575     if( navMinorVers >= 14 )
576     {
577         return CALL_NPN(CallNPN_HasPropertyProc, gNetscapeFuncs.hasproperty, instance, npobj, propertyName);
578     }
579     return false;
580 }
581
582 bool NPN_HasMethod(NPP instance, NPObject *npobj, NPIdentifier methodName)
583 {
584     int navMinorVers = gNetscapeFuncs.version & 0xFF;
585     if( navMinorVers >= 14 )
586     {
587         return CALL_NPN(CallNPN_HasMethodProc, gNetscapeFuncs.hasmethod, instance, npobj, methodName);
588     }
589     return false;
590 }
591
592 void NPN_ReleaseVariantValue(NPVariant *variant)
593 {
594     int navMinorVers = gNetscapeFuncs.version & 0xFF;
595     if( navMinorVers >= 14 )
596     {
597         CALL_NPN(CallNPN_ReleaseVariantValueProc, gNetscapeFuncs.releasevariantvalue, variant);
598     }
599 }
600
601 void NPN_SetException(NPObject *npobj, const NPUTF8 *message)
602 {
603     int navMinorVers = gNetscapeFuncs.version & 0xFF;
604     if( navMinorVers >= 14 )
605     {
606         CALL_NPN(CallNPN_SetExceptionProc, gNetscapeFuncs.setexception, npobj, message);
607     }
608 }
609
610 #pragma mark -
611
612 //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
613 //
614 // Wrapper functions for all calls from Netscape to the plugin.
615 // These functions let the plugin developer just create the APIs
616 // as documented and defined in npapi.h, without needing to
617 // install those functions in the function table or worry about
618 // setting up globals for 68K plugins.
619 //
620 //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
621
622 NPError     Private_Initialize(void);
623 void        Private_Shutdown(void);
624 NPError     Private_New(NPMIMEType pluginType, NPP instance, uint16_t mode, int16_t argc, char* argn[], char* argv[], NPSavedData* saved);
625 NPError     Private_Destroy(NPP instance, NPSavedData** save);
626 NPError     Private_SetWindow(NPP instance, NPWindow* window);
627 NPError     Private_GetValue( NPP instance, NPPVariable variable, void *value );
628 NPError     Private_SetValue( NPP instance, NPPVariable variable, void *value );
629 NPError     Private_NewStream(NPP instance, NPMIMEType type, NPStream* stream, NPBool seekable, uint16_t* stype);
630 NPError     Private_DestroyStream(NPP instance, NPStream* stream, NPError reason);
631 int32_t     Private_WriteReady(NPP instance, NPStream* stream);
632 int32_t     Private_Write(NPP instance, NPStream* stream, int32_t offset, int32_t len, void* buffer);
633 void        Private_StreamAsFile(NPP instance, NPStream* stream, const char* fname);
634 void        Private_Print(NPP instance, NPPrint* platformPrint);
635 int16_t     Private_HandleEvent(NPP instance, void* event);
636 void        Private_URLNotify(NPP instance, const char* url, NPReason reason, void* notifyData);
637 #ifdef OJI
638 jobject     Private_GetJavaClass(void);
639 #endif // OJI
640
641
642 NPError Private_Initialize(void)
643 {
644     NPError err;
645     EnterCodeResource();
646     PLUGINDEBUGSTR("\pInitialize;g;");
647     err = NPP_Initialize();
648     ExitCodeResource();
649     return err;
650 }
651
652 void Private_Shutdown(void)
653 {
654     EnterCodeResource();
655     PLUGINDEBUGSTR("\pShutdown;g;");
656     NPP_Shutdown();
657
658 #ifdef __MWERKS__
659     __destroy_global_chain();
660 #endif
661
662     ExitCodeResource();
663 }
664
665 NPError    Private_New(NPMIMEType pluginType, NPP instance, uint16_t mode, int16_t argc, char* argn[], char* argv[], NPSavedData* saved)
666 {
667     EnterCodeResource();
668     NPError ret = NPP_New(pluginType, instance, mode, argc, argn, argv, saved);
669     PLUGINDEBUGSTR("\pNew;g;");
670     ExitCodeResource();
671     return ret;
672 }
673
674 NPError Private_Destroy(NPP instance, NPSavedData** save)
675 {
676     NPError err;
677     EnterCodeResource();
678     PLUGINDEBUGSTR("\pDestroy;g;");
679     err = NPP_Destroy(instance, save);
680     ExitCodeResource();
681     return err;
682 }
683
684 NPError Private_SetWindow(NPP instance, NPWindow* window)
685 {
686     NPError err;
687     EnterCodeResource();
688     PLUGINDEBUGSTR("\pSetWindow;g;");
689     err = NPP_SetWindow(instance, window);
690     ExitCodeResource();
691     return err;
692 }
693
694 NPError Private_GetValue( NPP instance, NPPVariable variable, void *value )
695 {
696     NPError err;
697     EnterCodeResource();
698     PLUGINDEBUGSTR("\pGetValue;g;");
699     err = NPP_GetValue(instance, variable, value);
700     ExitCodeResource();
701     return err;
702 }
703
704 NPError Private_SetValue( NPP instance, NPNVariable variable, void *value )
705 {
706     NPError err;
707     EnterCodeResource();
708     PLUGINDEBUGSTR("\pSetValue;g;");
709     err = NPP_SetValue(instance, variable, value);
710     ExitCodeResource();
711     return err;
712 }
713
714 NPError Private_NewStream(NPP instance, NPMIMEType type, NPStream* stream, NPBool seekable, uint16_t* stype)
715 {
716     NPError err;
717     EnterCodeResource();
718     PLUGINDEBUGSTR("\pNewStream;g;");
719     err = NPP_NewStream(instance, type, stream, seekable, stype);
720     ExitCodeResource();
721     return err;
722 }
723
724 int32_t Private_WriteReady(NPP instance, NPStream* stream)
725 {
726     int32_t result;
727     EnterCodeResource();
728     PLUGINDEBUGSTR("\pWriteReady;g;");
729     result = NPP_WriteReady(instance, stream);
730     ExitCodeResource();
731     return result;
732 }
733
734 int32_t Private_Write(NPP instance, NPStream* stream, int32_t offset, int32_t len, void* buffer)
735 {
736     int32_t result;
737     EnterCodeResource();
738     PLUGINDEBUGSTR("\pWrite;g;");
739     result = NPP_Write(instance, stream, offset, len, buffer);
740     ExitCodeResource();
741     return result;
742 }
743
744 void Private_StreamAsFile(NPP instance, NPStream* stream, const char* fname)
745 {
746     EnterCodeResource();
747     PLUGINDEBUGSTR("\pStreamAsFile;g;");
748     NPP_StreamAsFile(instance, stream, fname);
749     ExitCodeResource();
750 }
751
752 NPError Private_DestroyStream(NPP instance, NPStream* stream, NPError reason)
753 {
754     NPError err;
755     EnterCodeResource();
756     PLUGINDEBUGSTR("\pDestroyStream;g;");
757     err = NPP_DestroyStream(instance, stream, reason);
758     ExitCodeResource();
759     return err;
760 }
761
762 int16_t Private_HandleEvent(NPP instance, void* event)
763 {
764     int16_t result;
765     EnterCodeResource();
766     PLUGINDEBUGSTR("\pHandleEvent;g;");
767     result = NPP_HandleEvent(instance, event);
768     ExitCodeResource();
769     return result;
770 }
771
772 void Private_Print(NPP instance, NPPrint* platformPrint)
773 {
774     EnterCodeResource();
775     PLUGINDEBUGSTR("\pPrint;g;");
776     NPP_Print(instance, platformPrint);
777     ExitCodeResource();
778 }
779
780 void Private_URLNotify(NPP instance, const char* url, NPReason reason, void* notifyData)
781 {
782     EnterCodeResource();
783     PLUGINDEBUGSTR("\pURLNotify;g;");
784     NPP_URLNotify(instance, url, reason, notifyData);
785     ExitCodeResource();
786 }
787
788 #ifdef OJI
789 jobject Private_GetJavaClass(void)
790 {
791     EnterCodeResource();
792     PLUGINDEBUGSTR("\pGetJavaClass;g;");
793
794     jobject clazz = NPP_GetJavaClass();
795     ExitCodeResource();
796     if (clazz)
797     {
798         JRIEnv* env = NPN_GetJavaEnv();
799         return (jobject)JRI_NewGlobalRef(env, clazz);
800     }
801     return NULL;
802 }
803 #endif
804
805 void SetUpQD(void);
806 void SetUpQD(void)
807 {
808 #if !TARGET_API_MAC_CARBON
809     ProcessSerialNumber PSN;
810     FSSpec              myFSSpec;
811     Str63               name;
812     ProcessInfoRec      infoRec;
813     OSErr               result = noErr;
814     CFragConnectionID   connID;
815     Str255              errName;
816 #endif
817
818     //
819     // Memorize the plugin’s resource file
820     // refnum for later use.
821     //
822     gResFile = CurResFile();
823
824 #if !TARGET_API_MAC_CARBON
825     //
826     // Ask the system if CFM is available.
827     //
828     long response;
829     OSErr err = Gestalt(gestaltCFMAttr, &response);
830     Boolean hasCFM = BitTst(&response, 31-gestaltCFMPresent);
831
832     ProcessInfoRec infoRec;
833     if (hasCFM)
834     {
835         //
836         // GetProcessInformation takes a process serial number and
837         // will give us back the name and FSSpec of the application.
838         // See the Process Manager in IM.
839         //
840         Str63 name;
841         FSSpec myFSSpec;
842         infoRec.processInfoLength = sizeof(ProcessInfoRec);
843         infoRec.processName = name;
844         infoRec.processAppSpec = &myFSSpec;
845
846         ProcessSerialNumber PSN;
847         PSN.highLongOfPSN = 0;
848         PSN.lowLongOfPSN = kCurrentProcess;
849
850         result = GetProcessInformation(&PSN, &infoRec);
851         if (result != noErr)
852             PLUGINDEBUGSTR("\pFailed in GetProcessInformation");
853     }
854     else
855         //
856         // If no CFM installed, assume it must be a 68K app.
857         //
858         result = -1;
859
860     CFragConnectionID connID;
861     if (result == noErr)
862     {
863         //
864         // Now that we know the app name and FSSpec, we can call GetDiskFragment
865         // to get a connID to use in a subsequent call to FindSymbol (it will also
866         // return the address of “main” in app, which we ignore).  If GetDiskFragment
867         // returns an error, we assume the app must be 68K.
868         //
869         Ptr mainAddr;
870         Str255 errName;
871         result =  GetDiskFragment(infoRec.processAppSpec, 0L, 0L, infoRec.processName,
872                                   kLoadCFrag, &connID, (Ptr*)&mainAddr, errName);
873     }
874
875     if (result == noErr)
876     {
877         //
878         // The app is a PPC code fragment, so call FindSymbol
879         // to get the exported “qd” symbol so we can access its
880         // QuickDraw globals.
881         //
882         CFragSymbolClass symClass;
883         result = FindSymbol(connID, "\pqd", (Ptr*)&gQDPtr, &symClass);
884         if (result != noErr) {  // this fails if we are in NS 6
885             gQDPtr = &qd;       // so we default to the standard QD globals
886         }
887     }
888     else
889     {
890         //
891         // The app is 68K, so use its A5 to compute the address
892         // of its QuickDraw globals.
893         //
894         gQDPtr = (QDGlobals*)(*((long*)SetCurrentA5()) - (sizeof(QDGlobals) - sizeof(GrafPtr)));
895     }
896 #endif
897 }
898
899 #ifdef __GNUC__
900 // gcc requires that main have an 'int' return type
901 typedef int main_return_t;
902 #else
903 typedef NPError mainReturnType;
904 #endif
905
906 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
907 typedef NPP_ShutdownUPP unloadupp_t;
908 #else
909 typedef NPP_ShutdownProcPtr unloadupp_t;
910 #endif
911
912
913 main_return_t main(NPNetscapeFuncs* nsTable, NPPluginFuncs* pluginFuncs, unloadupp_t* unloadUpp);
914
915 #if !TARGET_API_MAC_CARBON
916 #pragma export on
917
918 #if TARGET_RT_MAC_CFM
919
920 RoutineDescriptor mainRD = BUILD_ROUTINE_DESCRIPTOR(uppNPP_MainEntryProcInfo, main);
921
922 #endif
923
924 #pragma export off
925 #endif /* !TARGET_API_MAC_CARBON */
926
927 DEFINE_API_C(main_return_t) main(NPNetscapeFuncs* nsTable, NPPluginFuncs* pluginFuncs, unloadupp_t* unloadUpp)
928 {
929     EnterCodeResource();
930     PLUGINDEBUGSTR("\pmain");
931
932 #ifdef __MWERKS__
933     __InitCode__();
934 #endif
935
936     NPError err = NPERR_NO_ERROR;
937
938     //
939     // Ensure that everything Netscape passed us is valid!
940     //
941     if ((nsTable == NULL) || (pluginFuncs == NULL) || (unloadUpp == NULL))
942         err = NPERR_INVALID_FUNCTABLE_ERROR;
943
944     //
945     // Check the “major” version passed in Netscape’s function table.
946     // We won’t load if the major version is newer than what we expect.
947     // Also check that the function tables passed in are big enough for
948     // all the functions we need (they could be bigger, if Netscape added
949     // new APIs, but that’s OK with us -- we’ll just ignore them).
950     //
951     if (err == NPERR_NO_ERROR)
952     {
953         if ((nsTable->version >> 8) > NP_VERSION_MAJOR)     // Major version is in high byte
954             err = NPERR_INCOMPATIBLE_VERSION_ERROR;
955     }
956
957     if (err == NPERR_NO_ERROR)
958     {
959         //
960         // Copy all the fields of Netscape’s function table into our
961         // copy so we can call back into Netscape later.  Note that
962         // we need to copy the fields one by one, rather than assigning
963         // the whole structure, because the Netscape function table
964         // could actually be bigger than what we expect.
965         //
966
967         int navMinorVers = nsTable->version & 0xFF;
968
969         gNetscapeFuncs.version          = nsTable->version;
970         gNetscapeFuncs.size             = nsTable->size;
971         gNetscapeFuncs.posturl          = HOST_TO_PLUGIN_GLUE(posturl, nsTable->posturl);
972         gNetscapeFuncs.geturl           = HOST_TO_PLUGIN_GLUE(geturl, nsTable->geturl);
973         gNetscapeFuncs.requestread      = HOST_TO_PLUGIN_GLUE(requestread, nsTable->requestread);
974         gNetscapeFuncs.newstream        = HOST_TO_PLUGIN_GLUE(newstream, nsTable->newstream);
975         gNetscapeFuncs.write            = HOST_TO_PLUGIN_GLUE(write, nsTable->write);
976         gNetscapeFuncs.destroystream    = HOST_TO_PLUGIN_GLUE(destroystream, nsTable->destroystream);
977         gNetscapeFuncs.status           = HOST_TO_PLUGIN_GLUE(status, nsTable->status);
978         gNetscapeFuncs.uagent           = HOST_TO_PLUGIN_GLUE(uagent, nsTable->uagent);
979         gNetscapeFuncs.memalloc         = HOST_TO_PLUGIN_GLUE(memalloc, nsTable->memalloc);
980         gNetscapeFuncs.memfree          = HOST_TO_PLUGIN_GLUE(memfree, nsTable->memfree);
981         gNetscapeFuncs.memflush         = HOST_TO_PLUGIN_GLUE(memflush, nsTable->memflush);
982         gNetscapeFuncs.reloadplugins    = HOST_TO_PLUGIN_GLUE(reloadplugins, nsTable->reloadplugins);
983         if( navMinorVers >= NPVERS_HAS_LIVECONNECT )
984         {
985             gNetscapeFuncs.getJavaEnv   = HOST_TO_PLUGIN_GLUE(getJavaEnv, nsTable->getJavaEnv);
986             gNetscapeFuncs.getJavaPeer  = HOST_TO_PLUGIN_GLUE(getJavaPeer, nsTable->getJavaPeer);
987         }
988         if( navMinorVers >= NPVERS_HAS_NOTIFICATION )
989         {
990             gNetscapeFuncs.geturlnotify = HOST_TO_PLUGIN_GLUE(geturlnotify, nsTable->geturlnotify);
991             gNetscapeFuncs.posturlnotify    = HOST_TO_PLUGIN_GLUE(posturlnotify, nsTable->posturlnotify);
992         }
993         gNetscapeFuncs.getvalue         = HOST_TO_PLUGIN_GLUE(getvalue, nsTable->getvalue);
994         gNetscapeFuncs.setvalue         = HOST_TO_PLUGIN_GLUE(setvalue, nsTable->setvalue);
995         gNetscapeFuncs.invalidaterect   = HOST_TO_PLUGIN_GLUE(invalidaterect, nsTable->invalidaterect);
996         gNetscapeFuncs.invalidateregion = HOST_TO_PLUGIN_GLUE(invalidateregion, nsTable->invalidateregion);
997         gNetscapeFuncs.forceredraw      = HOST_TO_PLUGIN_GLUE(forceredraw, nsTable->forceredraw);
998         if( navMinorVers >= 14 )
999         {
1000             // NPRuntime support
1001             gNetscapeFuncs.getstringidentifier  = HOST_TO_PLUGIN_GLUE(getstringidentifier, nsTable->getstringidentifier);
1002             gNetscapeFuncs.getstringidentifiers = HOST_TO_PLUGIN_GLUE(getstringidentifiers, nsTable->getstringidentifiers);
1003             gNetscapeFuncs.getintidentifier     = HOST_TO_PLUGIN_GLUE(getintidentifier, nsTable->getintidentifier);
1004             gNetscapeFuncs.identifierisstring   = HOST_TO_PLUGIN_GLUE(identifierisstring, nsTable->identifierisstring);
1005             gNetscapeFuncs.utf8fromidentifier   = HOST_TO_PLUGIN_GLUE(utf8fromidentifier, nsTable->utf8fromidentifier);
1006             gNetscapeFuncs.intfromidentifier    = HOST_TO_PLUGIN_GLUE(intfromidentifier, nsTable->intfromidentifier);
1007             gNetscapeFuncs.createobject         = HOST_TO_PLUGIN_GLUE(createobject, nsTable->createobject);
1008             gNetscapeFuncs.retainobject         = HOST_TO_PLUGIN_GLUE(retainobject, nsTable->retainobject);
1009             gNetscapeFuncs.releaseobject        = HOST_TO_PLUGIN_GLUE(releaseobject, nsTable->releaseobject);
1010             gNetscapeFuncs.invoke               = HOST_TO_PLUGIN_GLUE(invoke, nsTable->invoke);
1011             gNetscapeFuncs.invokeDefault        = HOST_TO_PLUGIN_GLUE(invokeDefault, nsTable->invokeDefault);
1012             gNetscapeFuncs.evaluate             = HOST_TO_PLUGIN_GLUE(evaluate, nsTable->evaluate);
1013             gNetscapeFuncs.getproperty          = HOST_TO_PLUGIN_GLUE(getproperty, nsTable->getproperty);
1014             gNetscapeFuncs.setproperty          = HOST_TO_PLUGIN_GLUE(setproperty, nsTable->setproperty);
1015             gNetscapeFuncs.removeproperty       = HOST_TO_PLUGIN_GLUE(removeproperty, nsTable->removeproperty);
1016             gNetscapeFuncs.hasproperty          = HOST_TO_PLUGIN_GLUE(hasproperty, nsTable->hasproperty);
1017             gNetscapeFuncs.hasmethod            = HOST_TO_PLUGIN_GLUE(hasmethod, nsTable->hasmethod);
1018             gNetscapeFuncs.releasevariantvalue  = HOST_TO_PLUGIN_GLUE(releasevariantvalue, nsTable->releasevariantvalue);
1019             gNetscapeFuncs.setexception         = HOST_TO_PLUGIN_GLUE(setexception, nsTable->setexception);
1020         }
1021
1022         //
1023         // Set up the plugin function table that Netscape will use to
1024         // call us.  Netscape needs to know about our version and size
1025         // and have a UniversalProcPointer for every function we implement.
1026         //
1027         pluginFuncs->version        = (NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR;
1028         pluginFuncs->size           = sizeof(NPPluginFuncs);
1029 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
1030         pluginFuncs->newp           = NewNPP_NewProc(PLUGIN_TO_HOST_GLUE(newp, Private_New));
1031         pluginFuncs->destroy        = NewNPP_DestroyProc(PLUGIN_TO_HOST_GLUE(destroy, Private_Destroy));
1032         pluginFuncs->setwindow      = NewNPP_SetWindowProc(PLUGIN_TO_HOST_GLUE(setwindow, Private_SetWindow));
1033         pluginFuncs->newstream      = NewNPP_NewStreamProc(PLUGIN_TO_HOST_GLUE(newstream, Private_NewStream));
1034         pluginFuncs->destroystream  = NewNPP_DestroyStreamProc(PLUGIN_TO_HOST_GLUE(destroystream, Private_DestroyStream));
1035         pluginFuncs->asfile         = NewNPP_StreamAsFileProc(PLUGIN_TO_HOST_GLUE(asfile, Private_StreamAsFile));
1036         pluginFuncs->writeready     = NewNPP_WriteReadyProc(PLUGIN_TO_HOST_GLUE(writeready, Private_WriteReady));
1037         pluginFuncs->write          = NewNPP_WriteProc(PLUGIN_TO_HOST_GLUE(write, Private_Write));
1038         pluginFuncs->print          = NewNPP_PrintProc(PLUGIN_TO_HOST_GLUE(print, Private_Print));
1039         pluginFuncs->event          = NewNPP_HandleEventProc(PLUGIN_TO_HOST_GLUE(event, Private_HandleEvent));
1040         pluginFuncs->getvalue       = NewNPP_GetValueProc(PLUGIN_TO_HOST_GLUE(getvalue, Private_GetValue));
1041 #else
1042         pluginFuncs->newp           = (NPP_NewProcPtr)(PLUGIN_TO_HOST_GLUE(newp, Private_New));
1043         pluginFuncs->destroy        = (NPP_DestroyProcPtr)(PLUGIN_TO_HOST_GLUE(destroy, Private_Destroy));
1044         pluginFuncs->setwindow      = (NPP_SetWindowProcPtr)(PLUGIN_TO_HOST_GLUE(setwindow, Private_SetWindow));
1045         pluginFuncs->newstream      = (NPP_NewStreamProcPtr)(PLUGIN_TO_HOST_GLUE(newstream, Private_NewStream));
1046         pluginFuncs->destroystream  = (NPP_DestroyStreamProcPtr)(PLUGIN_TO_HOST_GLUE(destroystream, Private_DestroyStream));
1047         pluginFuncs->asfile         = (NPP_StreamAsFileProcPtr)(PLUGIN_TO_HOST_GLUE(asfile, Private_StreamAsFile));
1048         pluginFuncs->writeready     = (NPP_WriteReadyProcPtr)(PLUGIN_TO_HOST_GLUE(writeready, Private_WriteReady));
1049         pluginFuncs->write          = (NPP_WriteProcPtr)(PLUGIN_TO_HOST_GLUE(write, Private_Write));
1050         pluginFuncs->print          = (NPP_PrintProcPtr)(PLUGIN_TO_HOST_GLUE(print, Private_Print));
1051         pluginFuncs->event          = (NPP_HandleEventProcPtr)(PLUGIN_TO_HOST_GLUE(event, Private_HandleEvent));
1052         pluginFuncs->getvalue       = (NPP_GetValueProcPtr)(PLUGIN_TO_HOST_GLUE(getvalue, Private_GetValue));
1053 #endif
1054         if( navMinorVers >= NPVERS_HAS_NOTIFICATION )
1055         {
1056 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
1057             pluginFuncs->urlnotify = NewNPP_URLNotifyProc(PLUGIN_TO_HOST_GLUE(urlnotify, Private_URLNotify));
1058 #else
1059             pluginFuncs->urlnotify = (NPP_URLNotifyProcPtr)(PLUGIN_TO_HOST_GLUE(urlnotify, Private_URLNotify));
1060 #endif
1061         }
1062 #ifdef OJI
1063         if( navMinorVers >= NPVERS_HAS_LIVECONNECT )
1064         {
1065             pluginFuncs->javaClass  = (JRIGlobalRef) Private_GetJavaClass();
1066         }
1067 #else
1068         pluginFuncs->javaClass = NULL;
1069 #endif
1070 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
1071         *unloadUpp = NewNPP_ShutdownProc(PLUGIN_TO_HOST_GLUE(shutdown, Private_Shutdown));
1072 #else
1073         *unloadUpp = (NPP_ShutdownProcPtr)(PLUGIN_TO_HOST_GLUE(shutdown, Private_Shutdown));
1074 #endif
1075         SetUpQD();
1076         err = Private_Initialize();
1077     }
1078
1079     ExitCodeResource();
1080     return err;
1081 }
1082
1083 #ifdef __MACH__
1084
1085 /*
1086 ** netscape plugins functions when building Mach-O binary
1087 */
1088
1089 extern "C" {
1090     NPError NP_Initialize(NPNetscapeFuncs* nsTable);
1091     NPError NP_GetEntryPoints(NPPluginFuncs* pluginFuncs);
1092     NPError NP_Shutdown(void);
1093 }
1094
1095 /*
1096 ** netscape plugins functions when using Mach-O binary
1097 */
1098
1099 NPError NP_Initialize(NPNetscapeFuncs* nsTable)
1100 {
1101     PLUGINDEBUGSTR("\pNP_Initialize");
1102
1103     /* validate input parameters */
1104
1105     if( NULL == nsTable  )
1106         return NPERR_INVALID_FUNCTABLE_ERROR;
1107
1108     /*
1109      * Check the major version passed in Netscape's function table.
1110      * We won't load if the major version is newer than what we expect.
1111      * Also check that the function tables passed in are big enough for
1112      * all the functions we need (they could be bigger, if Netscape added
1113      * new APIs, but that's OK with us -- we'll just ignore them).
1114      *
1115      */
1116
1117     if ((nsTable->version >> 8) > NP_VERSION_MAJOR)
1118         return NPERR_INCOMPATIBLE_VERSION_ERROR;
1119
1120     if (nsTable->size < sizeof(NPNetscapeFuncs))
1121         return NPERR_INVALID_FUNCTABLE_ERROR;
1122
1123     int navMinorVers = nsTable->version & 0xFF;
1124
1125     /*
1126      * Copy all the fields of Netscape function table into our
1127      * copy so we can call back into Netscape later.  Note that
1128      * we need to copy the fields one by one, rather than assigning
1129      * the whole structure, because the Netscape function table
1130      * could actually be bigger than what we expect.
1131      */
1132     gNetscapeFuncs.version       = nsTable->version;
1133     gNetscapeFuncs.size          = nsTable->size;
1134     gNetscapeFuncs.posturl       = nsTable->posturl;
1135     gNetscapeFuncs.geturl        = nsTable->geturl;
1136     gNetscapeFuncs.requestread   = nsTable->requestread;
1137     gNetscapeFuncs.newstream     = nsTable->newstream;
1138     gNetscapeFuncs.write         = nsTable->write;
1139     gNetscapeFuncs.destroystream = nsTable->destroystream;
1140     gNetscapeFuncs.status        = nsTable->status;
1141     gNetscapeFuncs.uagent        = nsTable->uagent;
1142     gNetscapeFuncs.memalloc      = nsTable->memalloc;
1143     gNetscapeFuncs.memfree       = nsTable->memfree;
1144     gNetscapeFuncs.memflush      = nsTable->memflush;
1145     gNetscapeFuncs.reloadplugins = nsTable->reloadplugins;
1146     if( navMinorVers >= NPVERS_HAS_LIVECONNECT )
1147     {
1148         gNetscapeFuncs.getJavaEnv   = nsTable->getJavaEnv;
1149         gNetscapeFuncs.getJavaPeer  = nsTable->getJavaPeer;
1150     }
1151     if( navMinorVers >= NPVERS_HAS_NOTIFICATION )
1152     {
1153         gNetscapeFuncs.geturlnotify     = nsTable->geturlnotify;
1154         gNetscapeFuncs.posturlnotify    = nsTable->posturlnotify;
1155     }
1156
1157     gNetscapeFuncs.getvalue         = nsTable->getvalue;
1158     gNetscapeFuncs.setvalue         = nsTable->setvalue;
1159     gNetscapeFuncs.invalidaterect   = nsTable->invalidaterect;
1160     gNetscapeFuncs.invalidateregion = nsTable->invalidateregion;
1161     gNetscapeFuncs.forceredraw      = nsTable->forceredraw;
1162     if( navMinorVers >= 14 )
1163     {
1164         // NPRuntime support
1165         gNetscapeFuncs.getstringidentifier  = nsTable->getstringidentifier;
1166         gNetscapeFuncs.getstringidentifiers = nsTable->getstringidentifiers;
1167         gNetscapeFuncs.getintidentifier     = nsTable->getintidentifier;
1168         gNetscapeFuncs.identifierisstring   = nsTable->identifierisstring;
1169         gNetscapeFuncs.utf8fromidentifier   = nsTable->utf8fromidentifier;
1170         gNetscapeFuncs.intfromidentifier    = nsTable->intfromidentifier;
1171         gNetscapeFuncs.createobject         = nsTable->createobject;
1172         gNetscapeFuncs.retainobject         = nsTable->retainobject;
1173         gNetscapeFuncs.releaseobject        = nsTable->releaseobject;
1174         gNetscapeFuncs.invoke               = nsTable->invoke;
1175         gNetscapeFuncs.invokeDefault        = nsTable->invokeDefault;
1176         gNetscapeFuncs.evaluate             = nsTable->evaluate;
1177         gNetscapeFuncs.getproperty          = nsTable->getproperty;
1178         gNetscapeFuncs.setproperty          = nsTable->setproperty;
1179         gNetscapeFuncs.removeproperty       = nsTable->removeproperty;
1180         gNetscapeFuncs.hasproperty          = nsTable->hasproperty;
1181         gNetscapeFuncs.hasmethod            = nsTable->hasmethod;
1182         gNetscapeFuncs.releasevariantvalue  = nsTable->releasevariantvalue;
1183         gNetscapeFuncs.setexception         = nsTable->setexception;
1184     }
1185     return NPP_Initialize();
1186 }
1187
1188 NPError NP_GetEntryPoints(NPPluginFuncs* pluginFuncs)
1189 {
1190     int navMinorVers = gNetscapeFuncs.version & 0xFF;
1191
1192     PLUGINDEBUGSTR("\pNP_GetEntryPoints");
1193
1194     if( pluginFuncs == NULL )
1195         return NPERR_INVALID_FUNCTABLE_ERROR;
1196
1197     /*if (pluginFuncs->size < sizeof(NPPluginFuncs))
1198     return NPERR_INVALID_FUNCTABLE_ERROR;*/
1199
1200     /*
1201      * Set up the plugin function table that Netscape will use to
1202      * call us.  Netscape needs to know about our version and size
1203      * and have a UniversalProcPointer for every function we
1204      * implement.
1205      */
1206
1207     pluginFuncs->version    = (NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR;
1208     pluginFuncs->size       = sizeof(NPPluginFuncs);
1209 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
1210     pluginFuncs->newp       = NewNPP_NewProc(Private_New);
1211     pluginFuncs->destroy    = NewNPP_DestroyProc(Private_Destroy);
1212     pluginFuncs->setwindow  = NewNPP_SetWindowProc(Private_SetWindow);
1213     pluginFuncs->newstream  = NewNPP_NewStreamProc(Private_NewStream);
1214     pluginFuncs->destroystream = NewNPP_DestroyStreamProc(Private_DestroyStream);
1215     pluginFuncs->asfile     = NewNPP_StreamAsFileProc(Private_StreamAsFile);
1216     pluginFuncs->writeready = NewNPP_WriteReadyProc(Private_WriteReady);
1217     pluginFuncs->write      = NewNPP_WriteProc(Private_Write);
1218     pluginFuncs->print      = NewNPP_PrintProc(Private_Print);
1219     pluginFuncs->event      = NewNPP_HandleEventProc(Private_HandleEvent);
1220     pluginFuncs->getvalue   = NewNPP_GetValueProc(Private_GetValue);
1221     pluginFuncs->setvalue   = NewNPP_SetValueProc(Private_SetValue);
1222 #else
1223     pluginFuncs->newp       = (NPP_NewProcPtr)(Private_New);
1224     pluginFuncs->destroy    = (NPP_DestroyProcPtr)(Private_Destroy);
1225     pluginFuncs->setwindow  = (NPP_SetWindowProcPtr)(Private_SetWindow);
1226     pluginFuncs->newstream  = (NPP_NewStreamProcPtr)(Private_NewStream);
1227     pluginFuncs->destroystream = (NPP_DestroyStreamProcPtr)(Private_DestroyStream);
1228     pluginFuncs->asfile     = (NPP_StreamAsFileProcPtr)(Private_StreamAsFile);
1229     pluginFuncs->writeready = (NPP_WriteReadyProcPtr)(Private_WriteReady);
1230     pluginFuncs->write      = (NPP_WriteProcPtr)(Private_Write);
1231     pluginFuncs->print      = (NPP_PrintProcPtr)(Private_Print);
1232     pluginFuncs->event      = (NPP_HandleEventProcPtr)(Private_HandleEvent);
1233     pluginFuncs->getvalue   = (NPP_GetValueProcPtr)(Private_GetValue);
1234     pluginFuncs->setvalue   = (NPP_SetValueProcPtr)(Private_SetValue);
1235 #endif
1236     if( navMinorVers >= NPVERS_HAS_NOTIFICATION )
1237     {
1238         pluginFuncs->urlnotify = Private_URLNotify;
1239     }
1240 #ifdef OJI
1241     if( navMinorVers >= NPVERS_HAS_LIVECONNECT )
1242     {
1243         pluginFuncs->javaClass  = (JRIGlobalRef) Private_GetJavaClass();
1244     }
1245 #else
1246     pluginFuncs->javaClass = NULL;
1247 #endif
1248
1249     return NPERR_NO_ERROR;
1250 }
1251
1252 NPError NP_Shutdown(void)
1253 {
1254     PLUGINDEBUGSTR("\pNP_Shutdown");
1255     NPP_Shutdown();
1256     return NPERR_NO_ERROR;
1257 }
1258
1259 #endif