sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors. All rights reserved. |
| 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 COMPONENTS_UPDATE_CLIENT_UPDATE_CLIENT_H_ |
| 6 | #define COMPONENTS_UPDATE_CLIENT_UPDATE_CLIENT_H_ |
| 7 | |
| 8 | #include <stdint.h> |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 9 | |
sorin | 2adb2ca | 2016-06-29 01:44:35 | [diff] [blame] | 10 | #include <map> |
Sorin Jianu | 9d64af67 | 2020-02-05 19:14:34 | [diff] [blame] | 11 | #include <memory> |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 12 | #include <string> |
| 13 | #include <vector> |
| 14 | |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 15 | #include "base/callback_forward.h" |
bauerb | 810e60f4 | 2015-02-05 01:09:10 | [diff] [blame] | 16 | #include "base/memory/ref_counted.h" |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 17 | #include "base/version.h" |
sorin | 2892f721 | 2016-11-07 18:59:43 | [diff] [blame] | 18 | #include "components/update_client/update_client_errors.h" |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 19 | #include "third_party/abseil-cpp/absl/types/optional.h" |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 20 | |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 21 | // The UpdateClient class is a facade with a simple interface. The interface |
| 22 | // exposes a few APIs to install a CRX or update a group of CRXs. |
| 23 | // |
| 24 | // The difference between a CRX install and a CRX update is relatively minor. |
| 25 | // The terminology going forward will use the word "update" to cover both |
| 26 | // install and update scenarios, except where details regarding the install |
| 27 | // case are relevant. |
| 28 | // |
| 29 | // Handling an update consists of a series of actions such as sending an update |
| 30 | // check to the server, followed by parsing the server response, identifying |
| 31 | // the CRXs that require an update, downloading the differential update if |
| 32 | // it is available, unpacking and patching the differential update, then |
| 33 | // falling back to trying a similar set of actions using the full update. |
| 34 | // At the end of this process, completion pings are sent to the server, |
| 35 | // as needed, for the CRXs which had updates. |
| 36 | // |
| 37 | // As a general idea, this code handles the action steps needed to update |
| 38 | // a group of components serially, one step at a time. However, concurrent |
| 39 | // execution of calls to UpdateClient::Update is possible, therefore, |
| 40 | // queuing of updates could happen in some cases. More below. |
| 41 | // |
| 42 | // The UpdateClient class features a subject-observer interface to observe |
| 43 | // the CRX state changes during an update. |
| 44 | // |
| 45 | // The threading model for this code assumes that most of the code in the |
| 46 | // public interface runs on a SingleThreadTaskRunner. |
| 47 | // This task runner corresponds to the browser UI thread in many cases. There |
| 48 | // are parts of the installer interface that run on blocking task runners, which |
| 49 | // are usually threads in a thread pool. |
| 50 | // |
| 51 | // Using the UpdateClient is relatively easy. This assumes that the client |
| 52 | // of this code has already implemented the observer interface as needed, and |
| 53 | // can provide an installer, as described below. |
| 54 | // |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 55 | // std::unique_ptr<UpdateClient> update_client(UpdateClientFactory(...)); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 56 | // update_client->AddObserver(&observer); |
| 57 | // std::vector<std::string> ids; |
| 58 | // ids.push_back(...)); |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 59 | // update_client->Update(ids, base::BindOnce(...), base::BindOnce(...)); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 60 | // |
| 61 | // UpdateClient::Update takes two callbacks as parameters. First callback |
| 62 | // allows the client of this code to provide an instance of CrxComponent |
| 63 | // data structure that specifies additional parameters of the update. |
| 64 | // CrxComponent has a CrxInstaller data member, which must be provided by the |
| 65 | // callers of this class. The second callback indicates that this non-blocking |
| 66 | // call has completed. |
| 67 | // |
| 68 | // There could be several ways of triggering updates for a CRX, user-initiated, |
| 69 | // or timer-based. Since the execution of updates is concurrent, the parameters |
| 70 | // for the update must be provided right before the update is handled. |
| 71 | // Otherwise, the version of the CRX set in the CrxComponent may not be correct. |
| 72 | // |
| 73 | // The UpdateClient public interface includes two functions: Install and |
| 74 | // Update. These functions correspond to installing one CRX immediately as a |
| 75 | // foreground activity (Install), and updating a group of CRXs silently in the |
| 76 | // background (Update). This distinction is important. Background updates are |
| 77 | // queued up and their actions run serially, one at a time, for the purpose of |
| 78 | // conserving local resources such as CPU, network, and I/O. |
| 79 | // On the other hand, installs are never queued up but run concurrently, as |
| 80 | // requested by the user. |
| 81 | // |
| 82 | // The update client introduces a runtime constraint regarding interleaving |
| 83 | // updates and installs. If installs or updates for a given CRX are in progress, |
| 84 | // then installs for the same CRX will fail with a specific error. |
| 85 | // |
| 86 | // Implementation details. |
| 87 | // |
| 88 | // The implementation details below are not relevant to callers of this |
| 89 | // code. However, these design notes are relevant to the owners and maintainers |
| 90 | // of this module. |
| 91 | // |
| 92 | // The design for the update client consists of a number of abstractions |
| 93 | // such as: task, update engine, update context, and action. |
| 94 | // The execution model for these abstractions is simple. They usually expose |
| 95 | // a public, non-blocking Run function, and they invoke a callback when |
| 96 | // the Run function has completed. |
| 97 | // |
| 98 | // A task is the unit of work for the UpdateClient. A task is associated |
| 99 | // with a single call of the Update function. A task represents a group |
| 100 | // of CRXs that are updated together. |
| 101 | // |
| 102 | // The UpdateClient is responsible for the queuing of tasks, if queuing is |
| 103 | // needed. |
| 104 | // |
| 105 | // When the task runs, it calls the update engine to handle the updates for |
| 106 | // the CRXs associated with the task. The UpdateEngine is the abstraction |
| 107 | // responsible for breaking down the update in a set of discrete steps, which |
| 108 | // are implemented as actions, and running the actions. |
| 109 | // |
| 110 | // The UpdateEngine maintains a set of UpdateContext instances. Each of |
| 111 | // these instances maintains the update state for all the CRXs belonging to |
| 112 | // a given task. The UpdateContext contains a queue of CRX ids. |
| 113 | // The UpdateEngine will handle updates for the CRXs in the order they appear |
| 114 | // in the queue, until the queue is empty. |
| 115 | // |
| 116 | // The update state for each CRX is maintained in a container of CrxUpdateItem*. |
| 117 | // As actions run, each action updates the CRX state, represented by one of |
| 118 | // these CrxUpdateItem* instances. |
| 119 | // |
| 120 | // Although the UpdateEngine can and will run update tasks concurrently, the |
| 121 | // actions of a task are run sequentially. |
| 122 | // |
| 123 | // The Action is a polymorphic type. There is some code reuse for convenience, |
| 124 | // implemented as a mixin. The polymorphic behavior of some of the actions |
| 125 | // is achieved using a template method. |
| 126 | // |
| 127 | // State changes of a CRX could generate events, which are observed using a |
| 128 | // subject-observer interface. |
| 129 | // |
| 130 | // The actions chain up. In some sense, the actions implement a state machine, |
| 131 | // as the CRX undergoes a series of state transitions in the process of |
| 132 | // being checked for updates and applying the update. |
| 133 | |
waffles | d2d9a33 | 2016-04-09 01:59:57 | [diff] [blame] | 134 | class PrefRegistrySimple; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 135 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 136 | namespace base { |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 137 | class FilePath; |
| 138 | } |
| 139 | |
Joshua Pawlicki | f4b33f38 | 2018-08-17 17:36:51 | [diff] [blame] | 140 | namespace crx_file { |
| 141 | enum class VerifierFormat; |
| 142 | } |
| 143 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 144 | namespace update_client { |
| 145 | |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 146 | class Configurator; |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 147 | enum class Error; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 148 | struct CrxUpdateItem; |
| 149 | |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 150 | enum class ComponentState { |
| 151 | kNew, |
| 152 | kChecking, |
| 153 | kCanUpdate, |
| 154 | kDownloadingDiff, |
| 155 | kDownloading, |
| 156 | kDownloaded, |
| 157 | kUpdatingDiff, |
| 158 | kUpdating, |
| 159 | kUpdated, |
| 160 | kUpToDate, |
| 161 | kUpdateError, |
| 162 | kUninstalled, |
Sam Sappenfield | 4a06526 | 2020-07-29 19:28:18 | [diff] [blame] | 163 | kRegistration, |
Sorin Jianu | 4ab7c29 | 2017-06-15 18:40:21 | [diff] [blame] | 164 | kRun, |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 165 | kLastStatus |
| 166 | }; |
| 167 | |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 168 | // Defines an interface for a generic CRX installer. |
| 169 | class CrxInstaller : public base::RefCountedThreadSafe<CrxInstaller> { |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 170 | public: |
sorin | 2892f721 | 2016-11-07 18:59:43 | [diff] [blame] | 171 | // Contains the result of the Install operation. |
| 172 | struct Result { |
Sorin Jianu | 30c0e0b | 2021-02-01 17:00:52 | [diff] [blame] | 173 | Result() = default; |
sorin | 2892f721 | 2016-11-07 18:59:43 | [diff] [blame] | 174 | explicit Result(int error, int extended_error = 0) |
| 175 | : error(error), extended_error(extended_error) {} |
| 176 | explicit Result(InstallError error, int extended_error = 0) |
| 177 | : error(static_cast<int>(error)), extended_error(extended_error) {} |
Sorin Jianu | 30c0e0b | 2021-02-01 17:00:52 | [diff] [blame] | 178 | |
sorin | 2892f721 | 2016-11-07 18:59:43 | [diff] [blame] | 179 | int error = 0; // 0 indicates that install has been successful. |
| 180 | int extended_error = 0; |
Sorin Jianu | a26ee33af | 2021-01-20 04:18:06 | [diff] [blame] | 181 | |
| 182 | // Localized text displayed to the user, if applicable. |
| 183 | std::string installer_text; |
| 184 | |
| 185 | // Shell command run at the end of the install, if applicable. This string |
| 186 | // must be escaped to be a command line. |
| 187 | std::string installer_cmd_line; |
sorin | 2892f721 | 2016-11-07 18:59:43 | [diff] [blame] | 188 | }; |
| 189 | |
Sorin Jianu | 9d64af67 | 2020-02-05 19:14:34 | [diff] [blame] | 190 | struct InstallParams { |
| 191 | InstallParams(const std::string& run, const std::string& arguments); |
| 192 | std::string run; |
| 193 | std::string arguments; |
| 194 | }; |
| 195 | |
Sorin Jianu | c9d32b2f | 2020-05-08 23:07:26 | [diff] [blame] | 196 | using ProgressCallback = base::RepeatingCallback<void(int progress)>; |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 197 | using Callback = base::OnceCallback<void(const Result& result)>; |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 198 | |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 199 | // Called on the main thread when there was a problem unpacking or |
| 200 | // verifying the CRX. |error| is a non-zero value which is only meaningful |
| 201 | // to the caller. |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 202 | virtual void OnUpdateError(int error) = 0; |
| 203 | |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 204 | // Called by the update service when a CRX has been unpacked |
Sorin Jianu | c9d32b2f | 2020-05-08 23:07:26 | [diff] [blame] | 205 | // and it is ready to be installed. This method may be called from a |
| 206 | // sequence other than the main sequence. |
| 207 | // |unpack_path| contains the temporary directory with all the unpacked CRX |
| 208 | // files. |
| 209 | // |pubkey| contains the public key of the CRX in the PEM format, without the |
| 210 | // header and the footer. |
| 211 | // |install_params| is an optional parameter which provides the name and the |
| 212 | // arguments for a binary program which is invoked as part of the install or |
| 213 | // update flows. |
| 214 | // |progress_callback| reports installer progress. This callback must be run |
| 215 | // directly instead of posting it. |
| 216 | // |callback| must be the last callback invoked and it indicates that the |
| 217 | // install flow has completed. |
Sorin Jianu | 7aa6d1f | 2017-10-13 20:29:29 | [diff] [blame] | 218 | virtual void Install(const base::FilePath& unpack_path, |
Sorin Jianu | ea5534e9 | 2017-10-27 01:40:28 | [diff] [blame] | 219 | const std::string& public_key, |
Sorin Jianu | 9d64af67 | 2020-02-05 19:14:34 | [diff] [blame] | 220 | std::unique_ptr<InstallParams> install_params, |
Sorin Jianu | c9d32b2f | 2020-05-08 23:07:26 | [diff] [blame] | 221 | ProgressCallback progress_callback, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 222 | Callback callback) = 0; |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 223 | |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 224 | // Sets |installed_file| to the full path to the installed |file|. |file| is |
| 225 | // the filename of the file in this CRX. Returns false if this is |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 226 | // not possible (the file has been removed or modified, or its current |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 227 | // location is unknown). Otherwise, it returns true. |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 228 | virtual bool GetInstalledFile(const std::string& file, |
| 229 | base::FilePath* installed_file) = 0; |
| 230 | |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 231 | // Called when a CRX has been unregistered and all versions should |
| 232 | // be uninstalled from disk. Returns true if uninstallation is supported, |
| 233 | // and false otherwise. |
bauerb | 1f6657e7 | 2015-02-09 00:00:27 | [diff] [blame] | 234 | virtual bool Uninstall() = 0; |
| 235 | |
bauerb | 810e60f4 | 2015-02-05 01:09:10 | [diff] [blame] | 236 | protected: |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 237 | friend class base::RefCountedThreadSafe<CrxInstaller>; |
bauerb | 810e60f4 | 2015-02-05 01:09:10 | [diff] [blame] | 238 | |
Sorin Jianu | 3088115 | 2020-03-16 14:31:19 | [diff] [blame] | 239 | virtual ~CrxInstaller() = default; |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 240 | }; |
| 241 | |
Sorin Jianu | 2fe49a0 | 2019-11-25 19:15:56 | [diff] [blame] | 242 | // Defines an interface to handle |action| elements in the update response. |
| 243 | // The current implementation only handles run actions bound to a CRX, meaning |
| 244 | // that such CRX is unpacked and an executable file, contained inside the CRX, |
| 245 | // is run, then the results of the invocation are collected by the callback. |
| 246 | class ActionHandler : public base::RefCountedThreadSafe<ActionHandler> { |
| 247 | public: |
| 248 | using Callback = |
| 249 | base::OnceCallback<void(bool succeeded, int error_code, int extra_code1)>; |
| 250 | |
| 251 | virtual void Handle(const base::FilePath& action, |
| 252 | const std::string& session_id, |
| 253 | Callback callback) = 0; |
| 254 | |
| 255 | protected: |
| 256 | friend class base::RefCountedThreadSafe<ActionHandler>; |
| 257 | |
| 258 | virtual ~ActionHandler() = default; |
| 259 | }; |
| 260 | |
sorin | 2adb2ca | 2016-06-29 01:44:35 | [diff] [blame] | 261 | // A dictionary of installer-specific, arbitrary name-value pairs, which |
| 262 | // may be used in the update checks requests. |
| 263 | using InstallerAttributes = std::map<std::string, std::string>; |
| 264 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 265 | struct CrxComponent { |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 266 | CrxComponent(); |
vmpstr | b6449d51 | 2016-02-25 23:55:40 | [diff] [blame] | 267 | CrxComponent(const CrxComponent& other); |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 268 | ~CrxComponent(); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 269 | |
Joshua Pawlicki | 9656e392 | 2019-09-16 16:34:51 | [diff] [blame] | 270 | // Optional SHA256 hash of the CRX's public key. If not supplied, the |
| 271 | // unpacker can accept any CRX for this app, provided that the CRX meets the |
| 272 | // VerifierFormat requirements specified by the service's configurator. |
| 273 | // Callers that know or need a specific developer signature on acceptable CRX |
| 274 | // files must provide this. |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 275 | std::vector<uint8_t> pk_hash; |
Joshua Pawlicki | 9656e392 | 2019-09-16 16:34:51 | [diff] [blame] | 276 | |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 277 | scoped_refptr<CrxInstaller> installer; |
Sorin Jianu | 2fe49a0 | 2019-11-25 19:15:56 | [diff] [blame] | 278 | scoped_refptr<ActionHandler> action_handler; |
| 279 | |
Joshua Pawlicki | 9656e392 | 2019-09-16 16:34:51 | [diff] [blame] | 280 | std::string app_id; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 281 | |
| 282 | // The current version if the CRX is updated. Otherwise, "0" or "0.0" if |
| 283 | // the CRX is installed. |
pwnall | db0b7241 | 2016-08-19 21:39:12 | [diff] [blame] | 284 | base::Version version; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 285 | |
| 286 | std::string fingerprint; // Optional. |
| 287 | std::string name; // Optional. |
sorin | 2adb2ca | 2016-06-29 01:44:35 | [diff] [blame] | 288 | |
| 289 | // Optional. |
| 290 | // Valid values for the name part of an attribute match |
| 291 | // ^[-_a-zA-Z0-9]{1,256}$ and valid values the value part of an attribute |
Joshua Pawlicki | aabed6d | 2019-12-02 17:10:09 | [diff] [blame] | 292 | // match ^[-.,;+_=$a-zA-Z0-9]{0,256}$ . |
sorin | 2adb2ca | 2016-06-29 01:44:35 | [diff] [blame] | 293 | InstallerAttributes installer_attributes; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 294 | |
| 295 | // Specifies that the CRX can be background-downloaded in some cases. |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 296 | // The default for this value is |true|. |
| 297 | bool allows_background_download; |
| 298 | |
| 299 | // Specifies that the update checks and pings associated with this component |
| 300 | // require confidentiality. The default for this value is |true|. As a side |
| 301 | // note, the confidentiality of the downloads is enforced by the server, |
| 302 | // which only returns secure download URLs in this case. |
| 303 | bool requires_network_encryption; |
sorin | cb4e5e9 | 2016-08-02 21:48:40 | [diff] [blame] | 304 | |
Joshua Pawlicki | f4b33f38 | 2018-08-17 17:36:51 | [diff] [blame] | 305 | // Specifies the strength of package validation required for the item. |
| 306 | crx_file::VerifierFormat crx_format_requirement; |
| 307 | |
sorin | cb4e5e9 | 2016-08-02 21:48:40 | [diff] [blame] | 308 | // True if the component allows enabling or disabling updates by group policy. |
| 309 | // This member should be set to |false| for data, non-binary components, such |
| 310 | // as CRLSet, Supervised User Whitelists, STH Set, Origin Trials, and File |
| 311 | // Type Policies. |
| 312 | bool supports_group_policy_enable_component_updates; |
Minh X. Nguyen | ca599fb | 2017-09-14 21:10:39 | [diff] [blame] | 313 | |
| 314 | // Reasons why this component/extension is disabled. |
| 315 | std::vector<int> disabled_reasons; |
Minh X. Nguyen | f8c530aa | 2017-12-13 23:54:17 | [diff] [blame] | 316 | |
| 317 | // Information about where the component/extension was installed from. |
| 318 | // For extension, this information is set from the update service, which |
| 319 | // gets the install source from the update URL. |
| 320 | std::string install_source; |
Minh X. Nguyen | fc16497e | 2018-04-24 16:05:35 | [diff] [blame] | 321 | |
| 322 | // Information about where the component/extension was loaded from. |
| 323 | // For extensions, this information is inferred from the extension |
| 324 | // registry. |
| 325 | std::string install_location; |
Yann Dago | 8450525 | 2020-08-27 22:20:51 | [diff] [blame] | 326 | |
| 327 | // Information about the channel to send to the update server when updating |
| 328 | // the component. This optional field is typically populated by policy and is |
| 329 | // only populated on managed devices. |
| 330 | std::string channel; |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 331 | }; |
| 332 | |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 333 | // Called when a non-blocking call of UpdateClient completes. |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 334 | using Callback = base::OnceCallback<void(Error error)>; |
Sorin Jianu | f40ab4b3 | 2017-10-06 22:53:41 | [diff] [blame] | 335 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 336 | // All methods are safe to call only from the browser's main thread. Once an |
| 337 | // instance of this class is created, the reference to it must be released |
| 338 | // only after the thread pools of the browser process have been destroyed and |
| 339 | // the browser process has gone single-threaded. |
Sorin Jianu | 569aa719 | 2020-07-23 16:09:54 | [diff] [blame] | 340 | class UpdateClient : public base::RefCountedThreadSafe<UpdateClient> { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 341 | public: |
Sorin Jianu | a5d25fb | 2020-11-04 15:17:07 | [diff] [blame] | 342 | // Returns `CrxComponent` instances corresponding to the component ids |
| 343 | // passed as an argument to the callback. The order of components in the input |
| 344 | // and output vectors must match. If the instance of the `CrxComponent` is not |
| 345 | // available for some reason, implementors of the callback must not skip |
| 346 | // skip the component, and instead, they must insert a `nullopt` value in |
| 347 | // the output vector. |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 348 | using CrxDataCallback = |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 349 | base::OnceCallback<std::vector<absl::optional<CrxComponent>>( |
Sorin Jianu | 7c22795b | 2018-04-26 22:16:52 | [diff] [blame] | 350 | const std::vector<std::string>& ids)>; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 351 | |
Sorin Jianu | dfb12a4 | 2020-03-10 04:12:03 | [diff] [blame] | 352 | // Called when state changes occur during an Install or Update call. |
| 353 | using CrxStateChangeCallback = |
| 354 | base::RepeatingCallback<void(CrxUpdateItem item)>; |
| 355 | |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 356 | // Defines an interface to observe the UpdateClient. It provides |
| 357 | // notifications when state changes occur for the service itself or for the |
| 358 | // registered CRXs. |
| 359 | class Observer { |
| 360 | public: |
| 361 | enum class Events { |
| 362 | // Sent before the update client does an update check. |
sorin | 30474f0 | 2017-04-27 00:45:48 | [diff] [blame] | 363 | COMPONENT_CHECKING_FOR_UPDATES = 1, |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 364 | |
Sorin Jianu | d0298dd3 | 2019-10-25 16:47:10 | [diff] [blame] | 365 | // Sent when there is a new version of a registered CRX. The CRX will be |
| 366 | // downloaded after the notification unless the update client inserts |
| 367 | // a wait because of a throttling policy. |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 368 | COMPONENT_UPDATE_FOUND, |
| 369 | |
| 370 | // Sent when a CRX is in the update queue but it can't be acted on |
| 371 | // right away, because the update client spaces out CRX updates due to a |
| 372 | // throttling policy. |
| 373 | COMPONENT_WAIT, |
| 374 | |
| 375 | // Sent after the new CRX has been downloaded but before the install |
| 376 | // or the upgrade is attempted. |
| 377 | COMPONENT_UPDATE_READY, |
| 378 | |
| 379 | // Sent when a CRX has been successfully updated. |
| 380 | COMPONENT_UPDATED, |
| 381 | |
Sorin Jianu | cbb10e1 | 2018-01-23 18:01:44 | [diff] [blame] | 382 | // Sent when a CRX has not been updated because there was no update |
| 383 | // available for this component. |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 384 | COMPONENT_NOT_UPDATED, |
| 385 | |
Sorin Jianu | cbb10e1 | 2018-01-23 18:01:44 | [diff] [blame] | 386 | // Sent when an error ocurred during an update for any reason, including |
| 387 | // the update check itself failed, or the download of the update payload |
| 388 | // failed, or applying the update failed. |
| 389 | COMPONENT_UPDATE_ERROR, |
| 390 | |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 391 | // Sent when CRX bytes are being downloaded. |
| 392 | COMPONENT_UPDATE_DOWNLOADING, |
Sorin Jianu | c9d32b2f | 2020-05-08 23:07:26 | [diff] [blame] | 393 | |
| 394 | // Sent when install progress is received from the CRX installer. |
| 395 | COMPONENT_UPDATE_UPDATING, |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 396 | }; |
| 397 | |
Sorin Jianu | 3088115 | 2020-03-16 14:31:19 | [diff] [blame] | 398 | virtual ~Observer() = default; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 399 | |
| 400 | // Called by the update client when a state change happens. |
| 401 | // If an |id| is specified, then the event is fired on behalf of the |
| 402 | // specific CRX. The implementors of this interface are |
| 403 | // expected to filter the relevant events based on the id of the CRX. |
| 404 | virtual void OnEvent(Events event, const std::string& id) = 0; |
| 405 | }; |
| 406 | |
| 407 | // Adds an observer for this class. An observer should not be added more |
| 408 | // than once. The caller retains the ownership of the observer object. |
| 409 | virtual void AddObserver(Observer* observer) = 0; |
| 410 | |
| 411 | // Removes an observer. It is safe for an observer to be removed while |
| 412 | // the observers are being notified. |
| 413 | virtual void RemoveObserver(Observer* observer) = 0; |
| 414 | |
sorin | 842703b | 2016-11-02 23:59:23 | [diff] [blame] | 415 | // Installs the specified CRX. Calls back on |callback| after the |
Sorin Jianu | dfb12a4 | 2020-03-10 04:12:03 | [diff] [blame] | 416 | // update has been handled. Provides state change notifications through |
| 417 | // invocations of the optional |crx_state_change_callback| callback. |
| 418 | // The |error| parameter of the |callback| contains an error code in the case |
| 419 | // of a run-time error, or 0 if the install has been handled successfully. |
| 420 | // Overlapping calls of this function are executed concurrently, as long as |
| 421 | // the id parameter is different, meaning that installs of different |
| 422 | // components are parallelized. |
sorin | 08d153c | 2015-10-30 00:04:20 | [diff] [blame] | 423 | // The |Install| function is intended to be used for foreground installs of |
| 424 | // one CRX. These cases are usually associated with on-demand install |
| 425 | // scenarios, which are triggered by user actions. Installs are never |
| 426 | // queued up. |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 427 | virtual void Install(const std::string& id, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 428 | CrxDataCallback crx_data_callback, |
Sorin Jianu | dfb12a4 | 2020-03-10 04:12:03 | [diff] [blame] | 429 | CrxStateChangeCallback crx_state_change_callback, |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 430 | Callback callback) = 0; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 431 | |
| 432 | // Updates the specified CRXs. Calls back on |crx_data_callback| before the |
| 433 | // update is attempted to give the caller the opportunity to provide the |
Sorin Jianu | dfb12a4 | 2020-03-10 04:12:03 | [diff] [blame] | 434 | // instances of CrxComponent to be used for this update. Provides state change |
| 435 | // notifications through invocations of the optional |
| 436 | // |crx_state_change_callback| callback. |
| 437 | // The |Update| function is intended to be used for background updates of |
| 438 | // several CRXs. Overlapping calls to this function result in a queuing |
| 439 | // behavior, and the execution of each call is serialized. In addition, |
| 440 | // updates are always queued up when installs are running. The |is_foreground| |
| 441 | // parameter must be set to true if the invocation of this function is a |
| 442 | // result of a user initiated update. |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 443 | virtual void Update(const std::vector<std::string>& ids, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 444 | CrxDataCallback crx_data_callback, |
Sorin Jianu | dfb12a4 | 2020-03-10 04:12:03 | [diff] [blame] | 445 | CrxStateChangeCallback crx_state_change_callback, |
Sorin Jianu | b41a592a | 2018-03-02 16:30:27 | [diff] [blame] | 446 | bool is_foreground, |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 447 | Callback callback) = 0; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 448 | |
sorin | 805aa0311 | 2016-01-14 23:01:31 | [diff] [blame] | 449 | // Sends an uninstall ping for the CRX identified by |id| and |version|. The |
| 450 | // |reason| parameter is defined by the caller. The current implementation of |
| 451 | // this function only sends a best-effort, fire-and-forget ping. It has no |
| 452 | // other side effects regarding installs or updates done through an instance |
| 453 | // of this class. |
| 454 | virtual void SendUninstallPing(const std::string& id, |
pwnall | db0b7241 | 2016-08-19 21:39:12 | [diff] [blame] | 455 | const base::Version& version, |
sorin | 8037ac8c | 2017-04-19 16:28:00 | [diff] [blame] | 456 | int reason, |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 457 | Callback callback) = 0; |
sorin | 805aa0311 | 2016-01-14 23:01:31 | [diff] [blame] | 458 | |
Sam Sappenfield | 4a06526 | 2020-07-29 19:28:18 | [diff] [blame] | 459 | // Sends a registration ping for the CRX identified by |id| and |version|. |
| 460 | // The current implementation of this function only sends a best-effort, |
| 461 | // fire-and-forget ping. It has no other side effects regarding installs or |
| 462 | // updates done through an instance of this class. |
| 463 | virtual void SendRegistrationPing(const std::string& id, |
| 464 | const base::Version& version, |
| 465 | Callback callback) = 0; |
| 466 | |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 467 | // Returns status details about a CRX update. The function returns true in |
| 468 | // case of success and false in case of errors, such as |id| was |
| 469 | // invalid or not known. |
| 470 | virtual bool GetCrxUpdateState(const std::string& id, |
| 471 | CrxUpdateItem* update_item) const = 0; |
| 472 | |
sorin | 08d153c | 2015-10-30 00:04:20 | [diff] [blame] | 473 | // Returns true if the |id| is found in any running task. |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 474 | virtual bool IsUpdating(const std::string& id) const = 0; |
| 475 | |
sorin | ecaad3e | 2015-11-13 19:15:52 | [diff] [blame] | 476 | // Cancels the queued updates and makes a best effort to stop updates in |
| 477 | // progress as soon as possible. Some updates may not be stopped, in which |
| 478 | // case, the updates will run to completion. Calling this function has no |
| 479 | // effect if updates are not currently executed or queued up. |
| 480 | virtual void Stop() = 0; |
| 481 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 482 | protected: |
Sorin Jianu | 569aa719 | 2020-07-23 16:09:54 | [diff] [blame] | 483 | friend class base::RefCountedThreadSafe<UpdateClient>; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 484 | |
Sorin Jianu | 3088115 | 2020-03-16 14:31:19 | [diff] [blame] | 485 | virtual ~UpdateClient() = default; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 486 | }; |
| 487 | |
| 488 | // Creates an instance of the update client. |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 489 | scoped_refptr<UpdateClient> UpdateClientFactory( |
Sorin Jianu | 4912633 | 2018-02-13 17:07:42 | [diff] [blame] | 490 | scoped_refptr<Configurator> config); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 491 | |
waffles | d2d9a33 | 2016-04-09 01:59:57 | [diff] [blame] | 492 | // This must be called prior to the construction of any Configurator that |
| 493 | // contains a PrefService. |
| 494 | void RegisterPrefs(PrefRegistrySimple* registry); |
| 495 | |
Minh X. Nguyen | 3aa4069 | 2018-03-28 01:15:59 | [diff] [blame] | 496 | // This must be called prior to the construction of any Configurator that |
| 497 | // needs access to local user profiles. |
| 498 | // This function is mostly used for ExtensionUpdater, which requires update |
| 499 | // info from user profiles. |
| 500 | void RegisterProfilePrefs(PrefRegistrySimple* registry); |
| 501 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 502 | } // namespace update_client |
| 503 | |
| 504 | #endif // COMPONENTS_UPDATE_CLIENT_UPDATE_CLIENT_H_ |