File: shelf_controller_helper.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 (503 lines) | stat: -rw-r--r-- 17,984 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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
// Copyright 2012 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/ui/ash/shelf/shelf_controller_helper.h"

#include <optional>
#include <vector>

#include "ash/constants/ash_features.h"
#include "ash/public/cpp/shelf_types.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/apps/app_service/app_service_proxy.h"
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chrome/browser/apps/app_service/launch_utils.h"
#include "chrome/browser/apps/app_service/package_id_util.h"
#include "chrome/browser/apps/app_service/promise_apps/promise_app.h"
#include "chrome/browser/apps/app_service/promise_apps/promise_app_registry_cache.h"
#include "chrome/browser/apps/app_service/promise_apps/promise_app_service.h"
#include "chrome/browser/apps/browser_instance/web_contents_instance_id_utils.h"
#include "chrome/browser/ash/app_list/arc/arc_app_list_prefs.h"
#include "chrome/browser/ash/app_list/arc/arc_app_utils.h"
#include "chrome/browser/ash/arc/arc_util.h"
#include "chrome/browser/ash/arc/session/arc_session_manager.h"
#include "chrome/browser/ash/crostini/crostini_features.h"
#include "chrome/browser/ash/crostini/crostini_util.h"
#include "chrome/browser/ash/guest_os/guest_os_shelf_utils.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/ash/shelf/arc_app_shelf_id.h"
#include "chrome/browser/ui/ash/shelf/chrome_shelf_controller_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/extensions/app_launch_params.h"
#include "chrome/browser/ui/extensions/application_launch.h"
#include "chrome/browser/ui/extensions/extension_enable_flow.h"
#include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
#include "chrome/grit/generated_resources.h"
#include "chromeos/ash/experiences/arc/app/arc_app_constants.h"
#include "chromeos/ash/experiences/arc/arc_util.h"
#include "chromeos/ash/experiences/arc/metrics/arc_metrics_constants.h"
#include "components/services/app_service/public/cpp/app_launch_util.h"
#include "components/services/app_service/public/cpp/app_types.h"
#include "components/services/app_service/public/cpp/shortcut/shortcut.h"
#include "components/services/app_service/public/cpp/shortcut/shortcut_registry_cache.h"
#include "components/services/app_service/public/cpp/types_util.h"
#include "content/public/browser/navigation_entry.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_util.h"
#include "net/base/url_util.h"
#include "ui/base/l10n/l10n_util.h"

namespace {

constexpr float kProgressNone = 0;
constexpr float kProgressNotApplicable = -1;

std::string GetSourceFromAppListSource(ash::ShelfLaunchSource source) {
  switch (source) {
    case ash::LAUNCH_FROM_APP_LIST:
      return std::string(extension_urls::kLaunchSourceAppList);
    case ash::LAUNCH_FROM_APP_LIST_SEARCH:
      return std::string(extension_urls::kLaunchSourceAppListSearch);
    default:
      return std::string();
  }
}

}  // namespace

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

ShelfControllerHelper::~ShelfControllerHelper() = default;

std::u16string ShelfControllerHelper::GetLabelForPromiseStatus(
    apps::PromiseStatus status) {
  switch (status) {
    case apps::PromiseStatus::kUnknown:
    case apps::PromiseStatus::kPending:
      return l10n_util::GetStringUTF16(IDS_PROMISE_STATUS_WAITING);
    case apps::PromiseStatus::kInstalling:
    case apps::PromiseStatus::kSuccess:
    case apps::PromiseStatus::kCancelled:
      return l10n_util::GetStringUTF16(IDS_PROMISE_STATUS_INSTALLING);
  }
}

std::u16string ShelfControllerHelper::GetAccessibleLabelForPromiseStatus(
    std::optional<std::string> name,
    apps::PromiseStatus status) {
  switch (status) {
    case apps::PromiseStatus::kUnknown:
    case apps::PromiseStatus::kPending:
      if (!name.has_value()) {
        return l10n_util::GetStringUTF16(
            IDS_PROMISE_APP_PLACEHOLDER_ACCESSIBLE_LABEL_WAITING);
      }
      return l10n_util::GetStringFUTF16(
          IDS_PROMISE_APP_ACCESSIBLE_LABEL_WAITING,
          {base::UTF8ToUTF16(name.value())});
    case apps::PromiseStatus::kInstalling:
    case apps::PromiseStatus::kSuccess:
    case apps::PromiseStatus::kCancelled:
      if (!name.has_value()) {
        return l10n_util::GetStringUTF16(
            IDS_PROMISE_APP_PLACEHOLDER_ACCESSIBLE_LABEL_INSTALLING);
      }
      return l10n_util::GetStringFUTF16(
          IDS_PROMISE_APP_ACCESSIBLE_LABEL_INSTALLING,
          {base::UTF8ToUTF16(name.value())});
  }
}

// static
std::u16string ShelfControllerHelper::GetAppTitle(Profile* profile,
                                                  const std::string& app_id) {
  if (app_id.empty()) {
    return std::u16string();
  }

  // Get the title if the app is an ARC app. ARC shortcuts could call this
  // function when it's created, so AppService can't be used for ARC shortcuts,
  // because AppService is async.
  if (arc::IsArcItem(profile, app_id)) {
    std::unique_ptr<ArcAppListPrefs::AppInfo> app_info =
        ArcAppListPrefs::Get(profile)->GetApp(
            arc::ArcAppShelfId::FromString(app_id).app_id());
    DCHECK(app_info.get());
    return base::UTF8ToUTF16(app_info->name);
  }

  std::string name;
  apps::AppServiceProxyFactory::GetForProfile(profile)
      ->AppRegistryCache()
      .ForOneApp(app_id, [&name](const apps::AppUpdate& update) {
        if (apps_util::IsInstalled(update.Readiness())) {
          name = update.Name();
        }
      });
  if (!name.empty()) {
    return base::UTF8ToUTF16(name);
  }

  if (ash::features::ArePromiseIconsEnabled()) {
    const std::u16string promise_app_title =
        GetPromiseAppTitle(profile, app_id);
    if (!promise_app_title.empty()) {
      return promise_app_title;
    }
  }

  // Get the title for the extension which is not managed by AppService.
  extensions::ExtensionRegistry* registry =
      extensions::ExtensionRegistry::Get(profile);
  if (!registry) {
    return std::u16string();
  }

  auto* extension = registry->GetExtensionById(
      app_id, extensions::ExtensionRegistry::EVERYTHING);
  if (extension && extension->is_extension()) {
    return base::UTF8ToUTF16(extension->name());
  }

  return std::u16string();
}

std::u16string ShelfControllerHelper::GetPromiseAppAccessibleName(
    Profile* profile,
    const std::string& package_id) {
  if (!ash::features::ArePromiseIconsEnabled()) {
    return std::u16string();
  }
  const apps::PromiseApp* promise_app =
      apps::AppServiceProxyFactory::GetForProfile(profile)
          ->PromiseAppRegistryCache()
          ->GetPromiseAppForStringPackageId(package_id);
  if (!promise_app) {
    return std::u16string();
  }
  return GetAccessibleLabelForPromiseStatus(promise_app->name,
                                            promise_app->status);
}

// static
std::string ShelfControllerHelper::GetAppPackageId(Profile* profile,
                                                   const std::string& app_id) {
  if (app_id.empty()) {
    return std::string();
  }

  if (ash::features::ArePromiseIconsEnabled()) {
    const apps::PromiseApp* promise_app =
        apps::AppServiceProxyFactory::GetForProfile(profile)
            ->PromiseAppRegistryCache()
            ->GetPromiseAppForStringPackageId(app_id);
    if (promise_app) {
      return promise_app->package_id.ToString();
    }
  }

  std::optional<apps::PackageId> package_id;
  apps::AppServiceProxyFactory::GetForProfile(profile)
      ->AppRegistryCache()
      .ForOneApp(app_id, [&package_id, profile](const apps::AppUpdate& update) {
        if (apps_util::IsInstalled(update.Readiness())) {
          package_id = apps_util::GetPackageIdForApp(profile, update);
        }
      });
  if (package_id) {
    return package_id->ToString();
  }

  return std::string();
}

// static
ash::AppStatus ShelfControllerHelper::GetAppStatus(Profile* profile,
                                                   const std::string& app_id) {
  ash::AppStatus status = ash::AppStatus::kReady;

  if (!apps::AppServiceProxyFactory::IsAppServiceAvailableForProfile(profile)) {
    return status;
  }

  if (ash::features::ArePromiseIconsEnabled()) {
    const apps::PromiseApp* promise_app =
        apps::AppServiceProxyFactory::GetForProfile(profile)
            ->PromiseAppRegistryCache()
            ->GetPromiseAppForStringPackageId(app_id);
    if (promise_app) {
      return ConvertPromiseStatusToAppStatus(promise_app->status);
    }
  }

  apps::AppServiceProxyFactory::GetForProfile(profile)
      ->AppRegistryCache()
      .ForOneApp(app_id, [&status](const apps::AppUpdate& update) {
        if (apps_util::IsDisabled(update.Readiness())) {
          status = ash::AppStatus::kBlocked;
        } else if (update.Paused().value_or(false)) {
          status = ash::AppStatus::kPaused;
        }
      });

  return status;
}

// static
bool ShelfControllerHelper::IsAppDefaultInstalled(Profile* profile,
                                                  const std::string& app_id) {
  if (!apps::AppServiceProxyFactory::IsAppServiceAvailableForProfile(profile)) {
    return false;
  }

  bool default_installed = false;
  apps::AppServiceProxyFactory::GetForProfile(profile)
      ->AppRegistryCache()
      .ForOneApp(app_id, [&default_installed](const apps::AppUpdate& update) {
        default_installed =
            update.InstallReason() == apps::InstallReason::kDefault ||
            update.InstallReason() == apps::InstallReason::kSystem;
      });
  return default_installed;
}

std::string ShelfControllerHelper::GetAppID(content::WebContents* tab) {
  DCHECK(tab);
  return apps::GetInstanceAppIdForWebContents(tab).value_or(std::string());
}

std::u16string ShelfControllerHelper::GetPromiseAppTitle(
    Profile* profile,
    const std::string& string_package_id) {
  if (!apps::AppServiceProxyFactory::IsAppServiceAvailableForProfile(profile)) {
    return std::u16string();
  }

  const apps::PromiseApp* promise_app =
      apps::AppServiceProxyFactory::GetForProfile(profile)
          ->PromiseAppRegistryCache()
          ->GetPromiseAppForStringPackageId(string_package_id);
  if (!promise_app) {
    return std::u16string();
  }

  return GetLabelForPromiseStatus(promise_app->status);
}

// static
float ShelfControllerHelper::GetPromiseAppProgress(
    Profile* profile,
    const std::string& string_package_id) {
  if (!apps::AppServiceProxyFactory::IsAppServiceAvailableForProfile(profile)) {
    return kProgressNotApplicable;
  }
  const apps::PromiseApp* promise_app =
      apps::AppServiceProxyFactory::GetForProfile(profile)
          ->PromiseAppRegistryCache()
          ->GetPromiseAppForStringPackageId(string_package_id);
  if (!promise_app) {
    return kProgressNotApplicable;
  }
  if (!promise_app->progress.has_value()) {
    return kProgressNone;
  }
  return promise_app->progress.value();
}

// static
bool ShelfControllerHelper::IsPromiseApp(Profile* profile,
                                         const std::string& id) {
  if (!ash::features::ArePromiseIconsEnabled()) {
    return false;
  }
  return apps::AppServiceProxyFactory::GetForProfile(profile)
      ->PromiseAppRegistryCache()
      ->GetPromiseAppForStringPackageId(id);
}

// static
ash::AppStatus ShelfControllerHelper::ConvertPromiseStatusToAppStatus(
    apps::PromiseStatus promise_status) {
  switch (promise_status) {
    case apps::PromiseStatus::kUnknown:
      // Fallthrough.
    case apps::PromiseStatus::kPending:
      return ash::AppStatus::kPending;
    case apps::PromiseStatus::kInstalling:
      return ash::AppStatus::kInstalling;
    case apps::PromiseStatus::kSuccess:
      return ash::AppStatus::kInstallSuccess;
    case apps::PromiseStatus::kCancelled:
      return ash::AppStatus::kInstallCancelled;
  }
}

bool ShelfControllerHelper::IsValidIDForCurrentUser(
    const std::string& app_id) const {
  if (IsValidIDForArcApp(app_id)) {
    return true;
  }

  return IsValidIDFromAppService(app_id);
}

void ShelfControllerHelper::LaunchApp(const ash::ShelfID& id,
                                      ash::ShelfLaunchSource source,
                                      int event_flags,
                                      int64_t display_id,
                                      bool new_window) {
  const std::string& app_id = id.app_id;
  apps::AppServiceProxy* proxy =
      apps::AppServiceProxyFactory::GetForProfile(profile_);

  // Launch apps with AppServiceProxy.Launch.
  if (proxy->AppRegistryCache().GetAppType(app_id) != apps::AppType::kUnknown) {
    if (new_window) {
      apps::LaunchContainer container =
          apps::LaunchContainer::kLaunchContainerNone;
      proxy->AppRegistryCache().ForOneApp(
          app_id, [&](const apps::AppUpdate& update) {
            container = apps::ConvertWindowModeToAppLaunchContainer(
                update.WindowMode());
          });
      // TODO(b/310775293): Make this CHECK(launches_in_window), currently this
      // could be false due to the ash::LAUNCH_NEW shelf command being used for
      // both "New window" and "New tab".
      switch (container) {
        case apps::LaunchContainer::kLaunchContainerWindow:
          // TODO(b/310775293): Add test for this behaviour.
          // event_flags are ignored here because they control the disposition
          // which in this case has been overridden to always open a new window.
          proxy->LaunchAppWithParams(apps::AppLaunchParams(
              app_id, container, WindowOpenDisposition::NEW_WINDOW,
              ShelfLaunchSourceToAppsLaunchSource(source), display_id));
          return;
        case apps::LaunchContainer::kLaunchContainerPanelDeprecated:
        case apps::LaunchContainer::kLaunchContainerTab:
        case apps::LaunchContainer::kLaunchContainerNone:
          break;
      }
    }
    proxy->Launch(app_id, event_flags,
                  ShelfLaunchSourceToAppsLaunchSource(source),
                  std::make_unique<apps::WindowInfo>(display_id));
    return;
  }

  // Handle user selects promise app from Shelf
  if (IsPromiseApp(profile_, app_id)) {
    proxy->PromiseAppService()->UpdateInstallPriority(app_id);
    return;
  }

  // For extensions, Launch with AppServiceProxy.LaunchAppWithParams.

  // |extension| could be null when it is being unloaded for updating.
  const extensions::Extension* extension =
      extensions::ExtensionRegistry::Get(profile_)->GetExtensionById(
          app_id, extensions::ExtensionRegistry::EVERYTHING);
  if (!extension) {
    return;
  }

  if (!extensions::util::IsAppLaunchableWithoutEnabling(app_id, profile_)) {
    // Do nothing if there is already a running enable flow.
    if (extension_enable_flow_) {
      return;
    }

    extension_enable_flow_ =
        std::make_unique<ExtensionEnableFlow>(profile_, app_id, this);
    extension_enable_flow_->StartForNativeWindow(nullptr);
    return;
  }

  apps::AppLaunchParams params = CreateAppLaunchParamsWithEventFlags(
      profile_, extension, event_flags,
      ShelfLaunchSourceToAppsLaunchSource(source), display_id);
  if ((source == ash::LAUNCH_FROM_APP_LIST ||
       source == ash::LAUNCH_FROM_APP_LIST_SEARCH) &&
      app_id == extensions::kWebStoreAppId) {
    // Get the corresponding source string.
    std::string source_value = GetSourceFromAppListSource(source);

    // Set an override URL to include the source.
    GURL extension_url = extensions::AppLaunchInfo::GetFullLaunchURL(extension);
    params.override_url = net::AppendQueryParameter(
        extension_url, extension_urls::kWebstoreSourceField, source_value);
  }
  params.launch_id = id.launch_id;

  ::OpenApplication(profile_, std::move(params));
}

ArcAppListPrefs* ShelfControllerHelper::GetArcAppListPrefs() const {
  return ArcAppListPrefs::Get(profile_);
}

void ShelfControllerHelper::ExtensionEnableFlowFinished() {
  LaunchApp(ash::ShelfID(extension_enable_flow_->extension_id()),
            ash::LAUNCH_FROM_UNKNOWN, ui::EF_NONE, display::kInvalidDisplayId,
            /*new_window=*/false);
  extension_enable_flow_.reset();
}

void ShelfControllerHelper::ExtensionEnableFlowAborted(bool user_initiated) {
  extension_enable_flow_.reset();
}

bool ShelfControllerHelper::IsValidIDForArcApp(
    const std::string& app_id) const {
  const ArcAppListPrefs* arc_prefs = GetArcAppListPrefs();
  if (arc_prefs && arc_prefs->IsRegistered(app_id)) {
    return true;
  }

  if (app_id == arc::kPlayStoreAppId) {
    if (!arc::IsArcAllowedForProfile(profile()) ||
        !arc::IsPlayStoreAvailable()) {
      return false;
    }
    const arc::ArcSessionManager* arc_session_manager =
        arc::ArcSessionManager::Get();
    DCHECK(arc_session_manager);
    if (!arc_session_manager->IsAllowed()) {
      return false;
    }
    if (!arc::IsArcPlayStoreEnabledForProfile(profile()) &&
        arc::IsArcPlayStoreEnabledPreferenceManagedForProfile(profile())) {
      return false;
    }
    extensions::ExtensionRegistry* registry =
        extensions::ExtensionRegistry::Get(profile_);
    if (!registry->GetInstalledExtension(arc::kPlayStoreAppId)) {
      return false;
    }
    return true;
  }

  return false;
}

bool ShelfControllerHelper::IsValidIDFromAppService(
    const std::string& app_id) const {
  if (guest_os::IsUnregisteredCrostiniShelfAppId(app_id)) {
    return true;
  }

  bool is_valid = false;
  apps::AppServiceProxyFactory::GetForProfile(profile_)
      ->AppRegistryCache()
      .ForOneApp(app_id, [&is_valid](const apps::AppUpdate& update) {
        if (update.AppType() != apps::AppType::kArc &&
            update.AppType() != apps::AppType::kUnknown &&
            update.Readiness() != apps::Readiness::kUnknown &&
            apps_util::IsInstalled(update.Readiness())) {
          is_valid = true;
        }
      });

  return is_valid;
}