Add abhijeet.k@samsung.com to AUTHORS list.
[chromium-blink-merge.git] / components / component_updater / component_updater_service.h
blobac60a7f8e87b2dc5833a272adf6998ed8c0de722
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_
8 #include <stdint.h>
9 #include <string>
10 #include <vector>
12 #include "base/callback_forward.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/version.h"
16 #include "url/gurl.h"
18 class ComponentsUI;
19 class SupervisedUserWhitelistService;
21 namespace base {
22 class DictionaryValue;
23 class FilePath;
24 class SequencedTaskRunner;
27 namespace content {
28 class ResourceThrottle;
31 namespace net {
32 class URLRequestContextGetter;
33 class URLRequest;
36 namespace update_client {
37 class ComponentInstaller;
38 class Configurator;
39 struct CrxComponent;
40 struct CrxUpdateItem;
43 namespace component_updater {
45 class OnDemandUpdater;
47 // The component update service is in charge of installing or upgrading
48 // select parts of chrome. Each part is called a component and managed by
49 // instances of CrxComponent registered using RegisterComponent(). On the
50 // server, each component is packaged as a CRX which is the same format used
51 // to package extensions. To the update service each component is identified
52 // by its public key hash (CrxComponent::pk_hash). If there is an update
53 // available and its version is bigger than (CrxComponent::version), it will
54 // be downloaded, verified and unpacked. Then component-specific installer
55 // ComponentInstaller::Install (of CrxComponent::installer) will be called.
57 // During the normal operation of the component updater some specific
58 // notifications are fired, like COMPONENT_UPDATER_STARTED and
59 // COMPONENT_UPDATE_FOUND. See notification_type.h for more details.
61 // All methods are safe to call ONLY from the browser's main thread.
62 class ComponentUpdateService {
63 public:
64 enum Status { kOk, kReplaced, kInProgress, kError };
66 // Defines an interface to observe ComponentUpdateService. It provides
67 // notifications when state changes occur for the service or for the
68 // registered components.
69 class Observer {
70 public:
71 enum Events {
72 // Sent when the component updater starts doing update checks.
73 COMPONENT_UPDATER_STARTED,
75 // Sent when the component updater is going to take a long nap.
76 COMPONENT_UPDATER_SLEEPING,
78 // Sent when there is a new version of a registered component. After
79 // the notification is sent the component will be downloaded.
80 COMPONENT_UPDATE_FOUND,
82 // Sent when the new component has been downloaded and an installation
83 // or upgrade is about to be attempted.
84 COMPONENT_UPDATE_READY,
86 // Sent when a component has been successfully updated.
87 COMPONENT_UPDATED,
89 // Sent when a component has not been updated following an update check:
90 // either there was no update available, or an update failed.
91 COMPONENT_NOT_UPDATED,
93 // Sent when component bytes are being downloaded.
94 COMPONENT_UPDATE_DOWNLOADING,
97 virtual ~Observer() {}
99 // The component updater service will call this function when an interesting
100 // state change happens. If the |id| is specified, then the event is fired
101 // on behalf of a specific component. The implementors of this interface are
102 // expected to filter the relevant events based on the component id.
103 virtual void OnEvent(Events event, const std::string& id) = 0;
106 // Adds an observer for this class. An observer should not be added more
107 // than once. The caller retains the ownership of the observer object.
108 virtual void AddObserver(Observer* observer) = 0;
110 // Removes an observer. It is safe for an observer to be removed while
111 // the observers are being notified.
112 virtual void RemoveObserver(Observer* observer) = 0;
114 // Start doing update checks and installing new versions of registered
115 // components after Configurator::InitialDelay() seconds.
116 virtual Status Start() = 0;
118 // Stop doing update checks. In-flight requests and pending installations
119 // will not be canceled.
120 virtual Status Stop() = 0;
122 // Add component to be checked for updates. You can call this method
123 // before calling Start().
124 virtual Status RegisterComponent(
125 const update_client::CrxComponent& component) = 0;
127 // Unregisters the component with the given ID. This means that the component
128 // is not going to be included in future update checks. If a download or
129 // update operation for the component is currently in progress, it will
130 // silently finish without triggering the next step.
131 // Note that the installer for the component is responsible for removing any
132 // existing versions of the component from disk.
133 virtual Status UnregisterComponent(const std::string& crx_id) = 0;
135 // Returns a list of registered components.
136 virtual std::vector<std::string> GetComponentIDs() const = 0;
138 // Returns an interface for on-demand updates. On-demand updates are
139 // proactively triggered outside the normal component update service schedule.
140 virtual OnDemandUpdater& GetOnDemandUpdater() = 0;
142 // This method is used to trigger an on-demand update for component |crx_id|.
143 // This can be used when loading a resource that depends on this component.
145 // |callback| is called on the main thread once the on-demand update is
146 // complete, regardless of success. |callback| may be called immediately
147 // within the method body.
149 // Additionally, this function implements an embedder-defined cooldown
150 // interval between on demand update attempts. This behavior is intended
151 // to be defensive against programming bugs, usually triggered by web fetches,
152 // where the on-demand functionality is invoked too often. If this function
153 // is called while still on cooldown, |callback| will be called immediately.
154 virtual void MaybeThrottle(const std::string& crx_id,
155 const base::Closure& callback) = 0;
157 // Returns a task runner suitable for use by component installers.
158 virtual scoped_refptr<base::SequencedTaskRunner> GetSequencedTaskRunner() = 0;
160 virtual ~ComponentUpdateService() {}
162 private:
163 // Returns details about registered component in the |item| parameter. The
164 // function returns true in case of success and false in case of errors.
165 virtual bool GetComponentDetails(
166 const std::string& component_id,
167 update_client::CrxUpdateItem* item) const = 0;
169 friend class ::ComponentsUI;
172 typedef ComponentUpdateService::Observer ServiceObserver;
174 class OnDemandUpdater {
175 public:
176 virtual ~OnDemandUpdater() {}
178 private:
179 friend class OnDemandTester;
180 friend class SupervisedUserWhitelistInstaller;
181 friend class ::ComponentsUI;
183 // Triggers an update check for a component. |component_id| is a value
184 // returned by GetCrxComponentID(). If an update for this component is already
185 // in progress, the function returns |kInProgress|. If an update is available,
186 // the update will be applied. The caller can subscribe to component update
187 // service notifications to get an indication about the outcome of the
188 // on-demand update. The function does not implement any cooldown interval.
189 virtual ComponentUpdateService::Status OnDemandUpdate(
190 const std::string& component_id) = 0;
193 // Creates the component updater. You must pass a valid |config| allocated on
194 // the heap which the component updater will own.
195 ComponentUpdateService* ComponentUpdateServiceFactory(
196 update_client::Configurator* config);
198 } // namespace component_updater
200 #endif // COMPONENTS_COMPONENT_UPDATER_COMPONENT_UPDATER_SERVICE_H_