| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_CHROMEOS_NIGHT_LIGHT_NIGHT_LIGHT_CLIENT_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_NIGHT_LIGHT_NIGHT_LIGHT_CLIENT_H_ |
| 6 #define CHROME_BROWSER_CHROMEOS_NIGHT_LIGHT_NIGHT_LIGHT_CLIENT_H_ | 6 #define CHROME_BROWSER_CHROMEOS_NIGHT_LIGHT_NIGHT_LIGHT_CLIENT_H_ |
| 7 | 7 |
| 8 #include <memory> |
| 9 |
| 8 #include "ash/public/interfaces/night_light_controller.mojom.h" | 10 #include "ash/public/interfaces/night_light_controller.mojom.h" |
| 9 #include "base/macros.h" | 11 #include "base/macros.h" |
| 10 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 11 #include "base/timer/timer.h" | 13 #include "base/timer/timer.h" |
| 12 #include "chromeos/geolocation/simple_geolocation_provider.h" | 14 #include "chromeos/geolocation/simple_geolocation_provider.h" |
| 13 #include "mojo/public/cpp/bindings/binding.h" | 15 #include "mojo/public/cpp/bindings/binding.h" |
| 14 | 16 |
| 17 namespace base { |
| 18 class Clock; |
| 19 } // namespace base |
| 20 |
| 15 namespace net { | 21 namespace net { |
| 16 class URLRequestContextGetter; | 22 class URLRequestContextGetter; |
| 17 } | 23 } |
| 18 | 24 |
| 19 // Periodically requests the IP-based geolocation and provides it to the | 25 // Periodically requests the IP-based geolocation and provides it to the |
| 20 // NightLightController running in ash. | 26 // NightLightController running in ash. |
| 21 class NightLightClient | 27 class NightLightClient |
| 22 : public NON_EXPORTED_BASE(ash::mojom::NightLightClient) { | 28 : public NON_EXPORTED_BASE(ash::mojom::NightLightClient) { |
| 23 public: | 29 public: |
| 24 explicit NightLightClient(net::URLRequestContextGetter* url_context_getter); | 30 NightLightClient(net::URLRequestContextGetter* url_context_getter); |
| 25 ~NightLightClient() override; | 31 ~NightLightClient() override; |
| 26 | 32 |
| 27 // Starts watching changes in the Night Light schedule type in order to begin | 33 // Starts watching changes in the Night Light schedule type in order to begin |
| 28 // periodically pushing user's IP-based geoposition to NightLightController as | 34 // periodically pushing user's IP-based geoposition to NightLightController as |
| 29 // long as the type is set to "sunset to sunrise". | 35 // long as the type is set to "sunset to sunrise". |
| 30 void Start(); | 36 void Start(); |
| 31 | 37 |
| 32 // ash::mojom::NightLightClient: | 38 // ash::mojom::NightLightClient: |
| 33 void OnScheduleTypeChanged( | 39 void OnScheduleTypeChanged( |
| 34 ash::mojom::NightLightController::ScheduleType new_type) override; | 40 ash::mojom::NightLightController::ScheduleType new_type) override; |
| 35 | 41 |
| 42 const base::OneShotTimer& timer() const { return *timer_; } |
| 43 |
| 44 base::Time last_successful_geo_request_time() const { |
| 45 return last_successful_geo_request_time_; |
| 46 } |
| 47 |
| 36 bool using_geoposition() const { return using_geoposition_; } | 48 bool using_geoposition() const { return using_geoposition_; } |
| 37 | 49 |
| 50 static base::TimeDelta GetNextRequestDelayAfterSuccessForTesting(); |
| 51 |
| 38 void SetNightLightControllerPtrForTesting( | 52 void SetNightLightControllerPtrForTesting( |
| 39 ash::mojom::NightLightControllerPtr controller); | 53 ash::mojom::NightLightControllerPtr controller); |
| 40 | 54 |
| 41 void FlushNightLightControllerForTesting(); | 55 void FlushNightLightControllerForTesting(); |
| 42 | 56 |
| 57 void SetTimerForTesting(std::unique_ptr<base::OneShotTimer> timer); |
| 58 |
| 59 void SetClockForTesting(base::Clock* clock); |
| 60 |
| 43 protected: | 61 protected: |
| 44 void OnGeoposition(const chromeos::Geoposition& position, | 62 void OnGeoposition(const chromeos::Geoposition& position, |
| 45 bool server_error, | 63 bool server_error, |
| 46 const base::TimeDelta elapsed); | 64 const base::TimeDelta elapsed); |
| 47 | 65 |
| 48 private: | 66 private: |
| 67 base::Time GetNow() const; |
| 68 |
| 69 // Sends the most recent valid geoposition to NightLightController in ash. |
| 70 void SendCurrentGeoposition(); |
| 71 |
| 49 void ScheduleNextRequest(base::TimeDelta delay); | 72 void ScheduleNextRequest(base::TimeDelta delay); |
| 50 | 73 |
| 51 // Virtual so that it can be overriden by a fake implementation in unit tests | 74 // Virtual so that it can be overriden by a fake implementation in unit tests |
| 52 // that doesn't request actual geopositions. | 75 // that doesn't request actual geopositions. |
| 53 virtual void RequestGeoposition(); | 76 virtual void RequestGeoposition(); |
| 54 | 77 |
| 55 // The IP-based geolocation provider. | 78 // The IP-based geolocation provider. |
| 56 chromeos::SimpleGeolocationProvider provider_; | 79 chromeos::SimpleGeolocationProvider provider_; |
| 57 | 80 |
| 58 ash::mojom::NightLightControllerPtr night_light_controller_; | 81 ash::mojom::NightLightControllerPtr night_light_controller_; |
| 59 mojo::Binding<ash::mojom::NightLightClient> binding_; | 82 mojo::Binding<ash::mojom::NightLightClient> binding_; |
| 60 | 83 |
| 61 // Delay after which a new request is retried after a failed one. | 84 // Delay after which a new request is retried after a failed one. |
| 62 base::TimeDelta backoff_delay_; | 85 base::TimeDelta backoff_delay_; |
| 63 | 86 |
| 64 base::OneShotTimer timer_; | 87 std::unique_ptr<base::OneShotTimer> timer_; |
| 88 |
| 89 // Optional Used in tests to override the time of "Now". |
| 90 base::Clock* clock_ = nullptr; // Not owned. |
| 91 |
| 92 // Last successful geoposition coordinates and its timestamp. |
| 93 base::Time last_successful_geo_request_time_; |
| 94 double latitude_ = 0.0; |
| 95 double longitude_ = 0.0; |
| 65 | 96 |
| 66 // True as long as the schedule type is set to "sunset to sunrise", which | 97 // True as long as the schedule type is set to "sunset to sunrise", which |
| 67 // means this client will be retrieving the IP-based geoposition once per day. | 98 // means this client will be retrieving the IP-based geoposition once per day. |
| 68 bool using_geoposition_ = false; | 99 bool using_geoposition_ = false; |
| 69 | 100 |
| 70 DISALLOW_COPY_AND_ASSIGN(NightLightClient); | 101 DISALLOW_COPY_AND_ASSIGN(NightLightClient); |
| 71 }; | 102 }; |
| 72 | 103 |
| 73 #endif // CHROME_BROWSER_CHROMEOS_NIGHT_LIGHT_NIGHT_LIGHT_CLIENT_H_ | 104 #endif // CHROME_BROWSER_CHROMEOS_NIGHT_LIGHT_NIGHT_LIGHT_CLIENT_H_ |
| OLD | NEW |