1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/crostini/termina_installer.h"
#include <algorithm>
#include <memory>
#include <string_view>
#include "base/barrier_closure.h"
#include "base/containers/contains.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "chrome/browser/ash/crostini/crostini_util.h"
#include "chrome/browser/ash/guest_os/guest_os_dlc_helper.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browser_process_platform_part_ash.h"
#include "components/component_updater/ash/component_manager_ash.h"
#include "content/public/browser/network_service_instance.h"
#include "services/network/public/cpp/network_connection_tracker.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
namespace crostini {
TerminaInstaller::TerminaInstaller() = default;
TerminaInstaller::~TerminaInstaller() = default;
void TerminaInstaller::CancelInstall() {
// TODO(b/277835995): Tests demand concurrent installations despite that they
// need to be mass cancelled here (which is probably unintended). Consider
// switching to CachedCallback or similar.
for (auto& installation : installations_) {
installation->CancelGracefully();
}
}
void TerminaInstaller::Install(
base::OnceCallback<void(InstallResult)> callback) {
// The Remove*IfPresent methods require an unowned UninstallResult pointer to
// record their success/failure state. This has to be unowned so that in
// Uninstall it can be accessed further down the callback chain, but here we
// don't care about it, so we assign ownership of the pointer to a
// base::DoNothing that will delete it once called.
auto ptr = std::make_unique<UninstallResult>();
auto* uninstall_result_ptr = ptr.get();
auto remove_callback = base::BindOnce(
[](std::unique_ptr<UninstallResult> ptr) {}, std::move(ptr));
RemoveComponentIfPresent(std::move(remove_callback), uninstall_result_ptr);
installations_.push_back(std::make_unique<guest_os::GuestOsDlcInstallation>(
kCrostiniDlcName,
base::BindOnce(&TerminaInstaller::OnInstallDlc,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)),
base::DoNothing()));
}
void TerminaInstaller::OnInstallDlc(
base::OnceCallback<void(InstallResult)> callback,
guest_os::GuestOsDlcInstallation::Result result) {
if (result.has_value()) {
dlc_id_ = kCrostiniDlcName;
termina_location_ = result.value();
}
InstallResult response =
result
.transform_error([](guest_os::GuestOsDlcInstallation::Error err) {
switch (err) {
case guest_os::GuestOsDlcInstallation::Error::Cancelled:
return InstallResult::Cancelled;
case guest_os::GuestOsDlcInstallation::Error::Offline:
LOG(ERROR)
<< "Failed to install termina-dlc while offline, assuming "
"network issue.";
return InstallResult::Offline;
case guest_os::GuestOsDlcInstallation::Error::NeedUpdate:
case guest_os::GuestOsDlcInstallation::Error::NeedReboot:
LOG(ERROR) << "Failed to install termina-dlc because the OS "
"must be updated";
return InstallResult::NeedUpdate;
case guest_os::GuestOsDlcInstallation::Error::DiskFull:
case guest_os::GuestOsDlcInstallation::Error::Busy:
case guest_os::GuestOsDlcInstallation::Error::Internal:
case guest_os::GuestOsDlcInstallation::Error::Invalid:
case guest_os::GuestOsDlcInstallation::Error::UnknownFailure:
LOG(ERROR) << "Failed to install termina-dlc: " << err;
return InstallResult::Failure;
}
})
.error_or(InstallResult::Success);
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), response));
}
void TerminaInstaller::Uninstall(base::OnceCallback<void(bool)> callback) {
// Unset |termina_location_| now since it will become invalid at some point
// soon.
termina_location_ = std::nullopt;
// This is really a vector of bool, but std::vector<bool> has weird properties
// that stop us from using it in this way.
std::vector<UninstallResult> partial_results{0, 0};
UninstallResult* component_result = &partial_results[0];
UninstallResult* dlc_result = &partial_results[1];
// We want to get the results from both uninstall calls and combine them, and
// the asynchronous nature of this process means we can't use return values,
// so we need to pass a pointer into those calls to store their results and
// pass ownership of that memory into the result callback.
auto b_closure = BarrierClosure(
2, base::BindOnce(&TerminaInstaller::OnUninstallFinished,
weak_ptr_factory_.GetWeakPtr(), std::move(callback),
std::move(partial_results)));
RemoveComponentIfPresent(b_closure, component_result);
RemoveDlcIfPresent(b_closure, dlc_result);
}
void TerminaInstaller::RemoveComponentIfPresent(
base::OnceCallback<void()> callback,
UninstallResult* result) {
VLOG(1) << "Removing component";
scoped_refptr<component_updater::ComponentManagerAsh> component_manager =
g_browser_process->platform_part()->component_manager_ash();
base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE, {base::MayBlock()},
base::BindOnce(
[](scoped_refptr<component_updater::ComponentManagerAsh>
component_manager) {
return component_manager->IsRegisteredMayBlock(
imageloader::kTerminaComponentName);
},
std::move(component_manager)),
base::BindOnce(
[](base::OnceCallback<void()> callback, UninstallResult* result,
bool is_present) {
scoped_refptr<component_updater::ComponentManagerAsh>
component_manager =
g_browser_process->platform_part()->component_manager_ash();
if (is_present) {
VLOG(1) << "Component present, unloading";
*result =
component_manager->Unload(imageloader::kTerminaComponentName);
if (!*result) {
LOG(ERROR) << "Failed to remove cros-termina component";
}
} else {
VLOG(1) << "No component present, skipping";
*result = true;
}
std::move(callback).Run();
},
std::move(callback), result));
}
void TerminaInstaller::RemoveDlcIfPresent(base::OnceCallback<void()> callback,
UninstallResult* result) {
ash::DlcserviceClient::Get()->GetExistingDlcs(base::BindOnce(
[](base::WeakPtr<TerminaInstaller> weak_this,
base::OnceCallback<void()> callback, UninstallResult* result,
std::string_view err,
const dlcservice::DlcsWithContent& dlcs_with_content) {
if (!weak_this) {
return;
}
if (err != dlcservice::kErrorNone) {
LOG(ERROR) << "Failed to list installed DLCs: " << err;
*result = false;
std::move(callback).Run();
return;
}
for (const auto& dlc : dlcs_with_content.dlc_infos()) {
if (dlc.id() == kCrostiniDlcName) {
VLOG(1) << "DLC present, removing";
weak_this->RemoveDlc(std::move(callback), result);
return;
}
}
VLOG(1) << "No DLC present, skipping";
*result = true;
std::move(callback).Run();
},
weak_ptr_factory_.GetWeakPtr(), std::move(callback), result));
}
void TerminaInstaller::RemoveDlc(base::OnceCallback<void()> callback,
UninstallResult* result) {
ash::DlcserviceClient::Get()->Uninstall(
kCrostiniDlcName, base::BindOnce(
[](base::OnceCallback<void()> callback,
UninstallResult* result, std::string_view err) {
if (err == dlcservice::kErrorNone) {
VLOG(1) << "Removed DLC";
*result = true;
} else {
LOG(ERROR)
<< "Failed to remove termina-dlc: " << err;
*result = false;
}
std::move(callback).Run();
},
std::move(callback), result));
}
void TerminaInstaller::OnUninstallFinished(
base::OnceCallback<void(bool)> callback,
std::vector<UninstallResult> partial_results) {
std::move(callback).Run(!base::Contains(partial_results, 0));
}
base::FilePath TerminaInstaller::GetInstallLocation() {
CHECK(termina_location_)
<< "GetInstallLocation() called while termina not installed";
return *termina_location_;
}
std::optional<std::string> TerminaInstaller::GetDlcId() {
CHECK(termina_location_) << "GetDlcId() called while termina not installed";
return dlc_id_;
}
} // namespace crostini
|