DXR will be turned off on Tuesday, December 29th. It will redirect to Searchfox.
See the announcement on Discourse.

DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Untracked file

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
/* -*- Mode: IDL; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 *
 * The contents of this file are subject to the Mozilla 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/MPL/
 * 
 * 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 the Mozilla browser.
 * 
 * The Initial Developer of the Original Code is Netscape
 * Communications, Inc.  Portions created by Netscape are
 * Copyright (C) 1999, Mozilla.  All Rights Reserved.
 * 
 * Author(s)
 *   Adam Lock <adamlock@netscape.com>
 *
 * Contributor(s)
 */

#include "stdafx.h"

#include <sys/types.h>
#include <sys/stat.h>

#include "shlobj.h"

static BOOL LoadMozLibraryFromCurrentDir();
static BOOL LoadMozLibraryFromRegistry(BOOL bAskUserToSetPath);
static BOOL LoadLibraryWithPATHFixup(const TCHAR *szBinDirPath);


static HMODULE ghMozCtlX = NULL;
static HMODULE ghMozCtl = NULL;

static LPCTSTR c_szMozCtlDll = _T("mozctl.dll");
static LPCTSTR c_szMozillaControlKey = _T("Software\\Mozilla\\");
static LPCTSTR c_szMozillaBinDirPathValue = _T("BinDirectoryPath");
static LPCTSTR c_szMozCtlInProcServerKey =
    _T("CLSID\\{1339B54C-3453-11D2-93B9-000000000000}\\InProcServer32");

// Message strings - should come from a resource file
static LPCTSTR c_szWarningTitle =
    _T("Mozilla Control Warning");
static LPCTSTR c_szErrorTitle =
    _T("Mozilla Control Error");
static LPCTSTR c_szPrePickFolderMsg =
    _T("The Mozilla control was unable to detect where your Mozilla "
       "layout libraries may be found. You will now be shown a directory "
       "picker for you to locate them.");
static LPCTSTR c_szPickFolderDlgTitle =
    _T("Pick where the Mozilla bin directory is located, "
       "e.g. (c:\\mozilla\\bin)");
static LPCTSTR c_szRegistryErrorMsg =
    _T("The Mozilla control was unable to store the location of the Mozilla"
       "layout libraries in the registry. This may be due to the host "
       "program running with insufficient permissions to write there.\n\n"
       "You should consider running the control once as Administrator "
       "to enable this entry to added or alternatively move the "
       "control files mozctlx.dll and mozctl.dll into the same folder as "
       "the other libraries.");
static LPCTSTR c_szLoadErrorMsg =
    _T("The Mozilla control could not be loaded correctly. This is could "
       "be due to an invalid location being specified for the Mozilla "
       "layout libraries or missing files from your installation.\n\n"
       "Visit http://www.iol.ie/~locka/mozilla/mozilla.htm for in-depth"
       "troubleshooting advice.");

BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
                     )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
        ghMozCtlX = (HMODULE) hModule;
        break;
    case DLL_THREAD_ATTACH:
        break;
    case DLL_THREAD_DETACH:
        break;
    case DLL_PROCESS_DETACH:
        if (ghMozCtl)
        {
            FreeLibrary(ghMozCtl);
            ghMozCtl = NULL;
        }
        break;
    }
    return TRUE;
}

static BOOL LoadMozLibrary(BOOL bAskUserToSetPath = TRUE)
{
    if (ghMozCtl)
    {
        return TRUE;
    }

    ghMozCtl = LoadLibrary(c_szMozCtlDll);
    if (ghMozCtl)
    {
        return TRUE;
    }

    if (LoadMozLibraryFromCurrentDir())
    {
        return TRUE;
    }

    if (LoadMozLibraryFromRegistry(bAskUserToSetPath))
    {
        return TRUE;
    }

    ::MessageBox(NULL, c_szLoadErrorMsg, c_szErrorTitle, MB_OK);

    return FALSE;
}

static BOOL LoadMozLibraryFromCurrentDir()
{
    TCHAR szMozCtlXPath[MAX_PATH + 1];

    // Get this module's path
    ZeroMemory(szMozCtlXPath, sizeof(szMozCtlXPath));
    GetModuleFileName(ghMozCtlX, szMozCtlXPath,
        sizeof(szMozCtlXPath) / sizeof(szMozCtlXPath[0]));

    // Make the control path
    TCHAR szTmpDrive[_MAX_DRIVE];
    TCHAR szTmpDir[_MAX_DIR];
    TCHAR szTmpFname[_MAX_FNAME];
    TCHAR szTmpExt[_MAX_EXT];
    _tsplitpath(szMozCtlXPath, szTmpDrive, szTmpDir, szTmpFname, szTmpExt);

    TCHAR szMozCtlPath[MAX_PATH + 1];
    ZeroMemory(szMozCtlPath, sizeof(szMozCtlPath));
    _tmakepath(szMozCtlPath, szTmpDrive, szTmpDir, c_szMozCtlDll, NULL);

    // Stat to see if the control exists
    struct _stat dirStat;
    if (_tstat(szMozCtlPath, &dirStat) == 0)
    {
        TCHAR szBinDirPath[MAX_PATH + 1];
        ZeroMemory(szBinDirPath, sizeof(szBinDirPath));
        _tmakepath(szBinDirPath, szTmpDrive, szTmpDir, NULL, NULL);
        if (LoadLibraryWithPATHFixup(szBinDirPath))
        {
            return TRUE;
        }
    }

    return FALSE;
}

static BOOL LoadMozLibraryFromRegistry(BOOL bAskUserToSetPath)
{
    // 
    HKEY hkey = NULL;
    
    TCHAR szBinDirPath[MAX_PATH + 1];
    DWORD dwBufSize = sizeof(szBinDirPath);

    ZeroMemory(szBinDirPath, dwBufSize);

    // First try and read the path from the registry
    RegOpenKeyEx(HKEY_LOCAL_MACHINE, c_szMozillaControlKey, 0, KEY_READ, &hkey);
    if (hkey)
    {
        DWORD dwType = REG_SZ;
        RegQueryValueEx(hkey, c_szMozillaBinDirPathValue, NULL, &dwType,
            (LPBYTE) szBinDirPath, &dwBufSize);
        RegCloseKey(hkey);
        hkey = NULL;

        if (lstrlen(szBinDirPath) > 0 && LoadLibraryWithPATHFixup(szBinDirPath))
        {
            return TRUE;
        }

        // NOTE: We will drop through here if the registry key existed
        //       but didn't lead to the control being loaded.
    }
    
    // Ask the user to pick to pick the path?
    if (bAskUserToSetPath)
    {
        // Tell the user that they have to pick the bin dir path
        ::MessageBox(NULL, c_szPrePickFolderMsg, c_szWarningTitle, MB_OK);

        // Show a folder picker for the user to choose the bin directory
        BROWSEINFO bi;
        ZeroMemory(&bi, sizeof(bi));
        bi.hwndOwner = NULL;
        bi.pidlRoot = NULL;
        bi.pszDisplayName = szBinDirPath;
        bi.lpszTitle = c_szPickFolderDlgTitle;
        LPITEMIDLIST pItemList = SHBrowseForFolder(&bi);
        if (pItemList)
        {
            // Get the path from the user selection
            IMalloc *pShellAllocator = NULL;
            SHGetMalloc(&pShellAllocator);
            if (pShellAllocator)
            {
                char szPath[MAX_PATH + 1];

                if (SHGetPathFromIDList(pItemList, szPath))
                {
                    // Chop off the end path separator
                    int nPathSize = strlen(szPath);
                    if (nPathSize > 0)
                    {
                        if (szPath[nPathSize - 1] == '\\')
                        {
                            szPath[nPathSize - 1] = '\0';
                        }
                    }

                    // Form the file pattern
#ifdef UNICODE
                    MultiByteToWideChar(CP_ACP, 0, szPath, szBinDirPath, -1,
                        sizeof(szBinDirPath) / sizeof(TCHAR));
#else
                    lstrcpy(szBinDirPath, szPath);
#endif
                }
                pShellAllocator->Free(pItemList);
                pShellAllocator->Release();
            }
        }
        
        // Store the new path if we can
        if (lstrlen(szBinDirPath) > 0)
        {
            RegCreateKeyEx(HKEY_LOCAL_MACHINE, c_szMozillaControlKey, 0, NULL,
                REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL);
            if (hkey)
            {
                RegSetValueEx(hkey, c_szMozillaBinDirPathValue, 0, REG_SZ,
                    (LPBYTE) szBinDirPath, lstrlen(szBinDirPath) * sizeof(TCHAR));
                RegCloseKey(hkey);
            }
            else
            {
                // Tell the user the key can't be stored but the path they 
                // chose will be used for this session
                ::MessageBox(NULL, c_szRegistryErrorMsg, c_szErrorTitle, MB_OK);
            }
        }
    }

    if (lstrlen(szBinDirPath) > 0 && LoadLibraryWithPATHFixup(szBinDirPath))
    {
        return TRUE;
    }

    return FALSE;
}

static BOOL LoadLibraryWithPATHFixup(const TCHAR *szBinDirPath)
{
    TCHAR szOldPath[8192];
    TCHAR szNewPath[8192];
    
    ZeroMemory(szOldPath, sizeof(szOldPath));
    ZeroMemory(szNewPath, sizeof(szNewPath));

    // Append path to front of PATH
    GetEnvironmentVariable("PATH", szOldPath, sizeof(szOldPath) / sizeof(TCHAR));
    lstrcat(szNewPath, szBinDirPath);
    lstrcat(szNewPath, _T(";"));
    lstrcat(szNewPath, szOldPath);
    SetEnvironmentVariable("PATH", szNewPath);
    
    // Attempt load
    ghMozCtl = LoadLibrary(c_szMozCtlDll);
    if (ghMozCtl)
    {
        return TRUE;
    }

    // Restore old PATH
    SetEnvironmentVariable("PATH", szOldPath);

    return FALSE;
}


static BOOL FixupInProcRegistryEntry()
{
    TCHAR szMozCtlXLongPath[MAX_PATH+1];
    ZeroMemory(szMozCtlXLongPath, sizeof(szMozCtlXLongPath));
    GetModuleFileName(ghMozCtlX, szMozCtlXLongPath,
        sizeof(szMozCtlXLongPath) * sizeof(TCHAR));

    TCHAR szMozCtlXPath[MAX_PATH+1];
    ZeroMemory(szMozCtlXPath, sizeof(szMozCtlXPath));
    GetShortPathName(szMozCtlXLongPath, szMozCtlXPath,
        sizeof(szMozCtlXPath) * sizeof(TCHAR));

    HKEY hkey = NULL;
    RegOpenKeyEx(HKEY_CLASSES_ROOT, c_szMozCtlInProcServerKey, 0, KEY_ALL_ACCESS, &hkey);
    if (hkey)
    {
        RegSetValueEx(hkey, NULL, 0, REG_SZ, (LPBYTE) szMozCtlXPath,
            lstrlen(szMozCtlXPath) * sizeof(TCHAR));
        RegCloseKey(hkey);
        return TRUE;
    }

    return FALSE;
}

/////////////////////////////////////////////////////////////////////////////
// Used to determine whether the DLL can be unloaded by OLE

typedef HRESULT (STDAPICALLTYPE *DllCanUnloadNowFn)(void);

STDAPI DllCanUnloadNow(void)
{
    if (LoadMozLibrary())
    {
        DllCanUnloadNowFn pfn = (DllCanUnloadNowFn)
            GetProcAddress(ghMozCtl, _T("DllCanUnloadNow"));
        if (pfn)
        {
            return pfn();
        }
    }
    return S_OK;
}

/////////////////////////////////////////////////////////////////////////////
// Returns a class factory to create an object of the requested type

typedef HRESULT (STDAPICALLTYPE *DllGetClassObjectFn)(REFCLSID rclsid, REFIID riid, LPVOID* ppv);

STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
{
    if (LoadMozLibrary())
    {
        DllGetClassObjectFn pfn = (DllGetClassObjectFn) 
            GetProcAddress(ghMozCtl, _T("DllGetClassObject"));
        if (pfn)
        {
            return pfn(rclsid, riid, ppv);
        }
    }
    return E_FAIL;
}

/////////////////////////////////////////////////////////////////////////////
// DllRegisterServer - Adds entries to the system registry

typedef HRESULT (STDAPICALLTYPE *DllRegisterServerFn)(void);

STDAPI DllRegisterServer(void)
{
    if (LoadMozLibrary())
    {
        DllRegisterServerFn pfn = (DllRegisterServerFn) 
            GetProcAddress(ghMozCtl, _T("DllRegisterServer"));
        if (pfn)
        {
            HRESULT hr = pfn();
            if (SUCCEEDED(hr))
            {
                FixupInProcRegistryEntry();
            }
            return hr;
        }
    }
    return E_FAIL;
}

/////////////////////////////////////////////////////////////////////////////
// DllUnregisterServer - Removes entries from the system registry

typedef HRESULT (STDAPICALLTYPE *DllUnregisterServerFn)(void);

STDAPI DllUnregisterServer(void)
{
    if (LoadMozLibrary())
    {
        DllUnregisterServerFn pfn = (DllUnregisterServerFn)
            GetProcAddress(ghMozCtl, _T("DllUnregisterServer"));
        if (pfn)
        {
            return pfn();
        }
    }
    return E_FAIL;
}