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