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