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

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

Issue 2479633003: Makes the component installers return a Result instead of a bool. (Closed)
Patch Set: rebase Created 4 years, 1 month 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 "components/component_updater/default_component_installer.h" 5 #include "components/component_updater/default_component_installer.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/bind_helpers.h" 10 #include "base/bind_helpers.h"
11 #include "base/files/file_enumerator.h" 11 #include "base/files/file_enumerator.h"
12 #include "base/files/file_path.h" 12 #include "base/files/file_path.h"
13 #include "base/files/file_util.h" 13 #include "base/files/file_util.h"
14 #include "base/location.h" 14 #include "base/location.h"
15 #include "base/path_service.h" 15 #include "base/path_service.h"
16 #include "base/sequenced_task_runner.h" 16 #include "base/sequenced_task_runner.h"
17 #include "base/single_thread_task_runner.h" 17 #include "base/single_thread_task_runner.h"
18 #include "base/threading/thread_task_runner_handle.h" 18 #include "base/threading/thread_task_runner_handle.h"
19 #include "base/values.h" 19 #include "base/values.h"
20 #include "base/version.h" 20 #include "base/version.h"
21 #include "components/component_updater/component_updater_paths.h" 21 #include "components/component_updater/component_updater_paths.h"
22 // TODO(ddorwin): Find a better place for ReadManifest. 22 // TODO(ddorwin): Find a better place for ReadManifest.
23 #include "components/component_updater/component_updater_service.h" 23 #include "components/component_updater/component_updater_service.h"
24 #include "components/update_client/component_unpacker.h" 24 #include "components/update_client/component_unpacker.h"
25 #include "components/update_client/update_client.h"
26 #include "components/update_client/update_client_errors.h"
25 #include "components/update_client/utils.h" 27 #include "components/update_client/utils.h"
26 28
27 using update_client::CrxComponent;
28
29 namespace component_updater { 29 namespace component_updater {
30 30
31 namespace { 31 namespace {
32 32
33 // Version "0" corresponds to no installed version. By the server's conventions, 33 // Version "0" corresponds to no installed version. By the server's conventions,
34 // we represent it as a dotted quad. 34 // we represent it as a dotted quad.
35 const char kNullVersion[] = "0.0.0.0"; 35 const char kNullVersion[] = "0.0.0.0";
36 36
37 using Result = update_client::CrxInstaller::Result;
38 using InstallError = update_client::InstallError;
39
37 } // namespace 40 } // namespace
38 41
39 ComponentInstallerTraits::~ComponentInstallerTraits() { 42 ComponentInstallerTraits::~ComponentInstallerTraits() {
40 } 43 }
41 44
42 DefaultComponentInstaller::DefaultComponentInstaller( 45 DefaultComponentInstaller::DefaultComponentInstaller(
43 std::unique_ptr<ComponentInstallerTraits> installer_traits) 46 std::unique_ptr<ComponentInstallerTraits> installer_traits)
44 : current_version_(kNullVersion), 47 : current_version_(kNullVersion),
45 main_task_runner_(base::ThreadTaskRunnerHandle::Get()) { 48 main_task_runner_(base::ThreadTaskRunnerHandle::Get()) {
46 installer_traits_ = std::move(installer_traits); 49 installer_traits_ = std::move(installer_traits);
(...skipping 18 matching lines...) Expand all
65 base::Bind(&DefaultComponentInstaller::StartRegistration, 68 base::Bind(&DefaultComponentInstaller::StartRegistration,
66 this, cus), 69 this, cus),
67 base::Bind(&DefaultComponentInstaller::FinishRegistration, 70 base::Bind(&DefaultComponentInstaller::FinishRegistration,
68 this, cus, callback)); 71 this, cus, callback));
69 } 72 }
70 73
71 void DefaultComponentInstaller::OnUpdateError(int error) { 74 void DefaultComponentInstaller::OnUpdateError(int error) {
72 LOG(ERROR) << "Component update error: " << error; 75 LOG(ERROR) << "Component update error: " << error;
73 } 76 }
74 77
75 bool DefaultComponentInstaller::InstallHelper( 78 Result DefaultComponentInstaller::InstallHelper(
76 const base::DictionaryValue& manifest, 79 const base::DictionaryValue& manifest,
77 const base::FilePath& unpack_path, 80 const base::FilePath& unpack_path,
78 const base::FilePath& install_path) { 81 const base::FilePath& install_path) {
79 VLOG(1) << "InstallHelper: unpack_path=" << unpack_path.AsUTF8Unsafe() 82 VLOG(1) << "InstallHelper: unpack_path=" << unpack_path.AsUTF8Unsafe()
80 << " install_path=" << install_path.AsUTF8Unsafe(); 83 << " install_path=" << install_path.AsUTF8Unsafe();
81 84
82 if (!base::Move(unpack_path, install_path)) { 85 if (!base::Move(unpack_path, install_path)) {
83 PLOG(ERROR) << "Move failed."; 86 PLOG(ERROR) << "Move failed.";
84 return false; 87 return Result(InstallError::GENERIC_ERROR);
85 } 88 }
86 if (!installer_traits_->OnCustomInstall(manifest, install_path)) { 89 const auto result =
90 installer_traits_->OnCustomInstall(manifest, install_path);
91 if (result.error) {
87 PLOG(ERROR) << "CustomInstall failed."; 92 PLOG(ERROR) << "CustomInstall failed.";
88 return false; 93 return result;
89 } 94 }
90 if (!installer_traits_->VerifyInstallation(manifest, install_path)) { 95 if (!installer_traits_->VerifyInstallation(manifest, install_path)) {
91 PLOG(ERROR) << "VerifyInstallation failed."; 96 PLOG(ERROR) << "VerifyInstallation failed.";
92 return false; 97 return Result(InstallError::GENERIC_ERROR);
93 } 98 }
94 99
95 return true; 100 return Result(InstallError::NONE);
96 } 101 }
97 102
98 bool DefaultComponentInstaller::Install(const base::DictionaryValue& manifest, 103 Result DefaultComponentInstaller::Install(const base::DictionaryValue& manifest,
99 const base::FilePath& unpack_path) { 104 const base::FilePath& unpack_path) {
100 std::string manifest_version; 105 std::string manifest_version;
101 manifest.GetStringASCII("version", &manifest_version); 106 manifest.GetStringASCII("version", &manifest_version);
102 base::Version version(manifest_version); 107 base::Version version(manifest_version);
103 108
104 VLOG(1) << "Install: version=" << version.GetString() 109 VLOG(1) << "Install: version=" << version.GetString()
105 << " current version=" << current_version_.GetString(); 110 << " current version=" << current_version_.GetString();
106 111
107 if (!version.IsValid()) 112 if (!version.IsValid())
108 return false; 113 return Result(InstallError::GENERIC_ERROR);
109 if (current_version_.CompareTo(version) > 0) 114 if (current_version_.CompareTo(version) > 0)
110 return false; 115 return Result(InstallError::GENERIC_ERROR);
111 base::FilePath install_path; 116 base::FilePath install_path;
112 if (!PathService::Get(DIR_COMPONENT_USER, &install_path)) 117 if (!PathService::Get(DIR_COMPONENT_USER, &install_path))
113 return false; 118 return Result(InstallError::GENERIC_ERROR);
114 install_path = install_path.Append(installer_traits_->GetRelativeInstallDir()) 119 install_path = install_path.Append(installer_traits_->GetRelativeInstallDir())
115 .AppendASCII(version.GetString()); 120 .AppendASCII(version.GetString());
116 if (base::PathExists(install_path)) { 121 if (base::PathExists(install_path)) {
117 if (!base::DeleteFile(install_path, true)) 122 if (!base::DeleteFile(install_path, true))
118 return false; 123 return Result(InstallError::GENERIC_ERROR);
119 } 124 }
120 if (!InstallHelper(manifest, unpack_path, install_path)) { 125 const auto result = InstallHelper(manifest, unpack_path, install_path);
126 if (result.error) {
121 base::DeleteFile(install_path, true); 127 base::DeleteFile(install_path, true);
122 return false; 128 return result;
123 } 129 }
124 current_version_ = version; 130 current_version_ = version;
125 current_install_dir_ = install_path; 131 current_install_dir_ = install_path;
126 // TODO(ddorwin): Change parameter to std::unique_ptr<base::DictionaryValue> 132 // TODO(ddorwin): Change parameter to std::unique_ptr<base::DictionaryValue>
127 // so we can avoid this DeepCopy. 133 // so we can avoid this DeepCopy.
128 current_manifest_.reset(manifest.DeepCopy()); 134 current_manifest_.reset(manifest.DeepCopy());
129 std::unique_ptr<base::DictionaryValue> manifest_copy( 135 std::unique_ptr<base::DictionaryValue> manifest_copy(
130 current_manifest_->DeepCopy()); 136 current_manifest_->DeepCopy());
131 main_task_runner_->PostTask( 137 main_task_runner_->PostTask(
132 FROM_HERE, 138 FROM_HERE,
133 base::Bind(&DefaultComponentInstaller::ComponentReady, 139 base::Bind(&DefaultComponentInstaller::ComponentReady,
134 this, base::Passed(&manifest_copy))); 140 this, base::Passed(&manifest_copy)));
135 return true; 141 return result;
136 } 142 }
137 143
138 bool DefaultComponentInstaller::GetInstalledFile( 144 bool DefaultComponentInstaller::GetInstalledFile(
139 const std::string& file, 145 const std::string& file,
140 base::FilePath* installed_file) { 146 base::FilePath* installed_file) {
141 if (current_version_ == base::Version(kNullVersion)) 147 if (current_version_ == base::Version(kNullVersion))
142 return false; // No component has been installed yet. 148 return false; // No component has been installed yet.
143 *installed_file = current_install_dir_.AppendASCII(file); 149 *installed_file = current_install_dir_.AppendASCII(file);
144 return true; 150 return true;
145 } 151 }
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 DLOG(ERROR) << "Couldn't delete " << base_dir.value(); 324 DLOG(ERROR) << "Couldn't delete " << base_dir.value();
319 } 325 }
320 } 326 }
321 327
322 void DefaultComponentInstaller::FinishRegistration( 328 void DefaultComponentInstaller::FinishRegistration(
323 ComponentUpdateService* cus, 329 ComponentUpdateService* cus,
324 const base::Closure& callback) { 330 const base::Closure& callback) {
325 VLOG(1) << __func__ << " for " << installer_traits_->GetName(); 331 VLOG(1) << __func__ << " for " << installer_traits_->GetName();
326 DCHECK(thread_checker_.CalledOnValidThread()); 332 DCHECK(thread_checker_.CalledOnValidThread());
327 333
328 CrxComponent crx; 334 update_client::CrxComponent crx;
329 installer_traits_->GetHash(&crx.pk_hash); 335 installer_traits_->GetHash(&crx.pk_hash);
330 crx.installer = this; 336 crx.installer = this;
331 crx.version = current_version_; 337 crx.version = current_version_;
332 crx.fingerprint = current_fingerprint_; 338 crx.fingerprint = current_fingerprint_;
333 crx.name = installer_traits_->GetName(); 339 crx.name = installer_traits_->GetName();
334 crx.installer_attributes = installer_traits_->GetInstallerAttributes(); 340 crx.installer_attributes = installer_traits_->GetInstallerAttributes();
335 crx.requires_network_encryption = 341 crx.requires_network_encryption =
336 installer_traits_->RequiresNetworkEncryption(); 342 installer_traits_->RequiresNetworkEncryption();
337 crx.handled_mime_types = installer_traits_->GetMimeTypes(); 343 crx.handled_mime_types = installer_traits_->GetMimeTypes();
338 crx.supports_group_policy_enable_component_updates = 344 crx.supports_group_policy_enable_component_updates =
(...skipping 20 matching lines...) Expand all
359 365
360 void DefaultComponentInstaller::ComponentReady( 366 void DefaultComponentInstaller::ComponentReady(
361 std::unique_ptr<base::DictionaryValue> manifest) { 367 std::unique_ptr<base::DictionaryValue> manifest) {
362 VLOG(1) << "Component ready, version " << current_version_.GetString() 368 VLOG(1) << "Component ready, version " << current_version_.GetString()
363 << " in " << current_install_dir_.value(); 369 << " in " << current_install_dir_.value();
364 installer_traits_->ComponentReady(current_version_, current_install_dir_, 370 installer_traits_->ComponentReady(current_version_, current_install_dir_,
365 std::move(manifest)); 371 std::move(manifest));
366 } 372 }
367 373
368 } // namespace component_updater 374 } // namespace component_updater
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698