File: crostini_shared_devices.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (167 lines) | stat: -rw-r--r-- 7,014 bytes parent folder | download | duplicates (6)
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