wutao | a88fcc5 | 2020-04-28 02:10:35 | [diff] [blame] | 1 | // Copyright 2020 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 | #ifndef ASH_PUBLIC_CPP_AMBIENT_AMBIENT_BACKEND_CONTROLLER_H_ |
| 6 | #define ASH_PUBLIC_CPP_AMBIENT_AMBIENT_BACKEND_CONTROLLER_H_ |
| 7 | |
Jeffrey Young | 57520f4 | 2020-10-07 02:13:35 | [diff] [blame^] | 8 | #include <array> |
wutao | a88fcc5 | 2020-04-28 02:10:35 | [diff] [blame] | 9 | #include <string> |
wutao | 41ddbe83 | 2020-05-01 17:07:03 | [diff] [blame] | 10 | #include <vector> |
wutao | a88fcc5 | 2020-04-28 02:10:35 | [diff] [blame] | 11 | |
wutao | 2592026 | 2020-06-20 21:11:10 | [diff] [blame] | 12 | #include "ash/public/cpp/ambient/common/ambient_settings.h" |
wutao | a88fcc5 | 2020-04-28 02:10:35 | [diff] [blame] | 13 | #include "ash/public/cpp/ash_public_export.h" |
| 14 | #include "base/callback_forward.h" |
| 15 | #include "base/optional.h" |
| 16 | |
wutao | 41ddbe83 | 2020-05-01 17:07:03 | [diff] [blame] | 17 | namespace base { |
| 18 | class TimeDelta; |
| 19 | } // namespace base |
| 20 | |
wutao | a88fcc5 | 2020-04-28 02:10:35 | [diff] [blame] | 21 | namespace ash { |
| 22 | |
Jeffrey Young | 4944cfe | 2020-09-24 03:29:59 | [diff] [blame] | 23 | enum class AmbientModeTopicType { |
| 24 | kCurated, |
| 25 | kPersonal, |
| 26 | kFeatured, |
| 27 | kGeo, |
| 28 | kCulturalInstitute, |
| 29 | kRss, |
| 30 | kCapturedOnPixel, |
| 31 | kOther, |
| 32 | }; |
| 33 | |
wutao | a88fcc5 | 2020-04-28 02:10:35 | [diff] [blame] | 34 | // AmbientModeTopic contains the information we need for rendering photo frame |
| 35 | // for Ambient Mode. Corresponding to the |backdrop::ScreenUpdate::Topic| proto. |
| 36 | struct ASH_PUBLIC_EXPORT AmbientModeTopic { |
| 37 | AmbientModeTopic(); |
| 38 | AmbientModeTopic(const AmbientModeTopic&); |
| 39 | AmbientModeTopic& operator=(const AmbientModeTopic&); |
| 40 | ~AmbientModeTopic(); |
| 41 | |
Meilin Wang | 3195342d | 2020-08-12 06:28:40 | [diff] [blame] | 42 | // Details, i.e. the attribution, to be displayed for the current photo on |
| 43 | // ambient. |
| 44 | std::string details; |
| 45 | |
wutao | a88fcc5 | 2020-04-28 02:10:35 | [diff] [blame] | 46 | // Image url. |
| 47 | std::string url; |
| 48 | |
wutao | e8d5890 | 2020-09-23 06:33:55 | [diff] [blame] | 49 | // Only support portrait image tiling in landscape orientation. |
| 50 | base::Optional<std::string> related_image_url; |
Jeffrey Young | 4944cfe | 2020-09-24 03:29:59 | [diff] [blame] | 51 | |
| 52 | AmbientModeTopicType topic_type = AmbientModeTopicType::kOther; |
wutao | a88fcc5 | 2020-04-28 02:10:35 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | // WeatherInfo contains the weather information we need for rendering a |
| 56 | // glanceable weather content on Ambient Mode. Corresponding to the |
| 57 | // |backdrop::WeatherInfo| proto. |
| 58 | struct ASH_PUBLIC_EXPORT WeatherInfo { |
| 59 | WeatherInfo(); |
| 60 | WeatherInfo(const WeatherInfo&); |
| 61 | WeatherInfo& operator=(const WeatherInfo&); |
| 62 | ~WeatherInfo(); |
| 63 | |
| 64 | // The url of the weather condition icon image. |
| 65 | base::Optional<std::string> condition_icon_url; |
| 66 | |
| 67 | // Weather temperature in Fahrenheit. |
| 68 | base::Optional<float> temp_f; |
Jeffrey Young | e441bfc | 2020-08-10 16:41:09 | [diff] [blame] | 69 | |
| 70 | // If the temperature should be displayed in celsius. Conversion must happen |
| 71 | // before the value in temp_f is displayed. |
| 72 | bool show_celsius = false; |
wutao | a88fcc5 | 2020-04-28 02:10:35 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | // Trimmed-down version of |backdrop::ScreenUpdate| proto from the backdrop |
| 76 | // server. It contains necessary information we need to render photo frame and |
| 77 | // glancible weather card in Ambient Mode. |
| 78 | struct ASH_PUBLIC_EXPORT ScreenUpdate { |
| 79 | ScreenUpdate(); |
| 80 | ScreenUpdate(const ScreenUpdate&); |
| 81 | ScreenUpdate& operator=(const ScreenUpdate&); |
| 82 | ~ScreenUpdate(); |
| 83 | |
| 84 | // A list of |Topic| (size >= 0). |
| 85 | std::vector<AmbientModeTopic> next_topics; |
| 86 | |
| 87 | // Weather information with weather condition icon and temperature in |
| 88 | // Fahrenheit. Will be a null-opt if: |
| 89 | // 1. The weather setting was disabled in the request, or |
| 90 | // 2. Fatal errors, such as response parsing failure, happened during the |
Jeroen Dhollander | f277b5f | 2020-08-04 00:57:20 | [diff] [blame] | 91 | // process, and a default |ScreenUpdate| instance was returned to indicate |
wutao | a88fcc5 | 2020-04-28 02:10:35 | [diff] [blame] | 92 | // the error. |
| 93 | base::Optional<WeatherInfo> weather_info; |
| 94 | }; |
| 95 | |
| 96 | // Interface to manage ambient mode backend. |
| 97 | class ASH_PUBLIC_EXPORT AmbientBackendController { |
| 98 | public: |
| 99 | using OnScreenUpdateInfoFetchedCallback = |
| 100 | base::OnceCallback<void(const ScreenUpdate&)>; |
wutao | 2592026 | 2020-06-20 21:11:10 | [diff] [blame] | 101 | using GetSettingsCallback = |
| 102 | base::OnceCallback<void(const base::Optional<AmbientSettings>& settings)>; |
wutao | a88fcc5 | 2020-04-28 02:10:35 | [diff] [blame] | 103 | using UpdateSettingsCallback = base::OnceCallback<void(bool success)>; |
wutao | 03be4cb | 2020-07-17 00:13:00 | [diff] [blame] | 104 | using OnSettingPreviewFetchedCallback = |
| 105 | base::OnceCallback<void(const std::vector<std::string>& preview_urls)>; |
wutao | f4afba18 | 2020-06-21 23:36:28 | [diff] [blame] | 106 | using OnPersonalAlbumsFetchedCallback = |
wutao | df261d5 | 2020-06-30 08:19:54 | [diff] [blame] | 107 | base::OnceCallback<void(PersonalAlbums)>; |
wutao | 046f252 | 2020-08-06 00:11:28 | [diff] [blame] | 108 | // TODO(wutao): Make |settings| move only. |
| 109 | using OnSettingsAndAlbumsFetchedCallback = |
| 110 | base::OnceCallback<void(const base::Optional<AmbientSettings>& settings, |
| 111 | PersonalAlbums personal_albums)>; |
Xiaohui Chen | 5985f53 | 2020-09-28 18:43:36 | [diff] [blame] | 112 | using FetchWeatherCallback = |
| 113 | base::OnceCallback<void(const base::Optional<WeatherInfo>& weather_info)>; |
wutao | a88fcc5 | 2020-04-28 02:10:35 | [diff] [blame] | 114 | |
| 115 | static AmbientBackendController* Get(); |
| 116 | |
| 117 | AmbientBackendController(); |
| 118 | AmbientBackendController(const AmbientBackendController&) = delete; |
| 119 | AmbientBackendController& operator=(const AmbientBackendController&) = delete; |
| 120 | virtual ~AmbientBackendController(); |
| 121 | |
wutao | 7820f093 | 2020-05-21 19:26:36 | [diff] [blame] | 122 | // Sends request to retrieve |num_topics| of |ScreenUpdate| from the backdrop |
| 123 | // server. |
wutao | a88fcc5 | 2020-04-28 02:10:35 | [diff] [blame] | 124 | // Upon completion, |callback| is run with the parsed |ScreenUpdate|. If any |
| 125 | // errors happened during the process, e.g. failed to fetch access token, a |
Jeroen Dhollander | f277b5f | 2020-08-04 00:57:20 | [diff] [blame] | 126 | // default instance will be returned. |
wutao | a88fcc5 | 2020-04-28 02:10:35 | [diff] [blame] | 127 | virtual void FetchScreenUpdateInfo( |
wutao | 7820f093 | 2020-05-21 19:26:36 | [diff] [blame] | 128 | int num_topics, |
wutao | a88fcc5 | 2020-04-28 02:10:35 | [diff] [blame] | 129 | OnScreenUpdateInfoFetchedCallback callback) = 0; |
| 130 | |
| 131 | // Get ambient mode Settings from server. |
wutao | a88fcc5 | 2020-04-28 02:10:35 | [diff] [blame] | 132 | virtual void GetSettings(GetSettingsCallback callback) = 0; |
| 133 | |
| 134 | // Update ambient mode Settings to server. |
wutao | 2592026 | 2020-06-20 21:11:10 | [diff] [blame] | 135 | virtual void UpdateSettings(const AmbientSettings& settings, |
wutao | a88fcc5 | 2020-04-28 02:10:35 | [diff] [blame] | 136 | UpdateSettingsCallback callback) = 0; |
wutao | 41ddbe83 | 2020-05-01 17:07:03 | [diff] [blame] | 137 | |
wutao | 03be4cb | 2020-07-17 00:13:00 | [diff] [blame] | 138 | // Fetch preview images for live album. |
| 139 | virtual void FetchSettingPreview(int preview_width, |
| 140 | int preview_height, |
| 141 | OnSettingPreviewFetchedCallback) = 0; |
| 142 | |
wutao | f4afba18 | 2020-06-21 23:36:28 | [diff] [blame] | 143 | virtual void FetchPersonalAlbums(int banner_width, |
| 144 | int banner_height, |
| 145 | int num_albums, |
| 146 | const std::string& resume_token, |
| 147 | OnPersonalAlbumsFetchedCallback) = 0; |
| 148 | |
wutao | 046f252 | 2020-08-06 00:11:28 | [diff] [blame] | 149 | // Fetch the Settings and albums as one API. |
| 150 | virtual void FetchSettingsAndAlbums(int banner_width, |
| 151 | int banner_height, |
| 152 | int num_albums, |
| 153 | OnSettingsAndAlbumsFetchedCallback) = 0; |
| 154 | |
wutao | 41ddbe83 | 2020-05-01 17:07:03 | [diff] [blame] | 155 | // Set the photo refresh interval in ambient mode. |
| 156 | virtual void SetPhotoRefreshInterval(base::TimeDelta interval) = 0; |
Xiaohui Chen | 5985f53 | 2020-09-28 18:43:36 | [diff] [blame] | 157 | |
| 158 | // Fetch the weather information. |
| 159 | virtual void FetchWeather(FetchWeatherCallback) = 0; |
Jeffrey Young | 57520f4 | 2020-10-07 02:13:35 | [diff] [blame^] | 160 | |
| 161 | // Get stock photo urls to cache in advance in case Ambient mode is started |
| 162 | // without internet access. |
| 163 | virtual const std::array<const char*, 2>& GetBackupPhotoUrls() const = 0; |
wutao | a88fcc5 | 2020-04-28 02:10:35 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | } // namespace ash |
| 167 | |
| 168 | #endif // ASH_PUBLIC_CPP_AMBIENT_AMBIENT_BACKEND_CONTROLLER_H_ |