Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: chrome/browser/chromeos/night_light/night_light_client.h

Issue 2966873002: [Night Light] CL12: String changes and fix frequent requests. (Closed)
Patch Set: Nits Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "ash/public/interfaces/night_light_controller.mojom.h" 8 #include "ash/public/interfaces/night_light_controller.mojom.h"
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/memory/weak_ptr.h" 10 #include "base/memory/weak_ptr.h"
11 #include "base/timer/timer.h" 11 #include "base/timer/timer.h"
12 #include "chromeos/geolocation/simple_geolocation_provider.h" 12 #include "chromeos/geolocation/simple_geolocation_provider.h"
13 #include "mojo/public/cpp/bindings/binding.h" 13 #include "mojo/public/cpp/bindings/binding.h"
14 14
15 namespace base {
16 class Clock;
17 class TickClock;
18 } // namespace base
19
15 namespace net { 20 namespace net {
16 class URLRequestContextGetter; 21 class URLRequestContextGetter;
17 } 22 }
18 23
19 // Periodically requests the IP-based geolocation and provides it to the 24 // Periodically requests the IP-based geolocation and provides it to the
20 // NightLightController running in ash. 25 // NightLightController running in ash.
21 class NightLightClient 26 class NightLightClient
22 : public NON_EXPORTED_BASE(ash::mojom::NightLightClient) { 27 : public NON_EXPORTED_BASE(ash::mojom::NightLightClient) {
23 public: 28 public:
29 NightLightClient(net::URLRequestContextGetter* url_context_getter,
30 base::TickClock* tick_clock,
31 base::Clock* clock);
24 explicit NightLightClient(net::URLRequestContextGetter* url_context_getter); 32 explicit NightLightClient(net::URLRequestContextGetter* url_context_getter);
stevenjb 2017/07/01 00:02:35 Avoid multiple constructors for high level classes
James Cook 2017/07/01 00:06:57 Or make the parameters mandatory and use a SystemC
afakhry 2017/07/01 00:26:15 The problem is base::Timer, for which we cannot ch
stevenjb 2017/07/01 00:31:26 Could you use a unique_ptr<> for timer_?
afakhry 2017/07/01 00:57:44 Done. So much for those tests! :(
25 ~NightLightClient() override; 33 ~NightLightClient() override;
26 34
27 // Starts watching changes in the Night Light schedule type in order to begin 35 // Starts watching changes in the Night Light schedule type in order to begin
28 // periodically pushing user's IP-based geoposition to NightLightController as 36 // periodically pushing user's IP-based geoposition to NightLightController as
29 // long as the type is set to "sunset to sunrise". 37 // long as the type is set to "sunset to sunrise".
30 void Start(); 38 void Start();
31 39
32 // ash::mojom::NightLightClient: 40 // ash::mojom::NightLightClient:
33 void OnScheduleTypeChanged( 41 void OnScheduleTypeChanged(
34 ash::mojom::NightLightController::ScheduleType new_type) override; 42 ash::mojom::NightLightController::ScheduleType new_type) override;
35 43
44 const base::OneShotTimer& timer() const { return timer_; }
45
46 base::Time last_successful_geo_request_time() const {
47 return last_successful_geo_request_time_;
48 }
49
36 bool using_geoposition() const { return using_geoposition_; } 50 bool using_geoposition() const { return using_geoposition_; }
37 51
52 static base::TimeDelta GetNextRequestDelayAfterSuccessForTesting();
53
38 void SetNightLightControllerPtrForTesting( 54 void SetNightLightControllerPtrForTesting(
39 ash::mojom::NightLightControllerPtr controller); 55 ash::mojom::NightLightControllerPtr controller);
40 56
41 void FlushNightLightControllerForTesting(); 57 void FlushNightLightControllerForTesting();
42 58
43 protected: 59 protected:
44 void OnGeoposition(const chromeos::Geoposition& position, 60 void OnGeoposition(const chromeos::Geoposition& position,
45 bool server_error, 61 bool server_error,
46 const base::TimeDelta elapsed); 62 const base::TimeDelta elapsed);
47 63
48 private: 64 private:
65 base::Time GetNow() const;
66
67 // Sends the most recent valid geoposition to NightLightController in ash.
68 void SendCurrentGeoposition();
69
49 void ScheduleNextRequest(base::TimeDelta delay); 70 void ScheduleNextRequest(base::TimeDelta delay);
50 71
51 // Virtual so that it can be overriden by a fake implementation in unit tests 72 // Virtual so that it can be overriden by a fake implementation in unit tests
52 // that doesn't request actual geopositions. 73 // that doesn't request actual geopositions.
53 virtual void RequestGeoposition(); 74 virtual void RequestGeoposition();
54 75
55 // The IP-based geolocation provider. 76 // The IP-based geolocation provider.
56 chromeos::SimpleGeolocationProvider provider_; 77 chromeos::SimpleGeolocationProvider provider_;
57 78
58 ash::mojom::NightLightControllerPtr night_light_controller_; 79 ash::mojom::NightLightControllerPtr night_light_controller_;
59 mojo::Binding<ash::mojom::NightLightClient> binding_; 80 mojo::Binding<ash::mojom::NightLightClient> binding_;
60 81
61 // Delay after which a new request is retried after a failed one. 82 // Delay after which a new request is retried after a failed one.
62 base::TimeDelta backoff_delay_; 83 base::TimeDelta backoff_delay_;
63 84
64 base::OneShotTimer timer_; 85 base::OneShotTimer timer_;
65 86
87 // Optional Used in tests to override the time of "Now".
88 base::Clock* clock_; // Not owned.
89
90 // Last successful geoposition coordinates and its timestamp.
91 base::Time last_successful_geo_request_time_;
92 double latitude_ = 0.0;
93 double longitude_ = 0.0;
94
66 // True as long as the schedule type is set to "sunset to sunrise", which 95 // 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. 96 // means this client will be retrieving the IP-based geoposition once per day.
68 bool using_geoposition_ = false; 97 bool using_geoposition_ = false;
69 98
70 DISALLOW_COPY_AND_ASSIGN(NightLightClient); 99 DISALLOW_COPY_AND_ASSIGN(NightLightClient);
71 }; 100 };
72 101
73 #endif // CHROME_BROWSER_CHROMEOS_NIGHT_LIGHT_NIGHT_LIGHT_CLIENT_H_ 102 #endif // CHROME_BROWSER_CHROMEOS_NIGHT_LIGHT_NIGHT_LIGHT_CLIENT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698