Back to index

lightning-sunbird  0.9+nobinonly
npshell.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 #include <windows.h>
00039 #include <assert.h>
00040 
00041 #include "resource.h"
00042 
00043 #include "plugin.h" // this includes npapi.h
00044 #include "utils.h"
00045 #include "dbg.h"
00046 
00047 char szAppName[] = "NPNULL";
00048 
00049 //---------------------------------------------------------------------------
00050 // NPP_Initialize:
00051 //---------------------------------------------------------------------------
00052 NPError NPP_Initialize(void)
00053 {
00054   RegisterNullPluginWindowClass();
00055   return NPERR_NO_ERROR;
00056 }
00057 
00058 //---------------------------------------------------------------------------
00059 // NPP_Shutdown:
00060 //---------------------------------------------------------------------------
00061 void NPP_Shutdown(void)
00062 {
00063   UnregisterNullPluginWindowClass();
00064 }
00065 
00066 //---------------------------------------------------------------------------
00067 // NPP_New:
00068 //---------------------------------------------------------------------------
00069 NPError NP_LOADDS NPP_New(NPMIMEType pluginType,
00070                           NPP pInstance,
00071                           uint16 mode,
00072                           int16 argc,
00073                           char* argn[],
00074                           char* argv[],
00075                           NPSavedData* saved)
00076 {
00077   if(pInstance == NULL)
00078     return NPERR_INVALID_INSTANCE_ERROR;
00079 
00080   // See if the content provider specified from where to fetch the plugin
00081   char * szPageURL = NULL;
00082   char * szFileURL = NULL;
00083   char * szFileExtension = NULL;
00084   char * buf = NULL;
00085   BOOL bHidden = FALSE;
00086 
00087   for(int i = 0; i < argc; i++)
00088   {
00089     if(lstrcmpi(argn[i],"pluginspage") == 0 && argv[i] != NULL)
00090       szPageURL = (char *)argv[i];
00091     else if(lstrcmpi(argn[i],"codebase") == 0 && argv[i] != NULL)
00092       szPageURL = (char *)argv[i];
00093     else if(lstrcmpi(argn[i],"pluginurl") == 0 && argv[i] != NULL)
00094       szFileURL = (char *)argv[i];
00095     else if(lstrcmpi(argn[i],"classid") == 0 && argv[i] != NULL)
00096       szFileURL = (char *)argv[i];
00097     else if(lstrcmpi(argn[i],"SRC") == 0 && argv[i] != NULL)
00098       buf = (char *)argv[i];
00099     else if(lstrcmpi(argn[i],"HIDDEN") == 0 && argv[i] != NULL)
00100       bHidden = (lstrcmp((char *)argv[i], "TRUE") == 0);
00101   }
00102 
00103   /* some post-processing on the filename to attempt to extract the extension:  */
00104   if(buf != NULL)
00105   {
00106     buf = strrchr(buf, '.');
00107     if(buf)
00108       szFileExtension = ++buf;
00109   }
00110 
00111   CPlugin * pPlugin = new CPlugin(hInst, 
00112                                   pInstance, 
00113                                   mode, 
00114                                   pluginType, 
00115                                   szPageURL, 
00116                                   szFileURL, 
00117                                   szFileExtension,
00118                                   bHidden);
00119   if(pPlugin == NULL)
00120     return NPERR_OUT_OF_MEMORY_ERROR;
00121 
00122   if(bHidden)
00123   {
00124     if(!pPlugin->init(NULL))
00125     {
00126       delete pPlugin;
00127       pPlugin = NULL;
00128       return NPERR_MODULE_LOAD_FAILED_ERROR;
00129     }
00130   }
00131 
00132   pInstance->pdata = (void *)pPlugin;
00133 
00134   return NPERR_NO_ERROR;
00135 }
00136 
00137 //---------------------------------------------------------------------------
00138 // NPP_Destroy:
00139 //---------------------------------------------------------------------------
00140 NPError NP_LOADDS
00141 NPP_Destroy(NPP pInstance, NPSavedData** save)
00142 {
00143   dbgOut1("NPP_Destroy");
00144   if(pInstance == NULL)
00145     return NPERR_INVALID_INSTANCE_ERROR;
00146 
00147   CPlugin * pPlugin = (CPlugin *)pInstance->pdata;
00148   if(pPlugin != NULL)
00149   {
00150     pPlugin->shut();
00151     delete pPlugin;
00152   }
00153 
00154   return NPERR_NO_ERROR;
00155 }
00156 
00157 //---------------------------------------------------------------------------
00158 // NPP_SetWindow:
00159 //---------------------------------------------------------------------------
00160 NPError NP_LOADDS NPP_SetWindow(NPP pInstance, NPWindow * pNPWindow)
00161 {
00162   if(pInstance == NULL)
00163   {
00164     dbgOut1("NPP_SetWindow returns NPERR_INVALID_INSTANCE_ERROR");
00165     return NPERR_INVALID_INSTANCE_ERROR;
00166   }
00167 
00168   if(pNPWindow == NULL)
00169   {
00170     dbgOut1("NPP_SetWindow returns NPERR_GENERIC_ERROR");
00171     return NPERR_GENERIC_ERROR;
00172   }
00173 
00174   HWND hWnd = (HWND)(DWORD)pNPWindow->window;
00175 
00176   CPlugin * pPlugin = (CPlugin *)pInstance->pdata;
00177   assert(pPlugin != NULL);
00178 
00179   if(pPlugin == NULL) 
00180   {
00181     dbgOut1("NPP_SetWindow returns NPERR_GENERIC_ERROR");
00182     return NPERR_GENERIC_ERROR;
00183   }
00184 
00185   if((hWnd == NULL) && (pPlugin->getWindow() == NULL)) // spurious entry
00186   {
00187     dbgOut1("NPP_SetWindow just returns with NPERR_NO_ERROR");
00188     return NPERR_NO_ERROR;
00189   }
00190 
00191   if((hWnd == NULL) && (pPlugin->getWindow() != NULL))
00192   { // window went away
00193     dbgOut1("NPP_SetWindow, going away...");
00194     pPlugin->shut();
00195     return NPERR_NO_ERROR;
00196   }
00197 
00198   if((pPlugin->getWindow() == NULL) && (hWnd != NULL))
00199   { // First time in -- no window created by plugin yet
00200     dbgOut1("NPP_SetWindow, first time");
00201 
00202     if(!pPlugin->init(hWnd))
00203     {
00204       delete pPlugin;
00205       pPlugin = NULL;
00206       return NPERR_MODULE_LOAD_FAILED_ERROR;
00207     }
00208   }
00209 
00210   if((pPlugin->getWindow() != NULL) && (hWnd != NULL))
00211   { // Netscape window has been resized
00212     dbgOut1("NPP_SetWindow, resizing");
00213     pPlugin->resize();
00214   }
00215 
00216   return NPERR_NO_ERROR;
00217 }
00218 
00219 //------------------------------------------------------------------------------------
00220 // NPP_NewStream:
00221 //------------------------------------------------------------------------------------
00222 NPError NP_LOADDS
00223 NPP_NewStream(NPP pInstance,
00224               NPMIMEType type,
00225               NPStream *stream, 
00226               NPBool seekable,
00227               uint16 *stype)
00228 {
00229   dbgOut1("NPP_NewStream");
00230   if(pInstance == NULL)
00231     return NPERR_INVALID_INSTANCE_ERROR;
00232 
00233   CPlugin * pPlugin = (CPlugin *)pInstance->pdata;
00234   assert(pPlugin != NULL);
00235 
00236   if (!pPlugin)
00237     return NPERR_GENERIC_ERROR;
00238 
00239   return pPlugin->newStream(type, stream, seekable, stype);
00240 }
00241 
00242 //------------------------------------------------------------------------------------
00243 // NPP_WriteReady:
00244 //------------------------------------------------------------------------------------
00245 int32 NP_LOADDS
00246 NPP_WriteReady(NPP pInstance, NPStream *stream)
00247 {
00248   dbgOut1("NPP_WriteReady");
00249   if(pInstance == NULL)
00250     return NPERR_INVALID_INSTANCE_ERROR;
00251 
00252   CPlugin * pPlugin = (CPlugin *)pInstance->pdata;
00253   assert(pPlugin != NULL);
00254 
00255   // We don't want any data, kill the stream
00256   NPN_DestroyStream(pInstance, stream, NPRES_DONE);
00257 
00258   return -1L;   // dont accept any bytes in NPP_Write()
00259 }
00260 
00261 //------------------------------------------------------------------------------------
00262 // NPP_Write:
00263 //------------------------------------------------------------------------------------
00264 int32 NP_LOADDS
00265 NPP_Write(NPP pInstance, NPStream *stream, int32 offset, int32 len, void *buffer)
00266 {
00267   //dbgOut1("NPP_Write");
00268   if(pInstance == NULL)
00269     return NPERR_INVALID_INSTANCE_ERROR;
00270 
00271   CPlugin * pPlugin = (CPlugin *)pInstance->pdata;
00272   assert(pPlugin != NULL);
00273 
00274   // We don't want any data, kill the stream
00275   NPN_DestroyStream(pInstance, stream, NPRES_DONE);
00276 
00277   return -1;   // tell the browser to abort the stream, don't need it
00278 }
00279 
00280 //------------------------------------------------------------------------------------
00281 // NPP_DestroyStream:
00282 //------------------------------------------------------------------------------------
00283 NPError NP_LOADDS
00284 NPP_DestroyStream(NPP pInstance, NPStream *stream, NPError reason)
00285 {
00286   dbgOut1("NPP_DestroyStream");
00287   if(pInstance == NULL)
00288     return NPERR_INVALID_INSTANCE_ERROR;
00289 
00290   CPlugin * pPlugin = (CPlugin *)pInstance->pdata;
00291   assert(pPlugin != NULL);
00292 
00293   if (!pPlugin)
00294     return NPERR_GENERIC_ERROR;
00295 
00296   return pPlugin->destroyStream(stream, reason);
00297 }
00298 
00299 //------------------------------------------------------------------------------------
00300 // NPP_StreamAsFile:
00301 //------------------------------------------------------------------------------------
00302 void NP_LOADDS
00303 NPP_StreamAsFile(NPP instance, NPStream *stream, const char* fname)
00304 {
00305   dbgOut1("NPP_StreamAsFile");
00306 }
00307 
00308 //------------------------------------------------------------------------------------
00309 // NPP_Print:
00310 //------------------------------------------------------------------------------------
00311 void NP_LOADDS NPP_Print(NPP pInstance, NPPrint * printInfo)
00312 {
00313   dbgOut2("NPP_Print, printInfo = %#08x", printInfo);
00314 
00315   CPlugin * pPlugin = (CPlugin *)pInstance->pdata;
00316   assert(pPlugin != NULL);
00317 
00318   pPlugin->print(printInfo);
00319 }
00320 
00321 void NP_LOADDS NPP_URLNotify(NPP pInstance, const char* url, NPReason reason, void* notifyData)
00322 {
00323   dbgOut2("NPP_URLNotify, URL '%s'", url);
00324 
00325   CPlugin * pPlugin = (CPlugin *)pInstance->pdata;
00326   assert(pPlugin != NULL);
00327 
00328   pPlugin->URLNotify(url);
00329 }
00330 
00331 #ifdef OJI
00332 jref NP_LOADDS NPP_GetJavaClass(void)
00333 {
00334   return NULL;
00335 }
00336 #endif