Avi Drissman | 3a215d1e | 2022-09-07 19:43:09 | [diff] [blame] | 1 | // Copyright 2021 The Chromium Authors |
Cattalyya Nuengsigkapian | 2ef9433 | 2021-11-20 05:42:51 | [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 | |
| 5 | #ifndef ASH_SYSTEM_GEOLOCATION_GEOLOCATION_CONTROLLER_H_ |
| 6 | #define ASH_SYSTEM_GEOLOCATION_GEOLOCATION_CONTROLLER_H_ |
| 7 | |
| 8 | #include <memory> |
| 9 | #include <string> |
| 10 | |
| 11 | #include "ash/ash_export.h" |
Eric Sum | f97e7b89 | 2023-04-17 22:41:44 | [diff] [blame] | 12 | #include "ash/public/cpp/session/session_observer.h" |
| 13 | #include "base/memory/raw_ptr.h" |
Hans Wennborg | 9a23f41 | 2022-02-11 19:36:18 | [diff] [blame] | 14 | #include "base/observer_list.h" |
| 15 | #include "base/observer_list_types.h" |
Cattalyya Nuengsigkapian | 2ef9433 | 2021-11-20 05:42:51 | [diff] [blame] | 16 | #include "base/time/time.h" |
| 17 | #include "base/timer/timer.h" |
Henrique Ferreiro | 129f0b7 | 2022-09-01 20:57:05 | [diff] [blame] | 18 | #include "chromeos/ash/components/geolocation/simple_geolocation_provider.h" |
Henrique Ferreiro | 1eaedc4 | 2022-10-05 15:33:47 | [diff] [blame] | 19 | #include "chromeos/ash/components/settings/timezone_settings.h" |
Cattalyya Nuengsigkapian | 0a5b309 | 2022-05-11 04:50:51 | [diff] [blame] | 20 | #include "chromeos/dbus/power/power_manager_client.h" |
Cattalyya Nuengsigkapian | 2ef9433 | 2021-11-20 05:42:51 | [diff] [blame] | 21 | #include "services/network/public/cpp/shared_url_loader_factory.h" |
| 22 | |
Eric Sum | f97e7b89 | 2023-04-17 22:41:44 | [diff] [blame] | 23 | class PrefRegistrySimple; |
| 24 | class PrefService; |
| 25 | |
Cattalyya Nuengsigkapian | 2ef9433 | 2021-11-20 05:42:51 | [diff] [blame] | 26 | namespace base { |
| 27 | class Clock; |
| 28 | } // namespace base |
| 29 | |
| 30 | namespace ash { |
| 31 | |
| 32 | // Represents a geolocation position fix. It's "simple" because it doesn't |
| 33 | // expose all the parameters of the position interface as defined by the |
| 34 | // Geolocation API Specification: |
| 35 | // https://dev.w3.org/geo/api/spec-source.html#position_interface |
| 36 | // The GeolocationController is only interested in valid latitude and |
| 37 | // longitude. It also doesn't require any specific accuracy. The more accurate |
| 38 | // the positions, the more accurate sunset and sunrise times calculations. |
| 39 | // However, an IP-based geoposition is considered good enough. |
| 40 | struct SimpleGeoposition { |
| 41 | bool operator==(const SimpleGeoposition& other) const { |
| 42 | return latitude == other.latitude && longitude == other.longitude; |
| 43 | } |
| 44 | double latitude; |
| 45 | double longitude; |
| 46 | }; |
| 47 | |
| 48 | // Periodically requests the IP-based geolocation and provides it to the |
| 49 | // observers, `GeolocationController::Observer`. This class also observes |
| 50 | // timezone changes to request a new geoposition. |
| 51 | // TODO(crbug.com/1272178): `GeolocationController` should observe the sleep |
| 52 | // and update next request time. |
| 53 | class ASH_EXPORT GeolocationController |
Henrique Ferreiro | 1eaedc4 | 2022-10-05 15:33:47 | [diff] [blame] | 54 | : public system::TimezoneSettings::Observer, |
zauri | 0e126475 | 2023-04-04 16:52:36 | [diff] [blame] | 55 | public chromeos::PowerManagerClient::Observer, |
Eric Sum | f97e7b89 | 2023-04-17 22:41:44 | [diff] [blame] | 56 | public SessionObserver, |
zauri | 0e126475 | 2023-04-04 16:52:36 | [diff] [blame] | 57 | public SimpleGeolocationProvider::Delegate { |
Cattalyya Nuengsigkapian | 2ef9433 | 2021-11-20 05:42:51 | [diff] [blame] | 58 | public: |
| 59 | class Observer : public base::CheckedObserver { |
| 60 | public: |
Cattalyya Nuengsigkapian | 98d8131 | 2021-12-22 15:51:05 | [diff] [blame] | 61 | // Emitted when the Geoposition is updated with |
| 62 | // |possible_change_in_timezone| to indicate whether timezone might have |
| 63 | // changed as a result of the geoposition change. |
| 64 | virtual void OnGeopositionChanged(bool possible_change_in_timezone) {} |
Cattalyya Nuengsigkapian | 2ef9433 | 2021-11-20 05:42:51 | [diff] [blame] | 65 | |
| 66 | protected: |
| 67 | ~Observer() override = default; |
| 68 | }; |
| 69 | |
| 70 | explicit GeolocationController( |
| 71 | scoped_refptr<network::SharedURLLoaderFactory> factory); |
| 72 | GeolocationController(const GeolocationController&) = delete; |
| 73 | GeolocationController& operator=(const GeolocationController&) = delete; |
| 74 | ~GeolocationController() override; |
| 75 | |
Cattalyya Nuengsigkapian | 98d8131 | 2021-12-22 15:51:05 | [diff] [blame] | 76 | static GeolocationController* Get(); |
Eric Sum | f97e7b89 | 2023-04-17 22:41:44 | [diff] [blame] | 77 | static void RegisterProfilePrefs(PrefRegistrySimple* registry); |
Cattalyya Nuengsigkapian | 98d8131 | 2021-12-22 15:51:05 | [diff] [blame] | 78 | |
Cattalyya Nuengsigkapian | 2ef9433 | 2021-11-20 05:42:51 | [diff] [blame] | 79 | const base::OneShotTimer& timer() const { return *timer_; } |
| 80 | |
Cattalyya Nuengsigkapian | 2ef9433 | 2021-11-20 05:42:51 | [diff] [blame] | 81 | const std::u16string& current_timezone_id() const { |
| 82 | return current_timezone_id_; |
| 83 | } |
| 84 | |
| 85 | void AddObserver(Observer* observer); |
| 86 | void RemoveObserver(Observer* observer); |
| 87 | |
Henrique Ferreiro | 1eaedc4 | 2022-10-05 15:33:47 | [diff] [blame] | 88 | // system::TimezoneSettings::Observer: |
Cattalyya Nuengsigkapian | 2ef9433 | 2021-11-20 05:42:51 | [diff] [blame] | 89 | void TimezoneChanged(const icu::TimeZone& timezone) override; |
| 90 | |
Cattalyya Nuengsigkapian | 0a5b309 | 2022-05-11 04:50:51 | [diff] [blame] | 91 | // chromeos::PowerManagerClient::Observer: |
| 92 | void SuspendDone(base::TimeDelta sleep_duration) override; |
| 93 | |
zauri | 0e126475 | 2023-04-04 16:52:36 | [diff] [blame] | 94 | // SimpleGeolocationProvider::Delegate: |
| 95 | bool IsPreciseGeolocationAllowed() const override; |
| 96 | |
Eric Sum | f97e7b89 | 2023-04-17 22:41:44 | [diff] [blame] | 97 | // SessionObserver: |
| 98 | void OnActiveUserPrefServiceChanged(PrefService* pref_service) override; |
| 99 | |
| 100 | // Returns sunset and sunrise time calculated from the most recently observed |
| 101 | // geoposition. If a geoposition has not been observed, defaults to sunset |
| 102 | // 6 PM and sunrise 6 AM. |
Cattalyya Nuengsigkapian | 2ef9433 | 2021-11-20 05:42:51 | [diff] [blame] | 103 | base::Time GetSunsetTime() const { return GetSunRiseSet(/*sunrise=*/false); } |
| 104 | base::Time GetSunriseTime() const { return GetSunRiseSet(/*sunrise=*/true); } |
| 105 | |
| 106 | static base::TimeDelta GetNextRequestDelayAfterSuccessForTesting(); |
| 107 | |
Cattalyya Nuengsigkapian | 2e703a8 | 2022-03-26 00:29:59 | [diff] [blame] | 108 | network::SharedURLLoaderFactory* GetFactoryForTesting() { return factory_; } |
| 109 | |
Cattalyya Nuengsigkapian | 860e05d | 2022-04-26 19:12:00 | [diff] [blame] | 110 | base::OneShotTimer* GetTimerForTesting() { return timer_.get(); } |
| 111 | |
| 112 | bool HasObserverForTesting(const Observer* obs) const { |
| 113 | return observers_.HasObserver(obs); |
| 114 | } |
| 115 | |
Cattalyya Nuengsigkapian | 2ef9433 | 2021-11-20 05:42:51 | [diff] [blame] | 116 | void SetTimerForTesting(std::unique_ptr<base::OneShotTimer> timer); |
| 117 | |
| 118 | void SetClockForTesting(base::Clock* clock); |
| 119 | |
| 120 | void SetCurrentTimezoneIdForTesting(const std::u16string& timezone_id); |
| 121 | |
| 122 | protected: |
| 123 | // The callback of geolocation request via `provider_`. Once receiving a |
| 124 | // new position, it `NotifyWithCurrentGeoposition()` to broadcast the position |
| 125 | // to observers and `ScheduleNextRequest()` on the next day. If the retrieval |
| 126 | // fails, it `ScheduleNextRequest()` after a `backoff_delay_`, which is |
| 127 | // doubled for each failure. |
| 128 | void OnGeoposition(const Geoposition& position, |
| 129 | bool server_error, |
| 130 | const base::TimeDelta elapsed); |
| 131 | |
Cattalyya Nuengsigkapian | bba6f47 | 2022-02-07 22:56:59 | [diff] [blame] | 132 | // Virtual so that it can be overridden by a fake implementation in unit tests |
| 133 | // that doesn't request actual geopositions. |
| 134 | virtual void RequestGeoposition(); |
| 135 | |
Cattalyya Nuengsigkapian | 2ef9433 | 2021-11-20 05:42:51 | [diff] [blame] | 136 | private: |
| 137 | // Gets now time from the `clock_` or `base::Time::Now()` if `clock_` does |
| 138 | // not exist. |
| 139 | base::Time GetNow() const; |
| 140 | |
| 141 | // Calls `RequestGeoposition()` after `delay`. |
| 142 | void ScheduleNextRequest(base::TimeDelta delay); |
| 143 | |
Cattalyya Nuengsigkapian | 98d8131 | 2021-12-22 15:51:05 | [diff] [blame] | 144 | // Broadcasts the change in geoposition to all observers with |
| 145 | // |possible_change_in_timezone| to indicate whether timezone might have |
| 146 | // changed as a result of the geoposition change. |
| 147 | void NotifyGeopositionChange(bool possible_change_in_timezone); |
Cattalyya Nuengsigkapian | 2ef9433 | 2021-11-20 05:42:51 | [diff] [blame] | 148 | |
Cattalyya Nuengsigkapian | 2ef9433 | 2021-11-20 05:42:51 | [diff] [blame] | 149 | // Note that the below computation is intentionally performed every time |
| 150 | // GetSunsetTime() or GetSunriseTime() is called rather than once whenever we |
| 151 | // receive a geoposition (which happens at least once a day). This reduces |
| 152 | // the chances of getting inaccurate values, especially around DST changes. |
| 153 | base::Time GetSunRiseSet(bool sunrise) const; |
| 154 | |
Eric Sum | f97e7b89 | 2023-04-17 22:41:44 | [diff] [blame] | 155 | // Called only when the active user changes in order to see if we need to use |
| 156 | // a previously cached geoposition value from the active user's prefs. |
| 157 | void LoadCachedGeopositionIfNeeded(); |
| 158 | |
| 159 | // Called whenever we receive a new geoposition update to cache it in all |
| 160 | // logged-in users' prefs so that it can be used later in the event of not |
| 161 | // being able to retrieve a valid geoposition. |
| 162 | void StoreCachedGeoposition() const; |
| 163 | |
Arthur Sonzogni | 834e018f | 2023-04-22 10:20:02 | [diff] [blame] | 164 | const raw_ptr<network::SharedURLLoaderFactory, ExperimentalAsh> factory_; |
Cattalyya Nuengsigkapian | 2e703a8 | 2022-03-26 00:29:59 | [diff] [blame] | 165 | |
Eric Sum | f97e7b89 | 2023-04-17 22:41:44 | [diff] [blame] | 166 | // May be null if a user has not logged in yet. |
| 167 | base::raw_ptr<PrefService> active_user_pref_service_ = nullptr; |
| 168 | |
Cattalyya Nuengsigkapian | 2ef9433 | 2021-11-20 05:42:51 | [diff] [blame] | 169 | // The IP-based geolocation provider. |
| 170 | SimpleGeolocationProvider provider_; |
| 171 | |
| 172 | // Delay after which a new request is retried after a failed one. |
| 173 | base::TimeDelta backoff_delay_; |
| 174 | |
| 175 | std::unique_ptr<base::OneShotTimer> timer_; |
| 176 | |
| 177 | // Optional Used in tests to override the time of "Now". |
Arthur Sonzogni | 834e018f | 2023-04-22 10:20:02 | [diff] [blame] | 178 | raw_ptr<base::Clock, ExperimentalAsh> clock_ = nullptr; // Not owned. |
Cattalyya Nuengsigkapian | 2ef9433 | 2021-11-20 05:42:51 | [diff] [blame] | 179 | |
Cattalyya Nuengsigkapian | 2ef9433 | 2021-11-20 05:42:51 | [diff] [blame] | 180 | // The ID of the current timezone in the format similar to "America/Chicago". |
| 181 | std::u16string current_timezone_id_; |
| 182 | |
| 183 | base::ObserverList<Observer> observers_; |
| 184 | |
Eric Sum | f97e7b89 | 2023-04-17 22:41:44 | [diff] [blame] | 185 | // True if the current `geoposition_` is from a previously cached value in the |
| 186 | // user prefs of any of the users in the current session. It is reset to false |
| 187 | // once we receive a newly-updated geoposition. This is used to treat the |
| 188 | // current geoposition as temporary until we receive a valid geoposition |
| 189 | // update, and also not to let a cached geoposition value to leak to another |
| 190 | // user for privacy reasons. |
| 191 | bool is_current_geoposition_from_cache_ = false; |
| 192 | |
Cattalyya Nuengsigkapian | 2ef9433 | 2021-11-20 05:42:51 | [diff] [blame] | 193 | std::unique_ptr<SimpleGeoposition> geoposition_; |
Eric Sum | f97e7b89 | 2023-04-17 22:41:44 | [diff] [blame] | 194 | |
| 195 | ScopedSessionObserver scoped_session_observer_; |
Cattalyya Nuengsigkapian | 2ef9433 | 2021-11-20 05:42:51 | [diff] [blame] | 196 | }; |
| 197 | |
| 198 | } // namespace ash |
| 199 | |
| 200 | #endif // ASH_SYSTEM_GEOLOCATION_GEOLOCATION_CONTROLLER_H_ |