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_COMPONENT_INSTALLER_H_
6 #define COMPONENTS_COMPONENT_UPDATER_COMPONENT_INSTALLER_H_
14 #include "base/callback_forward.h"
15 #include "base/files/file_path.h"
16 #include "base/macros.h"
17 #include "base/memory/ref_counted.h"
18 #include "base/threading/thread_checker.h"
19 #include "base/values.h"
20 #include "base/version.h"
21 #include "components/update_client/update_client.h"
24 class SequencedTaskRunner;
25 class SingleThreadTaskRunner;
28 namespace component_updater {
29 using RegisterCallback =
30 base::OnceCallback<bool(const update_client::CrxComponent&)>;
32 class ComponentUpdateService;
34 // Components should use a ComponentInstaller by defining a class that
35 // implements the members of ComponentInstallerPolicy, and then registering a
36 // ComponentInstaller that has been constructed with an instance of that
38 class ComponentInstallerPolicy {
40 virtual ~ComponentInstallerPolicy();
42 // Verifies that a working installation resides within the directory specified
43 // by |install_dir|. |install_dir| is of the form <base directory>/<version>.
44 // |manifest| should have been read from the manifest file in |install_dir|.
45 // Called only from a thread belonging to a blocking thread pool.
46 // The implementation of this function must be efficient since the function
47 // can be called when Chrome starts.
48 virtual bool VerifyInstallation(const base::DictionaryValue& manifest,
49 const base::FilePath& install_dir) const = 0;
51 // Returns true if the component supports a group policy to enable updates.
52 // Called once during component registration from the UI thread.
53 virtual bool SupportsGroupPolicyEnabledComponentUpdates() const = 0;
55 // Returns true if the network communication related to this component
57 virtual bool RequiresNetworkEncryption() const = 0;
59 // OnCustomInstall is called during the installation process. Components that
60 // require custom installation operations should implement them here.
61 // Returns a failure result if a custom operation failed, and
62 // update_client::InstallError::NONE otherwise. Called only from a thread
63 // belonging to a blocking thread pool.
64 virtual update_client::CrxInstaller::Result OnCustomInstall(
65 const base::DictionaryValue& manifest,
66 const base::FilePath& install_dir) = 0;
68 // OnCustomUninstall is called during the unregister (uninstall) process.
69 // Components that require custom uninstallation operations should implement
71 // Called only from a thread belonging to a blocking thread pool.
72 virtual void OnCustomUninstall() = 0;
74 // ComponentReady is called in two cases:
75 // 1) After an installation is successfully completed.
76 // 2) During component registration if the component is already installed.
77 // In both cases the install is verified before this is called. This method
78 // is guaranteed to be called before any observers of the component are
79 // notified of a successful install, and is meant to support follow-on work
80 // such as updating paths elsewhere in Chrome. Called on the UI thread.
81 // |version| is the version of the component.
82 // |install_dir| is the path to the install directory for this version.
83 // |manifest| is the manifest for this version of the component.
84 virtual void ComponentReady(
85 const base::Version& version,
86 const base::FilePath& install_dir,
87 std::unique_ptr<base::DictionaryValue> manifest) = 0;
89 // Returns a relative path that will be appended to the component updater
90 // root directories to find the data for this particular component.
91 virtual base::FilePath GetRelativeInstallDir() const = 0;
93 // Returns the component's SHA2 hash as raw bytes.
94 virtual void GetHash(std::vector<uint8_t>* hash) const = 0;
96 // Returns the human-readable name of the component.
97 virtual std::string GetName() const = 0;
99 // Returns a container of name-value pairs representing arbitrary,
100 // installer-defined metadata.
101 // The installer metadata may be used in the update checks for this component.
102 // A compatible server may use these attributes to negotiate special update
103 // rules when issuing an update response.
104 // Valid values for the name part of an attribute match
105 // ^[-_a-zA-Z0-9]{1,256}$ and valid values the value part of an attribute
106 // match ^[-.,;+_=$a-zA-Z0-9]{0,256}$ .
107 virtual update_client::InstallerAttributes GetInstallerAttributes() const = 0;
110 // Defines the installer for Chrome components. The behavior of this class is
111 // controlled by an instance of ComponentInstallerPolicy, at construction time.
112 class ComponentInstaller final : public update_client::CrxInstaller {
115 std::unique_ptr<ComponentInstallerPolicy> installer_policy,
116 scoped_refptr<update_client::ActionHandler> action_handler = nullptr);
118 // Registers the component for update checks and installs.
119 // |cus| provides the registration logic.
120 // The passed |callback| will be called once the initial check for installed
121 // versions is done and the component has been registered.
122 void Register(ComponentUpdateService* cus, base::OnceClosure callback);
124 // Registers the component for update checks and installs.
125 // |register_callback| is called to do the registration.
126 // |callback| is called when registration finishes.
127 void Register(RegisterCallback register_callback, base::OnceClosure callback);
129 // Overrides from update_client::CrxInstaller.
130 void OnUpdateError(int error) override;
132 void Install(const base::FilePath& unpack_path,
133 const std::string& public_key,
134 std::unique_ptr<InstallParams> install_params,
135 ProgressCallback progress_callback,
136 Callback callback) override;
138 bool GetInstalledFile(const std::string& file,
139 base::FilePath* installed_file) override;
140 // Only user-level component installations can be uninstalled.
141 bool Uninstall() override;
144 struct RegistrationInfo : base::RefCountedThreadSafe<RegistrationInfo> {
147 base::FilePath install_dir;
148 base::Version version;
149 std::string fingerprint;
150 std::unique_ptr<base::DictionaryValue> manifest;
153 friend class base::RefCountedThreadSafe<RegistrationInfo>;
157 DISALLOW_COPY_AND_ASSIGN(RegistrationInfo);
160 ~ComponentInstaller() override;
162 // If there is a installation of the component set up alongside Chrome's
163 // files (as opposed to in the user data directory), sets current_* to the
164 // values associated with that installation and returns true; otherwise,
166 bool FindPreinstallation(const base::FilePath& root,
167 scoped_refptr<RegistrationInfo> registration_info);
168 update_client::CrxInstaller::Result InstallHelper(
169 const base::FilePath& unpack_path,
170 base::Value* manifest,
171 base::Version* version,
172 base::FilePath* install_path);
173 void StartRegistration(scoped_refptr<RegistrationInfo> registration_info);
174 void FinishRegistration(scoped_refptr<RegistrationInfo> registration_info,
175 RegisterCallback register_callback,
176 base::OnceClosure callback);
177 void ComponentReady(std::unique_ptr<base::DictionaryValue> manifest);
178 void UninstallOnTaskRunner();
180 THREAD_CHECKER(thread_checker_);
182 base::FilePath current_install_dir_;
183 base::Version current_version_;
184 std::string current_fingerprint_;
186 std::unique_ptr<ComponentInstallerPolicy> installer_policy_;
187 scoped_refptr<update_client::ActionHandler> action_handler_;
188 scoped_refptr<base::SequencedTaskRunner> task_runner_;
190 // Posts responses back to the main thread.
191 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
193 DISALLOW_COPY_AND_ASSIGN(ComponentInstaller);
196 } // namespace component_updater
198 #endif // COMPONENTS_COMPONENT_UPDATER_COMPONENT_INSTALLER_H_