File: chrome_pages.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (741 lines) | stat: -rw-r--r-- 28,822 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
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
// 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/chrome_pages.h"

#include <stddef.h>

#include <memory>
#include <string_view>

#include "ash/constants/ash_features.h"
#include "ash/webui/shortcut_customization_ui/url_constants.h"
#include "base/containers/fixed_flat_map.h"
#include "base/containers/map_util.h"
#include "base/feature_list.h"
#include "base/logging.h"
#include "base/metrics/user_metrics.h"
#include "base/strings/escape.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.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/bookmarks/bookmark_model_factory.h"
#include "chrome/browser/download/download_shelf.h"
#include "chrome/browser/file_system_access/file_system_access_features.h"
#include "chrome/browser/policy/profile_policy_connector.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/account_consistency_mode_manager.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_element_identifiers.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_navigator.h"
#include "chrome/browser/ui/browser_navigator_params.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/passwords/ui_utils.h"
#include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
#include "chrome/browser/ui/singleton_tabs.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/user_education/show_promo_in_page.h"
#include "chrome/browser/ui/webui/bookmarks/bookmarks_ui.h"
#include "chrome/browser/ui/webui/settings/site_settings_helper.h"
#include "chrome/browser/user_education/tutorial_identifiers.h"
#include "chrome/browser/user_education/user_education_service.h"
#include "chrome/browser/user_education/user_education_service_factory.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/url_constants.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/grit/generated_resources.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "components/bookmarks/browser/bookmark_node.h"
#include "components/commerce/core/commerce_constants.h"
#include "components/data_sharing/public/features.h"
#include "components/password_manager/core/common/password_manager_features.h"
#include "components/privacy_sandbox/privacy_sandbox_features.h"
#include "components/safe_browsing/core/common/safe_browsing_settings_metrics.h"
#include "components/safe_browsing/core/common/safebrowsing_referral_methods.h"
#include "components/signin/public/base/consent_level.h"
#include "content/public/browser/web_contents.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension_urls.h"
#include "net/base/url_util.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/window_open_disposition.h"
#include "url/url_util.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "ash/webui/connectivity_diagnostics/url_constants.h"
#include "ash/webui/settings/public/constants/routes.mojom.h"
#include "ash/webui/settings/public/constants/routes_util.h"
#include "chrome/browser/ui/ash/system_web_apps/system_web_app_ui_utils.h"
#include "chrome/browser/ui/settings_window_manager_chromeos.h"
#else
#include "chrome/browser/ui/signin/signin_view_controller.h"
#endif

#if !BUILDFLAG(IS_ANDROID)
#include "base/metrics/histogram_functions.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "components/signin/public/base/signin_pref_names.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#endif

#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
#include "chrome/browser/web_applications/web_app_utils.h"
#endif

using base::UserMetricsAction;

namespace chrome {
namespace {

const char kHashMark[] = "#";

void FocusWebContents(Browser* browser) {
  auto* const contents = browser->tab_strip_model()->GetActiveWebContents();
  if (contents) {
    contents->Focus();
  }
}

// Shows |url| in a tab in |browser|. If a tab is already open to |url|,
// ignoring the URL path, then that tab becomes selected. Overwrites the new tab
// page if it is open.
void ShowSingletonTabIgnorePathOverwriteNTP(Browser* browser, const GURL& url) {
  ShowSingletonTabOverwritingNTP(browser, url,
                                 NavigateParams::IGNORE_AND_NAVIGATE);
}

void OpenBookmarkManagerForNode(Browser* browser, int64_t node_id) {
  GURL url = GURL(kChromeUIBookmarksURL)
                 .Resolve(base::StringPrintf(
                     "/?id=%s", base::NumberToString(node_id).c_str()));
  ShowSingletonTabIgnorePathOverwriteNTP(browser, url);
}

#if BUILDFLAG(IS_CHROMEOS) && BUILDFLAG(GOOGLE_CHROME_BRANDING)
void LaunchReleaseNotesImpl(Profile* profile, apps::LaunchSource source) {
  base::RecordAction(UserMetricsAction("ReleaseNotes.ShowReleaseNotes"));
  ash::SystemAppLaunchParams params;
  params.url =
      base::FeatureList::IsEnabled(
          ash::features::kHelpAppOpensInsteadOfReleaseNotesNotification) &&
              source == apps::LaunchSource::kFromReleaseNotesNotification
          ? GURL("chrome://help-app/updates?launchSource=version-update")
          : GURL("chrome://help-app/updates");
  params.launch_source = source;
  LaunchSystemWebAppAsync(profile, ash::SystemWebAppType::HELP, params);
}
#endif

// Shows either the help app or the appropriate help page for |source|. If
// |browser| is NULL and the help page is used (vs the app), the help page is
// shown in the last active browser. If there is no such browser, a new browser
// is created.
void ShowHelpImpl(Browser* browser, Profile* profile, HelpSource source) {
  base::RecordAction(UserMetricsAction("ShowHelpTab"));
#if BUILDFLAG(IS_CHROMEOS) && BUILDFLAG(GOOGLE_CHROME_BRANDING)
  auto app_launch_source = apps::LaunchSource::kUnknown;
  switch (source) {
    case HELP_SOURCE_KEYBOARD:
      app_launch_source = apps::LaunchSource::kFromKeyboard;
      break;
    case HELP_SOURCE_MENU:
      app_launch_source = apps::LaunchSource::kFromMenu;
      break;
    case HELP_SOURCE_WEBUI:
    case HELP_SOURCE_WEBUI_CHROME_OS:
      app_launch_source = apps::LaunchSource::kFromOtherApp;
      break;
    default:
      NOTREACHED() << "Unhandled help source" << source;
  }

  ash::SystemAppLaunchParams params;
  params.launch_source = app_launch_source;
  LaunchSystemWebAppAsync(profile, ash::SystemWebAppType::HELP, params);
#else
  GURL url;
  switch (source) {
    case HELP_SOURCE_KEYBOARD:
      url = GURL(kChromeHelpViaKeyboardURL);
      break;
    case HELP_SOURCE_MENU:
      url = GURL(kChromeHelpViaMenuURL);
      break;
    case HELP_SOURCE_WEBHID:
      url = GURL(kChooserHidOverviewUrl);
      break;
#if BUILDFLAG(IS_CHROMEOS)
    case HELP_SOURCE_WEBUI:
      url = GURL(kChromeHelpViaWebUIURL);
      break;
    case HELP_SOURCE_WEBUI_CHROME_OS:
      url = GURL(kChromeOsHelpViaWebUIURL);
      break;
#else
    case HELP_SOURCE_WEBUI:
      url = GURL(kChromeHelpViaWebUIURL);
      break;
#endif  // BUILDFLAG(IS_CHROMEOS)
    case HELP_SOURCE_WEBUSB:
      url = GURL(kChooserUsbOverviewURL);
      break;
    default:
      NOTREACHED() << "Unhandled help source " << source;
  }
  if (browser) {
    ShowSingletonTab(browser, url);
  } else {
    ShowSingletonTab(profile, url);
  }
#endif  // BUILDFLAG(IS_CHROMEOS) && BUILDFLAG(GOOGLE_CHROME_BRANDING)
}

std::string GenerateContentSettingsExceptionsSubPage(ContentSettingsType type) {
  // In MD Settings, the exceptions no longer have a separate subpage.
  // This list overrides the group names defined in site_settings_helper for the
  // purposes of URL generation for MD Settings only. We need this because some
  // of the old group names are no longer appropriate.
  //
  // TODO(crbug.com/40523530): Update the group names defined in
  // site_settings_helper once Options is removed from Chrome. Then this list
  // will no longer be needed.

  static constexpr auto kSettingsPathOverrides =
      base::MakeFixedFlatMap<ContentSettingsType, std::string_view>({
          {ContentSettingsType::AUTOMATIC_DOWNLOADS, "automaticDownloads"},
          {ContentSettingsType::BACKGROUND_SYNC, "backgroundSync"},
          {ContentSettingsType::MEDIASTREAM_MIC, "microphone"},
          {ContentSettingsType::MEDIASTREAM_CAMERA, "camera"},
          {ContentSettingsType::MIDI_SYSEX, "midiDevices"},
          {ContentSettingsType::ADS, "ads"},
          {ContentSettingsType::HID_CHOOSER_DATA, "hidDevices"},
#if BUILDFLAG(IS_CHROMEOS)
          {ContentSettingsType::SMART_CARD_GUARD, "smartCardReaders"},
#endif
          {ContentSettingsType::STORAGE_ACCESS, "storageAccess"},
          {ContentSettingsType::USB_CHOOSER_DATA, "usbDevices"},
          {ContentSettingsType::WEB_PRINTING, "webPrinting"},
      });

  const std::string_view* override =
      base::FindOrNull(kSettingsPathOverrides, type);
  return base::StrCat(
      {kContentSettingsSubPage, "/",
       override ? *override
                : site_settings::ContentSettingsTypeToGroupName(type)});
}

bool SiteGURLIsValid(const GURL& url) {
  url::Origin site_origin = url::Origin::Create(url);
  // TODO(crbug.com/40399136): Site Details should work with file:// urls
  // when this bug is fixed, so add it to the allowlist when that happens.
  return !site_origin.opaque() && (url.SchemeIsHTTPOrHTTPS() ||
                                   url.SchemeIs(extensions::kExtensionScheme) ||
                                   url.SchemeIs(chrome::kIsolatedAppScheme));
}

void ShowSiteSettingsImpl(Browser* browser, Profile* profile, const GURL& url) {
  // If a valid non-file origin, open a settings page specific to the current
  // origin of the page. Otherwise, open Content Settings.
  constexpr char kParamRequest[] = "site";
  GURL link_destination = GetSettingsUrl(chrome::kContentSettingsSubPage);
  if (SiteGURLIsValid(url)) {
    std::string origin_string = url::Origin::Create(url).Serialize();
    link_destination =
        net::AppendQueryParameter(GetSettingsUrl(chrome::kSiteDetailsSubpage),
                                  kParamRequest, origin_string);
  }
  NavigateParams params(profile, link_destination, ui::PAGE_TRANSITION_TYPED);
  params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
  params.browser = browser;
  Navigate(&params);
}

// TODO(crbug.com/40101962): Add a browsertest that parallels the existing site
// settings browsertests that open the page info button, and click through to
// the file system site settings page for a given origin.
void ShowSiteSettingsFileSystemImpl(Browser* browser,
                                    Profile* profile,
                                    const GURL& url) {
  constexpr char kParamRequest[] = "site";
  GURL link_destination = GetSettingsUrl(chrome::kFileSystemSettingsSubpage);

  // If origin is valid, open a file system site settings page specific to the
  // current origin of the page. Otherwise, open the File System Site Settings
  // page.
  if (base::FeatureList::IsEnabled(
          features::kFileSystemAccessPersistentPermissions) &&
      SiteGURLIsValid(url)) {
    // TODO(crbug.com/40946480): Update `origin_string` to remove the encoded
    // trailing slash, once it's no longer required to correctly navigate to
    // file system site settings page for the given origin.
    const std::string origin_string =
        base::StrCat({url::Origin::Create(url).Serialize(), "/"});
    link_destination = net::AppendQueryParameter(link_destination,
                                                 kParamRequest, origin_string);
  }
  NavigateParams params(profile, link_destination, ui::PAGE_TRANSITION_TYPED);
  params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
  params.browser = browser;
  Navigate(&params);
}

#if BUILDFLAG(IS_CHROMEOS)
void ShowSystemAppInternal(Profile* profile,
                           const ash::SystemWebAppType type,
                           const ash::SystemAppLaunchParams& params) {
  ash::LaunchSystemWebAppAsync(profile, type, params);
}
void ShowSystemAppInternal(Profile* profile, const ash::SystemWebAppType type) {
  ash::SystemAppLaunchParams params;
  params.launch_source = apps::LaunchSource::kUnknown;
  ash::LaunchSystemWebAppAsync(profile, type, params);
}
#endif  // BUILDFLAG(IS_CHROMEOS)

Browser* GetOrCreateBrowserForProfile(Profile* profile) {
  Browser* browser = chrome::FindTabbedBrowser(profile, false);
  if (!browser) {
    return Browser::Create(Browser::CreateParams(profile, true));
  }
  return browser;
}

}  // namespace

void ShowBookmarkManager(Browser* browser) {
  base::RecordAction(UserMetricsAction("ShowBookmarkManager"));
  ShowSingletonTabIgnorePathOverwriteNTP(browser, GURL(kChromeUIBookmarksURL));
}

void ShowBookmarkManagerForNode(Browser* browser, int64_t node_id) {
  base::RecordAction(UserMetricsAction("ShowBookmarkManager"));
  OpenBookmarkManagerForNode(browser, node_id);
}

void ShowHistory(Browser* browser, const std::string& host_name) {
  // History UI should not be shown in Incognito mode, instead history
  // disclaimer bubble should show up. This also updates the behavior of history
  // keyboard shortcts in Incognito.
  if (browser->profile()->IsOffTheRecord()) {
    browser->window()->ShowIncognitoHistoryDisclaimerDialog();
    return;
  }

  base::RecordAction(UserMetricsAction("ShowHistory"));
  GURL url = GURL(kChromeUIHistoryURL);
  if (!host_name.empty()) {
    GURL::Replacements replacements;
    std::string query("q=");
    query += base::EscapeQueryParamValue(base::StrCat({"host:", host_name}),
                                         /*use_plus=*/false);
    replacements.SetQueryStr(query);
    url = url.ReplaceComponents(replacements);
  }
  ShowSingletonTabIgnorePathOverwriteNTP(browser, url);
}

void ShowHistory(Browser* browser) {
  ShowHistory(browser, std::string());
}

void ShowDownloads(Browser* browser) {
  base::RecordAction(UserMetricsAction("ShowDownloads"));
  if (browser->window() && browser->window()->IsDownloadShelfVisible()) {
    browser->window()->GetDownloadShelf()->Close();
  }
  ShowSingletonTabOverwritingNTP(browser, GURL(kChromeUIDownloadsURL));
}

void ShowExtensions(Browser* browser,
                    const std::string& extension_to_highlight) {
  base::RecordAction(UserMetricsAction("ShowExtensions"));
  GURL url(kChromeUIExtensionsURL);
  if (!extension_to_highlight.empty()) {
    GURL::Replacements replacements;
    std::string query("id=");
    query += extension_to_highlight;
    replacements.SetQueryStr(query);
    url = url.ReplaceComponents(replacements);
  }
  ShowSingletonTabIgnorePathOverwriteNTP(browser, url);
}

void ShowHelp(Browser* browser, HelpSource source) {
  ShowHelpImpl(browser, browser->profile(), source);
}

void ShowHelpForProfile(Profile* profile, HelpSource source) {
  ShowHelpImpl(nullptr, profile, source);
}

#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
void ShowChromeTips(Browser* browser) {
  static const char kChromeTipsURL[] = "https://www.google.com/chrome/tips/";
  ShowSingletonTab(browser, GURL(kChromeTipsURL));
}

#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
void ShowChromeWhatsNew(Browser* browser) {
  ShowSingletonTab(browser, GURL(kChromeUIWhatsNewURL));
}
#endif  // BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
#endif  // BUILDFLAG(GOOGLE_CHROME_BRANDING)

void LaunchReleaseNotes(Profile* profile, apps::LaunchSource source) {
#if BUILDFLAG(IS_CHROMEOS) && BUILDFLAG(GOOGLE_CHROME_BRANDING)
  LaunchReleaseNotesImpl(profile, source);
#endif
}

void ShowBetaForum(Browser* browser) {
  ShowSingletonTab(browser, GURL(kChromeBetaForumURL));
}

void ShowSlow(Browser* browser) {
#if BUILDFLAG(IS_CHROMEOS)
  ShowSingletonTab(browser, GURL(kChromeUISlowURL));
#endif
}

GURL GetSettingsUrl(std::string_view sub_page) {
  return GURL(base::StrCat({kChromeUISettingsURL, sub_page}));
}

bool IsTrustedPopupWindowWithScheme(const Browser* browser,
                                    const std::string& scheme) {
  if (browser->is_type_normal() || !browser->is_trusted_source()) {
    return false;
  }
  if (scheme.empty()) {  // Any trusted popup window
    return true;
  }
  content::WebContents* web_contents =
      browser->tab_strip_model()->GetWebContentsAt(0);
  if (!web_contents) {
    return false;
  }
  GURL url(web_contents->GetURL());
  return url.SchemeIs(scheme);
}

void ShowSettings(Browser* browser) {
  ShowSettingsSubPage(browser, std::string());
}

void ShowSettingsSubPage(Browser* browser, std::string_view sub_page) {
#if BUILDFLAG(IS_CHROMEOS)
  ShowSettingsSubPageForProfile(browser->profile(), sub_page);
#else
  ShowSettingsSubPageInTabbedBrowser(browser, sub_page);
#endif
}

void ShowSettingsSubPageForProfile(Profile* profile,
                                   std::string_view sub_page) {
#if BUILDFLAG(IS_CHROMEOS)
  // OS settings sub-pages are handled else where and should never be
  // encountered here.
  DCHECK(!chromeos::settings::IsOSSettingsSubPage(sub_page)) << sub_page;
#endif
  Browser* browser = GetOrCreateBrowserForProfile(profile);
  ShowSettingsSubPageInTabbedBrowser(browser, sub_page);
}

void ShowSettingsSubPageInTabbedBrowser(Browser* browser,
                                        std::string_view sub_page) {
  base::RecordAction(UserMetricsAction("ShowOptions"));

  // Since the user may be triggering navigation from another UI element such as
  // a menu, ensure the web contents (and therefore the settings page that is
  // about to be shown) is focused. (See crbug/926492 for motivation.)
  FocusWebContents(browser);
  ShowSingletonTabIgnorePathOverwriteNTP(browser, GetSettingsUrl(sub_page));
}

void ShowPageWithPromoForProfile(Profile* profile,
                                 ShowPromoInPage::Params promo_params) {
  Browser* browser = GetOrCreateBrowserForProfile(profile);
  ShowPromoInPage::Start(browser, std::move(promo_params));
}

void ShowContentSettingsExceptions(Browser* browser,
                                   ContentSettingsType content_settings_type) {
  ShowSettingsSubPage(
      browser, GenerateContentSettingsExceptionsSubPage(content_settings_type));
}

void ShowContentSettingsExceptionsForProfile(
    Profile* profile,
    ContentSettingsType content_settings_type) {
  ShowSettingsSubPageForProfile(
      profile, GenerateContentSettingsExceptionsSubPage(content_settings_type));
}

void ShowSiteSettings(Browser* browser, const GURL& url) {
  ShowSiteSettingsImpl(browser, browser->profile(), url);
}

void ShowSiteSettings(Profile* profile, const GURL& url) {
  DCHECK(profile);
  ShowSiteSettingsImpl(nullptr, profile, url);
}

void ShowSiteSettingsFileSystem(Browser* browser, const GURL& url) {
  ShowSiteSettingsFileSystemImpl(browser, browser->profile(), url);
}

void ShowSiteSettingsFileSystem(Profile* profile, const GURL& url) {
  DCHECK(profile);
  ShowSiteSettingsFileSystemImpl(nullptr, profile, url);
}

void ShowContentSettings(Browser* browser,
                         ContentSettingsType content_settings_type) {
  ShowSettingsSubPage(
      browser, base::StrCat({kContentSettingsSubPage, kHashMark,
                             site_settings::ContentSettingsTypeToGroupName(
                                 content_settings_type)}));
}

void ShowClearBrowsingDataDialog(Browser* browser) {
  base::RecordAction(UserMetricsAction("ClearBrowsingData_ShowDlg"));
  ShowSettingsSubPage(browser, kClearBrowserDataSubPage);
}

void ShowPasswordManager(Browser* browser) {
  base::RecordAction(UserMetricsAction("Options_ShowPasswordManager"));
  // This code is necessary to fix a bug (crbug.com/1448559) during Password
  // Manager Shortcut tutorial flow.
  auto* service =
      UserEducationServiceFactory::GetForBrowserContext(browser->profile());
  if (service) {
    auto* tutorial_service = &service->tutorial_service();
    if (tutorial_service &&
        tutorial_service->IsRunningTutorial(kPasswordManagerTutorialId)) {
      ShowSingletonTab(browser, GURL(kChromeUIPasswordManagerSettingsURL));
      return;
    }
  }
  ShowSingletonTabIgnorePathOverwriteNTP(browser,
                                         GURL(kChromeUIPasswordManagerURL));
}

void ShowPasswordDetailsPage(Browser* browser,
                             const std::string& password_domain_name) {
  base::RecordAction(
      UserMetricsAction("Options_ShowPasswordDetailsInPasswordManager"));
  std::string url = base::StrCat(
      {GetGooglePasswordManagerSubPageURLStr(), "/", password_domain_name});
  ShowSingletonTabIgnorePathOverwriteNTP(browser, GURL(url));
}

void ShowPasswordCheck(Browser* browser) {
  base::RecordAction(UserMetricsAction("Options_ShowPasswordCheck"));
  ShowSingletonTabIgnorePathOverwriteNTP(
      browser, GURL(kChromeUIPasswordManagerCheckupURL));
}

void ShowSafeBrowsingEnhancedProtection(Browser* browser) {
  safe_browsing::LogShowEnhancedProtectionAction();
  ShowSettingsSubPage(browser, kSafeBrowsingEnhancedProtectionSubPage);
}

void ShowSafeBrowsingEnhancedProtectionWithIph(
    Browser* browser,
    safe_browsing::SafeBrowsingSettingReferralMethod referral_method) {
#if BUILDFLAG(FULL_SAFE_BROWSING)
  ShowPromoInPage::Params params;
  params.target_url =
      chrome::GetSettingsUrl(chrome::kSafeBrowsingEnhancedProtectionSubPage);
  params.bubble_anchor_id = kEnhancedProtectionSettingElementId;
  params.bubble_arrow = user_education::HelpBubbleArrow::kBottomLeft;
  params.bubble_text = l10n_util::GetStringUTF16(
      IDS_SETTINGS_SAFEBROWSING_ENHANCED_IPH_BUBBLE_TEXT);
  params.close_button_alt_text_id =
      IDS_SETTINGS_SAFEBROWSING_ENHANCED_IPH_BUBBLE_CLOSE_BUTTON_ARIA_LABEL_TEXT;
  base::UmaHistogramEnumeration("SafeBrowsing.EsbPromotionFlow.IphShown",
                                referral_method);
  safe_browsing::LogShowEnhancedProtectionAction();
  ShowPromoInPage::Start(browser, std::move(params));
#endif
}

void ShowImportDialog(Browser* browser) {
  base::RecordAction(UserMetricsAction("Import_ShowDlg"));
  ShowSettingsSubPage(browser, kImportDataSubPage);
}

void ShowAboutChrome(Browser* browser) {
  base::RecordAction(UserMetricsAction("AboutChrome"));
  ShowSingletonTabIgnorePathOverwriteNTP(browser, GURL(kChromeUIHelpURL));
}

void ShowSearchEngineSettings(Browser* browser) {
  base::RecordAction(UserMetricsAction("EditSearchEngines"));
  ShowSettingsSubPage(browser, kSearchEnginesSubPage);
}

void ShowWebStore(Browser* browser, std::string_view utm_source_value) {
  GURL webstore_url = extension_urls::GetNewWebstoreLaunchURL();
  ShowSingletonTabIgnorePathOverwriteNTP(
      browser, extension_urls::AppendUtmSource(webstore_url, utm_source_value));
}

void ShowPrivacySandboxSettings(Browser* browser) {
  base::RecordAction(UserMetricsAction("Options_ShowPrivacySandbox"));
  ShowSettingsSubPage(browser, kAdPrivacySubPage);
}

void ShowPrivacySandboxAdMeasurementSettings(Browser* browser) {
  base::RecordAction(UserMetricsAction("Options_ShowPrivacySandbox"));
  ShowSettingsSubPage(browser, kPrivacySandboxMeasurementSubpage);
}

void ShowAddresses(Browser* browser) {
  base::RecordAction(UserMetricsAction("Options_ShowAddresses"));
  ShowSettingsSubPage(browser, kAddressesSubPage);
}

void ShowPaymentMethods(Browser* browser) {
  base::RecordAction(UserMetricsAction("Options_ShowPaymentMethods"));
  ShowSettingsSubPage(browser, kPaymentsSubPage);
}

void ShowAllSitesSettingsFilteredByRwsOwner(
    Browser* browser,
    const std::string& rws_owner_host_name) {
  GURL url = GetSettingsUrl(kAllSitesSettingsSubpage);
  if (!rws_owner_host_name.empty()) {
    GURL::Replacements replacements;
    std::string query("searchSubpage=");
    query += base::EscapeQueryParamValue(
        base::StrCat({"related:", rws_owner_host_name}),
        /*use_plus=*/false);
    replacements.SetQueryStr(query);
    url = url.ReplaceComponents(replacements);
  }

  ShowSingletonTabIgnorePathOverwriteNTP(browser, url);
}

void ShowEnterpriseManagementPageInTabbedBrowser(Browser* browser) {
  // Management shows in a tab because it has a "back" arrow that takes the
  // user to the Chrome browser about page, which is part of browser settings.
  ShowSingletonTabIgnorePathOverwriteNTP(browser, GURL(kChromeUIManagementURL));
}

void ShowSharedTabGroupActivity(Profile* profile) {
  Browser* browser = GetOrCreateBrowserForProfile(profile);
  ShowSingletonTab(browser,
                   GURL(data_sharing::features::kActivityLogsURL.Get()));
}

#if BUILDFLAG(IS_CHROMEOS)
void ShowAppManagementPage(Profile* profile,
                           const std::string& app_id,
                           ash::settings::AppManagementEntryPoint entry_point) {
  // This histogram is also declared and used at chrome/browser/resources/
  // settings/chrome_os/os_apps_page/app_management_page/constants.js.
  constexpr char kAppManagementEntryPointsHistogramName[] =
      "AppManagement.EntryPoints";

  base::UmaHistogramEnumeration(kAppManagementEntryPointsHistogramName,
                                entry_point);
  std::string sub_page = base::StrCat(
      {chromeos::settings::mojom::kAppDetailsSubpagePath, "?id=", app_id});
  chrome::SettingsWindowManager::GetInstance()->ShowOSSettings(profile,
                                                               sub_page);
}

void ShowGraduationApp(Profile* profile) {
  ash::SystemAppLaunchParams params;
  params.launch_source = apps::LaunchSource::kFromOtherApp;
  ShowSystemAppInternal(profile, ash::SystemWebAppType::GRADUATION, params);
}

GURL GetOSSettingsUrl(std::string_view sub_page) {
  DCHECK(sub_page.empty() || chromeos::settings::IsOSSettingsSubPage(sub_page))
      << sub_page;
  return GURL(base::StrCat({kChromeUIOSSettingsURL, sub_page}));
}

void ShowPrintManagementApp(Profile* profile) {
  ShowSystemAppInternal(profile, ash::SystemWebAppType::PRINT_MANAGEMENT);
}

void ShowConnectivityDiagnosticsApp(Profile* profile) {
  ShowSystemAppInternal(profile,
                        ash::SystemWebAppType::CONNECTIVITY_DIAGNOSTICS);
}

void ShowScanningApp(Profile* profile) {
  ShowSystemAppInternal(profile, ash::SystemWebAppType::SCANNING);
}

void ShowDiagnosticsApp(Profile* profile) {
  ShowSystemAppInternal(profile, ash::SystemWebAppType::DIAGNOSTICS);
}

void ShowFirmwareUpdatesApp(Profile* profile) {
  ShowSystemAppInternal(profile, ash::SystemWebAppType::FIRMWARE_UPDATE);
}

void ShowShortcutCustomizationApp(Profile* profile) {
  ShowSystemAppInternal(profile, ash::SystemWebAppType::SHORTCUT_CUSTOMIZATION);
}

void ShowShortcutCustomizationApp(Profile* profile,
                                  const std::string& action,
                                  const std::string& category) {
  const std::string query_string =
      base::StrCat({"action=", action, "&category=", category});
  ash::SystemAppLaunchParams params;
  params.launch_source = apps::LaunchSource::kUnknown;
  params.url = GURL(base::StrCat(
      {ash::kChromeUIShortcutCustomizationAppURL, "?", query_string}));
  ShowSystemAppInternal(profile, ash::SystemWebAppType::SHORTCUT_CUSTOMIZATION,
                        params);
}
#endif  // BUILDFLAG(IS_CHROMEOS)

#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
void ShowWebAppSettingsImpl(Browser* browser,
                            Profile* profile,
                            const std::string& app_id,
                            web_app::AppSettingsPageEntryPoint entry_point) {
  base::UmaHistogramEnumeration(
      web_app::kAppSettingsPageEntryPointsHistogramName, entry_point);

  const GURL link_destination(chrome::kChromeUIWebAppSettingsURL + app_id);
  NavigateParams params(profile, link_destination, ui::PAGE_TRANSITION_TYPED);
  params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
  params.browser = browser;
  Navigate(&params);
}

void ShowWebAppSettings(Browser* browser,
                        const std::string& app_id,
                        web_app::AppSettingsPageEntryPoint entry_point) {
  ShowWebAppSettingsImpl(browser, browser->profile(), app_id, entry_point);
}

void ShowWebAppSettings(Profile* profile,
                        const std::string& app_id,
                        web_app::AppSettingsPageEntryPoint entry_point) {
  ShowWebAppSettingsImpl(/*browser=*/nullptr, profile, app_id, entry_point);
}
#endif

void ShowAllComparisonTables(Browser* browser) {
  ShowSingletonTab(browser, GURL(commerce::kChromeUICompareUrl));
}

}  // namespace chrome