David Black | 71ab74d | 2018-04-12 06:52:31 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors. All rights reserved. |
David Black | e8f81504 | 2018-03-28 18:59:37 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
David Black | 00fdce3 | 2018-05-10 20:59:14 | [diff] [blame] | 5 | #include "ash/assistant/assistant_controller.h" |
David Black | e8f81504 | 2018-03-28 18:59:37 | [diff] [blame] | 6 | |
David Black | e802d406 | 2018-06-29 01:59:05 | [diff] [blame] | 7 | #include "ash/assistant/assistant_controller_observer.h" |
David Black | 9766ecd | 2018-06-20 18:29:11 | [diff] [blame] | 8 | #include "ash/assistant/assistant_interaction_controller.h" |
wutao | 16db603c | 2018-07-10 06:43:59 | [diff] [blame] | 9 | #include "ash/assistant/assistant_notification_controller.h" |
David Black | c22e435 | 2018-07-11 21:29:48 | [diff] [blame] | 10 | #include "ash/assistant/assistant_screen_context_controller.h" |
David Black | ee6f32b5 | 2018-08-09 21:28:46 | [diff] [blame] | 11 | #include "ash/assistant/assistant_setup_controller.h" |
David Black | 1d1b1b67 | 2018-06-20 21:41:57 | [diff] [blame] | 12 | #include "ash/assistant/assistant_ui_controller.h" |
David Black | edaf0c3 | 2018-06-28 22:06:39 | [diff] [blame] | 13 | #include "ash/assistant/util/deep_link_util.h" |
David Black | 2f7a70c3 | 2018-06-26 02:14:22 | [diff] [blame] | 14 | #include "ash/new_window_controller.h" |
David Black | e1a4b9ed | 2018-04-19 19:55:08 | [diff] [blame] | 15 | #include "ash/session/session_controller.h" |
David Black | e8f81504 | 2018-03-28 18:59:37 | [diff] [blame] | 16 | #include "ash/shell.h" |
Yue Li | bf33defd | 2018-08-01 21:04:06 | [diff] [blame] | 17 | #include "ash/voice_interaction/voice_interaction_controller.h" |
Muyuan Li | 0070f44d | 2018-05-16 02:57:23 | [diff] [blame] | 18 | #include "base/bind.h" |
| 19 | #include "base/memory/scoped_refptr.h" |
David Black | e1a4b9ed | 2018-04-19 19:55:08 | [diff] [blame] | 20 | #include "base/unguessable_token.h" |
David Black | e8f81504 | 2018-03-28 18:59:37 | [diff] [blame] | 21 | |
| 22 | namespace ash { |
| 23 | |
David Black | 00fdce3 | 2018-05-10 20:59:14 | [diff] [blame] | 24 | AssistantController::AssistantController() |
Muyuan Li | ed7c7640 | 2018-08-07 23:59:00 | [diff] [blame] | 25 | : assistant_volume_control_binding_(this), |
| 26 | assistant_interaction_controller_( |
David Black | 2f7a70c3 | 2018-06-26 02:14:22 | [diff] [blame] | 27 | std::make_unique<AssistantInteractionController>(this)), |
wutao | 16db603c | 2018-07-10 06:43:59 | [diff] [blame] | 28 | assistant_notification_controller_( |
| 29 | std::make_unique<AssistantNotificationController>(this)), |
David Black | c22e435 | 2018-07-11 21:29:48 | [diff] [blame] | 30 | assistant_screen_context_controller_( |
David Black | 1f58bcc | 2018-07-12 17:59:40 | [diff] [blame] | 31 | std::make_unique<AssistantScreenContextController>(this)), |
David Black | ee6f32b5 | 2018-08-09 21:28:46 | [diff] [blame] | 32 | assistant_setup_controller_( |
| 33 | std::make_unique<AssistantSetupController>(this)), |
David Black | c22e435 | 2018-07-11 21:29:48 | [diff] [blame] | 34 | assistant_ui_controller_(std::make_unique<AssistantUiController>(this)), |
Yue Li | bf33defd | 2018-08-01 21:04:06 | [diff] [blame] | 35 | voice_interaction_binding_(this), |
wutao | 16db603c | 2018-07-10 06:43:59 | [diff] [blame] | 36 | weak_factory_(this) { |
Yue Li | bf33defd | 2018-08-01 21:04:06 | [diff] [blame] | 37 | mojom::VoiceInteractionObserverPtr ptr; |
| 38 | voice_interaction_binding_.Bind(mojo::MakeRequest(&ptr)); |
| 39 | Shell::Get()->voice_interaction_controller()->AddObserver(std::move(ptr)); |
| 40 | |
David Black | f946f83 | 2018-07-13 17:29:09 | [diff] [blame] | 41 | AddObserver(this); |
David Black | 1f58bcc | 2018-07-12 17:59:40 | [diff] [blame] | 42 | NotifyConstructed(); |
Muyuan Li | ed7c7640 | 2018-08-07 23:59:00 | [diff] [blame] | 43 | chromeos::CrasAudioHandler::Get()->AddAudioObserver(this); |
David Black | 4703d7b | 2018-06-22 00:16:39 | [diff] [blame] | 44 | } |
David Black | e8f81504 | 2018-03-28 18:59:37 | [diff] [blame] | 45 | |
David Black | 4703d7b | 2018-06-22 00:16:39 | [diff] [blame] | 46 | AssistantController::~AssistantController() { |
Muyuan Li | ed7c7640 | 2018-08-07 23:59:00 | [diff] [blame] | 47 | chromeos::CrasAudioHandler::Get()->RemoveAudioObserver(this); |
David Black | 1f58bcc | 2018-07-12 17:59:40 | [diff] [blame] | 48 | NotifyDestroying(); |
David Black | f946f83 | 2018-07-13 17:29:09 | [diff] [blame] | 49 | RemoveObserver(this); |
David Black | 4703d7b | 2018-06-22 00:16:39 | [diff] [blame] | 50 | } |
David Black | e8f81504 | 2018-03-28 18:59:37 | [diff] [blame] | 51 | |
David Black | 00fdce3 | 2018-05-10 20:59:14 | [diff] [blame] | 52 | void AssistantController::BindRequest( |
| 53 | mojom::AssistantControllerRequest request) { |
David Black | 75f9fec | 2018-05-17 22:10:39 | [diff] [blame] | 54 | assistant_controller_bindings_.AddBinding(this, std::move(request)); |
David Black | e8f81504 | 2018-03-28 18:59:37 | [diff] [blame] | 55 | } |
| 56 | |
Muyuan Li | ed7c7640 | 2018-08-07 23:59:00 | [diff] [blame] | 57 | void AssistantController::BindRequest( |
| 58 | mojom::AssistantVolumeControlRequest request) { |
| 59 | assistant_volume_control_binding_.Bind(std::move(request)); |
| 60 | } |
| 61 | |
David Black | e802d406 | 2018-06-29 01:59:05 | [diff] [blame] | 62 | void AssistantController::AddObserver(AssistantControllerObserver* observer) { |
| 63 | observers_.AddObserver(observer); |
| 64 | } |
| 65 | |
| 66 | void AssistantController::RemoveObserver( |
| 67 | AssistantControllerObserver* observer) { |
| 68 | observers_.RemoveObserver(observer); |
| 69 | } |
| 70 | |
David Black | 00fdce3 | 2018-05-10 20:59:14 | [diff] [blame] | 71 | void AssistantController::SetAssistant( |
David Black | e8f81504 | 2018-03-28 18:59:37 | [diff] [blame] | 72 | chromeos::assistant::mojom::AssistantPtr assistant) { |
David Black | 4c3656c | 2018-04-19 17:31:47 | [diff] [blame] | 73 | assistant_ = std::move(assistant); |
| 74 | |
David Black | 4703d7b | 2018-06-22 00:16:39 | [diff] [blame] | 75 | // Provide reference to sub-controllers. |
David Black | 9766ecd | 2018-06-20 18:29:11 | [diff] [blame] | 76 | assistant_interaction_controller_->SetAssistant(assistant_.get()); |
wutao | 16db603c | 2018-07-10 06:43:59 | [diff] [blame] | 77 | assistant_notification_controller_->SetAssistant(assistant_.get()); |
David Black | c22e435 | 2018-07-11 21:29:48 | [diff] [blame] | 78 | assistant_screen_context_controller_->SetAssistant(assistant_.get()); |
| 79 | assistant_ui_controller_->SetAssistant(assistant_.get()); |
David Black | 4c3656c | 2018-04-19 17:31:47 | [diff] [blame] | 80 | } |
| 81 | |
David Black | 7ffd284 | 2018-05-25 01:25:53 | [diff] [blame] | 82 | void AssistantController::SetAssistantImageDownloader( |
| 83 | mojom::AssistantImageDownloaderPtr assistant_image_downloader) { |
| 84 | assistant_image_downloader_ = std::move(assistant_image_downloader); |
| 85 | } |
| 86 | |
David Black | ee6f32b5 | 2018-08-09 21:28:46 | [diff] [blame] | 87 | // TODO(dmblack): Call SetAssistantSetup directly on AssistantSetupController |
| 88 | // instead of going through AssistantController. |
Yue Li | bf89f72a | 2018-06-18 16:51:16 | [diff] [blame] | 89 | void AssistantController::SetAssistantSetup( |
| 90 | mojom::AssistantSetupPtr assistant_setup) { |
| 91 | assistant_setup_ = std::move(assistant_setup); |
David Black | ee6f32b5 | 2018-08-09 21:28:46 | [diff] [blame] | 92 | assistant_setup_controller_->SetAssistantSetup(assistant_setup_.get()); |
David Black | 9766ecd | 2018-06-20 18:29:11 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void AssistantController::SetWebContentsManager( |
| 96 | mojom::WebContentsManagerPtr web_contents_manager) { |
| 97 | web_contents_manager_ = std::move(web_contents_manager); |
Yue Li | bf89f72a | 2018-06-18 16:51:16 | [diff] [blame] | 98 | } |
| 99 | |
David Black | c22e435 | 2018-07-11 21:29:48 | [diff] [blame] | 100 | // TODO(dmblack): Expose AssistantScreenContextController over mojo rather |
| 101 | // than implementing RequestScreenshot here in AssistantController. |
Muyuan Li | 0070f44d | 2018-05-16 02:57:23 | [diff] [blame] | 102 | void AssistantController::RequestScreenshot( |
| 103 | const gfx::Rect& rect, |
| 104 | RequestScreenshotCallback callback) { |
David Black | c22e435 | 2018-07-11 21:29:48 | [diff] [blame] | 105 | assistant_screen_context_controller_->RequestScreenshot(rect, |
| 106 | std::move(callback)); |
Muyuan Li | 0070f44d | 2018-05-16 02:57:23 | [diff] [blame] | 107 | } |
| 108 | |
David Black | 5326a8eb | 2018-06-12 22:05:34 | [diff] [blame] | 109 | void AssistantController::ManageWebContents( |
David Black | e1a4b9ed | 2018-04-19 19:55:08 | [diff] [blame] | 110 | const base::UnguessableToken& id_token, |
David Black | 5326a8eb | 2018-06-12 22:05:34 | [diff] [blame] | 111 | mojom::ManagedWebContentsParamsPtr params, |
| 112 | mojom::WebContentsManager::ManageWebContentsCallback callback) { |
| 113 | DCHECK(web_contents_manager_); |
David Black | e1a4b9ed | 2018-04-19 19:55:08 | [diff] [blame] | 114 | |
| 115 | const mojom::UserSession* user_session = |
| 116 | Shell::Get()->session_controller()->GetUserSession(0); |
| 117 | |
| 118 | if (!user_session) { |
| 119 | LOG(WARNING) << "Unable to retrieve active user session."; |
David Black | 5326a8eb | 2018-06-12 22:05:34 | [diff] [blame] | 120 | std::move(callback).Run(base::nullopt); |
David Black | e1a4b9ed | 2018-04-19 19:55:08 | [diff] [blame] | 121 | return; |
| 122 | } |
| 123 | |
David Black | 5326a8eb | 2018-06-12 22:05:34 | [diff] [blame] | 124 | // Supply account ID. |
| 125 | params->account_id = user_session->user_info->account_id; |
| 126 | |
| 127 | // Specify that we will handle top level browser requests. |
David Black | 5326a8eb | 2018-06-12 22:05:34 | [diff] [blame] | 128 | ash::mojom::ManagedWebContentsOpenUrlDelegatePtr ptr; |
David Black | 2f7a70c3 | 2018-06-26 02:14:22 | [diff] [blame] | 129 | web_contents_open_url_delegate_bindings_.AddBinding(this, |
| 130 | mojo::MakeRequest(&ptr)); |
David Black | 5326a8eb | 2018-06-12 22:05:34 | [diff] [blame] | 131 | params->open_url_delegate_ptr_info = ptr.PassInterface(); |
| 132 | |
| 133 | web_contents_manager_->ManageWebContents(id_token, std::move(params), |
| 134 | std::move(callback)); |
David Black | e1a4b9ed | 2018-04-19 19:55:08 | [diff] [blame] | 135 | } |
| 136 | |
David Black | 5326a8eb | 2018-06-12 22:05:34 | [diff] [blame] | 137 | void AssistantController::ReleaseWebContents( |
| 138 | const base::UnguessableToken& id_token) { |
| 139 | web_contents_manager_->ReleaseWebContents(id_token); |
David Black | e1a4b9ed | 2018-04-19 19:55:08 | [diff] [blame] | 140 | } |
| 141 | |
David Black | 5326a8eb | 2018-06-12 22:05:34 | [diff] [blame] | 142 | void AssistantController::ReleaseWebContents( |
David Black | 3bfd96860 | 2018-04-24 22:07:46 | [diff] [blame] | 143 | const std::vector<base::UnguessableToken>& id_tokens) { |
David Black | 5326a8eb | 2018-06-12 22:05:34 | [diff] [blame] | 144 | web_contents_manager_->ReleaseAllWebContents(id_tokens); |
David Black | 3bfd96860 | 2018-04-24 22:07:46 | [diff] [blame] | 145 | } |
| 146 | |
David Black | 1a42f16 | 2018-08-13 23:07:01 | [diff] [blame^] | 147 | void AssistantController::NavigateWebContentsBack( |
| 148 | const base::UnguessableToken& id_token, |
| 149 | mojom::WebContentsManager::NavigateWebContentsBackCallback callback) { |
| 150 | web_contents_manager_->NavigateWebContentsBack(id_token, std::move(callback)); |
| 151 | } |
| 152 | |
David Black | 7ffd284 | 2018-05-25 01:25:53 | [diff] [blame] | 153 | void AssistantController::DownloadImage( |
| 154 | const GURL& url, |
| 155 | mojom::AssistantImageDownloader::DownloadCallback callback) { |
| 156 | DCHECK(assistant_image_downloader_); |
| 157 | |
| 158 | const mojom::UserSession* user_session = |
| 159 | Shell::Get()->session_controller()->GetUserSession(0); |
| 160 | |
| 161 | if (!user_session) { |
| 162 | LOG(WARNING) << "Unable to retrieve active user session."; |
David Black | 5326a8eb | 2018-06-12 22:05:34 | [diff] [blame] | 163 | std::move(callback).Run(gfx::ImageSkia()); |
David Black | 7ffd284 | 2018-05-25 01:25:53 | [diff] [blame] | 164 | return; |
| 165 | } |
| 166 | |
| 167 | AccountId account_id = user_session->user_info->account_id; |
| 168 | assistant_image_downloader_->Download(account_id, url, std::move(callback)); |
| 169 | } |
| 170 | |
David Black | b0749ca5 | 2018-07-13 21:30:02 | [diff] [blame] | 171 | void AssistantController::OnDeepLinkReceived( |
| 172 | assistant::util::DeepLinkType type, |
| 173 | const std::map<std::string, std::string>& params) { |
David Black | 7c07e30e | 2018-07-25 02:27:19 | [diff] [blame] | 174 | using namespace assistant::util; |
| 175 | |
David Black | b0749ca5 | 2018-07-13 21:30:02 | [diff] [blame] | 176 | switch (type) { |
David Black | 7c07e30e | 2018-07-25 02:27:19 | [diff] [blame] | 177 | case DeepLinkType::kFeedback: |
David Black | b0749ca5 | 2018-07-13 21:30:02 | [diff] [blame] | 178 | // TODO(dmblack): Possibly use a new FeedbackSource (this method defaults |
| 179 | // to kFeedbackSourceAsh). This may be useful for differentiating feedback |
| 180 | // UI and behavior for Assistant. |
| 181 | Shell::Get()->new_window_controller()->OpenFeedbackPage(); |
| 182 | break; |
David Black | 7c07e30e | 2018-07-25 02:27:19 | [diff] [blame] | 183 | case DeepLinkType::kUnsupported: |
| 184 | case DeepLinkType::kExplore: |
David Black | ee6f32b5 | 2018-08-09 21:28:46 | [diff] [blame] | 185 | case DeepLinkType::kOnboarding: |
David Black | 7c07e30e | 2018-07-25 02:27:19 | [diff] [blame] | 186 | case DeepLinkType::kQuery: |
| 187 | case DeepLinkType::kReminders: |
| 188 | case DeepLinkType::kSettings: |
David Black | b0749ca5 | 2018-07-13 21:30:02 | [diff] [blame] | 189 | // No action needed. |
| 190 | break; |
David Black | 1859f9f | 2018-07-13 18:03:34 | [diff] [blame] | 191 | } |
David Black | f946f83 | 2018-07-13 17:29:09 | [diff] [blame] | 192 | } |
| 193 | |
David Black | 2f7a70c3 | 2018-06-26 02:14:22 | [diff] [blame] | 194 | void AssistantController::OnOpenUrlFromTab(const GURL& url) { |
| 195 | OpenUrl(url); |
| 196 | } |
| 197 | |
Muyuan Li | ed7c7640 | 2018-08-07 23:59:00 | [diff] [blame] | 198 | void AssistantController::SetVolume(int volume, bool user_initiated) { |
| 199 | volume = std::min(100, volume); |
| 200 | volume = std::max(volume, 0); |
| 201 | chromeos::CrasAudioHandler::Get()->SetOutputVolumePercent(volume); |
| 202 | } |
| 203 | |
| 204 | void AssistantController::SetMuted(bool muted) { |
| 205 | chromeos::CrasAudioHandler::Get()->SetOutputMute(muted); |
| 206 | } |
| 207 | |
| 208 | void AssistantController::AddVolumeObserver(mojom::VolumeObserverPtr observer) { |
| 209 | volume_observer_.AddPtr(std::move(observer)); |
| 210 | |
| 211 | int output_volume = |
| 212 | chromeos::CrasAudioHandler::Get()->GetOutputVolumePercent(); |
| 213 | bool mute = chromeos::CrasAudioHandler::Get()->IsOutputMuted(); |
| 214 | OnOutputMuteChanged(mute, false /* system_adjust */); |
| 215 | OnOutputNodeVolumeChanged(0 /* node */, output_volume); |
| 216 | } |
| 217 | |
| 218 | void AssistantController::OnOutputMuteChanged(bool mute_on, |
| 219 | bool system_adjust) { |
| 220 | volume_observer_.ForAllPtrs([mute_on](mojom::VolumeObserver* observer) { |
| 221 | observer->OnMuteStateChanged(mute_on); |
| 222 | }); |
| 223 | } |
| 224 | |
| 225 | void AssistantController::OnOutputNodeVolumeChanged(uint64_t node, int volume) { |
| 226 | // |node| refers to the active volume device, which we don't care here. |
| 227 | volume_observer_.ForAllPtrs([volume](mojom::VolumeObserver* observer) { |
| 228 | observer->OnVolumeChanged(volume); |
| 229 | }); |
| 230 | } |
| 231 | |
David Black | 2f7a70c3 | 2018-06-26 02:14:22 | [diff] [blame] | 232 | void AssistantController::OpenUrl(const GURL& url) { |
David Black | edaf0c3 | 2018-06-28 22:06:39 | [diff] [blame] | 233 | if (assistant::util::IsDeepLinkUrl(url)) { |
David Black | 1f58bcc | 2018-07-12 17:59:40 | [diff] [blame] | 234 | NotifyDeepLinkReceived(url); |
David Black | edaf0c3 | 2018-06-28 22:06:39 | [diff] [blame] | 235 | return; |
| 236 | } |
David Black | 2f7a70c3 | 2018-06-26 02:14:22 | [diff] [blame] | 237 | |
David Black | edaf0c3 | 2018-06-28 22:06:39 | [diff] [blame] | 238 | Shell::Get()->new_window_controller()->NewTabWithUrl(url); |
David Black | 1f58bcc | 2018-07-12 17:59:40 | [diff] [blame] | 239 | NotifyUrlOpened(url); |
| 240 | } |
David Black | e802d406 | 2018-06-29 01:59:05 | [diff] [blame] | 241 | |
David Black | 1f58bcc | 2018-07-12 17:59:40 | [diff] [blame] | 242 | void AssistantController::NotifyConstructed() { |
| 243 | for (AssistantControllerObserver& observer : observers_) |
| 244 | observer.OnAssistantControllerConstructed(); |
| 245 | } |
| 246 | |
| 247 | void AssistantController::NotifyDestroying() { |
| 248 | for (AssistantControllerObserver& observer : observers_) |
| 249 | observer.OnAssistantControllerDestroying(); |
| 250 | } |
| 251 | |
| 252 | void AssistantController::NotifyDeepLinkReceived(const GURL& deep_link) { |
David Black | b0749ca5 | 2018-07-13 21:30:02 | [diff] [blame] | 253 | using namespace assistant::util; |
| 254 | |
| 255 | // Retrieve deep link type and parsed parameters. |
| 256 | DeepLinkType type = GetDeepLinkType(deep_link); |
| 257 | const std::map<std::string, std::string> params = |
| 258 | GetDeepLinkParams(deep_link); |
| 259 | |
David Black | 1f58bcc | 2018-07-12 17:59:40 | [diff] [blame] | 260 | for (AssistantControllerObserver& observer : observers_) |
David Black | b0749ca5 | 2018-07-13 21:30:02 | [diff] [blame] | 261 | observer.OnDeepLinkReceived(type, params); |
David Black | 1f58bcc | 2018-07-12 17:59:40 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | void AssistantController::NotifyUrlOpened(const GURL& url) { |
David Black | e802d406 | 2018-06-29 01:59:05 | [diff] [blame] | 265 | for (AssistantControllerObserver& observer : observers_) |
| 266 | observer.OnUrlOpened(url); |
David Black | 2f7a70c3 | 2018-06-26 02:14:22 | [diff] [blame] | 267 | } |
| 268 | |
Yue Li | bf33defd | 2018-08-01 21:04:06 | [diff] [blame] | 269 | void AssistantController::OnVoiceInteractionStatusChanged( |
| 270 | mojom::VoiceInteractionState state) { |
Xiaohui Chen | 0571521 | 2018-08-08 15:52:28 | [diff] [blame] | 271 | if (state == mojom::VoiceInteractionState::NOT_READY) |
Yue Li | bf33defd | 2018-08-01 21:04:06 | [diff] [blame] | 272 | assistant_ui_controller_->HideUi(AssistantSource::kUnspecified); |
| 273 | } |
| 274 | |
wutao | 16db603c | 2018-07-10 06:43:59 | [diff] [blame] | 275 | base::WeakPtr<AssistantController> AssistantController::GetWeakPtr() { |
| 276 | return weak_factory_.GetWeakPtr(); |
| 277 | } |
| 278 | |
David Black | e8f81504 | 2018-03-28 18:59:37 | [diff] [blame] | 279 | } // namespace ash |