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
|
// Copyright 2022 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/crostini_shared_devices.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "chrome/browser/ash/crostini/crostini_util.h"
#include "chrome/browser/ash/guest_os/guest_os_pref_names.h"
#include "chrome/browser/ash/guest_os/guest_os_session_tracker_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chromeos/ash/components/dbus/cicerone/cicerone_client.h"
namespace crostini {
CrostiniSharedDevices::CrostiniSharedDevices(Profile* profile)
: profile_(profile) {
// There is a risk that after a Chrome crash and restart, we won't recognize
// an already running container for which we have not yet applied user prefs.
// In this case, there is little risk as CrostiniRecoveryView should have been
// shown to tell the user they need to restart their container.
guest_os::GuestOsSessionTrackerFactory::GetForProfile(profile_)
->AddContainerStartedObserver(this);
}
CrostiniSharedDevices::~CrostiniSharedDevices() = default;
bool CrostiniSharedDevices::IsVmDeviceShared(guest_os::GuestId container_id,
const std::string& vm_device) {
const base::Value* shared_devices = GetContainerPrefValue(
profile_, container_id, guest_os::prefs::kContainerSharedVmDevicesKey);
if (!shared_devices || !shared_devices->is_dict()) {
VLOG(2) << "No shared_devices dict for " << container_id;
return false;
}
auto opt_shared = shared_devices->GetDict().FindBool(vm_device);
return opt_shared.has_value() && opt_shared.value();
}
void CrostiniSharedDevices::SetVmDeviceShared(guest_os::GuestId container_id,
const std::string& vm_device,
bool shared,
ResultCallback callback) {
const base::Value* prev_shared_devices = guest_os::GetContainerPrefValue(
profile_, container_id, guest_os::prefs::kContainerSharedVmDevicesKey);
base::Value::Dict shared_devices;
if (prev_shared_devices && prev_shared_devices->is_dict()) {
shared_devices = prev_shared_devices->GetDict().Clone();
}
shared_devices.Set(vm_device, shared);
if (guest_os::GuestOsSessionTrackerFactory::GetForProfile(profile_)
->IsRunning(container_id)) {
ApplySharingState(std::move(container_id), std::move(shared_devices),
std::move(callback));
} else {
VLOG(2) << "Not applying yet, container not running " << container_id;
guest_os::MergeContainerPref(profile_, container_id,
guest_os::prefs::kContainerSharedVmDevicesKey,
std::move(shared_devices));
std::move(callback).Run(false);
}
}
void CrostiniSharedDevices::ApplySharingState(
const guest_os::GuestId container_id,
base::Value::Dict next_shared_devices,
ResultCallback callback) {
if (next_shared_devices.empty()) {
VLOG(2) << "Nothing to apply";
return;
}
vm_tools::cicerone::UpdateContainerDevicesRequest request;
request.set_owner_id(CryptohomeIdForProfile(profile_));
request.set_vm_name(container_id.vm_name);
request.set_container_name(container_id.container_name);
auto* updates = request.mutable_updates();
for (auto it : next_shared_devices) {
auto opt_shared = it.second.GetIfBool();
vm_tools::cicerone::VmDeviceAction action =
opt_shared.has_value() && opt_shared.value()
? vm_tools::cicerone::VmDeviceAction::ENABLE
: vm_tools::cicerone::VmDeviceAction::DISABLE;
(*updates)[it.first] = action;
}
ash::CiceroneClient::Get()->UpdateContainerDevices(
request,
base::BindOnce(&CrostiniSharedDevices::OnUpdateContainerDevices,
weak_ptr_factory_.GetWeakPtr(), std::move(container_id),
std::move(next_shared_devices), std::move(callback)));
}
void CrostiniSharedDevices::OnUpdateContainerDevices(
const guest_os::GuestId container_id,
base::Value::Dict next_shared_devices,
ResultCallback callback,
std::optional<vm_tools::cicerone::UpdateContainerDevicesResponse>
response) {
bool success = true;
if (!response.has_value()) {
LOG(ERROR) << "No UpdateContainerDevicesResponse received.";
success = false;
} else if (response->status() !=
vm_tools::cicerone::UpdateContainerDevicesResponse::OK) {
LOG(ERROR)
<< "UpdateContainerDevices failed, error "
<< vm_tools::cicerone::UpdateContainerDevicesResponse_Status_Name(
response->status());
success = false;
}
if (success) {
for (const auto& it : response->results()) {
const base::Value* prev_shared_devices = guest_os::GetContainerPrefValue(
profile_, container_id,
guest_os::prefs::kContainerSharedVmDevicesKey);
const auto& vm_device = it.first;
auto result = it.second;
switch (result) {
case vm_tools::cicerone::UpdateContainerDevicesResponse::ACTION_FAILED:
LOG(WARNING) << "trying to restore old pref value for " << it.first;
if (prev_shared_devices && prev_shared_devices->is_dict()) {
// Preserve/restore the old pref value.
auto opt_shared =
prev_shared_devices->GetDict().FindBool(vm_device);
if (opt_shared.has_value()) {
next_shared_devices.Set(vm_device, opt_shared.value());
}
} else {
next_shared_devices.Remove(vm_device);
}
break;
case vm_tools::cicerone::UpdateContainerDevicesResponse::
NO_SUCH_VM_DEVICE:
LOG(WARNING) << "VM has no such device " << it.first;
next_shared_devices.Set(vm_device, false);
break;
case vm_tools::cicerone::UpdateContainerDevicesResponse::SUCCESS:
default:
VLOG(2) << "applied pref for " << it.first;
break;
}
}
guest_os::MergeContainerPref(profile_, container_id,
guest_os::prefs::kContainerSharedVmDevicesKey,
std::move(next_shared_devices));
}
std::move(callback).Run(success);
}
void CrostiniSharedDevices::OnContainerStarted(
const guest_os::GuestId& container_id) {
const base::Value* prev_shared_devices = guest_os::GetContainerPrefValue(
profile_, container_id, guest_os::prefs::kContainerSharedVmDevicesKey);
base::Value::Dict shared_devices;
if (prev_shared_devices && prev_shared_devices->is_dict()) {
shared_devices = prev_shared_devices->GetDict().Clone();
}
VLOG(2) << "Applying device sharing prefs for " << container_id;
ApplySharingState(std::move(container_id), std::move(shared_devices),
base::DoNothing());
}
} // namespace crostini
|