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