]> git.sesse.net Git - vlc/blob - mozilla/support/npwin.cpp
b1eb666d9cd1bc0162744f705ddd31c143584f50
[vlc] / mozilla / support / npwin.cpp
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
4  *
5  * The contents of this file are subject to the Netscape Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License at
8  * http://www.mozilla.org/NPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  *
15  * The Original Code is Mozilla Communicator client code.
16  *
17  * The Initial Developer of the Original Code is 
18  * Netscape Communications Corporation.
19  * Portions created by the Initial Developer are Copyright (C) 1998
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either the GNU General Public License Version 2 or later (the "GPL"), or 
26  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the NPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the NPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */
37
38 #include "config.h"
39
40 #ifdef HAVE_MOZILLA_CONFIG_H
41 #   include <mozilla-config.h>
42 #endif
43
44 #include "nscore.h"
45 #include "npapi.h"
46 #include "npupp.h"
47
48 //\\// DEFINE
49 #define NP_EXPORT
50
51 //\\// GLOBAL DATA
52 NPNetscapeFuncs* g_pNavigatorFuncs = 0;
53 JRIGlobalRef Private_GetJavaClass(void);
54
55 //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\.
56 ////\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//.
57 // Private_GetJavaClass (global function)
58 //
59 //      Given a Java class reference (thru NPP_GetJavaClass) inform JRT
60 //      of this class existence
61 //
62 JRIGlobalRef
63 Private_GetJavaClass(void)
64 {
65     jref clazz = NPP_GetJavaClass();
66     if (clazz) {
67         JRIEnv* env = NPN_GetJavaEnv();
68         return JRI_NewGlobalRef(env, clazz);
69     }
70     return NULL;
71 }
72
73 //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\.
74 ////\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//.
75 //                                              PLUGIN DLL entry points   
76 //
77 // These are the Windows specific DLL entry points. They must be exoprted
78 //
79
80 // we need these to be global since we have to fill one of its field
81 // with a data (class) which requires knowlwdge of the navigator
82 // jump-table. This jump table is known at Initialize time (NP_Initialize)
83 // which is called after NP_GetEntryPoint
84 static NPPluginFuncs* g_pluginFuncs;
85
86 //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\.
87 ////\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//.
88 // NP_GetEntryPoints
89 //
90 //      fills in the func table used by Navigator to call entry points in
91 //  plugin DLL.  Note that these entry points ensure that DS is loaded
92 //  by using the NP_LOADDS macro, when compiling for Win16
93 //
94 #ifdef __MINGW32__
95 extern "C" __declspec(dllexport) NPError WINAPI
96 #else
97 NPError WINAPI NP_EXPORT
98 #endif
99 NP_GetEntryPoints(NPPluginFuncs* pFuncs)
100 {
101     // trap a NULL ptr 
102     if(pFuncs == NULL)
103         return NPERR_INVALID_FUNCTABLE_ERROR;
104
105     // if the plugin's function table is smaller than the plugin expects,
106     // then they are incompatible, and should return an error 
107
108     pFuncs->version       = (NP_VERSION_MAJOR << 8) | NP_VERSION_MINOR;
109     pFuncs->newp          = NPP_New;
110     pFuncs->destroy       = NPP_Destroy;
111     pFuncs->setwindow     = NPP_SetWindow;
112     pFuncs->newstream     = NPP_NewStream;
113     pFuncs->destroystream = NPP_DestroyStream;
114     pFuncs->asfile        = NPP_StreamAsFile;
115     pFuncs->writeready    = NPP_WriteReady;
116     pFuncs->write         = NPP_Write;
117     pFuncs->print         = NPP_Print;
118     pFuncs->event         = 0;       /// reserved 
119     pFuncs->getvalue      = NPP_GetValue;
120     pFuncs->setvalue      = NPP_SetValue;
121
122     g_pluginFuncs             = pFuncs;
123
124     return NPERR_NO_ERROR;
125 }
126
127 //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\.
128 ////\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//.
129 // NP_Initialize
130 //
131 //      called immediately after the plugin DLL is loaded
132 //
133 #ifdef __MINGW32__
134 extern "C" __declspec(dllexport) NPError WINAPI
135 #else
136 NPError WINAPI NP_EXPORT 
137 #endif
138 NP_Initialize(NPNetscapeFuncs* pFuncs)
139 {
140     // trap a NULL ptr 
141     if(pFuncs == NULL)
142         return NPERR_INVALID_FUNCTABLE_ERROR;
143
144     g_pNavigatorFuncs = pFuncs; // save it for future reference 
145
146     // if the plugin's major ver level is lower than the Navigator's,
147     // then they are incompatible, and should return an error 
148     if(HIBYTE(pFuncs->version) > NP_VERSION_MAJOR)
149         return NPERR_INCOMPATIBLE_VERSION_ERROR;
150
151     // We have to defer these assignments until g_pNavigatorFuncs is set
152     int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
153
154     if( navMinorVers >= NPVERS_HAS_NOTIFICATION ) {
155         g_pluginFuncs->urlnotify = NPP_URLNotify;
156     }
157     
158     if( navMinorVers >= NPVERS_HAS_LIVECONNECT ) {
159         g_pluginFuncs->javaClass = Private_GetJavaClass();
160     }
161
162     // NPP_Initialize is a standard (cross-platform) initialize function.
163     return NPP_Initialize();
164 }
165
166 //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\.
167 ////\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//.
168 // NP_Shutdown
169 //
170 //      called immediately before the plugin DLL is unloaded.
171 //      This functio shuold check for some ref count on the dll to see if it is
172 //      unloadable or it needs to stay in memory. 
173 //
174 #ifdef __MINGW32__
175 extern "C" __declspec(dllexport) NPError WINAPI
176 #else
177 NPError WINAPI NP_EXPORT 
178 #endif
179 NP_Shutdown()
180 {
181     NPP_Shutdown();
182     g_pNavigatorFuncs = NULL;
183     return NPERR_NO_ERROR;
184 }
185
186 //                                              END - PLUGIN DLL entry points   
187 ////\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//.
188 //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\.
189
190 /*    NAVIGATOR Entry points    */
191
192 /* These entry points expect to be called from within the plugin.  The
193    noteworthy assumption is that DS has already been set to point to the
194    plugin's DLL data segment.  Don't call these functions from outside
195    the plugin without ensuring DS is set to the DLLs data segment first,
196    typically using the NP_LOADDS macro
197 */
198
199 /* returns the major/minor version numbers of the Plugin API for the plugin
200    and the Navigator
201 */
202 void NPN_Version(int* plugin_major, int* plugin_minor, int* netscape_major, int* netscape_minor)
203 {
204     *plugin_major   = NP_VERSION_MAJOR;
205     *plugin_minor   = NP_VERSION_MINOR;
206     *netscape_major = HIBYTE(g_pNavigatorFuncs->version);
207     *netscape_minor = LOBYTE(g_pNavigatorFuncs->version);
208 }
209
210 /* causes the specified URL to be fetched and streamed in
211 */
212 NPError NPN_GetURLNotify(NPP instance, const char *url, const char *target, void* notifyData)
213
214 {
215     int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
216     NPError err;
217     if( navMinorVers >= NPVERS_HAS_NOTIFICATION ) {
218         err = g_pNavigatorFuncs->geturlnotify(instance, url, target, notifyData);
219     }
220     else {
221         err = NPERR_INCOMPATIBLE_VERSION_ERROR;
222     }
223     return err;
224 }
225
226
227 NPError NPN_GetURL(NPP instance, const char *url, const char *target)
228 {
229     return g_pNavigatorFuncs->geturl(instance, url, target);
230 }
231
232 NPError NPN_PostURLNotify(NPP instance, const char* url, const char* window, uint32 len, const char* buf, NPBool file, void* notifyData)
233 {
234     int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
235     NPError err;
236     if( navMinorVers >= NPVERS_HAS_NOTIFICATION ) {
237         err = g_pNavigatorFuncs->posturlnotify(instance, url, window, len, buf, file, notifyData);
238     }
239     else {
240         err = NPERR_INCOMPATIBLE_VERSION_ERROR;
241     }
242     return err;
243 }
244
245
246 NPError NPN_PostURL(NPP instance, const char* url, const char* window, uint32 len, const char* buf, NPBool file)
247 {
248     return g_pNavigatorFuncs->posturl(instance, url, window, len, buf, file);
249 }
250
251 /* Requests that a number of bytes be provided on a stream.  Typically
252    this would be used if a stream was in "pull" mode.  An optional
253    position can be provided for streams which are seekable.
254 */
255 NPError NPN_RequestRead(NPStream* stream, NPByteRange* rangeList)
256 {
257     return g_pNavigatorFuncs->requestread(stream, rangeList);
258 }
259
260 /* Creates a new stream of data from the plug-in to be interpreted
261    by Netscape in the current window.
262 */
263 NPError NPN_NewStream(NPP instance, NPMIMEType type, 
264                                                                 const char* target, NPStream** stream)
265 {
266     int navMinorVersion = g_pNavigatorFuncs->version & 0xFF;
267     NPError err;
268
269     if( navMinorVersion >= NPVERS_HAS_STREAMOUTPUT ) {
270         err = g_pNavigatorFuncs->newstream(instance, type, target, stream);
271     }
272     else {
273         err = NPERR_INCOMPATIBLE_VERSION_ERROR;
274     }
275     return err;
276 }
277
278 /* Provides len bytes of data.
279 */
280 int32 NPN_Write(NPP instance, NPStream *stream,
281                 int32 len, void *buffer)
282 {
283     int navMinorVersion = g_pNavigatorFuncs->version & 0xFF;
284     int32 result;
285
286     if( navMinorVersion >= NPVERS_HAS_STREAMOUTPUT ) {
287         result = g_pNavigatorFuncs->write(instance, stream, len, buffer);
288     }
289     else {
290         result = -1;
291     }
292     return result;
293 }
294
295 /* Closes a stream object.  
296 reason indicates why the stream was closed.
297 */
298 NPError NPN_DestroyStream(NPP instance, NPStream* stream, NPError reason)
299 {
300     int navMinorVersion = g_pNavigatorFuncs->version & 0xFF;
301     NPError err;
302
303     if( navMinorVersion >= NPVERS_HAS_STREAMOUTPUT ) {
304         err = g_pNavigatorFuncs->destroystream(instance, stream, reason);
305     }
306     else {
307         err = NPERR_INCOMPATIBLE_VERSION_ERROR;
308     }
309     return err;
310 }
311
312 /* Provides a text status message in the Netscape client user interface
313 */
314 void NPN_Status(NPP instance, const char *message)
315 {
316     g_pNavigatorFuncs->status(instance, message);
317 }
318
319 /* returns the user agent string of Navigator, which contains version info
320 */
321 const char* NPN_UserAgent(NPP instance)
322 {
323     return g_pNavigatorFuncs->uagent(instance);
324 }
325
326 /* allocates memory from the Navigator's memory space.  Necessary so that
327    saved instance data may be freed by Navigator when exiting.
328 */
329
330
331 void* NPN_MemAlloc(uint32 size)
332 {
333     return g_pNavigatorFuncs->memalloc(size);
334 }
335
336 /* reciprocal of MemAlloc() above
337 */
338 void NPN_MemFree(void* ptr)
339 {
340     g_pNavigatorFuncs->memfree(ptr);
341 }
342
343 /* private function to Netscape.  do not use!
344 */
345 void NPN_ReloadPlugins(NPBool reloadPages)
346 {
347     g_pNavigatorFuncs->reloadplugins(reloadPages);
348 }
349
350 JRIEnv* NPN_GetJavaEnv(void)
351 {
352     return g_pNavigatorFuncs->getJavaEnv();
353 }
354
355 jref NPN_GetJavaPeer(NPP instance)
356 {
357     return g_pNavigatorFuncs->getJavaPeer(instance);
358 }
359
360 NPError NPN_GetValue(NPP instance, NPNVariable variable, void *result)
361 {
362     return g_pNavigatorFuncs->getvalue(instance, variable, result);
363 }
364
365 NPError NPN_SetValue(NPP instance, NPPVariable variable, void *value)
366 {
367     return g_pNavigatorFuncs->setvalue(instance, variable, value);
368 }
369
370 void NPN_InvalidateRect(NPP instance, NPRect *rect)
371 {
372     g_pNavigatorFuncs->invalidaterect(instance, rect);
373 }
374
375 void NPN_InvalidateRegion(NPP instance, NPRegion region)
376 {
377     g_pNavigatorFuncs->invalidateregion(instance, region);
378 }
379
380 void NPN_ForceRedraw(NPP instance)
381 {
382     g_pNavigatorFuncs->forceredraw(instance);
383 }
384
385 NPIdentifier NPN_GetStringIdentifier(const NPUTF8 *name)
386 {
387     int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
388     if( navMinorVers >= 14 )
389     {   
390         return g_pNavigatorFuncs->getstringidentifier(name);
391     }
392     return NULL;
393 }
394
395 void NPN_GetStringIdentifiers(const NPUTF8 **names, int32_t nameCount, NPIdentifier *identifiers)
396 {
397     int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
398     if( navMinorVers >= 14 )
399     {   
400         g_pNavigatorFuncs->getstringidentifiers(names, nameCount, identifiers);
401     }
402 }
403
404 NPIdentifier NPN_GetIntIdentifier(int32_t intid)
405 {
406     int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
407     if( navMinorVers >= 14 )
408     {   
409         return g_pNavigatorFuncs->getintidentifier(intid);
410     }
411     return NULL;
412 }
413
414 bool NPN_IdentifierIsString(NPIdentifier identifier)
415 {
416     int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
417     if( navMinorVers >= 14 )
418     {   
419         return g_pNavigatorFuncs->identifierisstring(identifier);
420     }
421     return false;
422 }
423
424 NPUTF8 *NPN_UTF8FromIdentifier(NPIdentifier identifier)
425 {
426     int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
427     if( navMinorVers >= 14 )
428     {   
429         return g_pNavigatorFuncs->utf8fromidentifier(identifier);
430     }
431     return NULL;
432 }
433
434 int32_t NPN_IntFromIdentifier(NPIdentifier identifier)
435 {
436     int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
437     if( navMinorVers >= 14 )
438     {   
439         return g_pNavigatorFuncs->intfromidentifier(identifier);
440     }
441     return 0;
442 }
443
444 NPObject *NPN_CreateObject(NPP instance, NPClass *aClass)
445 {
446     int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
447     if( navMinorVers >= 14 )
448     {   
449         return g_pNavigatorFuncs->createobject(instance, aClass);
450     }
451     return NULL;
452 }
453
454 NPObject *NPN_RetainObject(NPObject *npobj)
455 {
456     int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
457     if( navMinorVers >= 14 )
458     {   
459         return g_pNavigatorFuncs->retainobject(npobj);
460     }
461     return NULL;
462 }
463
464 void NPN_ReleaseObject(NPObject *npobj)
465 {
466     int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
467     if( navMinorVers >= 14 )
468     {   
469         g_pNavigatorFuncs->releaseobject(npobj);
470     }
471 }
472
473 bool NPN_Invoke(NPP instance, NPObject *npobj, NPIdentifier methodName, const NPVariant *args, uint32_t argCount, NPVariant *result)
474 {
475     int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
476     if( navMinorVers >= 14 )
477     {   
478         return g_pNavigatorFuncs->invoke(instance, npobj, methodName, args, argCount, result);
479     }
480     return false;
481 }
482
483 bool NPN_InvokeDefault(NPP instance, NPObject *npobj, const NPVariant *args, uint32_t argCount, NPVariant *result)
484 {
485     int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
486     if( navMinorVers >= 14 )
487     {   
488         return g_pNavigatorFuncs->invokeDefault(instance, npobj, args, argCount, result);
489     }
490     return false;
491 }
492
493 bool NPN_Evaluate(NPP instance, NPObject *npobj, NPString *script, NPVariant *result)
494 {
495     int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
496     if( navMinorVers >= 14 )
497     {   
498         return g_pNavigatorFuncs->evaluate(instance, npobj, script, result);
499     }
500     return false;
501 }
502
503 bool NPN_GetProperty(NPP instance, NPObject *npobj, NPIdentifier propertyName, NPVariant *result)
504 {
505     int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
506     if( navMinorVers >= 14 )
507     {   
508         return g_pNavigatorFuncs->getproperty(instance, npobj, propertyName, result);
509     }
510     return false;
511 }
512
513 bool NPN_SetProperty(NPP instance, NPObject *npobj, NPIdentifier propertyName, const NPVariant *value)
514 {
515     int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
516     if( navMinorVers >= 14 )
517     {   
518         return g_pNavigatorFuncs->setproperty(instance, npobj, propertyName, value);
519     }
520     return false;
521 }
522
523 bool NPN_RemoveProperty(NPP instance, NPObject *npobj, NPIdentifier propertyName)
524 {
525     int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
526     if( navMinorVers >= 14 )
527     {   
528         return g_pNavigatorFuncs->removeproperty(instance, npobj, propertyName);
529     }
530     return false;
531 }
532
533 bool NPN_HasProperty(NPP instance, NPObject *npobj, NPIdentifier propertyName)
534 {
535     int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
536     if( navMinorVers >= 14 )
537     {   
538         return g_pNavigatorFuncs->hasproperty(instance, npobj, propertyName);
539     }
540     return false;
541 }
542
543 bool NPN_HasMethod(NPP instance, NPObject *npobj, NPIdentifier methodName)
544 {
545     int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
546     if( navMinorVers >= 14 )
547     {   
548         return g_pNavigatorFuncs->hasmethod(instance, npobj, methodName);
549     }
550     return false;
551 }
552
553 void NPN_ReleaseVariantValue(NPVariant *variant)
554 {
555     int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
556     if( navMinorVers >= 14 )
557     {   
558         g_pNavigatorFuncs->releasevariantvalue(variant);
559     }
560 }
561
562 void NPN_SetException(NPObject *npobj, const NPUTF8 *message)
563 {
564     int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
565     if( navMinorVers >= 14 )
566     {   
567         g_pNavigatorFuncs->setexception(npobj, message);
568     }
569 }
570