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
|
// 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_management_step_controller.h"
#include "base/functional/bind.h"
#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/delete_profile_helper.h"
#include "chrome/browser/profiles/keep_alive/profile_keep_alive_types.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/profiles/profiles_state.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/ui/browser.h"
#include "chrome/browser/ui/profiles/profile_customization_util.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/views/profiles/profile_management_types.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/search_engine_choice/search_engine_choice_ui.h"
#include "components/search_engines/search_engine_choice/search_engine_choice_utils.h"
#include "google_apis/gaia/core_account_id.h"
#if BUILDFLAG(ENABLE_DICE_SUPPORT)
#include "chrome/browser/ui/views/profiles/profile_picker_dice_sign_in_provider.h"
#endif
namespace {
class ProfilePickerAppStepController : public ProfileManagementStepController {
public:
explicit ProfilePickerAppStepController(ProfilePickerWebContentsHost* host,
const GURL& initial_url)
: ProfileManagementStepController(host), initial_url_(initial_url) {}
~ProfilePickerAppStepController() override = default;
void Show(StepSwitchFinishedCallback step_shown_callback,
bool reset_state) override {
base::OnceClosure step_shown_success =
base::BindOnce(std::move(step_shown_callback.value()), true);
if (!loaded_ui_in_picker_contents_) {
loaded_ui_in_picker_contents_ = true;
host()->ShowScreenInPickerContents(initial_url_,
std::move(step_shown_success));
return;
}
if (reset_state) {
// Don't do a full reset, just go back to the beginning of the history:
host()->GetPickerContents()->GetController().GoToIndex(0);
}
host()->ShowScreenInPickerContents(GURL(), std::move(step_shown_success));
}
void OnNavigateBackRequested() override {
NavigateBackInternal(host()->GetPickerContents());
}
private:
// We want to load the WebUI page exactly once, and do more lightweight
// transitions if we need to switch back to this step later. So we track
// whether the UI has been loaded or not.
// Note that this relies on other steps not clearing the picker contents and
// using their own instead.
bool loaded_ui_in_picker_contents_ = false;
const GURL initial_url_;
};
#if BUILDFLAG(ENABLE_DICE_SUPPORT)
class DiceSignInStepController : public ProfileManagementStepController {
public:
explicit DiceSignInStepController(
ProfilePickerWebContentsHost* host,
std::unique_ptr<ProfilePickerDiceSignInProvider> dice_sign_in_provider,
DiceSignInStepFinishedCallback signed_in_callback)
: ProfileManagementStepController(host),
signed_in_callback_(std::move(signed_in_callback)),
dice_sign_in_provider_(std::move(dice_sign_in_provider)) {
DCHECK(signed_in_callback_);
}
~DiceSignInStepController() override = default;
void Show(StepSwitchFinishedCallback step_shown_callback,
bool reset_state) override {
CHECK(!step_shown_callback->is_null());
DCHECK(signed_in_callback_) << "Attempting to show Dice step again while "
"it was previously completed";
// Unretained ok because the provider is owned by `this`.
dice_sign_in_provider_->SwitchToSignIn(
std::move(step_shown_callback),
base::BindOnce(&DiceSignInStepController::OnStepFinished,
base::Unretained(this)));
}
void OnHidden() override {
host()->SetNativeToolbarVisible(false);
// We don't reset the provider when we navigate back as we want to keep this
// page and the ephemeral profile around for performance reasons.
// The caller should delete the step if clearing the provider is needed.
}
bool CanPopStep() const override {
return ProfileManagementStepController::CanPopStep() &&
dice_sign_in_provider_ && dice_sign_in_provider_->IsInitialized();
}
void OnReloadRequested() override {
// Sign-in may fail due to connectivity issues, allow reloading.
if (dice_sign_in_provider_) {
dice_sign_in_provider_->ReloadSignInPage();
}
}
void OnNavigateBackRequested() override {
if (dice_sign_in_provider_) {
NavigateBackInternal(dice_sign_in_provider_->contents());
}
}
private:
void OnStepFinished(Profile* profile,
const CoreAccountInfo& account_info,
std::unique_ptr<content::WebContents> contents) {
std::move(signed_in_callback_)
.Run(profile, account_info, std::move(contents),
StepSwitchFinishedCallback());
// The step controller can be destroyed when `signed_in_callback_` runs.
// Don't interact with members below.
}
DiceSignInStepFinishedCallback signed_in_callback_;
std::unique_ptr<ProfilePickerDiceSignInProvider> dice_sign_in_provider_;
};
class FinishSamlSignInStepController : public ProfileManagementStepController {
public:
explicit FinishSamlSignInStepController(
ProfilePickerWebContentsHost* host,
Profile* profile,
std::unique_ptr<content::WebContents> contents,
base::OnceCallback<void(PostHostClearedCallback)>
finish_picker_section_callback)
: ProfileManagementStepController(host),
profile_(profile),
contents_(std::move(contents)),
finish_picker_section_callback_(
std::move(finish_picker_section_callback)) {
DCHECK(finish_picker_section_callback_);
profile_keep_alive_ = std::make_unique<ScopedProfileKeepAlive>(
profile_, ProfileKeepAliveOrigin::kProfileCreationSamlFlow);
}
~FinishSamlSignInStepController() override {
if (finish_picker_section_callback_) {
finish_picker_section_callback_.Reset();
}
}
void Show(StepSwitchFinishedCallback step_shown_callback,
bool reset_state) override {
// First, stop showing `contents_` to free it up so it can be moved to a new
// browser window.
host()->ShowScreenInPickerContents(
GURL(url::kAboutBlankURL),
/*navigation_finished_closure=*/
base::BindOnce(&FinishSamlSignInStepController::OnSignInContentsFreedUp,
weak_ptr_factory_.GetWeakPtr(),
std::move(step_shown_callback)));
}
void OnNavigateBackRequested() override {
// Not supported here
}
private:
// Note: This will be executed after the profile management view closes, so
// the step instance will already be deleted.
static void ContinueSAMLSignin(std::unique_ptr<content::WebContents> contents,
Browser* browser) {
DCHECK(browser);
// Make a new tab with the desired contents and close the old tab.
browser->tab_strip_model()->AppendWebContents(std::move(contents),
/*foreground=*/true);
browser->tab_strip_model()->DetachAndDeleteWebContentsAt(/*index=*/0);
ProfileMetrics::LogProfileAddSignInFlowOutcome(
ProfileMetrics::ProfileSignedInFlowOutcome::kSAML);
}
void OnSignInContentsFreedUp(StepSwitchFinishedCallback step_shown_callback) {
DCHECK(finish_picker_section_callback_);
CHECK(!step_shown_callback->is_null());
std::move(step_shown_callback.value()).Run(true);
ProfileMetrics::LogProfileAddNewUser(
ProfileMetrics::ADD_NEW_PROFILE_PICKER_SIGNED_IN);
// Second, ensure the profile creation and set up is complete.
FinalizeNewProfileSetup(profile_,
profiles::GetDefaultNameForNewEnterpriseProfile(),
/*is_default_name=*/false);
// Finally, instruct the flow terminate in the picker and continue in a full
// browser window.
auto continue_callback = PostHostClearedCallback(
base::BindOnce(&FinishSamlSignInStepController::ContinueSAMLSignin,
std::move(contents_)));
std::move(finish_picker_section_callback_)
.Run(std::move(continue_callback));
}
std::unique_ptr<ScopedProfileKeepAlive> profile_keep_alive_;
raw_ptr<Profile> profile_;
std::unique_ptr<content::WebContents> contents_;
// Callback to be executed to close the flow host, when it is ready to
// continue the SAML sign-in in the full browser.
base::OnceCallback<void(PostHostClearedCallback)>
finish_picker_section_callback_;
base::WeakPtrFactory<FinishSamlSignInStepController> weak_ptr_factory_{this};
};
#endif
class PostSignInStepController : public ProfileManagementStepController {
public:
explicit PostSignInStepController(
ProfilePickerWebContentsHost* host,
std::unique_ptr<ProfilePickerSignedInFlowController> signed_in_flow)
: ProfileManagementStepController(host),
signed_in_flow_(std::move(signed_in_flow)) {}
~PostSignInStepController() override = default;
void Show(StepSwitchFinishedCallback step_shown_callback,
bool reset_state) override {
signed_in_flow_->Init(std::move(step_shown_callback));
}
void OnHidden() override { signed_in_flow_->Cancel(); }
void OnNavigateBackRequested() override {
// Do nothing, navigating back is not allowed.
}
private:
std::unique_ptr<ProfilePickerSignedInFlowController> signed_in_flow_;
base::WeakPtrFactory<PostSignInStepController> weak_ptr_factory_{this};
};
class FinishFlowAndRunInBrowserStepController
: public ProfileManagementStepController {
public:
FinishFlowAndRunInBrowserStepController(
ProfilePickerWebContentsHost* host,
base::OnceClosure finish_flow_and_run_in_browser_callback)
: ProfileManagementStepController(host),
finish_flow_and_run_in_browser_callback_(
std::move(finish_flow_and_run_in_browser_callback)) {
CHECK(finish_flow_and_run_in_browser_callback_);
}
void Show(StepSwitchFinishedCallback step_shown_callback,
bool reset_state) override {
CHECK(reset_state);
CHECK(!step_shown_callback->is_null());
std::move(step_shown_callback.value()).Run(true);
std::move(finish_flow_and_run_in_browser_callback_).Run();
}
void OnNavigateBackRequested() override {
// Do nothing, navigating back is not allowed.
NOTREACHED();
}
private:
base::OnceClosure finish_flow_and_run_in_browser_callback_;
};
class SearchEngineChoiceStepController
: public ProfileManagementStepController {
public:
SearchEngineChoiceStepController(
ProfilePickerWebContentsHost* host,
SearchEngineChoiceDialogService* search_engine_choice_dialog_service,
content::WebContents* web_contents,
SearchEngineChoiceDialogService::EntryPoint entry_point,
base::OnceClosure step_completed_callback)
: ProfileManagementStepController(host),
entry_point_(entry_point),
search_engine_choice_dialog_service_(
search_engine_choice_dialog_service),
step_completed_callback_(std::move(step_completed_callback)),
web_contents_(web_contents) {
CHECK(web_contents_);
}
void Show(StepSwitchFinishedCallback step_shown_callback,
bool reset_state) override {
CHECK(reset_state);
CHECK(!step_shown_callback->is_null());
if (!search_engine_choice_dialog_service_) {
// Mark that this step was skipped and proceed with the next one.
std::move(step_shown_callback.value()).Run(false);
std::move(step_completed_callback_).Run();
return;
}
base::OnceClosure navigation_finished_closure =
base::BindOnce(&SearchEngineChoiceStepController::OnLoadFinished,
base::Unretained(this));
// Notify the caller first.
navigation_finished_closure =
base::BindOnce(std::move(step_shown_callback.value()), true)
.Then(std::move(navigation_finished_closure));
search_engines::SearchEngineChoiceScreenEvents choice_screen_event =
search_engines::SearchEngineChoiceScreenEvents::
kProfileCreationChoiceScreenWasDisplayed;
if (entry_point_ ==
SearchEngineChoiceDialogService::EntryPoint::kFirstRunExperience) {
choice_screen_event = search_engines::SearchEngineChoiceScreenEvents::
kFreChoiceScreenWasDisplayed;
}
search_engines::RecordChoiceScreenEvent(choice_screen_event);
host()->ShowScreen(web_contents_,
GURL(chrome::kChromeUISearchEngineChoiceURL),
std::move(navigation_finished_closure));
}
void OnNavigateBackRequested() override {
// Do nothing, navigating back is not allowed.
NOTREACHED();
}
private:
void OnLoadFinished() {
auto* search_engine_choice_ui = web_contents_->GetWebUI()
->GetController()
->GetAs<SearchEngineChoiceUI>();
CHECK(search_engine_choice_ui);
CHECK(step_completed_callback_);
search_engine_choice_ui->Initialize(
/*display_dialog_callback=*/base::OnceClosure(),
/*on_choice_made_callback=*/
std::move(step_completed_callback_), entry_point_);
}
// The entry point from which the search engine choice screen is displayed.
// This could be either the FRE or Profile Creation in this case.
SearchEngineChoiceDialogService::EntryPoint entry_point_;
// Can be nullptr.
raw_ptr<SearchEngineChoiceDialogService> search_engine_choice_dialog_service_;
// Callback to be executed when the step is completed.
base::OnceClosure step_completed_callback_;
// The web contents in which we want to display the screen.
raw_ptr<content::WebContents> web_contents_;
};
} // namespace
// static
std::unique_ptr<ProfileManagementStepController>
ProfileManagementStepController::CreateForProfilePickerApp(
ProfilePickerWebContentsHost* host,
const GURL& initial_url) {
return std::make_unique<ProfilePickerAppStepController>(host, initial_url);
}
#if BUILDFLAG(ENABLE_DICE_SUPPORT)
// static
std::unique_ptr<ProfileManagementStepController>
ProfileManagementStepController::CreateForDiceSignIn(
ProfilePickerWebContentsHost* host,
std::unique_ptr<ProfilePickerDiceSignInProvider> dice_sign_in_provider,
DiceSignInStepFinishedCallback signed_in_callback) {
return std::make_unique<DiceSignInStepController>(
host, std::move(dice_sign_in_provider), std::move(signed_in_callback));
}
// static
std::unique_ptr<ProfileManagementStepController>
ProfileManagementStepController::CreateForFinishSamlSignIn(
ProfilePickerWebContentsHost* host,
Profile* profile,
std::unique_ptr<content::WebContents> contents,
base::OnceCallback<void(PostHostClearedCallback)>
finish_picker_section_callback) {
return std::make_unique<FinishSamlSignInStepController>(
host, profile, std::move(contents),
std::move(finish_picker_section_callback));
}
#endif
// static
std::unique_ptr<ProfileManagementStepController>
ProfileManagementStepController::CreateForPostSignInFlow(
ProfilePickerWebContentsHost* host,
std::unique_ptr<ProfilePickerSignedInFlowController> signed_in_flow) {
return std::make_unique<PostSignInStepController>(host,
std::move(signed_in_flow));
}
// static
std::unique_ptr<ProfileManagementStepController>
ProfileManagementStepController::CreateForSearchEngineChoice(
ProfilePickerWebContentsHost* host,
SearchEngineChoiceDialogService* search_engine_choice_dialog_service,
content::WebContents* web_contents,
SearchEngineChoiceDialogService::EntryPoint entry_point,
base::OnceClosure callback) {
return std::make_unique<SearchEngineChoiceStepController>(
host, search_engine_choice_dialog_service, web_contents, entry_point,
std::move(callback));
}
// static
std::unique_ptr<ProfileManagementStepController>
ProfileManagementStepController::CreateForFinishFlowAndRunInBrowser(
ProfilePickerWebContentsHost* host,
base::OnceClosure finish_flow_and_run_in_browser_callback) {
return std::make_unique<FinishFlowAndRunInBrowserStepController>(
host, std::move(finish_flow_and_run_in_browser_callback));
}
ProfileManagementStepController::ProfileManagementStepController(
ProfilePickerWebContentsHost* host)
: host_(host) {}
ProfileManagementStepController::~ProfileManagementStepController() = default;
#if BUILDFLAG(ENABLE_DICE_SUPPORT)
void ProfileManagementStepController::OnReloadRequested() {}
#endif
void ProfileManagementStepController::NavigateBackInternal(
content::WebContents* contents) {
if (contents && contents->GetController().CanGoBack()) {
contents->GetController().GoBack();
return;
}
if (CanPopStep()) {
DCHECK(pop_step_callback_);
std::move(pop_step_callback_).Run();
}
}
bool ProfileManagementStepController::CanPopStep() const {
return !pop_step_callback_.is_null();
}
|