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