Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(125)

Side by Side Diff: components/component_updater/default_component_installer.cc

Issue 879993005: Add support for uninstalling components and use it in SupervisedUserWhitelistInstaller. (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: fix Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/bind.h" 5 #include "base/bind.h"
6 #include "base/bind_helpers.h" 6 #include "base/bind_helpers.h"
7 #include "base/files/file_enumerator.h" 7 #include "base/files/file_enumerator.h"
8 #include "base/files/file_path.h" 8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h" 9 #include "base/files/file_util.h"
10 #include "base/location.h" 10 #include "base/location.h"
11 #include "base/message_loop/message_loop_proxy.h" 11 #include "base/message_loop/message_loop_proxy.h"
12 #include "base/sequenced_task_runner.h" 12 #include "base/sequenced_task_runner.h"
13 #include "base/single_thread_task_runner.h" 13 #include "base/single_thread_task_runner.h"
14 #include "base/values.h" 14 #include "base/values.h"
15 #include "base/version.h" 15 #include "base/version.h"
16 // TODO(ddorwin): Find a better place for ReadManifest. 16 // TODO(ddorwin): Find a better place for ReadManifest.
17 #include "components/component_updater/component_updater_service.h" 17 #include "components/component_updater/component_updater_service.h"
18 #include "components/component_updater/default_component_installer.h" 18 #include "components/component_updater/default_component_installer.h"
19 #include "components/update_client/component_unpacker.h" 19 #include "components/update_client/component_unpacker.h"
20 #include "components/update_client/update_client.h" 20 #include "components/update_client/utils.h"
21 21
22 using update_client::CrxComponent; 22 using update_client::CrxComponent;
23 23
24 namespace component_updater { 24 namespace component_updater {
25 25
26 namespace { 26 namespace {
27
27 // Version "0" corresponds to no installed version. By the server's conventions, 28 // Version "0" corresponds to no installed version. By the server's conventions,
28 // we represent it as a dotted quad. 29 // we represent it as a dotted quad.
29 const char kNullVersion[] = "0.0.0.0"; 30 const char kNullVersion[] = "0.0.0.0";
31
30 } // namespace 32 } // namespace
31 33
32 ComponentInstallerTraits::~ComponentInstallerTraits() { 34 ComponentInstallerTraits::~ComponentInstallerTraits() {
33 } 35 }
34 36
35 DefaultComponentInstaller::DefaultComponentInstaller( 37 DefaultComponentInstaller::DefaultComponentInstaller(
36 scoped_ptr<ComponentInstallerTraits> installer_traits) 38 scoped_ptr<ComponentInstallerTraits> installer_traits)
37 : current_version_(kNullVersion), 39 : current_version_(kNullVersion),
38 main_task_runner_(base::MessageLoopProxy::current()) { 40 main_task_runner_(base::MessageLoopProxy::current()) {
39 installer_traits_ = installer_traits.Pass(); 41 installer_traits_ = installer_traits.Pass();
40 } 42 }
41 43
42 DefaultComponentInstaller::~DefaultComponentInstaller() { 44 DefaultComponentInstaller::~DefaultComponentInstaller() {
43 DCHECK(thread_checker_.CalledOnValidThread());
44 } 45 }
45 46
46 void DefaultComponentInstaller::Register(ComponentUpdateService* cus) { 47 void DefaultComponentInstaller::Register(ComponentUpdateService* cus) {
47 DCHECK(thread_checker_.CalledOnValidThread()); 48 DCHECK(thread_checker_.CalledOnValidThread());
48 task_runner_ = cus->GetSequencedTaskRunner(); 49 task_runner_ = cus->GetSequencedTaskRunner();
49 50
50 if (!installer_traits_) { 51 if (!installer_traits_) {
51 NOTREACHED() << "A DefaultComponentInstaller has been created but " 52 NOTREACHED() << "A DefaultComponentInstaller has been created but "
52 << "has no installer traits."; 53 << "has no installer traits.";
53 return; 54 return;
(...skipping 16 matching lines...) Expand all
70 return false; 71 return false;
71 if (!installer_traits_->OnCustomInstall(manifest, install_path)) 72 if (!installer_traits_->OnCustomInstall(manifest, install_path))
72 return false; 73 return false;
73 if (!installer_traits_->VerifyInstallation(manifest, install_path)) 74 if (!installer_traits_->VerifyInstallation(manifest, install_path))
74 return false; 75 return false;
75 return true; 76 return true;
76 } 77 }
77 78
78 bool DefaultComponentInstaller::Install(const base::DictionaryValue& manifest, 79 bool DefaultComponentInstaller::Install(const base::DictionaryValue& manifest,
79 const base::FilePath& unpack_path) { 80 const base::FilePath& unpack_path) {
81 DCHECK(task_runner_->RunsTasksOnCurrentThread());
82
80 std::string manifest_version; 83 std::string manifest_version;
81 manifest.GetStringASCII("version", &manifest_version); 84 manifest.GetStringASCII("version", &manifest_version);
82 base::Version version(manifest_version.c_str()); 85 base::Version version(manifest_version.c_str());
83 if (!version.IsValid()) 86 if (!version.IsValid())
84 return false; 87 return false;
85 if (current_version_.CompareTo(version) > 0) 88 if (current_version_.CompareTo(version) > 0)
86 return false; 89 return false;
87 base::FilePath install_path = 90 base::FilePath install_path =
88 installer_traits_->GetBaseDirectory().AppendASCII(version.GetString()); 91 installer_traits_->GetBaseDirectory().AppendASCII(version.GetString());
89 if (base::PathExists(install_path)) { 92 if (base::PathExists(install_path)) {
(...skipping 22 matching lines...) Expand all
112 base::FilePath* installed_file) { 115 base::FilePath* installed_file) {
113 if (current_version_.Equals(base::Version(kNullVersion))) 116 if (current_version_.Equals(base::Version(kNullVersion)))
114 return false; // No component has been installed yet. 117 return false; // No component has been installed yet.
115 118
116 *installed_file = installer_traits_->GetBaseDirectory() 119 *installed_file = installer_traits_->GetBaseDirectory()
117 .AppendASCII(current_version_.GetString()) 120 .AppendASCII(current_version_.GetString())
118 .AppendASCII(file); 121 .AppendASCII(file);
119 return true; 122 return true;
120 } 123 }
121 124
125 bool DefaultComponentInstaller::Uninstall() {
126 task_runner_->PostTask(
127 FROM_HERE,
128 base::Bind(&DefaultComponentInstaller::UninstallOnTaskRunner, this));
129 return true;
130 }
131
122 void DefaultComponentInstaller::StartRegistration(ComponentUpdateService* cus) { 132 void DefaultComponentInstaller::StartRegistration(ComponentUpdateService* cus) {
123 DCHECK(task_runner_.get()); 133 DCHECK(task_runner_.get());
124 DCHECK(task_runner_->RunsTasksOnCurrentThread()); 134 DCHECK(task_runner_->RunsTasksOnCurrentThread());
125 base::FilePath base_dir = installer_traits_->GetBaseDirectory(); 135 base::FilePath base_dir = installer_traits_->GetBaseDirectory();
126 if (!base::PathExists(base_dir) && !base::CreateDirectory(base_dir)) { 136 if (!base::PathExists(base_dir) && !base::CreateDirectory(base_dir)) {
127 NOTREACHED() << "Could not create the base directory for " 137 NOTREACHED() << "Could not create the base directory for "
128 << installer_traits_->GetName() << " (" 138 << installer_traits_->GetName() << " ("
129 << base_dir.MaybeAsASCII() << ")."; 139 << base_dir.MaybeAsASCII() << ").";
130 return; 140 return;
131 } 141 }
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 // browser startup. 199 // browser startup.
190 for (const auto& older_path : older_paths) 200 for (const auto& older_path : older_paths)
191 base::DeleteFile(older_path, true); 201 base::DeleteFile(older_path, true);
192 202
193 main_task_runner_->PostTask( 203 main_task_runner_->PostTask(
194 FROM_HERE, 204 FROM_HERE,
195 base::Bind(&DefaultComponentInstaller::FinishRegistration, 205 base::Bind(&DefaultComponentInstaller::FinishRegistration,
196 this, cus)); 206 this, cus));
197 } 207 }
198 208
209 void DefaultComponentInstaller::UninstallOnTaskRunner() {
210 DCHECK(task_runner_.get());
211 DCHECK(task_runner_->RunsTasksOnCurrentThread());
212 const base::FilePath base_dir = installer_traits_->GetBaseDirectory();
213
214 base::FileEnumerator file_enumerator(base_dir, false,
215 base::FileEnumerator::DIRECTORIES);
216 for (base::FilePath path = file_enumerator.Next(); !path.value().empty();
217 path = file_enumerator.Next()) {
218 base::Version version(path.BaseName().MaybeAsASCII());
219
220 // Ignore folders that don't have valid version names. These folders are not
221 // managed by the component installer, so do not try to remove them.
222 if (!version.IsValid())
223 continue;
224
225 if (!base::DeleteFile(path, true))
226 DLOG(ERROR) << "Couldn't delete " << path.value();
227 }
228
229 // Delete the base directory if it's empty now.
230 if (base::IsDirectoryEmpty(base_dir)) {
231 if (base::DeleteFile(base_dir, false))
232 DLOG(ERROR) << "Couldn't delete " << base_dir.value();
233 }
234 }
235
199 base::FilePath DefaultComponentInstaller::GetInstallDirectory() { 236 base::FilePath DefaultComponentInstaller::GetInstallDirectory() {
200 return installer_traits_->GetBaseDirectory() 237 return installer_traits_->GetBaseDirectory()
201 .AppendASCII(current_version_.GetString()); 238 .AppendASCII(current_version_.GetString());
202 } 239 }
203 240
204 void DefaultComponentInstaller::FinishRegistration( 241 void DefaultComponentInstaller::FinishRegistration(
205 ComponentUpdateService* cus) { 242 ComponentUpdateService* cus) {
206 DCHECK(thread_checker_.CalledOnValidThread()); 243 DCHECK(thread_checker_.CalledOnValidThread());
207 if (installer_traits_->CanAutoUpdate()) { 244 if (installer_traits_->CanAutoUpdate()) {
208 CrxComponent crx; 245 CrxComponent crx;
(...skipping 19 matching lines...) Expand all
228 ComponentReady(manifest_copy.Pass()); 265 ComponentReady(manifest_copy.Pass());
229 } 266 }
230 267
231 void DefaultComponentInstaller::ComponentReady( 268 void DefaultComponentInstaller::ComponentReady(
232 scoped_ptr<base::DictionaryValue> manifest) { 269 scoped_ptr<base::DictionaryValue> manifest) {
233 installer_traits_->ComponentReady( 270 installer_traits_->ComponentReady(
234 current_version_, GetInstallDirectory(), manifest.Pass()); 271 current_version_, GetInstallDirectory(), manifest.Pass());
235 } 272 }
236 273
237 } // namespace component_updater 274 } // namespace component_updater
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698