File: setup_utils.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 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 (326 lines) | stat: -rw-r--r-- 10,477 bytes parent folder | download | duplicates (6)
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
// Copyright 2020 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/credential_provider/setup/setup_utils.h"

#include <Windows.h>

#include <string>

#include "base/files/file_util.h"
#include "base/json/json_string_value_serializer.h"
#include "base/strings/utf_string_conversions.h"
#include "base/win/registry.h"
#include "chrome/common/chrome_version.h"
#include "chrome/credential_provider/common/gcp_strings.h"
#include "chrome/credential_provider/gaiacp/gaia_resources.h"
#include "chrome/credential_provider/gaiacp/gcp_utils.h"
#include "chrome/credential_provider/gaiacp/logging.h"
#include "chrome/credential_provider/gaiacp/reg_utils.h"
#include "chrome/credential_provider/setup/gcpw_files.h"

namespace credential_provider {

namespace switches {

const char kParentHandle[] = "parent-handle";
const char kInstallPath[] = "install-path";
const char kUninstall[] = "uninstall";

const char kEnableStats[] = "enable-stats";
const char kDisableStats[] = "disable-stats";

const char kInstallerData[] = "installerdata";

const char kStandaloneInstall[] = "standalone";
}  // namespace switches

namespace {

// Path to the msi json value inside the dictionary which is parsed from
// installer data argument in the command line.
const char kMsiJsonPath[] = "distribution.msi";

// The registry name which is saved to indicate installation source.
const wchar_t kMsiInstall[] = L"msi";

// Parses the json data and returns it as a dictionary. If the json data isn't
// valid, returns std::nullopt.
std::optional<base::Value::Dict> ParseDistributionPreferences(
    const std::string& json_data) {
  JSONStringValueDeserializer json(json_data);
  std::string error;
  std::unique_ptr<base::Value> root(json.Deserialize(nullptr, &error));
  if (!root.get()) {
    LOGFN(WARNING) << "Failed to parse initial prefs file: " << error;
    return std::nullopt;
  }
  if (!root->is_dict()) {
    LOGFN(WARNING) << "Failed to parse installer data file";
    return std::nullopt;
  }
  return std::move(*root).TakeDict();
}

}  // namespace

StandaloneInstallerConfigurator::StandaloneInstallerConfigurator()
    : is_msi_installation_(false) {}

StandaloneInstallerConfigurator::~StandaloneInstallerConfigurator() = default;

// static
StandaloneInstallerConfigurator**
StandaloneInstallerConfigurator::GetInstanceStorage() {
  static StandaloneInstallerConfigurator* instance =
      new StandaloneInstallerConfigurator();

  return &instance;
}

// static
StandaloneInstallerConfigurator* StandaloneInstallerConfigurator::Get() {
  return *GetInstanceStorage();
}

// Sets the installer source for GCPW. When installed through MSI,
// contains installer data file name as argument.
void StandaloneInstallerConfigurator::ConfigureInstallationType(
    const base::CommandLine& cmdline) {
  // There are following scenarios for installations:
  // First time install from MSI
  // First time install from EXE
  // MSIs before this kMsiInstall registry gets auto-updated
  // MSIs with kMsiInstall registry gets auto-updated
  // EXEs with kMsiInstall registry gets auto-updated

  // |kStandaloneInstall| indicates fresh installation.
  if (cmdline.HasSwitch(switches::kStandaloneInstall)) {
    base::Value* is_msi = nullptr;
    if (cmdline.HasSwitch(switches::kInstallerData)) {
      base::FilePath prefs_path(
          cmdline.GetSwitchValuePath(switches::kInstallerData));

      if (InitializeFromInstallerData(prefs_path))
        is_msi = installer_data_dictionary_.FindByDottedPath(kMsiJsonPath);
    }

    is_msi_installation_ = false;
    if (is_msi && is_msi->is_bool() && is_msi->GetBool()) {
      is_msi_installation_ = true;
    }
  } else {
    // Honor the registry if it is found, otherwise fall back to MSI
    // installation.
    is_msi_installation_ =
        GetUpdaterClientsAppPathFlagOrDefault(kMsiInstall, 1);
  }

  HRESULT hr =
      SetUpdaterClientsAppPathFlag(kMsiInstall, is_msi_installation_ ? 1 : 0);
  if (FAILED(hr))
    LOGFN(ERROR) << "SetGlobalFlag failed" << putHR(hr);
}

std::wstring StandaloneInstallerConfigurator::GetCurrentDate() {
  static const wchar_t kDateFormat[] = L"yyyyMMdd";
  wchar_t date_str[std::size(kDateFormat)] = {};
  int len = GetDateFormatW(LOCALE_INVARIANT, 0, nullptr, kDateFormat, date_str,
                           std::size(date_str));
  if (len) {
    --len;  // Subtract terminating \0.
  } else {
    LOGFN(ERROR) << "GetDateFormat failed";
    return L"";
  }

  return std::wstring(date_str, len);
}

bool StandaloneInstallerConfigurator::IsStandaloneInstallation() const {
  return !is_msi_installation_;
}

HRESULT StandaloneInstallerConfigurator::AddUninstallKey(
    const base::FilePath& install_path) {
  LOGFN(VERBOSE);

  if (is_msi_installation_)
    return S_OK;

  std::wstring uninstall_reg = kRegUninstall;
  uninstall_reg.append(L"\\");
  uninstall_reg.append(kRegUninstallProduct);

  base::win::RegKey key;
  LONG status =
      key.Create(HKEY_LOCAL_MACHINE, uninstall_reg.c_str(), KEY_SET_VALUE);
  if (status != ERROR_SUCCESS) {
    HRESULT hr = HRESULT_FROM_WIN32(status);
    LOGFN(ERROR) << "Unable to create " << uninstall_reg << " hr=" << putHR(hr);
    return hr;
  }

  base::CommandLine uninstall_string(
      install_path.Append(kCredentialProviderSetupExe));
  uninstall_string.AppendSwitch(switches::kUninstall);

  status = key.WriteValue(kRegUninstallString,
                          uninstall_string.GetCommandLineString().c_str());
  if (status != ERROR_SUCCESS) {
    HRESULT hr = HRESULT_FROM_WIN32(status);
    LOGFN(ERROR) << "Unable to write " << kRegUninstallString
                 << " hr=" << putHR(hr);
    return hr;
  }

  status = key.WriteValue(kRegUninstallDisplayName,
                          GetStringResource(IDS_PROJNAME_BASE).c_str());
  if (status != ERROR_SUCCESS) {
    HRESULT hr = HRESULT_FROM_WIN32(status);
    LOGFN(ERROR) << "Unable to write " << kRegUninstallDisplayName
                 << " hr=" << putHR(hr);
    return hr;
  }

  status = key.WriteValue(kRegInstallLocation, install_path.value().c_str());
  if (status != ERROR_SUCCESS) {
    HRESULT hr = HRESULT_FROM_WIN32(status);
    LOGFN(ERROR) << "Unable to write " << kRegInstallLocation
                 << " hr=" << putHR(hr);
    return hr;
  }

  status = key.WriteValue(
      kRegDisplayIcon,
      (install_path.Append(kCredentialProviderSetupExe).value() + L",0")
          .c_str());
  if (status != ERROR_SUCCESS) {
    HRESULT hr = HRESULT_FROM_WIN32(status);
    LOGFN(ERROR) << "Unable to write " << kRegDisplayIcon
                 << " hr=" << putHR(hr);
    return hr;
  }

  status = key.WriteValue(kRegNoModify, 1);
  if (status != ERROR_SUCCESS) {
    HRESULT hr = HRESULT_FROM_WIN32(status);
    LOGFN(ERROR) << "Unable to write " << kRegNoModify << " hr=" << putHR(hr);
    return hr;
  }

  status = key.WriteValue(kRegNoRepair, 1);
  if (status != ERROR_SUCCESS) {
    HRESULT hr = HRESULT_FROM_WIN32(status);
    LOGFN(ERROR) << "Unable to write " << kRegNoRepair << " hr=" << putHR(hr);
    return hr;
  }

  status = key.WriteValue(kRegPublisherName, kRegPublisher);
  if (status != ERROR_SUCCESS) {
    HRESULT hr = HRESULT_FROM_WIN32(status);
    LOGFN(ERROR) << "Unable to write " << kRegPublisherName
                 << " hr=" << putHR(hr);
    return hr;
  }

  status = key.WriteValue(kRegInstallDate, GetCurrentDate().c_str());
  if (status != ERROR_SUCCESS) {
    HRESULT hr = HRESULT_FROM_WIN32(status);
    LOGFN(ERROR) << "Unable to write " << kRegInstallDate
                 << " hr=" << putHR(hr);
    return hr;
  }

  base::Version version(CHROME_VERSION_STRING);

  status = key.WriteValue(kRegVersion,
                          base::ASCIIToWide(version.GetString()).c_str());
  if (status != ERROR_SUCCESS) {
    HRESULT hr = HRESULT_FROM_WIN32(status);
    LOGFN(ERROR) << "Unable to write " << kRegVersion << " hr=" << putHR(hr);
    return hr;
  }

  status = key.WriteValue(kRegDisplayVersion,
                          base::ASCIIToWide(version.GetString()).c_str());
  if (status != ERROR_SUCCESS) {
    HRESULT hr = HRESULT_FROM_WIN32(status);
    LOGFN(ERROR) << "Unable to write " << kRegDisplayVersion
                 << " hr=" << putHR(hr);
    return hr;
  }

  const std::vector<uint32_t>& version_components = version.components();
  if (version_components.size() == 4) {
    status = key.WriteValue(kRegVersionMajor,
                            static_cast<DWORD>(version_components[2]));
    if (status != ERROR_SUCCESS) {
      HRESULT hr = HRESULT_FROM_WIN32(status);
      LOGFN(ERROR) << "Unable to write " << kRegVersionMajor
                   << " hr=" << putHR(hr);
      return hr;
    }

    status = key.WriteValue(kRegVersionMinor,
                            static_cast<DWORD>(version_components[3]));
    if (status != ERROR_SUCCESS) {
      HRESULT hr = HRESULT_FROM_WIN32(status);
      LOGFN(ERROR) << "Unable to write " << kRegVersionMinor
                   << " hr=" << putHR(hr);
      return hr;
    }
  }

  return S_OK;
}

HRESULT StandaloneInstallerConfigurator::RemoveUninstallKey() {
  LOGFN(VERBOSE);
  base::win::RegKey key;

  LONG status = key.Create(HKEY_LOCAL_MACHINE, kRegUninstall, KEY_SET_VALUE);
  if (status != ERROR_SUCCESS) {
    HRESULT hr = HRESULT_FROM_WIN32(status);
    LOGFN(ERROR) << "Unable to create " << kRegUninstall << " hr=" << putHR(hr);
    return hr;
  }

  status = key.DeleteKey(kRegUninstallProduct);
  if (status != ERROR_SUCCESS) {
    HRESULT hr = HRESULT_FROM_WIN32(status);
    LOGFN(ERROR) << "Unable to delete " << kRegUninstallProduct
                 << " hr=" << putHR(hr);
    return hr;
  }
  return S_OK;
}

bool StandaloneInstallerConfigurator::InitializeFromInstallerData(
    base::FilePath prefs_path) {
  std::string json_data;
  if (base::PathExists(prefs_path) &&
      !base::ReadFileToString(prefs_path, &json_data)) {
    LOGFN(ERROR) << "Failed to read preferences from " << prefs_path.value();
    return false;
  }

  if (json_data.empty()) {
    LOGFN(WARNING) << "Installer data is empty!";
    return false;
  }

  std::optional<base::Value::Dict> prefs =
      ParseDistributionPreferences(json_data);
  if (!prefs) {
    LOGFN(WARNING) << "Installer data isn't formatted correctly";
    return false;
  }

  installer_data_dictionary_ = std::move(prefs).value();

  return true;
}

}  // namespace credential_provider