File: profile_picker_flow_controller.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (804 lines) | stat: -rw-r--r-- 32,980 bytes parent folder | download | duplicates (2)
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
// Copyright 2022 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_flow_controller.h"

#include <string>
#include <variant>

#include "base/functional/bind.h"
#include "base/functional/callback_forward.h"
#include "base/functional/callback_helpers.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/trace_event/trace_event.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/metrics/first_web_contents_profiler_base.h"
#include "chrome/browser/profiles/delete_profile_helper.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profile_metrics.h"
#include "chrome/browser/profiles/profile_window.h"
#include "chrome/browser/search_engine_choice/search_engine_choice_dialog_service.h"
#include "chrome/browser/search_engine_choice/search_engine_choice_dialog_service_factory.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/themes/theme_service.h"
#include "chrome/browser/themes/theme_service_factory.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_window/public/browser_window_features.h"
#include "chrome/browser/ui/chrome_pages.h"
#include "chrome/browser/ui/profiles/profile_customization_util.h"
#include "chrome/browser/ui/signin/signin_view_controller.h"
#include "chrome/browser/ui/singleton_tabs.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/frame/toolbar_button_provider.h"
#include "chrome/browser/ui/views/profiles/avatar_toolbar_button.h"
#include "chrome/browser/ui/views/profiles/profile_customization_bubble_sync_controller.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_step_controller.h"
#include "chrome/browser/ui/views/profiles/profile_management_types.h"
#include "chrome/browser/ui/views/profiles/profile_picker_dice_reauth_provider.h"
#include "chrome/browser/ui/views/profiles/profile_picker_signed_in_flow_controller.h"
#include "chrome/browser/ui/views/profiles/profile_picker_web_contents_host.h"
#include "chrome/browser/ui/webui/signin/profile_picker_handler.h"
#include "chrome/browser/ui/webui/signin/signin_ui_error.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/grit/branded_strings.h"
#include "chrome/grit/generated_resources.h"
#include "components/signin/public/base/signin_metrics.h"
#include "google_apis/gaia/core_account_id.h"
#include "third_party/abseil-cpp/absl/functional/overload.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/mojom/themes.mojom.h"

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

namespace {

const signin_metrics::AccessPoint kAccessPoint =
    signin_metrics::AccessPoint::kUserManager;

// Returns the URL to load as initial content for the profile picker. If an
// empty URL is returned, the profile picker should not be shown until
// another explicit call with a non-empty URL given to the view
// (see `ProfilePickerView::ShowScreen()` for example)
GURL GetInitialURL(ProfilePicker::EntryPoint entry_point) {
  GURL base_url = GURL(chrome::kChromeUIProfilePickerUrl);
  switch (entry_point) {
    case ProfilePicker::EntryPoint::kOnStartup:
    case ProfilePicker::EntryPoint::kOnStartupNoProfile: {
      GURL::Replacements replacements;
      replacements.SetQueryStr(chrome::kChromeUIProfilePickerStartupQuery);
      return base_url.ReplaceComponents(replacements);
    }
    case ProfilePicker::EntryPoint::kProfileMenuManageProfiles:
    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::kNewSessionOnExistingProcessNoProfile:
    case ProfilePicker::EntryPoint::kAppMenuProfileSubMenuManageProfiles:
      return base_url;
    case ProfilePicker::EntryPoint::kProfileMenuAddNewProfile:
    case ProfilePicker::EntryPoint::kAppMenuProfileSubMenuAddNewProfile:
      return base_url.Resolve("new-profile");
    case ProfilePicker::EntryPoint::kFirstRun:
    case ProfilePicker::EntryPoint::kGlicManager:
      // Should not be used for this entry point.
      NOTREACHED();
  }
}

// Shows the customization bubble if possible. The bubble won't be shown if the
// color is enforced by policy or downloaded through Sync or the default theme
// should be used. An IPH is shown after the bubble, or right away if the bubble
// cannot be shown.
void ShowCustomizationBubble(std::optional<SkColor> new_profile_color,
                             Browser* browser) {
  DCHECK(browser);
  BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser);
  if (!browser_view || !browser_view->toolbar_button_provider()) {
    return;
  }

  if (ProfileCustomizationBubbleSyncController::CanThemeSyncStart(
          browser->profile())) {
    // For sync users, their profile color has not been applied yet. Call a
    // helper class that applies the color and shows the bubble only if there is
    // no conflict with a synced theme / color.
    ProfileCustomizationBubbleSyncController::
        ApplyColorAndShowBubbleWhenNoValueSynced(
            browser,
            /*suggested_profile_color=*/new_profile_color.value());
  } else {
    // For non syncing users, simply show the bubble.
    browser->GetFeatures()
        .signin_view_controller()
        ->ShowModalProfileCustomizationDialog();
  }
}

void MaybeShowProfileIPHs(Browser* browser) {
  DCHECK(browser);
  BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser);
  if (!browser_view) {
    return;
  }
  browser_view->MaybeShowSupervisedUserProfileSignInIPH();
  browser_view->MaybeShowProfileSwitchIPH();
}

// Class triggering the signed-in section of the profile management flow, most
// notably featuring the sync confirmation. In addition to what its base class
// `ProfilePickerSignedInFlowController` is doing, this class:
// - shows in product help and customization bubble at the end of the flow
// - applies profile customizations (theme, profile name)
// - finalizes the profile (deleting it if the flow is aborted, marks it
//   non-ephemeral if the flow is completed)
// `step_completed_callback` is not called if the flow is canceled.
// Note that `account_id` has been added to the `IdentityManager` but may not
// be set as primary yet, because this operation is asynchronous.
class ProfileCreationSignedInFlowController
    : public ProfilePickerSignedInFlowController {
 public:
  ProfileCreationSignedInFlowController(
      ProfilePickerWebContentsHost* host,
      Profile* profile,
      const CoreAccountInfo& account_info,
      std::unique_ptr<content::WebContents> contents,
      std::optional<SkColor> profile_color,
      base::OnceCallback<void(PostHostClearedCallback, bool)>
          step_completed_callback)
      : ProfilePickerSignedInFlowController(host,
                                            profile,
                                            account_info,
                                            std::move(contents),
                                            kAccessPoint,
                                            profile_color),
        step_completed_callback_(std::move(step_completed_callback)) {}

  ProfileCreationSignedInFlowController(
      const ProfilePickerSignedInFlowController&) = delete;
  ProfileCreationSignedInFlowController& operator=(
      const ProfilePickerSignedInFlowController&) = delete;

  ~ProfileCreationSignedInFlowController() override {
    // Record unfinished signed-in profile creation.
    if (!is_finishing_) {
      // TODO(crbug.com/40216113): Consider moving this recording into
      // ProfilePickerTurnSyncOnDelegate and unify this code with Cancel().
      ProfileMetrics::LogProfileAddSignInFlowOutcome(
          ProfileMetrics::ProfileSignedInFlowOutcome::kAbortedAfterSignIn);
    }
  }

  // ProfilePickerSignedInFlowController:
  void Init(StepSwitchFinishedCallback step_switch_callback) override {
    // Stop with the sign-in navigation and show a spinner instead. The spinner
    // will be shown until TurnSyncOnHelper figures out whether it's a
    // managed account and whether sync is disabled by policies (which in some
    // cases involves fetching policies and can take a couple of seconds).
    host()->ShowScreen(contents(), GetSyncConfirmationURL(/*loading=*/true),
                       base::OnceClosure());

    ProfilePickerSignedInFlowController::Init(std::move(step_switch_callback));

    // Listen for extended account info getting fetched.
    signin::IdentityManager* identity_manager =
        IdentityManagerFactory::GetForProfile(profile());
    profile_name_resolver_ =
        std::make_unique<ProfileNameResolver>(identity_manager, account_info());
  }

  void Cancel() override {
    if (is_finishing_) {
      return;
    }

    is_finishing_ = true;
  }

  void FinishAndOpenBrowserInternal(PostHostClearedCallback callback,
                                    bool is_continue_callback) override {
    // Do nothing if the sign-in flow is aborted or if this has already been
    // called. Note that this can get called first time from a special case
    // handling (such as the Settings link) and than second time when the
    // TurnSyncOnHelper finishes.
    if (is_finishing_) {
      return;
    }
    is_finishing_ = true;
    callback = CombineCallbacks<PostHostClearedCallback, Browser*>(
        std::move(callback), CreateFreshProfileExperienceCallback());

    profile_name_resolver_->RunWithProfileName(base::BindOnce(
        &ProfileCreationSignedInFlowController::FinishFlow,
        // Unretained ok: `this` outlives `profile_name_resolver_`.
        base::Unretained(this), std::move(callback), is_continue_callback));
  }

 private:
  PostHostClearedCallback CreateFreshProfileExperienceCallback() {
    // If there's no color to apply to the profile, skip the customization
    // bubble and trigger an IPH, instead.
    if (ThemeServiceFactory::GetForProfile(profile())->UsingPolicyTheme() ||
        !GetProfileColor().has_value()) {
      return PostHostClearedCallback(base::BindOnce(&MaybeShowProfileIPHs));
    } else {
      // If sync cannot start, we apply `GetProfileColor()` right away before
      // opening a browser window to avoid flicker. Otherwise, it's applied
      // later by code triggered from ShowCustomizationBubble().
      if (!ProfileCustomizationBubbleSyncController::CanThemeSyncStart(
              profile())) {
        auto* theme_service = ThemeServiceFactory::GetForProfile(profile());
        theme_service->SetUserColorAndBrowserColorVariant(
            GetProfileColor().value(),
            ui::mojom::BrowserColorVariant::kTonalSpot);
      }
      return PostHostClearedCallback(
          base::BindOnce(&ShowCustomizationBubble, GetProfileColor()));
    }
  }

  void FinishFlow(PostHostClearedCallback post_host_cleared_callback,
                  bool is_continue_callback,
                  std::u16string name_for_signed_in_profile) {
    TRACE_EVENT1("browser", "ProfileCreationSignedInFlowController::FinishFlow",
                 "profile_path", profile()->GetPath().AsUTF8Unsafe());
    CHECK(!name_for_signed_in_profile.empty());
    DCHECK(post_host_cleared_callback.value());
    DCHECK(step_completed_callback_);

    profile_name_resolver_.reset();

    FinalizeNewProfileSetup(profile(), name_for_signed_in_profile,
                            /*is_default_name=*/false);

    ProfileMetrics::LogProfileAddNewUser(
        ProfileMetrics::ADD_NEW_PROFILE_PICKER_SIGNED_IN);

    std::move(step_completed_callback_)
        .Run(std::move(post_host_cleared_callback), is_continue_callback);
  }

  // Controls whether the flow still needs to finalize (which includes showing
  // `profile` browser window at the end of the sign-in flow).
  bool is_finishing_ = false;

  std::unique_ptr<ProfileNameResolver> profile_name_resolver_;
  base::OnceCallback<void(PostHostClearedCallback, bool)>
      step_completed_callback_;
};

#if BUILDFLAG(ENABLE_DICE_SUPPORT)
class ReauthFlowStepController : public ProfileManagementStepController {
 public:
  explicit ReauthFlowStepController(
      ProfilePickerWebContentsHost* host,
      std::unique_ptr<ProfilePickerDiceReauthProvider> reauth_provider,
      Profile* profile)
      : ProfileManagementStepController(host),
        reauth_provider_(std::move(reauth_provider)) {}

  ~ReauthFlowStepController() override = default;

  void Show(StepSwitchFinishedCallback step_shown_callback,
            bool reset_state) override {
    reauth_provider_->SwitchToReauth(std::move(step_shown_callback));
  }

  void OnHidden() override { host()->SetNativeToolbarVisible(false); }

  void OnNavigateBackRequested() override {
    NavigateBackInternal(reauth_provider_->contents());
  }

 private:
  std::unique_ptr<ProfilePickerDiceReauthProvider> reauth_provider_;
};

std::unique_ptr<ProfileManagementStepController> CreateReauthtep(
    ProfilePickerWebContentsHost* host,
    Profile* profile,
    base::OnceCallback<void(bool, const ForceSigninUIError&)>
        on_reauth_completed) {
  ProfileAttributesEntry* entry =
      g_browser_process->profile_manager()
          ->GetProfileAttributesStorage()
          .GetProfileAttributesWithPath(profile->GetPath());

  return std::make_unique<ReauthFlowStepController>(
      host,
      std::make_unique<ProfilePickerDiceReauthProvider>(
          host, profile, entry->GetGAIAId(),
          base::UTF16ToUTF8(entry->GetUserName()),
          std::move(on_reauth_completed)),
      profile);
}
#endif

void RecordProfilingFinishReason(
    metrics::StartupProfilingFinishReason finish_reason) {
  base::UmaHistogramEnumeration(
      "ProfilePicker.FirstProfileTime.FirstWebContentsFinishReason",
      finish_reason);
}

class FirstWebContentsProfilerForProfilePicker
    : public metrics::FirstWebContentsProfilerBase {
 public:
  explicit FirstWebContentsProfilerForProfilePicker(
      content::WebContents* web_contents,
      base::TimeTicks pick_time);

  FirstWebContentsProfilerForProfilePicker(
      const FirstWebContentsProfilerForProfilePicker&) = delete;
  FirstWebContentsProfilerForProfilePicker& operator=(
      const FirstWebContentsProfilerForProfilePicker&) = delete;

 protected:
  // FirstWebContentsProfilerBase:
  void RecordFinishReason(
      metrics::StartupProfilingFinishReason finish_reason) override;
  void RecordNavigationFinished(base::TimeTicks navigation_start) override;
  void RecordFirstNonEmptyPaint() override;
  bool WasStartupInterrupted() override;

 private:
  ~FirstWebContentsProfilerForProfilePicker() override;

  const base::TimeTicks pick_time_;
};

FirstWebContentsProfilerForProfilePicker::
    FirstWebContentsProfilerForProfilePicker(content::WebContents* web_contents,
                                             base::TimeTicks pick_time)
    : FirstWebContentsProfilerBase(web_contents), pick_time_(pick_time) {
  DCHECK(!pick_time_.is_null());
}

FirstWebContentsProfilerForProfilePicker::
    ~FirstWebContentsProfilerForProfilePicker() = default;

void FirstWebContentsProfilerForProfilePicker::RecordFinishReason(
    metrics::StartupProfilingFinishReason finish_reason) {
  RecordProfilingFinishReason(finish_reason);
}

void FirstWebContentsProfilerForProfilePicker::RecordNavigationFinished(
    base::TimeTicks navigation_start) {
  // Nothing to record here for Profile Picker startups.
}

void FirstWebContentsProfilerForProfilePicker::RecordFirstNonEmptyPaint() {
  const char histogram_name[] =
      "ProfilePicker.FirstProfileTime.FirstWebContentsNonEmptyPaint";
  base::TimeTicks paint_time = base::TimeTicks::Now();
  base::UmaHistogramLongTimes100(histogram_name, paint_time - pick_time_);
  TRACE_EVENT_NESTABLE_ASYNC_BEGIN_WITH_TIMESTAMP0("startup", histogram_name,
                                                   this, pick_time_);
  TRACE_EVENT_NESTABLE_ASYNC_END_WITH_TIMESTAMP0("startup", histogram_name,
                                                 this, paint_time);
}

bool FirstWebContentsProfilerForProfilePicker::WasStartupInterrupted() {
  // We're assuming that no interruptions block opening an existing profile
  // from the profile picker. We would detect this by observing really high
  // latency on the tracked metric, and can start tracking interruptions if we
  // find that such cases occur.
  return false;
}

// Measures time to display the first web contents.
void BeginFirstWebContentsProfiling(Browser* browser,
                                    base::TimeTicks pick_time) {
  content::WebContents* visible_contents =
      metrics::FirstWebContentsProfilerBase::GetVisibleContents(browser);
  if (!visible_contents) {
    RecordProfilingFinishReason(metrics::StartupProfilingFinishReason::
                                    kAbandonNoInitiallyVisibleContent);
    return;
  }

  if (visible_contents->CompletedFirstVisuallyNonEmptyPaint()) {
    RecordProfilingFinishReason(
        metrics::StartupProfilingFinishReason::kAbandonAlreadyPaintedContent);
    return;
  }

  // FirstWebContentsProfilerForProfilePicker owns itself and is also bound to
  // |visible_contents|'s lifetime by observing WebContentsDestroyed().
  new FirstWebContentsProfilerForProfilePicker(visible_contents, pick_time);
}

void ShowLocalProfileCustomization(
    base::TimeTicks profile_picked_time_on_startup,
    Browser* browser) {
  if (!browser) {
    // TODO(crbug.com/40242414): Make sure we do something or log an error if
    // opening a browser window was not possible.
    return;
  }

  DCHECK(browser->window());
  Profile* profile = browser->profile();

  TRACE_EVENT1("browser", "ShowLocalProfileCustomization", "profile_path",
               profile->GetPath().AsUTF8Unsafe());

  if (!profile_picked_time_on_startup.is_null()) {
    BeginFirstWebContentsProfiling(browser, profile_picked_time_on_startup);
  }

  browser->GetFeatures()
      .signin_view_controller()
      ->ShowModalProfileCustomizationDialog(
          /*is_local_profile_creation=*/true);
}

void MaybeOpenPageInBrowser(Browser* browser,
                            const GURL& target_page_url,
                            bool open_settings) {
  // User clicked 'Edit' from the profile card menu.
  if (open_settings) {
    chrome::ShowSettingsSubPage(browser, chrome::kManageProfileSubPage);
    return;
  }

  // If no url is provided, proceed with the normal profile startup tabs
  // behaviour.
  if (target_page_url.is_empty()) {
    return;
  }

  // Opens the target url upon user selecting a pre-existing profile.
  if (target_page_url.spec() == chrome::kChromeUIHelpURL) {
    chrome::ShowAboutChrome(browser);
  } else if (target_page_url.spec() == chrome::kChromeUISettingsURL) {
    chrome::ShowSettings(browser);
  } else if (target_page_url.spec() == ProfilePicker::kTaskManagerUrl) {
    chrome::OpenTaskManager(browser);
  } else {
    ShowSingletonTabOverwritingNTP(browser, target_page_url);
  }
}

}  // namespace

ProfilePickerFlowController::ProfilePickerFlowController(
    ProfilePickerWebContentsHost* host,
    ClearHostClosure clear_host_callback,
    ProfilePicker::EntryPoint entry_point,
    const GURL& selected_profile_target_url)
    : ProfileManagementFlowControllerImpl(
          host,
          std::move(clear_host_callback),
          /*flow_type_string=*/"ProfilePickerFlow"),
      entry_point_(entry_point),
      selected_profile_target_url_(selected_profile_target_url) {}

ProfilePickerFlowController::~ProfilePickerFlowController() = default;

void ProfilePickerFlowController::Init() {
  RegisterStep(Step::kProfilePicker,
               ProfileManagementStepController::CreateForProfilePickerApp(
                   host(), GetInitialURL(entry_point_)));
  SwitchToStep(Step::kProfilePicker, /*reset_state=*/true);
}

#if BUILDFLAG(ENABLE_DICE_SUPPORT)
void ProfilePickerFlowController::SwitchToDiceSignIn(
    ProfilePicker::ProfileInfo profile_info,
    StepSwitchFinishedCallback switch_finished_callback) {
  DCHECK_EQ(Step::kProfilePicker, current_step());

  base::FilePath profile_path;
  // Split the variant information from `profile_info`.
  std::visit(absl::Overload{
                 [&suggested_profile_color =
                      suggested_profile_color_](std::optional<SkColor> color) {
                   suggested_profile_color = color;
                 },
                 [&profile_path](base::FilePath profile_path_info) {
                   profile_path = profile_path_info;
                 },
             },
             profile_info);

  SwitchToIdentityStepsFromAccountSelection(std::move(switch_finished_callback),
                                            kAccessPoint,
                                            std::move(profile_path));
}

void ProfilePickerFlowController::SwitchToReauth(
    Profile* profile,
    StepSwitchFinishedCallback switch_finished_callback,
    base::OnceCallback<void(const ForceSigninUIError&)> on_error_callback) {
  DCHECK_EQ(Step::kProfilePicker, current_step());

  // if the step was already initialized, unregister to make sure the new
  // reauth is properly initialised and the current reauth step is cleaned.
  //
  // TODO(crbug.com/40280498): Cleanup the unregistration of the step with a
  // proper resetable state within the `ProfilePickerDiceReauthProvider`, and
  // using the `ProfileManagementFlowController::SwitchToStep()` `reset_state`
  // value to trigger the reset.
  if (IsStepInitialized(Step::kReauth)) {
    UnregisterStep(Step::kReauth);
  }

  RegisterStep(
      Step::kReauth,
      CreateReauthtep(
          host(), profile,
          base::BindOnce(&ProfilePickerFlowController::OnReauthCompleted,
                         base::Unretained(this), profile,
                         std::move(on_error_callback))));

  SwitchToStep(
      Step::kReauth, true, std::move(switch_finished_callback),
      /*pop_step_callback=*/CreateSwitchToStepPopCallback(current_step()));
}

void ProfilePickerFlowController::OnReauthCompleted(
    Profile* profile,
    base::OnceCallback<void(const ForceSigninUIError&)> on_error_callback,
    bool success,
    const ForceSigninUIError& error) {
  if (!success) {
    CHECK_NE(error.type(), ForceSigninUIError::Type::kNone);

    SwitchToStep(
        Step::kProfilePicker, /*reset_state=*/true,
        StepSwitchFinishedCallback(base::BindOnce(
            &ProfilePickerFlowController::OnProfilePickerStepShownReauthError,
            base::Unretained(this), std::move(on_error_callback), error)));
    return;
  }

  g_browser_process->profile_manager()
      ->GetProfileAttributesStorage()
      .GetProfileAttributesWithPath(profile->GetPath())
      ->LockForceSigninProfile(false);

  FinishFlowAndRunInBrowser(profile, PostHostClearedCallback());
}

void ProfilePickerFlowController::OnProfilePickerStepShownReauthError(
    base::OnceCallback<void(const ForceSigninUIError&)> on_error_callback,
    const ForceSigninUIError& error,
    bool switch_step_success) {
  // If the step switch to the profile picker was not successful, do not proceed
  // with displaying the error dialog.
  if (!switch_step_success) {
    return;
  }

  std::move(on_error_callback).Run(error);
}

#endif

base::FilePath ProfilePickerFlowController::GetSwitchProfilePathOrEmpty()
    const {
  if (weak_signed_in_flow_controller_) {
    return weak_signed_in_flow_controller_->switch_profile_path();
  }
  return base::FilePath();
}

void ProfilePickerFlowController::CancelPostSignInFlow() {
  // Triggered from either entreprise welcome or profile switch screens.
  DCHECK_EQ(Step::kPostSignInFlow, current_step());

  switch (entry_point_) {
    case ProfilePicker::EntryPoint::kAppMenuProfileSubMenuManageProfiles:
    case ProfilePicker::EntryPoint::kOnStartup:
    case ProfilePicker::EntryPoint::kOnStartupNoProfile:
    case ProfilePicker::EntryPoint::kProfileMenuManageProfiles:
    case ProfilePicker::EntryPoint::kOpenNewWindowAfterProfileDeletion:
    case ProfilePicker::EntryPoint::kNewSessionOnExistingProcess:
    case ProfilePicker::EntryPoint::kNewSessionOnExistingProcessNoProfile:
    case ProfilePicker::EntryPoint::kProfileLocked:
    case ProfilePicker::EntryPoint::kUnableToCreateBrowser:
    case ProfilePicker::EntryPoint::kBackgroundModeManager:
    case ProfilePicker::EntryPoint::kProfileIdle: {
      SwitchToStep(Step::kProfilePicker, /*reset_state=*/true);
      UnregisterStep(Step::kPostSignInFlow);
#if BUILDFLAG(ENABLE_DICE_SUPPORT)
      UnregisterStep(Step::kAccountSelection);
#endif
      return;
    }
    case ProfilePicker::EntryPoint::kAppMenuProfileSubMenuAddNewProfile:
    case ProfilePicker::EntryPoint::kProfileMenuAddNewProfile: {
      // This results in destroying `this`.
      ExitFlow();
      return;
    }
    case ProfilePicker::EntryPoint::kFirstRun:
    case ProfilePicker::EntryPoint::kGlicManager:
      NOTREACHED()
          << "CancelPostSignInFlow() is not reachable from this entry point";
  }
}

std::u16string ProfilePickerFlowController::GetFallbackAccessibleWindowTitle()
    const {
  return l10n_util::GetStringUTF16(IDS_PROFILE_PICKER_MAIN_VIEW_TITLE);
}

std::unique_ptr<ProfilePickerSignedInFlowController>
ProfilePickerFlowController::CreateSignedInFlowController(
    Profile* signed_in_profile,
    const CoreAccountInfo& account_info,
    std::unique_ptr<content::WebContents> contents) {
  DCHECK(!weak_signed_in_flow_controller_);

  created_profile_ = signed_in_profile->GetWeakPtr();
  auto step_completed_callback =
      base::BindOnce(&ProfilePickerFlowController::HandleIdentityStepsCompleted,
                     // Unretained ok: the callback is passed to a step that
                     // the `this` will own and outlive.
                     base::Unretained(this),
                     // Unretained ok: the steps register a profile keep-alive
                     // and will be alive until this callback runs.
                     base::Unretained(created_profile_.get()));

  auto signed_in_flow = std::make_unique<ProfileCreationSignedInFlowController>(
      host(), signed_in_profile, account_info, std::move(contents),
      suggested_profile_color_, std::move(step_completed_callback));
  weak_signed_in_flow_controller_ = signed_in_flow->GetWeakPtr();
  return signed_in_flow;
}

void ProfilePickerFlowController::SwitchToSignedOutPostIdentityFlow(
    Profile* profile) {
  CHECK(profile);
  created_profile_ = profile->GetWeakPtr();
  CreateSignedOutFlowWebContents(created_profile_.get());

  HandleIdentityStepsCompleted(
      created_profile_.get(),
      PostHostClearedCallback(base::BindOnce(&ShowLocalProfileCustomization,
                                             profile_picked_time_on_startup_)),
      /*is_continue_callback=*/false);
}

void ProfilePickerFlowController::PickProfile(
    const base::FilePath& profile_path,
    ProfilePicker::ProfilePickingArgs args,
    base::OnceCallback<void(bool)> pick_profile_complete_callback) {
  if (args.should_record_startup_metrics &&
      // Avoid overriding the picked time if already recorded. This can happen
      // for example if multiple profiles are picked: https://crbug.com/1277466.
      profile_picked_time_on_startup_.is_null()) {
    profile_picked_time_on_startup_ = base::TimeTicks::Now();
  }

  profiles::SwitchToProfile(
      profile_path, /*always_create=*/false,
      base::BindOnce(&ProfilePickerFlowController::OnSwitchToProfileComplete,
                     weak_ptr_factory_.GetWeakPtr(), args.open_settings,
                     std::move(pick_profile_complete_callback)));
}

void ProfilePickerFlowController::OnSwitchToProfileComplete(
    bool open_settings,
    base::OnceCallback<void(bool)> pick_profile_complete_callback,
    Browser* browser) {
  if (!browser || browser->is_delete_scheduled()) {
    // The browser is destroyed or about to be destroyed.
    if (pick_profile_complete_callback) {
      std::move(pick_profile_complete_callback).Run(false);
    }
    return;
  }

  DCHECK(browser->window());
  if (pick_profile_complete_callback) {
    std::move(pick_profile_complete_callback).Run(true);
  }
  Profile* profile = browser->profile();
  TRACE_EVENT1("browser",
               "ProfilePickerFlowController::OnSwitchToProfileComplete",
               "profile_path", profile->GetPath().AsUTF8Unsafe());

  // Measure startup time to display first web contents if the profile picker
  // was displayed on startup and if the initiating action is instrumented. For
  // example we don't record pick time for profile creations.
  if (!profile_picked_time_on_startup_.is_null()) {
    BeginFirstWebContentsProfiling(browser, profile_picked_time_on_startup_);
  }

  // Only show the profile switch IPH when the user clicked the card, and there
  // are multiple profiles.
  std::vector<ProfileAttributesEntry*> entries =
      g_browser_process->profile_manager()
          ->GetProfileAttributesStorage()
          .GetAllProfilesAttributes();
  int profile_count =
      std::ranges::count(entries, false, &ProfileAttributesEntry::IsOmitted);
  if (profile_count > 1 && !open_settings &&
      selected_profile_target_url_.is_empty()) {
    browser->window()->MaybeShowProfileSwitchIPH();
  }

  if (profile->IsGuestSession()) {
    RecordProfilePickerAction(ProfilePickerAction::kLaunchGuestProfile);
  } else {
    RecordProfilePickerAction(
        open_settings
            ? ProfilePickerAction::kLaunchExistingProfileCustomizeSettings
            : ProfilePickerAction::kLaunchExistingProfile);
  }

  MaybeOpenPageInBrowser(browser, selected_profile_target_url_, open_settings);
  // Closes the Profile Picker.
  //
  // Making sure the flow has not already exited here is needed, because
  // potentially this specific flow can be run twice by the time the host was
  // cleared; e.g. by clicking quickly multiple times when picking a Profile
  // from the Profile Picker view.
  // Depending on the state of the first call, subsequent calls may result in
  // `BeginFirstWebContentsProfiling()` recording multiple metrics.
  // TODO(crbug.com/389887233): Investigate further how often this happens to
  // consider having a better architecture to avoid those issues with multiple
  // flow-exiting calls being executed at the same time.
  if (!HasFlowExited()) {
    ExitFlow();
  }
}

base::queue<ProfileManagementFlowController::Step>
ProfilePickerFlowController::RegisterPostIdentitySteps(
    PostHostClearedCallback post_host_cleared_callback) {
  CHECK(created_profile_);
  base::queue<ProfileManagementFlowController::Step> post_identity_steps;

  content::WebContents* web_contents = nullptr;
  if (weak_signed_in_flow_controller_) {
    // TODO(crbug.com/40942098): Find a way to get the web contents without
    // relying on the weak ptr.
    web_contents = weak_signed_in_flow_controller_->contents();
    CHECK(web_contents);
  } else {
    // TODO(crbug.com/40942098): Find another way to fetch the web contents.
    web_contents = GetSignedOutFlowWebContents();
    CHECK(web_contents);
  }

  auto search_engine_choice_step_completed = base::BindOnce(
      &ProfilePickerFlowController::AdvanceToNextPostIdentityStep,
      base::Unretained(this));
  SearchEngineChoiceDialogService* search_engine_choice_dialog_service =
      SearchEngineChoiceDialogServiceFactory::GetForProfile(
          created_profile_.get());
  RegisterStep(
      Step::kSearchEngineChoice,
      ProfileManagementStepController::CreateForSearchEngineChoice(
          host(), search_engine_choice_dialog_service, web_contents,
          SearchEngineChoiceDialogService::EntryPoint::kProfileCreation,
          std::move(search_engine_choice_step_completed)));
  post_identity_steps.emplace(
      ProfileManagementFlowController::Step::kSearchEngineChoice);

  RegisterStep(
      Step::kFinishFlow,
      ProfileManagementStepController::CreateForFinishFlowAndRunInBrowser(
          host(),
          base::BindOnce(
              &ProfilePickerFlowController::FinishFlowAndRunInBrowser,
              base::Unretained(this), base::Unretained(created_profile_.get()),
              std::move(post_host_cleared_callback))));
  post_identity_steps.emplace(
      ProfileManagementFlowController::Step::kFinishFlow);

  return post_identity_steps;
}