File: guest_os_shelf_utils.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 (334 lines) | stat: -rw-r--r-- 13,363 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Copyright 2020 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_os_shelf_utils.h"

#include <string_view>

#include "base/logging.h"
#include "base/no_destructor.h"
#include "base/strings/string_util.h"
#include "chrome/browser/ash/borealis/borealis_window_manager.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/guest_os_session_tracker.h"
#include "chrome/browser/ash/guest_os/guest_os_session_tracker_factory.h"
#include "chrome/browser/ash/guest_os/public/types.h"
#include "chrome/browser/profiles/profile.h"
#include "components/prefs/pref_service.h"
#include "components/services/app_service/public/cpp/app_types.h"

namespace guest_os {

namespace {

// This prefix is used as a prefix when generating shelf ids for windows we
// couldn't match to an app. It is also used for crostini web dialogs (e.g.
// crostini installer/upgrader) which need to appear in the shelf.
//
// Note: if the value is changed, you will also need to manually update
// kCrostiniInstallerShelfId and kCrostiniUpgraderShelfId.
constexpr std::string_view kCrostiniShelfIdPrefix = "crostini:";

// Prefix of the WindowAppId set on exo windows for GuestOS X apps.
constexpr std::string_view kGuestOsWindowAppIdPrefix = "org.chromium.guest_os.";
// This comes after kGuestOsWindowAppIdPrefix+token for GuestOS Wayland apps.
constexpr std::string_view kWaylandPrefix = "wayland.";
// This comes after kGuestOsWindowAppIdPrefix+token.
constexpr std::string_view kWmClassPrefix = "wmclass.";

// TODO(b/267377562): Borealis windows have a hardcoded "borealis" token.
constexpr std::string_view kBorealisToken = "borealis";

const std::string* GetAppNameForWMClass(std::string_view wmclass) {
  // A hard-coded mapping from WMClass to app names.
  // This is used to deal with the Linux apps that don't specify the correct
  // WMClass in their desktop files so that their aura windows can be identified
  // with their respective app IDs.
  static const base::NoDestructor<std::map<std::string, std::string>>
      kWMClassToNname({{"Octave-gui", "GNU Octave"},
                       {"MuseScore2", "MuseScore 2"},
                       {"XnViewMP", "XnView Multi Platform"}});
  const auto it = kWMClassToNname->find(std::string(wmclass));
  if (it == kWMClassToNname->end())
    return nullptr;
  return &it->second;
}

bool MatchingString(const std::string& search_string,
                    const std::string& value_string,
                    bool ignore_space) {
  std::string search = search_string;
  std::string value = value_string;
  if (ignore_space) {
    base::RemoveChars(search, " ", &search);
    base::RemoveChars(value, " ", &value);
  }
  return base::EqualsCaseInsensitiveASCII(search, value);
}

enum class FindAppIdResult { NoMatch, UniqueMatch, NonUniqueMatch };
// Looks for an app where prefs_key is set to search_value. Returns the apps id
// if there was only one app matching, otherwise returns an empty string.
FindAppIdResult FindAppId(const base::Value::Dict& prefs,
                          std::string_view prefs_key,
                          std::string_view search_value,
                          const std::optional<GuestId>& guest_id,
                          std::string* result,
                          bool require_startup_notify = false,
                          bool need_display = false,
                          bool ignore_space = false) {
  result->clear();
  for (const auto item : prefs) {
    if (require_startup_notify && !*item.second.GetDict().FindBool(
                                      guest_os::prefs::kAppStartupNotifyKey)) {
      continue;
    }

    if (need_display) {
      const std::optional<bool> no_display =
          item.second.GetDict().FindBool(guest_os::prefs::kAppNoDisplayKey);
      if (no_display && *no_display) {
        continue;
      }
    }

    // If guest_id is provided, also check that it matches. The guest_id is
    // considered matched if its vm_name and container_name matches
    // corresponding entries in the dictionary.
    if (guest_id && !MatchContainerDict(item.second, *guest_id)) {
      continue;
    }

    const base::Value* value = item.second.GetDict().Find(prefs_key);
    if (!value)
      continue;
    if (value->is_string()) {
      if (!MatchingString(std::string(search_value), value->GetString(),
                          ignore_space)) {
        continue;
      }
    } else if (value->is_dict()) {
      // Look at the unlocalized name to see if that matches.
      const std::string* str_value = value->GetDict().FindString("");
      if (!str_value || !MatchingString(std::string(search_value), *str_value,
                                        ignore_space)) {
        continue;
      }
    } else {
      continue;
    }

    if (!result->empty())
      return FindAppIdResult::NonUniqueMatch;
    *result = item.first;
  }

  if (!result->empty())
    return FindAppIdResult::UniqueMatch;
  return FindAppIdResult::NoMatch;
}

// For GuestOS |window_app_id|s which match the prefix of
// org.chromium.guest_os.<token>.*, return the guest token.
// The token should be one of the following:
// - For Crostini app windows: it is the container_token
// - For Bruschetta app windows: it is the container_token
// - For Borealis app windows: "borealis"
// - For all other guest app windows: "termina"
// Note that PluginVM does not match this prefix since it has a
// hard-coded window_app_id.
std::string GetGuestTokenForWindowId(const std::string* window_app_id) {
  if (!window_app_id ||
      !base::StartsWith(*window_app_id, kGuestOsWindowAppIdPrefix,
                        base::CompareCase::SENSITIVE)) {
    return std::string();
  }
  const auto token_start = kGuestOsWindowAppIdPrefix.size();
  // Find the first "." after the kGuestOsWindowAppIdPrefix
  const auto token_end = window_app_id->find(".", token_start);

  auto token = window_app_id->substr(token_start, token_end - token_start);

  return token;
}

std::string GetUnregisteredAppIdPrefix(const std::string& token) {
  if (token == kBorealisToken) {
    return borealis::kBorealisAnonymousPrefix;
  }

  // TODO(b/244651040): We should support other VMs, e.g. bruschetta.
  // For all other unregistered apps, default to "crostini:".
  return std::string(kCrostiniShelfIdPrefix);
}

}  // namespace

// The code follows these steps to identify apps and returns the first match:
// 1) If the |window_startup_id| is set, look for a matching desktop file id.
// 2) Ignore windows if the |window_app_id| is not set.
// 3) The |window_app_id| is prefixed by org.chromium.guest_os.<token>., so we
//    should be able to obtain a guest token from it. This will be used to find
//    a guest_id to which the app window belongs to. In the following steps, the
//    container_name and vm_name from the guest_id will be used to find a unique
//    match if available.
// 4) Remove the org.chromium.guest_os.<token>. prefix and use the remaining
//    string (the suffix) for the next steps.
// 5) If the suffix is prefixed by wayland., it's a native Wayland app. Look for
//    a matching desktop file id.
// 6) If the suffix from step 4 is prefixed by wmclass.:
// 6.1) Look for an app where StartupWMClass matches the remaining string.
// 6.2) Look for an app where the desktop file id matches the remaining string.
// 6.3) Look for an app where the unlocalized name matches the remaining
//      string. This handles the xterm & uxterm examples.
// 7) If we couldn't find a match, prefix the |window_app_id| with a generic
//    prefix of 'crostini:' or 'borealis:"', so we can easily identify
//    shelf entries as GuestOs apps. If we could not identify the VM, default
//    to using "crostini:".
std::string GetGuestOsShelfAppId(Profile* profile,
                                 const std::string* window_app_id,
                                 const std::string* window_startup_id) {
  if (!profile || !profile->GetPrefs())
    return std::string();

  const base::Value::Dict& apps =
      profile->GetPrefs()->GetDict(guest_os::prefs::kGuestOsRegistry);

  // TODO(b/244651040): Consider moving the borealis GetBorealisAppId logic
  // here.
  std::string app_id;

  std::string token = GetGuestTokenForWindowId(window_app_id);
  std::optional<GuestId> guest_id =
      GuestOsSessionTrackerFactory::GetForProfile(profile)->GetGuestIdForToken(
          token);

  if (window_startup_id) {
    if (FindAppId(apps, guest_os::prefs::kAppDesktopFileIdKey,
                  *window_startup_id, guest_id, &app_id,
                  true) == FindAppIdResult::UniqueMatch) {
      return app_id;
    }
    LOG(WARNING) << "Startup ID was set to '" << *window_startup_id
                 << "' but not matched. Will attempt to match with window ID.";
  }

  if (!window_app_id) {
    return std::string();
  }

  // If the window_id does not follow the expected format, return a generic id.
  if (!base::StartsWith(*window_app_id, kGuestOsWindowAppIdPrefix,
                        base::CompareCase::SENSITIVE)) {
    LOG(ERROR) << "window_app_id:" << *window_app_id
               << " provided is not prefixed with "
               << kGuestOsWindowAppIdPrefix;
    return GetUnregisteredAppIdPrefix(token) + *window_app_id;
  }

  // Get the suffix by stripping "org.chromium.guest_os.<token>.".
  // token.length() + 1 is used since the '.' separator was not included in the
  // token.
  std::string_view suffix =
      std::string_view(*window_app_id)
          .substr(kGuestOsWindowAppIdPrefix.size() + token.length() + 1);

  // Wayland apps will have a "wayland." identifier.
  std::optional<std::string_view> wayland_app =
      base::RemovePrefix(suffix, kWaylandPrefix);
  if (wayland_app) {
    if (FindAppId(apps, guest_os::prefs::kAppDesktopFileIdKey, *wayland_app,
                  guest_id, &app_id) == FindAppIdResult::UniqueMatch) {
      return app_id;
    }
    return GetUnregisteredAppIdPrefix(token) + *window_app_id;
  }

  // If we don't have an id to match to a desktop file, use the window app id.
  std::optional<std::string_view> key =
      base::RemovePrefix(suffix, kWmClassPrefix);
  if (!key) {
    return GetUnregisteredAppIdPrefix(token) + *window_app_id;
  }

  // If an app had StartupWMClass set to the given WM class, use that,
  // otherwise look for a desktop file id matching the WM class.
  FindAppIdResult result = FindAppId(
      apps, guest_os::prefs::kAppStartupWMClassKey, *key, guest_id, &app_id,
      false /* require_startup_notification */, true /* need_display */);
  if (result == FindAppIdResult::UniqueMatch) {
    return app_id;
  }
  if (result == FindAppIdResult::NonUniqueMatch) {
    return GetUnregisteredAppIdPrefix(token) + *window_app_id;
  }

  if (FindAppId(apps, guest_os::prefs::kAppDesktopFileIdKey, *key, guest_id,
                &app_id) == FindAppIdResult::UniqueMatch) {
    return app_id;
  }

  if (FindAppId(apps, guest_os::prefs::kAppNameKey, *key, guest_id, &app_id,
                false /* require_startup_notification */,
                true /* need_display */,
                true /* ignore_space */) == FindAppIdResult::UniqueMatch) {
    return app_id;
  }

  const std::string* app_name = GetAppNameForWMClass(*key);
  if (app_name &&
      FindAppId(apps, guest_os::prefs::kAppNameKey, *app_name, guest_id,
                &app_id, false /* require_startup_notification */,
                true /* need_display */) == FindAppIdResult::UniqueMatch) {
    return app_id;
  }

  return GetUnregisteredAppIdPrefix(token) + *window_app_id;
}

bool IsUnregisteredCrostiniShelfAppId(std::string_view shelf_app_id) {
  return base::StartsWith(shelf_app_id, kCrostiniShelfIdPrefix,
                          base::CompareCase::SENSITIVE);
}

bool IsUnregisteredGuestOsShelfAppId(std::string_view shelf_app_id) {
  return IsUnregisteredCrostiniShelfAppId(shelf_app_id) ||
         base::StartsWith(shelf_app_id, borealis::kBorealisAnonymousPrefix,
                          base::CompareCase::SENSITIVE);
}

bool IsCrostiniShelfAppId(const Profile* profile,
                          std::string_view shelf_app_id) {
  if (IsUnregisteredCrostiniShelfAppId(shelf_app_id)) {
    return true;
  }

  if (!profile || !profile->GetPrefs()) {
    return false;
  }
  // TODO(timloh): We need to handle desktop files that have been removed.
  // For example, running windows with a no-longer-valid app id will try to
  // use the ExtensionContextMenuModel.
  const auto& apps =
      profile->GetPrefs()->GetDict(guest_os::prefs::kGuestOsRegistry);
  return apps.contains(shelf_app_id);
}

apps::AppType GetAppType(Profile* profile, std::string_view shelf_app_id) {
  if (shelf_app_id.starts_with(kCrostiniShelfIdPrefix)) {
    shelf_app_id.remove_prefix(kCrostiniShelfIdPrefix.size());
  }
  const std::string id(shelf_app_id);
  const std::string token = GetGuestTokenForWindowId(&id);
  std::optional<GuestId> guest_id =
      GuestOsSessionTrackerFactory::GetForProfile(profile)->GetGuestIdForToken(
          token);
  if (guest_id.has_value()) {
    return ToAppType(guest_id->vm_type);
  }
  return ToAppType(vm_tools::apps::UNKNOWN);
}

}  // namespace guest_os