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
|
// 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_flow_controller.h"
#include "base/check_is_test.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_forward.h"
#include "base/functional/callback_helpers.h"
#include "base/metrics/histogram_functions.h"
#include "base/not_fatal_until.h"
#include "base/notreached.h"
#include "base/strings/strcat.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_window.h"
#include "chrome/browser/ui/browser_finder.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_web_contents_host.h"
namespace {
// LINT.IfChange(GetStepHistogramSuffix)
std::string_view GetStepHistogramSuffix(
ProfileManagementFlowController::Step step) {
switch (step) {
case ProfileManagementFlowController::Step::kUnknown:
NOTREACHED();
case ProfileManagementFlowController::Step::kProfilePicker:
return ".PickerMainApp";
case ProfileManagementFlowController::Step::kAccountSelection:
return ".SigninFlow";
case ProfileManagementFlowController::Step::kFinishSamlSignin:
return ".FinishSamlFlow";
case ProfileManagementFlowController::Step::kReauth:
return ".Reauth";
case ProfileManagementFlowController::Step::kPostSignInFlow:
return ".PostSignInSteps";
case ProfileManagementFlowController::Step::kIntro:
return ".FREIntro";
case ProfileManagementFlowController::Step::kDefaultBrowser:
return ".DefaultBrowser";
case ProfileManagementFlowController::Step::kSearchEngineChoice:
return ".SearchEngineChoice";
case ProfileManagementFlowController::Step::kFinishFlow:
return ".FinishFlow";
}
}
// LINT.ThenChange(//tools/metrics/histograms/metadata/profile/histograms.xml:StepName)
} // namespace
ProfileManagementFlowController::ProfileManagementFlowController(
ProfilePickerWebContentsHost* host,
ClearHostClosure clear_host_callback,
std::string_view flow_type_string)
: host_(host),
clear_host_callback_(std::move(clear_host_callback)),
flow_tracker_(flow_type_string) {
DCHECK(clear_host_callback_.value());
}
ProfileManagementFlowController::~ProfileManagementFlowController() {
flow_tracker_.ExitedFlow();
}
void ProfileManagementFlowController::SwitchToStep(
Step step,
bool reset_state,
StepSwitchFinishedCallback step_switch_finished_callback,
base::OnceClosure pop_step_callback) {
Step previous_step = flow_tracker_.tracked_step();
if (previous_step != Step::kUnknown) {
flow_tracker_.ExitedCurrentStep();
}
flow_tracker_.EnteredNewStep(step);
StepSwitchFinishedCallback internal_step_switch_finished_callback =
StepSwitchFinishedCallback(
base::BindOnce(&FlowTracker::FinishedStepSwitch,
base::Unretained(&flow_tracker_), step));
StepSwitchFinishedCallback combined_step_switch_callbacks =
CombineCallbacks<StepSwitchFinishedCallback, bool>(
std::move(internal_step_switch_finished_callback),
std::move(step_switch_finished_callback));
auto* new_step_controller = initialized_steps_.at(step).get();
DCHECK(new_step_controller);
new_step_controller->set_pop_step_callback(std::move(pop_step_callback));
new_step_controller->Show(std::move(combined_step_switch_callbacks),
reset_state);
if (initialized_steps_.contains(previous_step)) {
initialized_steps_.at(previous_step)->OnHidden();
}
}
void ProfileManagementFlowController::OnNavigateBackRequested() {
DCHECK(initialized_steps_.contains(flow_tracker_.tracked_step()));
initialized_steps_.at(flow_tracker_.tracked_step())
->OnNavigateBackRequested();
}
#if BUILDFLAG(ENABLE_DICE_SUPPORT)
void ProfileManagementFlowController::OnReloadRequested() {
DCHECK(initialized_steps_.contains(flow_tracker_.tracked_step()));
initialized_steps_.at(flow_tracker_.tracked_step())->OnReloadRequested();
}
#endif
std::u16string
ProfileManagementFlowController::GetFallbackAccessibleWindowTitle() const {
return std::u16string();
}
void ProfileManagementFlowController::RegisterStep(
Step step,
std::unique_ptr<ProfileManagementStepController> step_controller) {
initialized_steps_[step] = std::move(step_controller);
}
void ProfileManagementFlowController::UnregisterStep(Step step) {
CHECK_NE(step, flow_tracker_.tracked_step());
initialized_steps_.erase(step);
}
bool ProfileManagementFlowController::IsStepInitialized(Step step) const {
return initialized_steps_.contains(step) && initialized_steps_.at(step);
}
bool ProfileManagementFlowController::HasFlowExited() const {
return clear_host_callback_.value().is_null();
}
void ProfileManagementFlowController::ExitFlow() {
CHECK(!HasFlowExited());
std::move(clear_host_callback_.value()).Run();
}
bool ProfileManagementFlowController::PreFinishWithBrowser() {
return false;
}
ProfileManagementFlowController::Step
ProfileManagementFlowController::current_step() const {
return flow_tracker_.tracked_step();
}
void ProfileManagementFlowController::FinishFlowAndRunInBrowser(
Profile* profile,
PostHostClearedCallback post_host_cleared_callback) {
DCHECK(clear_host_callback_.value()); // The host shouldn't be cleared yet.
// TODO(crbug.com/40246333): Handle the return value and don't open a browser
// if it is already going to be opened.
PreFinishWithBrowser();
base::OnceCallback<void(Browser*)> post_browser_open_callback;
// `clear_host_callback_` and `post_host_cleared_callback` may be run after
// the `ProfileManagementFlowController` is deleted.
if (post_host_cleared_callback->is_null()) {
post_browser_open_callback =
base::IgnoreArgs<Browser*>(std::move(clear_host_callback_.value()));
} else {
post_browser_open_callback =
base::BindOnce(
[](base::OnceClosure clear_host_closure, Browser* browser) {
std::move(clear_host_closure).Run();
return browser;
},
std::move(clear_host_callback_.value()))
.Then(std::move(post_host_cleared_callback.value()));
}
// Start by opening the browser window, to ensure that we have another
// KeepAlive for `profile` by the time we clear the flow and its host.
// TODO(crbug.com/40242414): Make sure we do something or log an error if
// opening a browser window was not possible.
profiles::OpenBrowserWindowForProfile(
std::move(post_browser_open_callback),
/*always_create=*/false, // Don't create a window if one already exists.
/*is_new_profile=*/false, // Don't create a first run window.
/*unblock_extensions=*/false, // There is no need to unblock all
// extensions because we only open browser
// window if the Profile is not locked.
// Hence there is no extension blocked.
profile);
}
base::OnceClosure
ProfileManagementFlowController::CreateSwitchToStepPopCallback(Step step) {
return base::BindOnce(
&ProfileManagementFlowController::SwitchToStep,
// Binding as Unretained as `this` outlives the step
// controllers.
base::Unretained(this), step,
/*reset_state=*/false,
/*step_switch_finished_callback=*/StepSwitchFinishedCallback(),
/*pop_step_callback=*/base::OnceClosure());
}
void ProfileManagementFlowController::CreateSignedOutFlowWebContents(
Profile* profile) {
signed_out_flow_web_contents_ =
content::WebContents::Create(content::WebContents::CreateParams(profile));
}
content::WebContents*
ProfileManagementFlowController::GetSignedOutFlowWebContents() const {
return signed_out_flow_web_contents_.get();
}
void ProfileManagementFlowController::Reset(
StepSwitchFinishedCallback callback) {
Step previous_step = flow_tracker_.tracked_step();
// Activate the initial step.
SwitchToStep(Step::kProfilePicker, /*reset_state=*/true,
/*step_switch_finished_callback=*/std::move(callback));
// Unregister the previous active step.
UnregisterStep(previous_step);
}
ProfileManagementFlowController::FlowTracker::FlowTracker(
std::string_view flow_type_string)
: flow_type_string_(flow_type_string) {}
void ProfileManagementFlowController::FlowTracker::EnteredNewStep(Step step) {
CHECK_NE(Step::kUnknown, step);
CHECK_NE(tracked_step_, step);
tracked_step_ = step;
base::UmaHistogramEnumeration(
base::StrCat({"ProfilePicker.", flow_type_string_, ".StepStart"}), step);
step_start_elapsed_timer_.emplace();
}
void ProfileManagementFlowController::FlowTracker::FinishedStepSwitch(
Step step,
bool success) {
if (tracked_step_ != step) {
NOTREACHED(base::NotFatalUntil::M143)
<< "Step switch callback should run while the step is still the "
"current step being tracked.";
return;
}
if (!success) {
base::UmaHistogramEnumeration(
base::StrCat({"ProfilePicker.", flow_type_string_, ".StepSkipped"}),
step);
return;
}
step_shown_elapsed_timer_.emplace();
base::UmaHistogramEnumeration(
base::StrCat({"ProfilePicker.", flow_type_string_, ".StepShown"}), step);
}
void ProfileManagementFlowController::FlowTracker::ExitedCurrentStep() {
CHECK(step_start_elapsed_timer_.has_value());
base::UmaHistogramEnumeration(
base::StrCat({"ProfilePicker.", flow_type_string_, ".StepEnd"}),
tracked_step_);
const std::string step_total_duration_base_histogram =
base::StrCat({"ProfilePicker.", flow_type_string_, ".StepTotalDuration"});
base::TimeDelta step_start_exit_elapsed_time =
step_start_elapsed_timer_->Elapsed();
base::UmaHistogramMediumTimes(step_total_duration_base_histogram,
step_start_exit_elapsed_time);
base::UmaHistogramMediumTimes(
base::StrCat({step_total_duration_base_histogram,
GetStepHistogramSuffix(tracked_step_)}),
step_start_exit_elapsed_time);
step_start_elapsed_timer_.reset();
if (step_shown_elapsed_timer_) {
const std::string step_shown_duration_base_histogram = base::StrCat(
{"ProfilePicker.", flow_type_string_, ".StepShownDuration"});
base::TimeDelta step_shown_exit_elapsed_time =
step_shown_elapsed_timer_->Elapsed();
base::UmaHistogramMediumTimes(step_shown_duration_base_histogram,
step_shown_exit_elapsed_time);
base::UmaHistogramMediumTimes(
base::StrCat({step_shown_duration_base_histogram,
GetStepHistogramSuffix(tracked_step_)}),
step_shown_exit_elapsed_time);
step_shown_elapsed_timer_.reset();
}
}
void ProfileManagementFlowController::FlowTracker::ExitedFlow() {
// Records the last active step metrics if not already done.
if (step_start_elapsed_timer_.has_value()) {
ExitedCurrentStep();
}
base::UmaHistogramMediumTimes(
base::StrCat(
{"ProfilePicker." + flow_type_string_ + ".FlowTotalDuration"}),
flow_elapsed_timer_.Elapsed());
base::UmaHistogramEnumeration(
base::StrCat({"ProfilePicker." + flow_type_string_ + ".FlowEndedAtStep"}),
tracked_step_);
}
std::string_view GetStepHistogramSuffixForTesting(
ProfileManagementFlowController::Step step) {
CHECK_IS_TEST();
return GetStepHistogramSuffix(step);
}
|