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.
6 #include "base/bind_helpers.h"
7 #include "base/files/file_enumerator.h"
8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h"
10 #include "base/location.h"
11 #include "base/message_loop/message_loop_proxy.h"
12 #include "base/sequenced_task_runner.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/values.h"
15 #include "base/version.h"
16 // TODO(ddorwin): Find a better place for ReadManifest.
17 #include "components/component_updater/component_updater_service.h"
18 #include "components/component_updater/default_component_installer.h"
19 #include "components/update_client/component_unpacker.h"
20 #include "components/update_client/utils.h"
22 using update_client::CrxComponent
;
24 namespace component_updater
{
28 // Version "0" corresponds to no installed version. By the server's conventions,
29 // we represent it as a dotted quad.
30 const char kNullVersion
[] = "0.0.0.0";
34 ComponentInstallerTraits::~ComponentInstallerTraits() {
37 DefaultComponentInstaller::DefaultComponentInstaller(
38 scoped_ptr
<ComponentInstallerTraits
> installer_traits
)
39 : current_version_(kNullVersion
),
40 main_task_runner_(base::MessageLoopProxy::current()) {
41 installer_traits_
= installer_traits
.Pass();
44 DefaultComponentInstaller::~DefaultComponentInstaller() {
47 void DefaultComponentInstaller::Register(
48 ComponentUpdateService
* cus
,
49 const base::Closure
& callback
) {
50 DCHECK(thread_checker_
.CalledOnValidThread());
51 task_runner_
= cus
->GetSequencedTaskRunner();
53 if (!installer_traits_
) {
54 NOTREACHED() << "A DefaultComponentInstaller has been created but "
55 << "has no installer traits.";
58 task_runner_
->PostTaskAndReply(
60 base::Bind(&DefaultComponentInstaller::StartRegistration
,
62 base::Bind(&DefaultComponentInstaller::FinishRegistration
,
63 this, cus
, callback
));
66 void DefaultComponentInstaller::OnUpdateError(int error
) {
67 NOTREACHED() << "Component update error: " << error
;
70 bool DefaultComponentInstaller::InstallHelper(
71 const base::DictionaryValue
& manifest
,
72 const base::FilePath
& unpack_path
,
73 const base::FilePath
& install_path
) {
74 if (!base::Move(unpack_path
, install_path
))
76 if (!installer_traits_
->OnCustomInstall(manifest
, install_path
))
78 if (!installer_traits_
->VerifyInstallation(manifest
, install_path
))
83 bool DefaultComponentInstaller::Install(const base::DictionaryValue
& manifest
,
84 const base::FilePath
& unpack_path
) {
85 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
87 std::string manifest_version
;
88 manifest
.GetStringASCII("version", &manifest_version
);
89 base::Version
version(manifest_version
.c_str());
90 if (!version
.IsValid())
92 if (current_version_
.CompareTo(version
) > 0)
94 base::FilePath install_path
=
95 installer_traits_
->GetBaseDirectory().AppendASCII(version
.GetString());
96 if (base::PathExists(install_path
)) {
97 if (!base::DeleteFile(install_path
, true))
100 if (!InstallHelper(manifest
, unpack_path
, install_path
)) {
101 base::DeleteFile(install_path
, true);
104 current_version_
= version
;
105 // TODO(ddorwin): Change the parameter to scoped_ptr<base::DictionaryValue>
106 // so we can avoid this DeepCopy.
107 current_manifest_
.reset(manifest
.DeepCopy());
108 scoped_ptr
<base::DictionaryValue
> manifest_copy(
109 current_manifest_
->DeepCopy());
110 main_task_runner_
->PostTask(
112 base::Bind(&DefaultComponentInstaller::ComponentReady
,
113 this, base::Passed(&manifest_copy
)));
117 bool DefaultComponentInstaller::GetInstalledFile(
118 const std::string
& file
,
119 base::FilePath
* installed_file
) {
120 if (current_version_
.Equals(base::Version(kNullVersion
)))
121 return false; // No component has been installed yet.
123 *installed_file
= installer_traits_
->GetBaseDirectory()
124 .AppendASCII(current_version_
.GetString())
129 bool DefaultComponentInstaller::Uninstall() {
130 task_runner_
->PostTask(
132 base::Bind(&DefaultComponentInstaller::UninstallOnTaskRunner
, this));
136 void DefaultComponentInstaller::StartRegistration(ComponentUpdateService
* cus
) {
137 DCHECK(task_runner_
.get());
138 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
139 base::FilePath base_dir
= installer_traits_
->GetBaseDirectory();
140 if (!base::PathExists(base_dir
) && !base::CreateDirectory(base_dir
)) {
141 NOTREACHED() << "Could not create the base directory for "
142 << installer_traits_
->GetName() << " ("
143 << base_dir
.MaybeAsASCII() << ").";
147 base::FilePath latest_path
;
148 base::Version
latest_version(kNullVersion
);
149 scoped_ptr
<base::DictionaryValue
> latest_manifest
;
151 std::vector
<base::FilePath
> older_paths
;
152 base::FileEnumerator
file_enumerator(
153 base_dir
, false, base::FileEnumerator::DIRECTORIES
);
154 for (base::FilePath path
= file_enumerator
.Next();
155 !path
.value().empty();
156 path
= file_enumerator
.Next()) {
157 base::Version
version(path
.BaseName().MaybeAsASCII());
159 // Ignore folders that don't have valid version names. These folders are not
160 // managed by component installer so do not try to remove them.
161 if (!version
.IsValid())
164 // |version| not newer than the latest found version (kNullVersion if no
165 // version has been found yet) is marked for removal.
166 if (version
.CompareTo(latest_version
) <= 0) {
167 older_paths
.push_back(path
);
171 scoped_ptr
<base::DictionaryValue
> manifest
=
172 update_client::ReadManifest(path
);
173 if (!manifest
|| !installer_traits_
->VerifyInstallation(*manifest
, path
)) {
174 DLOG(ERROR
) << "Failed to read manifest or verify installation for "
175 << installer_traits_
->GetName() << " ("
176 << path
.MaybeAsASCII() << ").";
177 older_paths
.push_back(path
);
181 // New valid |version| folder found!
183 if (latest_manifest
) {
184 DCHECK(!latest_path
.empty());
185 older_paths
.push_back(latest_path
);
189 latest_version
= version
;
190 latest_manifest
= manifest
.Pass();
193 if (latest_manifest
) {
194 current_version_
= latest_version
;
195 current_manifest_
= latest_manifest
.Pass();
196 // TODO(ddorwin): Remove these members and pass them directly to
197 // FinishRegistration().
198 base::ReadFileToString(latest_path
.AppendASCII("manifest.fingerprint"),
199 ¤t_fingerprint_
);
202 // Remove older versions of the component. None should be in use during
204 for (const auto& older_path
: older_paths
)
205 base::DeleteFile(older_path
, true);
208 void DefaultComponentInstaller::UninstallOnTaskRunner() {
209 DCHECK(task_runner_
.get());
210 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
211 const base::FilePath base_dir
= installer_traits_
->GetBaseDirectory();
213 base::FileEnumerator
file_enumerator(base_dir
, false,
214 base::FileEnumerator::DIRECTORIES
);
215 for (base::FilePath path
= file_enumerator
.Next(); !path
.value().empty();
216 path
= file_enumerator
.Next()) {
217 base::Version
version(path
.BaseName().MaybeAsASCII());
219 // Ignore folders that don't have valid version names. These folders are not
220 // managed by the component installer, so do not try to remove them.
221 if (!version
.IsValid())
224 if (!base::DeleteFile(path
, true))
225 DLOG(ERROR
) << "Couldn't delete " << path
.value();
228 // Delete the base directory if it's empty now.
229 if (base::IsDirectoryEmpty(base_dir
)) {
230 if (base::DeleteFile(base_dir
, false))
231 DLOG(ERROR
) << "Couldn't delete " << base_dir
.value();
235 base::FilePath
DefaultComponentInstaller::GetInstallDirectory() {
236 return installer_traits_
->GetBaseDirectory()
237 .AppendASCII(current_version_
.GetString());
240 void DefaultComponentInstaller::FinishRegistration(
241 ComponentUpdateService
* cus
,
242 const base::Closure
& callback
) {
243 DCHECK(thread_checker_
.CalledOnValidThread());
244 if (installer_traits_
->CanAutoUpdate()) {
246 crx
.name
= installer_traits_
->GetName();
247 crx
.installer
= this;
248 crx
.version
= current_version_
;
249 crx
.fingerprint
= current_fingerprint_
;
250 installer_traits_
->GetHash(&crx
.pk_hash
);
251 ComponentUpdateService::Status status
= cus
->RegisterComponent(crx
);
252 if (status
!= ComponentUpdateService::kOk
&&
253 status
!= ComponentUpdateService::kReplaced
) {
254 NOTREACHED() << "Component registration failed for "
255 << installer_traits_
->GetName();
259 if (!callback
.is_null())
263 if (!current_manifest_
)
266 scoped_ptr
<base::DictionaryValue
> manifest_copy(
267 current_manifest_
->DeepCopy());
268 ComponentReady(manifest_copy
.Pass());
271 void DefaultComponentInstaller::ComponentReady(
272 scoped_ptr
<base::DictionaryValue
> manifest
) {
273 installer_traits_
->ComponentReady(
274 current_version_
, GetInstallDirectory(), manifest
.Pass());
277 } // namespace component_updater