File: profile_picker_view.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; 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 (972 lines) | stat: -rw-r--r-- 34,456 bytes parent folder | download
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
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/views/profiles/profile_picker_view.h"

#include "base/containers/contains.h"
#include "base/debug/dump_without_crashing.h"
#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/notreached.h"
#include "base/time/time.h"
#include "base/trace_event/trace_event.h"
#include "build/build_config.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/feature_engagement/tracker_factory.h"
#include "chrome/browser/lifetime/application_lifetime.h"
#include "chrome/browser/profiles/keep_alive/profile_keep_alive_types.h"
#include "chrome/browser/profiles/keep_alive/scoped_profile_keep_alive.h"
#include "chrome/browser/profiles/profile_attributes_storage.h"
#include "chrome/browser/profiles/profile_avatar_icon_util.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profile_window.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/signin/signin_promo.h"
#include "chrome/browser/signin/signin_util.h"
#include "chrome/browser/themes/theme_service.h"
#include "chrome/browser/themes/theme_service_factory.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/color/chrome_color_id.h"
#include "chrome/browser/ui/views/accelerator_table.h"
#include "chrome/browser/ui/views/profiles/profile_management_flow_controller.h"
#include "chrome/browser/ui/views/profiles/profile_management_flow_controller_impl.h"
#include "chrome/browser/ui/views/profiles/profile_management_types.h"
#include "chrome/browser/ui/views/profiles/profile_picker_feature_promo_controller.h"
#include "chrome/browser/ui/views/profiles/profile_picker_flow_controller.h"
#include "chrome/browser/ui/views/profiles/profile_picker_glic_flow_controller.h"
#include "chrome/browser/ui/webui/signin/profile_picker_ui.h"
#include "chrome/browser/ui/webui/signin/signin_url_utils.h"
#include "chrome/browser/user_education/user_education_service_factory.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/grit/branded_strings.h"
#include "components/keep_alive_registry/keep_alive_types.h"
#include "components/prefs/pref_service.h"
#include "components/signin/public/base/signin_metrics.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/startup_metric_utils/browser/startup_metric_utils.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/context_menu_params.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/url_constants.h"
#include "google_apis/gaia/gaia_id.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/views/controls/webview/webview.h"
#include "ui/views/layout/flex_layout.h"
#include "ui/views/view.h"
#include "ui/views/view_class_properties.h"
#include "ui/views/widget/widget.h"
#include "url/gurl.h"

#if BUILDFLAG(ENABLE_DICE_SUPPORT)
#include "chrome/browser/ui/views/profiles/first_run_flow_controller_dice.h"
#include "chrome/browser/ui/views/profiles/profile_picker_dice_sign_in_toolbar.h"
#endif

#if BUILDFLAG(IS_WIN)
#include "chrome/browser/shell_integration_win.h"
#include "ui/base/win/shell.h"
#include "ui/views/win/hwnd_util.h"
#endif

#if BUILDFLAG(IS_MAC)
#include "chrome/browser/global_keyboard_shortcuts_mac.h"
#endif

#if BUILDFLAG(IS_LINUX)
#include "chrome/browser/shell_integration_linux.h"
#endif

namespace {

ProfilePickerView* g_profile_picker_view = nullptr;
base::OnceClosure* g_profile_picker_opened_callback_for_testing = nullptr;

constexpr int kWindowTitleId = IDS_PRODUCT_NAME;

constexpr int kWindowWidth = 1024;
constexpr int kWindowHeight = 758;
constexpr float kMaxRatioOfWorkArea = 0.9;

constexpr int kSupportedAcceleratorCommands[] = {
    IDC_CLOSE_TAB,  IDC_CLOSE_WINDOW,    IDC_EXIT,
    IDC_FULLSCREEN, IDC_MINIMIZE_WINDOW, IDC_BACK,
#if BUILDFLAG(ENABLE_DICE_SUPPORT)
    IDC_RELOAD
#endif
};

class ProfilePickerWidget : public views::Widget {
 public:
  explicit ProfilePickerWidget(ProfilePickerView* profile_picker_view)
      : profile_picker_view_(profile_picker_view) {
    views::Widget::InitParams params(
        views::Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET);
    params.delegate = profile_picker_view_;
#if BUILDFLAG(IS_LINUX)
    params.wm_class_name = shell_integration_linux::GetProgramClassName();
    params.wm_class_class = shell_integration_linux::GetProgramClassClass();
    params.wayland_app_id = params.wm_class_class;
#endif
    Init(std::move(params));
  }
  ~ProfilePickerWidget() override = default;

 private:
  const raw_ptr<ProfilePickerView, DanglingUntriaged> profile_picker_view_;
};

// Returns whether the current flow is part of the classic profile picker flow.
// Checking this should become eventually unnecessary as flows move away from
// using static calls and global variables, and keep calls to native contained
// within their own steps. See crbug.com/1359352.
bool IsClassicProfilePickerFlow(const ProfilePicker::Params& params) {
  switch (params.entry_point()) {
    case ProfilePicker::EntryPoint::kAppMenuProfileSubMenuAddNewProfile:
    case ProfilePicker::EntryPoint::kAppMenuProfileSubMenuManageProfiles:
    case ProfilePicker::EntryPoint::kOnStartup:
    case ProfilePicker::EntryPoint::kProfileMenuManageProfiles:
    case ProfilePicker::EntryPoint::kProfileMenuAddNewProfile:
    case ProfilePicker::EntryPoint::kOpenNewWindowAfterProfileDeletion:
    case ProfilePicker::EntryPoint::kNewSessionOnExistingProcess:
    case ProfilePicker::EntryPoint::kProfileLocked:
    case ProfilePicker::EntryPoint::kUnableToCreateBrowser:
    case ProfilePicker::EntryPoint::kBackgroundModeManager:
    case ProfilePicker::EntryPoint::kProfileIdle:
    case ProfilePicker::EntryPoint::kOnStartupNoProfile:
    case ProfilePicker::EntryPoint::kNewSessionOnExistingProcessNoProfile:
      return true;
    case ProfilePicker::EntryPoint::kFirstRun:
    case ProfilePicker::EntryPoint::kGlicManager:
      return false;
  }
}

void ClearLockedProfilesFirstBrowserKeepAlive() {
  ProfileManager* profile_manager = g_browser_process->profile_manager();
  const std::vector<Profile*> loaded_profiles =
      profile_manager->GetLoadedProfiles();
  for (Profile* profile : loaded_profiles) {
    ProfileAttributesEntry* entry =
        profile_manager->GetProfileAttributesStorage()
            .GetProfileAttributesWithPath(profile->GetPath());
    if (entry && entry->IsSigninRequired()) {
      profile_manager->ClearFirstBrowserWindowKeepAlive(profile);
    }
  }
}

}  // namespace

// static
void ProfilePicker::Show(Params&& params) {
  // Re-open with new params if necessary.
  if (g_profile_picker_view && g_profile_picker_view->MaybeReopen(params)) {
    return;
  }

  if (g_profile_picker_view) {
    g_profile_picker_view->UpdateParams(std::move(params));
  } else {
    g_profile_picker_view = new ProfilePickerView(std::move(params));
  }
  g_profile_picker_view->Display();
}

// static
base::FilePath ProfilePicker::GetSwitchProfilePath() {
  if (g_profile_picker_view) {
    return g_profile_picker_view->GetProfilePickerFlowController()
        ->GetSwitchProfilePathOrEmpty();
  }
  return base::FilePath();
}

#if BUILDFLAG(ENABLE_DICE_SUPPORT)
// static
void ProfilePicker::SwitchToDiceSignIn(
    ProfilePicker::ProfileInfo profile_info,
    base::OnceCallback<void(bool)> switch_finished_callback) {
  if (g_profile_picker_view) {
    g_profile_picker_view->SwitchToDiceSignIn(
        std::move(profile_info), std::move(switch_finished_callback));
  }
}

// static
void ProfilePicker::SwitchToReauth(
    Profile* profile,
    base::OnceCallback<void(const ForceSigninUIError&)> on_error_callback) {
  if (g_profile_picker_view) {
    g_profile_picker_view->SwitchToReauth(profile,
                                          std::move(on_error_callback));
  }
}
#endif

// static
void ProfilePicker::SwitchToSignedOutPostIdentityFlow(
    std::optional<SkColor> profile_color,
    base::OnceCallback<void(bool)> switch_finished_callback) {
  if (g_profile_picker_view) {
    g_profile_picker_view->SwitchToSignedOutPostIdentityFlow(
        profile_color, std::move(switch_finished_callback));
  }
}

// static
void ProfilePicker::PickProfile(const base::FilePath& profile_path,
                                ProfilePickingArgs args) {
  if (g_profile_picker_view) {
    g_profile_picker_view->flow_controller_->PickProfile(profile_path, args);
  }
}

// static
void ProfilePicker::CancelSignedInFlow() {
  if (g_profile_picker_view) {
    g_profile_picker_view->flow_controller_.get()->CancelPostSignInFlow();
  }
}

// static
base::FilePath ProfilePicker::GetPickerProfilePath() {
  return ProfileManager::GetSystemProfilePath();
}

// static
void ProfilePicker::Hide() {
  if (g_profile_picker_view) {
    g_profile_picker_view->Clear();
  }
}

// static
bool ProfilePicker::IsOpen() {
  return g_profile_picker_view;
}

// static
bool ProfilePicker::IsFirstRunOpen() {
  return ProfilePicker::IsOpen() &&
         g_profile_picker_view->params_.entry_point() ==
             ProfilePicker::EntryPoint::kFirstRun;
}

bool ProfilePicker::IsActive() {
  if (!IsOpen()) {
    return false;
  }

#if BUILDFLAG(IS_MAC)
  return g_profile_picker_view->GetWidget() &&
         g_profile_picker_view->GetWidget()->IsVisible();
#else
  return g_profile_picker_view->GetWidget()->IsActive();
#endif
}

// static
views::WebView* ProfilePicker::GetWebViewForTesting() {
  if (!g_profile_picker_view) {
    return nullptr;
  }
  return g_profile_picker_view->web_view_;
}

// static
views::View* ProfilePicker::GetViewForTesting() {
  return g_profile_picker_view;
}

// static
void ProfilePicker::AddOnProfilePickerOpenedCallbackForTesting(
    base::OnceClosure callback) {
  DCHECK(!g_profile_picker_opened_callback_for_testing);
  DCHECK(!callback.is_null());
  g_profile_picker_opened_callback_for_testing =
      new base::OnceClosure(std::move(callback));
}

// ProfilePickerView::NavigationFinishedObserver ------------------------------

ProfilePickerView::NavigationFinishedObserver::NavigationFinishedObserver(
    const GURL& requested_url,
    base::OnceClosure closure,
    content::WebContents* contents)
    : content::WebContentsObserver(contents),
      requested_url_(requested_url),
      closure_(std::move(closure)) {}

ProfilePickerView::NavigationFinishedObserver::~NavigationFinishedObserver() =
    default;

void ProfilePickerView::NavigationFinishedObserver::DidFinishNavigation(
    content::NavigationHandle* navigation_handle) {
  if (!closure_ || !navigation_handle->HasCommitted()) {
    return;
  }

  if (navigation_handle->GetRedirectChain()[0] != requested_url_) {
    // Don't notify if the URL for the finishing navigation does not match.
    // The navigation may have been replaced by a new one. We are mindful to
    // allow redirections, which are necessary for example for Gaia sign-in
    // pages (see crbug.com/1430681).
    return;
  }

  if (navigation_handle->IsErrorPage() &&
      requested_url_.SchemeIs(content::kChromeUIScheme)) {
    // We observed some cases where the navigation to the intended page fails
    // (see crbug.com/1442159).
    // Loading the wrong URL may lead to crashes if we are expecting a certain
    // WebUI page to be loaded in the web contents. For these cases we will not
    // notify of the finished navigation to avoid crashing, but this negatively
    // affects the user experience anyway.
    // TODO(crbug.com/40911651): Improve the user experience for this error.
    base::debug::DumpWithoutCrashing();
    return;
  }

  std::move(closure_).Run();
}

// ProfilePickerView ----------------------------------------------------------

void ProfilePickerView::UpdateParams(ProfilePicker::Params&& params) {
  DCHECK(params_.CanReusePickerWindow(params));
  params_ = std::move(params);
}

void ProfilePickerView::ShowScreen(
    content::WebContents* contents,
    const GURL& url,
    base::OnceClosure navigation_finished_closure) {
  base::ScopedClosureRunner finish_init_runner;
  if (state_ == kInitializing) {
    finish_init_runner.ReplaceClosure(
        base::BindOnce(&ProfilePickerView::FinishInit, base::Unretained(this)));
  }

  if (url.is_empty()) {
    DCHECK(!navigation_finished_closure);
    ShowScreenFinished(contents);
    return;
  }

  contents->GetController().LoadURL(url, content::Referrer(),
                                    ui::PAGE_TRANSITION_AUTO_TOPLEVEL,
                                    std::string());

  // Special-case the first ever screen to make sure the WebView has a contents
  // assigned in the moment when it gets displayed. This avoids a black flash on
  // Win (and potentially other GPU artifacts on other platforms). The rest of
  // the work can still be done asynchronously in ShowScreenFinished().
  if (web_view_->GetWebContents() == nullptr) {
    web_view_->SetWebContents(contents);
  }

  // Binding as Unretained as `this` outlives member
  // `show_screen_finished_observer_`. If ShowScreen gets called twice in a
  // short period of time, the first callback may never get called as the first
  // observer gets destroyed here or later in ShowScreenFinished(). This is okay
  // as all the previous values get replaced by the new values.
  show_screen_finished_observer_ = std::make_unique<NavigationFinishedObserver>(
      url,
      base::BindOnce(&ProfilePickerView::ShowScreenFinished,
                     base::Unretained(this), contents,
                     std::move(navigation_finished_closure)),
      contents);

  if (!GetWidget()->IsVisible()) {
    GetWidget()->Show();
  }
}

void ProfilePickerView::ShowScreenInPickerContents(
    const GURL& url,
    base::OnceClosure navigation_finished_closure) {
  ShowScreen(contents_.get(), url, std::move(navigation_finished_closure));
}

void ProfilePickerView::Clear() {
  TRACE_EVENT1("browser,startup", "ProfilePickerView::Clear", "state", state_);
  if (state_ == kClosing) {
    return;
  }

  state_ = kClosing;

  if (GetWidget()) {
    GetWidget()->Close();
    return;
  }

  WindowClosing();
  // TODO(crbug.com/40232473): Here we set owned by widget to ensure the
  // DeleteDelegate() call deletes this instance. Once the full migration to
  // "client owns delegate" is done, this will need to change.
  SetOwnedByWidget(OwnedByWidgetPassKey());
  DeleteDelegate();
}

bool ProfilePickerView::ShouldUseDarkColors() const {
  return GetNativeTheme()->ShouldUseDarkColors();
}

content::WebContents* ProfilePickerView::GetPickerContents() const {
  return contents_.get();
}

content::WebContentsDelegate* ProfilePickerView::GetWebContentsDelegate() {
  return this;
}

web_modal::WebContentsModalDialogHost*
ProfilePickerView::GetWebContentsModalDialogHost() {
  return this;
}

void ProfilePickerView::Reset(StepSwitchFinishedCallback callback) {
  flow_controller_->Reset(std::move(callback));
}

void ProfilePickerView::SwitchToSignedOutPostIdentityFlow(
    std::optional<SkColor> profile_color,
    base::OnceCallback<void(bool)> switch_finished_callback) {
  ProfileManager::CreateMultiProfileAsync(
      g_browser_process->profile_manager()
          ->GetProfileAttributesStorage()
          .ChooseNameForNewProfile(),
      profiles::GetPlaceholderAvatarIndex(), /*is_hidden=*/true,
      base::BindOnce(&ProfilePickerView::OnLocalProfileInitialized,
                     weak_ptr_factory_.GetWeakPtr(), profile_color,
                     std::move(switch_finished_callback)));
}

void ProfilePickerView::OnLocalProfileInitialized(
    std::optional<SkColor> profile_color,
    base::OnceCallback<void(bool)> switch_finished_callback,
    Profile* profile) {
  if (!profile) {
    NOTREACHED() << "Local fail in creating new profile";
  }
  CHECK(!signin_util::IsForceSigninEnabled());

  // Apply a new color to the profile or use the default theme.
  // TODO(b/328587059): Share the theme color logic with the same code in
  // `profile_picker_flow_controller.cc`.
  auto* theme_service = ThemeServiceFactory::GetForProfile(profile);
  if (profile_color.has_value()) {
    theme_service->SetUserColorAndBrowserColorVariant(
        *profile_color, ui::mojom::BrowserColorVariant::kTonalSpot);
  } else {
    theme_service->UseDefaultTheme();
  }

  // TODO(crbug.com/40209493): Add shortcut creation.
  // Skip the FRE for this profile as sign-in was offered as part of the flow.
  profile->GetPrefs()->SetBoolean(prefs::kHasSeenWelcomePage, true);
  GetProfilePickerFlowController()->SwitchToSignedOutPostIdentityFlow(
      profile, std::move(switch_finished_callback));
}

#if BUILDFLAG(ENABLE_DICE_SUPPORT)
void ProfilePickerView::SetNativeToolbarVisible(bool visible) {
  if (!visible) {
    toolbar_->SetVisible(false);
    return;
  }

  if (toolbar_->children().empty()) {
    toolbar_->BuildToolbar(
        base::BindRepeating(&ProfilePickerView::NavigateBack,
                            // Binding as Unretained as `this` is the
                            // `toolbar_`'s parent and outlives it.
                            base::Unretained(this)));
  }
  toolbar_->SetVisible(true);
}

bool ProfilePickerView::IsNativeToolbarVisibleForTesting() const {
  return toolbar_->GetVisible();
}

SkColor ProfilePickerView::GetPreferredBackgroundColor() const {
  return GetColorProvider()->GetColor(kColorToolbar);
}
#endif

bool ProfilePickerView::HandleKeyboardEvent(
    content::WebContents* source,
    const input::NativeWebKeyboardEvent& event) {
  // Forward the keyboard event to AcceleratorPressed() through the
  // FocusManager.
  return unhandled_keyboard_event_handler_.HandleKeyboardEvent(
      event, GetFocusManager());
}

bool ProfilePickerView::HandleContextMenu(
    content::RenderFrameHost& render_frame_host,
    const content::ContextMenuParams& params) {
  // Ignores context menu.
  return true;
}

gfx::NativeView ProfilePickerView::GetHostView() const {
  return GetWidget()->GetNativeView();
}

gfx::Point ProfilePickerView::GetDialogPosition(const gfx::Size& size) {
  gfx::Size widget_size = GetWidget()->GetWindowBoundsInScreen().size();
  return gfx::Point(std::max(0, (widget_size.width() - size.width()) / 2), 0);
}

gfx::Size ProfilePickerView::GetMaximumDialogSize() {
  return GetWidget()->GetWindowBoundsInScreen().size();
}

void ProfilePickerView::AddObserver(
    web_modal::ModalDialogHostObserver* observer) {}

void ProfilePickerView::RemoveObserver(
    web_modal::ModalDialogHostObserver* observer) {}

ProfilePickerView::ProfilePickerView(ProfilePicker::Params&& params)
    : keep_alive_(KeepAliveOrigin::USER_MANAGER_VIEW,
                  KeepAliveRestartOption::DISABLED),
      params_(std::move(params)) {
  // Setup the WidgetDelegate.
  SetHasWindowSizeControls(true);
  SetTitle(kWindowTitleId);

  ConfigureAccelerators();

  // Record creation metrics.
  base::UmaHistogramEnumeration("ProfilePicker.Shown", params_.entry_point());
  if (params_.entry_point() == ProfilePicker::EntryPoint::kOnStartup) {
    DCHECK(creation_time_on_startup_.is_null());
    creation_time_on_startup_ = base::TimeTicks::Now();
    base::UmaHistogramTimes(
        "ProfilePicker.StartupTime.BeforeCreation",
        creation_time_on_startup_ -
            startup_metric_utils::GetCommon().MainEntryPointTicks());
  }
}

ProfilePickerView::~ProfilePickerView() {
  if (contents_) {
    contents_->SetDelegate(nullptr);
  }
}

bool ProfilePickerView::MaybeReopen(ProfilePicker::Params& params) {
  // Re-open if already closing or if the picker cannot be reused with `params`.
  if (state_ != kClosing && params.CanReusePickerWindow(params_)) {
    return false;
  }

  restart_on_window_closing_ =
      base::BindOnce(&ProfilePicker::Show, std::move(params));
  // No-op if already closing.
  ProfilePicker::Hide();
  return true;
}

void ProfilePickerView::Display() {
  DCHECK_NE(state_, kClosing);
  TRACE_EVENT2("browser,startup", "ProfilePickerView::Display", "entry_point",
               params_.entry_point(), "state", state_);

  if (state_ == kNotStarted) {
    state_ = kInitializing;
    // Build the layout synchronously before creating the picker profile to
    // simplify tests.
    BuildLayout();
    g_browser_process->profile_manager()->CreateProfileAsync(
        params_.profile_path(),
        base::BindOnce(&ProfilePickerView::OnPickerProfileCreated,
                       weak_ptr_factory_.GetWeakPtr()));
    return;
  }

  if (state_ == kInitializing) {
    return;
  }

  GetWidget()->Activate();
}

void ProfilePickerView::OnPickerProfileCreated(Profile* picker_profile) {
  TRACE_EVENT1(
      "browser,startup", "ProfilePickerView::OnPickerProfileCreated",
      "profile_path",
      (picker_profile ? picker_profile->GetPath().AsUTF8Unsafe() : ""));
  DCHECK(picker_profile);
  Init(picker_profile);

  InitializeFeaturePromo(picker_profile);
}

void ProfilePickerView::Init(Profile* picker_profile) {
  DCHECK_EQ(state_, kInitializing);
  TRACE_EVENT1(
      "browser,startup", "ProfilePickerView::Init", "profile_path",
      (picker_profile ? picker_profile->GetPath().AsUTF8Unsafe() : ""));
  contents_ = content::WebContents::Create(
      content::WebContents::CreateParams(picker_profile));
  contents_->SetDelegate(this);

  // Destroy the System Profile when the ProfilePickerView is closed (assuming
  // its refcount hits 0). We need to use GetOriginalProfile() here because
  // |profile_picker| is an OTR Profile, and ScopedProfileKeepAlive only
  // supports non-OTR Profiles. Trying to acquire a keepalive on the OTR Profile
  // would trigger a DCHECK.
  //
  // TODO(crbug.com/40159237): Once OTR Profiles use refcounting, remove the
  // call to GetOriginalProfile(). The OTR Profile will hold a keepalive on the
  // regular Profile, so the ownership model will be more straightforward.
  profile_keep_alive_ = std::make_unique<ScopedProfileKeepAlive>(
      picker_profile->GetOriginalProfile(),
      ProfileKeepAliveOrigin::kProfilePickerView);

  // The `FlowController` is created before the widget so it can be used to
  // determine certain aspects of it. E.g. see `GetAccessibleWindowTitle()`.
  flow_controller_ = CreateFlowController(picker_profile, GetClearClosure());

  // The widget is owned by the native widget.
  new ProfilePickerWidget(this);

#if BUILDFLAG(IS_WIN)
  // Set the app id for the user manager to the app id of its parent.
  ui::win::SetAppIdForWindow(
      shell_integration::win::GetAppUserModelIdForBrowser(
          picker_profile->GetPath()),
      views::HWNDForWidget(GetWidget()));
#endif

  DCHECK(flow_controller_);
  flow_controller_->Init();
}

void ProfilePickerView::FinishInit() {
  DCHECK_EQ(kInitializing, state_);
  state_ = kDisplayed;

  if (IsClassicProfilePickerFlow(params_)) {
    PrefService* prefs = g_browser_process->local_state();
    prefs->SetBoolean(prefs::kBrowserProfilePickerShown, true);
  }

  if (params_.entry_point() == ProfilePicker::EntryPoint::kOnStartup) {
    DCHECK(!creation_time_on_startup_.is_null());
    base::UmaHistogramTimes("ProfilePicker.StartupTime.WebViewCreated",
                            base::TimeTicks::Now() - creation_time_on_startup_);
  }

  if (g_profile_picker_opened_callback_for_testing) {
    std::move(*g_profile_picker_opened_callback_for_testing).Run();
    delete g_profile_picker_opened_callback_for_testing;
    g_profile_picker_opened_callback_for_testing = nullptr;
  }
}

std::unique_ptr<ProfileManagementFlowController>
ProfilePickerView::CreateFlowController(Profile* picker_profile,
                                        ClearHostClosure clear_host_callback) {
#if BUILDFLAG(ENABLE_DICE_SUPPORT)
  if (params_.entry_point() == ProfilePicker::EntryPoint::kFirstRun) {
    auto first_run_exited_callback =
        base::BindOnce(&ProfilePicker::Params::NotifyFirstRunExited,
                       // Unretained ok because the controller is owned
                       // by this through `initialized_steps_`.
                       base::Unretained(&params_));
    return std::make_unique<FirstRunFlowControllerDice>(
        /*host=*/this, std::move(clear_host_callback), picker_profile,
        std::move(first_run_exited_callback));
  }
#endif

  if (params_.entry_point() == ProfilePicker::EntryPoint::kGlicManager) {
    auto profile_picked_callback =
        base::BindOnce(&ProfilePicker::Params::NotifyProfilePicked,
                       // Unretained ok because the controller is owned
                       // by this through `initialized_steps_`.
                       base::Unretained(&params_));
    return std::make_unique<ProfilePickerGlicFlowController>(
        /*host=*/this, std::move(clear_host_callback),
        std::move(profile_picked_callback));
  }

  DCHECK(IsClassicProfilePickerFlow(params_));
  return std::make_unique<ProfilePickerFlowController>(
      /*host=*/this, std::move(clear_host_callback), params_.entry_point(),
      params_.on_select_profile_target_url());
}

#if BUILDFLAG(ENABLE_DICE_SUPPORT)
void ProfilePickerView::SwitchToDiceSignIn(
    ProfilePicker::ProfileInfo profile_info,
    base::OnceCallback<void(bool)> switch_finished_callback) {
  GetProfilePickerFlowController()->SwitchToDiceSignIn(
      std::move(profile_info), std::move(switch_finished_callback));
}

void ProfilePickerView::SwitchToReauth(
    Profile* profile,
    base::OnceCallback<void(const ForceSigninUIError&)> on_error_callback) {
  GetProfilePickerFlowController()->SwitchToReauth(
      profile, std::move(on_error_callback));
}
#endif

void ProfilePickerView::WindowClosing() {
  // If a profile is locked, it might have been loaded and it's first browser
  // will never be created, we need to remove it's equivalent
  // `ProfileKeepAliveOrigin::kWaitingForFirstBrowserWindow` to be able to
  // delete the profile.
  ClearLockedProfilesFirstBrowserKeepAlive();

  views::WidgetDelegateView::WindowClosing();
  // Now that the window is closed, we can allow a new one to be opened.
  // (WindowClosing comes in asynchronously from the call to Close() and we
  // may have already opened a new instance).
  if (g_profile_picker_view == this) {
    g_profile_picker_view = nullptr;
  }

  // Show a new profile window if it has been requested while the current window
  // was closing.
  if (state_ == kClosing && restart_on_window_closing_) {
    std::move(restart_on_window_closing_).Run();
  }
}

views::ClientView* ProfilePickerView::CreateClientView(views::Widget* widget) {
  return new views::ClientView(widget, TransferOwnershipOfContentsView());
}

views::View* ProfilePickerView::GetContentsView() {
  return this;
}

std::u16string ProfilePickerView::GetAccessibleWindowTitle() const {
  if (web_view_ && web_view_->GetWebContents() &&
      !web_view_->GetWebContents()->GetTitle().empty()) {
    return web_view_->GetWebContents()->GetTitle();
  }

  auto flow_fallback_title =
      flow_controller_->GetFallbackAccessibleWindowTitle();
  if (!flow_fallback_title.empty()) {
    return flow_fallback_title;
  }

  return l10n_util::GetStringUTF16(kWindowTitleId);
}

gfx::Size ProfilePickerView::CalculatePreferredSize(
    const views::SizeBounds& available_size) const {
  gfx::Size preferred_size = gfx::Size(kWindowWidth, kWindowHeight);
  gfx::Size work_area_size = GetWidget()->GetWorkAreaBoundsInScreen().size();
  // Keep the window smaller then |work_area_size| so that it feels more like a
  // dialog then like the actual Chrome window.
  gfx::Size max_dialog_size = ScaleToFlooredSize(
      work_area_size, kMaxRatioOfWorkArea, kMaxRatioOfWorkArea);
  preferred_size.SetToMin(max_dialog_size);
  return preferred_size;
}

gfx::Size ProfilePickerView::GetMinimumSize() const {
  // On small screens, the preferred size may be smaller than the picker
  // minimum size. In that case there will be scrollbars on the picker.
  gfx::Size minimum_size = GetPreferredSize();
  minimum_size.SetToMin(ProfilePickerUI::GetMinimumSize());
  return minimum_size;
}

bool ProfilePickerView::AcceleratorPressed(const ui::Accelerator& accelerator) {
  const auto& iter = accelerator_table_.find(accelerator);
  CHECK(iter != accelerator_table_.end());
  int command_id = iter->second;
  switch (command_id) {
    case IDC_CLOSE_TAB:
    case IDC_CLOSE_WINDOW:
      // kEscKeyPressed is used although that shortcut is disabled (this is
      // Ctrl/Cmd-W instead).
      GetWidget()->CloseWithReason(views::Widget::ClosedReason::kEscKeyPressed);
      break;
    case IDC_EXIT:
      // Stop the browser from re-opening when we close Chrome while
      // in the first run experience.
      params_.NotifyFirstRunExited(
          ProfilePicker::FirstRunExitStatus::kAbandonedFlow);
      chrome::AttemptUserExit();
      break;
    case IDC_FULLSCREEN:
      GetWidget()->SetFullscreen(!GetWidget()->IsFullscreen());
      break;
    case IDC_MINIMIZE_WINDOW:
      GetWidget()->Minimize();
      break;
    case IDC_BACK: {
      NavigateBack();
      break;
    }
#if BUILDFLAG(ENABLE_DICE_SUPPORT)
    // Always reload bypassing cache.
    case IDC_RELOAD:
    case IDC_RELOAD_BYPASSING_CACHE:
    case IDC_RELOAD_CLEARING_CACHE:
      flow_controller_->OnReloadRequested();
      break;

#endif
    default:
      NOTREACHED() << "Unexpected command_id: " << command_id;
  }

  return true;
}

bool ProfilePickerView::GetAcceleratorForCommandId(
    int command_id,
    ui::Accelerator* accelerator) const {
  for (const auto& [accelerator_entry, command_id_entry] : accelerator_table_) {
    if (command_id == command_id_entry) {
      *accelerator = accelerator_entry;
      return true;
    }
  }
  return false;
}

void ProfilePickerView::BuildLayout() {
  SetLayoutManager(std::make_unique<views::FlexLayout>())
      ->SetOrientation(views::LayoutOrientation::kVertical)
      .SetMainAxisAlignment(views::LayoutAlignment::kStart)
      .SetCrossAxisAlignment(views::LayoutAlignment::kStretch)
      .SetDefault(
          views::kFlexBehaviorKey,
          views::FlexSpecification(views::MinimumFlexSizeRule::kScaleToMinimum,
                                   views::MaximumFlexSizeRule::kUnbounded));

#if BUILDFLAG(ENABLE_DICE_SUPPORT)
  auto toolbar = std::make_unique<ProfilePickerDiceSignInToolbar>();
  toolbar_ = AddChildView(std::move(toolbar));
  // Toolbar gets built and set visible once we it's needed for the Dice signin.
  SetNativeToolbarVisible(false);
#endif

  auto web_view = std::make_unique<views::WebView>();
  web_view->set_allow_accelerators(true);
  web_view_ = AddChildView(std::move(web_view));

  web_contents_attached_subscription_ =
      web_view_->AddWebContentsAttachedCallback(base::BindRepeating(
          &ProfilePickerView::UpdateAccessibleNameForRootView,
          weak_ptr_factory_.GetWeakPtr()));
}

void ProfilePickerView::ShowScreenFinished(
    content::WebContents* contents,
    base::OnceClosure navigation_finished_closure) {
  // Stop observing for this (or any previous) navigation.
  if (show_screen_finished_observer_) {
    show_screen_finished_observer_.reset();
  }

  web_view_->SetWebContents(contents);
  contents->Focus();

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

void ProfilePickerView::NavigateBack() {
  flow_controller_->OnNavigateBackRequested();
}

void ProfilePickerView::ConfigureAccelerators() {
  const std::vector<AcceleratorMapping> accelerator_list(GetAcceleratorList());
  for (const auto& entry : accelerator_list) {
    if (!base::Contains(kSupportedAcceleratorCommands, entry.command_id)) {
      continue;
    }
    ui::Accelerator accelerator(entry.keycode, entry.modifiers);
    accelerator_table_[accelerator] = entry.command_id;
    AddAccelerator(accelerator);
  }

#if BUILDFLAG(IS_MAC)
  // Check Mac-specific accelerators. Note: Chrome does not support dynamic or
  // user-configured accelerators on Mac. Default static accelerators are used
  // instead.
  for (int command_id : kSupportedAcceleratorCommands) {
    ui::Accelerator accelerator;
    bool mac_accelerator_found =
        GetDefaultMacAcceleratorForCommandId(command_id, &accelerator);
    if (mac_accelerator_found) {
      accelerator_table_[accelerator] = command_id;
      AddAccelerator(accelerator);
    }
  }
#endif  // BUILDFLAG(IS_MAC)
}

void ProfilePickerView::InitializeFeaturePromo(Profile* system_profile) {
  feature_engagement::Tracker* const tracker_service =
      feature_engagement::TrackerFactory::GetForBrowserContext(system_profile);
  UserEducationService* const user_education_service =
      UserEducationServiceFactory::GetForBrowserContext(system_profile);

  feature_promo_ = std::make_unique<ProfilePickerFeaturePromoController>(
      tracker_service, user_education_service, g_profile_picker_view);
}

ProfilePickerFlowController* ProfilePickerView::GetProfilePickerFlowController()
    const {
  DCHECK(IsClassicProfilePickerFlow(params_));
  return static_cast<ProfilePickerFlowController*>(flow_controller_.get());
}

ClearHostClosure ProfilePickerView::GetClearClosure() {
  return ClearHostClosure(base::BindOnce(&ProfilePickerView::Clear,
                                         weak_ptr_factory_.GetWeakPtr()));
}

void ProfilePickerView::UpdateAccessibleNameForRootView(views::WebView*) {
  if (GetWidget()) {
    GetWidget()->UpdateAccessibleNameForRootView();
  }
}

void ProfilePickerView::ShowForceSigninErrorDialog(
    const ForceSigninUIError& error,
    bool switch_step_success) {
  if (!switch_step_success) {
    return;
  }

  CHECK(signin_util::IsForceSigninEnabled());
  ProfilePickerUI* web_ui = web_view_->GetWebContents()
                                ->GetWebUI()
                                ->GetController()
                                ->GetAs<ProfilePickerUI>();
  web_ui->ShowForceSigninErrorDialog(error);
}

BEGIN_METADATA(ProfilePickerView)
END_METADATA