Back to index

lightning-sunbird  0.9+nobinonly
plugin.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 "windows.h"
00039 
00040 #include "npupp.h"
00041 #include "npapi.h"
00042 #include "plugin.h"
00043 #include "plugload.h"
00044 #include "dbg.h"
00045 
00046 extern NPNetscapeFuncs NPNFuncs;
00047 NPNetscapeFuncs wrapperNPNFuncs;
00048 NPPluginFuncs pluginNPPFuncs;
00049 
00050 typedef NPError (__stdcall * NP_GETENTRYPOINTS)(NPPluginFuncs *);
00051 typedef NPError (__stdcall * NP_INITIALIZE)(NPNetscapeFuncs *);
00052 typedef NPError (__stdcall * NP_SHUTDOWN)(void);
00053 
00054 NP_SHUTDOWN plugin_NP_Shutdown = NULL;
00055 HINSTANCE pluginLibrary = NULL;
00056 
00057 nsPluginThread::nsPluginThread(DWORD aP1) : CThread(),
00058   mP1(aP1),
00059   mP2(0),
00060   mP3(0),
00061   mP4(0),
00062   mP5(0),
00063   mP6(0),
00064   mP7(0)
00065 {
00066   dbgOut1("nsPluginThread::nsPluginThread");
00067 
00068   open(this);
00069 }
00070 
00071 nsPluginThread::~nsPluginThread()
00072 {
00073   dbgOut1("nsPluginThread::~nsPluginThread");
00074 
00075   close(this);
00076 }
00077 
00078 BOOL nsPluginThread::init()
00079 {
00080   dbgOut1("nsPluginThread::init");
00081 
00082   // scan plugins dir for available plugins to see if we have anything 
00083   // for the given mimetype
00084   pluginLibrary = LoadRealPlugin((NPMIMEType)mP1);
00085   if(!pluginLibrary)
00086     return FALSE;
00087 
00088   NP_GETENTRYPOINTS plugin_NP_GetEntryPoints = (NP_GETENTRYPOINTS)GetProcAddress(pluginLibrary, "NP_GetEntryPoints");
00089   if(!plugin_NP_GetEntryPoints)
00090     return FALSE;
00091 
00092   NP_INITIALIZE plugin_NP_Initialize = (NP_INITIALIZE)GetProcAddress(pluginLibrary, "NP_Initialize");
00093   if(!plugin_NP_Initialize)
00094     return FALSE;
00095 
00096   plugin_NP_Shutdown = (NP_SHUTDOWN)GetProcAddress(pluginLibrary, "NP_Shutdown");
00097   if(!plugin_NP_Shutdown)
00098     return FALSE;
00099 
00100   // fill callbacks structs
00101   memset(&pluginNPPFuncs, 0, sizeof(NPPluginFuncs));
00102   pluginNPPFuncs.size = sizeof(NPPluginFuncs);
00103 
00104   plugin_NP_GetEntryPoints(&pluginNPPFuncs);
00105 
00106   // inform the plugin about our entry point it should call
00107   memset((void *)&wrapperNPNFuncs, 0, sizeof(wrapperNPNFuncs));
00108   wrapperNPNFuncs.size             = sizeof(wrapperNPNFuncs);
00109   wrapperNPNFuncs.version          = NPNFuncs.version;
00110   wrapperNPNFuncs.geturlnotify     = NPN_GetURLNotify;
00111   wrapperNPNFuncs.geturl           = NPN_GetURL;
00112   wrapperNPNFuncs.posturlnotify    = NPN_PostURLNotify;
00113   wrapperNPNFuncs.posturl          = NPN_PostURL;
00114   wrapperNPNFuncs.requestread      = NPN_RequestRead;
00115   wrapperNPNFuncs.newstream        = NPN_NewStream;
00116   wrapperNPNFuncs.write            = NPN_Write;
00117   wrapperNPNFuncs.destroystream    = NPN_DestroyStream;
00118   wrapperNPNFuncs.status           = NPN_Status;
00119   wrapperNPNFuncs.uagent           = NPN_UserAgent;
00120   wrapperNPNFuncs.memalloc         = NPN_MemAlloc;
00121   wrapperNPNFuncs.memfree          = NPN_MemFree;
00122   wrapperNPNFuncs.memflush         = NPN_MemFlush;
00123   wrapperNPNFuncs.reloadplugins    = NPN_ReloadPlugins;
00124   wrapperNPNFuncs.getJavaEnv       = NPN_GetJavaEnv;
00125   wrapperNPNFuncs.getJavaPeer      = NPN_GetJavaPeer;
00126   wrapperNPNFuncs.getvalue         = NPN_GetValue;
00127   wrapperNPNFuncs.setvalue         = NPN_SetValue;
00128   wrapperNPNFuncs.invalidaterect   = NPN_InvalidateRect;
00129   wrapperNPNFuncs.invalidateregion = NPN_InvalidateRegion;
00130   wrapperNPNFuncs.forceredraw      = NPN_ForceRedraw;
00131 
00132   plugin_NP_Initialize(&wrapperNPNFuncs);
00133 
00134   return TRUE;
00135 }
00136 
00137 void nsPluginThread::shut()
00138 {
00139   dbgOut1("nsPluginThread::shut");
00140 
00141   if (!plugin_NP_Shutdown)
00142     plugin_NP_Shutdown();
00143 
00144   if (!pluginLibrary)
00145     UnloadRealPlugin(pluginLibrary);
00146 }
00147 
00148 // NPP_ call translator
00149 DWORD nsPluginThread::callNPP(npapiAction aAction, DWORD aP1, DWORD aP2, 
00150                               DWORD aP3, DWORD aP4, DWORD aP5, 
00151                               DWORD aP6, DWORD aP7)
00152 {
00153   // don't enter untill thread is ready
00154   while (isBusy()) {
00155     Sleep(0);
00156   }
00157 
00158   mP1 = aP1;
00159   mP2 = aP2;
00160   mP3 = aP3;
00161   mP4 = aP4;
00162   mP5 = aP5;
00163   mP6 = aP6;
00164   mP7 = aP7;
00165 
00166   doAction(aAction);
00167 
00168   // don't return untill thread is ready
00169   while (isBusy()) {
00170     Sleep(0);
00171   }
00172 
00173   return NPERR_NO_ERROR;
00174 }
00175 void nsPluginThread::dispatch()
00176 {
00177   dbgOut2("nsPluginThread::dispatch: %s", FormatAction(mAction));
00178 
00179   switch (mAction) {
00180     case action_npp_new:
00181       pluginNPPFuncs.newp((NPMIMEType)mP1, (NPP)mP2, (uint16)mP3, (int16)mP4, (char**)mP5, (char**)mP6, (NPSavedData*)mP7);
00182       break;
00183     case action_npp_destroy:
00184       pluginNPPFuncs.destroy((NPP)mP1, (NPSavedData**)mP2);
00185       break;
00186     case action_npp_set_window:
00187       pluginNPPFuncs.setwindow((NPP)mP1, (NPWindow*)mP2);
00188       break;
00189     case action_npp_new_stream:
00190       pluginNPPFuncs.newstream((NPP)mP1, (NPMIMEType)mP2, (NPStream*)mP3, (NPBool)mP4, (uint16*)mP5);
00191       break;
00192     case action_npp_destroy_stream:
00193     {
00194       NPStream * stream = (NPStream *)mP2;
00195       pluginNPPFuncs.destroystream((NPP)mP1, stream, (NPError)mP3);
00196       break;
00197     }
00198     case action_npp_stream_as_file:
00199     {
00200       NPStream * stream = (NPStream *)mP2;
00201       pluginNPPFuncs.asfile((NPP)mP1, stream, (char*)mP3);
00202       break;
00203     }
00204     case action_npp_write_ready:
00205       pluginNPPFuncs.writeready((NPP)mP1, (NPStream *)mP2);
00206       break;
00207     case action_npp_write:
00208       pluginNPPFuncs.write((NPP)mP1, (NPStream *)mP2, (int32)mP3, (int32)mP4, (void *)mP5);
00209       break;
00210     case action_npp_print:
00211       pluginNPPFuncs.print((NPP)mP1, (NPPrint*)mP2);
00212       break;
00213     case action_npp_handle_event:
00214       pluginNPPFuncs.event((NPP)mP1, (void *)mP2);
00215       break;
00216     case action_npp_url_notify:
00217       pluginNPPFuncs.urlnotify((NPP)mP1, (const char*)mP2, (NPReason)mP3, (void*)mP4);
00218       break;
00219     case action_npp_get_java_class:
00220       //pluginNPPFuncs.javaClass;
00221       break;
00222     case action_npp_get_value:
00223       pluginNPPFuncs.getvalue((NPP)mP1, (NPPVariable)mP2, (void *)mP3);
00224       break;
00225     case action_npp_set_value:
00226       pluginNPPFuncs.setvalue((NPP)mP1, (NPNVariable)mP2, (void *)mP3);
00227       break;
00228     default:
00229       dbgOut1("Unexpected action!");
00230       break;
00231   }
00232 }
00233