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