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 |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;
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;
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;
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;
69 // Returns the directory that the installer will place versioned installs of
70 // the component into.
71 virtual base::FilePath
GetBaseDirectory() const = 0;
73 // Returns the component's SHA2 hash as raw bytes.
74 virtual void GetHash(std::vector
<uint8_t>* hash
) const = 0;
76 // Returns the human-readable name of the component.
77 virtual std::string
GetName() const = 0;
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
{
85 DefaultComponentInstaller(
86 scoped_ptr
<ComponentInstallerTraits
> installer_traits
);
88 // Registers the component for update checks and installs.
89 void Register(ComponentUpdateService
* cus
);
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
;
98 virtual ~DefaultComponentInstaller();
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
);
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_
;
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_
;
118 base::ThreadChecker thread_checker_
;
120 DISALLOW_COPY_AND_ASSIGN(DefaultComponentInstaller
);
123 } // namespace component_updater
125 #endif // COMPONENTS_COMPONENT_UPDATER_DEFAULT_COMPONENT_INSTALLER_H_