blob: e6d770646c7544fbf58bfabc83c702b05cd5f2f4 [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2021 The Chromium Authors
Sorin Jianu2b9271232021-06-01 18:40:142// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/updater/updater_scope.h"
6
Sorin Jianua4b3d382023-11-10 20:08:467#include <optional>
8
Sorin Jianu2b9271232021-06-01 18:40:149#include "base/command_line.h"
Xiaoling Bao761d0272023-06-06 22:50:1910#include "base/path_service.h"
Sorin Jianu2b9271232021-06-01 18:40:1411#include "build/build_config.h"
12#include "chrome/updater/constants.h"
S. Ganesh1b1e7712022-12-15 23:57:3813#include "chrome/updater/util/util.h"
Sorin Jianu2b9271232021-06-01 18:40:1414
Xiaohan Wang822cc382022-01-15 19:33:5115#if BUILDFLAG(IS_WIN)
Sorin Jianu2b9271232021-06-01 18:40:1416#include "chrome/updater/tag.h"
Noah Rose Ledesmafc9b3652022-11-12 02:10:4917#include "chrome/updater/util/win_util.h"
Sorin Jianu2b9271232021-06-01 18:40:1418#endif
19
20namespace updater {
21namespace {
22
S. Ganesh8fd7ddb2022-03-29 19:57:3423bool IsSystemProcessForCommandLine(const base::CommandLine& command_line) {
24 return command_line.HasSwitch(kSystemSwitch);
Sorin Jianu2b9271232021-06-01 18:40:1425}
26
27} // namespace
28
S. Ganeshcdee2232024-01-25 09:38:5029std::optional<tagging::NeedsAdmin> NeedsAdminFromTagArgs(
30 const std::optional<tagging::TagArgs> tag_args) {
31 if (!tag_args) {
32 return {};
33 }
34 if (!tag_args->apps.empty()) {
35 return tag_args->apps.front().needs_admin;
36 }
37 if (tag_args->runtime_mode) {
38 return tag_args->runtime_mode->needs_admin;
39 }
S. Ganeshcdee2232024-01-25 09:38:5040 return {};
41}
42
S. Ganeshab09f27c2022-07-18 23:14:2743bool IsPrefersForCommandLine(const base::CommandLine& command_line) {
44#if BUILDFLAG(IS_WIN)
S. Ganeshcdee2232024-01-25 09:38:5045 std::optional<tagging::NeedsAdmin> needs_admin =
46 NeedsAdminFromTagArgs(GetTagArgsForCommandLine(command_line).tag_args);
47 return needs_admin ? *needs_admin == tagging::NeedsAdmin::kPrefers : false;
S. Ganeshab09f27c2022-07-18 23:14:2748#else
49 return false;
50#endif
51}
52
S. Ganesh8fd7ddb2022-03-29 19:57:3453UpdaterScope GetUpdaterScopeForCommandLine(
54 const base::CommandLine& command_line) {
Xiaohan Wang822cc382022-01-15 19:33:5155#if BUILDFLAG(IS_WIN)
S. Ganesh8fd7ddb2022-03-29 19:57:3456 if (IsSystemProcessForCommandLine(command_line)) {
Sorin Jianu2b9271232021-06-01 18:40:1457 return UpdaterScope::kSystem;
58 }
59
Sorin Jianuf62ce12e92023-05-22 12:06:5960 // Assume only one app is present since bundles are not supported.
S. Ganeshcdee2232024-01-25 09:38:5061 std::optional<tagging::NeedsAdmin> needs_admin =
62 NeedsAdminFromTagArgs(GetTagArgsForCommandLine(command_line).tag_args);
63 if (needs_admin) {
64 switch (*needs_admin) {
65 case tagging::NeedsAdmin::kYes:
Sorin Jianu2b9271232021-06-01 18:40:1466 return UpdaterScope::kSystem;
S. Ganeshcdee2232024-01-25 09:38:5067 case tagging::NeedsAdmin::kNo:
Sorin Jianu2b9271232021-06-01 18:40:1468 return UpdaterScope::kUser;
S. Ganeshcdee2232024-01-25 09:38:5069 case tagging::NeedsAdmin::kPrefers:
S. Ganesh15636a92022-04-09 01:53:0870 return command_line.HasSwitch(kCmdLinePrefersUser)
71 ? UpdaterScope::kUser
72 : UpdaterScope::kSystem;
Sorin Jianu2b9271232021-06-01 18:40:1473 }
74 }
Xiaoling Bao761d0272023-06-06 22:50:1975
76 // The legacy updater could launch the shim without specifying the scope
77 // explicitly. This includes command line switches: '/healthcheck', '/regsvc',
78 // '/regserver', and '/ping'. In this case, choose system scope if this
79 // program is run as a system shim.
Sorin Jianua4b3d382023-11-10 20:08:4680 std::optional<base::FilePath> system_shim_path =
Xiaoling Bao761d0272023-06-06 22:50:1981 GetGoogleUpdateExePath(UpdaterScope::kSystem);
82 base::FilePath exe_path;
83 if (system_shim_path && base::PathService::Get(base::FILE_EXE, &exe_path) &&
84 system_shim_path->DirName().IsParent(exe_path)) {
85 return UpdaterScope::kSystem;
86 }
Sorin Jianu2b9271232021-06-01 18:40:1487 return UpdaterScope::kUser;
88#else
S. Ganesh8fd7ddb2022-03-29 19:57:3489 return IsSystemProcessForCommandLine(command_line) ? UpdaterScope::kSystem
90 : UpdaterScope::kUser;
Sorin Jianu2b9271232021-06-01 18:40:1491#endif
92}
93
S. Ganesh8fd7ddb2022-03-29 19:57:3494UpdaterScope GetUpdaterScope() {
S. Ganeshdfd56602023-06-24 02:21:2495 return GetUpdaterScopeForCommandLine(*base::CommandLine::ForCurrentProcess());
S. Ganesh8fd7ddb2022-03-29 19:57:3496}
97
S. Ganesha07f69b2022-11-30 20:43:5898bool IsSystemInstall() {
S. Ganesh2016d52b2022-12-01 19:58:5499 return IsSystemInstall(GetUpdaterScope());
100}
101
102bool IsSystemInstall(UpdaterScope scope) {
103 return scope == UpdaterScope::kSystem;
S. Ganesha07f69b2022-11-30 20:43:58104}
105
Sorin Jianu2b9271232021-06-01 18:40:14106} // namespace updater