[Sync] Add auto-generated ModelType in Java.
[chromium-blink-merge.git] / components / component_updater / default_component_installer.cc
blobe4eb99cba721b76e840e46f6437c4bc5ac05da16
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 #include "base/bind.h"
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/sequenced_task_runner.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/thread_task_runner_handle.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 {
26 namespace {
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";
32 } // namespace
34 ComponentInstallerTraits::~ComponentInstallerTraits() {
37 DefaultComponentInstaller::DefaultComponentInstaller(
38 scoped_ptr<ComponentInstallerTraits> installer_traits)
39 : current_version_(kNullVersion),
40 main_task_runner_(base::ThreadTaskRunnerHandle::Get()) {
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.";
56 return;
58 task_runner_->PostTaskAndReply(
59 FROM_HERE,
60 base::Bind(&DefaultComponentInstaller::StartRegistration,
61 this, cus),
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))
75 return false;
76 if (!installer_traits_->OnCustomInstall(manifest, install_path))
77 return false;
78 if (!installer_traits_->VerifyInstallation(manifest, install_path))
79 return false;
80 return true;
83 bool DefaultComponentInstaller::Install(const base::DictionaryValue& manifest,
84 const base::FilePath& unpack_path) {
85 std::string manifest_version;
86 manifest.GetStringASCII("version", &manifest_version);
87 base::Version version(manifest_version.c_str());
88 if (!version.IsValid())
89 return false;
90 if (current_version_.CompareTo(version) > 0)
91 return false;
92 base::FilePath install_path =
93 installer_traits_->GetBaseDirectory().AppendASCII(version.GetString());
94 if (base::PathExists(install_path)) {
95 if (!base::DeleteFile(install_path, true))
96 return false;
98 if (!InstallHelper(manifest, unpack_path, install_path)) {
99 base::DeleteFile(install_path, true);
100 return false;
102 current_version_ = version;
103 // TODO(ddorwin): Change the parameter to scoped_ptr<base::DictionaryValue>
104 // so we can avoid this DeepCopy.
105 current_manifest_.reset(manifest.DeepCopy());
106 scoped_ptr<base::DictionaryValue> manifest_copy(
107 current_manifest_->DeepCopy());
108 main_task_runner_->PostTask(
109 FROM_HERE,
110 base::Bind(&DefaultComponentInstaller::ComponentReady,
111 this, base::Passed(&manifest_copy)));
112 return true;
115 bool DefaultComponentInstaller::GetInstalledFile(
116 const std::string& file,
117 base::FilePath* installed_file) {
118 if (current_version_.Equals(base::Version(kNullVersion)))
119 return false; // No component has been installed yet.
121 *installed_file = installer_traits_->GetBaseDirectory()
122 .AppendASCII(current_version_.GetString())
123 .AppendASCII(file);
124 return true;
127 bool DefaultComponentInstaller::Uninstall() {
128 DCHECK(thread_checker_.CalledOnValidThread());
129 task_runner_->PostTask(
130 FROM_HERE,
131 base::Bind(&DefaultComponentInstaller::UninstallOnTaskRunner, this));
132 return true;
135 void DefaultComponentInstaller::StartRegistration(ComponentUpdateService* cus) {
136 DCHECK(task_runner_.get());
137 DCHECK(task_runner_->RunsTasksOnCurrentThread());
138 base::FilePath base_dir = installer_traits_->GetBaseDirectory();
139 if (!base::PathExists(base_dir) && !base::CreateDirectory(base_dir)) {
140 NOTREACHED() << "Could not create the base directory for "
141 << installer_traits_->GetName() << " ("
142 << base_dir.MaybeAsASCII() << ").";
143 return;
146 base::FilePath latest_path;
147 base::Version latest_version(kNullVersion);
148 scoped_ptr<base::DictionaryValue> latest_manifest;
150 std::vector<base::FilePath> older_paths;
151 base::FileEnumerator file_enumerator(
152 base_dir, false, base::FileEnumerator::DIRECTORIES);
153 for (base::FilePath path = file_enumerator.Next();
154 !path.value().empty();
155 path = file_enumerator.Next()) {
156 base::Version version(path.BaseName().MaybeAsASCII());
158 // Ignore folders that don't have valid version names. These folders are not
159 // managed by component installer so do not try to remove them.
160 if (!version.IsValid())
161 continue;
163 // |version| not newer than the latest found version (kNullVersion if no
164 // version has been found yet) is marked for removal.
165 if (version.CompareTo(latest_version) <= 0) {
166 older_paths.push_back(path);
167 continue;
170 scoped_ptr<base::DictionaryValue> manifest =
171 update_client::ReadManifest(path);
172 if (!manifest || !installer_traits_->VerifyInstallation(*manifest, path)) {
173 DLOG(ERROR) << "Failed to read manifest or verify installation for "
174 << installer_traits_->GetName() << " ("
175 << path.MaybeAsASCII() << ").";
176 older_paths.push_back(path);
177 continue;
180 // New valid |version| folder found!
182 if (latest_manifest) {
183 DCHECK(!latest_path.empty());
184 older_paths.push_back(latest_path);
187 latest_path = path;
188 latest_version = version;
189 latest_manifest = manifest.Pass();
192 if (latest_manifest) {
193 current_version_ = latest_version;
194 current_manifest_ = latest_manifest.Pass();
195 // TODO(ddorwin): Remove these members and pass them directly to
196 // FinishRegistration().
197 base::ReadFileToString(latest_path.AppendASCII("manifest.fingerprint"),
198 &current_fingerprint_);
201 // Remove older versions of the component. None should be in use during
202 // browser startup.
203 for (const auto& older_path : older_paths)
204 base::DeleteFile(older_path, true);
207 void DefaultComponentInstaller::UninstallOnTaskRunner() {
208 DCHECK(task_runner_.get());
209 DCHECK(task_runner_->RunsTasksOnCurrentThread());
210 const base::FilePath base_dir = installer_traits_->GetBaseDirectory();
212 base::FileEnumerator file_enumerator(base_dir, false,
213 base::FileEnumerator::DIRECTORIES);
214 for (base::FilePath path = file_enumerator.Next(); !path.value().empty();
215 path = file_enumerator.Next()) {
216 base::Version version(path.BaseName().MaybeAsASCII());
218 // Ignore folders that don't have valid version names. These folders are not
219 // managed by the component installer, so do not try to remove them.
220 if (!version.IsValid())
221 continue;
223 if (!base::DeleteFile(path, true))
224 DLOG(ERROR) << "Couldn't delete " << path.value();
227 // Delete the base directory if it's empty now.
228 if (base::IsDirectoryEmpty(base_dir)) {
229 if (base::DeleteFile(base_dir, false))
230 DLOG(ERROR) << "Couldn't delete " << base_dir.value();
234 base::FilePath DefaultComponentInstaller::GetInstallDirectory() {
235 return installer_traits_->GetBaseDirectory()
236 .AppendASCII(current_version_.GetString());
239 void DefaultComponentInstaller::FinishRegistration(
240 ComponentUpdateService* cus,
241 const base::Closure& callback) {
242 DCHECK(thread_checker_.CalledOnValidThread());
243 if (installer_traits_->CanAutoUpdate()) {
244 CrxComponent crx;
245 crx.name = installer_traits_->GetName();
246 crx.installer = this;
247 crx.version = current_version_;
248 crx.fingerprint = current_fingerprint_;
249 installer_traits_->GetHash(&crx.pk_hash);
250 if (!cus->RegisterComponent(crx)) {
251 NOTREACHED() << "Component registration failed for "
252 << installer_traits_->GetName();
253 return;
256 if (!callback.is_null())
257 callback.Run();
260 if (!current_manifest_)
261 return;
263 scoped_ptr<base::DictionaryValue> manifest_copy(
264 current_manifest_->DeepCopy());
265 ComponentReady(manifest_copy.Pass());
268 void DefaultComponentInstaller::ComponentReady(
269 scoped_ptr<base::DictionaryValue> manifest) {
270 installer_traits_->ComponentReady(
271 current_version_, GetInstallDirectory(), manifest.Pass());
274 } // namespace component_updater