File: plugin_vm_features.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (226 lines) | stat: -rw-r--r-- 7,682 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
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
// 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/plugin_vm/plugin_vm_features.h"

#include "base/feature_list.h"
#include "base/system/sys_info.h"
#include "chrome/browser/ash/plugin_vm/plugin_vm_pref_names.h"
#include "chrome/browser/ash/plugin_vm/plugin_vm_util.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_features.h"
#include "chromeos/ash/components/install_attributes/install_attributes.h"
#include "chromeos/ash/components/settings/cros_settings.h"
#include "chromeos/ash/components/settings/cros_settings_names.h"
#include "chromeos/ash/experiences/guest_os/virtual_machines/virtual_machines_util.h"
#include "components/prefs/pref_service.h"
#include "components/user_manager/user.h"

namespace plugin_vm {

namespace {

using ProfileSupported = PluginVmFeatures::ProfileSupported;
using PolicyConfigured = PluginVmFeatures::PolicyConfigured;

ProfileSupported CheckProfileSupported(const Profile* profile) {
  if (!profile) {
    VLOG(1) << "profile == nullptr";
    return ProfileSupported::kErrorNotSupported;
  }

  if (!ash::ProfileHelper::IsPrimaryProfile(profile)) {
    return ProfileSupported::kErrorNonPrimary;
  }

  if (profile->IsChild()) {
    return ProfileSupported::kErrorChildAccount;
  }

  if (profile->IsOffTheRecord()) {
    return ProfileSupported::kErrorOffTheRecord;
  }

  if (ash::ProfileHelper::IsEphemeralUserProfile(profile)) {
    return ProfileSupported::kErrorEphemeral;
  }

  if (!ash::ProfileHelper::IsUserProfile(profile)) {
    VLOG(1) << "non-regular profile is not supported";
    // If this happens, the profile is for something like the sign in screen or
    // lock screen. Return a generic error code because the user will not be
    // able to see the error code/message anyway.
    return ProfileSupported::kErrorNotSupported;
  }

  return ProfileSupported::kOk;
}

PolicyConfigured CheckPolicyConfigured(const Profile* profile) {
  // Bypass other checks when a fake policy is set, or running linux-chromeos.
  if (FakeLicenseKeyIsSet() || !base::SysInfo::IsRunningOnChromeOS()) {
    return PolicyConfigured::kOk;
  }

  if (profile == nullptr) {
    VLOG(1) << "Profile is null";
    return PolicyConfigured::kErrorUnableToCheckPolicy;
  }

  // Check that the device is enterprise enrolled.
  if (!ash::InstallAttributes::Get()->IsEnterpriseManaged()) {
    return PolicyConfigured::kErrorNotEnterpriseEnrolled;
  }

  // Check that the user is affiliated.
  const user_manager::User* const user =
      ash::ProfileHelper::Get()->GetUserByProfile(profile);
  if (user == nullptr || !user->IsAffiliated()) {
    return PolicyConfigured::kErrorUserNotAffiliated;
  }

  // Check that VirtualMachines are allowed by policy.
  if (!virtual_machines::AreVirtualMachinesAllowedByPolicy()) {
    return PolicyConfigured::kErrorVirtualMachinesNotAllowed;
  }

  // Check that PluginVm is allowed to run by policy.
  bool plugin_vm_allowed_for_device;
  if (!ash::CrosSettings::Get()->GetBoolean(ash::kPluginVmAllowed,
                                            &plugin_vm_allowed_for_device)) {
    return PolicyConfigured::kErrorUnableToCheckDevicePolicy;
  }

  if (!plugin_vm_allowed_for_device) {
    return PolicyConfigured::kErrorNotAllowedByDevicePolicy;
  }

  bool plugin_vm_allowed_for_user =
      profile->GetPrefs()->GetBoolean(plugin_vm::prefs::kPluginVmAllowed);
  if (!plugin_vm_allowed_for_user) {
    return PolicyConfigured::kErrorNotAllowedByUserPolicy;
  }

  if (GetPluginVmUserIdForProfile(profile).empty()) {
    return PolicyConfigured::kErrorLicenseNotSetUp;
  }

  return PolicyConfigured::kOk;
}

}  // namespace

bool PluginVmFeatures::IsAllowedDiagnostics::IsOk() const {
  return device_supported && profile_supported == ProfileSupported::kOk &&
         policy_configured == PolicyConfigured::kOk;
}

std::string PluginVmFeatures::IsAllowedDiagnostics::GetTopError() const {
  if (!device_supported) {
    return "Parallels Desktop is not supported on this device";
  }

  switch (profile_supported) {
    case ProfileSupported::kOk:
      break;
    case ProfileSupported::kErrorNonPrimary:
      return "Parallels Desktop is only allowed in primary user sessions";
    case ProfileSupported::kErrorChildAccount:
      return "Child accounts are not supported";
    case ProfileSupported::kErrorOffTheRecord:
      return "Guest profiles are not supported";
    case ProfileSupported::kErrorEphemeral:
      return "Ephemeral user profiles are not supported";
    case ProfileSupported::kErrorNotSupported:
      return "This user session is not allowed to run Parallels Desktop";
  }

  switch (policy_configured) {
    case PolicyConfigured::kOk:
      break;
    case PolicyConfigured::kErrorUnableToCheckPolicy:
      return "Unable to check policy";
    case PolicyConfigured::kErrorNotEnterpriseEnrolled:
      return "This is not an enterprise-managed device";
    case PolicyConfigured::kErrorUserNotAffiliated:
      return "This user is not affiliated with the organization";
    case PolicyConfigured::kErrorUnableToCheckDevicePolicy:
      return "Unable to determine if device-level policy allows running VMs";
    case PolicyConfigured::kErrorNotAllowedByDevicePolicy:
      return "VMs are disallowed by policy on this device";
    case PolicyConfigured::kErrorNotAllowedByUserPolicy:
      return "VMs are disallowed by policy";
    case PolicyConfigured::kErrorLicenseNotSetUp:
      return "License for the product is not set up in policy";
    case PolicyConfigured::kErrorVirtualMachinesNotAllowed:
      return "No Virtual Machines are allowed on this device";
  }

  return "";
}

static PluginVmFeatures* g_plugin_vm_features = nullptr;

PluginVmFeatures* PluginVmFeatures::Get() {
  if (!g_plugin_vm_features) {
    g_plugin_vm_features = new PluginVmFeatures();
  }
  return g_plugin_vm_features;
}

void PluginVmFeatures::SetForTesting(PluginVmFeatures* features) {
  g_plugin_vm_features = features;
}

PluginVmFeatures::PluginVmFeatures() = default;

PluginVmFeatures::~PluginVmFeatures() = default;

// For PluginVm to be allowed:
// * PluginVm feature should be enabled.
// * Profile should be eligible.
// * Device should be enterprise enrolled:
//   * User should be affiliated.
//   * PluginVmAllowed device policy should be set to true.
//   * UserPluginVmAllowed user policy should be set to true.
// * PluginVmUserId policy is set.
PluginVmFeatures::IsAllowedDiagnostics
PluginVmFeatures::GetIsAllowedDiagnostics(const Profile* profile) {
  auto diagnostics = IsAllowedDiagnostics{
      /*device_supported=*/base::FeatureList::IsEnabled(features::kPluginVm),
      /*profile_supported=*/CheckProfileSupported(profile),
      /*policy_configured=*/CheckPolicyConfigured(profile),
  };
  if (!diagnostics.IsOk()) {
    VLOG(1) << diagnostics.GetTopError();
  }

  return diagnostics;
}

bool PluginVmFeatures::IsAllowed(const Profile* profile, std::string* reason) {
  auto diagnostics = GetIsAllowedDiagnostics(profile);
  if (!diagnostics.IsOk()) {
    if (reason) {
      *reason = diagnostics.GetTopError();
      DCHECK(!reason->empty());
    }
    return false;
  }

  return true;
}

bool PluginVmFeatures::IsConfigured(const Profile* profile) {
  return profile->GetPrefs()->GetBoolean(
      plugin_vm::prefs::kPluginVmImageExists);
}

bool PluginVmFeatures::IsEnabled(const Profile* profile) {
  return PluginVmFeatures::Get()->IsAllowed(profile) &&
         PluginVmFeatures::Get()->IsConfigured(profile);
}

}  // namespace plugin_vm