File: crostini_terminal_provider.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; 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,806; 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 (174 lines) | stat: -rw-r--r-- 7,438 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
// 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_terminal_provider.h"

#include "base/no_destructor.h"
#include "base/strings/stringprintf.h"
#include "chrome/browser/ash/crostini/crostini_features.h"
#include "chrome/browser/ash/crostini/crostini_manager.h"
#include "chrome/browser/ash/crostini/crostini_types.mojom-shared.h"
#include "chrome/browser/ash/crostini/crostini_util.h"
#include "chrome/browser/ash/file_manager/path_util.h"
#include "chrome/browser/ash/guest_os/guest_os_share_path.h"
#include "chrome/browser/ash/guest_os/guest_os_share_path_factory.h"
#include "chrome/browser/ash/guest_os/guest_os_terminal.h"
#include "chrome/browser/extensions/api/terminal/startup_status.h"
#include "chrome/browser/ui/views/crostini/crostini_recovery_view.h"
#include "chrome/grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"

namespace crostini {
using mojom::InstallerState;

namespace {
// Counts from chrome/browser/ash/crostini/crostini_types.mojom.
const int kMaxStage = static_cast<int>(InstallerState::kConfigureContainer) + 1;
}  // namespace

// Displays startup status to the crostini terminal.
class CrostiniStartupStatus : public crostini::CrostiniManager::RestartObserver,
                              public extensions::StartupStatus {
 public:
  explicit CrostiniStartupStatus(
      std::unique_ptr<extensions::StartupStatusPrinter> printer)
      : StartupStatus(std::move(printer), kMaxStage) {}
  ~CrostiniStartupStatus() override = default;

  // crostini::CrostiniManager::RestartObserver
  void OnStageStarted(crostini::mojom::InstallerState stage) override {
    int stage_index = static_cast<int>(stage);
    static base::NoDestructor<base::flat_map<InstallerState, std::string>>
        kStartStrings({
            {InstallerState::kStart,
             l10n_util::GetStringUTF8(IDS_CROSTINI_TERMINAL_STATUS_START)},
            {InstallerState::kInstallImageLoader,
             l10n_util::GetStringUTF8(
                 IDS_CROSTINI_TERMINAL_STATUS_INSTALL_IMAGE_LOADER)},
            {InstallerState::kCreateDiskImage,
             l10n_util::GetStringUTF8(
                 IDS_CROSTINI_TERMINAL_STATUS_CREATE_DISK_IMAGE)},
            {InstallerState::kStartTerminaVm,
             l10n_util::GetStringUTF8(
                 IDS_CROSTINI_TERMINAL_STATUS_START_TERMINA_VM)},
            {InstallerState::kStartLxd,
             l10n_util::GetStringUTF8(IDS_CROSTINI_TERMINAL_STATUS_START_LXD)},
            {InstallerState::kCreateContainer,
             l10n_util::GetStringUTF8(
                 IDS_CROSTINI_TERMINAL_STATUS_CREATE_CONTAINER)},
            {InstallerState::kSetupContainer,
             l10n_util::GetStringUTF8(
                 IDS_CROSTINI_TERMINAL_STATUS_SETUP_CONTAINER)},
            {InstallerState::kStartContainer,
             l10n_util::GetStringUTF8(
                 IDS_CROSTINI_TERMINAL_STATUS_START_CONTAINER)},
            {InstallerState::kConfigureContainer,
             l10n_util::GetStringUTF8(
                 IDS_CROSTINI_TERMINAL_STATUS_CONFIGURE_CONTAINER)},
        });
    const std::string& stage_string = (*kStartStrings)[stage];
    // Ensure we have a valid string for each stage.
    DCHECK(!stage_string.empty());
    printer()->PrintStage(stage_index, stage_string);
  }
};

CrostiniTerminalProvider::CrostiniTerminalProvider(
    Profile* profile,
    guest_os::GuestId container_id)
    : profile_(profile), container_id_(container_id) {}
CrostiniTerminalProvider::~CrostiniTerminalProvider() = default;

std::string CrostiniTerminalProvider::Label() {
  return crostini::FormatForUi(container_id_);
}

guest_os::GuestId CrostiniTerminalProvider::GuestId() {
  return container_id_;
}

bool CrostiniTerminalProvider::RecoveryRequired(int64_t display_id) {
  auto* crostini_manager = crostini::CrostiniManager::GetForProfile(profile_);
  if (crostini_manager->IsUncleanStartup()) {
    ShowCrostiniRecoveryView(profile_, crostini::CrostiniUISurface::kAppList,
                             guest_os::kTerminalSystemAppId, display_id, {},
                             base::DoNothing());
    return true;
  }
  return false;
}

bool CrostiniTerminalProvider::AllowedByPolicy() {
  return CrostiniFeatures::Get()->IsAllowedNow(profile_);
}

std::string CrostiniTerminalProvider::PrepareCwd(storage::FileSystemURL url) {
  std::string cwd;
  CrostiniManager::RestartOptions options;
  auto* share_path = guest_os::GuestOsSharePathFactory::GetForProfile(profile_);
  base::FilePath path;
  if (file_manager::util::ConvertFileSystemURLToPathInsideCrostini(
          profile_, url, &path)) {
    cwd = path.value();
    const auto& fs_id = url.mount_filesystem_id();
    auto mount_name =
        // TODO(b/217469540): Currently the default Crostini container gets
        // mounted in a different location to other Guest OS mounts, as we get
        // consistent file sharing across Guest OSs we can remove this special
        // case.
        (container_id_ == DefaultContainerId())
            ? file_manager::util::GetCrostiniMountPointName(profile_)
            : file_manager::util::GetGuestOsMountPointName(profile_,
                                                           container_id_);
    if (fs_id != mount_name &&
        !share_path->IsPathShared(container_id_.vm_name, url.path())) {
      // Path isn't already shared, so share it.
      options.share_paths.push_back(url.path());
    }
  } else {
    LOG(WARNING) << "Failed to parse: " << path << ". Not setting terminal cwd";
    return "";
  }
  // This completes async, but we don't wait for it since the terminal itself
  // also calls RestartCrostini and that'll get serialised, ensuring that this
  // call has completed before the share gets used.
  CrostiniManager::GetForProfile(profile_)->RestartCrostiniWithOptions(
      container_id_, std::move(options), base::DoNothing());
  return cwd;
}

std::unique_ptr<extensions::StartupStatus>
CrostiniTerminalProvider::CreateStartupStatus(
    std::unique_ptr<extensions::StartupStatusPrinter> printer) {
  return std::make_unique<CrostiniStartupStatus>(std::move(printer));
}

void CrostiniTerminalProvider::EnsureRunning(
    extensions::StartupStatus* startup_status,
    base::OnceCallback<void(bool success, std::string failure_reason)>
        callback) {
  CrostiniManager::GetForProfile(profile_)->RestartCrostini(
      container_id_,
      base::BindOnce(
          [](base::OnceCallback<void(bool, std::string)> callback,
             CrostiniResult result) {
            if (result == CrostiniResult::SUCCESS) {
              std::move(callback).Run(true, "");
            } else {
              crostini::RecordAppLaunchResultHistogram(
                  crostini::CrostiniAppLaunchAppType::kTerminal, result);
              std::move(callback).Run(
                  false,
                  base::StringPrintf(
                      "Error starting crostini for terminal: %d (%s)",
                      static_cast<int>(result), CrostiniResultString(result)));
            }
          },
          std::move(callback)),
      // Downcast is safe because terminal_private_api.cc will only call us with
      // the startup status we created in CreateStartupStatus.
      static_cast<CrostiniStartupStatus*>(startup_status));
}

}  // namespace crostini