1 // Copyright 2012 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 CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_UPDATER_SERVICE_H_
6 #define CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_UPDATER_SERVICE_H_
11 #include "base/version.h"
17 class DictionaryValue
;
22 class URLRequestContextGetter
;
27 class ResourceThrottle
;
30 namespace component_updater
{
34 // Component specific installers must derive from this class and implement
35 // OnUpdateError() and Install(). A valid instance of this class must be
36 // given to ComponentUpdateService::RegisterComponent().
37 class ComponentInstaller
{
39 // Called by the component updater on the UI thread when there was a
40 // problem unpacking or verifying the component. |error| is a non-zero
41 // value which is only meaningful to the component updater.
42 virtual void OnUpdateError(int error
) = 0;
44 // Called by the component updater when a component has been unpacked
45 // and is ready to be installed. |manifest| contains the CRX manifest
46 // json dictionary and |unpack_path| contains the temporary directory
47 // with all the unpacked CRX files.
48 virtual bool Install(const base::DictionaryValue
& manifest
,
49 const base::FilePath
& unpack_path
) = 0;
51 // Set |installed_file| to the full path to the installed |file|. |file| is
52 // the filename of the file in this component's CRX. Returns false if this is
53 // not possible (the file has been removed or modified, or its current
54 // location is unknown). Otherwise, returns true.
55 virtual bool GetInstalledFile(const std::string
& file
,
56 base::FilePath
* installed_file
) = 0;
58 virtual ~ComponentInstaller() {}
61 // Describes a particular component that can be installed or updated. This
62 // structure is required to register a component with the component updater.
63 // |pk_hash| is the SHA256 hash of the component's public key. If the component
64 // is to be installed then version should be "0" or "0.0", else it should be
65 // the current version. |fingerprint|, and |name| are optional.
66 // |allow_background_download| specifies that the component can be background
67 // downloaded in some cases. The default for this value is |true| and the value
68 // can be overriden at the registration time. This is a temporary change until
69 // the issue 340448 is resolved.
71 std::vector
<uint8
> pk_hash
;
72 ComponentInstaller
* installer
;
74 std::string fingerprint
;
76 bool allow_background_download
;
81 // Convenience structure to use with component listing / enumeration.
82 struct CrxComponentInfo
{
83 // |id| is currently derived from |CrxComponent.pk_hash|, see rest of the
84 // class implementation for details.
92 // The component update service is in charge of installing or upgrading
93 // select parts of chrome. Each part is called a component and managed by
94 // instances of CrxComponent registered using RegisterComponent(). On the
95 // server, each component is packaged as a CRX which is the same format used
96 // to package extensions. To the update service each component is identified
97 // by its public key hash (CrxComponent::pk_hash). If there is an update
98 // available and its version is bigger than (CrxComponent::version), it will
99 // be downloaded, verified and unpacked. Then component-specific installer
100 // ComponentInstaller::Install (of CrxComponent::installer) will be called.
102 // During the normal operation of the component updater some specific
103 // notifications are fired, like COMPONENT_UPDATER_STARTED and
104 // COMPONENT_UPDATE_FOUND. See notification_type.h for more details.
106 // All methods are safe to call ONLY from chrome's UI thread.
107 class ComponentUpdateService
{
109 enum Status
{ kOk
, kReplaced
, kInProgress
, kError
};
110 // Controls the component updater behavior.
113 virtual ~Configurator() {}
114 // Delay in seconds from calling Start() to the first update check.
115 virtual int InitialDelay() = 0;
116 // Delay in seconds to every subsequent update check. 0 means don't check.
117 virtual int NextCheckDelay() = 0;
118 // Delay in seconds from each task step. Used to smooth out CPU/IO usage.
119 virtual int StepDelay() = 0;
120 // Delay in seconds between applying updates for different components, if
121 // several updates are available at a given time.
122 virtual int StepDelayMedium() = 0;
123 // Minimum delta time in seconds before checking again the same component.
124 virtual int MinimumReCheckWait() = 0;
125 // Minimum delta time in seconds before an on-demand check is allowed
126 // for the same component.
127 virtual int OnDemandDelay() = 0;
128 // The url that is going to be used update checks over Omaha protocol.
129 virtual GURL
UpdateUrl() = 0;
130 // The url where the completion pings are sent. Invalid if and only if
131 // pings are disabled.
132 virtual GURL
PingUrl() = 0;
133 // Parameters added to each url request. It can be null if none are needed.
134 virtual std::string
ExtraRequestParams() = 0;
135 // How big each update request can be. Don't go above 2000.
136 virtual size_t UrlSizeLimit() = 0;
137 // The source of contexts for all the url requests.
138 virtual net::URLRequestContextGetter
* RequestContext() = 0;
139 // True means that all ops are performed in this process.
140 virtual bool InProcess() = 0;
141 // True means that this client can handle delta updates.
142 virtual bool DeltasEnabled() const = 0;
143 // True means that the background downloader can be used for downloading
144 // non on-demand components.
145 virtual bool UseBackgroundDownloader() const = 0;
148 // Defines an interface to observe ComponentUpdateService. It provides
149 // notifications when state changes occur for the service or for the
150 // registered components.
154 // Sent when the component updater starts doing update checks.
155 COMPONENT_UPDATER_STARTED
,
157 // Sent when the component updater is going to take a long nap.
158 COMPONENT_UPDATER_SLEEPING
,
160 // Sent when there is a new version of a registered component. After
161 // the notification is sent the component will be downloaded.
162 COMPONENT_UPDATE_FOUND
,
164 // Sent when the new component has been downloaded and an installation
165 // or upgrade is about to be attempted.
166 COMPONENT_UPDATE_READY
,
168 // Sent when a component has been successfully updated.
171 // Sent when a component has not been updated following an update check:
172 // either there was no update available, or an update failed.
173 COMPONENT_NOT_UPDATED
,
175 // Sent when component bytes are being downloaded.
176 COMPONENT_UPDATE_DOWNLOADING
,
179 virtual ~Observer() {}
181 // The component updater service will call this function when an interesting
182 // state change happens. If the |id| is specified, then the event is fired
183 // on behalf of a specific component. The implementors of this interface are
184 // expected to filter the relevant events based on the component id.
185 virtual void OnEvent(Events event
, const std::string
& id
) = 0;
188 // Adds an observer for this class. An observer should not be added more
189 // than once. The caller retains the ownership of the observer object.
190 virtual void AddObserver(Observer
* observer
) = 0;
192 // Removes an observer. It is safe for an observer to be removed while
193 // the observers are being notified.
194 virtual void RemoveObserver(Observer
* observer
) = 0;
196 // Start doing update checks and installing new versions of registered
197 // components after Configurator::InitialDelay() seconds.
198 virtual Status
Start() = 0;
200 // Stop doing update checks. In-flight requests and pending installations
201 // will not be canceled.
202 virtual Status
Stop() = 0;
204 // Add component to be checked for updates. You can call this method
205 // before calling Start().
206 virtual Status
RegisterComponent(const CrxComponent
& component
) = 0;
208 // Returns a list of registered components.
209 virtual void GetComponents(std::vector
<CrxComponentInfo
>* components
) = 0;
211 // Returns a network resource throttle. It means that a component will be
212 // downloaded and installed before the resource is unthrottled. This is the
213 // only function callable from the IO thread.
214 virtual content::ResourceThrottle
* GetOnDemandResourceThrottle(
215 net::URLRequest
* request
,
216 const std::string
& crx_id
) = 0;
218 virtual ~ComponentUpdateService() {}
220 friend class ::ComponentsUI
;
221 friend class OnDemandTester
;
224 // Ask the component updater to do an update check for a previously
225 // registered component, immediately. If an update or check is already
226 // in progress, returns |kInProgress|.
227 // There is no guarantee that the item will actually be updated,
228 // since an update may not be available. Listeners for the component will
229 // know the outcome of the check.
230 virtual Status
OnDemandUpdate(const std::string
& component_id
) = 0;
233 typedef ComponentUpdateService::Observer ServiceObserver
;
235 // Creates the component updater. You must pass a valid |config| allocated on
236 // the heap which the component updater will own.
237 ComponentUpdateService
* ComponentUpdateServiceFactory(
238 ComponentUpdateService::Configurator
* config
);
240 } // namespace component_updater
242 #endif // CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_UPDATER_SERVICE_H_