1 // Copyright 2013 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/file_util.h"
7 #include "base/files/file_enumerator.h"
8 #include "base/files/file_path.h"
9 #include "base/values.h"
10 #include "base/version.h"
11 // TODO(ddorwin): Find a better place for ReadManifest.
12 #include "chrome/browser/component_updater/component_unpacker.h"
13 #include "chrome/browser/component_updater/default_component_installer.h"
14 #include "content/public/browser/browser_thread.h"
16 namespace component_updater
{
19 // Version "0" corresponds to no installed version. By the server's conventions,
20 // we represent it as a dotted quad.
21 const char kNullVersion
[] = "0.0.0.0";
24 ComponentInstallerTraits::~ComponentInstallerTraits() {
27 DefaultComponentInstaller::DefaultComponentInstaller(
28 scoped_ptr
<ComponentInstallerTraits
> installer_traits
)
29 : current_version_(kNullVersion
) {
30 installer_traits_
= installer_traits
.Pass();
33 DefaultComponentInstaller::~DefaultComponentInstaller() {
36 void DefaultComponentInstaller::Register(ComponentUpdateService
* cus
) {
37 if (!installer_traits_
) {
38 NOTREACHED() << "A DefaultComponentInstaller has been created but "
39 << "has no installer traits.";
42 content::BrowserThread::PostBlockingPoolTask(
44 base::Bind(&DefaultComponentInstaller::StartRegistration
,
45 base::Unretained(this),
49 void DefaultComponentInstaller::OnUpdateError(int error
) {
50 NOTREACHED() << "Component update error: " << error
;
53 bool DefaultComponentInstaller::InstallHelper(
54 const base::DictionaryValue
& manifest
,
55 const base::FilePath
& unpack_path
,
56 const base::FilePath
& install_path
) {
57 if (!base::Move(unpack_path
, install_path
))
59 if (!installer_traits_
->OnCustomInstall(manifest
, install_path
))
61 if (!installer_traits_
->VerifyInstallation(install_path
))
66 bool DefaultComponentInstaller::Install(const base::DictionaryValue
& manifest
,
67 const base::FilePath
& unpack_path
) {
68 std::string manifest_version
;
69 manifest
.GetStringASCII("version", &manifest_version
);
70 base::Version
version(manifest_version
.c_str());
71 if (!version
.IsValid())
73 if (current_version_
.CompareTo(version
) > 0)
75 base::FilePath install_path
=
76 installer_traits_
->GetBaseDirectory().AppendASCII(version
.GetString());
77 if (base::PathExists(install_path
)) {
78 if (!base::DeleteFile(install_path
, true))
81 if (!InstallHelper(manifest
, unpack_path
, install_path
)) {
82 base::DeleteFile(install_path
, true);
85 current_version_
= version
;
86 // TODO(ddorwin): Change the parameter to scoped_ptr<base::DictionaryValue>
87 // so we can avoid this DeepCopy.
88 current_manifest_
.reset(manifest
.DeepCopy());
89 scoped_ptr
<base::DictionaryValue
> manifest_copy(
90 current_manifest_
->DeepCopy());
91 content::BrowserThread::PostTask(
92 content::BrowserThread::UI
,
94 base::Bind(&ComponentInstallerTraits::ComponentReady
,
95 base::Unretained(installer_traits_
.get()),
97 GetInstallDirectory(),
98 base::Passed(&manifest_copy
)));
102 bool DefaultComponentInstaller::GetInstalledFile(
103 const std::string
& file
,
104 base::FilePath
* installed_file
) {
105 if (current_version_
.Equals(base::Version(kNullVersion
)))
106 return false; // No component has been installed yet.
108 *installed_file
= installer_traits_
->GetBaseDirectory()
109 .AppendASCII(current_version_
.GetString())
114 void DefaultComponentInstaller::StartRegistration(ComponentUpdateService
* cus
) {
115 base::FilePath base_dir
= installer_traits_
->GetBaseDirectory();
116 if (!base::PathExists(base_dir
) && !base::CreateDirectory(base_dir
)) {
117 NOTREACHED() << "Could not create the base directory for "
118 << installer_traits_
->GetName() << " ("
119 << base_dir
.MaybeAsASCII() << ").";
123 base::FilePath latest_dir
;
124 base::Version
latest_version(kNullVersion
);
125 std::vector
<base::FilePath
> older_dirs
;
127 base::FileEnumerator
file_enumerator(
128 base_dir
, false, base::FileEnumerator::DIRECTORIES
);
129 for (base::FilePath path
= file_enumerator
.Next();
130 !path
.value().empty();
131 path
= file_enumerator
.Next()) {
132 base::Version
version(path
.BaseName().MaybeAsASCII());
133 // Ignore folders that don't have valid version names. These folders are not
134 // managed by component installer so do not try to remove them.
135 if (!version
.IsValid())
137 if (!installer_traits_
->VerifyInstallation(path
)) {
138 older_dirs
.push_back(path
);
142 if (version
.CompareTo(latest_version
) > 0) {
143 older_dirs
.push_back(latest_dir
);
145 latest_version
= version
;
147 older_dirs
.push_back(path
);
151 latest_version
= version
;
157 current_version_
= latest_version
;
158 // TODO(ddorwin): Remove these members and pass them directly to
159 // FinishRegistration().
160 base::ReadFileToString(latest_dir
.AppendASCII("manifest.fingerprint"),
161 ¤t_fingerprint_
);
162 current_manifest_
= ReadManifest(latest_dir
);
163 if (!current_manifest_
) {
164 DLOG(ERROR
) << "Failed to read manifest for "
165 << installer_traits_
->GetName() << " ("
166 << base_dir
.MaybeAsASCII() << ").";
171 // Remove older versions of the component. None should be in use during
173 for (std::vector
<base::FilePath
>::iterator iter
= older_dirs
.begin();
174 iter
!= older_dirs
.end();
176 base::DeleteFile(*iter
, true);
179 content::BrowserThread::PostTask(
180 content::BrowserThread::UI
,
182 base::Bind(&DefaultComponentInstaller::FinishRegistration
,
183 base::Unretained(this),
187 base::FilePath
DefaultComponentInstaller::GetInstallDirectory() {
188 return installer_traits_
->GetBaseDirectory()
189 .AppendASCII(current_version_
.GetString());
192 void DefaultComponentInstaller::FinishRegistration(
193 ComponentUpdateService
* cus
) {
194 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
195 if (installer_traits_
->CanAutoUpdate()) {
197 crx
.name
= installer_traits_
->GetName();
198 crx
.installer
= this;
199 crx
.version
= current_version_
;
200 crx
.fingerprint
= current_fingerprint_
;
201 installer_traits_
->GetHash(&crx
.pk_hash
);
202 ComponentUpdateService::Status status
= cus
->RegisterComponent(crx
);
203 if (status
!= ComponentUpdateService::kOk
&&
204 status
!= ComponentUpdateService::kReplaced
) {
205 NOTREACHED() << "Component registration failed for "
206 << installer_traits_
->GetName();
211 if (current_version_
.CompareTo(base::Version(kNullVersion
)) > 0) {
212 scoped_ptr
<base::DictionaryValue
> manifest_copy(
213 current_manifest_
->DeepCopy());
214 installer_traits_
->ComponentReady(
215 current_version_
, GetInstallDirectory(), manifest_copy
.Pass());
219 } // namespace component_updater