Back to index

lightning-sunbird  0.9+nobinonly
npwin.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #ifndef _NPAPI_H_
00039 #include "npapi.h"
00040 #endif
00041 #ifndef _NPUPP_H_
00042 #include "npupp.h"
00043 #endif
00044 
00045 #include "nsDefaultPlugin.h"
00046 
00047 //\\// DEFINE
00048 #define NP_EXPORT
00049 
00050 //\\// GLOBAL DATA
00051 NPNetscapeFuncs* g_pNavigatorFuncs = 0;
00052 #ifdef OJI
00053 JRIGlobalRef Private_GetJavaClass(void);
00054 
00055 //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\.
00057 // Private_GetJavaClass (global function)
00058 //
00059 //     Given a Java class reference (thru NPP_GetJavaClass) inform JRT
00060 //     of this class existence
00061 //
00062 JRIGlobalRef
00063 Private_GetJavaClass(void)
00064 {
00065     jref clazz = NPP_GetJavaClass();
00066     if (clazz) {
00067               JRIEnv* env = NPN_GetJavaEnv();
00068               return JRI_NewGlobalRef(env, clazz);
00069     }
00070     return NULL;
00071 }
00072 #endif /* OJI */
00073 //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\.
00075 //                                        PLUGIN DLL entry points   
00076 //
00077 // These are the Windows specific DLL entry points. They must be exoprted
00078 //
00079 
00080 // we need these to be global since we have to fill one of its field
00081 // with a data (class) which requires knowlwdge of the navigator
00082 // jump-table. This jump table is known at Initialize time (NP_Initialize)
00083 // which is called after NP_GetEntryPoint
00084 static NPPluginFuncs* g_pluginFuncs;
00085 
00086 //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\.
00088 // NP_GetEntryPoints
00089 //
00090 //     fills in the func table used by Navigator to call entry points in
00091 //  plugin DLL.  Note that these entry points ensure that DS is loaded
00092 //  by using the NP_LOADDS macro, when compiling for Win16
00093 //
00094 NPError WINAPI NP_EXPORT
00095 NP_GetEntryPoints(NPPluginFuncs* pFuncs)
00096 {
00097     // trap a NULL ptr 
00098     if(pFuncs == NULL)
00099         return NPERR_INVALID_FUNCTABLE_ERROR;
00100 
00101     // if the plugin's function table is smaller than the plugin expects,
00102     // then they are incompatible, and should return an error 
00103 
00104     pFuncs->version       = (NP_VERSION_MAJOR << 8) | NP_VERSION_MINOR;
00105     pFuncs->newp          = NPP_New;
00106     pFuncs->destroy       = NPP_Destroy;
00107     pFuncs->setwindow     = NPP_SetWindow;
00108     pFuncs->newstream     = NPP_NewStream;
00109     pFuncs->destroystream = NPP_DestroyStream;
00110     pFuncs->asfile        = NPP_StreamAsFile;
00111     pFuncs->writeready    = NPP_WriteReady;
00112     pFuncs->write         = NPP_Write;
00113     pFuncs->print         = NPP_Print;
00114     pFuncs->event         = 0;       
00115 
00116        g_pluginFuncs          = pFuncs;
00117 
00118     return NPERR_NO_ERROR;
00119 }
00120 
00121 //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\.
00123 // NP_Initialize
00124 //
00125 //     called immediately after the plugin DLL is loaded
00126 //
00127 NPError WINAPI NP_EXPORT 
00128 NP_Initialize(NPNetscapeFuncs* pFuncs)
00129 {
00130     // trap a NULL ptr 
00131     if(pFuncs == NULL)
00132         return NPERR_INVALID_FUNCTABLE_ERROR;
00133 
00134     g_pNavigatorFuncs = pFuncs; // save it for future reference 
00135 
00136     // if the plugin's major ver level is lower than the Navigator's,
00137     // then they are incompatible, and should return an error 
00138     if(HIBYTE(pFuncs->version) > NP_VERSION_MAJOR)
00139         return NPERR_INCOMPATIBLE_VERSION_ERROR;
00140 
00141        // We have to defer these assignments until g_pNavigatorFuncs is set
00142     int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
00143 
00144        if( navMinorVers >= NPVERS_HAS_NOTIFICATION ) {
00145               g_pluginFuncs->urlnotify = NPP_URLNotify;
00146        }
00147 #ifdef OJI    
00148        if( navMinorVers >= NPVERS_HAS_LIVECONNECT ) {
00149               g_pluginFuncs->javaClass = Private_GetJavaClass();
00150        }
00151 #endif
00152        // NPP_Initialize is a standard (cross-platform) initialize function.
00153     return NPP_Initialize();
00154 }
00155 
00156 //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\.
00158 // NP_Shutdown
00159 //
00160 //     called immediately before the plugin DLL is unloaded.
00161 //     This functio shuold check for some ref count on the dll to see if it is
00162 //     unloadable or it needs to stay in memory. 
00163 //
00164 NPError WINAPI NP_EXPORT 
00165 NP_Shutdown()
00166 {
00167     NPP_Shutdown();
00168     g_pNavigatorFuncs = NULL;
00169     return NPERR_NO_ERROR;
00170 }
00171 
00172 char * NP_GetMIMEDescription()
00173 {
00174   static char mimetype[] = NS_PLUGIN_DEFAULT_MIME_DESCRIPTION;
00175   return mimetype;
00176 }
00177 
00178 //                                        END - PLUGIN DLL entry points   
00180 //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\.
00181 
00182 /*    NAVIGATOR Entry points    */
00183 
00184 /* These entry points expect to be called from within the plugin.  The
00185    noteworthy assumption is that DS has already been set to point to the
00186    plugin's DLL data segment.  Don't call these functions from outside
00187    the plugin without ensuring DS is set to the DLLs data segment first,
00188    typically using the NP_LOADDS macro
00189 */
00190 
00191 /* returns the major/minor version numbers of the Plugin API for the plugin
00192    and the Navigator
00193 */
00194 void NPN_Version(int* plugin_major, int* plugin_minor, int* netscape_major, int* netscape_minor)
00195 {
00196     *plugin_major   = NP_VERSION_MAJOR;
00197     *plugin_minor   = NP_VERSION_MINOR;
00198     *netscape_major = HIBYTE(g_pNavigatorFuncs->version);
00199     *netscape_minor = LOBYTE(g_pNavigatorFuncs->version);
00200 }
00201 
00202 NPError NPN_GetValue(NPP instance, NPNVariable variable, void *result)
00203 {
00204     return g_pNavigatorFuncs->getvalue(instance, variable, result);
00205 }
00206 
00207 
00208 /* causes the specified URL to be fetched and streamed in
00209 */
00210 NPError NPN_GetURLNotify(NPP instance, const char *url, const char *target, void* notifyData)
00211 
00212 {
00213        int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
00214        NPError err;
00215        if( navMinorVers >= NPVERS_HAS_NOTIFICATION ) {
00216               err = g_pNavigatorFuncs->geturlnotify(instance, url, target, notifyData);
00217        }
00218        else {
00219               err = NPERR_INCOMPATIBLE_VERSION_ERROR;
00220        }
00221        return err;
00222 }
00223 
00224 
00225 NPError NPN_GetURL(NPP instance, const char *url, const char *target)
00226 {
00227     return g_pNavigatorFuncs->geturl(instance, url, target);
00228 }
00229 
00230 NPError NPN_PostURLNotify(NPP instance, const char* url, const char* window, uint32 len, const char* buf, NPBool file, void* notifyData)
00231 {
00232        int navMinorVers = g_pNavigatorFuncs->version & 0xFF;
00233        NPError err;
00234        if( navMinorVers >= NPVERS_HAS_NOTIFICATION ) {
00235               err = g_pNavigatorFuncs->posturlnotify(instance, url, window, len, buf, file, notifyData);
00236        }
00237        else {
00238               err = NPERR_INCOMPATIBLE_VERSION_ERROR;
00239        }
00240        return err;
00241 }
00242 
00243 
00244 NPError NPN_PostURL(NPP instance, const char* url, const char* window, uint32 len, const char* buf, NPBool file)
00245 {
00246     return g_pNavigatorFuncs->posturl(instance, url, window, len, buf, file);
00247 }
00248 
00249 /* Requests that a number of bytes be provided on a stream.  Typically
00250    this would be used if a stream was in "pull" mode.  An optional
00251    position can be provided for streams which are seekable.
00252 */
00253 NPError NPN_RequestRead(NPStream* stream, NPByteRange* rangeList)
00254 {
00255     return g_pNavigatorFuncs->requestread(stream, rangeList);
00256 }
00257 
00258 /* Creates a new stream of data from the plug-in to be interpreted
00259    by Netscape in the current window.
00260 */
00261 NPError NPN_NewStream(NPP instance, NPMIMEType type, 
00262                                                         const char* target, NPStream** stream)
00263 {
00264        int navMinorVersion = g_pNavigatorFuncs->version & 0xFF;
00265        NPError err;
00266 
00267        if( navMinorVersion >= NPVERS_HAS_STREAMOUTPUT ) {
00268               err = g_pNavigatorFuncs->newstream(instance, type, target, stream);
00269        }
00270        else {
00271               err = NPERR_INCOMPATIBLE_VERSION_ERROR;
00272        }
00273        return err;
00274 }
00275 
00276 /* Provides len bytes of data.
00277 */
00278 int32 NPN_Write(NPP instance, NPStream *stream,
00279                 int32 len, void *buffer)
00280 {
00281        int navMinorVersion = g_pNavigatorFuncs->version & 0xFF;
00282        int32 result;
00283 
00284        if( navMinorVersion >= NPVERS_HAS_STREAMOUTPUT ) {
00285               result = g_pNavigatorFuncs->write(instance, stream, len, buffer);
00286        }
00287        else {
00288               result = -1;
00289        }
00290        return result;
00291 }
00292 
00293 /* Closes a stream object.  
00294 reason indicates why the stream was closed.
00295 */
00296 NPError NPN_DestroyStream(NPP instance, NPStream* stream, NPError reason)
00297 {
00298        int navMinorVersion = g_pNavigatorFuncs->version & 0xFF;
00299        NPError err;
00300 
00301        if( navMinorVersion >= NPVERS_HAS_STREAMOUTPUT ) {
00302               err = g_pNavigatorFuncs->destroystream(instance, stream, reason);
00303        }
00304        else {
00305               err = NPERR_INCOMPATIBLE_VERSION_ERROR;
00306        }
00307        return err;
00308 }
00309 
00310 /* Provides a text status message in the Netscape client user interface
00311 */
00312 void NPN_Status(NPP instance, const char *message)
00313 {
00314     g_pNavigatorFuncs->status(instance, message);
00315 }
00316 
00317 /* returns the user agent string of Navigator, which contains version info
00318 */
00319 const char* NPN_UserAgent(NPP instance)
00320 {
00321     return g_pNavigatorFuncs->uagent(instance);
00322 }
00323 
00324 /* allocates memory from the Navigator's memory space.  Necessary so that
00325    saved instance data may be freed by Navigator when exiting.
00326 */
00327 
00328 
00329 void* NPN_MemAlloc(uint32 size)
00330 {
00331     return g_pNavigatorFuncs->memalloc(size);
00332 }
00333 
00334 /* reciprocal of MemAlloc() above
00335 */
00336 void NPN_MemFree(void* ptr)
00337 {
00338     g_pNavigatorFuncs->memfree(ptr);
00339 }
00340 #ifdef OJI
00341 /* private function to Netscape.  do not use!
00342 */
00343 void NPN_ReloadPlugins(NPBool reloadPages)
00344 {
00345     g_pNavigatorFuncs->reloadplugins(reloadPages);
00346 }
00347 
00348 JRIEnv* NPN_GetJavaEnv(void)
00349 {
00350        return g_pNavigatorFuncs->getJavaEnv();
00351 }
00352 
00353 jref NPN_GetJavaPeer(NPP instance)
00354 {
00355        return g_pNavigatorFuncs->getJavaPeer(instance);
00356 }
00357 #endif
00358