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. 4 5 #ifndef COMPONENTS_COMPONENT_UPDATER_COMPONENT_UPDATER_SERVICE_H_ 6 #define COMPONENTS_COMPONENT_UPDATER_COMPONENT_UPDATER_SERVICE_H_ 7 8 #include <stdint.h> 9 #include <string> 10 #include <vector> 11 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" 17 18 class ComponentsUI; 19 20 namespace base { 21 class DictionaryValue; 22 class FilePath; 23 class SequencedTaskRunner; 24 } 25 26 namespace net { 27 class URLRequestContextGetter; 28 class URLRequest; 29 } 30 31 namespace content { 32 class ResourceThrottle; 33 } 34 35 namespace component_updater { 36 37 class Configurator; 38 class OnDemandUpdater; 39 40 // Component specific installers must derive from this class and implement 41 // OnUpdateError() and Install(). A valid instance of this class must be 42 // given to ComponentUpdateService::RegisterComponent(). 43 class ComponentInstaller { 44 public: 45 // Called by the component updater on the main thread when there was a 46 // problem unpacking or verifying the component. |error| is a non-zero 47 // value which is only meaningful to the component updater. 48 virtual void OnUpdateError(int error) = 0; 49 50 // Called by the component updater when a component has been unpacked 51 // and is ready to be installed. |manifest| contains the CRX manifest 52 // json dictionary and |unpack_path| contains the temporary directory 53 // with all the unpacked CRX files. This method may be called from 54 // a thread other than the main thread. 55 virtual bool Install(const base::DictionaryValue& manifest, 56 const base::FilePath& unpack_path) = 0; 57 58 // Set |installed_file| to the full path to the installed |file|. |file| is 59 // the filename of the file in this component's CRX. Returns false if this is 60 // not possible (the file has been removed or modified, or its current 61 // location is unknown). Otherwise, returns true. 62 virtual bool GetInstalledFile(const std::string& file, 63 base::FilePath* installed_file) = 0; 64 ~ComponentInstaller()65 virtual ~ComponentInstaller() {} 66 }; 67 68 // Describes a particular component that can be installed or updated. This 69 // structure is required to register a component with the component updater. 70 // |pk_hash| is the SHA256 hash of the component's public key. If the component 71 // is to be installed then version should be "0" or "0.0", else it should be 72 // the current version. |fingerprint|, and |name| are optional. 73 // |allow_background_download| specifies that the component can be background 74 // downloaded in some cases. The default for this value is |true| and the value 75 // can be overriden at the registration time. This is a temporary change until 76 // the issue 340448 is resolved. 77 struct CrxComponent { 78 std::vector<uint8_t> pk_hash; 79 ComponentInstaller* installer; 80 Version version; 81 std::string fingerprint; 82 std::string name; 83 bool allow_background_download; 84 CrxComponent(); 85 ~CrxComponent(); 86 }; 87 88 struct CrxUpdateItem; 89 90 // The component update service is in charge of installing or upgrading 91 // select parts of chrome. Each part is called a component and managed by 92 // instances of CrxComponent registered using RegisterComponent(). On the 93 // server, each component is packaged as a CRX which is the same format used 94 // to package extensions. To the update service each component is identified 95 // by its public key hash (CrxComponent::pk_hash). If there is an update 96 // available and its version is bigger than (CrxComponent::version), it will 97 // be downloaded, verified and unpacked. Then component-specific installer 98 // ComponentInstaller::Install (of CrxComponent::installer) will be called. 99 // 100 // During the normal operation of the component updater some specific 101 // notifications are fired, like COMPONENT_UPDATER_STARTED and 102 // COMPONENT_UPDATE_FOUND. See notification_type.h for more details. 103 // 104 // All methods are safe to call ONLY from the browser's main thread. 105 class ComponentUpdateService { 106 public: 107 enum Status { kOk, kReplaced, kInProgress, kError }; 108 109 // Defines an interface to observe ComponentUpdateService. It provides 110 // notifications when state changes occur for the service or for the 111 // registered components. 112 class Observer { 113 public: 114 enum Events { 115 // Sent when the component updater starts doing update checks. 116 COMPONENT_UPDATER_STARTED, 117 118 // Sent when the component updater is going to take a long nap. 119 COMPONENT_UPDATER_SLEEPING, 120 121 // Sent when there is a new version of a registered component. After 122 // the notification is sent the component will be downloaded. 123 COMPONENT_UPDATE_FOUND, 124 125 // Sent when the new component has been downloaded and an installation 126 // or upgrade is about to be attempted. 127 COMPONENT_UPDATE_READY, 128 129 // Sent when a component has been successfully updated. 130 COMPONENT_UPDATED, 131 132 // Sent when a component has not been updated following an update check: 133 // either there was no update available, or an update failed. 134 COMPONENT_NOT_UPDATED, 135 136 // Sent when component bytes are being downloaded. 137 COMPONENT_UPDATE_DOWNLOADING, 138 }; 139 ~Observer()140 virtual ~Observer() {} 141 142 // The component updater service will call this function when an interesting 143 // state change happens. If the |id| is specified, then the event is fired 144 // on behalf of a specific component. The implementors of this interface are 145 // expected to filter the relevant events based on the component id. 146 virtual void OnEvent(Events event, const std::string& id) = 0; 147 }; 148 149 // Adds an observer for this class. An observer should not be added more 150 // than once. The caller retains the ownership of the observer object. 151 virtual void AddObserver(Observer* observer) = 0; 152 153 // Removes an observer. It is safe for an observer to be removed while 154 // the observers are being notified. 155 virtual void RemoveObserver(Observer* observer) = 0; 156 157 // Start doing update checks and installing new versions of registered 158 // components after Configurator::InitialDelay() seconds. 159 virtual Status Start() = 0; 160 161 // Stop doing update checks. In-flight requests and pending installations 162 // will not be canceled. 163 virtual Status Stop() = 0; 164 165 // Add component to be checked for updates. You can call this method 166 // before calling Start(). 167 virtual Status RegisterComponent(const CrxComponent& component) = 0; 168 169 // Returns a list of registered components. 170 virtual std::vector<std::string> GetComponentIDs() const = 0; 171 172 // Returns an interface for on-demand updates. On-demand updates are 173 // proactively triggered outside the normal component update service schedule. 174 virtual OnDemandUpdater& GetOnDemandUpdater() = 0; 175 176 // This method is used to trigger an on-demand update for component |crx_id|. 177 // This can be used when loading a resource that depends on this component. 178 // 179 // |callback| is called on the main thread once the on-demand update is 180 // complete, regardless of success. |callback| may be called immediately 181 // within the method body. 182 // 183 // Additionally, this function implements an embedder-defined cooldown 184 // interval between on demand update attempts. This behavior is intended 185 // to be defensive against programming bugs, usually triggered by web fetches, 186 // where the on-demand functionality is invoked too often. If this function 187 // is called while still on cooldown, |callback| will be called immediately. 188 virtual void MaybeThrottle(const std::string& crx_id, 189 const base::Closure& callback) = 0; 190 191 // Returns a task runner suitable for use by component installers. 192 virtual scoped_refptr<base::SequencedTaskRunner> GetSequencedTaskRunner() = 0; 193 ~ComponentUpdateService()194 virtual ~ComponentUpdateService() {} 195 196 private: 197 // Returns details about registered component in the |item| parameter. The 198 // function returns true in case of success and false in case of errors. 199 virtual bool GetComponentDetails(const std::string& component_id, 200 CrxUpdateItem* item) const = 0; 201 202 friend class ::ComponentsUI; 203 }; 204 205 typedef ComponentUpdateService::Observer ServiceObserver; 206 207 class OnDemandUpdater { 208 public: ~OnDemandUpdater()209 virtual ~OnDemandUpdater() {} 210 211 private: 212 friend class OnDemandTester; 213 friend class ::ComponentsUI; 214 215 // Triggers an update check for a component. |component_id| is a value 216 // returned by GetCrxComponentID(). If an update for this component is already 217 // in progress, the function returns |kInProgress|. If an update is available, 218 // the update will be applied. The caller can subscribe to component update 219 // service notifications to get an indication about the outcome of the 220 // on-demand update. The function does not implement any cooldown interval. 221 virtual ComponentUpdateService::Status OnDemandUpdate( 222 const std::string& component_id) = 0; 223 }; 224 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(Configurator* config); 228 229 } // namespace component_updater 230 231 #endif // COMPONENTS_COMPONENT_UPDATER_COMPONENT_UPDATER_SERVICE_H_ 232