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 | 1d1b1b67 | 2018-06-20 21:41:57 | [diff] [blame] | 11 | #include "ash/assistant/assistant_ui_controller.h" |
David Black | edaf0c3 | 2018-06-28 22:06:39 | [diff] [blame] | 12 | #include "ash/assistant/util/deep_link_util.h" |
David Black | 2f7a70c3 | 2018-06-26 02:14:22 | [diff] [blame] | 13 | #include "ash/new_window_controller.h" |
David Black | e1a4b9ed | 2018-04-19 19:55:08 | [diff] [blame] | 14 | #include "ash/session/session_controller.h" |
David Black | e8f81504 | 2018-03-28 18:59:37 | [diff] [blame] | 15 | #include "ash/shell.h" |
Muyuan Li | 0070f44d | 2018-05-16 02:57:23 | [diff] [blame] | 16 | #include "base/bind.h" |
| 17 | #include "base/memory/scoped_refptr.h" |
David Black | e1a4b9ed | 2018-04-19 19:55:08 | [diff] [blame] | 18 | #include "base/unguessable_token.h" |
David Black | e8f81504 | 2018-03-28 18:59:37 | [diff] [blame] | 19 | |
| 20 | namespace ash { |
| 21 | |
David Black | 00fdce3 | 2018-05-10 20:59:14 | [diff] [blame] | 22 | AssistantController::AssistantController() |
David Black | 9766ecd | 2018-06-20 18:29:11 | [diff] [blame] | 23 | : assistant_interaction_controller_( |
David Black | 2f7a70c3 | 2018-06-26 02:14:22 | [diff] [blame] | 24 | std::make_unique<AssistantInteractionController>(this)), |
wutao | 16db603c | 2018-07-10 06:43:59 | [diff] [blame] | 25 | assistant_notification_controller_( |
| 26 | std::make_unique<AssistantNotificationController>(this)), |
David Black | c22e435 | 2018-07-11 21:29:48 | [diff] [blame] | 27 | assistant_screen_context_controller_( |
David Black | 1f58bcc | 2018-07-12 17:59:40 | [diff] [blame] | 28 | std::make_unique<AssistantScreenContextController>(this)), |
David Black | c22e435 | 2018-07-11 21:29:48 | [diff] [blame] | 29 | assistant_ui_controller_(std::make_unique<AssistantUiController>(this)), |
wutao | 16db603c | 2018-07-10 06:43:59 | [diff] [blame] | 30 | weak_factory_(this) { |
David Black | f946f83 | 2018-07-13 17:29:09 | [diff] [blame] | 31 | AddObserver(this); |
David Black | 1f58bcc | 2018-07-12 17:59:40 | [diff] [blame] | 32 | NotifyConstructed(); |
David Black | 4703d7b | 2018-06-22 00:16:39 | [diff] [blame] | 33 | } |
David Black | e8f81504 | 2018-03-28 18:59:37 | [diff] [blame] | 34 | |
David Black | 4703d7b | 2018-06-22 00:16:39 | [diff] [blame] | 35 | AssistantController::~AssistantController() { |
David Black | 1f58bcc | 2018-07-12 17:59:40 | [diff] [blame] | 36 | NotifyDestroying(); |
David Black | f946f83 | 2018-07-13 17:29:09 | [diff] [blame] | 37 | RemoveObserver(this); |
David Black | 4703d7b | 2018-06-22 00:16:39 | [diff] [blame] | 38 | } |
David Black | e8f81504 | 2018-03-28 18:59:37 | [diff] [blame] | 39 | |
David Black | 00fdce3 | 2018-05-10 20:59:14 | [diff] [blame] | 40 | void AssistantController::BindRequest( |
| 41 | mojom::AssistantControllerRequest request) { |
David Black | 75f9fec | 2018-05-17 22:10:39 | [diff] [blame] | 42 | assistant_controller_bindings_.AddBinding(this, std::move(request)); |
David Black | e8f81504 | 2018-03-28 18:59:37 | [diff] [blame] | 43 | } |
| 44 | |
David Black | e802d406 | 2018-06-29 01:59:05 | [diff] [blame] | 45 | void AssistantController::AddObserver(AssistantControllerObserver* observer) { |
| 46 | observers_.AddObserver(observer); |
| 47 | } |
| 48 | |
| 49 | void AssistantController::RemoveObserver( |
| 50 | AssistantControllerObserver* observer) { |
| 51 | observers_.RemoveObserver(observer); |
| 52 | } |
| 53 | |
David Black | 00fdce3 | 2018-05-10 20:59:14 | [diff] [blame] | 54 | void AssistantController::SetAssistant( |
David Black | e8f81504 | 2018-03-28 18:59:37 | [diff] [blame] | 55 | chromeos::assistant::mojom::AssistantPtr assistant) { |
David Black | 4c3656c | 2018-04-19 17:31:47 | [diff] [blame] | 56 | assistant_ = std::move(assistant); |
| 57 | |
David Black | 4703d7b | 2018-06-22 00:16:39 | [diff] [blame] | 58 | // Provide reference to sub-controllers. |
David Black | 9766ecd | 2018-06-20 18:29:11 | [diff] [blame] | 59 | assistant_interaction_controller_->SetAssistant(assistant_.get()); |
wutao | 16db603c | 2018-07-10 06:43:59 | [diff] [blame] | 60 | assistant_notification_controller_->SetAssistant(assistant_.get()); |
David Black | c22e435 | 2018-07-11 21:29:48 | [diff] [blame] | 61 | assistant_screen_context_controller_->SetAssistant(assistant_.get()); |
| 62 | assistant_ui_controller_->SetAssistant(assistant_.get()); |
David Black | 4c3656c | 2018-04-19 17:31:47 | [diff] [blame] | 63 | } |
| 64 | |
David Black | 7ffd284 | 2018-05-25 01:25:53 | [diff] [blame] | 65 | void AssistantController::SetAssistantImageDownloader( |
| 66 | mojom::AssistantImageDownloaderPtr assistant_image_downloader) { |
| 67 | assistant_image_downloader_ = std::move(assistant_image_downloader); |
| 68 | } |
| 69 | |
Yue Li | bf89f72a | 2018-06-18 16:51:16 | [diff] [blame] | 70 | void AssistantController::SetAssistantSetup( |
| 71 | mojom::AssistantSetupPtr assistant_setup) { |
| 72 | assistant_setup_ = std::move(assistant_setup); |
David Black | 9766ecd | 2018-06-20 18:29:11 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void AssistantController::SetWebContentsManager( |
| 76 | mojom::WebContentsManagerPtr web_contents_manager) { |
| 77 | web_contents_manager_ = std::move(web_contents_manager); |
Yue Li | bf89f72a | 2018-06-18 16:51:16 | [diff] [blame] | 78 | } |
| 79 | |
David Black | c22e435 | 2018-07-11 21:29:48 | [diff] [blame] | 80 | // TODO(dmblack): Expose AssistantScreenContextController over mojo rather |
| 81 | // than implementing RequestScreenshot here in AssistantController. |
Muyuan Li | 0070f44d | 2018-05-16 02:57:23 | [diff] [blame] | 82 | void AssistantController::RequestScreenshot( |
| 83 | const gfx::Rect& rect, |
| 84 | RequestScreenshotCallback callback) { |
David Black | c22e435 | 2018-07-11 21:29:48 | [diff] [blame] | 85 | assistant_screen_context_controller_->RequestScreenshot(rect, |
| 86 | std::move(callback)); |
Muyuan Li | 0070f44d | 2018-05-16 02:57:23 | [diff] [blame] | 87 | } |
| 88 | |
David Black | 5326a8eb | 2018-06-12 22:05:34 | [diff] [blame] | 89 | void AssistantController::ManageWebContents( |
David Black | e1a4b9ed | 2018-04-19 19:55:08 | [diff] [blame] | 90 | const base::UnguessableToken& id_token, |
David Black | 5326a8eb | 2018-06-12 22:05:34 | [diff] [blame] | 91 | mojom::ManagedWebContentsParamsPtr params, |
| 92 | mojom::WebContentsManager::ManageWebContentsCallback callback) { |
| 93 | DCHECK(web_contents_manager_); |
David Black | e1a4b9ed | 2018-04-19 19:55:08 | [diff] [blame] | 94 | |
| 95 | const mojom::UserSession* user_session = |
| 96 | Shell::Get()->session_controller()->GetUserSession(0); |
| 97 | |
| 98 | if (!user_session) { |
| 99 | LOG(WARNING) << "Unable to retrieve active user session."; |
David Black | 5326a8eb | 2018-06-12 22:05:34 | [diff] [blame] | 100 | std::move(callback).Run(base::nullopt); |
David Black | e1a4b9ed | 2018-04-19 19:55:08 | [diff] [blame] | 101 | return; |
| 102 | } |
| 103 | |
David Black | 5326a8eb | 2018-06-12 22:05:34 | [diff] [blame] | 104 | // Supply account ID. |
| 105 | params->account_id = user_session->user_info->account_id; |
| 106 | |
| 107 | // Specify that we will handle top level browser requests. |
David Black | 5326a8eb | 2018-06-12 22:05:34 | [diff] [blame] | 108 | ash::mojom::ManagedWebContentsOpenUrlDelegatePtr ptr; |
David Black | 2f7a70c3 | 2018-06-26 02:14:22 | [diff] [blame] | 109 | web_contents_open_url_delegate_bindings_.AddBinding(this, |
| 110 | mojo::MakeRequest(&ptr)); |
David Black | 5326a8eb | 2018-06-12 22:05:34 | [diff] [blame] | 111 | params->open_url_delegate_ptr_info = ptr.PassInterface(); |
| 112 | |
| 113 | web_contents_manager_->ManageWebContents(id_token, std::move(params), |
| 114 | std::move(callback)); |
David Black | e1a4b9ed | 2018-04-19 19:55:08 | [diff] [blame] | 115 | } |
| 116 | |
David Black | 5326a8eb | 2018-06-12 22:05:34 | [diff] [blame] | 117 | void AssistantController::ReleaseWebContents( |
| 118 | const base::UnguessableToken& id_token) { |
| 119 | web_contents_manager_->ReleaseWebContents(id_token); |
David Black | e1a4b9ed | 2018-04-19 19:55:08 | [diff] [blame] | 120 | } |
| 121 | |
David Black | 5326a8eb | 2018-06-12 22:05:34 | [diff] [blame] | 122 | void AssistantController::ReleaseWebContents( |
David Black | 3bfd96860 | 2018-04-24 22:07:46 | [diff] [blame] | 123 | const std::vector<base::UnguessableToken>& id_tokens) { |
David Black | 5326a8eb | 2018-06-12 22:05:34 | [diff] [blame] | 124 | web_contents_manager_->ReleaseAllWebContents(id_tokens); |
David Black | 3bfd96860 | 2018-04-24 22:07:46 | [diff] [blame] | 125 | } |
| 126 | |
David Black | 7ffd284 | 2018-05-25 01:25:53 | [diff] [blame] | 127 | void AssistantController::DownloadImage( |
| 128 | const GURL& url, |
| 129 | mojom::AssistantImageDownloader::DownloadCallback callback) { |
| 130 | DCHECK(assistant_image_downloader_); |
| 131 | |
| 132 | const mojom::UserSession* user_session = |
| 133 | Shell::Get()->session_controller()->GetUserSession(0); |
| 134 | |
| 135 | if (!user_session) { |
| 136 | LOG(WARNING) << "Unable to retrieve active user session."; |
David Black | 5326a8eb | 2018-06-12 22:05:34 | [diff] [blame] | 137 | std::move(callback).Run(gfx::ImageSkia()); |
David Black | 7ffd284 | 2018-05-25 01:25:53 | [diff] [blame] | 138 | return; |
| 139 | } |
| 140 | |
| 141 | AccountId account_id = user_session->user_info->account_id; |
| 142 | assistant_image_downloader_->Download(account_id, url, std::move(callback)); |
| 143 | } |
| 144 | |
David Black | b0749ca5 | 2018-07-13 21:30:02 | [diff] [blame] | 145 | void AssistantController::OnDeepLinkReceived( |
| 146 | assistant::util::DeepLinkType type, |
| 147 | const std::map<std::string, std::string>& params) { |
David Black | 7c07e30e | 2018-07-25 02:27:19 | [diff] [blame^] | 148 | using namespace assistant::util; |
| 149 | |
David Black | b0749ca5 | 2018-07-13 21:30:02 | [diff] [blame] | 150 | switch (type) { |
David Black | 7c07e30e | 2018-07-25 02:27:19 | [diff] [blame^] | 151 | case DeepLinkType::kFeedback: |
David Black | b0749ca5 | 2018-07-13 21:30:02 | [diff] [blame] | 152 | // TODO(dmblack): Possibly use a new FeedbackSource (this method defaults |
| 153 | // to kFeedbackSourceAsh). This may be useful for differentiating feedback |
| 154 | // UI and behavior for Assistant. |
| 155 | Shell::Get()->new_window_controller()->OpenFeedbackPage(); |
| 156 | break; |
David Black | 7c07e30e | 2018-07-25 02:27:19 | [diff] [blame^] | 157 | case DeepLinkType::kOnboarding: |
| 158 | if (GetDeepLinkParamAsBool(params, DeepLinkParam::kRelaunch)) { |
Yue Li | 3a41afb4 | 2018-07-18 17:50:37 | [diff] [blame] | 159 | assistant_setup_->StartAssistantOptInFlow(base::BindOnce( |
| 160 | [](AssistantUiController* ui_controller, bool completed) { |
| 161 | if (completed) |
| 162 | ui_controller->ShowUi(AssistantSource::kSetup); |
| 163 | }, |
| 164 | // |assistant_setup_| and |assistant_ui_controller_| are both owned |
| 165 | // by this class, so a raw pointer is safe here. |
| 166 | assistant_ui_controller_.get())); |
| 167 | } else { |
| 168 | assistant_setup_->StartAssistantOptInFlow(base::DoNothing()); |
| 169 | } |
| 170 | assistant_ui_controller_->HideUi(AssistantSource::kSetup); |
David Black | b0749ca5 | 2018-07-13 21:30:02 | [diff] [blame] | 171 | break; |
David Black | 7c07e30e | 2018-07-25 02:27:19 | [diff] [blame^] | 172 | case DeepLinkType::kUnsupported: |
| 173 | case DeepLinkType::kExplore: |
| 174 | case DeepLinkType::kQuery: |
| 175 | case DeepLinkType::kReminders: |
| 176 | case DeepLinkType::kSettings: |
David Black | b0749ca5 | 2018-07-13 21:30:02 | [diff] [blame] | 177 | // No action needed. |
| 178 | break; |
David Black | 1859f9f | 2018-07-13 18:03:34 | [diff] [blame] | 179 | } |
David Black | f946f83 | 2018-07-13 17:29:09 | [diff] [blame] | 180 | } |
| 181 | |
David Black | 2f7a70c3 | 2018-06-26 02:14:22 | [diff] [blame] | 182 | void AssistantController::OnOpenUrlFromTab(const GURL& url) { |
| 183 | OpenUrl(url); |
| 184 | } |
| 185 | |
| 186 | void AssistantController::OpenUrl(const GURL& url) { |
David Black | edaf0c3 | 2018-06-28 22:06:39 | [diff] [blame] | 187 | if (assistant::util::IsDeepLinkUrl(url)) { |
David Black | 1f58bcc | 2018-07-12 17:59:40 | [diff] [blame] | 188 | NotifyDeepLinkReceived(url); |
David Black | edaf0c3 | 2018-06-28 22:06:39 | [diff] [blame] | 189 | return; |
| 190 | } |
David Black | 2f7a70c3 | 2018-06-26 02:14:22 | [diff] [blame] | 191 | |
David Black | edaf0c3 | 2018-06-28 22:06:39 | [diff] [blame] | 192 | Shell::Get()->new_window_controller()->NewTabWithUrl(url); |
David Black | 1f58bcc | 2018-07-12 17:59:40 | [diff] [blame] | 193 | NotifyUrlOpened(url); |
| 194 | } |
David Black | e802d406 | 2018-06-29 01:59:05 | [diff] [blame] | 195 | |
David Black | 1f58bcc | 2018-07-12 17:59:40 | [diff] [blame] | 196 | void AssistantController::NotifyConstructed() { |
| 197 | for (AssistantControllerObserver& observer : observers_) |
| 198 | observer.OnAssistantControllerConstructed(); |
| 199 | } |
| 200 | |
| 201 | void AssistantController::NotifyDestroying() { |
| 202 | for (AssistantControllerObserver& observer : observers_) |
| 203 | observer.OnAssistantControllerDestroying(); |
| 204 | } |
| 205 | |
| 206 | void AssistantController::NotifyDeepLinkReceived(const GURL& deep_link) { |
David Black | b0749ca5 | 2018-07-13 21:30:02 | [diff] [blame] | 207 | using namespace assistant::util; |
| 208 | |
| 209 | // Retrieve deep link type and parsed parameters. |
| 210 | DeepLinkType type = GetDeepLinkType(deep_link); |
| 211 | const std::map<std::string, std::string> params = |
| 212 | GetDeepLinkParams(deep_link); |
| 213 | |
David Black | 1f58bcc | 2018-07-12 17:59:40 | [diff] [blame] | 214 | for (AssistantControllerObserver& observer : observers_) |
David Black | b0749ca5 | 2018-07-13 21:30:02 | [diff] [blame] | 215 | observer.OnDeepLinkReceived(type, params); |
David Black | 1f58bcc | 2018-07-12 17:59:40 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | void AssistantController::NotifyUrlOpened(const GURL& url) { |
David Black | e802d406 | 2018-06-29 01:59:05 | [diff] [blame] | 219 | for (AssistantControllerObserver& observer : observers_) |
| 220 | observer.OnUrlOpened(url); |
David Black | 2f7a70c3 | 2018-06-26 02:14:22 | [diff] [blame] | 221 | } |
| 222 | |
wutao | 16db603c | 2018-07-10 06:43:59 | [diff] [blame] | 223 | base::WeakPtr<AssistantController> AssistantController::GetWeakPtr() { |
| 224 | return weak_factory_.GetWeakPtr(); |
| 225 | } |
| 226 | |
David Black | e8f81504 | 2018-03-28 18:59:37 | [diff] [blame] | 227 | } // namespace ash |