blob: d8d546a16a2f43cb12a05647196425273c16f97c [file] [log] [blame]
sorin52ac0882015-01-24 01:15:001// 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>
dchengd0fc6aa92016-04-22 18:03:129
sorin2adb2ca2016-06-29 01:44:3510#include <map>
dchengd0fc6aa92016-04-22 18:03:1211#include <memory>
sorin52ac0882015-01-24 01:15:0012#include <string>
13#include <vector>
14
sorin9797aba2015-04-17 17:15:0315#include "base/callback_forward.h"
bauerb810e60f42015-02-05 01:09:1016#include "base/memory/ref_counted.h"
sorin52ac0882015-01-24 01:15:0017#include "base/version.h"
sorin2892f7212016-11-07 18:59:4318#include "components/update_client/update_client_errors.h"
sorin52ac0882015-01-24 01:15:0019
sorin9797aba2015-04-17 17:15:0320// The UpdateClient class is a facade with a simple interface. The interface
21// exposes a few APIs to install a CRX or update a group of CRXs.
22//
23// The difference between a CRX install and a CRX update is relatively minor.
24// The terminology going forward will use the word "update" to cover both
25// install and update scenarios, except where details regarding the install
26// case are relevant.
27//
28// Handling an update consists of a series of actions such as sending an update
29// check to the server, followed by parsing the server response, identifying
30// the CRXs that require an update, downloading the differential update if
31// it is available, unpacking and patching the differential update, then
32// falling back to trying a similar set of actions using the full update.
33// At the end of this process, completion pings are sent to the server,
34// as needed, for the CRXs which had updates.
35//
36// As a general idea, this code handles the action steps needed to update
37// a group of components serially, one step at a time. However, concurrent
38// execution of calls to UpdateClient::Update is possible, therefore,
39// queuing of updates could happen in some cases. More below.
40//
41// The UpdateClient class features a subject-observer interface to observe
42// the CRX state changes during an update.
43//
44// The threading model for this code assumes that most of the code in the
45// public interface runs on a SingleThreadTaskRunner.
46// This task runner corresponds to the browser UI thread in many cases. There
47// are parts of the installer interface that run on blocking task runners, which
48// are usually threads in a thread pool.
49//
50// Using the UpdateClient is relatively easy. This assumes that the client
51// of this code has already implemented the observer interface as needed, and
52// can provide an installer, as described below.
53//
dchengd0fc6aa92016-04-22 18:03:1254// std::unique_ptr<UpdateClient> update_client(UpdateClientFactory(...));
sorin9797aba2015-04-17 17:15:0355// update_client->AddObserver(&observer);
56// std::vector<std::string> ids;
57// ids.push_back(...));
Sorin Jianua8ef73d2017-11-02 16:55:1758// update_client->Update(ids, base::BindOnce(...), base::BindOnce(...));
sorin9797aba2015-04-17 17:15:0359//
60// UpdateClient::Update takes two callbacks as parameters. First callback
61// allows the client of this code to provide an instance of CrxComponent
62// data structure that specifies additional parameters of the update.
63// CrxComponent has a CrxInstaller data member, which must be provided by the
64// callers of this class. The second callback indicates that this non-blocking
65// call has completed.
66//
67// There could be several ways of triggering updates for a CRX, user-initiated,
68// or timer-based. Since the execution of updates is concurrent, the parameters
69// for the update must be provided right before the update is handled.
70// Otherwise, the version of the CRX set in the CrxComponent may not be correct.
71//
72// The UpdateClient public interface includes two functions: Install and
73// Update. These functions correspond to installing one CRX immediately as a
74// foreground activity (Install), and updating a group of CRXs silently in the
75// background (Update). This distinction is important. Background updates are
76// queued up and their actions run serially, one at a time, for the purpose of
77// conserving local resources such as CPU, network, and I/O.
78// On the other hand, installs are never queued up but run concurrently, as
79// requested by the user.
80//
81// The update client introduces a runtime constraint regarding interleaving
82// updates and installs. If installs or updates for a given CRX are in progress,
83// then installs for the same CRX will fail with a specific error.
84//
85// Implementation details.
86//
87// The implementation details below are not relevant to callers of this
88// code. However, these design notes are relevant to the owners and maintainers
89// of this module.
90//
91// The design for the update client consists of a number of abstractions
92// such as: task, update engine, update context, and action.
93// The execution model for these abstractions is simple. They usually expose
94// a public, non-blocking Run function, and they invoke a callback when
95// the Run function has completed.
96//
97// A task is the unit of work for the UpdateClient. A task is associated
98// with a single call of the Update function. A task represents a group
99// of CRXs that are updated together.
100//
101// The UpdateClient is responsible for the queuing of tasks, if queuing is
102// needed.
103//
104// When the task runs, it calls the update engine to handle the updates for
105// the CRXs associated with the task. The UpdateEngine is the abstraction
106// responsible for breaking down the update in a set of discrete steps, which
107// are implemented as actions, and running the actions.
108//
109// The UpdateEngine maintains a set of UpdateContext instances. Each of
110// these instances maintains the update state for all the CRXs belonging to
111// a given task. The UpdateContext contains a queue of CRX ids.
112// The UpdateEngine will handle updates for the CRXs in the order they appear
113// in the queue, until the queue is empty.
114//
115// The update state for each CRX is maintained in a container of CrxUpdateItem*.
116// As actions run, each action updates the CRX state, represented by one of
117// these CrxUpdateItem* instances.
118//
119// Although the UpdateEngine can and will run update tasks concurrently, the
120// actions of a task are run sequentially.
121//
122// The Action is a polymorphic type. There is some code reuse for convenience,
123// implemented as a mixin. The polymorphic behavior of some of the actions
124// is achieved using a template method.
125//
126// State changes of a CRX could generate events, which are observed using a
127// subject-observer interface.
128//
129// The actions chain up. In some sense, the actions implement a state machine,
130// as the CRX undergoes a series of state transitions in the process of
131// being checked for updates and applying the update.
132
wafflesd2d9a332016-04-09 01:59:57133class PrefRegistrySimple;
sorin9797aba2015-04-17 17:15:03134
sorin52ac0882015-01-24 01:15:00135namespace base {
sorin52ac0882015-01-24 01:15:00136class FilePath;
137}
138
139namespace update_client {
140
sorin9797aba2015-04-17 17:15:03141class Configurator;
sorin7b8650522016-11-02 18:23:41142enum class Error;
sorin9797aba2015-04-17 17:15:03143struct CrxUpdateItem;
144
sorin30474f02017-04-27 00:45:48145enum class ComponentState {
146 kNew,
147 kChecking,
148 kCanUpdate,
149 kDownloadingDiff,
150 kDownloading,
151 kDownloaded,
152 kUpdatingDiff,
153 kUpdating,
154 kUpdated,
155 kUpToDate,
156 kUpdateError,
157 kUninstalled,
Sorin Jianu4ab7c292017-06-15 18:40:21158 kRun,
sorin30474f02017-04-27 00:45:48159 kLastStatus
160};
161
sorin9797aba2015-04-17 17:15:03162// Defines an interface for a generic CRX installer.
163class CrxInstaller : public base::RefCountedThreadSafe<CrxInstaller> {
sorin52ac0882015-01-24 01:15:00164 public:
sorin2892f7212016-11-07 18:59:43165 // Contains the result of the Install operation.
166 struct Result {
167 explicit Result(int error, int extended_error = 0)
168 : error(error), extended_error(extended_error) {}
169 explicit Result(InstallError error, int extended_error = 0)
170 : error(static_cast<int>(error)), extended_error(extended_error) {}
171 int error = 0; // 0 indicates that install has been successful.
172 int extended_error = 0;
173 };
174
Sorin Jianua8ef73d2017-11-02 16:55:17175 using Callback = base::OnceCallback<void(const Result& result)>;
Sorin Jianuf40ab4b32017-10-06 22:53:41176
sorin9797aba2015-04-17 17:15:03177 // Called on the main thread when there was a problem unpacking or
178 // verifying the CRX. |error| is a non-zero value which is only meaningful
179 // to the caller.
sorin52ac0882015-01-24 01:15:00180 virtual void OnUpdateError(int error) = 0;
181
sorin9797aba2015-04-17 17:15:03182 // Called by the update service when a CRX has been unpacked
Sorin Jianu7aa6d1f2017-10-13 20:29:29183 // and it is ready to be installed. |unpack_path| contains the
Sorin Jianuea5534e92017-10-27 01:40:28184 // temporary directory with all the unpacked CRX files. |pubkey| contains the
185 // public key of the CRX in the PEM format, without the header and the footer.
186 // The caller must invoke the |callback| when the install flow has completed.
sorin9797aba2015-04-17 17:15:03187 // This method may be called from a thread other than the main thread.
Sorin Jianu7aa6d1f2017-10-13 20:29:29188 virtual void Install(const base::FilePath& unpack_path,
Sorin Jianuea5534e92017-10-27 01:40:28189 const std::string& public_key,
Sorin Jianua8ef73d2017-11-02 16:55:17190 Callback callback) = 0;
sorin52ac0882015-01-24 01:15:00191
sorin9797aba2015-04-17 17:15:03192 // Sets |installed_file| to the full path to the installed |file|. |file| is
193 // the filename of the file in this CRX. Returns false if this is
sorin52ac0882015-01-24 01:15:00194 // not possible (the file has been removed or modified, or its current
sorin9797aba2015-04-17 17:15:03195 // location is unknown). Otherwise, it returns true.
sorin52ac0882015-01-24 01:15:00196 virtual bool GetInstalledFile(const std::string& file,
197 base::FilePath* installed_file) = 0;
198
sorin9797aba2015-04-17 17:15:03199 // Called when a CRX has been unregistered and all versions should
200 // be uninstalled from disk. Returns true if uninstallation is supported,
201 // and false otherwise.
bauerb1f6657e72015-02-09 00:00:27202 virtual bool Uninstall() = 0;
203
bauerb810e60f42015-02-05 01:09:10204 protected:
sorin9797aba2015-04-17 17:15:03205 friend class base::RefCountedThreadSafe<CrxInstaller>;
bauerb810e60f42015-02-05 01:09:10206
sorin9797aba2015-04-17 17:15:03207 virtual ~CrxInstaller() {}
sorin52ac0882015-01-24 01:15:00208};
209
sorin2adb2ca2016-06-29 01:44:35210// A dictionary of installer-specific, arbitrary name-value pairs, which
211// may be used in the update checks requests.
212using InstallerAttributes = std::map<std::string, std::string>;
213
sorin9797aba2015-04-17 17:15:03214// TODO(sorin): this structure will be refactored soon.
sorin52ac0882015-01-24 01:15:00215struct CrxComponent {
sorin52ac0882015-01-24 01:15:00216 CrxComponent();
vmpstrb6449d512016-02-25 23:55:40217 CrxComponent(const CrxComponent& other);
sorin52ac0882015-01-24 01:15:00218 ~CrxComponent();
sorin9797aba2015-04-17 17:15:03219
220 // SHA256 hash of the CRX's public key.
221 std::vector<uint8_t> pk_hash;
222 scoped_refptr<CrxInstaller> installer;
223
224 // The current version if the CRX is updated. Otherwise, "0" or "0.0" if
225 // the CRX is installed.
pwnalldb0b72412016-08-19 21:39:12226 base::Version version;
sorin9797aba2015-04-17 17:15:03227
228 std::string fingerprint; // Optional.
229 std::string name; // Optional.
waffles77255cc2016-08-02 17:25:12230 std::vector<std::string> handled_mime_types;
sorin2adb2ca2016-06-29 01:44:35231
232 // Optional.
233 // Valid values for the name part of an attribute match
234 // ^[-_a-zA-Z0-9]{1,256}$ and valid values the value part of an attribute
235 // match ^[-.,;+_=a-zA-Z0-9]{0,256}$ .
236 InstallerAttributes installer_attributes;
sorin9797aba2015-04-17 17:15:03237
238 // Specifies that the CRX can be background-downloaded in some cases.
sorinfccbf2d2016-04-04 20:34:34239 // The default for this value is |true|.
240 bool allows_background_download;
241
242 // Specifies that the update checks and pings associated with this component
243 // require confidentiality. The default for this value is |true|. As a side
244 // note, the confidentiality of the downloads is enforced by the server,
245 // which only returns secure download URLs in this case.
246 bool requires_network_encryption;
sorincb4e5e92016-08-02 21:48:40247
248 // True if the component allows enabling or disabling updates by group policy.
249 // This member should be set to |false| for data, non-binary components, such
250 // as CRLSet, Supervised User Whitelists, STH Set, Origin Trials, and File
251 // Type Policies.
252 bool supports_group_policy_enable_component_updates;
Minh X. Nguyenca599fb2017-09-14 21:10:39253
254 // Reasons why this component/extension is disabled.
255 std::vector<int> disabled_reasons;
Minh X. Nguyenf8c530aa2017-12-13 23:54:17256
257 // Information about where the component/extension was installed from.
258 // For extension, this information is set from the update service, which
259 // gets the install source from the update URL.
260 std::string install_source;
Minh X. Nguyenfc16497e2018-04-24 16:05:35261
262 // Information about where the component/extension was loaded from.
263 // For extensions, this information is inferred from the extension
264 // registry.
265 std::string install_location;
sorin52ac0882015-01-24 01:15:00266};
267
Sorin Jianuf40ab4b32017-10-06 22:53:41268// Called when a non-blocking call of UpdateClient completes.
Vladislav Kuzkokov12eca792017-10-20 12:45:38269using Callback = base::OnceCallback<void(Error error)>;
Sorin Jianuf40ab4b32017-10-06 22:53:41270
sorin7c717622015-05-26 19:59:09271// All methods are safe to call only from the browser's main thread. Once an
272// instance of this class is created, the reference to it must be released
273// only after the thread pools of the browser process have been destroyed and
274// the browser process has gone single-threaded.
275class UpdateClient : public base::RefCounted<UpdateClient> {
sorin9797aba2015-04-17 17:15:03276 public:
277 using CrxDataCallback =
Sorin Jianu7c22795b2018-04-26 22:16:52278 base::OnceCallback<std::vector<std::unique_ptr<CrxComponent>>(
279 const std::vector<std::string>& ids)>;
sorin9797aba2015-04-17 17:15:03280
281 // Defines an interface to observe the UpdateClient. It provides
282 // notifications when state changes occur for the service itself or for the
283 // registered CRXs.
284 class Observer {
285 public:
286 enum class Events {
287 // Sent before the update client does an update check.
sorin30474f02017-04-27 00:45:48288 COMPONENT_CHECKING_FOR_UPDATES = 1,
sorin9797aba2015-04-17 17:15:03289
290 // Sent when there is a new version of a registered CRX. After
291 // the notification is sent the CRX will be downloaded unless the
292 // update client inserts a
293 COMPONENT_UPDATE_FOUND,
294
295 // Sent when a CRX is in the update queue but it can't be acted on
296 // right away, because the update client spaces out CRX updates due to a
297 // throttling policy.
298 COMPONENT_WAIT,
299
300 // Sent after the new CRX has been downloaded but before the install
301 // or the upgrade is attempted.
302 COMPONENT_UPDATE_READY,
303
304 // Sent when a CRX has been successfully updated.
305 COMPONENT_UPDATED,
306
Sorin Jianucbb10e12018-01-23 18:01:44307 // Sent when a CRX has not been updated because there was no update
308 // available for this component.
sorin9797aba2015-04-17 17:15:03309 COMPONENT_NOT_UPDATED,
310
Sorin Jianucbb10e12018-01-23 18:01:44311 // Sent when an error ocurred during an update for any reason, including
312 // the update check itself failed, or the download of the update payload
313 // failed, or applying the update failed.
314 COMPONENT_UPDATE_ERROR,
315
sorin9797aba2015-04-17 17:15:03316 // Sent when CRX bytes are being downloaded.
317 COMPONENT_UPDATE_DOWNLOADING,
318 };
319
320 virtual ~Observer() {}
321
322 // Called by the update client when a state change happens.
323 // If an |id| is specified, then the event is fired on behalf of the
324 // specific CRX. The implementors of this interface are
325 // expected to filter the relevant events based on the id of the CRX.
326 virtual void OnEvent(Events event, const std::string& id) = 0;
327 };
328
329 // Adds an observer for this class. An observer should not be added more
330 // than once. The caller retains the ownership of the observer object.
331 virtual void AddObserver(Observer* observer) = 0;
332
333 // Removes an observer. It is safe for an observer to be removed while
334 // the observers are being notified.
335 virtual void RemoveObserver(Observer* observer) = 0;
336
sorin842703b2016-11-02 23:59:23337 // Installs the specified CRX. Calls back on |callback| after the
338 // update has been handled. The |error| parameter of the |callback|
sorin08d153c2015-10-30 00:04:20339 // contains an error code in the case of a run-time error, or 0 if the
340 // install has been handled successfully. Overlapping calls of this function
341 // are executed concurrently, as long as the id parameter is different,
342 // meaning that installs of different components are parallelized.
343 // The |Install| function is intended to be used for foreground installs of
344 // one CRX. These cases are usually associated with on-demand install
345 // scenarios, which are triggered by user actions. Installs are never
346 // queued up.
sorin9797aba2015-04-17 17:15:03347 virtual void Install(const std::string& id,
Sorin Jianua8ef73d2017-11-02 16:55:17348 CrxDataCallback crx_data_callback,
Vladislav Kuzkokov12eca792017-10-20 12:45:38349 Callback callback) = 0;
sorin9797aba2015-04-17 17:15:03350
351 // Updates the specified CRXs. Calls back on |crx_data_callback| before the
352 // update is attempted to give the caller the opportunity to provide the
sorin08d153c2015-10-30 00:04:20353 // instances of CrxComponent to be used for this update. The |Update| function
354 // is intended to be used for background updates of several CRXs. Overlapping
355 // calls to this function result in a queuing behavior, and the execution
356 // of each call is serialized. In addition, updates are always queued up when
Sorin Jianub41a592a2018-03-02 16:30:27357 // installs are running. The |is_foreground| parameter must be set to true if
358 // the invocation of this function is a result of a user initiated update.
sorin9797aba2015-04-17 17:15:03359 virtual void Update(const std::vector<std::string>& ids,
Sorin Jianua8ef73d2017-11-02 16:55:17360 CrxDataCallback crx_data_callback,
Sorin Jianub41a592a2018-03-02 16:30:27361 bool is_foreground,
Vladislav Kuzkokov12eca792017-10-20 12:45:38362 Callback callback) = 0;
sorin9797aba2015-04-17 17:15:03363
sorin805aa03112016-01-14 23:01:31364 // Sends an uninstall ping for the CRX identified by |id| and |version|. The
365 // |reason| parameter is defined by the caller. The current implementation of
366 // this function only sends a best-effort, fire-and-forget ping. It has no
367 // other side effects regarding installs or updates done through an instance
368 // of this class.
369 virtual void SendUninstallPing(const std::string& id,
pwnalldb0b72412016-08-19 21:39:12370 const base::Version& version,
sorin8037ac8c2017-04-19 16:28:00371 int reason,
Vladislav Kuzkokov12eca792017-10-20 12:45:38372 Callback callback) = 0;
sorin805aa03112016-01-14 23:01:31373
sorin9797aba2015-04-17 17:15:03374 // Returns status details about a CRX update. The function returns true in
375 // case of success and false in case of errors, such as |id| was
376 // invalid or not known.
377 virtual bool GetCrxUpdateState(const std::string& id,
378 CrxUpdateItem* update_item) const = 0;
379
sorin08d153c2015-10-30 00:04:20380 // Returns true if the |id| is found in any running task.
sorin9797aba2015-04-17 17:15:03381 virtual bool IsUpdating(const std::string& id) const = 0;
382
sorinecaad3e2015-11-13 19:15:52383 // Cancels the queued updates and makes a best effort to stop updates in
384 // progress as soon as possible. Some updates may not be stopped, in which
385 // case, the updates will run to completion. Calling this function has no
386 // effect if updates are not currently executed or queued up.
387 virtual void Stop() = 0;
388
sorin7c717622015-05-26 19:59:09389 protected:
390 friend class base::RefCounted<UpdateClient>;
391
sorin9797aba2015-04-17 17:15:03392 virtual ~UpdateClient() {}
393};
394
395// Creates an instance of the update client.
sorin7c717622015-05-26 19:59:09396scoped_refptr<UpdateClient> UpdateClientFactory(
Sorin Jianu49126332018-02-13 17:07:42397 scoped_refptr<Configurator> config);
sorin9797aba2015-04-17 17:15:03398
wafflesd2d9a332016-04-09 01:59:57399// This must be called prior to the construction of any Configurator that
400// contains a PrefService.
401void RegisterPrefs(PrefRegistrySimple* registry);
402
Minh X. Nguyen3aa40692018-03-28 01:15:59403// This must be called prior to the construction of any Configurator that
404// needs access to local user profiles.
405// This function is mostly used for ExtensionUpdater, which requires update
406// info from user profiles.
407void RegisterProfilePrefs(PrefRegistrySimple* registry);
408
sorin52ac0882015-01-24 01:15:00409} // namespace update_client
410
411#endif // COMPONENTS_UPDATE_CLIENT_UPDATE_CLIENT_H_