1 // Copyright 2014 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.
5 #ifndef COMPONENTS_COMPONENT_UPDATER_COMPONENT_UPDATER_SERVICE_H_
6 #define COMPONENTS_COMPONENT_UPDATER_COMPONENT_UPDATER_SERVICE_H_
11 #include "base/callback_forward.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/version.h"
20 class DictionaryValue
;
22 class SequencedTaskRunner
;
26 class URLRequestContextGetter
;
31 class ResourceThrottle
;
34 namespace component_updater
{
37 class OnDemandUpdater
;
39 // Component specific installers must derive from this class and implement
40 // OnUpdateError() and Install(). A valid instance of this class must be
41 // given to ComponentUpdateService::RegisterComponent().
42 class ComponentInstaller
{
44 // Called by the component updater on the main thread when there was a
45 // problem unpacking or verifying the component. |error| is a non-zero
46 // value which is only meaningful to the component updater.
47 virtual void OnUpdateError(int error
) = 0;
49 // Called by the component updater when a component has been unpacked
50 // and is ready to be installed. |manifest| contains the CRX manifest
51 // json dictionary and |unpack_path| contains the temporary directory
52 // with all the unpacked CRX files. This method may be called from
53 // a thread other than the main thread.
54 virtual bool Install(const base::DictionaryValue
& manifest
,
55 const base::FilePath
& unpack_path
) = 0;
57 // Set |installed_file| to the full path to the installed |file|. |file| is
58 // the filename of the file in this component's CRX. Returns false if this is
59 // not possible (the file has been removed or modified, or its current
60 // location is unknown). Otherwise, returns true.
61 virtual bool GetInstalledFile(const std::string
& file
,
62 base::FilePath
* installed_file
) = 0;
64 virtual ~ComponentInstaller() {}
67 // Describes a particular component that can be installed or updated. This
68 // structure is required to register a component with the component updater.
69 // |pk_hash| is the SHA256 hash of the component's public key. If the component
70 // is to be installed then version should be "0" or "0.0", else it should be
71 // the current version. |fingerprint|, and |name| are optional.
72 // |allow_background_download| specifies that the component can be background
73 // downloaded in some cases. The default for this value is |true| and the value
74 // can be overriden at the registration time. This is a temporary change until
75 // the issue 340448 is resolved.
77 std::vector
<uint8
> pk_hash
;
78 ComponentInstaller
* installer
;
80 std::string fingerprint
;
82 bool allow_background_download
;
89 // The component update service is in charge of installing or upgrading
90 // select parts of chrome. Each part is called a component and managed by
91 // instances of CrxComponent registered using RegisterComponent(). On the
92 // server, each component is packaged as a CRX which is the same format used
93 // to package extensions. To the update service each component is identified
94 // by its public key hash (CrxComponent::pk_hash). If there is an update
95 // available and its version is bigger than (CrxComponent::version), it will
96 // be downloaded, verified and unpacked. Then component-specific installer
97 // ComponentInstaller::Install (of CrxComponent::installer) will be called.
99 // During the normal operation of the component updater some specific
100 // notifications are fired, like COMPONENT_UPDATER_STARTED and
101 // COMPONENT_UPDATE_FOUND. See notification_type.h for more details.
103 // All methods are safe to call ONLY from the browser's main thread.
104 class ComponentUpdateService
{
106 enum Status
{ kOk
, kReplaced
, kInProgress
, kError
};
108 // Defines an interface to observe ComponentUpdateService. It provides
109 // notifications when state changes occur for the service or for the
110 // registered components.
114 // Sent when the component updater starts doing update checks.
115 COMPONENT_UPDATER_STARTED
,
117 // Sent when the component updater is going to take a long nap.
118 COMPONENT_UPDATER_SLEEPING
,
120 // Sent when there is a new version of a registered component. After
121 // the notification is sent the component will be downloaded.
122 COMPONENT_UPDATE_FOUND
,
124 // Sent when the new component has been downloaded and an installation
125 // or upgrade is about to be attempted.
126 COMPONENT_UPDATE_READY
,
128 // Sent when a component has been successfully updated.
131 // Sent when a component has not been updated following an update check:
132 // either there was no update available, or an update failed.
133 COMPONENT_NOT_UPDATED
,
135 // Sent when component bytes are being downloaded.
136 COMPONENT_UPDATE_DOWNLOADING
,
139 virtual ~Observer() {}
141 // The component updater service will call this function when an interesting
142 // state change happens. If the |id| is specified, then the event is fired
143 // on behalf of a specific component. The implementors of this interface are
144 // expected to filter the relevant events based on the component id.
145 virtual void OnEvent(Events event
, const std::string
& id
) = 0;
148 // Adds an observer for this class. An observer should not be added more
149 // than once. The caller retains the ownership of the observer object.
150 virtual void AddObserver(Observer
* observer
) = 0;
152 // Removes an observer. It is safe for an observer to be removed while
153 // the observers are being notified.
154 virtual void RemoveObserver(Observer
* observer
) = 0;
156 // Start doing update checks and installing new versions of registered
157 // components after Configurator::InitialDelay() seconds.
158 virtual Status
Start() = 0;
160 // Stop doing update checks. In-flight requests and pending installations
161 // will not be canceled.
162 virtual Status
Stop() = 0;
164 // Add component to be checked for updates. You can call this method
165 // before calling Start().
166 virtual Status
RegisterComponent(const CrxComponent
& component
) = 0;
168 // Returns a list of registered components.
169 virtual std::vector
<std::string
> GetComponentIDs() const = 0;
171 // Returns an interface for on-demand updates. On-demand updates are
172 // proactively triggered outside the normal component update service schedule.
173 virtual OnDemandUpdater
& GetOnDemandUpdater() = 0;
175 // This method is used to trigger an on-demand update for component |crx_id|.
176 // This can be used when loading a resource that depends on this component.
178 // |callback| is called on the main thread once the on-demand update is
179 // complete, regardless of success. |callback| may be called immediately
180 // within the method body.
182 // Additionally, this function implements an embedder-defined cooldown
183 // interval between on demand update attempts. This behavior is intended
184 // to be defensive against programming bugs, usually triggered by web fetches,
185 // where the on-demand functionality is invoked too often. If this function
186 // is called while still on cooldown, |callback| will be called immediately.
187 virtual void MaybeThrottle(const std::string
& crx_id
,
188 const base::Closure
& callback
) = 0;
190 // Returns a task runner suitable for use by component installers.
191 virtual scoped_refptr
<base::SequencedTaskRunner
> GetSequencedTaskRunner() = 0;
193 virtual ~ComponentUpdateService() {}
196 // Returns details about registered component in the |item| parameter. The
197 // function returns true in case of success and false in case of errors.
198 virtual bool GetComponentDetails(const std::string
& component_id
,
199 CrxUpdateItem
* item
) const = 0;
201 friend class ::ComponentsUI
;
204 typedef ComponentUpdateService::Observer ServiceObserver
;
206 class OnDemandUpdater
{
208 virtual ~OnDemandUpdater() {}
211 friend class OnDemandTester
;
212 friend class ::ComponentsUI
;
214 // Triggers an update check for a component. |component_id| is a value
215 // returned by GetCrxComponentID(). If an update for this component is already
216 // in progress, the function returns |kInProgress|. If an update is available,
217 // the update will be applied. The caller can subscribe to component update
218 // service notifications to get an indication about the outcome of the
219 // on-demand update. The function does not implement any cooldown interval.
220 virtual ComponentUpdateService::Status
OnDemandUpdate(
221 const std::string
& component_id
) = 0;
224 // Creates the component updater. You must pass a valid |config| allocated on
225 // the heap which the component updater will own.
226 ComponentUpdateService
* ComponentUpdateServiceFactory(Configurator
* config
);
228 } // namespace component_updater
230 #endif // COMPONENTS_COMPONENT_UPDATER_COMPONENT_UPDATER_SERVICE_H_