Fahad Mansoor | dfde4c0 | 2023-03-16 13:01:24 | [diff] [blame] | 1 | // Copyright 2023 The Chromium Authors |
| 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/ambient/ambient_managed_slideshow_ui_launcher.h" |
| 6 | |
Fahad Mansoor | bb5651f | 2023-04-03 10:08:25 | [diff] [blame] | 7 | #include <vector> |
| 8 | |
Fahad Mansoor | dfde4c0 | 2023-03-16 13:01:24 | [diff] [blame] | 9 | #include "ash/ambient/ambient_managed_photo_controller.h" |
| 10 | #include "ash/ambient/ambient_view_delegate_impl.h" |
| 11 | #include "ash/ambient/model/ambient_slideshow_photo_config.h" |
| 12 | #include "ash/ambient/ui/photo_view.h" |
Fahad Mansoor | bb5651f | 2023-04-03 10:08:25 | [diff] [blame] | 13 | #include "ash/public/cpp/ambient/ambient_managed_photo_source.h" |
Fahad Mansoor | dfde4c0 | 2023-03-16 13:01:24 | [diff] [blame] | 14 | #include "base/check.h" |
| 15 | #include "base/functional/callback.h" |
Fahad Mansoor | bb5651f | 2023-04-03 10:08:25 | [diff] [blame] | 16 | #include "base/logging.h" |
| 17 | #include "base/memory/weak_ptr.h" |
Fahad Mansoor | dfde4c0 | 2023-03-16 13:01:24 | [diff] [blame] | 18 | |
| 19 | namespace ash { |
| 20 | |
| 21 | AmbientManagedSlideshowUiLauncher::AmbientManagedSlideshowUiLauncher( |
| 22 | AmbientViewDelegateImpl* view_delegate) |
| 23 | : photo_controller_(*view_delegate, |
| 24 | CreateAmbientManagedSlideshowPhotoConfig()), |
| 25 | delegate_(view_delegate) { |
| 26 | ambient_backend_model_observer_.Observe( |
| 27 | photo_controller_.ambient_backend_model()); |
Fahad Mansoor | bb5651f | 2023-04-03 10:08:25 | [diff] [blame] | 28 | CHECK(AmbientManagedPhotoSource::Get()); |
| 29 | AmbientManagedPhotoSource::Get()->SetScreensaverImagesUpdatedCallback( |
| 30 | base::BindRepeating( |
| 31 | &AmbientManagedSlideshowUiLauncher::UpdateImageFilePaths, |
| 32 | weak_factory_.GetWeakPtr())); |
Fahad Mansoor | dfde4c0 | 2023-03-16 13:01:24 | [diff] [blame] | 33 | } |
| 34 | AmbientManagedSlideshowUiLauncher::~AmbientManagedSlideshowUiLauncher() = |
| 35 | default; |
| 36 | |
Fahad Mansoor | 013aa59d | 2023-04-06 12:16:08 | [diff] [blame] | 37 | void AmbientManagedSlideshowUiLauncher::Initialize( |
| 38 | InitializationCallback on_done) { |
Fahad Mansoor | dfde4c0 | 2023-03-16 13:01:24 | [diff] [blame] | 39 | initialization_callback_ = std::move(on_done); |
Fahad Mansoor | bb5651f | 2023-04-03 10:08:25 | [diff] [blame] | 40 | if (!AmbientManagedPhotoSource::Get()) { |
| 41 | LOG(WARNING) << "AmbientManagedPhotoSource not present. Probably " |
| 42 | "AmbientManagedPhotoController screen update is being " |
| 43 | "started during a shutdown"; |
Fahad Mansoor | 013aa59d | 2023-04-06 12:16:08 | [diff] [blame] | 44 | std::move(initialization_callback_).Run(/*success=*/false); |
Fahad Mansoor | bb5651f | 2023-04-03 10:08:25 | [diff] [blame] | 45 | return; |
| 46 | } |
| 47 | photo_controller_.UpdateImageFilePaths( |
| 48 | AmbientManagedPhotoSource::Get()->GetScreensaverImages()); |
Fahad Mansoor | dfde4c0 | 2023-03-16 13:01:24 | [diff] [blame] | 49 | photo_controller_.StartScreenUpdate(); |
| 50 | } |
| 51 | |
Fahad Mansoor | bb5651f | 2023-04-03 10:08:25 | [diff] [blame] | 52 | void AmbientManagedSlideshowUiLauncher::UpdateImageFilePaths( |
| 53 | const std::vector<base::FilePath>& path_to_images) { |
| 54 | photo_controller_.UpdateImageFilePaths(path_to_images); |
| 55 | } |
| 56 | |
Fahad Mansoor | dfde4c0 | 2023-03-16 13:01:24 | [diff] [blame] | 57 | std::unique_ptr<views::View> AmbientManagedSlideshowUiLauncher::CreateView() { |
Fahad Mansoor | 258b5df | 2023-04-06 10:05:19 | [diff] [blame] | 58 | return std::make_unique<PhotoView>(delegate_, |
| 59 | /*peripheral_ui_visible=*/false); |
Fahad Mansoor | dfde4c0 | 2023-03-16 13:01:24 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void AmbientManagedSlideshowUiLauncher::Finalize() { |
| 63 | photo_controller_.StopScreenUpdate(); |
| 64 | } |
| 65 | |
| 66 | AmbientBackendModel* |
| 67 | AmbientManagedSlideshowUiLauncher::GetAmbientBackendModel() { |
| 68 | return photo_controller_.ambient_backend_model(); |
| 69 | } |
Fahad Mansoor | 258b5df | 2023-04-06 10:05:19 | [diff] [blame] | 70 | |
Fahad Mansoor | dfde4c0 | 2023-03-16 13:01:24 | [diff] [blame] | 71 | void AmbientManagedSlideshowUiLauncher::OnImagesReady() { |
| 72 | CHECK(initialization_callback_); |
Fahad Mansoor | 013aa59d | 2023-04-06 12:16:08 | [diff] [blame] | 73 | std::move(initialization_callback_).Run(/*success=*/true); |
Fahad Mansoor | dfde4c0 | 2023-03-16 13:01:24 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | bool AmbientManagedSlideshowUiLauncher::IsActive() { |
| 77 | return photo_controller_.IsScreenUpdateActive(); |
| 78 | } |
| 79 | |
Fahad Mansoor | bb5651f | 2023-04-03 10:08:25 | [diff] [blame] | 80 | } // namespace ash |