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