tutorial_service.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_USER_EDUCATION_COMMON_TUTORIAL_SERVICE_H_
  5. #define COMPONENTS_USER_EDUCATION_COMMON_TUTORIAL_SERVICE_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/callback_forward.h"
  9. #include "base/callback_helpers.h"
  10. #include "base/callback_list.h"
  11. #include "base/memory/raw_ptr.h"
  12. #include "components/user_education/common/tutorial.h"
  13. #include "components/user_education/common/tutorial_identifier.h"
  14. #include "third_party/abseil-cpp/absl/types/optional.h"
  15. #include "ui/base/interaction/element_tracker.h"
  16. // Declare in the global scope for testing purposes.
  17. class TutorialInteractiveUitest;
  18. namespace user_education {
  19. class HelpBubble;
  20. class HelpBubbleFactoryRegistry;
  21. class TutorialRegistry;
  22. // A profile based service which provides the current running tutorial. A
  23. // TutorialService should be constructed by a factory which fills in the correct
  24. // tutorials based on the platform the tutorial targets.
  25. class TutorialService {
  26. public:
  27. TutorialService(TutorialRegistry* tutorial_registry,
  28. HelpBubbleFactoryRegistry* help_bubble_factory_registry);
  29. virtual ~TutorialService();
  30. using CompletedCallback = base::OnceClosure;
  31. using AbortedCallback = base::OnceClosure;
  32. // Returns true if there is a currently running tutorial.
  33. bool IsRunningTutorial() const;
  34. // Sets the current help bubble stored by the service.
  35. void SetCurrentBubble(std::unique_ptr<HelpBubble> bubble);
  36. // Hides the current help bubble currently being shown by the service.
  37. void HideCurrentBubbleIfShowing();
  38. // Starts the tutorial by looking for the id in the Tutorial Registry.
  39. virtual bool StartTutorial(
  40. TutorialIdentifier id,
  41. ui::ElementContext context,
  42. CompletedCallback completed_callback = base::DoNothing(),
  43. AbortedCallback aborted_callback = base::DoNothing());
  44. void LogIPHLinkClicked(TutorialIdentifier id, bool iph_link_was_clicked);
  45. virtual void LogStartedFromWhatsNewPage(TutorialIdentifier id,
  46. bool iph_link_was_clicked);
  47. // Uses the stored tutorial creation params to restart a tutorial. Replaces
  48. // the current_tutorial with a newly generated tutorial.
  49. bool RestartTutorial();
  50. // Accessors for registries.
  51. TutorialRegistry* tutorial_registry() { return tutorial_registry_; }
  52. HelpBubbleFactoryRegistry* bubble_factory_registry() {
  53. return help_bubble_factory_registry_;
  54. }
  55. // Accessors for the help bubble used in tests.
  56. HelpBubble* currently_displayed_bubble() {
  57. return currently_displayed_bubble_.get();
  58. }
  59. // Calls the abort code for the running tutorial.
  60. void AbortTutorial(absl::optional<int> abort_step);
  61. // Returns application-specific strings.
  62. virtual std::u16string GetBodyIconAltText(bool is_last_step) const = 0;
  63. private:
  64. friend class Tutorial;
  65. friend TutorialInteractiveUitest;
  66. // Struct used to reconstruct a tutorial from the params initially used to
  67. // create it.
  68. struct TutorialCreationParams {
  69. TutorialCreationParams(TutorialDescription* description,
  70. ui::ElementContext context);
  71. raw_ptr<TutorialDescription> description_;
  72. ui::ElementContext context_;
  73. };
  74. // Calls the completion code for the running tutorial.
  75. // TODO (dpenning): allow for registering a callback that performs any
  76. // IPH/other code on completion of tutorial
  77. void CompleteTutorial();
  78. // Reset all of the running tutorial member variables.
  79. void ResetRunningTutorial();
  80. // Tracks when the user toggles focus to a help bubble via the keyboard.
  81. void OnFocusToggledForAccessibility(HelpBubble* bubble);
  82. // Creation params for the last started tutorial. Used to restart the
  83. // tutorial after it has been completed.
  84. std::unique_ptr<TutorialCreationParams> running_tutorial_creation_params_;
  85. // The current tutorial created by the start or restart methods. This
  86. // tutorial is not required to have it's interaction sequence started to
  87. // be stored in the service.
  88. std::unique_ptr<Tutorial> running_tutorial_;
  89. // Was restarted denotes that the current running tutorial was restarted,
  90. // and when logging that the tutorial aborts, instead should log as completed.
  91. bool running_tutorial_was_restarted_ = false;
  92. // Called when the current tutorial is completed.
  93. CompletedCallback completed_callback_ = base::DoNothing();
  94. // Called if the current tutorial is aborted.
  95. AbortedCallback aborted_callback_ = base::DoNothing();
  96. // The current help bubble displayed by the tutorial. This is owned by the
  97. // service so that when the tutorial exits, the bubble can continue existing.
  98. std::unique_ptr<HelpBubble> currently_displayed_bubble_;
  99. // Pointers to the registries used for constructing and showing tutorials and
  100. // help bubbles.
  101. const raw_ptr<TutorialRegistry> tutorial_registry_;
  102. const raw_ptr<HelpBubbleFactoryRegistry> help_bubble_factory_registry_;
  103. // Number of times focus was toggled during the current tutorial.
  104. int toggle_focus_count_ = 0;
  105. base::CallbackListSubscription toggle_focus_subscription_;
  106. // status bit to denote that the tutorial service is in the process of
  107. // restarting a tutorial. This prevents calling the abort callbacks.
  108. bool is_restarting_ = false;
  109. };
  110. } // namespace user_education
  111. #endif // COMPONENTS_USER_EDUCATION_COMMON_TUTORIAL_SERVICE_H_