Back to index

lightning-sunbird  0.9+nobinonly
npp_gate.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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.org 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 #include "xp.h"
00039 
00040 #include "epmanager.h"
00041 #include "plugload.h"
00042 #include "logger.h"
00043 
00044 extern NPNetscapeFuncs NPNFuncs;
00045 extern Logger * logger;
00046 
00047 NPNetscapeFuncs fakeNPNFuncs;
00048 
00049 extern NPPEntryPointManager * epManager;
00050 
00051 jref NPP_GetJavaClass (void)
00052 {
00053   if(logger)
00054     logger->logCall(action_npp_get_java_class);
00055 
00056   if(logger)
00057     logger->logReturn();
00058   return NULL;
00059 }
00060 
00061 NPError NPP_New(NPMIMEType pluginType,
00062                 NPP instance,
00063                 uint16 mode,
00064                 int16 argc,
00065                 char* argn[],
00066                 char* argv[],
00067                 NPSavedData* saved)
00068 {   
00069   if(epManager == NULL)
00070     return NPERR_GENERIC_ERROR;
00071   
00072   if(instance == NULL)
00073     return NPERR_INVALID_INSTANCE_ERROR;
00074 
00075   if(logger)
00076     logger->logCall(action_npp_new, (DWORD)pluginType, (DWORD)instance, (DWORD)mode, (DWORD)argc, (DWORD)argn, (DWORD)argv, (DWORD)saved);
00077 
00078 /* now action begins */
00079 
00080   if(NULL == epManager->findEntryPointsForPlugin(pluginType))
00081   {
00082     // if it is first time in, we don't have it yet
00083     // scan plugins dir for available plugins to see if we have anything 
00084     // for the given mimetype
00085     XP_HLIB hLib = LoadRealPlugin(pluginType);
00086     if(!hLib)
00087     {
00088       // what do we do if we don't?
00089       return NPERR_GENERIC_ERROR;
00090     }
00091 
00092     NP_GETENTRYPOINTS real_NP_GetEntryPoints = (NP_GETENTRYPOINTS)XP_GetSymbol(hLib, "NP_GetEntryPoints");
00093     if(!real_NP_GetEntryPoints)
00094       return NPERR_GENERIC_ERROR;
00095 
00096     NP_INITIALIZE real_NP_Initialize = (NP_INITIALIZE)XP_GetSymbol(hLib, "NP_Initialize");
00097     if(!real_NP_Initialize)
00098       return NPERR_GENERIC_ERROR;
00099 
00100     NP_SHUTDOWN real_NP_Shutdown = (NP_SHUTDOWN)XP_GetSymbol(hLib, "NP_Shutdown");
00101     if(!real_NP_Shutdown)
00102       return NPERR_GENERIC_ERROR;
00103 
00104     // fill callbacks structs
00105     NPPluginFuncs realNPPFuncs;
00106     memset(&realNPPFuncs, 0, sizeof(NPPluginFuncs));
00107     realNPPFuncs.size = sizeof(NPPluginFuncs);
00108 
00109     real_NP_GetEntryPoints(&realNPPFuncs);
00110 
00111     if(logger)
00112       logger->logSPY_NP_GetEntryPoints(&realNPPFuncs);
00113 
00114     // store the table with the entry point manager
00115     epManager->createEntryPointsForPlugin(pluginType, &realNPPFuncs, real_NP_Shutdown, hLib);
00116     
00117     // inform the plugin about our entry point it should call
00118     memset((void *)&fakeNPNFuncs, 0, sizeof(fakeNPNFuncs));
00119 
00120     fakeNPNFuncs.size             = sizeof(fakeNPNFuncs);
00121     fakeNPNFuncs.version          = NPNFuncs.version;
00122     fakeNPNFuncs.geturlnotify     = NPN_GetURLNotify;
00123     fakeNPNFuncs.geturl           = NPN_GetURL;
00124     fakeNPNFuncs.posturlnotify    = NPN_PostURLNotify;
00125     fakeNPNFuncs.posturl          = NPN_PostURL;
00126     fakeNPNFuncs.requestread      = NPN_RequestRead;
00127     fakeNPNFuncs.newstream        = NPN_NewStream;
00128     fakeNPNFuncs.write            = NPN_Write;
00129     fakeNPNFuncs.destroystream    = NPN_DestroyStream;
00130     fakeNPNFuncs.status           = NPN_Status;
00131     fakeNPNFuncs.uagent           = NPN_UserAgent;
00132     fakeNPNFuncs.memalloc         = NPN_MemAlloc;
00133     fakeNPNFuncs.memfree          = NPN_MemFree;
00134     fakeNPNFuncs.memflush         = NPN_MemFlush;
00135     fakeNPNFuncs.reloadplugins    = NPN_ReloadPlugins;
00136     fakeNPNFuncs.getJavaEnv       = NPN_GetJavaEnv;
00137     fakeNPNFuncs.getJavaPeer      = NPN_GetJavaPeer;
00138     fakeNPNFuncs.getvalue         = NPN_GetValue;
00139     fakeNPNFuncs.setvalue         = NPN_SetValue;
00140     fakeNPNFuncs.invalidaterect   = NPN_InvalidateRect;
00141     fakeNPNFuncs.invalidateregion = NPN_InvalidateRegion;
00142     fakeNPNFuncs.forceredraw      = NPN_ForceRedraw;
00143 
00144     if(logger)
00145       logger->logSPY_NP_Initialize();
00146 
00147     real_NP_Initialize(&fakeNPNFuncs);
00148   }
00149   
00150   NPError rv = epManager->callNPP_New(pluginType, instance, mode, argc, argn, argv, saved);
00151   
00152   if(logger)
00153     logger->logReturn();
00154 
00155   return rv;
00156 }
00157 
00158 NPError NPP_Destroy (NPP instance, NPSavedData** save)
00159 {
00160   if(epManager == NULL)
00161     return NPERR_GENERIC_ERROR;
00162 
00163   if(instance == NULL)
00164     return NPERR_INVALID_INSTANCE_ERROR;
00165 
00166   BOOL last = FALSE;
00167 
00168   if(logger)
00169     logger->logCall(action_npp_destroy, (DWORD)instance, (DWORD)save);
00170 
00171   NPError rv = epManager->callNPP_Destroy(instance, save, &last);
00172 
00173   if(logger)
00174     logger->logReturn();
00175 
00176   if(last && logger->bSPALID)
00177   {
00178     // this will log it
00179     epManager->callNP_Shutdown(instance);
00180 
00181     XP_HLIB hLib = NULL;
00182 
00183     epManager->removeEntryPointsForPlugin(instance, &hLib);
00184   
00185     UnloadRealPlugin(hLib);
00186   }
00187   return rv;
00188 }
00189 
00190 NPError NPP_SetWindow (NPP instance, NPWindow* pNPWindow)
00191 {    
00192   if(epManager == NULL)
00193     return NPERR_GENERIC_ERROR;
00194 
00195   if(instance == NULL)
00196     return NPERR_INVALID_INSTANCE_ERROR;
00197 
00198   if(logger)
00199     logger->logCall(action_npp_set_window, (DWORD)instance, (DWORD)pNPWindow);
00200 
00201   NPError rv = epManager->callNPP_SetWindow(instance, pNPWindow);
00202 
00203   if(logger)
00204     logger->logReturn();
00205 
00206   return rv;
00207 }
00208 
00209 NPError NPP_NewStream(NPP instance,
00210                       NPMIMEType type,
00211                       NPStream* stream, 
00212                       NPBool seekable,
00213                       uint16* stype)
00214 {
00215   if(epManager == NULL)
00216     return NPERR_GENERIC_ERROR;
00217 
00218   if(instance == NULL)
00219     return NPERR_INVALID_INSTANCE_ERROR;
00220 
00221   if(logger)
00222     logger->logCall(action_npp_new_stream, (DWORD)instance, (DWORD)type, (DWORD)stream, (DWORD)seekable, (DWORD)stype);
00223 
00224   NPError rv = epManager->callNPP_NewStream(instance, type, stream, seekable, stype);
00225 
00226   if(logger)
00227     logger->logReturn();
00228 
00229   return rv;
00230 }
00231 
00232 int32 NPP_WriteReady (NPP instance, NPStream *stream)
00233 {
00234   if(epManager == NULL)
00235     return NPERR_GENERIC_ERROR;
00236 
00237   if(instance == NULL)
00238     return NPERR_INVALID_INSTANCE_ERROR;
00239 
00240   if(logger)
00241     logger->logCall(action_npp_write_ready, (DWORD)instance, (DWORD)stream);
00242 
00243   int32 rv = epManager->callNPP_WriteReady(instance, stream);
00244 
00245   if(logger)
00246     logger->logReturn();
00247 
00248   return rv;
00249 }
00250 
00251 int32 NPP_Write (NPP instance, NPStream *stream, int32 offset, int32 len, void *buffer)
00252 {   
00253   if(epManager == NULL)
00254     return NPERR_GENERIC_ERROR;
00255 
00256   if(instance == NULL)
00257     return NPERR_INVALID_INSTANCE_ERROR;
00258 
00259   if(logger)
00260     logger->logCall(action_npp_write, (DWORD)instance, (DWORD)stream, (DWORD)offset, (DWORD)len, (DWORD)buffer);
00261 
00262   int32 rv = epManager->callNPP_Write(instance, stream, offset, len, buffer);
00263 
00264   if(logger)
00265     logger->logReturn();
00266 
00267   return rv;
00268 }
00269 
00270 NPError NPP_DestroyStream (NPP instance, NPStream *stream, NPError reason)
00271 {
00272   if(epManager == NULL)
00273     return NPERR_GENERIC_ERROR;
00274 
00275   if(instance == NULL)
00276     return NPERR_INVALID_INSTANCE_ERROR;
00277 
00278   if(logger)
00279     logger->logCall(action_npp_destroy_stream, (DWORD)instance, (DWORD)stream, (DWORD)reason);
00280 
00281   NPError rv = epManager->callNPP_DestroyStream(instance, stream, reason);
00282 
00283   if(logger)
00284     logger->logReturn();
00285 
00286   return rv;
00287 }
00288 
00289 void NPP_StreamAsFile (NPP instance, NPStream* stream, const char* fname)
00290 {
00291   if(epManager == NULL)
00292     return;
00293 
00294   if(instance == NULL)
00295     return;
00296 
00297   if(logger)
00298     logger->logCall(action_npp_stream_as_file, (DWORD)instance, (DWORD)stream, (DWORD)fname);
00299 
00300   epManager->callNPP_StreamAsFile(instance, stream, fname);
00301 }
00302 
00303 void NPP_Print (NPP instance, NPPrint* printInfo)
00304 {
00305   if(epManager == NULL)
00306     return;
00307 
00308   if(logger)
00309     logger->logCall(action_npp_print, (DWORD)instance, (DWORD)printInfo);
00310 
00311   epManager->callNPP_Print(instance, printInfo);
00312 }
00313 
00314 void NPP_URLNotify(NPP instance, const char* url, NPReason reason, void* notifyData)
00315 {
00316   if(epManager == NULL)
00317     return;
00318 
00319   if(instance == NULL)
00320     return;
00321 
00322   if(logger)
00323     logger->logCall(action_npp_url_notify, (DWORD)instance, (DWORD)url, (DWORD)reason, (DWORD)notifyData);
00324 
00325   epManager->callNPP_URLNotify(instance, url, reason, notifyData);
00326 }
00327 
00328 NPError       NPP_GetValue(NPP instance, NPPVariable variable, void *value)
00329 {
00330   if(epManager == NULL)
00331     return NPERR_GENERIC_ERROR;
00332 
00333   if(instance == NULL)
00334     return NPERR_INVALID_INSTANCE_ERROR;
00335 
00336   if(logger)
00337     logger->logCall(action_npp_get_value, (DWORD)instance, (DWORD)variable, (DWORD)value);
00338 
00339   NPError rv = epManager->callNPP_GetValue(instance, variable, value);
00340 
00341   if(logger)
00342     logger->logReturn();
00343 
00344   return rv;
00345 }
00346 
00347 NPError NPP_SetValue(NPP instance, NPNVariable variable, void *value)
00348 {
00349   if(epManager == NULL)
00350     return NPERR_GENERIC_ERROR;
00351 
00352   if(instance == NULL)
00353     return NPERR_INVALID_INSTANCE_ERROR;
00354 
00355   if(logger)
00356     logger->logCall(action_npp_set_value, (DWORD)instance, (DWORD)variable, (DWORD)value);
00357 
00358   NPError rv = epManager->callNPP_SetValue(instance, variable, value);
00359 
00360   if(logger)
00361     logger->logReturn();
00362 
00363   return rv;
00364 }
00365 
00366 int16  NPP_HandleEvent(NPP instance, void* event)
00367 {
00368   if(epManager == NULL)
00369     return 0;
00370 
00371   if(instance == NULL)
00372     return 0;
00373 
00374   if(logger)
00375     logger->logCall(action_npp_handle_event, (DWORD)instance, (DWORD)event);
00376 
00377   int16 rv = epManager->callNPP_HandleEvent(instance, event);
00378 
00379   if(logger)
00380     logger->logReturn();
00381 
00382   return rv;
00383 }