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.
5 #ifndef COMPONENTS_COMPONENT_UPDATER_DEFAULT_COMPONENT_INSTALLER_H_
6 #define COMPONENTS_COMPONENT_UPDATER_DEFAULT_COMPONENT_INSTALLER_H_
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"
23 class SequencedTaskRunner
;
24 class SingleThreadTaskRunner
;
27 namespace component_updater
{
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
33 class ComponentInstallerTraits
{
35 virtual ~ComponentInstallerTraits();
37 // Verifies that a working installation resides within the directory specified
38 // by |install_dir|. |install_dir| is of the form <base directory>/<version>.
39 // |manifest| should have been read from the manifest file in |install_dir|.
40 // Called only from a thread belonging to a blocking thread pool.
41 // The implementation of this function must be efficient since the function
42 // can be called when Chrome starts.
43 virtual bool VerifyInstallation(const base::DictionaryValue
& manifest
,
44 const base::FilePath
& install_dir
) const = 0;
46 // Returns true if the component can be automatically updated. Called once
47 // during component registration from the UI thread.
48 virtual bool CanAutoUpdate() const = 0;
50 // OnCustomInstall is called during the installation process. Components that
51 // require custom installation operations should implement them here.
52 // Returns false if a custom operation failed, and true otherwise.
53 // Called only from a thread belonging to a blocking thread pool.
54 virtual bool OnCustomInstall(const base::DictionaryValue
& manifest
,
55 const base::FilePath
& install_dir
) = 0;
57 // ComponentReady is called in two cases:
58 // 1) After an installation is successfully completed.
59 // 2) During component registration if the component is already installed.
60 // In both cases the install is verified before this is called. This method
61 // is guaranteed to be called before any observers of the component are
62 // notified of a successful install, and is meant to support follow-on work
63 // such as updating paths elsewhere in Chrome. Called on the UI thread.
64 // |version| is the version of the component.
65 // |install_dir| is the path to the install directory for this version.
66 // |manifest| is the manifest for this version of the component.
67 virtual void ComponentReady(const base::Version
& version
,
68 const base::FilePath
& install_dir
,
69 scoped_ptr
<base::DictionaryValue
> manifest
) = 0;
71 // Returns the directory that the installer will place versioned installs of
72 // the component into.
73 virtual base::FilePath
GetBaseDirectory() const = 0;
75 // Returns the component's SHA2 hash as raw bytes.
76 virtual void GetHash(std::vector
<uint8_t>* hash
) const = 0;
78 // Returns the human-readable name of the component.
79 virtual std::string
GetName() const = 0;
82 // A DefaultComponentInstaller is intended to be final, and not derived from.
83 // Customization must be provided by passing a ComponentInstallerTraits object
84 // to the constructor.
85 class DefaultComponentInstaller
: public ComponentInstaller
{
87 DefaultComponentInstaller(
88 scoped_ptr
<ComponentInstallerTraits
> installer_traits
);
90 // Registers the component for update checks and installs.
91 void Register(ComponentUpdateService
* cus
);
93 // Overridden from ComponentInstaller:
94 void OnUpdateError(int error
) override
;
95 bool Install(const base::DictionaryValue
& manifest
,
96 const base::FilePath
& unpack_path
) override
;
97 bool GetInstalledFile(const std::string
& file
,
98 base::FilePath
* installed_file
) override
;
100 ~DefaultComponentInstaller() override
;
103 base::FilePath
GetInstallDirectory();
104 bool InstallHelper(const base::DictionaryValue
& manifest
,
105 const base::FilePath
& unpack_path
,
106 const base::FilePath
& install_path
);
107 void StartRegistration(ComponentUpdateService
* cus
);
108 void FinishRegistration(ComponentUpdateService
* cus
);
110 base::Version current_version_
;
111 std::string current_fingerprint_
;
112 scoped_ptr
<base::DictionaryValue
> current_manifest_
;
113 scoped_ptr
<ComponentInstallerTraits
> installer_traits_
;
114 scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
116 // Used to post responses back to the main thread. Initialized on the main
117 // loop but accessed from the task runner.
118 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner_
;
120 base::ThreadChecker thread_checker_
;
122 DISALLOW_COPY_AND_ASSIGN(DefaultComponentInstaller
);
125 } // namespace component_updater
127 #endif // COMPONENTS_COMPONENT_UPDATER_DEFAULT_COMPONENT_INSTALLER_H_