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
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/user_education/common/tutorial/tutorial_service.h"
#include <memory>
#include <vector>
#include "base/auto_reset.h"
#include "base/callback_list.h"
#include "base/functional/bind.h"
#include "base/time/time.h"
#include "components/user_education/common/help_bubble/help_bubble.h"
#include "components/user_education/common/help_bubble/help_bubble_factory_registry.h"
#include "components/user_education/common/tutorial/tutorial.h"
#include "components/user_education/common/tutorial/tutorial_identifier.h"
#include "components/user_education/common/tutorial/tutorial_registry.h"
namespace user_education {
namespace {
// How long a tutorial has to go without a bubble before we assume it's broken
// and abort it.
constexpr base::TimeDelta kBrokenTutorialTimeout = base::Seconds(15);
// How long a tutorial has to go before the first bubble is shown before we
// assume it's been broken or abandoned and abort it. This is longer than the
// above because we want to allow the user time to navigate to the surface that
// triggers the tutorial.
constexpr base::TimeDelta kTutorialNotStartedTimeout = base::Seconds(60);
} // namespace
TutorialService::TutorialCreationParams::TutorialCreationParams(
const TutorialDescription* description,
ui::ElementContext context)
: description_(description), context_(context) {}
TutorialService::TutorialService(
TutorialRegistry* tutorial_registry,
HelpBubbleFactoryRegistry* help_bubble_factory_registry)
: tutorial_registry_(tutorial_registry),
help_bubble_factory_registry_(help_bubble_factory_registry) {}
TutorialService::~TutorialService() = default;
void TutorialService::StartTutorial(TutorialIdentifier id,
ui::ElementContext context,
CompletedCallback completed_callback,
AbortedCallback aborted_callback,
RestartedCallback restarted_callback) {
CancelTutorialIfRunning();
// Get the description from the tutorial registry.
const TutorialDescription* const description =
tutorial_registry_->GetTutorialDescription(id);
CHECK(description);
// Construct the tutorial from the description.
running_tutorial_ =
Tutorial::Builder::BuildFromDescription(*description, this, context);
// Set the external callbacks.
completed_callback_ = std::move(completed_callback);
aborted_callback_ = std::move(aborted_callback);
restarted_callback_ = std::move(restarted_callback);
// Save the params for creating the tutorial to be used when restarting.
running_tutorial_creation_params_ =
std::make_unique<TutorialCreationParams>(description, context);
// Before starting the tutorial, set a timeout just in case the user never
// actually gets to a place where they can launch the first bubble.
broken_tutorial_timer_.Start(
FROM_HERE, kTutorialNotStartedTimeout,
base::BindOnce(&TutorialService::OnBrokenTutorial,
base::Unretained(this)));
// Start the tutorial and mark the params used to created it for restarting.
most_recent_tutorial_id_ = id;
if (description->temporary_state_callback) {
running_tutorial_->SetState(
description->temporary_state_callback.Run(context));
}
running_tutorial_->Start();
}
bool TutorialService::CancelTutorialIfRunning(
std::optional<TutorialIdentifier> id) {
if (!running_tutorial_) {
return false;
}
// If a specific tutorial was requested to be aborted, make sure that's the
// one that is running.
if (id.has_value() && most_recent_tutorial_id_ != id) {
return false;
}
if (is_final_bubble_) {
// The current tutorial is showing the final congratulatory bubble, so it
// is effectively complete.
CompleteTutorial();
is_final_bubble_ = false;
} else {
running_tutorial_->Abort();
}
return true;
}
void TutorialService::LogIPHLinkClicked(TutorialIdentifier id,
bool iph_link_was_clicked) {
const TutorialDescription* const description =
tutorial_registry_->GetTutorialDescription(id);
CHECK(description);
if (description->histograms) {
description->histograms->RecordIphLinkClicked(iph_link_was_clicked);
}
}
void TutorialService::LogStartedFromWhatsNewPage(TutorialIdentifier id,
bool success) {
const TutorialDescription* const description =
tutorial_registry_->GetTutorialDescription(id);
CHECK(description);
if (description->histograms) {
description->histograms->RecordStartedFromWhatsNewPage(success);
}
}
bool TutorialService::RestartTutorial() {
DCHECK(running_tutorial_ && running_tutorial_creation_params_);
base::AutoReset<bool> resetter(&is_restarting_, true);
HideCurrentBubbleIfShowing();
running_tutorial_ = Tutorial::Builder::BuildFromDescription(
*running_tutorial_creation_params_->description_, this,
running_tutorial_creation_params_->context_);
if (!running_tutorial_) {
ResetRunningTutorial();
return false;
}
if (running_tutorial_creation_params_->description_
->temporary_state_callback) {
running_tutorial_->SetState(
running_tutorial_creation_params_->description_
->temporary_state_callback.Run(
running_tutorial_creation_params_->context_));
}
// Note: if we restart the tutorial, we won't record whether the user pressed
// the pane focus key to focus the help bubble until the user actually decides
// they're finished, but we also won't reset the count, so at the end we can
// record the total.
// TODO(dfried): decide if this is actually the correct behavior.
running_tutorial_was_restarted_ = true;
running_tutorial_->Start();
restarted_callback_.Run();
return true;
}
void TutorialService::AbortTutorial(std::optional<int> abort_step) {
// For various reasons, we could get called here while e.g. tearing down the
// interaction sequence. We only want to actually run AbortTutorial() or
// CompleteTutorial() exactly once, so we won't continue if the tutorial has
// already been disposed. We also only want to abort the tutorial if we are
// not in the process of restarting. When calling reset on the help bubble,
// or when resetting the tutorial, the interaction sequence or callbacks could
// call the abort.
if (!running_tutorial_ || is_restarting_) {
return;
}
// If the tutorial had been restarted and then aborted, The tutorial should be
// considered completed.
if (running_tutorial_was_restarted_) {
CompleteTutorial();
return;
}
if (running_tutorial_creation_params_->description_->histograms) {
if (abort_step.has_value()) {
running_tutorial_creation_params_->description_->histograms
->RecordAbortStep(abort_step.value());
}
running_tutorial_creation_params_->description_->histograms->RecordComplete(
false);
}
UMA_HISTOGRAM_BOOLEAN("Tutorial.Completion", false);
// Reset the tutorial and call the external abort callback.
ResetRunningTutorial();
if (aborted_callback_) {
std::move(aborted_callback_).Run();
}
}
void TutorialService::OnNonFinalBubbleClosed(HelpBubble* bubble,
HelpBubble::CloseReason) {
if (bubble != currently_displayed_bubble_.get()) {
return;
}
bubble_closed_subscription_ = base::CallbackListSubscription();
currently_displayed_bubble_.reset();
broken_tutorial_timer_.Start(
FROM_HERE, kBrokenTutorialTimeout,
base::BindOnce(&TutorialService::OnBrokenTutorial,
base::Unretained(this)));
}
void TutorialService::CompleteTutorial() {
DCHECK(running_tutorial_);
// Log the completion metric based on if the tutorial was restarted or not.
if (running_tutorial_creation_params_->description_->histograms) {
running_tutorial_creation_params_->description_->histograms->RecordComplete(
true);
}
UMA_HISTOGRAM_BOOLEAN("Tutorial.Completion", true);
ResetRunningTutorial();
std::move(completed_callback_).Run();
}
void TutorialService::SetCurrentBubble(std::unique_ptr<HelpBubble> bubble,
bool is_last_step) {
DCHECK(running_tutorial_);
currently_displayed_bubble_ = std::move(bubble);
broken_tutorial_timer_.Stop();
if (is_last_step) {
is_final_bubble_ = true;
bubble_closed_subscription_ =
currently_displayed_bubble_->AddOnCloseCallback(base::BindOnce(
[](TutorialService* service, HelpBubble*, HelpBubble::CloseReason) {
service->CompleteTutorial();
},
base::Unretained(this)));
} else {
is_final_bubble_ = false;
bubble_closed_subscription_ =
currently_displayed_bubble_->AddOnCloseCallback(base::BindOnce(
&TutorialService::OnNonFinalBubbleClosed, base::Unretained(this)));
}
}
void TutorialService::HideCurrentBubbleIfShowing() {
if (!currently_displayed_bubble_) {
return;
}
bubble_closed_subscription_ = base::CallbackListSubscription();
currently_displayed_bubble_.reset();
}
bool TutorialService::IsRunningTutorial(
std::optional<TutorialIdentifier> id) const {
if (!running_tutorial_) {
return false;
}
return !id.has_value() || id.value() == most_recent_tutorial_id_;
}
void TutorialService::ResetRunningTutorial() {
DCHECK(running_tutorial_);
broken_tutorial_timer_.Stop();
running_tutorial_.reset();
running_tutorial_creation_params_.reset();
running_tutorial_was_restarted_ = false;
HideCurrentBubbleIfShowing();
}
void TutorialService::OnBrokenTutorial() {
if (running_tutorial_ && !currently_displayed_bubble_) {
running_tutorial_->Abort();
}
}
} // namespace user_education
|