1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "chrome/browser/ui/chrome_pages.h"
6 
7 #include <stddef.h>
8 
9 #include <memory>
10 
11 #include "base/feature_list.h"
12 #include "base/logging.h"
13 #include "base/macros.h"
14 #include "base/metrics/user_metrics.h"
15 #include "base/no_destructor.h"
16 #include "base/strings/strcat.h"
17 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/stringprintf.h"
19 #include "build/branding_buildflags.h"
20 #include "build/build_config.h"
21 #include "chrome/browser/apps/app_service/app_launch_params.h"
22 #include "chrome/browser/apps/app_service/app_service_proxy.h"
23 #include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
24 #include "chrome/browser/apps/app_service/browser_app_launcher.h"
25 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
26 #include "chrome/browser/browser_process.h"
27 #include "chrome/browser/chromeos/web_applications/default_web_app_ids.h"
28 #include "chrome/browser/download/download_shelf.h"
29 #include "chrome/browser/policy/profile_policy_connector.h"
30 #include "chrome/browser/profiles/profile.h"
31 #include "chrome/browser/profiles/profile_manager.h"
32 #include "chrome/browser/signin/account_consistency_mode_manager.h"
33 #include "chrome/browser/ui/browser.h"
34 #include "chrome/browser/ui/browser_finder.h"
35 #include "chrome/browser/ui/browser_navigator_params.h"
36 #include "chrome/browser/ui/browser_window.h"
37 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
38 #include "chrome/browser/ui/singleton_tabs.h"
39 #include "chrome/browser/ui/tabs/tab_strip_model.h"
40 #include "chrome/browser/ui/web_applications/system_web_app_ui_utils.h"
41 #include "chrome/browser/ui/webui/bookmarks/bookmarks_ui.h"
42 #include "chrome/browser/ui/webui/settings/site_settings_helper.h"
43 #include "chrome/browser/web_applications/components/app_registrar.h"
44 #include "chrome/browser/web_applications/components/web_app_constants.h"
45 #include "chrome/browser/web_applications/components/web_app_id.h"
46 #include "chrome/browser/web_applications/system_web_app_manager.h"
47 #include "chrome/common/chrome_features.h"
48 #include "chrome/common/chrome_switches.h"
49 #include "chrome/common/url_constants.h"
50 #include "chromeos/login/login_state/login_state.h"
51 #include "components/bookmarks/browser/bookmark_model.h"
52 #include "components/bookmarks/browser/bookmark_node.h"
53 #include "content/public/browser/web_contents.h"
54 #include "extensions/browser/extension_prefs.h"
55 #include "extensions/common/constants.h"
56 #include "net/base/url_util.h"
57 #include "ui/base/l10n/l10n_util.h"
58 #include "ui/base/window_open_disposition.h"
59 #include "url/url_util.h"
60 
61 #if defined(OS_CHROMEOS)
62 #include "base/metrics/histogram_functions.h"
63 #include "chrome/browser/ui/settings_window_manager_chromeos.h"
64 #include "chrome/browser/ui/webui/settings/chromeos/app_management/app_management_uma.h"
65 #include "chrome/browser/ui/webui/settings/chromeos/constants/routes.mojom.h"
66 #include "chrome/browser/ui/webui/settings/chromeos/constants/routes_util.h"
67 #include "chromeos/components/connectivity_diagnostics/url_constants.h"
68 #include "chromeos/constants/chromeos_features.h"
69 #else
70 #include "chrome/browser/ui/signin_view_controller.h"
71 #endif
72 
73 #if !defined(OS_ANDROID)
74 #include "chrome/browser/signin/identity_manager_factory.h"
75 #include "components/signin/public/base/signin_pref_names.h"
76 #include "components/signin/public/identity_manager/identity_manager.h"
77 #endif
78 
79 using base::UserMetricsAction;
80 
81 namespace chrome {
82 namespace {
83 
84 const char kHashMark[] = "#";
85 
FocusWebContents(Browser * browser)86 void FocusWebContents(Browser* browser) {
87   auto* const contents = browser->tab_strip_model()->GetActiveWebContents();
88   if (contents)
89     contents->Focus();
90 }
91 
92 // Shows |url| in a tab in |browser|. If a tab is already open to |url|,
93 // ignoring the URL path, then that tab becomes selected. Overwrites the new tab
94 // page if it is open.
ShowSingletonTabIgnorePathOverwriteNTP(Browser * browser,const GURL & url)95 void ShowSingletonTabIgnorePathOverwriteNTP(Browser* browser, const GURL& url) {
96   NavigateParams params(GetSingletonTabNavigateParams(browser, url));
97   params.path_behavior = NavigateParams::IGNORE_AND_NAVIGATE;
98   ShowSingletonTabOverwritingNTP(browser, std::move(params));
99 }
100 
OpenBookmarkManagerForNode(Browser * browser,int64_t node_id)101 void OpenBookmarkManagerForNode(Browser* browser, int64_t node_id) {
102   GURL url = GURL(kChromeUIBookmarksURL)
103                  .Resolve(base::StringPrintf(
104                      "/?id=%s", base::NumberToString(node_id).c_str()));
105   ShowSingletonTabIgnorePathOverwriteNTP(browser, url);
106 }
107 
108 #if defined(OS_CHROMEOS) && BUILDFLAG(GOOGLE_CHROME_BRANDING)
109 
LaunchReleaseNotesImpl(Profile * profile,apps::mojom::LaunchSource source)110 void LaunchReleaseNotesImpl(Profile* profile,
111                             apps::mojom::LaunchSource source) {
112   base::RecordAction(UserMetricsAction("ReleaseNotes.ShowReleaseNotes"));
113   apps::AppServiceProxy* proxy =
114       apps::AppServiceProxyFactory::GetForProfileRedirectInIncognito(profile);
115   proxy->LaunchAppWithUrl(
116       chromeos::default_web_apps::kHelpAppId, ui::EventFlags::EF_NONE,
117       GURL("chrome://help-app/updates"), source, display::kDefaultDisplayId);
118 }
119 
120 #endif
121 
122 // Shows either the help app or the appropriate help page for |source|. If
123 // |browser| is NULL and the help page is used (vs the app), the help page is
124 // shown in the last active browser. If there is no such browser, a new browser
125 // is created.
ShowHelpImpl(Browser * browser,Profile * profile,HelpSource source)126 void ShowHelpImpl(Browser* browser, Profile* profile, HelpSource source) {
127   base::RecordAction(UserMetricsAction("ShowHelpTab"));
128 #if defined(OS_CHROMEOS) && BUILDFLAG(GOOGLE_CHROME_BRANDING)
129   auto app_launch_source = apps::mojom::LaunchSource::kUnknown;
130   switch (source) {
131     case HELP_SOURCE_KEYBOARD:
132       app_launch_source = apps::mojom::LaunchSource::kFromKeyboard;
133       break;
134     case HELP_SOURCE_MENU:
135       app_launch_source = apps::mojom::LaunchSource::kFromMenu;
136       break;
137     case HELP_SOURCE_WEBUI:
138     case HELP_SOURCE_WEBUI_CHROME_OS:
139       app_launch_source = apps::mojom::LaunchSource::kFromOtherApp;
140       break;
141     default:
142       NOTREACHED() << "Unhandled help source" << source;
143   }
144   apps::AppServiceProxy* proxy =
145       apps::AppServiceProxyFactory::GetForProfileRedirectInIncognito(profile);
146   proxy->Launch(chromeos::default_web_apps::kHelpAppId, ui::EventFlags::EF_NONE,
147                 app_launch_source, display::kDefaultDisplayId);
148 #else
149   GURL url;
150   switch (source) {
151     case HELP_SOURCE_KEYBOARD:
152       url = GURL(kChromeHelpViaKeyboardURL);
153       break;
154     case HELP_SOURCE_MENU:
155       url = GURL(kChromeHelpViaMenuURL);
156       break;
157 #if defined(OS_CHROMEOS)
158     case HELP_SOURCE_WEBUI:
159       url = GURL(kChromeHelpViaWebUIURL);
160       break;
161     case HELP_SOURCE_WEBUI_CHROME_OS:
162       url = GURL(kChromeOsHelpViaWebUIURL);
163       break;
164 #else
165     case HELP_SOURCE_WEBUI:
166       url = GURL(kChromeHelpViaWebUIURL);
167       break;
168 #endif
169     default:
170       NOTREACHED() << "Unhandled help source " << source;
171   }
172   std::unique_ptr<ScopedTabbedBrowserDisplayer> displayer;
173   if (!browser) {
174     displayer = std::make_unique<ScopedTabbedBrowserDisplayer>(profile);
175     browser = displayer->browser();
176   }
177   ShowSingletonTab(browser, url);
178 #endif
179 }
180 
GenerateContentSettingsExceptionsSubPage(ContentSettingsType type)181 std::string GenerateContentSettingsExceptionsSubPage(ContentSettingsType type) {
182   // In MD Settings, the exceptions no longer have a separate subpage.
183   // This list overrides the group names defined in site_settings_helper for the
184   // purposes of URL generation for MD Settings only. We need this because some
185   // of the old group names are no longer appropriate: i.e. "plugins" =>
186   // "flash".
187   //
188   // TODO(crbug.com/728353): Update the group names defined in
189   // site_settings_helper once Options is removed from Chrome. Then this list
190   // will no longer be needed.
191   static base::NoDestructor<std::map<ContentSettingsType, std::string>>
192       kSettingsPathOverrides(
193           {{ContentSettingsType::AUTOMATIC_DOWNLOADS, "automaticDownloads"},
194            {ContentSettingsType::BACKGROUND_SYNC, "backgroundSync"},
195            {ContentSettingsType::MEDIASTREAM_MIC, "microphone"},
196            {ContentSettingsType::MEDIASTREAM_CAMERA, "camera"},
197            {ContentSettingsType::MIDI_SYSEX, "midiDevices"},
198            {ContentSettingsType::PLUGINS, "flash"},
199            {ContentSettingsType::ADS, "ads"},
200            {ContentSettingsType::PPAPI_BROKER, "unsandboxedPlugins"}});
201   const auto it = kSettingsPathOverrides->find(type);
202   const std::string content_type_path =
203       (it == kSettingsPathOverrides->end())
204           ? site_settings::ContentSettingsTypeToGroupName(type)
205           : it->second;
206 
207   return std::string(kContentSettingsSubPage) + "/" + content_type_path;
208 }
209 
ShowSiteSettingsImpl(Browser * browser,Profile * profile,const GURL & url)210 void ShowSiteSettingsImpl(Browser* browser, Profile* profile, const GURL& url) {
211   // If a valid non-file origin, open a settings page specific to the current
212   // origin of the page. Otherwise, open Content Settings.
213   url::Origin site_origin = url::Origin::Create(url);
214   std::string link_destination(chrome::kChromeUIContentSettingsURL);
215   // TODO(https://crbug.com/444047): Site Details should work with file:// urls
216   // when this bug is fixed, so add it to the allowlist when that happens.
217   if (!site_origin.opaque() && (url.SchemeIsHTTPOrHTTPS() ||
218                                 url.SchemeIs(extensions::kExtensionScheme))) {
219     std::string origin_string = site_origin.Serialize();
220     url::RawCanonOutputT<char> percent_encoded_origin;
221     url::EncodeURIComponent(origin_string.c_str(), origin_string.length(),
222                             &percent_encoded_origin);
223     link_destination = chrome::kChromeUISiteDetailsPrefixURL +
224                        std::string(percent_encoded_origin.data(),
225                                    percent_encoded_origin.length());
226   }
227   NavigateParams params(profile, GURL(link_destination),
228                         ui::PAGE_TRANSITION_TYPED);
229   params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
230   params.browser = browser;
231   Navigate(&params);
232 }
233 
234 }  // namespace
235 
ShowBookmarkManager(Browser * browser)236 void ShowBookmarkManager(Browser* browser) {
237   base::RecordAction(UserMetricsAction("ShowBookmarkManager"));
238   ShowSingletonTabIgnorePathOverwriteNTP(browser, GURL(kChromeUIBookmarksURL));
239 }
240 
ShowBookmarkManagerForNode(Browser * browser,int64_t node_id)241 void ShowBookmarkManagerForNode(Browser* browser, int64_t node_id) {
242   base::RecordAction(UserMetricsAction("ShowBookmarkManager"));
243   OpenBookmarkManagerForNode(browser, node_id);
244 }
245 
ShowHistory(Browser * browser)246 void ShowHistory(Browser* browser) {
247   base::RecordAction(UserMetricsAction("ShowHistory"));
248   ShowSingletonTabIgnorePathOverwriteNTP(browser, GURL(kChromeUIHistoryURL));
249 }
250 
ShowDownloads(Browser * browser)251 void ShowDownloads(Browser* browser) {
252   base::RecordAction(UserMetricsAction("ShowDownloads"));
253   if (browser->window() && browser->window()->IsDownloadShelfVisible())
254     browser->window()->GetDownloadShelf()->Close();
255 
256   ShowSingletonTabOverwritingNTP(
257       browser,
258       GetSingletonTabNavigateParams(browser, GURL(kChromeUIDownloadsURL)));
259 }
260 
ShowExtensions(Browser * browser,const std::string & extension_to_highlight)261 void ShowExtensions(Browser* browser,
262                     const std::string& extension_to_highlight) {
263   base::RecordAction(UserMetricsAction("ShowExtensions"));
264   GURL url(kChromeUIExtensionsURL);
265   if (!extension_to_highlight.empty()) {
266     GURL::Replacements replacements;
267     std::string query("id=");
268     query += extension_to_highlight;
269     replacements.SetQueryStr(query);
270     url = url.ReplaceComponents(replacements);
271   }
272   ShowSingletonTabIgnorePathOverwriteNTP(browser, url);
273 }
274 
ShowHelp(Browser * browser,HelpSource source)275 void ShowHelp(Browser* browser, HelpSource source) {
276   ShowHelpImpl(browser, browser->profile(), source);
277 }
278 
ShowHelpForProfile(Profile * profile,HelpSource source)279 void ShowHelpForProfile(Profile* profile, HelpSource source) {
280   ShowHelpImpl(NULL, profile, source);
281 }
282 
LaunchReleaseNotes(Profile * profile,apps::mojom::LaunchSource source)283 void LaunchReleaseNotes(Profile* profile, apps::mojom::LaunchSource source) {
284 #if defined(OS_CHROMEOS) && BUILDFLAG(GOOGLE_CHROME_BRANDING)
285   LaunchReleaseNotesImpl(profile, source);
286 #endif
287 }
288 
ShowBetaForum(Browser * browser)289 void ShowBetaForum(Browser* browser) {
290   ShowSingletonTab(browser, GURL(kChromeBetaForumURL));
291 }
292 
ShowPolicy(Browser * browser)293 void ShowPolicy(Browser* browser) {
294   ShowSingletonTab(browser, GURL(kChromeUIPolicyURL));
295 }
296 
ShowSlow(Browser * browser)297 void ShowSlow(Browser* browser) {
298 #if defined(OS_CHROMEOS)
299   ShowSingletonTab(browser, GURL(kChromeUISlowURL));
300 #endif
301 }
302 
GetSettingsUrl(const std::string & sub_page)303 GURL GetSettingsUrl(const std::string& sub_page) {
304   return GURL(std::string(kChromeUISettingsURL) + sub_page);
305 }
306 
IsTrustedPopupWindowWithScheme(const Browser * browser,const std::string & scheme)307 bool IsTrustedPopupWindowWithScheme(const Browser* browser,
308                                     const std::string& scheme) {
309   if (browser->is_type_normal() || !browser->is_trusted_source())
310     return false;
311   if (scheme.empty())  // Any trusted popup window
312     return true;
313   content::WebContents* web_contents =
314       browser->tab_strip_model()->GetWebContentsAt(0);
315   if (!web_contents)
316     return false;
317   GURL url(web_contents->GetURL());
318   return url.SchemeIs(scheme);
319 }
320 
ShowSettings(Browser * browser)321 void ShowSettings(Browser* browser) {
322   ShowSettingsSubPage(browser, std::string());
323 }
324 
ShowSettingsSubPage(Browser * browser,const std::string & sub_page)325 void ShowSettingsSubPage(Browser* browser, const std::string& sub_page) {
326 #if defined(OS_CHROMEOS)
327   ShowSettingsSubPageForProfile(browser->profile(), sub_page);
328 #else
329   ShowSettingsSubPageInTabbedBrowser(browser, sub_page);
330 #endif
331 }
332 
ShowSettingsSubPageForProfile(Profile * profile,const std::string & sub_page)333 void ShowSettingsSubPageForProfile(Profile* profile,
334                                    const std::string& sub_page) {
335 #if defined(OS_CHROMEOS)
336   // OS settings sub-pages are handled else where and should never be
337   // encountered here.
338   DCHECK(!chromeos::settings::IsOSSettingsSubPage(sub_page)) << sub_page;
339 #endif
340   Browser* browser = chrome::FindTabbedBrowser(profile, false);
341   if (!browser)
342     browser = Browser::Create(Browser::CreateParams(profile, true));
343   ShowSettingsSubPageInTabbedBrowser(browser, sub_page);
344 }
345 
ShowSettingsSubPageInTabbedBrowser(Browser * browser,const std::string & sub_page)346 void ShowSettingsSubPageInTabbedBrowser(Browser* browser,
347                                         const std::string& sub_page) {
348   base::RecordAction(UserMetricsAction("ShowOptions"));
349 
350   // Since the user may be triggering navigation from another UI element such as
351   // a menu, ensure the web contents (and therefore the settings page that is
352   // about to be shown) is focused. (See crbug/926492 for motivation.)
353   FocusWebContents(browser);
354   ShowSingletonTabIgnorePathOverwriteNTP(browser, GetSettingsUrl(sub_page));
355 }
356 
ShowContentSettingsExceptions(Browser * browser,ContentSettingsType content_settings_type)357 void ShowContentSettingsExceptions(Browser* browser,
358                                    ContentSettingsType content_settings_type) {
359   ShowSettingsSubPage(
360       browser, GenerateContentSettingsExceptionsSubPage(content_settings_type));
361 }
362 
ShowContentSettingsExceptionsForProfile(Profile * profile,ContentSettingsType content_settings_type)363 void ShowContentSettingsExceptionsForProfile(
364     Profile* profile,
365     ContentSettingsType content_settings_type) {
366   ShowSettingsSubPageForProfile(
367       profile, GenerateContentSettingsExceptionsSubPage(content_settings_type));
368 }
369 
ShowSiteSettings(Browser * browser,const GURL & url)370 void ShowSiteSettings(Browser* browser, const GURL& url) {
371   ShowSiteSettingsImpl(browser, browser->profile(), url);
372 }
373 
ShowSiteSettings(Profile * profile,const GURL & url)374 void ShowSiteSettings(Profile* profile, const GURL& url) {
375   DCHECK(profile);
376   ShowSiteSettingsImpl(nullptr, profile, url);
377 }
378 
ShowContentSettings(Browser * browser,ContentSettingsType content_settings_type)379 void ShowContentSettings(Browser* browser,
380                          ContentSettingsType content_settings_type) {
381   ShowSettingsSubPage(
382       browser,
383       kContentSettingsSubPage + std::string(kHashMark) +
384           site_settings::ContentSettingsTypeToGroupName(content_settings_type));
385 }
386 
ShowClearBrowsingDataDialog(Browser * browser)387 void ShowClearBrowsingDataDialog(Browser* browser) {
388   base::RecordAction(UserMetricsAction("ClearBrowsingData_ShowDlg"));
389   ShowSettingsSubPage(browser, kClearBrowserDataSubPage);
390 }
391 
ShowPasswordManager(Browser * browser)392 void ShowPasswordManager(Browser* browser) {
393   base::RecordAction(UserMetricsAction("Options_ShowPasswordManager"));
394   ShowSettingsSubPage(browser, kPasswordManagerSubPage);
395 }
396 
ShowPasswordCheck(Browser * browser)397 void ShowPasswordCheck(Browser* browser) {
398   base::RecordAction(UserMetricsAction("Options_ShowPasswordCheck"));
399   ShowSettingsSubPage(browser, kPasswordCheckSubPage);
400 }
401 
ShowSafeBrowsingEnhancedProtection(Browser * browser)402 void ShowSafeBrowsingEnhancedProtection(Browser* browser) {
403   base::RecordAction(
404       UserMetricsAction("Options_ShowSafeBrowsingEnhancedProtection"));
405   ShowSettingsSubPage(browser, kSafeBrowsingEnhancedProtectionSubPage);
406 }
407 
ShowImportDialog(Browser * browser)408 void ShowImportDialog(Browser* browser) {
409   base::RecordAction(UserMetricsAction("Import_ShowDlg"));
410   ShowSettingsSubPage(browser, kImportDataSubPage);
411 }
412 
ShowAboutChrome(Browser * browser)413 void ShowAboutChrome(Browser* browser) {
414   base::RecordAction(UserMetricsAction("AboutChrome"));
415   ShowSingletonTabIgnorePathOverwriteNTP(browser, GURL(kChromeUIHelpURL));
416 }
417 
ShowSearchEngineSettings(Browser * browser)418 void ShowSearchEngineSettings(Browser* browser) {
419   base::RecordAction(UserMetricsAction("EditSearchEngines"));
420   ShowSettingsSubPage(browser, kSearchEnginesSubPage);
421 }
422 
423 #if defined(OS_CHROMEOS)
ShowEnterpriseManagementPageInTabbedBrowser(Browser * browser)424 void ShowEnterpriseManagementPageInTabbedBrowser(Browser* browser) {
425   // Management shows in a tab because it has a "back" arrow that takes the
426   // user to the Chrome browser about page, which is part of browser settings.
427   ShowSingletonTabIgnorePathOverwriteNTP(browser, GURL(kChromeUIManagementURL));
428 }
429 
ShowAppManagementPage(Profile * profile,const std::string & app_id,AppManagementEntryPoint entry_point)430 void ShowAppManagementPage(Profile* profile,
431                            const std::string& app_id,
432                            AppManagementEntryPoint entry_point) {
433   // This histogram is also declared and used at chrome/browser/resources/
434   // settings/chrome_os/os_apps_page/app_management_page/constants.js.
435   constexpr char kAppManagementEntryPointsHistogramName[] =
436       "AppManagement.EntryPoints";
437 
438   base::UmaHistogramEnumeration(kAppManagementEntryPointsHistogramName,
439                                 entry_point);
440   std::string sub_page = base::StrCat(
441       {chromeos::settings::mojom::kAppDetailsSubpagePath, "?id=", app_id});
442   chrome::SettingsWindowManager::GetInstance()->ShowOSSettings(profile,
443                                                                sub_page);
444 }
445 
ShowPrintManagementApp(Profile * profile,PrintManagementAppEntryPoint entry_point)446 void ShowPrintManagementApp(Profile* profile,
447                             PrintManagementAppEntryPoint entry_point) {
448   DCHECK(
449       base::FeatureList::IsEnabled(chromeos::features::kPrintJobManagementApp));
450   DCHECK(entry_point == PrintManagementAppEntryPoint::kSettings ||
451          entry_point == PrintManagementAppEntryPoint::kNotification);
452 
453   base::UmaHistogramEnumeration("Printing.CUPS.PrintManagementAppEntryPoint",
454                                 entry_point);
455   LaunchSystemWebApp(profile, web_app::SystemAppType::PRINT_MANAGEMENT,
456                      GURL(chrome::kChromeUIPrintManagementUrl));
457 }
458 
ShowConnectivityDiagnosticsApp(Profile * profile)459 void ShowConnectivityDiagnosticsApp(Profile* profile) {
460   DCHECK(base::FeatureList::IsEnabled(
461       chromeos::features::kConnectivityDiagnosticsWebUi));
462   LaunchSystemWebApp(profile, web_app::SystemAppType::CONNECTIVITY_DIAGNOSTICS,
463                      GURL(chromeos::kChromeUIConnectivityDiagnosticsUrl));
464 }
465 
GetOSSettingsUrl(const std::string & sub_page)466 GURL GetOSSettingsUrl(const std::string& sub_page) {
467   DCHECK(sub_page.empty() || chromeos::settings::IsOSSettingsSubPage(sub_page))
468       << sub_page;
469   std::string url = kChromeUIOSSettingsURL;
470   return GURL(url + sub_page);
471 }
472 #endif
473 
474 #if !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
ShowBrowserSignin(Browser * browser,signin_metrics::AccessPoint access_point)475 void ShowBrowserSignin(Browser* browser,
476                        signin_metrics::AccessPoint access_point) {
477   Profile* original_profile = browser->profile()->GetOriginalProfile();
478   DCHECK(original_profile->GetPrefs()->GetBoolean(prefs::kSigninAllowed));
479 
480   // If the browser's profile is an incognito profile, make sure to use
481   // a browser window from the original profile. The user cannot sign in
482   // from an incognito window.
483   auto displayer =
484       std::make_unique<ScopedTabbedBrowserDisplayer>(original_profile);
485   browser = displayer->browser();
486 
487   profiles::BubbleViewMode bubble_view_mode =
488       IdentityManagerFactory::GetForProfile(original_profile)
489               ->HasPrimaryAccount()
490           ? profiles::BUBBLE_VIEW_MODE_GAIA_REAUTH
491           : profiles::BUBBLE_VIEW_MODE_GAIA_SIGNIN;
492   browser->signin_view_controller()->ShowSignin(bubble_view_mode, access_point);
493 }
494 
ShowBrowserSigninOrSettings(Browser * browser,signin_metrics::AccessPoint access_point)495 void ShowBrowserSigninOrSettings(Browser* browser,
496                                  signin_metrics::AccessPoint access_point) {
497   Profile* original_profile = browser->profile()->GetOriginalProfile();
498   DCHECK(original_profile->GetPrefs()->GetBoolean(prefs::kSigninAllowed));
499   if (IdentityManagerFactory::GetForProfile(original_profile)
500           ->HasPrimaryAccount())
501     ShowSettings(browser);
502   else
503     ShowBrowserSignin(browser, access_point);
504 }
505 #endif
506 
507 }  // namespace chrome
508