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/update_client.h"
22 using update_client::CrxComponent
;
24 namespace component_updater
{
27 // Version "0" corresponds to no installed version. By the server's conventions,
28 // we represent it as a dotted quad.
29 const char kNullVersion
[] = "0.0.0.0";
32 ComponentInstallerTraits::~ComponentInstallerTraits() {
35 DefaultComponentInstaller::DefaultComponentInstaller(
36 scoped_ptr
<ComponentInstallerTraits
> installer_traits
)
37 : current_version_(kNullVersion
),
38 main_task_runner_(base::MessageLoopProxy::current()) {
39 installer_traits_
= installer_traits
.Pass();
42 DefaultComponentInstaller::~DefaultComponentInstaller() {
43 DCHECK(thread_checker_
.CalledOnValidThread());
46 void DefaultComponentInstaller::Register(ComponentUpdateService
* cus
) {
47 DCHECK(thread_checker_
.CalledOnValidThread());
48 task_runner_
= cus
->GetSequencedTaskRunner();
50 if (!installer_traits_
) {
51 NOTREACHED() << "A DefaultComponentInstaller has been created but "
52 << "has no installer traits.";
55 task_runner_
->PostTask(
57 base::Bind(&DefaultComponentInstaller::StartRegistration
,
58 base::Unretained(this),
62 void DefaultComponentInstaller::OnUpdateError(int error
) {
63 NOTREACHED() << "Component update error: " << error
;
66 bool DefaultComponentInstaller::InstallHelper(
67 const base::DictionaryValue
& manifest
,
68 const base::FilePath
& unpack_path
,
69 const base::FilePath
& install_path
) {
70 if (!base::Move(unpack_path
, install_path
))
72 if (!installer_traits_
->OnCustomInstall(manifest
, install_path
))
74 if (!installer_traits_
->VerifyInstallation(manifest
, install_path
))
79 bool DefaultComponentInstaller::Install(const base::DictionaryValue
& manifest
,
80 const base::FilePath
& unpack_path
) {
81 std::string manifest_version
;
82 manifest
.GetStringASCII("version", &manifest_version
);
83 base::Version
version(manifest_version
.c_str());
84 if (!version
.IsValid())
86 if (current_version_
.CompareTo(version
) > 0)
88 base::FilePath install_path
=
89 installer_traits_
->GetBaseDirectory().AppendASCII(version
.GetString());
90 if (base::PathExists(install_path
)) {
91 if (!base::DeleteFile(install_path
, true))
94 if (!InstallHelper(manifest
, unpack_path
, install_path
)) {
95 base::DeleteFile(install_path
, true);
98 current_version_
= version
;
99 // TODO(ddorwin): Change the parameter to scoped_ptr<base::DictionaryValue>
100 // so we can avoid this DeepCopy.
101 current_manifest_
.reset(manifest
.DeepCopy());
102 scoped_ptr
<base::DictionaryValue
> manifest_copy(
103 current_manifest_
->DeepCopy());
104 main_task_runner_
->PostTask(
106 base::Bind(&ComponentInstallerTraits::ComponentReady
,
107 base::Unretained(installer_traits_
.get()),
109 GetInstallDirectory(),
110 base::Passed(&manifest_copy
)));
114 bool DefaultComponentInstaller::GetInstalledFile(
115 const std::string
& file
,
116 base::FilePath
* installed_file
) {
117 if (current_version_
.Equals(base::Version(kNullVersion
)))
118 return false; // No component has been installed yet.
120 *installed_file
= installer_traits_
->GetBaseDirectory()
121 .AppendASCII(current_version_
.GetString())
126 void DefaultComponentInstaller::StartRegistration(ComponentUpdateService
* cus
) {
127 DCHECK(task_runner_
.get());
128 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
129 base::FilePath base_dir
= installer_traits_
->GetBaseDirectory();
130 if (!base::PathExists(base_dir
) && !base::CreateDirectory(base_dir
)) {
131 NOTREACHED() << "Could not create the base directory for "
132 << installer_traits_
->GetName() << " ("
133 << base_dir
.MaybeAsASCII() << ").";
137 base::FilePath latest_path
;
138 base::Version
latest_version(kNullVersion
);
139 scoped_ptr
<base::DictionaryValue
> latest_manifest
;
141 std::vector
<base::FilePath
> older_paths
;
142 base::FileEnumerator
file_enumerator(
143 base_dir
, false, base::FileEnumerator::DIRECTORIES
);
144 for (base::FilePath path
= file_enumerator
.Next();
145 !path
.value().empty();
146 path
= file_enumerator
.Next()) {
147 base::Version
version(path
.BaseName().MaybeAsASCII());
149 // Ignore folders that don't have valid version names. These folders are not
150 // managed by component installer so do not try to remove them.
151 if (!version
.IsValid())
154 // |version| not newer than the latest found version (kNullVersion if no
155 // version has been found yet) is marked for removal.
156 if (version
.CompareTo(latest_version
) <= 0) {
157 older_paths
.push_back(path
);
161 scoped_ptr
<base::DictionaryValue
> manifest
=
162 update_client::ReadManifest(path
);
163 if (!manifest
|| !installer_traits_
->VerifyInstallation(*manifest
, path
)) {
164 DLOG(ERROR
) << "Failed to read manifest or verify installation for "
165 << installer_traits_
->GetName() << " ("
166 << path
.MaybeAsASCII() << ").";
167 older_paths
.push_back(path
);
171 // New valid |version| folder found!
173 if (latest_manifest
) {
174 DCHECK(!latest_path
.empty());
175 older_paths
.push_back(latest_path
);
179 latest_version
= version
;
180 latest_manifest
= manifest
.Pass();
183 if (latest_manifest
) {
184 current_version_
= latest_version
;
185 current_manifest_
= latest_manifest
.Pass();
186 // TODO(ddorwin): Remove these members and pass them directly to
187 // FinishRegistration().
188 base::ReadFileToString(latest_path
.AppendASCII("manifest.fingerprint"),
189 ¤t_fingerprint_
);
192 // Remove older versions of the component. None should be in use during
194 for (const auto& older_path
: older_paths
)
195 base::DeleteFile(older_path
, true);
197 main_task_runner_
->PostTask(
199 base::Bind(&DefaultComponentInstaller::FinishRegistration
,
200 base::Unretained(this),
204 base::FilePath
DefaultComponentInstaller::GetInstallDirectory() {
205 return installer_traits_
->GetBaseDirectory()
206 .AppendASCII(current_version_
.GetString());
209 void DefaultComponentInstaller::FinishRegistration(
210 ComponentUpdateService
* cus
) {
211 DCHECK(thread_checker_
.CalledOnValidThread());
212 if (installer_traits_
->CanAutoUpdate()) {
214 crx
.name
= installer_traits_
->GetName();
215 crx
.installer
= this;
216 crx
.version
= current_version_
;
217 crx
.fingerprint
= current_fingerprint_
;
218 installer_traits_
->GetHash(&crx
.pk_hash
);
219 ComponentUpdateService::Status status
= cus
->RegisterComponent(crx
);
220 if (status
!= ComponentUpdateService::kOk
&&
221 status
!= ComponentUpdateService::kReplaced
) {
222 NOTREACHED() << "Component registration failed for "
223 << installer_traits_
->GetName();
228 if (!current_manifest_
)
231 scoped_ptr
<base::DictionaryValue
> manifest_copy(
232 current_manifest_
->DeepCopy());
233 installer_traits_
->ComponentReady(
234 current_version_
, GetInstallDirectory(), manifest_copy
.Pass());
237 } // namespace component_updater