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
|
// Copyright 2023 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/user_education/start_tutorial_in_page.h"
#include "base/memory/weak_ptr.h"
#include "base/threading/thread_checker.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_navigator.h"
#include "chrome/browser/ui/browser_navigator_params.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/user_education/user_education_types.h"
#include "chrome/browser/user_education/user_education_service.h"
#include "chrome/browser/user_education/user_education_service_factory.h"
#include "components/user_education/common/feature_promo/feature_promo_controller.h"
#include "ui/base/page_transition_types.h"
namespace {
class StartTutorialInPageImpl : public StartTutorialInPage {
public:
explicit StartTutorialInPageImpl(Browser* browser, Params params)
: browser_(browser->AsWeakPtr()), callback_(std::move(params.callback)) {
DCHECK(callback_);
DCHECK(browser_);
DCHECK(params.tutorial_id.has_value());
tutorial_id_ = params.tutorial_id.value();
auto* tutorial_service = GetTutorialService();
if (!tutorial_service) {
std::move(callback_).Run(tutorial_service);
return;
}
auto context = GetUiElementContext();
if (!context.has_value()) {
std::move(callback_).Run(tutorial_service);
return;
}
tutorial_service->StartTutorial(
params.tutorial_id.value(), context.value(),
base::BindOnce(&StartTutorialInPageImpl::OnTutorialCompleted,
GetWeakPtr()),
base::BindOnce(&StartTutorialInPageImpl::OnTutorialAborted,
GetWeakPtr()));
std::move(callback_).Run(tutorial_service);
if (params.target_url.has_value()) {
NavigateParams navigate_params(browser, params.target_url.value(),
ui::PAGE_TRANSITION_LINK);
// This does not work
// Try the handle stuff from show_promo_in_page?
navigate_params.disposition = user_education::GetWindowOpenDisposition(
params.page_open_mode);
Navigate(&navigate_params);
}
}
~StartTutorialInPageImpl() override {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
}
base::WeakPtr<StartTutorialInPageImpl> GetWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
private:
std::optional<ui::ElementContext> GetUiElementContext() {
if (browser_) {
return browser_->window()->GetElementContext();
}
return std::nullopt;
}
user_education::TutorialService* GetTutorialService() {
if (browser_) {
UserEducationService* const service =
UserEducationServiceFactory::GetForBrowserContext(
browser_->profile());
if (service) {
return &service->tutorial_service();
}
}
return nullptr;
}
void OnTutorialCompleted() { delete this; }
void OnTutorialAborted() { delete this; }
const base::WeakPtr<Browser> browser_;
user_education::TutorialIdentifier tutorial_id_;
Callback callback_;
THREAD_CHECKER(thread_checker_);
base::WeakPtrFactory<StartTutorialInPageImpl> weak_ptr_factory_{this};
};
} // namespace
StartTutorialInPage::Params::Params() = default;
StartTutorialInPage::Params::Params(Params&& other) noexcept = default;
StartTutorialInPage::Params& StartTutorialInPage::Params::operator=(
Params&& other) noexcept = default;
StartTutorialInPage::Params::~Params() = default;
StartTutorialInPage::StartTutorialInPage() = default;
StartTutorialInPage::~StartTutorialInPage() = default;
base::WeakPtr<StartTutorialInPage> StartTutorialInPage::Start(Browser* browser,
Params params) {
return (new StartTutorialInPageImpl(browser, std::move(params)))
->GetWeakPtr();
}
|