tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

nsInstallationDirLayout.cpp (2347B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 #include "nsInstallationDirLayout.h"
      6 #include "InstallationDirLayout.h"
      7 #include "mozilla/Logging.h"
      8 #ifdef XP_WIN
      9 #  include <windows.h>
     10 #endif
     11 
     12 static mozilla::LazyLogModule sInstallDirLayoutLog("InstallDirLayout");
     13 static const wchar_t* dllName = L"InstallationDirLayout.dll";
     14 
     15 static InstallationDirLayoutType sLayoutType =
     16    InstallationDirLayoutType::Unknown;
     17 using FuncType = InstallationDirLayoutType (*)();
     18 
     19 namespace mozilla {
     20 
     21 nsresult InitializeInstallationDirLayout() {
     22 #ifdef XP_WIN
     23  HINSTANCE hRuntimeLibrary = LoadLibraryExW(dllName, nullptr, 0);
     24  if (!hRuntimeLibrary) {
     25    MOZ_LOG(sInstallDirLayoutLog, LogLevel::Error,
     26            ("Failed to open installation directory layout dll"));
     27    return NS_ERROR_FAILURE;
     28  }
     29  FuncType dirLayoutFunc =
     30      (FuncType)GetProcAddress(hRuntimeLibrary, "GetInstallationDirLayoutType");
     31  if (!dirLayoutFunc) {
     32    MOZ_LOG(sInstallDirLayoutLog, LogLevel::Error,
     33            ("GetInstallationDirLayoutType function not found in installation "
     34             "directory layout dll"));
     35    FreeLibrary(hRuntimeLibrary);
     36    return NS_ERROR_FAILURE;
     37  }
     38  sLayoutType = dirLayoutFunc();
     39  bool freeResult = FreeLibrary(hRuntimeLibrary);
     40  if (!freeResult) {
     41    MOZ_LOG(sInstallDirLayoutLog, LogLevel::Warning,
     42            ("FreeLibrary returned false"));
     43    // Not a fatal problem.
     44  }
     45  return NS_OK;
     46 #else
     47  sLayoutType = InstallationDirLayoutType::Single;
     48  return NS_OK;
     49 #endif
     50 }
     51 
     52 NS_IMPL_ISUPPORTS(InstallationDirLayout, nsIInstallationDirLayout)
     53 
     54 NS_IMETHODIMP
     55 InstallationDirLayout::GetIsInstallationLayoutVersioned(
     56    bool* installationDirIsVersioned) {
     57  bool isVersioned;
     58  switch (sLayoutType) {
     59    case InstallationDirLayoutType::Single:
     60      isVersioned = false;
     61      break;
     62    case InstallationDirLayoutType::Versioned:
     63      isVersioned = true;
     64      break;
     65    default:
     66      MOZ_LOG(sInstallDirLayoutLog, LogLevel::Error,
     67              ("Unexpected value for installation dir layout type: %d",
     68               (int)sLayoutType));
     69      return NS_ERROR_ILLEGAL_VALUE;
     70  }
     71  *installationDirIsVersioned = isVersioned;
     72  return NS_OK;
     73 }
     74 
     75 }  // namespace mozilla