Back to index

lightning-sunbird  0.9+nobinonly
epmanager.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 "logger.h"
00042 
00043 extern Logger * logger;
00044 
00045 InstanceList::InstanceList(NPP _instance) : 
00046   next(NULL), 
00047   instance(_instance)
00048 {
00049 }
00050 
00051 InstanceList::~InstanceList()
00052 {
00053 }
00054 
00055 PluginEntryPointList::PluginEntryPointList() :
00056   next(NULL),
00057   instances(NULL)
00058 {
00059   mimetype[0] = '\0';
00060   memset((void *)&realNPPFuncs, 0, sizeof(realNPPFuncs));
00061   realShutdown = NULL;
00062   hLib = NULL;
00063 }
00064 
00065 PluginEntryPointList::~PluginEntryPointList()
00066 {
00067 }
00068 
00069 NPPEntryPointManager::NPPEntryPointManager() :
00070   mEntryPoints(NULL)
00071 {
00072 }
00073 
00074 NPPEntryPointManager::~NPPEntryPointManager()
00075 {
00076   for(PluginEntryPointList * eps = mEntryPoints; eps != NULL;)
00077   {
00078     for(InstanceList * instances = eps->instances; instances != NULL;)
00079     {
00080       InstanceList * next = instances->next;
00081       delete instances;
00082       instances = next;
00083     }
00084 
00085     PluginEntryPointList * next = eps->next;
00086     delete eps;
00087     eps = next;
00088   }
00089 }
00090 
00091 void NPPEntryPointManager::createEntryPointsForPlugin(char * mimetype, NPPluginFuncs * funcs, NP_SHUTDOWN shutdownproc, XP_HLIB hLib)
00092 {
00093   PluginEntryPointList * eps = new PluginEntryPointList();
00094 
00095   if(eps == NULL)
00096     return;
00097 
00098   strcpy(eps->mimetype, mimetype);
00099 
00100   if(funcs)
00101   {
00102     eps->realNPPFuncs.size          = funcs->size;
00103     eps->realNPPFuncs.version       = funcs->version;
00104     eps->realNPPFuncs.newp          = funcs->newp;
00105     eps->realNPPFuncs.destroy       = funcs->destroy;
00106     eps->realNPPFuncs.setwindow     = funcs->setwindow;
00107     eps->realNPPFuncs.newstream     = funcs->newstream;
00108     eps->realNPPFuncs.destroystream = funcs->destroystream;
00109     eps->realNPPFuncs.asfile        = funcs->asfile;
00110     eps->realNPPFuncs.writeready    = funcs->writeready;
00111     eps->realNPPFuncs.write         = funcs->write;
00112     eps->realNPPFuncs.print         = funcs->print;
00113     eps->realNPPFuncs.event         = funcs->event;
00114     eps->realNPPFuncs.urlnotify     = funcs->urlnotify;
00115     eps->realNPPFuncs.javaClass     = funcs->javaClass;
00116     eps->realNPPFuncs.getvalue      = funcs->getvalue;
00117     eps->realNPPFuncs.setvalue      = funcs->setvalue;
00118   }
00119 
00120   eps->realShutdown = shutdownproc;
00121   eps->hLib = hLib;
00122 
00123   eps->next = mEntryPoints;
00124   mEntryPoints = eps;
00125 }
00126 
00127 void NPPEntryPointManager::removeEntryPointsForPlugin(NPP instance, XP_HLIB * lib)
00128 {
00129   NPPluginFuncs * eptoremove = findEntryPointsForInstance(instance);
00130 
00131   PluginEntryPointList * prev = NULL;
00132 
00133   for(PluginEntryPointList * eps = mEntryPoints; eps != NULL; eps = eps->next)
00134   {
00135     if(&eps->realNPPFuncs == eptoremove)
00136     {
00137       if(prev)
00138         prev->next = eps->next;
00139       else
00140         mEntryPoints = eps->next;
00141 
00142       *lib = eps->hLib;
00143       delete eps;
00144       return;
00145     }
00146 
00147     prev = eps;
00148   }
00149 }
00150 
00151 NPPluginFuncs * NPPEntryPointManager::findEntryPointsForPlugin(char * mimetype)
00152 {
00153   for(PluginEntryPointList * eps = mEntryPoints; eps != NULL; eps = eps->next)
00154   {
00155     if(0 == stricmp(eps->mimetype, mimetype))
00156       return &eps->realNPPFuncs;
00157   }
00158 
00159   return NULL;
00160 }
00161 
00162 NPPluginFuncs * NPPEntryPointManager::findEntryPointsForInstance(NPP instance)
00163 {
00164   for(PluginEntryPointList * eps = mEntryPoints; eps != NULL; eps = eps->next)
00165   {
00166     for(InstanceList * instances = eps->instances; instances != NULL; instances = instances->next)
00167     {
00168       if(instances->instance == instance)
00169         return &eps->realNPPFuncs;
00170     }
00171   }
00172 
00173   return NULL;
00174 }
00175 
00176 void NPPEntryPointManager::callNP_ShutdownAll()
00177 {
00178   for(PluginEntryPointList * eps = mEntryPoints; eps != NULL; eps = eps->next)
00179   {
00180     if(eps->realShutdown)
00181     {
00182       logger->logSPY_NP_Shutdown(eps->mimetype);
00183       eps->realShutdown();
00184       eps->realShutdown = NULL; // don't want to call it more than once
00185     }
00186   }
00187 }
00188 
00189 void NPPEntryPointManager::callNP_Shutdown(NPP instance)
00190 {
00191   for(PluginEntryPointList * eps = mEntryPoints; eps != NULL; eps = eps->next)
00192   {
00193     for(InstanceList * instances = eps->instances; instances != NULL; instances = instances->next)
00194     {
00195       if(instances->instance == instance)
00196       {
00197         if(eps->realShutdown)
00198         {
00199           logger->logSPY_NP_Shutdown(eps->mimetype);
00200           eps->realShutdown();
00201           eps->realShutdown = NULL; // don't want to call it more than once
00202         }
00203       }
00204     }
00205   }
00206 }
00207 
00208 NPError NPPEntryPointManager::callNPP_New(NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc, char* argn[], char* argv[], NPSavedData* saved)
00209 {
00210   NPPluginFuncs * nppfuncs = NULL;
00211 
00212   for(PluginEntryPointList * eps = mEntryPoints; eps != NULL; eps = eps->next)
00213   {
00214     if(0 == stricmp(eps->mimetype, pluginType))
00215     {
00216       nppfuncs = &eps->realNPPFuncs;
00217 
00218       // now we should associate this plugin instance with plugin entry points
00219       // so that later we could find entry points by instance rather than by mimetype
00220       InstanceList * inst = new InstanceList(instance);
00221       inst->next = eps->instances;
00222       eps->instances = inst;
00223 
00224       break;
00225     }
00226   }
00227 
00228   if(!nppfuncs || !nppfuncs->newp)
00229     return NPERR_GENERIC_ERROR;
00230 
00231   NPError rv = CallNPP_NewProc(nppfuncs->newp, pluginType, instance, mode, argc, argn, argv, saved);
00232 
00233   return rv;
00234 }
00235 
00236 NPError NPPEntryPointManager::callNPP_Destroy(NPP instance, NPSavedData** save, BOOL * last)
00237 {
00238   NPPluginFuncs * nppfuncs = NULL;
00239 
00240   BOOL done = FALSE;
00241 
00242   for(PluginEntryPointList * eps = mEntryPoints; eps != NULL; eps = eps->next)
00243   {
00244     InstanceList * prev = NULL;
00245     for(InstanceList * instances = eps->instances; instances != NULL; instances = instances->next)
00246     {
00247       if(instances->instance == instance)
00248       {
00249         nppfuncs = &eps->realNPPFuncs;
00250         done = TRUE;
00251 
00252         // check if this is the last one
00253         if(eps->instances->next == NULL) 
00254           *last = TRUE;
00255         else
00256         {
00257           // deassociate instance if this is not the last one
00258           // last instance will be needed to find corresponding shutdown proc
00259           if(prev)
00260             prev->next = instances->next;
00261           else
00262             eps->instances = instances->next;
00263 
00264           delete instances;
00265         }
00266 
00267         break;
00268       }
00269       prev = instances;
00270     }
00271     if(done)
00272       break;
00273   }
00274 
00275   if(!nppfuncs || !nppfuncs->destroy)
00276     return NPERR_GENERIC_ERROR;
00277 
00278   return CallNPP_DestroyProc(nppfuncs->destroy, instance, save);
00279 }
00280 
00281 NPError NPPEntryPointManager::callNPP_SetWindow(NPP instance, NPWindow* window)
00282 {
00283   NPPluginFuncs * nppfuncs = findEntryPointsForInstance(instance);
00284   if(!nppfuncs || !nppfuncs->setwindow)
00285     return NPERR_GENERIC_ERROR;
00286 
00287   return CallNPP_SetWindowProc(nppfuncs->setwindow, instance, window);
00288 }
00289 
00290 NPError NPPEntryPointManager::callNPP_NewStream(NPP instance, NPMIMEType type, NPStream* stream, NPBool seekable, uint16* stype)
00291 {
00292   NPPluginFuncs * nppfuncs = findEntryPointsForInstance(instance);
00293   if(!nppfuncs || !nppfuncs->newstream)
00294     return NPERR_GENERIC_ERROR;
00295 
00296   return CallNPP_NewStreamProc(nppfuncs->newstream, instance, type, stream, seekable, stype);
00297 }
00298 
00299 NPError NPPEntryPointManager::callNPP_DestroyStream(NPP instance, NPStream* stream, NPReason reason)
00300 {
00301   NPPluginFuncs * nppfuncs = findEntryPointsForInstance(instance);
00302   if(!nppfuncs || !nppfuncs->destroystream)
00303     return NPERR_GENERIC_ERROR;
00304 
00305   return CallNPP_DestroyStreamProc(nppfuncs->destroystream, instance, stream, reason);
00306 }
00307 
00308 int32 NPPEntryPointManager::callNPP_WriteReady(NPP instance, NPStream* stream)
00309 {
00310   NPPluginFuncs * nppfuncs = findEntryPointsForInstance(instance);
00311   if(!nppfuncs || !nppfuncs->writeready)
00312     return NPERR_GENERIC_ERROR;
00313 
00314   return CallNPP_WriteReadyProc(nppfuncs->writeready, instance, stream);
00315 }
00316 
00317 int32 NPPEntryPointManager::callNPP_Write(NPP instance, NPStream* stream, int32 offset, int32 len, void* buffer)
00318 {
00319   NPPluginFuncs * nppfuncs = findEntryPointsForInstance(instance);
00320   if(!nppfuncs || !nppfuncs->write)
00321     return NPERR_GENERIC_ERROR;
00322 
00323   return CallNPP_WriteProc(nppfuncs->write, instance, stream, offset, len, buffer);
00324 }
00325 
00326 void NPPEntryPointManager::callNPP_StreamAsFile(NPP instance, NPStream* stream, const char* fname)
00327 {
00328   NPPluginFuncs * nppfuncs = findEntryPointsForInstance(instance);
00329   if(!nppfuncs || !nppfuncs->asfile)
00330     return;
00331 
00332   CallNPP_StreamAsFileProc(nppfuncs->asfile, instance, stream, fname);
00333 }
00334 
00335 void NPPEntryPointManager::callNPP_Print(NPP instance, NPPrint* platformPrint)
00336 {
00337   NPPluginFuncs * nppfuncs = findEntryPointsForInstance(instance);
00338   if(!nppfuncs || !nppfuncs->print)
00339     return;
00340 
00341   CallNPP_PrintProc(nppfuncs->print, instance, platformPrint);
00342 }
00343 
00344 void NPPEntryPointManager::callNPP_URLNotify(NPP instance, const char* url, NPReason reason, void* notifyData)
00345 {
00346   NPPluginFuncs * nppfuncs = findEntryPointsForInstance(instance);
00347   if(!nppfuncs || !nppfuncs->urlnotify)
00348     return;
00349 
00350   CallNPP_URLNotifyProc(nppfuncs->urlnotify, instance, url, reason, notifyData);
00351 }
00352 
00353 NPError NPPEntryPointManager::callNPP_GetValue(NPP instance, NPPVariable variable, void *value)
00354 {
00355   NPPluginFuncs * nppfuncs = findEntryPointsForInstance(instance);
00356   if(!nppfuncs || !nppfuncs->getvalue)
00357     return NPERR_GENERIC_ERROR;
00358 
00359   return CallNPP_GetValueProc(nppfuncs->getvalue, instance, variable, value);
00360 }
00361 
00362 NPError NPPEntryPointManager::callNPP_SetValue(NPP instance, NPNVariable variable, void *value)
00363 {
00364   NPPluginFuncs * nppfuncs = findEntryPointsForInstance(instance);
00365   if(!nppfuncs || !nppfuncs->setvalue)
00366     return NPERR_GENERIC_ERROR;
00367 
00368   return CallNPP_SetValueProc(nppfuncs->setvalue, instance, variable, value);
00369 }
00370 
00371 int16 NPPEntryPointManager::callNPP_HandleEvent(NPP instance, void* event)
00372 {
00373   NPPluginFuncs * nppfuncs = findEntryPointsForInstance(instance);
00374   if(!nppfuncs || !nppfuncs->event)
00375     return NPERR_GENERIC_ERROR;
00376 
00377   return CallNPP_HandleEventProc(nppfuncs->event, instance, event);
00378 }