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