Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / component_updater / component_updater_service.h
blob551167d2ebb489dd579dc4b72b0b5bc5c8484dbc
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_
8 #include <string>
9 #include <vector>
11 #include "base/version.h"
12 #include "url/gurl.h"
14 class ComponentsUI;
16 namespace base {
17 class DictionaryValue;
18 class FilePath;
21 namespace net {
22 class URLRequestContextGetter;
23 class URLRequest;
26 namespace component_updater {
27 class OnDemandTester;
30 namespace content {
31 class ResourceThrottle;
34 namespace component_updater {
36 class ComponentPatcher;
38 // Component specific installers must derive from this class and implement
39 // OnUpdateError() and Install(). A valid instance of this class must be
40 // given to ComponentUpdateService::RegisterComponent().
41 class ComponentInstaller {
42 public :
43 // Called by the component updater on the UI thread when there was a
44 // problem unpacking or verifying the component. |error| is a non-zero
45 // value which is only meaningful to the component updater.
46 virtual void OnUpdateError(int error) = 0;
48 // Called by the component updater when a component has been unpacked
49 // and is ready to be installed. |manifest| contains the CRX manifest
50 // json dictionary and |unpack_path| contains the temporary directory
51 // with all the unpacked CRX files.
52 virtual bool Install(const base::DictionaryValue& manifest,
53 const base::FilePath& unpack_path) = 0;
55 // Set |installed_file| to the full path to the installed |file|. |file| is
56 // the filename of the file in this component's CRX. Returns false if this is
57 // not possible (the file has been removed or modified, or its current
58 // location is unknown). Otherwise, returns true.
59 virtual bool GetInstalledFile(const std::string& file,
60 base::FilePath* installed_file) = 0;
62 virtual ~ComponentInstaller() {}
65 // Defines an interface to observe a CrxComponent.
66 class ComponentObserver {
67 public:
68 enum Events {
69 // Sent when the component updater starts doing update checks.
70 COMPONENT_UPDATER_STARTED,
72 // Sent when the component updater is going to take a long nap.
73 COMPONENT_UPDATER_SLEEPING,
75 // Sent when there is a new version of a registered component. After
76 // the notification is sent the component will be downloaded.
77 COMPONENT_UPDATE_FOUND,
79 // Sent when the new component has been downloaded and an installation
80 // or upgrade is about to be attempted.
81 COMPONENT_UPDATE_READY,
83 // Sent when a component has been successfully updated.
84 COMPONENT_UPDATED,
86 // Sent when a component has not been updated following an update check:
87 // either there was no update available, or an update failed.
88 COMPONENT_NOT_UPDATED,
91 virtual ~ComponentObserver() {}
93 // The component updater service will call this function when an interesting
94 // event happens for a specific component. |extra| is |event| dependent.
95 virtual void OnEvent(Events event, int extra) = 0;
98 // Describes a particular component that can be installed or updated. This
99 // structure is required to register a component with the component updater.
100 // |pk_hash| is the SHA256 hash of the component's public key. If the component
101 // is to be installed then version should be "0" or "0.0", else it should be
102 // the current version. |observer|, |fingerprint|, and |name| are optional.
103 struct CrxComponent {
104 std::vector<uint8> pk_hash;
105 ComponentInstaller* installer;
106 ComponentObserver* observer;
107 Version version;
108 std::string fingerprint;
109 std::string name;
110 CrxComponent();
111 ~CrxComponent();
114 // Convenience structure to use with component listing / enumeration.
115 struct CrxComponentInfo {
116 // |id| is currently derived from |CrxComponent.pk_hash|, see rest of the
117 // class implementation for details.
118 std::string id;
119 std::string version;
120 std::string name;
121 CrxComponentInfo();
122 ~CrxComponentInfo();
125 // The component update service is in charge of installing or upgrading
126 // select parts of chrome. Each part is called a component and managed by
127 // instances of CrxComponent registered using RegisterComponent(). On the
128 // server, each component is packaged as a CRX which is the same format used
129 // to package extensions. To the update service each component is identified
130 // by its public key hash (CrxComponent::pk_hash). If there is an update
131 // available and its version is bigger than (CrxComponent::version), it will
132 // be downloaded, verified and unpacked. Then component-specific installer
133 // ComponentInstaller::Install (of CrxComponent::installer) will be called.
135 // During the normal operation of the component updater some specific
136 // notifications are fired, like COMPONENT_UPDATER_STARTED and
137 // COMPONENT_UPDATE_FOUND. See notification_type.h for more details.
139 // All methods are safe to call ONLY from chrome's UI thread.
140 class ComponentUpdateService {
141 public:
142 enum Status {
143 kOk,
144 kReplaced,
145 kInProgress,
146 kError
148 // Controls the component updater behavior.
149 class Configurator {
150 public:
151 virtual ~Configurator() {}
152 // Delay in seconds from calling Start() to the first update check.
153 virtual int InitialDelay() = 0;
154 // Delay in seconds to every subsequent update check. 0 means don't check.
155 virtual int NextCheckDelay() = 0;
156 // Delay in seconds from each task step. Used to smooth out CPU/IO usage.
157 virtual int StepDelay() = 0;
158 // Delay in seconds between applying updates for different components, if
159 // several updates are available at a given time.
160 virtual int StepDelayMedium() = 0;
161 // Minimum delta time in seconds before checking again the same component.
162 virtual int MinimumReCheckWait() = 0;
163 // Minimum delta time in seconds before an on-demand check is allowed
164 // for the same component.
165 virtual int OnDemandDelay() = 0;
166 // The url that is going to be used update checks over Omaha protocol.
167 virtual GURL UpdateUrl() = 0;
168 // The url where the completion pings are sent. Invalid if and only if
169 // pings are disabled.
170 virtual GURL PingUrl() = 0;
171 // Parameters added to each url request. It can be null if none are needed.
172 virtual std::string ExtraRequestParams() = 0;
173 // How big each update request can be. Don't go above 2000.
174 virtual size_t UrlSizeLimit() = 0;
175 // The source of contexts for all the url requests.
176 virtual net::URLRequestContextGetter* RequestContext() = 0;
177 // True means that all ops are performed in this process.
178 virtual bool InProcess() = 0;
179 // Creates a new ComponentPatcher in a platform-specific way. This is useful
180 // for dependency injection.
181 virtual ComponentPatcher* CreateComponentPatcher() = 0;
182 // True means that this client can handle delta updates.
183 virtual bool DeltasEnabled() const = 0;
184 // True means that the background downloader can be used for downloading
185 // non on-demand components.
186 virtual bool UseBackgroundDownloader() const = 0;
189 // Start doing update checks and installing new versions of registered
190 // components after Configurator::InitialDelay() seconds.
191 virtual Status Start() = 0;
193 // Stop doing update checks. In-flight requests and pending installations
194 // will not be canceled.
195 virtual Status Stop() = 0;
197 // Add component to be checked for updates. You can call this method
198 // before calling Start().
199 virtual Status RegisterComponent(const CrxComponent& component) = 0;
201 // Returns a list of registered components.
202 virtual void GetComponents(std::vector<CrxComponentInfo>* components) = 0;
204 // Returns a network resource throttle. It means that a component will be
205 // downloaded and installed before the resource is unthrottled. This is the
206 // only function callable from the IO thread.
207 virtual content::ResourceThrottle* GetOnDemandResourceThrottle(
208 net::URLRequest* request, const std::string& crx_id) = 0;
210 virtual ~ComponentUpdateService() {}
212 friend class ::ComponentsUI;
213 friend class OnDemandTester;
215 private:
216 // Ask the component updater to do an update check for a previously
217 // registered component, immediately. If an update or check is already
218 // in progress, returns |kInProgress|.
219 // There is no guarantee that the item will actually be updated,
220 // since an update may not be available. Listeners for the component will
221 // know the outcome of the check.
222 virtual Status OnDemandUpdate(const std::string& component_id) = 0;
225 // Creates the component updater. You must pass a valid |config| allocated on
226 // the heap which the component updater will own.
227 ComponentUpdateService* ComponentUpdateServiceFactory(
228 ComponentUpdateService::Configurator* config);
230 } // namespace component_updater
232 #endif // CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_UPDATER_SERVICE_H_