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