File: bruschetta_apps.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 (250 lines) | stat: -rw-r--r-- 9,735 bytes parent folder | download | duplicates (5)
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
// Copyright 2023 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/apps/app_service/publishers/bruschetta_apps.h"

#include <string>
#include <utility>
#include <variant>
#include <vector>

#include "ash/public/cpp/app_menu_constants.h"
#include "base/functional/callback_helpers.h"
#include "chrome/browser/apps/app_service/app_launch_params.h"
#include "chrome/browser/apps/app_service/app_service_proxy.h"
#include "chrome/browser/apps/app_service/menu_util.h"
#include "chrome/browser/ash/bruschetta/bruschetta_launcher.h"
#include "chrome/browser/ash/bruschetta/bruschetta_service.h"
#include "chrome/browser/ash/bruschetta/bruschetta_service_factory.h"
#include "chrome/browser/ash/bruschetta/bruschetta_util.h"
#include "chrome/browser/ash/crostini/crostini_manager.h"
#include "chrome/browser/ash/crostini/crostini_util.h"
#include "chrome/browser/ash/file_manager/fileapi_util.h"
#include "chrome/browser/ash/file_manager/path_util.h"
#include "chrome/browser/ash/guest_os/guest_os_launcher.h"
#include "chrome/browser/ash/guest_os/guest_os_registry_service.h"
#include "chrome/browser/ash/guest_os/guest_os_session_tracker.h"
#include "chrome/browser/ash/guest_os/guest_os_session_tracker_factory.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/profiles/profile.h"
#include "chrome/browser/ui/ash/shelf/chrome_shelf_controller.h"
#include "chrome/browser/ui/ash/shelf/shelf_spinner_controller.h"
#include "chrome/browser/ui/ash/shelf/shelf_spinner_item_controller.h"
#include "chrome/grit/chrome_unscaled_resources.h"
#include "chrome/grit/generated_resources.h"
#include "components/services/app_service/public/cpp/app_types.h"
#include "components/services/app_service/public/cpp/intent.h"

namespace apps {

namespace {

void AddSpinner(const std::string& app_id) {
  if (auto* chrome_controller = ChromeShelfController::instance()) {
    chrome_controller->GetShelfSpinnerController()->AddSpinnerToShelf(
        app_id, std::make_unique<ShelfSpinnerItemController>(app_id));
  }
}

// It's safe to remove a non-existent spinner.
void RemoveSpinner(const std::string& app_id) {
  if (auto* chrome_controller = ChromeShelfController::instance()) {
    chrome_controller->GetShelfSpinnerController()->CloseSpinner(app_id);
  }
}

void OnLaunchFailed(const std::string& app_id,
                    LaunchCallback callback,
                    const std::string& reason) {
  LOG(ERROR) << "Failed to launch Bruschetta app " << app_id << ": " << reason;
  RemoveSpinner(app_id);
  std::move(callback).Run(ConvertBoolToLaunchResult(false));
}

void OnSharePathForLaunchApplication(
    Profile* profile,
    const std::string& app_id,
    guest_os::GuestOsRegistryService::Registration registration,
    const guest_os::GuestId container_id,
    int64_t display_id,
    const std::vector<std::string>& args,
    LaunchCallback callback,
    bool success,
    const std::string& failure_reason) {
  if (!success) {
    OnLaunchFailed(app_id, std::move(callback),
                   "Failed to share paths with Bruschetta: " + failure_reason);
    return;
  }
  guest_os::launcher::LaunchApplication(
      profile, container_id, std::move(registration), display_id, args,
      base::BindOnce(
          [](const std::string& app_id, LaunchCallback callback, bool success,
             const std::string& failure_reason) {
            if (!success) {
              OnLaunchFailed(app_id, std::move(callback), failure_reason);
              return;
            }
            RemoveSpinner(app_id);
            std::move(callback).Run(ConvertBoolToLaunchResult(success));
          },
          app_id, std::move(callback)));
}

void LaunchApplication(
    Profile* profile,
    const std::string& app_id,
    guest_os::GuestOsRegistryService::Registration registration,
    int64_t display_id,
    const std::vector<guest_os::LaunchArg> args,
    LaunchCallback callback) {
  // TODO(b/265601951): Handle window permissions. Crostini uses
  // AppServiceAppWindowCrostiniTracker::OnAppLaunchRequested for this.

  // Get vm_info because we need seneschal_server_handle.
  const std::string& vm_name = registration.VmName();
  auto vm_info =
      guest_os::GuestOsSessionTrackerFactory::GetForProfile(profile)->GetVmInfo(
          vm_name);
  if (!vm_info) {
    OnLaunchFailed(app_id, std::move(callback),
                   "Bruschetta VM not running: " + vm_name);
    return;
  }

  const guest_os::GuestId container_id(registration.ToGuestId());
  auto* share_path = guest_os::GuestOsSharePathFactory::GetForProfile(profile);
  auto paths_or_error = share_path->ConvertArgsToPathsToShare(
      registration, args, bruschetta::BruschettaChromeOSBaseDirectory(),
      /*map_crostini_home=*/false);
  if (std::holds_alternative<std::string>(paths_or_error)) {
    OnLaunchFailed(app_id, std::move(callback),
                   std::get<std::string>(paths_or_error));
    return;
  }
  const auto& paths =
      std::get<guest_os::GuestOsSharePath::PathsToShare>(paths_or_error);
  share_path->SharePaths(
      vm_name, vm_info->seneschal_server_handle(),
      std::move(paths.paths_to_share),
      base::BindOnce(OnSharePathForLaunchApplication, profile, app_id,
                     std::move(registration), std::move(container_id),
                     display_id, std::move(paths.launch_args),
                     std::move(callback)));
}

}  // namespace

BruschettaApps::BruschettaApps(AppServiceProxy* proxy) : GuestOSApps(proxy) {}

bool BruschettaApps::CouldBeAllowed() const {
  return true;
}

apps::AppType BruschettaApps::AppType() const {
  return AppType::kBruschetta;
}

guest_os::VmType BruschettaApps::VmType() const {
  return guest_os::VmType::BRUSCHETTA;
}

int BruschettaApps::DefaultIconResourceId() const {
  return IDR_LOGO_BRUSCHETTA_DEFAULT;
}

void BruschettaApps::Launch(const std::string& app_id,
                            int32_t event_flags,
                            LaunchSource launch_source,
                            WindowInfoPtr window_info) {
  LaunchAppWithIntent(app_id, event_flags, /*intent=*/nullptr, launch_source,
                      std::move(window_info), /*callback=*/base::DoNothing());
}

void BruschettaApps::LaunchAppWithIntent(const std::string& app_id,
                                         int32_t event_flags,
                                         IntentPtr intent,
                                         LaunchSource launch_source,
                                         WindowInfoPtr window_info,
                                         LaunchCallback callback) {
  const int64_t display_id =
      window_info ? window_info->display_id : display::kInvalidDisplayId;
  std::optional<guest_os::GuestOsRegistryService::Registration> registration =
      registry()->GetRegistration(app_id);
  if (!registration) {
    // TODO(b/247638226): RecordAppLaunchHistogram(kUnknown) to collect usage
    // stats for failed launches.
    OnLaunchFailed(app_id, std::move(callback),
                   "Unknown Bruschetta app_id: " + app_id);
    return;
  }

  // TODO(b/247638226): RecordAppLaunchHistogram(kRegisteredApp) to collect
  // usage stats for successful launches.

  // Update the last launched time.
  registry()->AppLaunched(app_id);

  // Start the bruschetta VM if necessary.
  const std::string& vm_name = registration->VmName();
  auto launcher = bruschetta::BruschettaServiceFactory::GetForProfile(profile())
                      ->GetLauncher(vm_name);
  if (!launcher) {
    OnLaunchFailed(app_id, std::move(callback),
                   "Unknown Bruschetta VM name: " + vm_name);
    return;
  }
  auto args = ArgsFromIntent(intent.get());
  AddSpinner(app_id);
  launcher->EnsureRunning(base::BindOnce(
      [](Profile* profile, const std::string& app_id,
         guest_os::GuestOsRegistryService::Registration registration,
         int64_t display_id, const std::vector<guest_os::LaunchArg> args,
         const std::string& vm_name, LaunchCallback callback,
         bruschetta::BruschettaResult result) {
        if (result != bruschetta::BruschettaResult::kSuccess) {
          OnLaunchFailed(app_id, std::move(callback),
                         "Failed to start Bruschetta VM " + vm_name + ": " +
                             bruschetta::BruschettaResultString(result));
          return;
        }
        LaunchApplication(profile, app_id, std::move(registration), display_id,
                          std::move(args), std::move(callback));
      },
      profile(), app_id, std::move(registration.value()), display_id,
      std::move(args), vm_name, std::move(callback)));
}

void BruschettaApps::CreateAppOverrides(
    const guest_os::GuestOsRegistryService::Registration& registration,
    App* app) {
  // TODO(b/247638042): Implement IsUninstallable and use it here.
}

void BruschettaApps::GetMenuModel(
    const std::string& app_id,
    MenuType menu_type,
    int64_t display_id,
    base::OnceCallback<void(MenuItems)> callback) {
  MenuItems menu_items;

  if (menu_type == MenuType::kShelf) {
    AddCommandItem(ash::APP_CONTEXT_MENU_NEW_WINDOW, IDS_APP_LIST_NEW_WINDOW,
                   menu_items);
  }

  if (ShouldAddOpenItem(app_id, menu_type, profile())) {
    AddCommandItem(ash::LAUNCH_NEW, IDS_APP_CONTEXT_MENU_ACTIVATE_ARC,
                   menu_items);
  }

  if (ShouldAddCloseItem(app_id, menu_type, profile())) {
    AddCommandItem(ash::MENU_CLOSE, IDS_SHELF_CONTEXT_MENU_CLOSE, menu_items);
  }

  std::move(callback).Run(std::move(menu_items));
}

}  // namespace apps