Back to index

lightning-sunbird  0.9+nobinonly
npunix.c
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is mozilla.org code.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 1998
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Stephen Mak <smak@sun.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 /*
00041  * npunix.c
00042  *
00043  * Netscape Client Plugin API
00044  * - Wrapper function to interface with the Netscape Navigator
00045  *
00046  * dp Suresh <dp@netscape.com>
00047  *
00048  *----------------------------------------------------------------------
00049  * PLUGIN DEVELOPERS:
00050  *  YOU WILL NOT NEED TO EDIT THIS FILE.
00051  *----------------------------------------------------------------------
00052  */
00053 
00054 #define XP_UNIX 1
00055 
00056 #include <stdio.h>
00057 #include "npapi.h"
00058 #include "npupp.h"
00059 
00060 /*
00061  * Define PLUGIN_TRACE to have the wrapper functions print
00062  * messages to stderr whenever they are called.
00063  */
00064 
00065 #ifdef PLUGIN_TRACE
00066 #include <stdio.h>
00067 #define PLUGINDEBUGSTR(msg) fprintf(stderr, "%s\n", msg)
00068 #else
00069 #define PLUGINDEBUGSTR(msg)
00070 #endif
00071 
00072 
00073 /***********************************************************************
00074  *
00075  * Globals
00076  *
00077  ***********************************************************************/
00078 
00079 static NPNetscapeFuncs   gNetscapeFuncs;    /* Netscape Function table */
00080 
00081 
00082 /***********************************************************************
00083  *
00084  * Wrapper functions : plugin calling Netscape Navigator
00085  *
00086  * These functions let the plugin developer just call the APIs
00087  * as documented and defined in npapi.h, without needing to know
00088  * about the function table and call macros in npupp.h.
00089  *
00090  ***********************************************************************/
00091 
00092 void
00093 NPN_Version(int* plugin_major, int* plugin_minor,
00094          int* netscape_major, int* netscape_minor)
00095 {
00096     *plugin_major = NP_VERSION_MAJOR;
00097     *plugin_minor = NP_VERSION_MINOR;
00098 
00099     /* Major version is in high byte */
00100     *netscape_major = gNetscapeFuncs.version >> 8;
00101     /* Minor version is in low byte */
00102     *netscape_minor = gNetscapeFuncs.version & 0xFF;
00103 }
00104 
00105 NPError
00106 NPN_GetValue(NPP instance, NPNVariable variable, void *r_value)
00107 {
00108     return CallNPN_GetValueProc(gNetscapeFuncs.getvalue,
00109                     instance, variable, r_value);
00110 }
00111 
00112 NPError
00113 NPN_SetValue(NPP instance, NPPVariable variable, void *value)
00114 {
00115     return CallNPN_SetValueProc(gNetscapeFuncs.setvalue,
00116                     instance, variable, value);
00117 }
00118 
00119 NPError
00120 NPN_GetURL(NPP instance, const char* url, const char* window)
00121 {
00122     return CallNPN_GetURLProc(gNetscapeFuncs.geturl, instance, url, window);
00123 }
00124 
00125 NPError
00126 NPN_GetURLNotify(NPP instance, const char* url, const char* window, void* notifyData)
00127 {
00128     return CallNPN_GetURLNotifyProc(gNetscapeFuncs.geturlnotify, instance, url, window, notifyData);
00129 }
00130 
00131 NPError
00132 NPN_PostURL(NPP instance, const char* url, const char* window,
00133          uint32 len, const char* buf, NPBool file)
00134 {
00135     return CallNPN_PostURLProc(gNetscapeFuncs.posturl, instance,
00136                     url, window, len, buf, file);
00137 }
00138 
00139 NPError
00140 NPN_PostURLNotify(NPP instance, const char* url, const char* window, uint32 len,
00141                   const char* buf, NPBool file, void* notifyData)
00142 {
00143     return CallNPN_PostURLNotifyProc(gNetscapeFuncs.posturlnotify,
00144             instance, url, window, len, buf, file, notifyData);
00145 }
00146 
00147 NPError
00148 NPN_RequestRead(NPStream* stream, NPByteRange* rangeList)
00149 {
00150     return CallNPN_RequestReadProc(gNetscapeFuncs.requestread,
00151                     stream, rangeList);
00152 }
00153 
00154 NPError
00155 NPN_NewStream(NPP instance, NPMIMEType type, const char *window,
00156           NPStream** stream_ptr)
00157 {
00158     return CallNPN_NewStreamProc(gNetscapeFuncs.newstream, instance,
00159                     type, window, stream_ptr);
00160 }
00161 
00162 int32
00163 NPN_Write(NPP instance, NPStream* stream, int32 len, void* buffer)
00164 {
00165     return CallNPN_WriteProc(gNetscapeFuncs.write, instance,
00166                     stream, len, buffer);
00167 }
00168 
00169 NPError
00170 NPN_DestroyStream(NPP instance, NPStream* stream, NPError reason)
00171 {
00172     return CallNPN_DestroyStreamProc(gNetscapeFuncs.destroystream,
00173                         instance, stream, reason);
00174 }
00175 
00176 void
00177 NPN_Status(NPP instance, const char* message)
00178 {
00179     CallNPN_StatusProc(gNetscapeFuncs.status, instance, message);
00180 }
00181 
00182 const char*
00183 NPN_UserAgent(NPP instance)
00184 {
00185     return CallNPN_UserAgentProc(gNetscapeFuncs.uagent, instance);
00186 }
00187 
00188 void*
00189 NPN_MemAlloc(uint32 size)
00190 {
00191     return CallNPN_MemAllocProc(gNetscapeFuncs.memalloc, size);
00192 }
00193 
00194 void NPN_MemFree(void* ptr)
00195 {
00196     CallNPN_MemFreeProc(gNetscapeFuncs.memfree, ptr);
00197 }
00198 
00199 uint32 NPN_MemFlush(uint32 size)
00200 {
00201     return CallNPN_MemFlushProc(gNetscapeFuncs.memflush, size);
00202 }
00203 
00204 void NPN_ReloadPlugins(NPBool reloadPages)
00205 {
00206     CallNPN_ReloadPluginsProc(gNetscapeFuncs.reloadplugins, reloadPages);
00207 }
00208 
00209 #ifdef OJI
00210 JRIEnv* NPN_GetJavaEnv()
00211 {
00212     return CallNPN_GetJavaEnvProc(gNetscapeFuncs.getJavaEnv);
00213 }
00214 
00215 jref NPN_GetJavaPeer(NPP instance)
00216 {
00217     return CallNPN_GetJavaPeerProc(gNetscapeFuncs.getJavaPeer,
00218                        instance);
00219 }
00220 #endif
00221 
00222 void
00223 NPN_InvalidateRect(NPP instance, NPRect *invalidRect)
00224 {
00225     CallNPN_InvalidateRectProc(gNetscapeFuncs.invalidaterect, instance,
00226         invalidRect);
00227 }
00228 
00229 void
00230 NPN_InvalidateRegion(NPP instance, NPRegion invalidRegion)
00231 {
00232     CallNPN_InvalidateRegionProc(gNetscapeFuncs.invalidateregion, instance,
00233         invalidRegion);
00234 }
00235 
00236 void
00237 NPN_ForceRedraw(NPP instance)
00238 {
00239     CallNPN_ForceRedrawProc(gNetscapeFuncs.forceredraw, instance);
00240 }
00241 
00242 void NPN_PushPopupsEnabledState(NPP instance, NPBool enabled)
00243 {
00244     CallNPN_PushPopupsEnabledStateProc(gNetscapeFuncs.pushpopupsenabledstate,
00245         instance, enabled);
00246 }
00247 
00248 void NPN_PopPopupsEnabledState(NPP instance)
00249 {
00250     CallNPN_PopPopupsEnabledStateProc(gNetscapeFuncs.poppopupsenabledstate,
00251         instance);
00252 }
00253 
00254 
00255 
00256 /***********************************************************************
00257  *
00258  * Wrapper functions : Netscape Navigator -> plugin
00259  *
00260  * These functions let the plugin developer just create the APIs
00261  * as documented and defined in npapi.h, without needing to 
00262  * install those functions in the function table or worry about
00263  * setting up globals for 68K plugins.
00264  *
00265  ***********************************************************************/
00266 
00267 NPError
00268 Private_New(NPMIMEType pluginType, NPP instance, uint16 mode,
00269         int16 argc, char* argn[], char* argv[], NPSavedData* saved)
00270 {
00271     NPError ret;
00272     PLUGINDEBUGSTR("New");
00273     ret = NPP_New(pluginType, instance, mode, argc, argn, argv, saved);
00274     return ret; 
00275 }
00276 
00277 NPError
00278 Private_Destroy(NPP instance, NPSavedData** save)
00279 {
00280     PLUGINDEBUGSTR("Destroy");
00281     return NPP_Destroy(instance, save);
00282 }
00283 
00284 NPError
00285 Private_SetWindow(NPP instance, NPWindow* window)
00286 {
00287     NPError err;
00288     PLUGINDEBUGSTR("SetWindow");
00289     err = NPP_SetWindow(instance, window);
00290     return err;
00291 }
00292 
00293 NPError
00294 Private_NewStream(NPP instance, NPMIMEType type, NPStream* stream,
00295             NPBool seekable, uint16* stype)
00296 {
00297     NPError err;
00298     PLUGINDEBUGSTR("NewStream");
00299     err = NPP_NewStream(instance, type, stream, seekable, stype);
00300     return err;
00301 }
00302 
00303 int32
00304 Private_WriteReady(NPP instance, NPStream* stream)
00305 {
00306     unsigned int result;
00307     PLUGINDEBUGSTR("WriteReady");
00308     result = NPP_WriteReady(instance, stream);
00309     return result;
00310 }
00311 
00312 int32
00313 Private_Write(NPP instance, NPStream* stream, int32 offset, int32 len,
00314         void* buffer)
00315 {
00316     unsigned int result;
00317     PLUGINDEBUGSTR("Write");
00318     result = NPP_Write(instance, stream, offset, len, buffer);
00319     return result;
00320 }
00321 
00322 void
00323 Private_StreamAsFile(NPP instance, NPStream* stream, const char* fname)
00324 {
00325     PLUGINDEBUGSTR("StreamAsFile");
00326     NPP_StreamAsFile(instance, stream, fname);
00327 }
00328 
00329 
00330 NPError
00331 Private_DestroyStream(NPP instance, NPStream* stream, NPError reason)
00332 {
00333     NPError err;
00334     PLUGINDEBUGSTR("DestroyStream");
00335     err = NPP_DestroyStream(instance, stream, reason);
00336     return err;
00337 }
00338 
00339 void
00340 Private_URLNotify(NPP instance, const char* url,
00341                 NPReason reason, void* notifyData)
00342                 
00343 {
00344     PLUGINDEBUGSTR("URLNotify");
00345     NPP_URLNotify(instance, url, reason, notifyData);
00346 }
00347 
00348 
00349 
00350 void
00351 Private_Print(NPP instance, NPPrint* platformPrint)
00352 {
00353     PLUGINDEBUGSTR("Print");
00354     NPP_Print(instance, platformPrint);
00355 }
00356 
00357 #ifdef OJI
00358 JRIGlobalRef
00359 Private_GetJavaClass(void)
00360 {
00361     jref clazz = NPP_GetJavaClass();
00362     if (clazz) {
00363     JRIEnv* env = NPN_GetJavaEnv();
00364     return JRI_NewGlobalRef(env, clazz);
00365     }
00366     return NULL;
00367 }
00368 #endif
00369 
00370 /*********************************************************************** 
00371  *
00372  * These functions are located automagically by netscape.
00373  *
00374  ***********************************************************************/
00375 
00376 /*
00377  * NP_GetMIMEDescription
00378  *  - Netscape needs to know about this symbol
00379  *  - Netscape uses the return value to identify when an object instance
00380  *    of this plugin should be created.
00381  */
00382 char *
00383 NP_GetMIMEDescription(void)
00384 {
00385     return NPP_GetMIMEDescription();
00386 }
00387 
00388 /*
00389  * NP_GetValue [optional]
00390  *  - Netscape needs to know about this symbol.
00391  *  - Interfaces with plugin to get values for predefined variables
00392  *    that the navigator needs.
00393  */
00394 NPError
00395 NP_GetValue(void* future, NPPVariable variable, void *value)
00396 {
00397     return NPP_GetValue(future, variable, value);
00398 }
00399 
00400 /*
00401  * NP_Initialize
00402  *  - Netscape needs to know about this symbol.
00403  *  - It calls this function after looking up its symbol before it
00404  *    is about to create the first ever object of this kind.
00405  *
00406  * PARAMETERS
00407  *    nsTable   - The netscape function table. If developers just use these
00408  *        wrappers, they dont need to worry about all these function
00409  *        tables.
00410  * RETURN
00411  *    pluginFuncs
00412  *      - This functions needs to fill the plugin function table
00413  *        pluginFuncs and return it. Netscape Navigator plugin
00414  *        library will use this function table to call the plugin.
00415  *
00416  */
00417 NPError
00418 NP_Initialize(NPNetscapeFuncs* nsTable, NPPluginFuncs* pluginFuncs)
00419 {
00420     NPError err = NPERR_NO_ERROR;
00421 
00422     PLUGINDEBUGSTR("NP_Initialize");
00423     
00424     /* validate input parameters */
00425 
00426     if ((nsTable == NULL) || (pluginFuncs == NULL))
00427         err = NPERR_INVALID_FUNCTABLE_ERROR;
00428     
00429     /*
00430      * Check the major version passed in Netscape's function table.
00431      * We won't load if the major version is newer than what we expect.
00432      * Also check that the function tables passed in are big enough for
00433      * all the functions we need (they could be bigger, if Netscape added
00434      * new APIs, but that's OK with us -- we'll just ignore them).
00435      *
00436      */
00437 
00438     if (err == NPERR_NO_ERROR) {
00439         if ((nsTable->version >> 8) > NP_VERSION_MAJOR)
00440             err = NPERR_INCOMPATIBLE_VERSION_ERROR;
00441         if (nsTable->size < sizeof(NPNetscapeFuncs))
00442             err = NPERR_INVALID_FUNCTABLE_ERROR;
00443         if (pluginFuncs->size < sizeof(NPPluginFuncs))      
00444             err = NPERR_INVALID_FUNCTABLE_ERROR;
00445     }
00446         
00447     
00448     if (err == NPERR_NO_ERROR) {
00449         /*
00450          * Copy all the fields of Netscape function table into our
00451          * copy so we can call back into Netscape later.  Note that
00452          * we need to copy the fields one by one, rather than assigning
00453          * the whole structure, because the Netscape function table
00454          * could actually be bigger than what we expect.
00455          */
00456         gNetscapeFuncs.version       = nsTable->version;
00457         gNetscapeFuncs.size          = nsTable->size;
00458         gNetscapeFuncs.posturl       = nsTable->posturl;
00459         gNetscapeFuncs.geturl        = nsTable->geturl;
00460         gNetscapeFuncs.geturlnotify  = nsTable->geturlnotify;
00461         gNetscapeFuncs.requestread   = nsTable->requestread;
00462         gNetscapeFuncs.newstream     = nsTable->newstream;
00463         gNetscapeFuncs.write         = nsTable->write;
00464         gNetscapeFuncs.destroystream = nsTable->destroystream;
00465         gNetscapeFuncs.status        = nsTable->status;
00466         gNetscapeFuncs.uagent        = nsTable->uagent;
00467         gNetscapeFuncs.memalloc      = nsTable->memalloc;
00468         gNetscapeFuncs.memfree       = nsTable->memfree;
00469         gNetscapeFuncs.memflush      = nsTable->memflush;
00470         gNetscapeFuncs.reloadplugins = nsTable->reloadplugins;
00471 #ifdef OJI
00472         gNetscapeFuncs.getJavaEnv    = nsTable->getJavaEnv;
00473         gNetscapeFuncs.getJavaPeer   = nsTable->getJavaPeer;
00474 #endif
00475         gNetscapeFuncs.getvalue      = nsTable->getvalue;
00476         gNetscapeFuncs.pushpopupsenabledstate = nsTable->pushpopupsenabledstate;
00477         gNetscapeFuncs.poppopupsenabledstate  = nsTable->poppopupsenabledstate;
00478 
00479         /*
00480          * Set up the plugin function table that Netscape will use to
00481          * call us.  Netscape needs to know about our version and size
00482          * and have a UniversalProcPointer for every function we
00483          * implement.
00484          */
00485         pluginFuncs->version    = (NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR;
00486         pluginFuncs->size       = sizeof(NPPluginFuncs);
00487         pluginFuncs->newp       = NewNPP_NewProc(Private_New);
00488         pluginFuncs->destroy    = NewNPP_DestroyProc(Private_Destroy);
00489         pluginFuncs->setwindow  = NewNPP_SetWindowProc(Private_SetWindow);
00490         pluginFuncs->newstream  = NewNPP_NewStreamProc(Private_NewStream);
00491         pluginFuncs->destroystream = NewNPP_DestroyStreamProc(Private_DestroyStream);
00492         pluginFuncs->asfile     = NewNPP_StreamAsFileProc(Private_StreamAsFile);
00493         pluginFuncs->writeready = NewNPP_WriteReadyProc(Private_WriteReady);
00494         pluginFuncs->write      = NewNPP_WriteProc(Private_Write);
00495         pluginFuncs->print      = NewNPP_PrintProc(Private_Print);
00496         pluginFuncs->urlnotify  = NewNPP_URLNotifyProc(Private_URLNotify);
00497         pluginFuncs->event      = NULL;
00498 #ifdef OJI
00499         pluginFuncs->javaClass  = Private_GetJavaClass();
00500 #endif
00501 
00502         err = NPP_Initialize();
00503     }
00504     
00505     return err;
00506 }
00507 
00508 /*
00509  * NP_Shutdown [optional]
00510  *  - Netscape needs to know about this symbol.
00511  *  - It calls this function after looking up its symbol after
00512  *    the last object of this kind has been destroyed.
00513  *
00514  */
00515 NPError
00516 NP_Shutdown(void)
00517 {
00518     PLUGINDEBUGSTR("NP_Shutdown");
00519     NPP_Shutdown();
00520     return NPERR_NO_ERROR;
00521 }