File: installer_win.cc

package info (click to toggle)
chromium 139.0.7258.127-1~deb13u1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,096 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (139 lines) | stat: -rw-r--r-- 5,095 bytes parent folder | download | duplicates (3)
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
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/platform_experience/installer/installer_win.h"

#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "base/path_service.h"
#include "base/process/launch.h"
#include "base/win/scoped_variant.h"
#include "chrome/browser/google/google_update_app_command.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/install_static/install_details.h"
#include "chrome/installer/util/install_util.h"

namespace {

// Switch used to install platform_experience_helper
constexpr char kPlatformExperienceHelperForceInstallSwitch[] = "force-install";
// Directory under which platform_experience_helper is installed
constexpr wchar_t kPlatformExperienceHelperDir[] = L"PlatformExperienceHelper";
// Name of the platform_experience_helper executable
constexpr wchar_t kPlatformExperienceHelperExe[] =
    L"platform_experience_helper.exe";

// This function might block.
// Returns true if the platform_experience_helper is installed.
// Returns true if it can't determine whether it's installed or not.
bool PlatformExperienceHelperMightBeInstalled() {
  base::FilePath peh_base_dir = base::PathService::CheckedGet(
      install_static::IsSystemInstall()
          ? static_cast<int>(base::DIR_EXE)
          : static_cast<int>(chrome::DIR_USER_DATA));

  base::FilePath peh_exe_path =
      peh_base_dir.Append(kPlatformExperienceHelperDir)
          .Append(kPlatformExperienceHelperExe);
  return base::PathExists(peh_exe_path);
}

// Enum for tracking the launch status of the platform experience helper
// installer for system installs.
// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
// LINT.IfChange(SystemInstallerLaunchStatus)
enum class SystemInstallerLaunchStatus {
  kSuccess = 0,
  kAppCommandNotFound = 1,
  kAppCommandExecutionFailed = 2,
  kMaxValue = kAppCommandExecutionFailed,
};
// LINT.ThenChange(//tools/metrics/histograms/metadata/windows/enums.xml:SystemInstallerLaunchStatus)

// Enum for tracking the launch status of the platform experience helper
// installer for user installs.
// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
// LINT.IfChange(UserInstallerLaunchStatus)
enum class UserInstallerLaunchStatus {
  kSuccess = 0,
  kFileNotFound = 1,
  kAccessDenied = 2,
  kOtherFailure = 3,
  kMaxValue = kOtherFailure,
};
// LINT.ThenChange(//tools/metrics/histograms/metadata/windows/enums.xml:UserInstallerLaunchStatus)

void ReportSystemInstallerLaunchStatus(SystemInstallerLaunchStatus status) {
  base::UmaHistogramEnumeration(
      "Windows.PlatformExperienceHelper.InstallerLaunchStatus.System", status);
}

void ReportUserInstallLaunchStatusMetric(UserInstallerLaunchStatus status) {
  base::UmaHistogramEnumeration(
      "Windows.PlatformExperienceHelper.InstallerLaunchStatus.User", status);
}

}  // namespace

namespace platform_experience {

void MaybeInstallPlatformExperienceHelper() {
  if (PlatformExperienceHelperMightBeInstalled()) {
    return;
  }

  if (install_static::IsSystemInstall()) {
    auto command = GetUpdaterAppCommand(installer::kCmdInstallPEH);
    if (!command.has_value()) {
      ReportSystemInstallerLaunchStatus(
          SystemInstallerLaunchStatus::kAppCommandNotFound);
      return;
    }

    const VARIANT& var = base::win::ScopedVariant::kEmptyVariant;
    if (FAILED(
            (*command)->execute(var, var, var, var, var, var, var, var, var))) {
      ReportSystemInstallerLaunchStatus(
          SystemInstallerLaunchStatus::kAppCommandExecutionFailed);
    } else {
      ReportSystemInstallerLaunchStatus(SystemInstallerLaunchStatus::kSuccess);
    }
    return;
  }

  base::FilePath peh_installer_path =
      base::PathService::CheckedGet(base::DIR_MODULE)
          .Append(FILE_PATH_LITERAL("os_update_handler.exe"));
  base::CommandLine install_cmd(peh_installer_path);
  install_cmd.AppendSwitch(kPlatformExperienceHelperForceInstallSwitch);
  InstallUtil::AppendModeAndChannelSwitches(&install_cmd);

  base::LaunchOptions launch_options;
  launch_options.feedback_cursor_off = true;
  launch_options.force_breakaway_from_job_ = true;
  ::SetLastError(ERROR_SUCCESS);
  base::Process process = base::LaunchProcess(install_cmd, launch_options);
  UserInstallerLaunchStatus status = UserInstallerLaunchStatus::kSuccess;
  if (!process.IsValid()) {
    switch (::GetLastError()) {
      case ERROR_FILE_NOT_FOUND:
        status = UserInstallerLaunchStatus::kFileNotFound;
        break;
      case ERROR_ACCESS_DENIED:
        status = UserInstallerLaunchStatus::kAccessDenied;
        break;
      default:
        status = UserInstallerLaunchStatus::kOtherFailure;
        break;
    }
  }
  ReportUserInstallLaunchStatusMetric(status);
}

}  // namespace platform_experience