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