| Joshua Pawlicki | 85369dc | 2024-02-29 17:40:03 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CHROME_UPDATER_UPDATE_SERVICE_IMPL_IMPL_H_ |
| 6 | #define CHROME_UPDATER_UPDATE_SERVICE_IMPL_IMPL_H_ |
| 7 | |
| Joshua Pawlicki | 3435a25d5 | 2025-08-06 17:38:56 | [diff] [blame] | 8 | #include <cstdint> |
| Joshua Pawlicki | 85369dc | 2024-02-29 17:40:03 | [diff] [blame] | 9 | #include <map> |
| 10 | #include <optional> |
| 11 | #include <string> |
| 12 | #include <vector> |
| 13 | |
| 14 | #include "base/containers/flat_map.h" |
| 15 | #include "base/containers/queue.h" |
| 16 | #include "base/functional/callback_forward.h" |
| 17 | #include "base/memory/scoped_refptr.h" |
| 18 | #include "base/sequence_checker.h" |
| 19 | #include "base/values.h" |
| S. Ganesh | 52e2c90 | 2025-08-29 01:10:45 | [diff] [blame] | 20 | #include "chrome/updater/registration_data.h" |
| Joshua Pawlicki | 85369dc | 2024-02-29 17:40:03 | [diff] [blame] | 21 | #include "chrome/updater/update_service.h" |
| 22 | |
| 23 | namespace base { |
| 24 | class FilePath; |
| 25 | class SequencedTaskRunner; |
| 26 | class Version; |
| 27 | } // namespace base |
| 28 | |
| Xiaoling Bao | 4a698aa | 2025-02-11 00:34:14 | [diff] [blame] | 29 | namespace policy { |
| 30 | enum class PolicyFetchReason; |
| 31 | } // namespace policy |
| 32 | |
| Joshua Pawlicki | 85369dc | 2024-02-29 17:40:03 | [diff] [blame] | 33 | namespace update_client { |
| 34 | class UpdateClient; |
| 35 | } // namespace update_client |
| 36 | |
| 37 | namespace updater { |
| 38 | class Configurator; |
| 39 | class PersistedData; |
| 40 | class PolicyService; |
| Joshua Pawlicki | 85369dc | 2024-02-29 17:40:03 | [diff] [blame] | 41 | |
| Joshua Pawlicki | 85369dc | 2024-02-29 17:40:03 | [diff] [blame] | 42 | // All functions and callbacks must be called on the same sequence. |
| 43 | class UpdateServiceImplImpl : public UpdateService { |
| 44 | public: |
| 45 | explicit UpdateServiceImplImpl(scoped_refptr<Configurator> config); |
| 46 | |
| 47 | // Overrides for updater::UpdateService. |
| 48 | void GetVersion( |
| 49 | base::OnceCallback<void(const base::Version&)> callback) override; |
| Xiaoling Bao | 4a698aa | 2025-02-11 00:34:14 | [diff] [blame] | 50 | void FetchPolicies(policy::PolicyFetchReason reason, |
| 51 | base::OnceCallback<void(int)> callback) override; |
| Joshua Pawlicki | 85369dc | 2024-02-29 17:40:03 | [diff] [blame] | 52 | void RegisterApp(const RegistrationRequest& request, |
| 53 | base::OnceCallback<void(int)> callback) override; |
| 54 | void GetAppStates( |
| 55 | base::OnceCallback<void(const std::vector<AppState>&)>) override; |
| 56 | void RunPeriodicTasks(base::OnceClosure callback) override; |
| Sorin Jianu | 92a871e | 2024-08-27 18:06:56 | [diff] [blame] | 57 | void CheckForUpdate( |
| 58 | const std::string& app_id, |
| 59 | Priority priority, |
| 60 | PolicySameVersionUpdate policy_same_version_update, |
| S. Ganesh | 57c3e92 | 2024-12-20 02:16:05 | [diff] [blame] | 61 | const std::string& language, |
| Sorin Jianu | 92a871e | 2024-08-27 18:06:56 | [diff] [blame] | 62 | base::RepeatingCallback<void(const UpdateState&)> state_update, |
| 63 | base::OnceCallback<void(Result)> callback) override; |
| Joshua Pawlicki | 85369dc | 2024-02-29 17:40:03 | [diff] [blame] | 64 | void Update(const std::string& app_id, |
| 65 | const std::string& install_data_index, |
| 66 | Priority priority, |
| 67 | PolicySameVersionUpdate policy_same_version_update, |
| S. Ganesh | 57c3e92 | 2024-12-20 02:16:05 | [diff] [blame] | 68 | const std::string& language, |
| Sorin Jianu | 92a871e | 2024-08-27 18:06:56 | [diff] [blame] | 69 | base::RepeatingCallback<void(const UpdateState&)> state_update, |
| Sorin Jianu | 27e5ad2 | 2024-08-26 18:30:24 | [diff] [blame] | 70 | base::OnceCallback<void(Result)> callback) override; |
| Sorin Jianu | 92a871e | 2024-08-27 18:06:56 | [diff] [blame] | 71 | void UpdateAll(base::RepeatingCallback<void(const UpdateState&)> state_update, |
| Sorin Jianu | 27e5ad2 | 2024-08-26 18:30:24 | [diff] [blame] | 72 | base::OnceCallback<void(Result)> callback) override; |
| Joshua Pawlicki | 85369dc | 2024-02-29 17:40:03 | [diff] [blame] | 73 | void Install(const RegistrationRequest& registration, |
| 74 | const std::string& client_install_data, |
| 75 | const std::string& install_data_index, |
| 76 | Priority priority, |
| S. Ganesh | 57c3e92 | 2024-12-20 02:16:05 | [diff] [blame] | 77 | const std::string& language, |
| Sorin Jianu | 92a871e | 2024-08-27 18:06:56 | [diff] [blame] | 78 | base::RepeatingCallback<void(const UpdateState&)> state_update, |
| Sorin Jianu | 27e5ad2 | 2024-08-26 18:30:24 | [diff] [blame] | 79 | base::OnceCallback<void(Result)> callback) override; |
| Joshua Pawlicki | 85369dc | 2024-02-29 17:40:03 | [diff] [blame] | 80 | void CancelInstalls(const std::string& app_id) override; |
| Sorin Jianu | 92a871e | 2024-08-27 18:06:56 | [diff] [blame] | 81 | void RunInstaller( |
| 82 | const std::string& app_id, |
| 83 | const base::FilePath& installer_path, |
| 84 | const std::string& install_args, |
| 85 | const std::string& install_data, |
| 86 | const std::string& install_settings, |
| S. Ganesh | 57c3e92 | 2024-12-20 02:16:05 | [diff] [blame] | 87 | const std::string& language, |
| Sorin Jianu | 92a871e | 2024-08-27 18:06:56 | [diff] [blame] | 88 | base::RepeatingCallback<void(const UpdateState&)> state_update, |
| 89 | base::OnceCallback<void(Result)> callback) override; |
| Joshua Pawlicki | 85369dc | 2024-02-29 17:40:03 | [diff] [blame] | 90 | |
| 91 | private: |
| 92 | ~UpdateServiceImplImpl() override; |
| 93 | |
| 94 | // Runs the task at the head of `tasks_`, if any. |
| 95 | void TaskStart(); |
| 96 | |
| 97 | // Pops `tasks_`, and calls TaskStart. |
| 98 | void TaskDone(); |
| 99 | |
| 100 | // Installs applications in the wake task based on the ForceInstalls policy. |
| Sorin Jianu | 92a871e | 2024-08-27 18:06:56 | [diff] [blame] | 101 | void ForceInstall( |
| 102 | base::RepeatingCallback<void(const UpdateState&)> state_update, |
| 103 | base::OnceCallback<void(Result)> callback); |
| Joshua Pawlicki | 85369dc | 2024-02-29 17:40:03 | [diff] [blame] | 104 | |
| S. Ganesh | 85f8aed3 | 2025-01-29 01:33:42 | [diff] [blame] | 105 | void GetAppStatesImpl( |
| 106 | base::OnceCallback<void(const std::vector<AppState>&)> callback); |
| 107 | |
| S. Ganesh | b5b75d7 | 2025-01-23 13:30:19 | [diff] [blame] | 108 | void CheckForUpdateImpl( |
| 109 | const std::string& app_id, |
| 110 | Priority priority, |
| 111 | PolicySameVersionUpdate policy_same_version_update, |
| 112 | const std::string& language, |
| 113 | base::RepeatingCallback<void(const UpdateState&)> state_update, |
| 114 | base::OnceCallback<void(Result)> callback); |
| 115 | |
| 116 | void UpdateImpl( |
| 117 | const std::string& app_id, |
| 118 | const std::string& install_data_index, |
| 119 | Priority priority, |
| 120 | PolicySameVersionUpdate policy_same_version_update, |
| 121 | const std::string& language, |
| 122 | base::RepeatingCallback<void(const UpdateState&)> state_update, |
| 123 | base::OnceCallback<void(Result)> callback); |
| 124 | |
| S. Ganesh | 85f8aed3 | 2025-01-29 01:33:42 | [diff] [blame] | 125 | void InstallImpl( |
| 126 | const RegistrationRequest& registration, |
| 127 | const std::string& client_install_data, |
| 128 | const std::string& install_data_index, |
| 129 | Priority priority, |
| 130 | const std::string& language, |
| 131 | base::RepeatingCallback<void(const UpdateState&)> state_update, |
| 132 | base::OnceCallback<void(Result)> callback); |
| 133 | |
| 134 | void RunInstallerImpl( |
| 135 | const std::string& app_id, |
| 136 | const base::FilePath& installer_path, |
| 137 | const std::string& install_args, |
| 138 | const std::string& install_data, |
| 139 | const std::string& install_settings, |
| 140 | const std::string& language, |
| 141 | base::RepeatingCallback<void(const UpdateState&)> state_update, |
| 142 | base::OnceCallback<void(Result)> callback); |
| 143 | |
| Joshua Pawlicki | 85369dc | 2024-02-29 17:40:03 | [diff] [blame] | 144 | bool IsUpdateDisabledByPolicy(const std::string& app_id, |
| 145 | Priority priority, |
| 146 | bool is_install, |
| 147 | int& policy); |
| Sorin Jianu | 92a871e | 2024-08-27 18:06:56 | [diff] [blame] | 148 | void HandleUpdateDisabledByPolicy( |
| 149 | const std::string& app_id, |
| 150 | int policy, |
| 151 | bool is_install, |
| S. Ganesh | 57c3e92 | 2024-12-20 02:16:05 | [diff] [blame] | 152 | const std::string& language, |
| Sorin Jianu | 92a871e | 2024-08-27 18:06:56 | [diff] [blame] | 153 | base::RepeatingCallback<void(const UpdateState&)> state_update, |
| 154 | base::OnceCallback<void(Result)> callback); |
| Joshua Pawlicki | 85369dc | 2024-02-29 17:40:03 | [diff] [blame] | 155 | |
| 156 | void OnShouldBlockCheckForUpdateForMeteredNetwork( |
| 157 | const std::string& app_id, |
| 158 | Priority priority, |
| 159 | PolicySameVersionUpdate policy_same_version_update, |
| S. Ganesh | 57c3e92 | 2024-12-20 02:16:05 | [diff] [blame] | 160 | const std::string& language, |
| Sorin Jianu | 92a871e | 2024-08-27 18:06:56 | [diff] [blame] | 161 | base::RepeatingCallback<void(const UpdateState&)> state_update, |
| Sorin Jianu | 27e5ad2 | 2024-08-26 18:30:24 | [diff] [blame] | 162 | base::OnceCallback<void(Result)> callback, |
| Joshua Pawlicki | 85369dc | 2024-02-29 17:40:03 | [diff] [blame] | 163 | bool update_blocked); |
| 164 | |
| 165 | void OnShouldBlockUpdateForMeteredNetwork( |
| 166 | const std::vector<std::string>& app_ids, |
| Sorin Jianu | 7c2d13c5 | 2024-08-28 19:41:48 | [diff] [blame] | 167 | const base::flat_map<std::string, std::string>& app_client_install_data, |
| 168 | const base::flat_map<std::string, std::string>& app_install_data_index, |
| Joshua Pawlicki | 85369dc | 2024-02-29 17:40:03 | [diff] [blame] | 169 | Priority priority, |
| 170 | PolicySameVersionUpdate policy_same_version_update, |
| S. Ganesh | 57c3e92 | 2024-12-20 02:16:05 | [diff] [blame] | 171 | const std::string& language, |
| Sorin Jianu | 92a871e | 2024-08-27 18:06:56 | [diff] [blame] | 172 | base::RepeatingCallback<void(const UpdateState&)> state_update, |
| Sorin Jianu | 27e5ad2 | 2024-08-26 18:30:24 | [diff] [blame] | 173 | base::OnceCallback<void(Result)> callback, |
| Joshua Pawlicki | 85369dc | 2024-02-29 17:40:03 | [diff] [blame] | 174 | bool update_blocked); |
| 175 | |
| 176 | void OnShouldBlockForceInstallForMeteredNetwork( |
| 177 | const std::vector<std::string>& app_ids, |
| Sorin Jianu | 7c2d13c5 | 2024-08-28 19:41:48 | [diff] [blame] | 178 | const base::flat_map<std::string, std::string>& app_client_install_data, |
| 179 | const base::flat_map<std::string, std::string>& app_install_data_index, |
| Joshua Pawlicki | 85369dc | 2024-02-29 17:40:03 | [diff] [blame] | 180 | PolicySameVersionUpdate policy_same_version_update, |
| Sorin Jianu | 92a871e | 2024-08-27 18:06:56 | [diff] [blame] | 181 | base::RepeatingCallback<void(const UpdateState&)> state_update, |
| Sorin Jianu | 27e5ad2 | 2024-08-26 18:30:24 | [diff] [blame] | 182 | base::OnceCallback<void(Result)> callback, |
| Joshua Pawlicki | 85369dc | 2024-02-29 17:40:03 | [diff] [blame] | 183 | bool update_blocked); |
| 184 | |
| Noah Rose Ledesma | 32ba9b1 | 2024-10-25 14:02:54 | [diff] [blame] | 185 | void MaybeInstallEnterpriseCompanionAppOTA(base::OnceClosure callback, |
| 186 | bool is_cloud_managed); |
| 187 | |
| Joshua Pawlicki | 85369dc | 2024-02-29 17:40:03 | [diff] [blame] | 188 | SEQUENCE_CHECKER(sequence_checker_); |
| 189 | |
| 190 | scoped_refptr<Configurator> config_; |
| 191 | scoped_refptr<base::SequencedTaskRunner> main_task_runner_; |
| 192 | scoped_refptr<update_client::UpdateClient> update_client_; |
| 193 | |
| 194 | // The queue serializes periodic task execution. |
| 195 | base::queue<base::OnceClosure> tasks_; |
| 196 | |
| 197 | // Cancellation callbacks, keyed by appid. |
| 198 | std::multimap<std::string, base::RepeatingClosure> cancellation_callbacks_; |
| 199 | }; |
| 200 | |
| 201 | namespace internal { |
| 202 | UpdateService::Result ToResult(update_client::Error error); |
| 203 | |
| 204 | void GetComponents( |
| 205 | scoped_refptr<PolicyService> policy_service, |
| 206 | crx_file::VerifierFormat verifier_format, |
| Joshua Pawlicki | 3435a25d5 | 2025-08-06 17:38:56 | [diff] [blame] | 207 | std::optional<std::vector<uint8_t>> crx_public_key_hash, |
| Joshua Pawlicki | 85369dc | 2024-02-29 17:40:03 | [diff] [blame] | 208 | scoped_refptr<PersistedData> persisted_data, |
| Sorin Jianu | 7c2d13c5 | 2024-08-28 19:41:48 | [diff] [blame] | 209 | const base::flat_map<std::string, std::string>& app_client_install_data, |
| 210 | const base::flat_map<std::string, std::string>& app_install_data_index, |
| Joshua Pawlicki | f14af82 | 2024-05-22 19:37:11 | [diff] [blame] | 211 | const std::string& install_source, |
| Joshua Pawlicki | 85369dc | 2024-02-29 17:40:03 | [diff] [blame] | 212 | UpdateService::Priority priority, |
| 213 | bool update_blocked, |
| 214 | UpdateService::PolicySameVersionUpdate policy_same_version_update, |
| 215 | const std::vector<std::string>& ids, |
| 216 | base::OnceCallback< |
| 217 | void(const std::vector<std::optional<update_client::CrxComponent>>&)> |
| 218 | callback); |
| 219 | |
| S Ganesh | 9eb87fd | 2024-11-27 03:13:08 | [diff] [blame] | 220 | #if BUILDFLAG(IS_WIN) |
| 221 | std::string GetInstallerText(UpdateService::ErrorCategory error_category, |
| 222 | int error_code, |
| S. Ganesh | 57c3e92 | 2024-12-20 02:16:05 | [diff] [blame] | 223 | int extra_code, |
| 224 | const std::string& language); |
| S Ganesh | 9eb87fd | 2024-11-27 03:13:08 | [diff] [blame] | 225 | #endif // BUILDFLAG(IS_WIN) |
| Joshua Pawlicki | 85369dc | 2024-02-29 17:40:03 | [diff] [blame] | 226 | } // namespace internal |
| 227 | |
| 228 | } // namespace updater |
| 229 | |
| 230 | #endif // CHROME_UPDATER_UPDATE_SERVICE_IMPL_IMPL_H_ |