Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / component_updater / component_updater_service.h
index fa59340..40e48ed 100644 (file)
@@ -8,6 +8,9 @@
 #include <string>
 #include <vector>
 
+#include "base/callback_forward.h"
+#include "base/gtest_prod_util.h"
+#include "base/memory/ref_counted.h"
 #include "base/version.h"
 #include "url/gurl.h"
 
@@ -16,6 +19,7 @@ class ComponentsUI;
 namespace base {
 class DictionaryValue;
 class FilePath;
+class SequencedTaskRunner;
 }
 
 namespace net {
@@ -29,15 +33,15 @@ class ResourceThrottle;
 
 namespace component_updater {
 
-class OnDemandTester;
-class ComponentPatcher;
+class Configurator;
+class OnDemandUpdater;
 
 // Component specific installers must derive from this class and implement
 // OnUpdateError() and Install(). A valid instance of this class must be
 // given to ComponentUpdateService::RegisterComponent().
 class ComponentInstaller {
- public :
-  // Called by the component updater on the UI thread when there was a
+ public:
+  // Called by the component updater on the main thread when there was a
   // problem unpacking or verifying the component. |error| is a non-zero
   // value which is only meaningful to the component updater.
   virtual void OnUpdateError(int error) = 0;
@@ -45,7 +49,8 @@ class ComponentInstaller {
   // Called by the component updater when a component has been unpacked
   // and is ready to be installed. |manifest| contains the CRX manifest
   // json dictionary and |unpack_path| contains the temporary directory
-  // with all the unpacked CRX files.
+  // with all the unpacked CRX files. This method may be called from
+  // a thread other than the main thread.
   virtual bool Install(const base::DictionaryValue& manifest,
                        const base::FilePath& unpack_path) = 0;
 
@@ -59,44 +64,11 @@ class ComponentInstaller {
   virtual ~ComponentInstaller() {}
 };
 
-// Defines an interface to observe a CrxComponent.
-class ComponentObserver {
- public:
-  enum Events {
-    // Sent when the component updater starts doing update checks.
-    COMPONENT_UPDATER_STARTED,
-
-    // Sent when the component updater is going to take a long nap.
-    COMPONENT_UPDATER_SLEEPING,
-
-    // Sent when there is a new version of a registered component. After
-    // the notification is sent the component will be downloaded.
-    COMPONENT_UPDATE_FOUND,
-
-    // Sent when the new component has been downloaded and an installation
-    // or upgrade is about to be attempted.
-    COMPONENT_UPDATE_READY,
-
-    // Sent when a component has been successfully updated.
-    COMPONENT_UPDATED,
-
-    // Sent when a component has not been updated following an update check:
-    // either there was no update available, or an update failed.
-    COMPONENT_NOT_UPDATED,
-  };
-
-  virtual ~ComponentObserver() {}
-
-  // The component updater service will call this function when an interesting
-  // event happens for a specific component. |extra| is |event| dependent.
-  virtual void OnEvent(Events event, int extra) = 0;
-};
-
 // Describes a particular component that can be installed or updated. This
 // structure is required to register a component with the component updater.
 // |pk_hash| is the SHA256 hash of the component's public key. If the component
 // is to be installed then version should be "0" or "0.0", else it should be
-// the current version. |observer|, |fingerprint|, and |name| are optional.
+// the current version. |fingerprint|, and |name| are optional.
 // |allow_background_download| specifies that the component can be background
 // downloaded in some cases. The default for this value is |true| and the value
 // can be overriden at the registration time. This is a temporary change until
@@ -104,7 +76,6 @@ class ComponentObserver {
 struct CrxComponent {
   std::vector<uint8> pk_hash;
   ComponentInstaller* installer;
-  ComponentObserver* observer;
   Version version;
   std::string fingerprint;
   std::string name;
@@ -113,16 +84,7 @@ struct CrxComponent {
   ~CrxComponent();
 };
 
-// Convenience structure to use with component listing / enumeration.
-struct CrxComponentInfo {
-  // |id| is currently derived from |CrxComponent.pk_hash|, see rest of the
-  // class implementation for details.
-  std::string id;
-  std::string version;
-  std::string name;
-  CrxComponentInfo();
-  ~CrxComponentInfo();
-};
+struct CrxUpdateItem;
 
 // The component update service is in charge of installing or upgrading
 // select parts of chrome. Each part is called a component and managed by
@@ -138,56 +100,59 @@ struct CrxComponentInfo {
 // notifications are fired, like COMPONENT_UPDATER_STARTED and
 // COMPONENT_UPDATE_FOUND. See notification_type.h for more details.
 //
-// All methods are safe to call ONLY from chrome's UI thread.
+// All methods are safe to call ONLY from the browser's main thread.
 class ComponentUpdateService {
  public:
-  enum Status {
-    kOk,
-    kReplaced,
-    kInProgress,
-    kError
-  };
-  // Controls the component updater behavior.
-  class Configurator {
+  enum Status { kOk, kReplaced, kInProgress, kError };
+
+  // Defines an interface to observe ComponentUpdateService. It provides
+  // notifications when state changes occur for the service or for the
+  // registered components.
+  class Observer {
    public:
-    virtual ~Configurator() {}
-    // Delay in seconds from calling Start() to the first update check.
-    virtual int InitialDelay() = 0;
-    // Delay in seconds to every subsequent update check. 0 means don't check.
-    virtual int NextCheckDelay() = 0;
-    // Delay in seconds from each task step. Used to smooth out CPU/IO usage.
-    virtual int StepDelay() = 0;
-    // Delay in seconds between applying updates for different components, if
-    // several updates are available at a given time.
-    virtual int StepDelayMedium() = 0;
-    // Minimum delta time in seconds before checking again the same component.
-    virtual int MinimumReCheckWait() = 0;
-    // Minimum delta time in seconds before an on-demand check is allowed
-    // for the same component.
-    virtual int OnDemandDelay() = 0;
-    // The url that is going to be used update checks over Omaha protocol.
-    virtual GURL UpdateUrl() = 0;
-    // The url where the completion pings are sent. Invalid if and only if
-    // pings are disabled.
-    virtual GURL PingUrl() = 0;
-    // Parameters added to each url request. It can be null if none are needed.
-    virtual std::string ExtraRequestParams() = 0;
-    // How big each update request can be. Don't go above 2000.
-    virtual size_t UrlSizeLimit() = 0;
-    // The source of contexts for all the url requests.
-    virtual net::URLRequestContextGetter* RequestContext() = 0;
-    // True means that all ops are performed in this process.
-    virtual bool InProcess() = 0;
-    // Creates a new ComponentPatcher in a platform-specific way. This is useful
-    // for dependency injection.
-    virtual ComponentPatcher* CreateComponentPatcher() = 0;
-    // True means that this client can handle delta updates.
-    virtual bool DeltasEnabled() const = 0;
-    // True means that the background downloader can be used for downloading
-    // non on-demand components.
-    virtual bool UseBackgroundDownloader() const = 0;
+    enum Events {
+      // Sent when the component updater starts doing update checks.
+      COMPONENT_UPDATER_STARTED,
+
+      // Sent when the component updater is going to take a long nap.
+      COMPONENT_UPDATER_SLEEPING,
+
+      // Sent when there is a new version of a registered component. After
+      // the notification is sent the component will be downloaded.
+      COMPONENT_UPDATE_FOUND,
+
+      // Sent when the new component has been downloaded and an installation
+      // or upgrade is about to be attempted.
+      COMPONENT_UPDATE_READY,
+
+      // Sent when a component has been successfully updated.
+      COMPONENT_UPDATED,
+
+      // Sent when a component has not been updated following an update check:
+      // either there was no update available, or an update failed.
+      COMPONENT_NOT_UPDATED,
+
+      // Sent when component bytes are being downloaded.
+      COMPONENT_UPDATE_DOWNLOADING,
+    };
+
+    virtual ~Observer() {}
+
+    // The component updater service will call this function when an interesting
+    // state change happens. If the |id| is specified, then the event is fired
+    // on behalf of a specific component. The implementors of this interface are
+    // expected to filter the relevant events based on the component id.
+    virtual void OnEvent(Events event, const std::string& id) = 0;
   };
 
+  // Adds an observer for this class. An observer should not be added more
+  // than once. The caller retains the ownership of the observer object.
+  virtual void AddObserver(Observer* observer) = 0;
+
+  // Removes an observer. It is safe for an observer to be removed while
+  // the observers are being notified.
+  virtual void RemoveObserver(Observer* observer) = 0;
+
   // Start doing update checks and installing new versions of registered
   // components after Configurator::InitialDelay() seconds.
   virtual Status Start() = 0;
@@ -201,33 +166,64 @@ class ComponentUpdateService {
   virtual Status RegisterComponent(const CrxComponent& component) = 0;
 
   // Returns a list of registered components.
-  virtual void GetComponents(std::vector<CrxComponentInfo>* components) = 0;
-
-  // Returns a network resource throttle. It means that a component will be
-  // downloaded and installed before the resource is unthrottled. This is the
-  // only function callable from the IO thread.
-  virtual content::ResourceThrottle* GetOnDemandResourceThrottle(
-      net::URLRequest* request, const std::string& crx_id) = 0;
+  virtual std::vector<std::string> GetComponentIDs() const = 0;
+
+  // Returns an interface for on-demand updates. On-demand updates are
+  // proactively triggered outside the normal component update service schedule.
+  virtual OnDemandUpdater& GetOnDemandUpdater() = 0;
+
+  // This method is used to trigger an on-demand update for component |crx_id|.
+  // This can be used when loading a resource that depends on this component.
+  //
+  // |callback| is called on the main thread once the on-demand update is
+  // complete, regardless of success. |callback| may be called immediately
+  // within the method body.
+  //
+  // Additionally, this function implements an embedder-defined cooldown
+  // interval between on demand update attempts. This behavior is intended
+  // to be defensive against programming bugs, usually triggered by web fetches,
+  // where the on-demand functionality is invoked too often. If this function
+  // is called while still on cooldown, |callback| will be called immediately.
+  virtual void MaybeThrottle(const std::string& crx_id,
+                             const base::Closure& callback) = 0;
+
+  // Returns a task runner suitable for use by component installers.
+  virtual scoped_refptr<base::SequencedTaskRunner> GetSequencedTaskRunner() = 0;
 
   virtual ~ComponentUpdateService() {}
 
+ private:
+  // Returns details about registered component in the |item| parameter. The
+  // function returns true in case of success and false in case of errors.
+  virtual bool GetComponentDetails(const std::string& component_id,
+                                   CrxUpdateItem* item) const = 0;
+
   friend class ::ComponentsUI;
-  friend class OnDemandTester;
+};
+
+typedef ComponentUpdateService::Observer ServiceObserver;
+
+class OnDemandUpdater {
+ public:
+  virtual ~OnDemandUpdater() {}
 
  private:
-  // Ask the component updater to do an update check for a previously
-  // registered component, immediately. If an update or check is already
-  // in progress, returns |kInProgress|.
-  // There is no guarantee that the item will actually be updated,
-  // since an update may not be available. Listeners for the component will
-  // know the outcome of the check.
-  virtual Status OnDemandUpdate(const std::string& component_id) = 0;
+  friend class OnDemandTester;
+  friend class ::ComponentsUI;
+
+  // Triggers an update check for a component. |component_id| is a value
+  // returned by GetCrxComponentID(). If an update for this component is already
+  // in progress, the function returns |kInProgress|. If an update is available,
+  // the update will be applied. The caller can subscribe to component update
+  // service notifications to get an indication about the outcome of the
+  // on-demand update. The function does not implement any cooldown interval.
+  virtual ComponentUpdateService::Status OnDemandUpdate(
+      const std::string& component_id) = 0;
 };
 
 // Creates the component updater. You must pass a valid |config| allocated on
 // the heap which the component updater will own.
-ComponentUpdateService* ComponentUpdateServiceFactory(
-    ComponentUpdateService::Configurator* config);
+ComponentUpdateService* ComponentUpdateServiceFactory(Configurator* config);
 
 }  // namespace component_updater