Back to index

lightning-sunbird  0.9+nobinonly
np_entry.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 
00039 //
00040 // Main plugin entry point implementation -- exports from the 
00041 // plugin library
00042 //
00043 #include "xp.h"
00044 #include "plugbase.h"
00045 #include "logger.h"
00046 
00047 CLogger * pLogger = NULL;
00048 static char szTarget[] = LOGGER_DEFAULT_TARGET;
00049 
00050 NPNetscapeFuncs NPNFuncs;
00051 
00052 NPError OSCALL NP_Shutdown()
00053 {
00054   if (pLogger) {
00055     delete pLogger;
00056     pLogger = NULL;
00057   }
00058   return NPERR_NO_ERROR;
00059 }
00060 
00061 static NPError fillPluginFunctionTable(NPPluginFuncs* aNPPFuncs)
00062 {
00063   if(aNPPFuncs == NULL)
00064     return NPERR_INVALID_FUNCTABLE_ERROR;
00065 
00066   // Set up the plugin function table that Netscape will use to
00067   // call us. Netscape needs to know about our version and size   
00068   // and have a UniversalProcPointer for every function we implement.
00069 
00070   aNPPFuncs->version       = (NP_VERSION_MAJOR << 8) | NP_VERSION_MINOR;
00071 #ifdef XP_MAC
00072   aNPPFuncs->newp          = NewNPP_NewProc(Private_New);
00073   aNPPFuncs->destroy       = NewNPP_DestroyProc(Private_Destroy);
00074   aNPPFuncs->setwindow     = NewNPP_SetWindowProc(Private_SetWindow);
00075   aNPPFuncs->newstream     = NewNPP_NewStreamProc(Private_NewStream);
00076   aNPPFuncs->destroystream = NewNPP_DestroyStreamProc(Private_DestroyStream);
00077   aNPPFuncs->asfile        = NewNPP_StreamAsFileProc(Private_StreamAsFile);
00078   aNPPFuncs->writeready    = NewNPP_WriteReadyProc(Private_WriteReady);
00079   aNPPFuncs->write         = NewNPP_WriteProc(Private_Write);
00080   aNPPFuncs->print         = NewNPP_PrintProc(Private_Print);
00081   aNPPFuncs->event         = NewNPP_HandleEventProc(Private_HandleEvent);    
00082   aNPPFuncs->urlnotify     = NewNPP_URLNotifyProc(Private_URLNotify);               
00083   aNPPFuncs->getvalue      = NewNPP_GetValueProc(Private_GetValue);
00084   aNPPFuncs->setvalue      = NewNPP_SetValueProc(Private_SetValue);
00085 #else
00086   aNPPFuncs->newp          = NPP_New;
00087   aNPPFuncs->destroy       = NPP_Destroy;
00088   aNPPFuncs->setwindow     = NPP_SetWindow;
00089   aNPPFuncs->newstream     = NPP_NewStream;
00090   aNPPFuncs->destroystream = NPP_DestroyStream;
00091   aNPPFuncs->asfile        = NPP_StreamAsFile;
00092   aNPPFuncs->writeready    = NPP_WriteReady;
00093   aNPPFuncs->write         = NPP_Write;
00094   aNPPFuncs->print         = NPP_Print;
00095   aNPPFuncs->event         = NPP_HandleEvent;
00096   aNPPFuncs->urlnotify     = NPP_URLNotify;
00097   aNPPFuncs->getvalue      = NPP_GetValue;
00098   aNPPFuncs->setvalue      = NPP_SetValue;
00099 #endif
00100   aNPPFuncs->javaClass     = NULL;
00101 
00102   return NPERR_NO_ERROR;
00103 }
00104 
00105 static NPError fillNetscapeFunctionTable(NPNetscapeFuncs* aNPNFuncs)
00106 {
00107   if(aNPNFuncs == NULL)
00108     return NPERR_INVALID_FUNCTABLE_ERROR;
00109 
00110   if(HIBYTE(aNPNFuncs->version) > NP_VERSION_MAJOR)
00111     return NPERR_INCOMPATIBLE_VERSION_ERROR;
00112 
00113   if(aNPNFuncs->size < sizeof(NPNetscapeFuncs))
00114     return NPERR_INVALID_FUNCTABLE_ERROR;
00115 
00116   NPNFuncs.size             = aNPNFuncs->size;
00117   NPNFuncs.version          = aNPNFuncs->version;
00118   NPNFuncs.geturlnotify     = aNPNFuncs->geturlnotify;
00119   NPNFuncs.geturl           = aNPNFuncs->geturl;
00120   NPNFuncs.posturlnotify    = aNPNFuncs->posturlnotify;
00121   NPNFuncs.posturl          = aNPNFuncs->posturl;
00122   NPNFuncs.requestread      = aNPNFuncs->requestread;
00123   NPNFuncs.newstream        = aNPNFuncs->newstream;
00124   NPNFuncs.write            = aNPNFuncs->write;
00125   NPNFuncs.destroystream    = aNPNFuncs->destroystream;
00126   NPNFuncs.status           = aNPNFuncs->status;
00127   NPNFuncs.uagent           = aNPNFuncs->uagent;
00128   NPNFuncs.memalloc         = aNPNFuncs->memalloc;
00129   NPNFuncs.memfree          = aNPNFuncs->memfree;
00130   NPNFuncs.memflush         = aNPNFuncs->memflush;
00131   NPNFuncs.reloadplugins    = aNPNFuncs->reloadplugins;
00132   NPNFuncs.getJavaEnv       = aNPNFuncs->getJavaEnv;
00133   NPNFuncs.getJavaPeer      = aNPNFuncs->getJavaPeer;
00134   NPNFuncs.getvalue         = aNPNFuncs->getvalue;
00135   NPNFuncs.setvalue         = aNPNFuncs->setvalue;
00136   NPNFuncs.invalidaterect   = aNPNFuncs->invalidaterect;
00137   NPNFuncs.invalidateregion = aNPNFuncs->invalidateregion;
00138   NPNFuncs.forceredraw      = aNPNFuncs->forceredraw;
00139 
00140   return NPERR_NO_ERROR;
00141 }
00142 
00143 //
00144 // Some exports are different on different platforms
00145 //
00146 
00147 /**************************************************/
00148 /*                                                */
00149 /*                   Windows                      */
00150 /*                    OS/2                        */
00151 /**************************************************/
00152 #if defined(XP_WIN) || defined(XP_OS2)
00153 
00154 NPError OSCALL NP_Initialize(NPNetscapeFuncs* aNPNFuncs)
00155 {
00156   NPError rv = fillNetscapeFunctionTable(aNPNFuncs);
00157 
00158   pLogger = new CLogger(szTarget);
00159   if(pLogger) {
00160     DWORD dwTickEnter = XP_GetTickCount();
00161     pLogger->blockDumpToFile(TRUE);
00162     DWORD dwTickReturn = XP_GetTickCount();
00163     pLogger->appendToLog(action_np_initialize, dwTickEnter, dwTickReturn, NPERR_NO_ERROR);
00164   }
00165   return rv;
00166 }
00167 
00168 NPError OSCALL NP_GetEntryPoints(NPPluginFuncs* aNPPFuncs)
00169 {
00170   return fillPluginFunctionTable(aNPPFuncs);
00171 }
00172 
00173 #endif //XP_WIN || XP_OS2
00174 
00175 /**************************************************/
00176 /*                                                */
00177 /*                    Unix                        */
00178 /*                                                */
00179 /**************************************************/
00180 #ifdef XP_UNIX
00181 
00182 NPError NP_Initialize(NPNetscapeFuncs* aNPNFuncs, NPPluginFuncs* aNPPFuncs)
00183 {
00184   NPError rv = fillNetscapeFunctionTable(aNPNFuncs);
00185   if(rv != NPERR_NO_ERROR)
00186     return rv;
00187 
00188   rv = fillPluginFunctionTable(aNPPFuncs);
00189   if(rv != NPERR_NO_ERROR)
00190     return rv;
00191 
00192   pLogger = new CLogger(szTarget);
00193   if(pLogger) {
00194     DWORD dwTickEnter = XP_GetTickCount();
00195     pLogger->blockDumpToFile(TRUE);
00196     DWORD dwTickReturn = XP_GetTickCount();
00197     pLogger->appendToLog(action_np_initialize, dwTickEnter, dwTickReturn, NPERR_NO_ERROR);
00198   }
00199 }
00200 
00201 char * NP_GetMIMEDescription(void)
00202 {
00203   return NPP_GetMIMEDescription();
00204 }
00205 
00206 NPError NP_GetValue(void *future, NPPVariable aVariable, void *aValue)
00207 {
00208   return NPP_GetValue((NPP_t *)future, aVariable, aValue);
00209 }
00210 
00211 #endif //XP_UNIX
00212 
00213 /**************************************************/
00214 /*                                                */
00215 /*                     Mac                        */
00216 /*                                                */
00217 /**************************************************/
00218 #ifdef XP_MAC
00219 
00220 #if !TARGET_API_MAC_CARBON
00221 QDGlobals* gQDPtr; // Pointer to Netscape's QuickDraw globals
00222 #endif
00223 
00224 short gResFile; // Refnum of the plugin's resource file
00225 
00226 NPError Private_Initialize(void)
00227 {
00228   EnterCodeResource();
00229   pLogger = new CLogger(szTarget);
00230   if(pLogger) {
00231     DWORD dwTickEnter = XP_GetTickCount();
00232     pLogger->blockDumpToFile(TRUE);
00233     DWORD dwTickReturn = XP_GetTickCount();
00234     pLogger->appendToLog(action_np_initialize, dwTickEnter, dwTickReturn, NPERR_NO_ERROR);
00235   }
00236   ExitCodeResource();
00237   return rv;
00238 }
00239 
00240 void Private_Shutdown(void)
00241 {
00242   EnterCodeResource();
00243   NS_PluginShutdown();
00244   __destroy_global_chain();
00245   ExitCodeResource();
00246 }
00247 
00248 void SetUpQD(void);
00249 
00250 void SetUpQD(void)
00251 {
00252   ProcessSerialNumber PSN;
00253   FSSpec              myFSSpec;
00254   Str63               name;
00255   ProcessInfoRec      infoRec;
00256   OSErr               result = noErr;
00257   CFragConnectionID   connID;
00258   Str255              errName;
00259 
00260   // Memorize the pluginıs resource file refnum for later use.
00261   gResFile = CurResFile();
00262 
00263 #if !TARGET_API_MAC_CARBON
00264   // Ask the system if CFM is available.
00265   long response;
00266   OSErr err = Gestalt(gestaltCFMAttr, &response);
00267   Boolean hasCFM = BitTst(&response, 31-gestaltCFMPresent);
00268 
00269   if (hasCFM) {
00270     // GetProcessInformation takes a process serial number and 
00271     // will give us back the name and FSSpec of the application.
00272     // See the Process Manager in IM.
00273     infoRec.processInfoLength = sizeof(ProcessInfoRec);
00274     infoRec.processName = name;
00275     infoRec.processAppSpec = &myFSSpec;
00276 
00277     PSN.highLongOfPSN = 0;
00278     PSN.lowLongOfPSN = kCurrentProcess;
00279 
00280     result = GetProcessInformation(&PSN, &infoRec);
00281   }
00282        else
00283     // If no CFM installed, assume it must be a 68K app.
00284     result = -1;            
00285 
00286   if (result == noErr) {
00287     // Now that we know the app name and FSSpec, we can call GetDiskFragment
00288     // to get a connID to use in a subsequent call to FindSymbol (it will also
00289     // return the address of ³main² in app, which we ignore).  If GetDiskFragment 
00290     // returns an error, we assume the app must be 68K.
00291     Ptr mainAddr;    
00292     result =  GetDiskFragment(infoRec.processAppSpec, 0L, 0L, infoRec.processName,
00293                               kReferenceCFrag, &connID, (Ptr*)&mainAddr, errName);
00294   }
00295 
00296   if (result == noErr) {
00297     // The app is a PPC code fragment, so call FindSymbol
00298     // to get the exported ³qd² symbol so we can access its
00299     // QuickDraw globals.
00300     CFragSymbolClass symClass;
00301     result = FindSymbol(connID, "\pqd", (Ptr*)&gQDPtr, &symClass);
00302   }
00303   else {
00304     // The app is 68K, so use its A5 to compute the address
00305     // of its QuickDraw globals.
00306     gQDPtr = (QDGlobals*)(*((long*)SetCurrentA5()) - (sizeof(QDGlobals) - sizeof(GrafPtr)));
00307   }
00308 #endif /* !TARGET_API_MAC_CARBON */
00309 }
00310 
00311 NPError main(NPNetscapeFuncs* nsTable, NPPluginFuncs* pluginFuncs, NPP_ShutdownUPP* unloadUpp);
00312 
00313 #if !TARGET_API_MAC_CARBON
00314 #pragma export on
00315 #if GENERATINGCFM
00316 RoutineDescriptor mainRD = BUILD_ROUTINE_DESCRIPTOR(uppNPP_MainEntryProcInfo, main);
00317 #endif
00318 #pragma export off
00319 #endif /* !TARGET_API_MAC_CARBON */
00320 
00321 
00322 NPError main(NPNetscapeFuncs* aNPNFuncs, NPPluginFuncs* aNPPFuncs, NPP_ShutdownUPP* aUnloadUpp)
00323 {
00324   EnterCodeResource();
00325   NPError rv = NPERR_NO_ERROR;
00326 
00327   if (aUnloadUpp == NULL)
00328     rv = NPERR_INVALID_FUNCTABLE_ERROR;
00329 
00330   if (rv == NPERR_NO_ERROR)
00331     rv = fillNetscapeFunctionTable(aNPNFuncs);
00332 
00333   if (rv == NPERR_NO_ERROR) {
00334     // defer static constructors until the global functions are initialized.
00335     __InitCode__();
00336     rv = fillPluginFunctionTable(aNPPFuncs);
00337   }
00338 
00339   *aUnloadUpp = NewNPP_ShutdownProc(Private_Shutdown);
00340   SetUpQD();
00341   rv = Private_Initialize();
00342        
00343   ExitCodeResource();
00344   return rv;
00345 }
00346 #endif //XP_MAC