File: guest_os_terminal_provider_registry.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 (123 lines) | stat: -rw-r--r-- 4,524 bytes parent folder | download | duplicates (7)
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
// 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/public/guest_os_terminal_provider_registry.h"

#include <climits>
#include <memory>
#include <vector>

#include "base/strings/string_number_conversions.h"
#include "chrome/browser/ash/guest_os/guest_id.h"
#include "chrome/browser/ash/guest_os/guest_os_pref_names.h"
#include "chrome/browser/ash/guest_os/public/guest_os_terminal_provider.h"
#include "content/public/browser/browser_thread.h"

namespace guest_os {

GuestOsTerminalProviderRegistry::GuestOsTerminalProviderRegistry(
    Profile* profile)
    : profile_(profile) {}

GuestOsTerminalProviderRegistry::~GuestOsTerminalProviderRegistry() = default;

std::vector<GuestOsTerminalProviderRegistry::Id>
GuestOsTerminalProviderRegistry::List() {
  std::vector<Id> ret = std::vector<Id>();
  ret.reserve(providers_.size());
  for (const auto& pair : providers_) {
    ret.push_back(pair.first);
  }
  return ret;
}

GuestOsTerminalProvider* GuestOsTerminalProviderRegistry::Get(Id id) const {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  auto pos = providers_.find(id);
  if (pos == providers_.end()) {
    return nullptr;
  }
  return pos->second.get();
}

GuestOsTerminalProvider* GuestOsTerminalProviderRegistry::Get(
    const std::string& id) const {
  int output;
  if (!base::StringToInt(id, &output)) {
    return nullptr;
  }
  return Get(output);
}

GuestOsTerminalProvider* GuestOsTerminalProviderRegistry::Get(
    const guest_os::GuestId& id) const {
  for (const auto& pair : providers_) {
    if (pair.second->GuestId() == id) {
      return pair.second.get();
    }
  }
  return nullptr;
}

GuestOsTerminalProviderRegistry::Id GuestOsTerminalProviderRegistry::Register(
    std::unique_ptr<GuestOsTerminalProvider> provider) {
  // We use the range 0->INT_MAX because these IDs can get serialised into
  // base::Value, and that's the range they support.
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  CHECK(next_id_ < INT_MAX);
  Id id = next_id_++;
  providers_[id] = std::move(provider);
  SyncPrefs(id);

  return id;
}

void GuestOsTerminalProviderRegistry::SyncPrefs(
    GuestOsTerminalProviderRegistry::Id id) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  auto pos = providers_.find(id);
  CHECK(pos != providers_.end());
  auto* provider = pos->second.get();

  // Per discussion on http://crrev/c/3774559, terminal app would like to read
  // prefs directly (via a private Chrome API) instead of getting data from
  // anywhere else, so update prefs too. First we add in case it doesn't already
  // exist, this is a no-op if it already does, then we update it.
  // Note: We only unset for explicit unregistration, if e.g. policy changes
  // while Chrome isn't running the pref doesn't get updated. Per the above CL
  // this is an explicit ask so they're still listed in the terminal app.
  AddContainerToPrefs(profile_, provider->GuestId(), {});
  UpdateContainerPref(profile_, provider->GuestId(),
                      prefs::kTerminalSupportedKey, base::Value(true));
  UpdateContainerPref(profile_, provider->GuestId(), prefs::kTerminalLabel,
                      base::Value(provider->Label()));
  UpdateContainerPref(profile_, provider->GuestId(),
                      prefs::kTerminalPolicyDisabled,
                      base::Value(!provider->AllowedByPolicy()));
}

std::unique_ptr<GuestOsTerminalProvider>
GuestOsTerminalProviderRegistry::Unregister(Id id) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  auto pos = providers_.find(id);
  // No one should be unregistering random providers, so it's an error to try
  // and unregister one which doesn't exist rather than a no-op.
  CHECK(pos != providers_.end());
  auto ret = std::move(pos->second);
  providers_.erase(pos);

  // Per discussion on http://crrev/c/3774559, terminal app would like to read
  // prefs directly (via a private Chrome API) instead of getting data from
  // anywhere else, so update prefs to mark this guest as not supporting
  // terminal.
  // Note: We only unset for explicit unregistration, if e.g. policy changes
  // while Chrome isn't running the pref doesn't get updated. Per the above CL
  // this is an explicit ask so they're still listed in the terminal app.
  UpdateContainerPref(profile_, ret->GuestId(), prefs::kTerminalSupportedKey,
                      base::Value(false));
  return ret;
}

}  // namespace guest_os