/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* ***** BEGIN LICENSE BLOCK ***** * Version: NPL 1.1/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Netscape Public License * Version 1.1 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * http://www.mozilla.org/NPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is mozilla.org code. * * The Initial Developer of the Original Code is * Netscape Communications Corporation. * Portions created by the Initial Developer are Copyright (C) 1998 * the Initial Developer. All Rights Reserved. * * Contributor(s): * * Alternatively, the contents of this file may be used under the terms of * either the GNU General Public License Version 2 or later (the "GPL"), or * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the NPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the NPL, the GPL or the LGPL. * * ***** END LICENSE BLOCK ***** */ /**************************************************************************** Module Notes: ============= Overview: This little win32 application knows how to load the modules for intalled version netscape and mozilla browsers. It preloads these modules in order to make overall startup time seem faster to our end users. Notes: We assume that this preloader is stored with the mozilla/netscape install somewhere, and that a link to that file is stored in either the CURRENT_USER startup folder or the ALL_USERS startup folder. If it's get's put somewhere else (like DEFAULT_USERS) then the code we use to remove ourselves from the startup folder needs to be adjusted accordingly. Who When What Changed ================================================================== rickg 03.15.01 Version 1.0 of the preloader; proof of concept. rickg 04.16.01 changed code to use system-tray API's directly rickg 04.17.01 added menu code to system tray to allow user to disable us rickg 04.18.01 added code to auto-remove the preloader from startup folder rickg 04.18.01 switched strings to resource files for easier localization. rickg 04.23.01 added code to prevent multiple instances rickg 04.23.01 added code to prevent preloader operation if browser is already running rickg 04.23.01 added code to display unique icon (ugly yellow) if browser is already running. rickg 04.23.01 added code to get/set "tuning" config settings from config dialog rickg 04.24.01 added code to get/set "tuning" settings from registry rickg 04.24.01 added accelerators to menu, and changed tooltip rickg 04.24.01 moved more strings to resource file rickg 04.24.01 hooked up "tuning" config settings for gModulePercent and gFrequencyPercent rickg 04.27.01 hooked up "tuning" config settings for gEntryPercent rickg 04.27.01 added a new config setting that specifies which browser instance to preload ****************************************************************************/ //additional includes #include "resrc1.h" #include #include #include #include //trayicon notification message #define WM_TRAY WM_USER+0x100+5 static NOTIFYICONDATA gTrayData = {0,0,0,0,0,0,0}; static HWND gMainWindow=0; static HICON gCheckIcons[2] = {0,0}; static HICON gBrowserRunningIcon = {0}; static const int kLoadTimerID = 2; static const int kPingModuleTimerID = 3; static char gExePath[1024]={0}; //used to keep a path to our executable... static const char* thePathSep="\\"; static const char* gBrowserWindowName=0; static const char *gRegKey=0; static const char *gRegSubKey=0; static HINSTANCE gMainInst=0; //application main instance handle static int gModulePercent = 100; //This tells us the percent of modules to load (default value) static int gEntryPercent = 50; //Tells us the % of entry points per module (default value) static int gFrequencyPercent = 50; //Tells us relative frequency to call entry points (default value) /*********************************************************** call this to change the icon shown for our tray app ***********************************************************/ void SetTrayIcon(const HICON hIcon){ gTrayData.hIcon=hIcon; Shell_NotifyIcon(NIM_MODIFY, &gTrayData); } /*********************************************************** call this to change the tooltip shown for our tray app ***********************************************************/ void SetTrayToolTip(const char *aTip){ if(aTip) { if(!gTrayData.szTip[0]) //if previously no tooltip gTrayData.uFlags=gTrayData.uFlags | NIF_TIP; strcpy(gTrayData.szTip, aTip); } else gTrayData.uFlags=NIF_ICON | NIF_MESSAGE; Shell_NotifyIcon(NIM_MODIFY, &gTrayData); } /*********************************************************** call this to init and display the tray app. ***********************************************************/ void ShowTrayApp(bool aVisible) { if(aVisible) { gTrayData.cbSize=sizeof(NOTIFYICONDATA); gTrayData.hWnd=gMainWindow; gTrayData.uID=IDI_CHECK; //our tray ID gTrayData.uFlags= NIF_ICON | NIF_MESSAGE | NIF_TIP; gTrayData.uCallbackMessage=WM_TRAY; //send a WM_TRAY message when users clicks in our tray window gTrayData.hIcon=gCheckIcons[0]; //init our default icon Shell_NotifyIcon(NIM_ADD, &gTrayData); //now show our tray icon char theTip[256]; if(LoadString(gMainInst,IDS_TOOLTIP,theTip,sizeof(theTip))){ SetTrayToolTip(theTip); } else SetTrayToolTip("Click to configure moz-preloader"); SetTrayIcon(gCheckIcons[0]); } else { Shell_NotifyIcon(NIM_DELETE, &gTrayData); } } //******************************************************************** static bool gUseFullModuleList = false; //this enum distinguishes version of netscape (and mozilla). enum eAppVersion {eNetscape65, eNetscape60, eMozilla, eNetscapePre60, eUserPath,eUnknownVersion,eAutoDetect}; static eAppVersion gAppVersion=eNetscape65; //Constants for my DLL loader to use... static char gMozPath[2048]= {0}; static char gUserPath[2048]= {0}; static int gPreloadJava = 1; static char gMozModuleList[4096] = {0}; static char* gModuleCP = gMozModuleList; static int gModuleCount=0; static int gMozPathLen=0; static int gUserPathLen=0; static const char *theMozPath=0; static int gDLLIndex=0; /********************************************************* This counts the number of unique modules in the given list of modules, by counting semicolons. *********************************************************/ int CountModules(char*&aModuleList) { char *cp=aModuleList; int count=0; while(*cp) { if(*cp) { count++; } char *theSemi=strchr(cp,';'); if(theSemi) { cp=theSemi+1; //skip the semi } } return count; } /********************************************************* This list describes the set of modules for NS6.0 XXX This data should really live in a string table too. *********************************************************/ int Get60ModuleList(char*&aModuleList) { static char* theModuleList = "nspr4;plds4;plc4;mozreg;xpcom;img3250;zlib;" \ "gkgfxwin;gkwidget;" \ "components\\gkparser;" \ "jpeg3250;" \ "js3250;" \ "jsj3250;" \ "jsdom;" \ "components\\jsloader;" \ "components\\activation;" \ "components\\appcomps;" \ "components\\addrbook;" \ "components\\appshell;" \ "components\\caps;" \ "components\\chardet;" \ "components\\chrome;" \ "components\\cookie;" \ "components\\docshell;" \ "components\\editor;" \ "components\\gkhtml;" \ "components\\gkplugin;" \ "components\\gkview;" \ "gkwidget;" \ "components\\jar50;" \ "components\\lwbrk;" \ "mozreg;" \ "components\\necko;" \ "components\\nsgif;" \ "components\\nslocale;" \ "components\\nsprefm;" \ "components\\profile;" \ "components\\psmglue;" \ "components\\rdf;" \ "components\\shistory;" \ "components\\strres;" \ "components\\txmgr;" \ "components\\txtsvc;" \ "components\\ucharuti;" \ "components\\uconv;" \ "components\\ucvlatin;" \ "components\\ucvcn;" \ "components\\ucvja;" \ "components\\urildr;" \ "components\\wallet;" \ "components\\xpc3250;" \ "components\\xpinstal;" \ "components\\xppref32;" \ "components\\mozbrwsr;" \ "components\\nsjpg;" \ "components\\oji;" \ "msgbsutl;" \ "components\\mork;" \ "components\\msglocal;" \ "xprt;" \ "xptl;" \ "xpcs;"; strcpy(aModuleList,theModuleList); return CountModules(theModuleList); } /********************************************************* This list describes the set of modules for NS6.5 XXX This data should really live in a string table too. *********************************************************/ int Get65ModuleList(char *&aModuleList) { static char* theModuleList = "nspr4;plds4;plc4;mozreg;xpcom;img3250;zlib;" \ "gkgfxwin;gkwidget;" \ "components\\gkparser;" \ "jpeg3250;" \ "js3250;" \ "jsj3250;" \ "jsdom;" \ "components\\jsloader;" \ "components\\activation;" \ "components\\addrbook;" \ "components\\appcomps;" \ "components\\appshell;" \ "components\\embedcomponents;" "components\\caps;" \ "components\\chardet;" \ "components\\chrome;" \ "components\\cookie;" \ "components\\docshell;" \ "components\\editor;" \ "components\\gkplugin;" \ "components\\gkview;" \ "gkwidget;" \ "gfx2;" \ "components\\jar50;" \ "components\\lwbrk;" \ "components\\necko;" \ "components\\nsgif;" \ "components\\nslocale;" \ "components\\nsprefm;" \ "components\\profile;" \ "components\\psmglue;" \ "components\\rdf;" \ "components\\shistory;" \ "components\\strres;" \ "components\\txmgr;" \ "components\\txtsvc;" \ "components\\ucharuti;" \ "components\\uconv;" \ "components\\ucvlatin;" \ "components\\ucvcn;" \ "components\\ucvja;" \ "components\\urildr;" \ "components\\wallet;" \ "components\\xpc3250;" \ "components\\xpinstal;" \ "components\\xppref32;" \ "components\\xmlextras;" \ "components\\gklayout;" \ "components\\gkcontent;" \ "components\\mozbrwsr;" \ "components\\nsjpg;" \ "components\\oji;" \ "msgbsutl;" \ "components\\mork;" \ "components\\msglocal;" \ "xprt;" \ "xptl;" \ "xpcs;"; strcpy(aModuleList,theModuleList); return CountModules(theModuleList); } /********************************************************* ... *********************************************************/ static char gKeyBuffer[512]={0}; static char gSubKeyBuffer[128]={0}; static char gWinNameBuffer[128]={0}; //this is the expected browser name. void GetPathFromRegistry(eAppVersion aVersion, const char *&aKey, const char *&aSubkey, char *&aModuleList, int &aSize) { switch(aVersion) { case eMozilla: aKey=(LoadString(gMainInst,IDS_MOZ_KEY,gKeyBuffer,sizeof(gKeyBuffer))) ? gKeyBuffer : "Software\\Mozilla.org\\Mozilla\\0.8 (en)\\Main"; aSubkey = (LoadString(gMainInst,IDS_SUBKEY_INSTALL,gSubKeyBuffer,sizeof(gSubKeyBuffer))) ? gSubKeyBuffer : "Install Directory"; gBrowserWindowName = (LoadString(gMainInst,IDS_MOZ_WINDOWNAME,gWinNameBuffer,sizeof(gWinNameBuffer))) ? gWinNameBuffer: "- Mozilla"; aSize=Get65ModuleList(aModuleList); break; case eNetscape65: aKey=(LoadString(gMainInst,IDS_NS65_KEY,gKeyBuffer,sizeof(gKeyBuffer))) ? gKeyBuffer : "Software\\Netscape\\Netscape 6\\6.5 (en)\\Main"; aSubkey = (LoadString(gMainInst,IDS_SUBKEY_INSTALL,gSubKeyBuffer,sizeof(gSubKeyBuffer))) ? gSubKeyBuffer : "Install Directory"; gBrowserWindowName = (LoadString(gMainInst,IDS_NS_WINDOWNAME,gWinNameBuffer,sizeof(gWinNameBuffer))) ? gWinNameBuffer: "- Netstacpe 6"; aSize=Get65ModuleList(aModuleList); break; case eNetscape60: aKey=(LoadString(gMainInst,IDS_NS60_KEY,gKeyBuffer,sizeof(gKeyBuffer))) ? gKeyBuffer : "Software\\Netscape\\Netscape 6\\6.0 (en)\\Main"; aSubkey = (LoadString(gMainInst,IDS_SUBKEY_PATH,gSubKeyBuffer,sizeof(gSubKeyBuffer))) ? gSubKeyBuffer : "Path"; gBrowserWindowName = (LoadString(gMainInst,IDS_NS_WINDOWNAME,gWinNameBuffer,sizeof(gWinNameBuffer))) ? gWinNameBuffer: "- Netstacpe 6"; aSize=Get60ModuleList(aModuleList); break; case eNetscapePre60: aKey=(LoadString(gMainInst,IDS_PRE60_KEY,gKeyBuffer,sizeof(gKeyBuffer))) ? gKeyBuffer : "Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\netscp6.exe"; aSubkey = (LoadString(gMainInst,IDS_SUBKEY_PATH,gSubKeyBuffer,sizeof(gSubKeyBuffer))) ? gSubKeyBuffer : "Path"; gBrowserWindowName = (LoadString(gMainInst,IDS_NS_WINDOWNAME,gWinNameBuffer,sizeof(gWinNameBuffer))) ? gWinNameBuffer: "- Netstacpe 6"; aSize=Get60ModuleList(aModuleList); break; case eUserPath: aKey = 0; aSubkey= 0; strcpy(gMozPath,gUserPath); aSize=Get65ModuleList(aModuleList); gBrowserWindowName = (LoadString(gMainInst,IDS_MOZ_WINDOWNAME,gWinNameBuffer,sizeof(gWinNameBuffer))) ? gWinNameBuffer: "- Mozilla"; break; case eUnknownVersion: break; } } /********************************************************* Get the path to the netscape6 browser via the registry... *********************************************************/ bool GetMozillaRegistryInfo(eAppVersion &aVersion) { //first we try to get the registry info based on the command line settings. //if that fails, we try others. bool found=false; LONG theOpenResult = 1; //any non-zero will do to initialize this... if ( aVersion == eUserPath) { found=true; } while((ERROR_SUCCESS!=theOpenResult) && (aVersiongMaxEntryIndex) gMaxEntryIndex=theEntryCount; //we track the highest index we find. } return theInstance; } /**************************************************************** Call this once for each module you want to preload. We use gEntryPercent to determine what percentage of entry p oints to acquire (we will always get at least 1, and we'll skip at most 20 entry points at a time). ****************************************************************/ HINSTANCE LoadModule(const char* aName) { //we operate on gMozPath directly to avoid an unnecessary string copy. //when we're done with this method, we reset gMozpath to it's original value for reuse. strcat(gMozPath,aName); strcat(gMozPath,".dll"); HINSTANCE theInstance = LoadModuleFromPath(gMozPath); gMozPath[gMozPathLen]=0; return theInstance; } BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam ) { char buf[256]={0}; if(GetWindowText(hwnd,buf,sizeof(buf))){ if(strstr(buf,gBrowserWindowName)) { return FALSE; //stop iterating now... } } return TRUE; } /**************************************************************** Call this to detect whether the browser is running. We cache the result to speed up this function (by preventing subsuquent searches of top level windows). ****************************************************************/ bool BrowserIsRunning() { static bool gBrowserIsRunning=false; if(!gBrowserIsRunning) { if(!EnumWindows(EnumWindowsProc,0)) { gBrowserIsRunning=true; } } return gBrowserIsRunning; } /**************************************************************** This function get's called repeatedly to call on a timer, and it calls GetProcAddr() to keep modules from paging. NOTE: This method uses gFrequencyPercent to determine how much work to do each time it gets called. ****************************************************************/ VOID CALLBACK KeepAliveTimerProc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime ) { //let's go see if mozilla is running. //if so, then bail out without pinging modules... if(BrowserIsRunning()) { return; } static bool theTimerIsRunning=false; static int theCurrentModule=0; static int theCurrentProc=0; if(!theTimerIsRunning) { //ignore other timer calls till we're done. theTimerIsRunning=true; //constrain the step to 1..gDLLCount static const double kPercent=gFrequencyPercent/100.0; static const int kMaxSteps=1+int(3*gDLLCount*kPercent); int count=0; //this version iterates the entry points in each module before moving to the next module while(count= gDLLCount) { theCurrentModule = 0; theCurrentProc = (theCurrentProc>=gMaxEntryIndex) ? 0 : theCurrentProc+1; } else { theCurrentModule++; } #else //breadth first... if(theCurrentProc >= gMaxEntryIndex) { theCurrentProc=0; theCurrentModule = (theCurrentModule>=gDLLCount) ? 0 : theCurrentModule+1; } else { theCurrentProc++; } #endif } theTimerIsRunning=false; } } /**************************************************************** This gets called repeatedly by a windows timer, and loads the modules that it gets from the modulelist (see top of this file). This routine has been updated to account for the gModulesPercent setting (1-100). We'll load modules until we cross over this percentage. ****************************************************************/ VOID CALLBACK LoadModuleTimerProc(HWND hwnd, UINT uMsg, UINT idEvent,DWORD dwTime) { static bool theTimerIsRunning=false; static int gTrayIconIndex = 0; if(!theTimerIsRunning) { theTimerIsRunning=true; //gDLLCount is the number of modules loaded so far //gModuleCount is the total number of modules we know about //gModulePercent is the total % of modules we're being asked to load (config setting) double theMaxPercentToLoad=gModulePercent/100.0; //we'll only load more modules if haven't loaded the max percentage of modules //based on the configuration UI (stored in gModulePercent). const int theModulePerCallCount = 3; // specifies how many modules to load per timer callback. bool done=false; for(int theModuleIndex=0;theModuleIndex