File: web_app_browser_controller.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 (750 lines) | stat: -rw-r--r-- 25,617 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
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
// Copyright 2015 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/web_applications/web_app_browser_controller.h"

#include "ash/constants/web_app_id_constants.h"
#include "base/callback_list.h"
#include "base/check_is_test.h"
#include "base/containers/flat_set.h"
#include "base/feature_list.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/strcat.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.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/ash/browser_delegate/browser_controller.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/browser_window/public/browser_window_interface.h"
#include "chrome/browser/ui/tabs/tab_menu_model_factory.h"
#include "chrome/browser/ui/web_applications/web_app_launch_utils.h"
#include "chrome/browser/ui/web_applications/web_app_tabbed_utils.h"
#include "chrome/browser/web_applications/locks/app_lock.h"
#include "chrome/browser/web_applications/web_app_command_scheduler.h"
#include "chrome/browser/web_applications/web_app_constants.h"
#include "chrome/browser/web_applications/web_app_helpers.h"
#include "chrome/browser/web_applications/web_app_icon_manager.h"
#include "chrome/browser/web_applications/web_app_install_manager.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/browser/web_applications/web_app_registrar.h"
#include "chrome/browser/web_applications/web_app_sync_bridge.h"
#include "chrome/browser/web_applications/web_app_tab_helper.h"
#include "chrome/browser/web_applications/web_app_ui_manager.h"
#include "chrome/grit/generated_resources.h"
#include "components/password_manager/core/common/password_manager_features.h"
#include "components/services/app_service/public/cpp/app_registry_cache.h"
#include "components/services/app_service/public/cpp/app_types.h"
#include "components/webapps/browser/installable/installable_metrics.h"
#include "components/webapps/common/web_app_id.h"
#include "content/public/browser/site_isolation_policy.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_features.h"
#include "third_party/blink/public/common/features.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/favicon_size.h"
#include "ui/gfx/image/image.h"
#include "ui/native_theme/native_theme.h"
#include "url/gurl.h"
#include "url/origin.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "ash/constants/ash_features.h"
#include "chrome/browser/ash/apps/apk_web_app_service.h"
#include "chrome/browser/ash/system_web_apps/color_helpers.h"
#include "chrome/browser/web_applications/chromeos_web_app_experiments.h"
#include "chrome/common/chrome_features.h"
#include "chromeos/ash/experiences/system_web_apps/types/system_web_app_delegate.h"
#include "chromeos/constants/chromeos_features.h"
#endif

namespace web_app {

namespace {

#if BUILDFLAG(IS_CHROMEOS)
constexpr char kRelationship[] = "delegate_permission/common.handle_all_urls";

// SystemWebAppDelegate provides menu.
class SystemAppTabMenuModelFactory : public TabMenuModelFactory {
 public:
  explicit SystemAppTabMenuModelFactory(
      const ash::SystemWebAppDelegate* system_app)
      : system_app_(system_app) {}
  SystemAppTabMenuModelFactory(const SystemAppTabMenuModelFactory&) = delete;
  SystemAppTabMenuModelFactory& operator=(const SystemAppTabMenuModelFactory&) =
      delete;
  ~SystemAppTabMenuModelFactory() override = default;

  std::unique_ptr<ui::SimpleMenuModel> Create(
      ui::SimpleMenuModel::Delegate* delegate,
      TabMenuModelDelegate* tab_menu_model_delegate,
      TabStripModel*,
      int) override {
    return system_app_->GetTabMenuModel(delegate);
  }

 private:
  const raw_ptr<const ash::SystemWebAppDelegate> system_app_;
};
#endif  // BUILDFLAG(IS_CHROMEOS)

base::OnceClosure& IconLoadCallbackForTesting() {
  static base::NoDestructor<base::OnceClosure> callback;
  return *callback;
}

base::OnceClosure& ManifestUpdateAppliedCallbackForTesting() {
  static base::NoDestructor<base::OnceClosure> callback;
  return *callback;
}

}  // namespace

WebAppBrowserController::WebAppBrowserController(
    WebAppProvider& provider,
    Browser* browser,
    webapps::AppId app_id,
#if BUILDFLAG(IS_CHROMEOS)
    const ash::SystemWebAppDelegate* system_app,
#endif  // BUILDFLAG(IS_CHROMEOS)
    bool has_tab_strip)
    : AppBrowserController(browser, std::move(app_id), has_tab_strip),
      provider_(provider)
#if BUILDFLAG(IS_CHROMEOS)
      ,
      system_app_(system_app)
#endif  // BUILDFLAG(IS_CHROMEOS)
{
  effective_display_mode_ =
      registrar().GetAppEffectiveDisplayMode(this->app_id());
  install_manager_observation_.Observe(&provider.install_manager());
  PerformDigitalAssetLinkVerification(browser);
}

WebAppBrowserController::~WebAppBrowserController() = default;

bool WebAppBrowserController::HasMinimalUiButtons() const {
  if (has_tab_strip()) {
    return false;
  }
  return effective_display_mode_ == DisplayMode::kMinimalUi;
}

bool WebAppBrowserController::IsHostedApp() const {
  return true;
}

std::unique_ptr<TabMenuModelFactory>
WebAppBrowserController::GetTabMenuModelFactory() const {
#if BUILDFLAG(IS_CHROMEOS)
  if (system_app() && system_app()->HasCustomTabMenuModel()) {
    return std::make_unique<SystemAppTabMenuModelFactory>(system_app());
  }
#endif  // BUILDFLAG(IS_CHROMEOS)
  return nullptr;
}

bool WebAppBrowserController::AppUsesWindowControlsOverlay() const {
  return effective_display_mode_ == DisplayMode::kWindowControlsOverlay;
}

bool WebAppBrowserController::IsWindowControlsOverlayEnabled() const {
  return AppUsesWindowControlsOverlay() &&
         registrar().GetWindowControlsOverlayEnabled(app_id());
}

void WebAppBrowserController::ToggleWindowControlsOverlayEnabled(
    base::OnceClosure on_complete) {
  DCHECK(AppUsesWindowControlsOverlay());

  provider_->scheduler().ScheduleCallback(
      "WebAppBrowserController::ToggleWindowControlsOverlayEnabled",
      AppLockDescription(app_id()),
      base::BindOnce(
          [](const webapps::AppId& app_id, AppLock& lock,
             base::Value::Dict& debug_value) {
            lock.sync_bridge().SetAppWindowControlsOverlayEnabled(
                app_id,
                !lock.registrar().GetWindowControlsOverlayEnabled(app_id));
          },
          app_id()),
      /*on_complete=*/std::move(on_complete));
}

bool WebAppBrowserController::AppUsesBorderlessMode() const {
  return IsIsolatedWebApp() &&
         effective_display_mode_ == DisplayMode::kBorderless;
}

bool WebAppBrowserController::AppUsesTabbed() const {
  if (!base::FeatureList::IsEnabled(blink::features::kDesktopPWAsTabStrip)) {
    return false;
  }
  return effective_display_mode_ == DisplayMode::kTabbed;
}

bool WebAppBrowserController::IsIsolatedWebApp() const {
  return is_isolated_web_app_for_testing_ || registrar().IsIsolated(app_id());
}

void WebAppBrowserController::SetIsolatedWebAppTrueForTesting() {
  is_isolated_web_app_for_testing_ = true;
}

gfx::Rect WebAppBrowserController::GetDefaultBounds() const {
#if BUILDFLAG(IS_CHROMEOS)
  if (system_app_) {
    return system_app_->GetDefaultBounds(
        ash::BrowserController::GetInstance()->GetDelegate(browser()));
  }
#endif  // BUILDFLAG(IS_CHROMEOS)
  return gfx::Rect();
}

bool WebAppBrowserController::HasReloadButton() const {
#if BUILDFLAG(IS_CHROMEOS)
  if (system_app_) {
    return system_app_->ShouldHaveReloadButtonInMinimalUi();
  }
#endif  // BUILDFLAG(IS_CHROMEOS)
  return true;
}

#if !BUILDFLAG(IS_CHROMEOS)
bool WebAppBrowserController::HasProfileMenuButton() const {
#if BUILDFLAG(IS_MAC)
  return true;
#else
  return app_id() == ash::kPasswordManagerAppId;
#endif
}

bool WebAppBrowserController::IsProfileMenuButtonVisible() const {
  CHECK(HasProfileMenuButton());
  if (app_id() == ash::kPasswordManagerAppId) {
    return true;
  }
#if BUILDFLAG(IS_MAC)
  return AppShimRegistry::Get()->GetInstalledProfilesForApp(app_id()).size() >
         1;
#else
  NOTREACHED();
#endif
}
#endif  // !BUILDFLAG(IS_CHROMEOS)

#if BUILDFLAG(IS_CHROMEOS)
const ash::SystemWebAppDelegate* WebAppBrowserController::system_app() const {
  return system_app_;
}
#endif  // BUILDFLAG(IS_CHROMEOS)

#if BUILDFLAG(IS_MAC)
bool WebAppBrowserController::AlwaysShowToolbarInFullscreen() const {
  // Reading this setting synchronously rather than going through the command
  // manager greatly simplifies where this is read. This should be fine, since
  // this is only persisted in the web app db.
  return registrar().AlwaysShowToolbarInFullscreen(app_id());
}

void WebAppBrowserController::ToggleAlwaysShowToolbarInFullscreen() {
  provider_->scheduler().ScheduleCallback(
      "WebAppBrowserController::ToggleAlwaysShowToolbarInFullscreen",
      AppLockDescription(app_id()),
      base::BindOnce(
          [](const webapps::AppId& app_id, AppLock& lock,
             base::Value::Dict& debug_value) {
            lock.sync_bridge().SetAlwaysShowToolbarInFullscreen(
                app_id,
                !lock.registrar().AlwaysShowToolbarInFullscreen(app_id));
          },
          app_id()),
      /*on_complete=*/base::DoNothing());
}
#endif

#if BUILDFLAG(IS_CHROMEOS)
bool WebAppBrowserController::ShouldShowCustomTabBar() const {
  if (AppBrowserController::ShouldShowCustomTabBar()) {
    return true;
  }

  return is_verified_.value_or(false);
}

void WebAppBrowserController::CheckDigitalAssetLinkRelationshipForAndroidApp(
    const std::string& package_name,
    const std::string& fingerprint) {
  // base::Unretained is safe as |asset_link_handler_| is owned by this object
  // and will be destroyed if this object is destroyed.
  // TODO(swestphal): Support passing several fingerprints for verification.
  std::vector<std::string> fingerprints{fingerprint};
  asset_link_handler_->CheckDigitalAssetLinkRelationshipForAndroidApp(
      url::Origin::Create(GetAppStartUrl()), kRelationship,
      std::move(fingerprints), package_name,
      base::BindOnce(&WebAppBrowserController::OnRelationshipCheckComplete,
                     base::Unretained(this)));
}

void WebAppBrowserController::OnRelationshipCheckComplete(
    content_relationship_verification::RelationshipCheckResult result) {
  bool should_show_cct = false;
  switch (result) {
    case content_relationship_verification::RelationshipCheckResult::kSuccess:
      should_show_cct = false;
      break;
    case content_relationship_verification::RelationshipCheckResult::kFailure:
    case content_relationship_verification::RelationshipCheckResult::
        kNoConnection:
      should_show_cct = true;
      break;
  }
  is_verified_ = should_show_cct;
  browser()->window()->UpdateCustomTabBarVisibility(should_show_cct,
                                                    false /* animate */);
}
#endif  // BUILDFLAG(IS_CHROMEOS)

void WebAppBrowserController::OnWebAppUninstalled(
    const webapps::AppId& uninstalled_app_id,
    webapps::WebappUninstallSource uninstall_source) {
  if (uninstalled_app_id == app_id()) {
    chrome::CloseWindow(browser());
  }
}

void WebAppBrowserController::OnWebAppManifestUpdated(
    const webapps::AppId& updated_app_id) {
  if (updated_app_id == app_id()) {
    UpdateThemePack();
    app_icon_.reset();
    browser()->window()->UpdateTitleBar();

    if (ManifestUpdateAppliedCallbackForTesting()) {
      std::move(ManifestUpdateAppliedCallbackForTesting()).Run();
    }
  }
}

void WebAppBrowserController::OnWebAppInstallManagerDestroyed() {
  install_manager_observation_.Reset();
}

ui::ImageModel WebAppBrowserController::GetWindowAppIcon() const {
  if (app_icon_) {
    return *app_icon_;
  }
  app_icon_ = GetFallbackAppIcon();

#if BUILDFLAG(IS_CHROMEOS)
  if (apps::AppServiceProxyFactory::IsAppServiceAvailableForProfile(
          browser()->profile())) {
    LoadAppIcon(true /* allow_placeholder_icon */);
    return *app_icon_;
  }
#endif

  if (provider_->icon_manager().HasSmallestIcon(app_id(), {IconPurpose::ANY},
                                                kWebAppIconSmall)) {
    provider_->icon_manager().ReadSmallestIcon(
        app_id(), {IconPurpose::ANY}, kWebAppIconSmall,
        base::BindOnce(&WebAppBrowserController::OnReadIcon,
                       weak_ptr_factory_.GetWeakPtr()));
  }

  return *app_icon_;
}

gfx::ImageSkia WebAppBrowserController::GetHomeTabIcon() const {
  return provider_->icon_manager().GetMonochromeFavicon(app_id());
}

gfx::ImageSkia WebAppBrowserController::GetFallbackHomeTabIcon() const {
  return provider_->icon_manager().GetFaviconImageSkia(app_id());
}

ui::ImageModel WebAppBrowserController::GetWindowIcon() const {
  return GetWindowAppIcon();
}

std::optional<SkColor> WebAppBrowserController::GetThemeColor() const {
#if BUILDFLAG(IS_CHROMEOS)
  // System App popups (settings pages) always use default theme.
  if (system_app() && browser()->is_type_app_popup()) {
    return std::nullopt;
  }
#endif  // BUILDFLAG(IS_CHROMEOS)

  std::optional<SkColor> web_theme_color =
      AppBrowserController::GetThemeColor();
  if (web_theme_color) {
    return web_theme_color;
  }

#if BUILDFLAG(IS_CHROMEOS)
  if (chromeos::features::IsUploadOfficeToCloudEnabled() &&
      ChromeOsWebAppExperiments::IgnoreManifestColor(app_id())) {
    return std::nullopt;
  }

  // System Apps with dynamic color ignore manifest and pull theme color from
  // the OS.
  if (system_app() && system_app()->UseSystemThemeColor()) {
    return ash::GetSystemThemeColor();
  }
#endif  // BUILDFLAG(IS_CHROMEOS)

  if (ui::NativeTheme::GetInstanceForNativeUi()->ShouldUseDarkColors()) {
    std::optional<SkColor> dark_mode_color =
        registrar().GetAppDarkModeThemeColor(app_id());

    if (dark_mode_color) {
      return dark_mode_color;
    }
  }

  return registrar().GetAppThemeColor(app_id());
}

std::optional<SkColor> WebAppBrowserController::GetBackgroundColor() const {
  std::optional<SkColor> web_contents_color =
      AppBrowserController::GetBackgroundColor();
  std::optional<SkColor> manifest_color = GetResolvedManifestBackgroundColor();

#if BUILDFLAG(IS_CHROMEOS)
  if (chromeos::features::IsUploadOfficeToCloudEnabled() &&
      ChromeOsWebAppExperiments::IgnoreManifestColor(app_id())) {
    manifest_color = std::nullopt;
  }
#endif  // BUILDFLAG(IS_CHROMEOS)

  // Prefer an available web contents color but when such a color is
  // unavailable (i.e. in the time between when a window launches and it's web
  // content loads) attempt to pull the background color from the manifest.
  std::optional<SkColor> result =
      web_contents_color ? web_contents_color : manifest_color;

#if BUILDFLAG(IS_CHROMEOS)
  if (system_app() && system_app()->UseSystemThemeColor()) {
    // With jelly enabled, some system apps prefer system color over manifest.
    SkColor os_color = ash::GetSystemBackgroundColor();
    result = web_contents_color ? web_contents_color : os_color;
  }
#endif  // BUILDFLAG(IS_CHROMEOS)

  return result;
}

GURL WebAppBrowserController::GetAppStartUrl() const {
  return registrar().GetAppStartUrl(app_id());
}

GURL WebAppBrowserController::GetAppNewTabUrl() const {
  return registrar().GetAppNewTabUrl(app_id());
}

bool WebAppBrowserController::ShouldHideNewTabButton() const {
#if BUILDFLAG(IS_CHROMEOS)
  // Configure new tab button visibility for system apps based on their delegate
  // implementation.
  if (system_app() && system_app()->ShouldHaveTabStrip()) {
    return system_app()->ShouldHideNewTabButton();
  }
#endif  // BUILDFLAG(IS_CHROMEOS)

  if (!registrar().IsTabbedWindowModeEnabled(app_id())) {
    return true;
  }

  // If the app added a pinned home tab without changing their new tab URL, we
  // hide the new tab button to avoid the start_url being opened in a non home
  // tab.
  return IsUrlInHomeTabScope(GetAppNewTabUrl());
}

bool WebAppBrowserController::IsUrlInHomeTabScope(const GURL& url) const {
  return registrar().IsUrlInHomeTabScope(url, app_id());
}

bool WebAppBrowserController::ShouldShowAppIconOnTab(int index) const {
#if BUILDFLAG(IS_CHROMEOS)
  return !system_app() &&
         web_app::IsPinnedHomeTab(browser()->tab_strip_model(), index);
#else
  return web_app::IsPinnedHomeTab(browser()->tab_strip_model(), index);
#endif
}

bool WebAppBrowserController::IsUrlInAppScope(const GURL& url) const {
#if BUILDFLAG(IS_CHROMEOS)
  if (system_app() && system_app()->IsUrlInSystemAppScope(url)) {
    return true;
  }

  if (chromeos::features::IsUploadOfficeToCloudEnabled()) {
    size_t extended_scope_score =
        ChromeOsWebAppExperiments::GetExtendedScopeScore(app_id(), url.spec());
    if (extended_scope_score > 0) {
      return true;
    }
  }
#endif  // BUILDFLAG(IS_CHROMEOS)

  size_t app_extended_scope_score =
      registrar().GetAppExtendedScopeScore(url, app_id());
  if (app_extended_scope_score > 0) {
    return true;
  }

  GURL app_scope = registrar().GetAppScope(app_id());
  if (!app_scope.is_valid()) {
    return false;
  }

  // https://w3c.github.io/manifest/#navigation-scope
  // If url is same origin as scope and url path starts with scope path, return
  // true. Otherwise, return false.
  if (!url::IsSameOriginWith(app_scope, url)) {
    // We allow an upgrade from http |app_scope| to https |url|.
    if (app_scope.scheme() != url::kHttpScheme) {
      return false;
    }

    GURL::Replacements rep;
    rep.SetSchemeStr(url::kHttpsScheme);
    GURL secure_app_scope = app_scope.ReplaceComponents(rep);
    if (!url::IsSameOriginWith(secure_app_scope, url)) {
      return false;
    }
  }
  // Past here, the url and scope must be same-origin.

  // For scopes without paths, return 'true' early (allowing blobs to be in
  // scope).
  if (!app_scope.has_path() || app_scope.path() == "/") {
    return true;
  }
  if (url.scheme() == url::kBlobScheme) {
    // Blobs can only be in-scope in the above case where the app scope doesn't
    // have a path.
    return false;
  }
  std::string scope_path = app_scope.path();
  std::string url_path = url.path();
  return base::StartsWith(url_path, scope_path, base::CompareCase::SENSITIVE);
}

WebAppBrowserController* WebAppBrowserController::AsWebAppBrowserController() {
  return this;
}

std::u16string WebAppBrowserController::GetTitle() const {
  // When showing the toolbar, display the name of the app, instead of the
  // current page as the title.
  if (ShouldShowCustomTabBar()) {
    // TODO(crbug.com/40118430): Use name instead of short_name.
    return base::UTF8ToUTF16(registrar().GetAppShortName(app_id()));
  }

  std::u16string raw_title = AppBrowserController::GetTitle();

  std::u16string app_name = base::UTF8ToUTF16(
      provider_->registrar_unsafe().GetAppShortName(app_id()));

  // If app title is set, then use that with the app name as the title.
  std::optional<std::u16string> application_title;
  content::WebContents* web_contents =
      browser()->tab_strip_model()->GetActiveWebContents();
  if (web_contents) {
    application_title = web_contents->GetApplicationTitle();
  }

  // If the app title is empty, then use the app name.
  if (application_title.has_value()) {
    return application_title.value().empty()
               ? app_name
               : l10n_util::GetStringFUTF16(IDS_WEB_APP_WITH_APP_TITLE,
                                            app_name,
                                            application_title.value());
  }
  if (base::StartsWith(raw_title, app_name)) {
    return raw_title;
  }

  if (raw_title.empty()) {
    return app_name;
  }

  return base::StrCat({app_name, u" - ", raw_title});
}

std::u16string WebAppBrowserController::GetAppShortName() const {
  return base::UTF8ToUTF16(registrar().GetAppShortName(app_id()));
}

std::u16string WebAppBrowserController::GetFormattedUrlOrigin() const {
  if (registrar().GetScopeExtensions(app_id()).empty()) {
    return FormatUrlOrigin(GetAppStartUrl());
  }

  CHECK(browser() != nullptr && browser()->tab_strip_model() != nullptr);
  content::WebContents* contents =
      browser()->tab_strip_model()->GetActiveWebContents();
  if (contents == nullptr) {
    return FormatUrlOrigin(GetAppStartUrl());
  }
  GURL last_committed_url = contents->GetLastCommittedURL();
  if (last_committed_url.is_empty()) {
    return FormatUrlOrigin(GetAppStartUrl());
  }
  return FormatUrlOrigin(last_committed_url);
}

bool WebAppBrowserController::CanUserUninstall() const {
  return registrar().CanUserUninstallWebApp(app_id());
}

void WebAppBrowserController::Uninstall(
    webapps::WebappUninstallSource webapp_uninstall_source) {
  provider_->ui_manager().PresentUserUninstallDialog(
      app_id(), webapps::WebappUninstallSource::kAppMenu, browser()->window(),
      base::DoNothing());
}

bool WebAppBrowserController::IsInstalled() const {
  return registrar().IsInstallState(
      app_id(), {proto::InstallState::SUGGESTED_FROM_ANOTHER_DEVICE,
                 proto::InstallState::INSTALLED_WITHOUT_OS_INTEGRATION,
                 proto::InstallState::INSTALLED_WITH_OS_INTEGRATION});
}

void WebAppBrowserController::SetIconLoadCallbackForTesting(
    base::OnceClosure callback) {
  IconLoadCallbackForTesting() = std::move(callback);
}

void WebAppBrowserController::SetManifestUpdateAppliedCallbackForTesting(
    base::OnceClosure callback) {
  ManifestUpdateAppliedCallbackForTesting() = std::move(callback);
}

void WebAppBrowserController::OnTabInserted(content::WebContents* contents) {
  AppBrowserController::OnTabInserted(contents);

  WebAppTabHelper* tab_helper = WebAppTabHelper::FromWebContents(contents);
  tab_helper->SetIsInAppWindow(app_id());

  if (AppUsesTabbed() && IsUrlInHomeTabScope(contents->GetLastCommittedURL())) {
    tab_helper->set_is_pinned_home_tab(true);
  }
}

void WebAppBrowserController::OnTabRemoved(content::WebContents* contents) {
  AppBrowserController::OnTabRemoved(contents);
  WebAppTabHelper::FromWebContents(contents)->SetIsInAppWindow(
      /*window_app_id=*/std::nullopt);
}

const WebAppRegistrar& WebAppBrowserController::registrar() const {
  return provider_->registrar_unsafe();
}

const WebAppInstallManager& WebAppBrowserController::install_manager() const {
  return provider_->install_manager();
}

void WebAppBrowserController::LoadAppIcon(bool allow_placeholder_icon) const {
  apps::AppServiceProxy* proxy =
      apps::AppServiceProxyFactory::GetForProfile(browser()->profile());
  proxy->LoadIcon(app_id(), apps::IconType::kStandard, kWebAppIconSmall,
                  allow_placeholder_icon,
                  base::BindOnce(&WebAppBrowserController::OnLoadIcon,
                                 weak_ptr_factory_.GetWeakPtr()));
}

void WebAppBrowserController::OnLoadIcon(apps::IconValuePtr icon_value) {
  if (!icon_value || icon_value->icon_type != apps::IconType::kStandard) {
    return;
  }

  app_icon_ = ui::ImageModel::FromImageSkia(icon_value->uncompressed);

  if (icon_value->is_placeholder_icon) {
    LoadAppIcon(false /* allow_placeholder_icon */);
  }

  if (auto* contents = web_contents()) {
    contents->NotifyNavigationStateChanged(content::INVALIDATE_TYPE_TAB);
  }
  if (IconLoadCallbackForTesting()) {
    std::move(IconLoadCallbackForTesting()).Run();
  }
}

void WebAppBrowserController::OnReadIcon(IconPurpose purpose, SkBitmap bitmap) {
  // We request only IconPurpose::ANY icons.
  DCHECK_EQ(purpose, IconPurpose::ANY);

  if (bitmap.empty()) {
    DLOG(ERROR) << "Failed to read icon for web app";
    return;
  }

  app_icon_ =
      ui::ImageModel::FromImageSkia(gfx::ImageSkia::CreateFrom1xBitmap(bitmap));
  if (auto* contents = web_contents()) {
    contents->NotifyNavigationStateChanged(content::INVALIDATE_TYPE_TAB);
  }
  if (IconLoadCallbackForTesting()) {
    std::move(IconLoadCallbackForTesting()).Run();
  }
}

void WebAppBrowserController::PerformDigitalAssetLinkVerification(
    Browser* browser) {
#if BUILDFLAG(IS_CHROMEOS)
  asset_link_handler_ = std::make_unique<
      content_relationship_verification::DigitalAssetLinksHandler>(
      browser->profile()->GetURLLoaderFactory());
  is_verified_ = std::nullopt;

  ash::ApkWebAppService* apk_web_app_service =
      ash::ApkWebAppService::Get(browser->profile());
  if (!apk_web_app_service || !apk_web_app_service->IsWebOnlyTwa(app_id())) {
    return;
  }

  const std::optional<std::string> package_name =
      apk_web_app_service->GetPackageNameForWebApp(app_id());
  const std::optional<std::string> fingerprint =
      apk_web_app_service->GetCertificateSha256Fingerprint(app_id());

  // Any web-only TWA should have an associated package name and fingerprint.
  DCHECK(package_name.has_value());
  DCHECK(fingerprint.has_value());

  CheckDigitalAssetLinkRelationshipForAndroidApp(*package_name, *fingerprint);
#endif
}

std::optional<SkColor>
WebAppBrowserController::GetResolvedManifestBackgroundColor() const {
  if (ui::NativeTheme::GetInstanceForNativeUi()->ShouldUseDarkColors()) {
    auto dark_mode_color = registrar().GetAppDarkModeBackgroundColor(app_id());
    if (dark_mode_color) {
      return dark_mode_color;
    }
  }
  return registrar().GetAppBackgroundColor(app_id());
}

}  // namespace web_app