| Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2021 The Chromium Authors |
| Sorin Jianu | 2b927123 | 2021-06-01 18:40:14 | [diff] [blame] | 2 | // 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 Jianu | a4b3d38 | 2023-11-10 20:08:46 | [diff] [blame] | 7 | #include <optional> |
| 8 | |
| Sorin Jianu | 2b927123 | 2021-06-01 18:40:14 | [diff] [blame] | 9 | #include "base/command_line.h" |
| Xiaoling Bao | 761d027 | 2023-06-06 22:50:19 | [diff] [blame] | 10 | #include "base/path_service.h" |
| Sorin Jianu | 2b927123 | 2021-06-01 18:40:14 | [diff] [blame] | 11 | #include "build/build_config.h" |
| 12 | #include "chrome/updater/constants.h" |
| S. Ganesh | 1b1e771 | 2022-12-15 23:57:38 | [diff] [blame] | 13 | #include "chrome/updater/util/util.h" |
| Sorin Jianu | 2b927123 | 2021-06-01 18:40:14 | [diff] [blame] | 14 | |
| Xiaohan Wang | 822cc38 | 2022-01-15 19:33:51 | [diff] [blame] | 15 | #if BUILDFLAG(IS_WIN) |
| Sorin Jianu | 2b927123 | 2021-06-01 18:40:14 | [diff] [blame] | 16 | #include "chrome/updater/tag.h" |
| Noah Rose Ledesma | fc9b365 | 2022-11-12 02:10:49 | [diff] [blame] | 17 | #include "chrome/updater/util/win_util.h" |
| Sorin Jianu | 2b927123 | 2021-06-01 18:40:14 | [diff] [blame] | 18 | #endif |
| 19 | |
| 20 | namespace updater { |
| 21 | namespace { |
| 22 | |
| S. Ganesh | 8fd7ddb | 2022-03-29 19:57:34 | [diff] [blame] | 23 | bool IsSystemProcessForCommandLine(const base::CommandLine& command_line) { |
| 24 | return command_line.HasSwitch(kSystemSwitch); |
| Sorin Jianu | 2b927123 | 2021-06-01 18:40:14 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | } // namespace |
| 28 | |
| S. Ganesh | cdee223 | 2024-01-25 09:38:50 | [diff] [blame] | 29 | std::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. Ganesh | cdee223 | 2024-01-25 09:38:50 | [diff] [blame] | 40 | return {}; |
| 41 | } |
| 42 | |
| S. Ganesh | ab09f27c | 2022-07-18 23:14:27 | [diff] [blame] | 43 | bool IsPrefersForCommandLine(const base::CommandLine& command_line) { |
| 44 | #if BUILDFLAG(IS_WIN) |
| S. Ganesh | cdee223 | 2024-01-25 09:38:50 | [diff] [blame] | 45 | std::optional<tagging::NeedsAdmin> needs_admin = |
| 46 | NeedsAdminFromTagArgs(GetTagArgsForCommandLine(command_line).tag_args); |
| 47 | return needs_admin ? *needs_admin == tagging::NeedsAdmin::kPrefers : false; |
| S. Ganesh | ab09f27c | 2022-07-18 23:14:27 | [diff] [blame] | 48 | #else |
| 49 | return false; |
| 50 | #endif |
| 51 | } |
| 52 | |
| S. Ganesh | 8fd7ddb | 2022-03-29 19:57:34 | [diff] [blame] | 53 | UpdaterScope GetUpdaterScopeForCommandLine( |
| 54 | const base::CommandLine& command_line) { |
| Xiaohan Wang | 822cc38 | 2022-01-15 19:33:51 | [diff] [blame] | 55 | #if BUILDFLAG(IS_WIN) |
| S. Ganesh | 8fd7ddb | 2022-03-29 19:57:34 | [diff] [blame] | 56 | if (IsSystemProcessForCommandLine(command_line)) { |
| Sorin Jianu | 2b927123 | 2021-06-01 18:40:14 | [diff] [blame] | 57 | return UpdaterScope::kSystem; |
| 58 | } |
| 59 | |
| Sorin Jianu | f62ce12e9 | 2023-05-22 12:06:59 | [diff] [blame] | 60 | // Assume only one app is present since bundles are not supported. |
| S. Ganesh | cdee223 | 2024-01-25 09:38:50 | [diff] [blame] | 61 | 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 Jianu | 2b927123 | 2021-06-01 18:40:14 | [diff] [blame] | 66 | return UpdaterScope::kSystem; |
| S. Ganesh | cdee223 | 2024-01-25 09:38:50 | [diff] [blame] | 67 | case tagging::NeedsAdmin::kNo: |
| Sorin Jianu | 2b927123 | 2021-06-01 18:40:14 | [diff] [blame] | 68 | return UpdaterScope::kUser; |
| S. Ganesh | cdee223 | 2024-01-25 09:38:50 | [diff] [blame] | 69 | case tagging::NeedsAdmin::kPrefers: |
| S. Ganesh | 15636a9 | 2022-04-09 01:53:08 | [diff] [blame] | 70 | return command_line.HasSwitch(kCmdLinePrefersUser) |
| 71 | ? UpdaterScope::kUser |
| 72 | : UpdaterScope::kSystem; |
| Sorin Jianu | 2b927123 | 2021-06-01 18:40:14 | [diff] [blame] | 73 | } |
| 74 | } |
| Xiaoling Bao | 761d027 | 2023-06-06 22:50:19 | [diff] [blame] | 75 | |
| 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 Jianu | a4b3d38 | 2023-11-10 20:08:46 | [diff] [blame] | 80 | std::optional<base::FilePath> system_shim_path = |
| Xiaoling Bao | 761d027 | 2023-06-06 22:50:19 | [diff] [blame] | 81 | 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 Jianu | 2b927123 | 2021-06-01 18:40:14 | [diff] [blame] | 87 | return UpdaterScope::kUser; |
| 88 | #else |
| S. Ganesh | 8fd7ddb | 2022-03-29 19:57:34 | [diff] [blame] | 89 | return IsSystemProcessForCommandLine(command_line) ? UpdaterScope::kSystem |
| 90 | : UpdaterScope::kUser; |
| Sorin Jianu | 2b927123 | 2021-06-01 18:40:14 | [diff] [blame] | 91 | #endif |
| 92 | } |
| 93 | |
| S. Ganesh | 8fd7ddb | 2022-03-29 19:57:34 | [diff] [blame] | 94 | UpdaterScope GetUpdaterScope() { |
| S. Ganesh | dfd5660 | 2023-06-24 02:21:24 | [diff] [blame] | 95 | return GetUpdaterScopeForCommandLine(*base::CommandLine::ForCurrentProcess()); |
| S. Ganesh | 8fd7ddb | 2022-03-29 19:57:34 | [diff] [blame] | 96 | } |
| 97 | |
| S. Ganesh | a07f69b | 2022-11-30 20:43:58 | [diff] [blame] | 98 | bool IsSystemInstall() { |
| S. Ganesh | 2016d52b | 2022-12-01 19:58:54 | [diff] [blame] | 99 | return IsSystemInstall(GetUpdaterScope()); |
| 100 | } |
| 101 | |
| 102 | bool IsSystemInstall(UpdaterScope scope) { |
| 103 | return scope == UpdaterScope::kSystem; |
| S. Ganesh | a07f69b | 2022-11-30 20:43:58 | [diff] [blame] | 104 | } |
| 105 | |
| Sorin Jianu | 2b927123 | 2021-06-01 18:40:14 | [diff] [blame] | 106 | } // namespace updater |