Home | History | Annotate | Download | only in component_updater
      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_DEFAULT_COMPONENT_INSTALLER_H_
      6 #define COMPONENTS_COMPONENT_UPDATER_DEFAULT_COMPONENT_INSTALLER_H_
      7 
      8 #include <stdint.h>
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "base/compiler_specific.h"
     13 #include "base/macros.h"
     14 #include "base/memory/ref_counted.h"
     15 #include "base/memory/scoped_ptr.h"
     16 #include "base/threading/thread_checker.h"
     17 #include "base/values.h"
     18 #include "base/version.h"
     19 #include "components/component_updater/component_updater_service.h"
     20 
     21 namespace base {
     22 class FilePath;
     23 class SequencedTaskRunner;
     24 class SingleThreadTaskRunner;
     25 }  // namespace base
     26 
     27 namespace component_updater {
     28 
     29 // Components should use a DefaultComponentInstaller by defining a class that
     30 // implements the members of ComponentInstallerTraits, and then registering a
     31 // DefaultComponentInstaller that has been constructed with an instance of that
     32 // class.
     33 class ComponentInstallerTraits {
     34  public:
     35   virtual ~ComponentInstallerTraits();
     36 
     37   // Verifies that a working installation resides within the directory specified
     38   // by |dir|. |dir| is of the form <base directory>/<version>.
     39   // Called only from a thread belonging to a blocking thread pool.
     40   // The implementation of this function must be efficient since the function
     41   // can be called when Chrome starts.
     42   virtual bool VerifyInstallation(const base::FilePath& dir) const = 0;
     43 
     44   // Returns true if the component can be automatically updated. Called once
     45   // during component registration from the UI thread.
     46   virtual bool CanAutoUpdate() const = 0;
     47 
     48   // OnCustomInstall is called during the installation process. Components that
     49   // require custom installation operations should implement them here.
     50   // Returns false if a custom operation failed, and true otherwise.
     51   // Called only from a thread belonging to a blocking thread pool.
     52   virtual bool OnCustomInstall(const base::DictionaryValue& manifest,
     53                                const base::FilePath& install_dir) = 0;
     54 
     55   // ComponentReady is called in two cases:
     56   //   1) After an installation is successfully completed.
     57   //   2) During component registration if the component is already installed.
     58   // In both cases the install is verified before this is called. This method
     59   // is guaranteed to be called before any observers of the component are
     60   // notified of a successful install, and is meant to support follow-on work
     61   // such as updating paths elsewhere in Chrome. Called on the UI thread.
     62   // |version| is the version of the component.
     63   // |install_dir| is the path to the install directory for this version.
     64   // |manifest| is the manifest for this version of the component.
     65   virtual void ComponentReady(const base::Version& version,
     66                               const base::FilePath& install_dir,
     67                               scoped_ptr<base::DictionaryValue> manifest) = 0;
     68 
     69   // Returns the directory that the installer will place versioned installs of
     70   // the component into.
     71   virtual base::FilePath GetBaseDirectory() const = 0;
     72 
     73   // Returns the component's SHA2 hash as raw bytes.
     74   virtual void GetHash(std::vector<uint8_t>* hash) const = 0;
     75 
     76   // Returns the human-readable name of the component.
     77   virtual std::string GetName() const = 0;
     78 };
     79 
     80 // A DefaultComponentInstaller is intended to be final, and not derived from.
     81 // Customization must be provided by passing a ComponentInstallerTraits object
     82 // to the constructor.
     83 class DefaultComponentInstaller : public ComponentInstaller {
     84  public:
     85   DefaultComponentInstaller(
     86       scoped_ptr<ComponentInstallerTraits> installer_traits);
     87 
     88   // Registers the component for update checks and installs.
     89   void Register(ComponentUpdateService* cus);
     90 
     91   // Overridden from ComponentInstaller:
     92   virtual void OnUpdateError(int error) OVERRIDE;
     93   virtual bool Install(const base::DictionaryValue& manifest,
     94                        const base::FilePath& unpack_path) OVERRIDE;
     95   virtual bool GetInstalledFile(const std::string& file,
     96                                 base::FilePath* installed_file) OVERRIDE;
     97 
     98   virtual ~DefaultComponentInstaller();
     99 
    100  private:
    101   base::FilePath GetInstallDirectory();
    102   bool InstallHelper(const base::DictionaryValue& manifest,
    103                      const base::FilePath& unpack_path,
    104                      const base::FilePath& install_path);
    105   void StartRegistration(ComponentUpdateService* cus);
    106   void FinishRegistration(ComponentUpdateService* cus);
    107 
    108   base::Version current_version_;
    109   std::string current_fingerprint_;
    110   scoped_ptr<base::DictionaryValue> current_manifest_;
    111   scoped_ptr<ComponentInstallerTraits> installer_traits_;
    112   scoped_refptr<base::SequencedTaskRunner> task_runner_;
    113 
    114   // Used to post responses back to the main thread. Initialized on the main
    115   // loop but accessed from the task runner.
    116   scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
    117 
    118   base::ThreadChecker thread_checker_;
    119 
    120   DISALLOW_COPY_AND_ASSIGN(DefaultComponentInstaller);
    121 };
    122 
    123 }  // namespace component_updater
    124 
    125 #endif  // COMPONENTS_COMPONENT_UPDATER_DEFAULT_COMPONENT_INSTALLER_H_
    126