File: guest_id.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 (262 lines) | stat: -rw-r--r-- 8,869 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// 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/guest_os/guest_id.h"

#include <algorithm>
#include <memory>
#include <string_view>
#include <vector>

#include "base/containers/contains.h"
#include "base/logging.h"
#include "base/no_destructor.h"
#include "base/strings/string_split.h"
#include "base/strings/stringprintf.h"
#include "chrome/browser/ash/guest_os/guest_os_pref_names.h"
#include "chrome/browser/ash/guest_os/public/types.h"
#include "chrome/browser/profiles/profile.h"
#include "chromeos/ash/components/dbus/vm_applications/apps.pb.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"

namespace guest_os {
namespace {

static const base::NoDestructor<std::vector<std::string>> kPropertiesAllowList{{
    prefs::kContainerCreateOptions,
    prefs::kContainerOsVersionKey,
    prefs::kContainerOsPrettyNameKey,
    prefs::kContainerColorKey,
    prefs::kTerminalSupportedKey,
    prefs::kTerminalLabel,
    prefs::kTerminalPolicyDisabled,
    prefs::kContainerSharedVmDevicesKey,
    prefs::kBruschettaConfigId,
}};

}  // namespace

GuestId::GuestId(VmType vm_type,
                 std::string vm_name,
                 std::string container_name) noexcept
    : vm_type(vm_type),
      vm_name(std::move(vm_name)),
      container_name(std::move(container_name)) {}

GuestId::GuestId(std::string vm_name, std::string container_name) noexcept
    : vm_type(VmType::UNKNOWN),
      vm_name(std::move(vm_name)),
      container_name(std::move(container_name)) {}

GuestId::GuestId(const base::Value& value) noexcept {
  const base::Value::Dict* dict = value.GetIfDict();
  vm_type = VmTypeFromPref(value);
  const std::string* vm = nullptr;
  const std::string* container = nullptr;
  if (dict != nullptr) {
    vm = dict->FindString(prefs::kVmNameKey);
    container = dict->FindString(prefs::kContainerNameKey);
  }
  vm_name = vm ? *vm : "";
  container_name = container ? *container : "";
}

base::flat_map<std::string, std::string> GuestId::ToMap() const {
  base::flat_map<std::string, std::string> extras;
  extras[prefs::kVmNameKey] = vm_name;
  extras[prefs::kContainerNameKey] = container_name;
  return extras;
}

base::Value::Dict GuestId::ToDictValue() const {
  base::Value::Dict dict;
  dict.Set(prefs::kVmTypeKey, static_cast<int>(vm_type));
  dict.Set(prefs::kVmNameKey, vm_name);
  dict.Set(prefs::kContainerNameKey, container_name);
  return dict;
}

bool operator<(const GuestId& lhs, const GuestId& rhs) noexcept {
  int result = lhs.vm_name.compare(rhs.vm_name);
  if (result != 0) {
    return result < 0;
  }
  return lhs.container_name < rhs.container_name;
}

bool operator==(const GuestId& lhs, const GuestId& rhs) noexcept {
  return lhs.vm_name == rhs.vm_name && lhs.container_name == rhs.container_name;
}

std::ostream& operator<<(std::ostream& ostream, const GuestId& container_id) {
  return ostream << "(type:" << container_id.vm_type << ":"
                 << vm_tools::apps::VmType_Name(container_id.vm_type)
                 << " vm:\"" << container_id.vm_name << "\" container:\""
                 << container_id.container_name << "\")";
}

std::string GuestId::Serialize() const {
  return base::StringPrintf("%s:%s:%s", VmType_Name(this->vm_type),
                            this->vm_name, this->container_name);
}

std::optional<GuestId> Deserialize(std::string_view guest_id_string) {
  std::vector<std::string> string_tokens = base::SplitString(
      guest_id_string, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);

  if (string_tokens.size() != 3) {
    return {};
  }

  // vm_type and vm_name should be present, but container name may be empty for
  // containerless guests.
  if (string_tokens[0].empty() || string_tokens[1].empty()) {
    return {};
  }

  VmType vm_type;
  if (!VmType_Parse(string_tokens[0], &vm_type)) {
    return {};
  }

  return GuestId(vm_type, string_tokens[1], string_tokens[2]);
}

bool MatchContainerDict(const base::Value& dict, const GuestId& container_id) {
  const std::string* vm_name = dict.GetDict().FindString(prefs::kVmNameKey);
  const std::string* container_name =
      dict.GetDict().FindString(prefs::kContainerNameKey);
  return (vm_name && *vm_name == container_id.vm_name) &&
         (container_name && *container_name == container_id.container_name);
}

std::vector<GuestId> GetContainers(Profile* profile, VmType vm_type) {
  std::vector<GuestId> result;
  const base::Value::List& container_list =
      profile->GetPrefs()->GetList(prefs::kGuestOsContainers);
  for (const auto& container : container_list) {
    guest_os::GuestId id(container);
    if (id.vm_type == vm_type) {
      result.push_back(std::move(id));
    }
  }
  return result;
}

void AddContainerToPrefs(Profile* profile,
                         const GuestId& container_id,
                         base::Value::Dict properties) {
  ScopedListPrefUpdate updater(profile->GetPrefs(), prefs::kGuestOsContainers);
  if (std::ranges::any_of(*updater, [&container_id](const auto& dict) {
        return MatchContainerDict(dict, container_id);
      })) {
    return;
  }

  base::Value::Dict new_container = container_id.ToDictValue();
  for (auto [key, value] : properties) {
    if (base::Contains(*kPropertiesAllowList, key)) {
      new_container.Set(key, std::move(value));
    }
  }
  updater->Append(std::move(new_container));
}

void RemoveContainerFromPrefs(Profile* profile, const GuestId& container_id) {
  auto* pref_service = profile->GetPrefs();
  ScopedListPrefUpdate updater(pref_service, prefs::kGuestOsContainers);
  base::Value::List& update_list = updater.Get();
  auto it = std::ranges::find_if(update_list, [&](const auto& dict) {
    return MatchContainerDict(dict, container_id);
  });
  if (it != update_list.end()) {
    update_list.erase(it);
  }
}

void RemoveVmFromPrefs(Profile* profile, VmType vm_type) {
  auto* pref_service = profile->GetPrefs();
  ScopedListPrefUpdate updater(pref_service, prefs::kGuestOsContainers);
  base::Value::List& update_list = updater.Get();
  auto it = std::ranges::find(update_list, vm_type, &VmTypeFromPref);
  if (it != update_list.end()) {
    update_list.erase(it);
  }
}

const base::Value* GetContainerPrefValue(Profile* profile,
                                         const GuestId& container_id,
                                         const std::string& key) {
  const base::Value::List& containers =
      profile->GetPrefs()->GetList(prefs::kGuestOsContainers);
  for (const auto& dict : containers) {
    if (MatchContainerDict(dict, container_id)) {
      return dict.GetDict().Find(key);
    }
  }
  return nullptr;
}

void UpdateContainerPref(Profile* profile,
                         const GuestId& container_id,
                         const std::string& key,
                         base::Value value) {
  ScopedListPrefUpdate updater(profile->GetPrefs(), prefs::kGuestOsContainers);
  auto it = std::ranges::find_if(*updater, [&](const auto& dict) {
    return MatchContainerDict(dict, container_id);
  });
  if (it != updater->end()) {
    if (base::Contains(*kPropertiesAllowList, key)) {
      it->GetDict().Set(key, std::move(value));
    } else {
      LOG(ERROR) << "Ignoring disallowed property: " << key;
    }
  }
}

void MergeContainerPref(Profile* profile,
                        const GuestId& container_id,
                        const std::string& key,
                        base::Value::Dict dict) {
  ScopedListPrefUpdate updater(profile->GetPrefs(), prefs::kGuestOsContainers);
  auto it = std::ranges::find_if(*updater, [&](const auto& dict) {
    return MatchContainerDict(dict, container_id);
  });
  if (it != updater->end()) {
    if (base::Contains(*kPropertiesAllowList, key)) {
      base::Value::Dict* old_container_dict = it->GetIfDict();
      if (old_container_dict) {
        base::Value::Dict wrapped;
        wrapped.Set(key, std::move(dict));
        old_container_dict->Merge(std::move(wrapped));
      } else {
        LOG(ERROR) << "Expected a dict for " << container_id;
      }
    } else {
      LOG(ERROR) << "Ignoring disallowed property: " << key;
    }
  }
}

VmType VmTypeFromPref(const base::Value& pref) {
  if (!pref.is_dict()) {
    return VmType::UNKNOWN;
  }

  // Default is TERMINA(0) if field not present since this field was introduced
  // when only TERMINA was using prefs..
  auto type = pref.GetDict().FindInt(guest_os::prefs::kVmTypeKey);
  if (!type.has_value()) {
    LOG(WARNING) << "No VM type in pref, defaulting to termina";
    return VmType::TERMINA;
  }
  if (*type < vm_tools::apps::VmType_MIN ||
      *type > vm_tools::apps::VmType_MAX) {
    return VmType::UNKNOWN;
  }
  return static_cast<guest_os::VmType>(*type);
}

}  // namespace guest_os