File: guest_os_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 (253 lines) | stat: -rw-r--r-- 9,994 bytes parent folder | download | duplicates (3)
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
// 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/guest_os_apps.h"

#include <utility>
#include <vector>

#include "base/check_is_test.h"
#include "base/strings/string_util.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/app_service_proxy_base.h"
#include "chrome/browser/apps/app_service/intent_util.h"
#include "chrome/browser/apps/app_service/launch_utils.h"
#include "chrome/browser/ash/crostini/crostini_features.h"
#include "chrome/browser/ash/file_manager/fileapi_util.h"
#include "chrome/browser/ash/guest_os/guest_os_registry_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "components/services/app_service/public/cpp/app_types.h"
#include "components/services/app_service/public/cpp/icon_types.h"
#include "components/services/app_service/public/cpp/intent_util.h"
#include "storage/browser/file_system/file_system_context.h"

namespace apps {

GuestOSApps::GuestOSApps(AppServiceProxy* proxy)
    : AppPublisher(proxy), profile_(proxy->profile()) {}

GuestOSApps::~GuestOSApps() = default;

void GuestOSApps::InitializeForTesting() {
  CHECK_IS_TEST();
  Initialize();
}

void GuestOSApps::Initialize() {
  DCHECK(profile_);
  if (!CouldBeAllowed()) {
    // Set the publisher unavailable to remove apps saved in the AppStorage
    // file, and related launch requests.
    proxy()->SetPublisherUnavailable(AppType());
    return;
  }
  registry_ = guest_os::GuestOsRegistryServiceFactory::GetForProfile(profile_);
  if (!registry_) {
    // Set the publisher unavailable to remove apps saved in the AppStorage
    // file, and related launch requests.
    proxy()->SetPublisherUnavailable(AppType());
    return;
  }
  registry_observation_.Observe(registry_);
  RegisterPublisher(AppType());
  std::vector<AppPtr> apps;
  for (const auto& pair : registry_->GetRegisteredApps(VmType())) {
    const guest_os::GuestOsRegistryService::Registration& registration =
        pair.second;
    apps.push_back(CreateApp(registration,
                             /*generate_new_icon_key=*/true));
  }
  AppPublisher::Publish(std::move(apps), AppType(),
                        /*should_notify_initialized=*/true);
}

std::vector<guest_os::LaunchArg> GuestOSApps::ArgsFromIntent(
    const apps::Intent* intent) {
  std::vector<guest_os::LaunchArg> args;
  if (!intent || intent->files.empty()) {
    return args;
  }
  args.reserve(intent->files.size());
  storage::FileSystemContext* file_system_context =
      file_manager::util::GetFileManagerFileSystemContext(profile());
  for (auto& file : intent->files) {
    args.emplace_back(
        file_system_context->CrackURLInFirstPartyContext(file->url));
  }
  return args;
}

void GuestOSApps::GetCompressedIconData(const std::string& app_id,
                                        int32_t size_in_dip,
                                        ui::ResourceScaleFactor scale_factor,
                                        LoadIconCallback callback) {
  GetGuestOSAppCompressedIconData(profile_, app_id, size_in_dip, scale_factor,
                                  std::move(callback));
}

void GuestOSApps::LaunchAppWithParams(AppLaunchParams&& params,
                                      LaunchCallback callback) {
  auto event_flags = apps::GetEventFlags(params.disposition,
                                         /*prefer_container=*/false);
  if (params.intent) {
    LaunchAppWithIntent(params.app_id, event_flags, std::move(params.intent),
                        params.launch_source,
                        std::make_unique<WindowInfo>(params.display_id),
                        std::move(callback));
  } else {
    Launch(params.app_id, event_flags, params.launch_source,
           std::make_unique<WindowInfo>(params.display_id));
    // TODO(crbug.com/40787924): Add launch return value.
    std::move(callback).Run(LaunchResult());
  }
}

void GuestOSApps::OnRegistryUpdated(
    guest_os::GuestOsRegistryService* registry_service,
    guest_os::VmType vm_type,
    const std::vector<std::string>& updated_apps,
    const std::vector<std::string>& removed_apps,
    const std::vector<std::string>& inserted_apps) {
  if (vm_type != VmType()) {
    return;
  }
  // TODO(sidereal): Do something cleverer here so we only need to publish a new
  // icon when the icon has actually changed.
  for (const std::string& app_id : updated_apps) {
    if (auto registration = registry_->GetRegistration(app_id)) {
      AppPublisher::Publish(
          CreateApp(*registration, /*generate_new_icon_key=*/true));
    }
  }
  for (const std::string& app_id : removed_apps) {
    auto app = std::make_unique<App>(AppType(), app_id);
    app->readiness = Readiness::kUninstalledByUser;
    AppPublisher::Publish(std::move(app));
  }
  for (const std::string& app_id : inserted_apps) {
    if (auto registration = registry_->GetRegistration(app_id)) {
      AppPublisher::Publish(
          CreateApp(*registration, /*generate_new_icon_key=*/true));
    }
  }
}

void GuestOSApps::OnAppLastLaunchTimeUpdated(
    guest_os::VmType vm_type,
    const std::string& app_id,
    const base::Time& last_launch_time) {
  if (vm_type != VmType()) {
    return;
  }

  auto app = std::make_unique<App>(AppType(), app_id);
  app->last_launch_time = last_launch_time;
  std::vector<AppPtr> apps;
  apps.push_back(std::move(app));
  AppPublisher::Publish(std::move(apps), AppType(),
                        /*should_notify_initialized=*/false);
}

AppPtr GuestOSApps::CreateApp(
    const guest_os::GuestOsRegistryService::Registration& registration,
    bool generate_new_icon_key) {
  DCHECK_EQ(registration.VmType(), VmType());
  auto app = AppPublisher::MakeApp(
      AppType(), registration.app_id(), Readiness::kReady, registration.Name(),
      InstallReason::kUser, InstallSource::kUnknown);

  const std::string& executable_file_name = registration.ExecutableFileName();
  if (!executable_file_name.empty()) {
    app->additional_search_terms.push_back(executable_file_name);
  }
  for (const std::string& keyword : registration.Keywords()) {
    app->additional_search_terms.push_back(keyword);
  }

  if (generate_new_icon_key) {
    IconEffects icon_effects = IconEffects::kCrOsStandardIcon;
    if (crostini::CrostiniFeatures::Get()->IsMultiContainerAllowed(profile_)) {
      icon_effects |= IconEffects::kGuestOsBadge;
    }
    app->icon_key = IconKey(icon_effects);
  }

  app->last_launch_time = registration.LastLaunchTime();
  app->install_time = registration.InstallTime();

  auto show = !registration.NoDisplay();
  app->show_in_launcher = show;
  app->show_in_search = show;
  app->show_in_shelf = show;
  app->show_in_management = false;
  app->allow_uninstall = false;
  app->allow_close = true;

  // Add intent filters based on file extensions.
  app->handles_intents = true;
  const guest_os::GuestOsMimeTypesService* mime_types_service =
      guest_os::GuestOsMimeTypesServiceFactory::GetForProfile(profile());
  app->intent_filters =
      CreateIntentFilterForAppService(mime_types_service, registration);

  app->SetExtraField("vm_name", registration.VmName());
  app->SetExtraField("container_name", registration.ContainerName());
  app->SetExtraField("desktop_file_id", registration.DesktopFileId());
  app->SetExtraField("exec", registration.Exec());
  app->SetExtraField("executable_file_name", registration.ExecutableFileName());
  app->SetExtraField("no_display", registration.NoDisplay());
  app->SetExtraField("terminal", registration.Terminal());
  app->SetExtraField("scaled", registration.IsScaled());
  app->SetExtraField("package_id", registration.PackageId());
  app->SetExtraField("startup_wm_class", registration.StartupWmClass());
  app->SetExtraField("startup_notify", registration.StartupNotify());

  // Allow subclasses of GuestOSApps to modify app.
  CreateAppOverrides(registration, app.get());

  return app;
}

apps::IntentFilters CreateIntentFilterForAppService(
    const guest_os::GuestOsMimeTypesService* mime_types_service,
    const guest_os::GuestOsRegistryService::Registration& registration) {
  const std::set<std::string> mime_types_set = registration.MimeTypes();
  if (mime_types_set.empty()) {
    return {};
  }

  // When a file has a mime type that Files App can't recognise but the guest
  // can (e.g. a proprietary file type), we should look at the file extensions
  // that the app can support. We find these extension types by checking what
  // extensions correspond to the app's supported mime types.
  std::vector<std::string> extension_types =
      mime_types_service->GetExtensionTypesFromMimeTypes(
          mime_types_set, registration.VmName(), registration.ContainerName());
  std::vector<std::string> mime_types(mime_types_set.begin(),
                                      mime_types_set.end());

  // If we see that the app supports the text/plain mime-type, then the app
  // supports all files with type text/*, as per xdg spec.
  // https://specifications.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-latest.html.
  // In this case, remove all mime types that begin with "text/" and replace
  // them with a single "text/*" mime type.
  if (base::Contains(mime_types, "text/plain")) {
    std::erase_if(mime_types, [](const std::string& s) {
      return base::StartsWith(s, "text/");
    });
    mime_types.push_back("text/*");
  }

  apps::IntentFilters intent_filters;
  intent_filters.push_back(apps_util::CreateFileFilter(
      {apps_util::kIntentActionView}, mime_types, extension_types,
      // TODO(crbug.com/40233967): Remove activity_name when default file
      // handling preferences for Files App are migrated.
      /*activity_name=*/apps_util::kGuestOsActivityName));

  return intent_filters;
}

}  // namespace apps