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