File: app_uninstall.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 (320 lines) | stat: -rw-r--r-- 11,541 bytes parent folder | download | duplicates (5)
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
// 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/updater/app/app_uninstall.h"

#include <optional>
#include <string>
#include <utility>
#include <vector>

#include "base/command_line.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/memory/scoped_refptr.h"
#include "base/notreached.h"
#include "base/process/launch.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "base/threading/platform_thread.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "chrome/enterprise_companion/installer_paths.h"
#include "chrome/updater/app/app.h"
#include "chrome/updater/app/app_utils.h"
#include "chrome/updater/branded_constants.h"
#include "chrome/updater/configurator.h"
#include "chrome/updater/constants.h"
#include "chrome/updater/external_constants.h"
#include "chrome/updater/lock.h"
#include "chrome/updater/persisted_data.h"
#include "chrome/updater/prefs.h"
#include "chrome/updater/updater_scope.h"
#include "chrome/updater/updater_version.h"
#include "chrome/updater/util/util.h"
#include "components/update_client/protocol_definition.h"
#include "components/update_client/update_client.h"

#if BUILDFLAG(IS_WIN)
#include "chrome/updater/win/setup/uninstall.h"
#elif BUILDFLAG(IS_POSIX)
#include "chrome/updater/posix/setup.h"
#endif

namespace updater {

std::vector<base::FilePath> GetVersionExecutablePaths(UpdaterScope scope) {
  const std::optional<base::FilePath> updater_folder_path =
      GetInstallDirectory(scope);
  if (!updater_folder_path) {
    LOG(ERROR) << __func__ << ": failed to get the updater install directory.";
    return {};
  }
  std::vector<base::FilePath> version_executable_paths;
  base::FileEnumerator(*updater_folder_path, false,
                       base::FileEnumerator::DIRECTORIES)
      .ForEach([&scope, &version_executable_paths](
                   const base::FilePath& version_folder_path) {
        // Skip the current version.
        if (version_folder_path == GetVersionedInstallDirectory(scope)) {
          return;
        }

        // Skip if the folder is not named as a valid version. All updater
        // version directories are named as valid versions.
        if (!base::Version(version_folder_path.BaseName().AsUTF8Unsafe())
                 .IsValid()) {
          return;
        }

        const base::FilePath version_executable_path =
            version_folder_path.Append(GetExecutableRelativePath());
        version_executable_paths.push_back(version_executable_path);
        VLOG(1) << __func__ << " : added to version_executable_paths: "
                << version_executable_path;
      });

  return version_executable_paths;
}

base::CommandLine GetUninstallSelfCommandLine(
    UpdaterScope scope,
    const base::FilePath& executable_path) {
  base::CommandLine command_line(executable_path);
  command_line.AppendSwitch(kUninstallSelfSwitch);
  if (IsSystemInstall(scope)) {
    command_line.AppendSwitch(kSystemSwitch);
  }
  return command_line;
}

namespace {

// Uninstalls the enterprise companion app if it exists.
[[nodiscard]] int UninstallEnterpriseCompanionApp() {
  std::optional<base::FilePath> exe_path =
      enterprise_companion::FindExistingInstall();
  if (!exe_path) {
    VLOG(1) << __func__
            << ": Could not locate an enterprise companion app installation.";
    return kErrorOk;
  }

  base::CommandLine command_line(*exe_path);
  command_line.AppendSwitch(kUninstallCompanionAppSwitch);
  int exit_code = -1;
  std::string output;
  if (!base::GetAppOutputWithExitCode(command_line, &output, &exit_code)) {
    return kErrorFailedToUninstallCompanionApp;
  }
  VLOG(1) << __func__ << ": Ran: " << command_line.GetCommandLineString()
          << ": " << output << ": " << exit_code;

  // Wait until the enterprise companion install is completely removed. For
  // instance, enterprise companion spawns a separate cmd script on Windows to
  // complete the uninstall.
  for (const auto deadline = base::TimeTicks::Now() + base::Seconds(20);
       enterprise_companion::FindExistingInstall() &&
       (base::TimeTicks::Now() < deadline);
       base::PlatformThread::Sleep(base::Milliseconds(100))) {
  }
  VLOG(1) << __func__ << ": !enterprise_companion::FindExistingInstall(): "
          << !enterprise_companion::FindExistingInstall();

  return exit_code == 0 ? kErrorOk : kErrorFailedToUninstallCompanionApp;
}

// Uninstalls all versions not matching the current version of the updater for
// the given `scope`.
[[nodiscard]] int UninstallOtherVersions(UpdaterScope scope) {
  bool has_error = false;
  for (const base::FilePath& version_executable_path :
       GetVersionExecutablePaths(scope)) {
    const base::CommandLine command_line(
        GetUninstallSelfCommandLine(scope, version_executable_path));
    if (!base::PathExists(command_line.GetProgram())) {
      VLOG(1)
          << __func__
          << ": Other version updater has no main binary, skip the uninstall.";
      return kErrorOk;
    }
    int exit_code = -1;
    std::string output;
    if (base::GetAppOutputWithExitCode(command_line, &output, &exit_code)) {
      VLOG(1) << __func__ << ": Ran: " << command_line.GetCommandLineString()
              << ": " << output << ": " << exit_code;
      if (exit_code != 0) {
        has_error = true;
      } else {
        // Wait until the install is completely removed, for instance, wait for
        // the completion of the separate cmd script on Windows to complete the
        // uninstall.
        for (const auto deadline = base::TimeTicks::Now() + base::Seconds(20);
             base::PathExists(command_line.GetProgram()) &&
             (base::TimeTicks::Now() < deadline);
             base::PlatformThread::Sleep(base::Milliseconds(100))) {
        }
      }
    } else {
      VLOG(1) << "Failed to run the command to uninstall other versions.";
      has_error = true;
    }
  }
  return has_error ? kErrorFailedToUninstallOtherVersion : kErrorOk;
}

void UninstallInThreadPool(UpdaterScope scope,
                           base::OnceCallback<void(int)> shutdown) {
  base::ThreadPool::PostTaskAndReplyWithResult(
      FROM_HERE, {base::MayBlock()},
      base::BindOnce(
          [](UpdaterScope scope) {
            int error_code = kErrorOk;
            if (IsSystemInstall(scope)) {
              error_code = UninstallEnterpriseCompanionApp();
            }
            if (int result = UninstallOtherVersions(scope);
                result != kErrorOk) {
#if !BUILDFLAG(IS_LINUX)
              // TODO(crbug.com/366249606): Ignores the errors when uninstalls
              // the other versions, because currently older Linux updater on
              // CIPD exits with error `kErrorFailedToDeleteFolder`.
              error_code = result;
#endif
            }
            if (int result = Uninstall(scope); result != kErrorOk) {
              error_code = result;
            }
            return error_code;
          },
          scope),
      std::move(shutdown));
}

}  // namespace

// AppUninstall uninstalls the updater.
class AppUninstall : public App {
 private:
  ~AppUninstall() override = default;
  [[nodiscard]] int Initialize() override;
  void FirstTaskRun() override;

  void UninstallAll(int reason);

  // Inter-process lock taken by AppInstall, AppUninstall, and AppUpdate. May
  // be null if the setup lock wasn't acquired.
  std::unique_ptr<ScopedLock> setup_lock_;

  // These may be null if the global prefs lock wasn't acquired.
  scoped_refptr<GlobalPrefs> global_prefs_;
  scoped_refptr<Configurator> config_;
};

int AppUninstall::Initialize() {
  setup_lock_ =
      CreateScopedLock(kSetupMutex, updater_scope(), kWaitForSetupLock);
  global_prefs_ = CreateGlobalPrefs(updater_scope());
  if (global_prefs_) {
    config_ = base::MakeRefCounted<Configurator>(
        global_prefs_, CreateExternalConstants(), updater_scope());
  }
  return kErrorOk;
}

void AppUninstall::UninstallAll(int reason) {
  update_client::CrxComponent uninstall_data;
  uninstall_data.ap = config_->GetUpdaterPersistedData()->GetAP(kUpdaterAppId);
  uninstall_data.app_id = kUpdaterAppId;
  uninstall_data.brand =
      config_->GetUpdaterPersistedData()->GetBrandCode(kUpdaterAppId);
  uninstall_data.requires_network_encryption = false;
  uninstall_data.version =
      config_->GetUpdaterPersistedData()->GetProductVersion(kUpdaterAppId);
  if (!uninstall_data.version.IsValid()) {
    // In cases where there is no version in persisted data, fall back to the
    // currently-running version of the updater.
    uninstall_data.version = base::Version(kUpdaterVersion);
  }

  // If the terms of service have not been accepted, don't ping.
  if (config_->GetUpdaterPersistedData()->GetEulaRequired()) {
    UninstallInThreadPool(updater_scope(),
                          base::BindOnce(&AppUninstall::Shutdown, this));
    return;
  }

  // Otherwise, send an uninstall ping then uninstall.
  update_client::UpdateClientFactory(config_)->SendPing(
      uninstall_data,
      {.event_type = update_client::protocol_request::kEventUninstall,
       .result = update_client::protocol_request::kEventResultSuccess,
       .error_code = 0,
       .extra_code1 = reason},
      base::BindOnce(
          [](base::OnceCallback<void(int)> shutdown, UpdaterScope scope,
             update_client::Error uninstall_ping_error) {
            VLOG_IF(1, uninstall_ping_error != update_client::Error::NONE)
                << "Uninstall ping failed: " << uninstall_ping_error;
            UninstallInThreadPool(scope, std::move(shutdown));
          },
          base::BindOnce(&AppUninstall::Shutdown, this), updater_scope()));
}

void AppUninstall::FirstTaskRun() {
  if (WrongUser(updater_scope())) {
    VLOG(0) << "The current user is not compatible with the current scope.";
    Shutdown(kErrorWrongUser);
    return;
  }

  if (!setup_lock_) {
    VLOG(0) << "Failed to acquire setup mutex; shutting down.";
    Shutdown(kErrorFailedToLockSetupMutex);
    return;
  }

  if (!global_prefs_) {
    VLOG(0) << "Failed to acquire global prefs; shutting down.";
    Shutdown(kErrorFailedToLockPrefsMutex);
    return;
  }

  const base::CommandLine* command_line =
      base::CommandLine::ForCurrentProcess();

  if (command_line->HasSwitch(kUninstallSwitch)) {
    UninstallAll(kUninstallPingReasonUninstalled);
    return;
  }

  if (command_line->HasSwitch(kUninstallIfUnusedSwitch)) {
    const bool had_apps = config_->GetUpdaterPersistedData()->GetHadApps();
    const bool should_uninstall =
        ShouldUninstall(config_->GetUpdaterPersistedData()->GetAppIds(),
                        global_prefs_->CountServerStarts(), had_apps);
    VLOG(1) << "ShouldUninstall returned: " << should_uninstall;
    if (should_uninstall) {
      UninstallAll(had_apps ? kUninstallPingReasonNoAppsRemain
                            : kUninstallPingReasonNeverHadApps);
    } else {
      base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
          FROM_HERE, base::BindOnce(&AppUninstall::Shutdown, this, 0));
    }
    return;
  }

  NOTREACHED();
}

scoped_refptr<App> MakeAppUninstall() {
  return base::MakeRefCounted<AppUninstall>();
}

}  // namespace updater