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