assistant_view_delegate_impl.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright 2018 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. #include "ash/assistant/assistant_view_delegate_impl.h"
  5. #include <utility>
  6. #include "ash/assistant/assistant_controller_impl.h"
  7. #include "ash/assistant/assistant_notification_controller_impl.h"
  8. #include "ash/assistant/model/assistant_interaction_model.h"
  9. #include "ash/assistant/model/assistant_interaction_model_observer.h"
  10. #include "ash/assistant/model/assistant_notification_model.h"
  11. #include "ash/assistant/model/assistant_notification_model_observer.h"
  12. #include "ash/assistant/ui/assistant_ui_constants.h"
  13. #include "ash/public/cpp/assistant/assistant_state_base.h"
  14. #include "ash/public/cpp/session/session_types.h"
  15. #include "ash/public/cpp/session/user_info.h"
  16. #include "ash/session/session_controller_impl.h"
  17. #include "ash/shell.h"
  18. #include "ash/wm/tablet_mode/tablet_mode_controller.h"
  19. #include "base/command_line.h"
  20. #include "chromeos/ash/services/assistant/public/cpp/features.h"
  21. #include "chromeos/ash/services/assistant/public/cpp/switches.h"
  22. namespace ash {
  23. namespace {
  24. using assistant::ui::kOnboardingMaxSessionsShown;
  25. } // namespace
  26. AssistantViewDelegateImpl::AssistantViewDelegateImpl(
  27. AssistantControllerImpl* assistant_controller)
  28. : assistant_controller_(assistant_controller) {}
  29. AssistantViewDelegateImpl::~AssistantViewDelegateImpl() = default;
  30. const AssistantNotificationModel*
  31. AssistantViewDelegateImpl::GetNotificationModel() const {
  32. return assistant_controller_->notification_controller()->model();
  33. }
  34. void AssistantViewDelegateImpl::AddObserver(
  35. AssistantViewDelegateObserver* observer) {
  36. view_delegate_observers_.AddObserver(observer);
  37. }
  38. void AssistantViewDelegateImpl::RemoveObserver(
  39. AssistantViewDelegateObserver* observer) {
  40. view_delegate_observers_.RemoveObserver(observer);
  41. }
  42. void AssistantViewDelegateImpl::DownloadImage(
  43. const GURL& url,
  44. ImageDownloader::DownloadCallback callback) {
  45. assistant_controller_->DownloadImage(url, std::move(callback));
  46. }
  47. ::wm::CursorManager* AssistantViewDelegateImpl::GetCursorManager() {
  48. return Shell::Get()->cursor_manager();
  49. }
  50. std::string AssistantViewDelegateImpl::GetPrimaryUserGivenName() const {
  51. return Shell::Get()
  52. ->session_controller()
  53. ->GetPrimaryUserSession()
  54. ->user_info.given_name;
  55. }
  56. aura::Window* AssistantViewDelegateImpl::GetRootWindowForDisplayId(
  57. int64_t display_id) {
  58. return Shell::Get()->GetRootWindowForDisplayId(display_id);
  59. }
  60. aura::Window* AssistantViewDelegateImpl::GetRootWindowForNewWindows() {
  61. return Shell::Get()->GetRootWindowForNewWindows();
  62. }
  63. bool AssistantViewDelegateImpl::IsTabletMode() const {
  64. return Shell::Get()->tablet_mode_controller()->InTabletMode();
  65. }
  66. void AssistantViewDelegateImpl::OnDialogPlateButtonPressed(
  67. AssistantButtonId id) {
  68. for (auto& observer : view_delegate_observers_)
  69. observer.OnDialogPlateButtonPressed(id);
  70. }
  71. void AssistantViewDelegateImpl::OnDialogPlateContentsCommitted(
  72. const std::string& text) {
  73. for (auto& observer : view_delegate_observers_)
  74. observer.OnDialogPlateContentsCommitted(text);
  75. }
  76. void AssistantViewDelegateImpl::OnNotificationButtonPressed(
  77. const std::string& notification_id,
  78. int notification_button_index) {
  79. assistant_controller_->notification_controller()->OnNotificationClicked(
  80. notification_id, notification_button_index, /*reply=*/absl::nullopt);
  81. }
  82. void AssistantViewDelegateImpl::OnOnboardingShown() {
  83. for (auto& observer : view_delegate_observers_)
  84. observer.OnOnboardingShown();
  85. }
  86. void AssistantViewDelegateImpl::OnOptInButtonPressed() {
  87. for (auto& observer : view_delegate_observers_)
  88. observer.OnOptInButtonPressed();
  89. }
  90. void AssistantViewDelegateImpl::OnSuggestionPressed(
  91. const base::UnguessableToken& suggestion_id) {
  92. for (AssistantViewDelegateObserver& observer : view_delegate_observers_)
  93. observer.OnSuggestionPressed(suggestion_id);
  94. }
  95. bool AssistantViewDelegateImpl::ShouldShowOnboarding() const {
  96. // UI developers need to be able to force the onboarding flow.
  97. if (base::CommandLine::ForCurrentProcess()->HasSwitch(
  98. assistant::switches::kForceAssistantOnboarding)) {
  99. return true;
  100. }
  101. // Once a user has had an interaction with Assistant, we will no longer show
  102. // onboarding in that user session.
  103. auto* interaction_controller = AssistantInteractionController::Get();
  104. const bool has_had_interaction = interaction_controller->HasHadInteraction();
  105. if (has_had_interaction)
  106. return false;
  107. // If we do show onboarding to a user in a session, we will keep showing it
  108. // for that session until an Assistant interaction takes place.
  109. auto* ui_controller = AssistantUiController::Get();
  110. const bool has_shown_onboarding = ui_controller->HasShownOnboarding();
  111. if (has_shown_onboarding)
  112. return true;
  113. // Once a user has seen onboarding in any session, they will continue to see
  114. // onboarding each session until the maximum number of sessions is reached.
  115. const int number_of_sessions_where_onboarding_shown =
  116. ui_controller->GetNumberOfSessionsWhereOnboardingShown();
  117. if (number_of_sessions_where_onboarding_shown > 0) {
  118. return number_of_sessions_where_onboarding_shown <
  119. kOnboardingMaxSessionsShown;
  120. }
  121. // The feature will start to show only for new users which we define as users
  122. // who haven't had an interaction with Assistant in the last 28 days.
  123. return interaction_controller->GetTimeDeltaSinceLastInteraction() >=
  124. base::Days(28);
  125. }
  126. } // namespace ash