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" |
David Black | 1d1b1b67 | 2018-06-20 21:41:57 | [diff] [blame] | 9 | #include "ash/assistant/assistant_ui_controller.h" |
David Black | edaf0c3 | 2018-06-28 22:06:39 | [diff] [blame] | 10 | #include "ash/assistant/util/deep_link_util.h" |
David Black | 2f7a70c3 | 2018-06-26 02:14:22 | [diff] [blame] | 11 | #include "ash/new_window_controller.h" |
David Black | e1a4b9ed | 2018-04-19 19:55:08 | [diff] [blame] | 12 | #include "ash/session/session_controller.h" |
David Black | e8f81504 | 2018-03-28 18:59:37 | [diff] [blame] | 13 | #include "ash/shell.h" |
Muyuan Li | 0070f44d | 2018-05-16 02:57:23 | [diff] [blame] | 14 | #include "base/bind.h" |
| 15 | #include "base/memory/scoped_refptr.h" |
David Black | e1a4b9ed | 2018-04-19 19:55:08 | [diff] [blame] | 16 | #include "base/unguessable_token.h" |
Muyuan Li | 0070f44d | 2018-05-16 02:57:23 | [diff] [blame] | 17 | #include "ui/snapshot/snapshot.h" |
David Black | e8f81504 | 2018-03-28 18:59:37 | [diff] [blame] | 18 | |
| 19 | namespace ash { |
| 20 | |
David Black | 00fdce3 | 2018-05-10 20:59:14 | [diff] [blame] | 21 | AssistantController::AssistantController() |
David Black | 9766ecd | 2018-06-20 18:29:11 | [diff] [blame] | 22 | : assistant_interaction_controller_( |
David Black | 2f7a70c3 | 2018-06-26 02:14:22 | [diff] [blame] | 23 | std::make_unique<AssistantInteractionController>(this)), |
David Black | 4703d7b | 2018-06-22 00:16:39 | [diff] [blame] | 24 | assistant_ui_controller_(std::make_unique<AssistantUiController>(this)) { |
| 25 | // Note that the sub-controllers have a circular dependency. |
| 26 | // TODO(dmblack): Remove this circular dependency. |
| 27 | assistant_interaction_controller_->SetAssistantUiController( |
| 28 | assistant_ui_controller_.get()); |
| 29 | assistant_ui_controller_->SetAssistantInteractionController( |
| 30 | assistant_interaction_controller_.get()); |
David Black | 315ac2c | 2018-06-25 22:25:20 | [diff] [blame] | 31 | |
| 32 | // Observe HighlighterController for region selections. |
| 33 | Shell::Get()->highlighter_controller()->AddObserver(this); |
David Black | 4703d7b | 2018-06-22 00:16:39 | [diff] [blame] | 34 | } |
David Black | e8f81504 | 2018-03-28 18:59:37 | [diff] [blame] | 35 | |
David Black | 4703d7b | 2018-06-22 00:16:39 | [diff] [blame] | 36 | AssistantController::~AssistantController() { |
David Black | 315ac2c | 2018-06-25 22:25:20 | [diff] [blame] | 37 | Shell::Get()->highlighter_controller()->RemoveObserver(this); |
| 38 | |
David Black | 4703d7b | 2018-06-22 00:16:39 | [diff] [blame] | 39 | // Explicitly clean up the circular dependency in the sub-controllers. |
| 40 | assistant_interaction_controller_->SetAssistantUiController(nullptr); |
| 41 | assistant_ui_controller_->SetAssistantInteractionController(nullptr); |
| 42 | } |
David Black | e8f81504 | 2018-03-28 18:59:37 | [diff] [blame] | 43 | |
David Black | 00fdce3 | 2018-05-10 20:59:14 | [diff] [blame] | 44 | void AssistantController::BindRequest( |
| 45 | mojom::AssistantControllerRequest request) { |
David Black | 75f9fec | 2018-05-17 22:10:39 | [diff] [blame] | 46 | assistant_controller_bindings_.AddBinding(this, std::move(request)); |
David Black | e8f81504 | 2018-03-28 18:59:37 | [diff] [blame] | 47 | } |
| 48 | |
David Black | e802d406 | 2018-06-29 01:59:05 | [diff] [blame^] | 49 | void AssistantController::AddObserver(AssistantControllerObserver* observer) { |
| 50 | observers_.AddObserver(observer); |
| 51 | } |
| 52 | |
| 53 | void AssistantController::RemoveObserver( |
| 54 | AssistantControllerObserver* observer) { |
| 55 | observers_.RemoveObserver(observer); |
| 56 | } |
| 57 | |
David Black | 00fdce3 | 2018-05-10 20:59:14 | [diff] [blame] | 58 | void AssistantController::SetAssistant( |
David Black | e8f81504 | 2018-03-28 18:59:37 | [diff] [blame] | 59 | chromeos::assistant::mojom::AssistantPtr assistant) { |
David Black | 4c3656c | 2018-04-19 17:31:47 | [diff] [blame] | 60 | assistant_ = std::move(assistant); |
| 61 | |
David Black | 4703d7b | 2018-06-22 00:16:39 | [diff] [blame] | 62 | // Provide reference to sub-controllers. |
David Black | 9766ecd | 2018-06-20 18:29:11 | [diff] [blame] | 63 | assistant_interaction_controller_->SetAssistant(assistant_.get()); |
David Black | 4703d7b | 2018-06-22 00:16:39 | [diff] [blame] | 64 | assistant_ui_controller_->SetAssistant(assistant_.get()); |
David Black | 4c3656c | 2018-04-19 17:31:47 | [diff] [blame] | 65 | } |
| 66 | |
David Black | 7ffd284 | 2018-05-25 01:25:53 | [diff] [blame] | 67 | void AssistantController::SetAssistantImageDownloader( |
| 68 | mojom::AssistantImageDownloaderPtr assistant_image_downloader) { |
| 69 | assistant_image_downloader_ = std::move(assistant_image_downloader); |
| 70 | } |
| 71 | |
Yue Li | bf89f72a | 2018-06-18 16:51:16 | [diff] [blame] | 72 | void AssistantController::SetAssistantSetup( |
| 73 | mojom::AssistantSetupPtr assistant_setup) { |
| 74 | assistant_setup_ = std::move(assistant_setup); |
David Black | 9766ecd | 2018-06-20 18:29:11 | [diff] [blame] | 75 | |
David Black | 4703d7b | 2018-06-22 00:16:39 | [diff] [blame] | 76 | // Provide reference to UI controller. |
| 77 | assistant_ui_controller_->SetAssistantSetup(assistant_setup_.get()); |
David Black | 9766ecd | 2018-06-20 18:29:11 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void AssistantController::SetWebContentsManager( |
| 81 | mojom::WebContentsManagerPtr web_contents_manager) { |
| 82 | web_contents_manager_ = std::move(web_contents_manager); |
Yue Li | bf89f72a | 2018-06-18 16:51:16 | [diff] [blame] | 83 | } |
| 84 | |
Muyuan Li | 0070f44d | 2018-05-16 02:57:23 | [diff] [blame] | 85 | void AssistantController::RequestScreenshot( |
| 86 | const gfx::Rect& rect, |
| 87 | RequestScreenshotCallback callback) { |
| 88 | // TODO(muyuanli): handle multi-display when assistant's behavior is defined. |
| 89 | auto* root_window = Shell::GetPrimaryRootWindow(); |
| 90 | gfx::Rect source_rect = |
| 91 | rect.IsEmpty() ? gfx::Rect(root_window->bounds().size()) : rect; |
| 92 | ui::GrabWindowSnapshotAsyncJPEG( |
| 93 | root_window, source_rect, |
| 94 | base::BindRepeating( |
| 95 | [](RequestScreenshotCallback callback, |
| 96 | scoped_refptr<base::RefCountedMemory> data) { |
| 97 | std::move(callback).Run(std::vector<uint8_t>( |
| 98 | data->front(), data->front() + data->size())); |
| 99 | }, |
| 100 | base::Passed(&callback))); |
| 101 | } |
| 102 | |
David Black | 5326a8eb | 2018-06-12 22:05:34 | [diff] [blame] | 103 | void AssistantController::ManageWebContents( |
David Black | e1a4b9ed | 2018-04-19 19:55:08 | [diff] [blame] | 104 | const base::UnguessableToken& id_token, |
David Black | 5326a8eb | 2018-06-12 22:05:34 | [diff] [blame] | 105 | mojom::ManagedWebContentsParamsPtr params, |
| 106 | mojom::WebContentsManager::ManageWebContentsCallback callback) { |
| 107 | DCHECK(web_contents_manager_); |
David Black | e1a4b9ed | 2018-04-19 19:55:08 | [diff] [blame] | 108 | |
| 109 | const mojom::UserSession* user_session = |
| 110 | Shell::Get()->session_controller()->GetUserSession(0); |
| 111 | |
| 112 | if (!user_session) { |
| 113 | LOG(WARNING) << "Unable to retrieve active user session."; |
David Black | 5326a8eb | 2018-06-12 22:05:34 | [diff] [blame] | 114 | std::move(callback).Run(base::nullopt); |
David Black | e1a4b9ed | 2018-04-19 19:55:08 | [diff] [blame] | 115 | return; |
| 116 | } |
| 117 | |
David Black | 5326a8eb | 2018-06-12 22:05:34 | [diff] [blame] | 118 | // Supply account ID. |
| 119 | params->account_id = user_session->user_info->account_id; |
| 120 | |
| 121 | // Specify that we will handle top level browser requests. |
David Black | 5326a8eb | 2018-06-12 22:05:34 | [diff] [blame] | 122 | ash::mojom::ManagedWebContentsOpenUrlDelegatePtr ptr; |
David Black | 2f7a70c3 | 2018-06-26 02:14:22 | [diff] [blame] | 123 | web_contents_open_url_delegate_bindings_.AddBinding(this, |
| 124 | mojo::MakeRequest(&ptr)); |
David Black | 5326a8eb | 2018-06-12 22:05:34 | [diff] [blame] | 125 | params->open_url_delegate_ptr_info = ptr.PassInterface(); |
| 126 | |
| 127 | web_contents_manager_->ManageWebContents(id_token, std::move(params), |
| 128 | std::move(callback)); |
David Black | e1a4b9ed | 2018-04-19 19:55:08 | [diff] [blame] | 129 | } |
| 130 | |
David Black | 5326a8eb | 2018-06-12 22:05:34 | [diff] [blame] | 131 | void AssistantController::ReleaseWebContents( |
| 132 | const base::UnguessableToken& id_token) { |
| 133 | web_contents_manager_->ReleaseWebContents(id_token); |
David Black | e1a4b9ed | 2018-04-19 19:55:08 | [diff] [blame] | 134 | } |
| 135 | |
David Black | 5326a8eb | 2018-06-12 22:05:34 | [diff] [blame] | 136 | void AssistantController::ReleaseWebContents( |
David Black | 3bfd96860 | 2018-04-24 22:07:46 | [diff] [blame] | 137 | const std::vector<base::UnguessableToken>& id_tokens) { |
David Black | 5326a8eb | 2018-06-12 22:05:34 | [diff] [blame] | 138 | web_contents_manager_->ReleaseAllWebContents(id_tokens); |
David Black | 3bfd96860 | 2018-04-24 22:07:46 | [diff] [blame] | 139 | } |
| 140 | |
David Black | 7ffd284 | 2018-05-25 01:25:53 | [diff] [blame] | 141 | void AssistantController::DownloadImage( |
| 142 | const GURL& url, |
| 143 | mojom::AssistantImageDownloader::DownloadCallback callback) { |
| 144 | DCHECK(assistant_image_downloader_); |
| 145 | |
| 146 | const mojom::UserSession* user_session = |
| 147 | Shell::Get()->session_controller()->GetUserSession(0); |
| 148 | |
| 149 | if (!user_session) { |
| 150 | LOG(WARNING) << "Unable to retrieve active user session."; |
David Black | 5326a8eb | 2018-06-12 22:05:34 | [diff] [blame] | 151 | std::move(callback).Run(gfx::ImageSkia()); |
David Black | 7ffd284 | 2018-05-25 01:25:53 | [diff] [blame] | 152 | return; |
| 153 | } |
| 154 | |
| 155 | AccountId account_id = user_session->user_info->account_id; |
| 156 | assistant_image_downloader_->Download(account_id, url, std::move(callback)); |
| 157 | } |
| 158 | |
David Black | 315ac2c | 2018-06-25 22:25:20 | [diff] [blame] | 159 | void AssistantController::OnHighlighterSelectionRecognized( |
| 160 | const gfx::Rect& rect) { |
| 161 | // TODO(muyuanli): Request screen context for |rect|. |
| 162 | NOTIMPLEMENTED(); |
| 163 | } |
| 164 | |
David Black | 2f7a70c3 | 2018-06-26 02:14:22 | [diff] [blame] | 165 | void AssistantController::OnOpenUrlFromTab(const GURL& url) { |
| 166 | OpenUrl(url); |
| 167 | } |
| 168 | |
| 169 | void AssistantController::OpenUrl(const GURL& url) { |
David Black | edaf0c3 | 2018-06-28 22:06:39 | [diff] [blame] | 170 | if (assistant::util::IsDeepLinkUrl(url)) { |
David Black | e802d406 | 2018-06-29 01:59:05 | [diff] [blame^] | 171 | for (AssistantControllerObserver& observer : observers_) |
| 172 | observer.OnDeepLinkReceived(url); |
David Black | edaf0c3 | 2018-06-28 22:06:39 | [diff] [blame] | 173 | return; |
| 174 | } |
David Black | 2f7a70c3 | 2018-06-26 02:14:22 | [diff] [blame] | 175 | |
David Black | edaf0c3 | 2018-06-28 22:06:39 | [diff] [blame] | 176 | Shell::Get()->new_window_controller()->NewTabWithUrl(url); |
David Black | e802d406 | 2018-06-29 01:59:05 | [diff] [blame^] | 177 | |
| 178 | for (AssistantControllerObserver& observer : observers_) |
| 179 | observer.OnUrlOpened(url); |
David Black | 2f7a70c3 | 2018-06-26 02:14:22 | [diff] [blame] | 180 | } |
| 181 | |
David Black | e8f81504 | 2018-03-28 18:59:37 | [diff] [blame] | 182 | } // namespace ash |