]> git.sesse.net Git - vlc/blob - mozilla/support/npwin.cpp
* configure.ac, mozilla/*: the win32 mozilla plugin compiles again (most likely won...
[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 "nscore.h"
39 #include "npapi.h"
40 #include "npupp.h"
41
42 //\\// DEFINE
43 #define NP_EXPORT
44
45 //\\// GLOBAL DATA
46 NPNetscapeFuncs* g_pNavigatorFuncs = 0;
47 JRIGlobalRef Private_GetJavaClass(void);
48
49 //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\.
50 ////\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//.
51 // Private_GetJavaClass (global function)
52 //
53 //      Given a Java class reference (thru NPP_GetJavaClass) inform JRT
54 //      of this class existence
55 //
56 JRIGlobalRef
57 Private_GetJavaClass(void)
58 {
59     jref clazz = NPP_GetJavaClass();
60     if (clazz) {
61                 JRIEnv* env = NPN_GetJavaEnv();
62                 return JRI_NewGlobalRef(env, clazz);
63     }
64     return NULL;
65 }
66
67 //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\.
68 ////\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//.
69 //                                              PLUGIN DLL entry points   
70 //
71 // These are the Windows specific DLL entry points. They must be exoprted
72 //
73
74 // we need these to be global since we have to fill one of its field
75 // with a data (class) which requires knowlwdge of the navigator
76 // jump-table. This jump table is known at Initialize time (NP_Initialize)
77 // which is called after NP_GetEntryPoint
78 static NPPluginFuncs* g_pluginFuncs;
79
80 //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\.
81 ////\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//.
82 // NP_GetEntryPoints
83 //
84 //      fills in the func table used by Navigator to call entry points in
85 //  plugin DLL.  Note that these entry points ensure that DS is loaded
86 //  by using the NP_LOADDS macro, when compiling for Win16
87 //
88 #ifdef __MINGW32__
89 extern "C" NPError
90 #else
91 NPError WINAPI NP_EXPORT
92 #endif
93 NP_GetEntryPoints(NPPluginFuncs* pFuncs)
94 {
95     // trap a NULL ptr 
96     if(pFuncs == NULL)
97         return NPERR_INVALID_FUNCTABLE_ERROR;
98
99     // if the plugin's function table is smaller than the plugin expects,
100     // then they are incompatible, and should return an error 
101
102     pFuncs->version       = (NP_VERSION_MAJOR << 8) | NP_VERSION_MINOR;
103     pFuncs->newp          = NPP_New;
104     pFuncs->destroy       = NPP_Destroy;
105     pFuncs->setwindow     = NPP_SetWindow;
106     pFuncs->newstream     = NPP_NewStream;
107     pFuncs->destroystream = NPP_DestroyStream;
108     pFuncs->asfile        = NPP_StreamAsFile;
109     pFuncs->writeready    = NPP_WriteReady;
110     pFuncs->write         = NPP_Write;
111     pFuncs->print         = NPP_Print;
112     pFuncs->getvalue      = NPP_GetValue;
113     pFuncs->event         = 0;       /// reserved 
114
115         g_pluginFuncs             = pFuncs;
116
117     return NPERR_NO_ERROR;
118 }
119
120 //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\.
121 ////\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//.
122 // NP_Initialize
123 //
124 //      called immediately after the plugin DLL is loaded
125 //
126 #ifdef __MINGW32__
127 extern "C" NPError
128 #else
129 NPError WINAPI NP_EXPORT 
130 #endif
131 NP_Initialize(NPNetscapeFuncs* pFuncs, NPPluginFuncs*)
132 {
133     // trap a NULL ptr 
134     if(pFuncs == NULL)
135         return NPERR_INVALID_FUNCTABLE_ERROR;
136
137     g_pNavigatorFuncs = pFuncs; // save it for future reference 
138
139     // if the plugin's major ver level is lower than the Navigator's,
140     // then they are incompatible, and should return an error 
141     if(HIBYTE(pFuncs->version) > NP_VERSION_MAJOR)
142         return NPERR_INCOMPATIBLE_VERSION_ERROR;
143
144         // We have to defer these assignments until g_pNavigatorFuncs is set
145     int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
146
147         if( navMinorVers >= NPVERS_HAS_NOTIFICATION ) {
148                 g_pluginFuncs->urlnotify = NPP_URLNotify;
149         }
150         
151         if( navMinorVers >= NPVERS_HAS_LIVECONNECT ) {
152                 g_pluginFuncs->javaClass = Private_GetJavaClass();
153         }
154
155         // NPP_Initialize is a standard (cross-platform) initialize function.
156     return NPP_Initialize();
157 }
158
159 //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\.
160 ////\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//.
161 // NP_Shutdown
162 //
163 //      called immediately before the plugin DLL is unloaded.
164 //      This functio shuold check for some ref count on the dll to see if it is
165 //      unloadable or it needs to stay in memory. 
166 //
167 #ifdef __MINGW32__
168 extern "C" NPError
169 #else
170 NPError WINAPI NP_EXPORT 
171 #endif
172 NP_Shutdown()
173 {
174     NPP_Shutdown();
175     g_pNavigatorFuncs = NULL;
176     return NPERR_NO_ERROR;
177 }
178
179 //                                              END - PLUGIN DLL entry points   
180 ////\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//.
181 //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\.
182
183 /*    NAVIGATOR Entry points    */
184
185 /* These entry points expect to be called from within the plugin.  The
186    noteworthy assumption is that DS has already been set to point to the
187    plugin's DLL data segment.  Don't call these functions from outside
188    the plugin without ensuring DS is set to the DLLs data segment first,
189    typically using the NP_LOADDS macro
190 */
191
192 /* returns the major/minor version numbers of the Plugin API for the plugin
193    and the Navigator
194 */
195 void NPN_Version(int* plugin_major, int* plugin_minor, int* netscape_major, int* netscape_minor)
196 {
197     *plugin_major   = NP_VERSION_MAJOR;
198     *plugin_minor   = NP_VERSION_MINOR;
199     *netscape_major = HIBYTE(g_pNavigatorFuncs->version);
200     *netscape_minor = LOBYTE(g_pNavigatorFuncs->version);
201 }
202
203 NPError NPN_GetValue(NPP instance, NPNVariable variable, void *result)
204 {
205     return g_pNavigatorFuncs->getvalue(instance, variable, result);
206 }
207
208
209 /* causes the specified URL to be fetched and streamed in
210 */
211 NPError NPN_GetURLNotify(NPP instance, const char *url, const char *target, void* notifyData)
212
213 {
214         int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
215         NPError err;
216         if( navMinorVers >= NPVERS_HAS_NOTIFICATION ) {
217                 err = g_pNavigatorFuncs->geturlnotify(instance, url, target, notifyData);
218         }
219         else {
220                 err = NPERR_INCOMPATIBLE_VERSION_ERROR;
221         }
222         return err;
223 }
224
225
226 NPError NPN_GetURL(NPP instance, const char *url, const char *target)
227 {
228     return g_pNavigatorFuncs->geturl(instance, url, target);
229 }
230
231 NPError NPN_PostURLNotify(NPP instance, const char* url, const char* window, uint32 len, const char* buf, NPBool file, void* notifyData)
232 {
233         int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
234         NPError err;
235         if( navMinorVers >= NPVERS_HAS_NOTIFICATION ) {
236                 err = g_pNavigatorFuncs->posturlnotify(instance, url, window, len, buf, file, notifyData);
237         }
238         else {
239                 err = NPERR_INCOMPATIBLE_VERSION_ERROR;
240         }
241         return err;
242 }
243
244
245 NPError NPN_PostURL(NPP instance, const char* url, const char* window, uint32 len, const char* buf, NPBool file)
246 {
247     return g_pNavigatorFuncs->posturl(instance, url, window, len, buf, file);
248 }
249
250 /* Requests that a number of bytes be provided on a stream.  Typically
251    this would be used if a stream was in "pull" mode.  An optional
252    position can be provided for streams which are seekable.
253 */
254 NPError NPN_RequestRead(NPStream* stream, NPByteRange* rangeList)
255 {
256     return g_pNavigatorFuncs->requestread(stream, rangeList);
257 }
258
259 /* Creates a new stream of data from the plug-in to be interpreted
260    by Netscape in the current window.
261 */
262 NPError NPN_NewStream(NPP instance, NPMIMEType type, 
263                                                                 const char* target, NPStream** stream)
264 {
265         int navMinorVersion = g_pNavigatorFuncs->version & 0xFF;
266         NPError err;
267
268         if( navMinorVersion >= NPVERS_HAS_STREAMOUTPUT ) {
269                 err = g_pNavigatorFuncs->newstream(instance, type, target, stream);
270         }
271         else {
272                 err = NPERR_INCOMPATIBLE_VERSION_ERROR;
273         }
274         return err;
275 }
276
277 /* Provides len bytes of data.
278 */
279 int32 NPN_Write(NPP instance, NPStream *stream,
280                 int32 len, void *buffer)
281 {
282         int navMinorVersion = g_pNavigatorFuncs->version & 0xFF;
283         int32 result;
284
285         if( navMinorVersion >= NPVERS_HAS_STREAMOUTPUT ) {
286                 result = g_pNavigatorFuncs->write(instance, stream, len, buffer);
287         }
288         else {
289                 result = -1;
290         }
291         return result;
292 }
293
294 /* Closes a stream object.  
295 reason indicates why the stream was closed.
296 */
297 NPError NPN_DestroyStream(NPP instance, NPStream* stream, NPError reason)
298 {
299         int navMinorVersion = g_pNavigatorFuncs->version & 0xFF;
300         NPError err;
301
302         if( navMinorVersion >= NPVERS_HAS_STREAMOUTPUT ) {
303                 err = g_pNavigatorFuncs->destroystream(instance, stream, reason);
304         }
305         else {
306                 err = NPERR_INCOMPATIBLE_VERSION_ERROR;
307         }
308         return err;
309 }
310
311 /* Provides a text status message in the Netscape client user interface
312 */
313 void NPN_Status(NPP instance, const char *message)
314 {
315     g_pNavigatorFuncs->status(instance, message);
316 }
317
318 /* returns the user agent string of Navigator, which contains version info
319 */
320 const char* NPN_UserAgent(NPP instance)
321 {
322     return g_pNavigatorFuncs->uagent(instance);
323 }
324
325 /* allocates memory from the Navigator's memory space.  Necessary so that
326    saved instance data may be freed by Navigator when exiting.
327 */
328
329
330 void* NPN_MemAlloc(uint32 size)
331 {
332     return g_pNavigatorFuncs->memalloc(size);
333 }
334
335 /* reciprocal of MemAlloc() above
336 */
337 void NPN_MemFree(void* ptr)
338 {
339     g_pNavigatorFuncs->memfree(ptr);
340 }
341
342 /* private function to Netscape.  do not use!
343 */
344 void NPN_ReloadPlugins(NPBool reloadPages)
345 {
346     g_pNavigatorFuncs->reloadplugins(reloadPages);
347 }
348
349 JRIEnv* NPN_GetJavaEnv(void)
350 {
351         return g_pNavigatorFuncs->getJavaEnv();
352 }
353
354 jref NPN_GetJavaPeer(NPP instance)
355 {
356         return g_pNavigatorFuncs->getJavaPeer(instance);
357 }
358