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

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

Issue 2966873002: [Night Light] CL12: String changes and fix frequent requests. (Closed)
Patch Set: James comments 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 #include "chrome/browser/chromeos/night_light/night_light_client.h" 5 #include "chrome/browser/chromeos/night_light/night_light_client.h"
6 6
7 #include <algorithm>
8
7 #include "ash/public/interfaces/constants.mojom.h" 9 #include "ash/public/interfaces/constants.mojom.h"
8 #include "content/public/common/service_manager_connection.h" 10 #include "content/public/common/service_manager_connection.h"
9 #include "services/service_manager/public/cpp/connector.h" 11 #include "services/service_manager/public/cpp/connector.h"
10 12
11 namespace { 13 namespace {
12 14
13 // Delay to wait for a response to our geolocation request, if we get a response 15 // Delay to wait for a response to our geolocation request, if we get a response
14 // after which, we will consider the request a failure. 16 // after which, we will consider the request a failure.
15 constexpr base::TimeDelta kGeolocationRequestTimeout = 17 constexpr base::TimeDelta kGeolocationRequestTimeout =
16 base::TimeDelta::FromSeconds(60); 18 base::TimeDelta::FromSeconds(60);
(...skipping 27 matching lines...) Expand all
44 } 46 }
45 ash::mojom::NightLightClientPtr client; 47 ash::mojom::NightLightClientPtr client;
46 binding_.Bind(mojo::MakeRequest(&client)); 48 binding_.Bind(mojo::MakeRequest(&client));
47 night_light_controller_->SetClient(std::move(client)); 49 night_light_controller_->SetClient(std::move(client));
48 } 50 }
49 51
50 void NightLightClient::OnScheduleTypeChanged( 52 void NightLightClient::OnScheduleTypeChanged(
51 ash::mojom::NightLightController::ScheduleType new_type) { 53 ash::mojom::NightLightController::ScheduleType new_type) {
52 if (new_type == 54 if (new_type ==
53 ash::mojom::NightLightController::ScheduleType::kSunsetToSunrise) { 55 ash::mojom::NightLightController::ScheduleType::kSunsetToSunrise) {
54 // Schedule an immediate request.
55 using_geoposition_ = true; 56 using_geoposition_ = true;
56 ScheduleNextRequest(base::TimeDelta::FromSeconds(0)); 57 // No need to request a new position if we already have a valid one from a
58 // request less than kNextRequestDelayAfterSuccess ago.
59 base::Time now = base::Time::Now();
60 if ((now - last_successful_geo_request_time_) <
61 kNextRequestDelayAfterSuccess) {
62 // Use the current valid position to update NightLightController.
63 SendCurrentGeoposition();
64 }
65
66 // Next request is either immediate or kNextRequestDelayAfterSuccess later
67 // than the last success time, whichever is greater.
68 ScheduleNextRequest(std::max(base::TimeDelta::FromSeconds(0),
69 last_successful_geo_request_time_ +
70 kNextRequestDelayAfterSuccess - now));
57 } else { 71 } else {
58 using_geoposition_ = false; 72 using_geoposition_ = false;
59 timer_.Stop(); 73 timer_.Stop();
60 } 74 }
61 } 75 }
62 76
77 // static
78 base::TimeDelta NightLightClient::GetNextRequestDelayAfterSuccessForTesting() {
79 return kNextRequestDelayAfterSuccess;
80 }
81
63 void NightLightClient::SetNightLightControllerPtrForTesting( 82 void NightLightClient::SetNightLightControllerPtrForTesting(
64 ash::mojom::NightLightControllerPtr controller) { 83 ash::mojom::NightLightControllerPtr controller) {
65 night_light_controller_ = std::move(controller); 84 night_light_controller_ = std::move(controller);
66 } 85 }
67 86
68 void NightLightClient::FlushNightLightControllerForTesting() { 87 void NightLightClient::FlushNightLightControllerForTesting() {
69 night_light_controller_.FlushForTesting(); 88 night_light_controller_.FlushForTesting();
70 } 89 }
71 90
72 void NightLightClient::OnGeoposition(const chromeos::Geoposition& position, 91 void NightLightClient::OnGeoposition(const chromeos::Geoposition& position,
(...skipping 10 matching lines...) Expand all
83 elapsed > kGeolocationRequestTimeout) { 102 elapsed > kGeolocationRequestTimeout) {
84 // Don't send invalid positions to ash. 103 // Don't send invalid positions to ash.
85 // On failure, we schedule another request after the current backoff delay. 104 // On failure, we schedule another request after the current backoff delay.
86 ScheduleNextRequest(backoff_delay_); 105 ScheduleNextRequest(backoff_delay_);
87 106
88 // If another failure occurs next, our backoff delay should double. 107 // If another failure occurs next, our backoff delay should double.
89 backoff_delay_ *= 2; 108 backoff_delay_ *= 2;
90 return; 109 return;
91 } 110 }
92 111
93 night_light_controller_->SetCurrentGeoposition( 112 last_successful_geo_request_time_ = base::Time::Now();
94 ash::mojom::SimpleGeoposition::New(position.latitude, 113
95 position.longitude)); 114 latitude_ = position.latitude;
115 longitude_ = position.longitude;
116 SendCurrentGeoposition();
96 117
97 // On success, reset the backoff delay to its minimum value, and schedule 118 // On success, reset the backoff delay to its minimum value, and schedule
98 // another request. 119 // another request.
99 backoff_delay_ = kMinimumDelayAfterFailure; 120 backoff_delay_ = kMinimumDelayAfterFailure;
100 ScheduleNextRequest(kNextRequestDelayAfterSuccess); 121 ScheduleNextRequest(kNextRequestDelayAfterSuccess);
101 } 122 }
102 123
124 void NightLightClient::SendCurrentGeoposition() {
125 night_light_controller_->SetCurrentGeoposition(
126 ash::mojom::SimpleGeoposition::New(latitude_, longitude_));
127 }
128
103 void NightLightClient::ScheduleNextRequest(base::TimeDelta delay) { 129 void NightLightClient::ScheduleNextRequest(base::TimeDelta delay) {
104 timer_.Start(FROM_HERE, delay, this, &NightLightClient::RequestGeoposition); 130 timer_.Start(FROM_HERE, delay, this, &NightLightClient::RequestGeoposition);
105 } 131 }
106 132
107 void NightLightClient::RequestGeoposition() { 133 void NightLightClient::RequestGeoposition() {
108 provider_.RequestGeolocation( 134 provider_.RequestGeolocation(
109 kGeolocationRequestTimeout, false /* send_wifi_access_points */, 135 kGeolocationRequestTimeout, false /* send_wifi_access_points */,
110 false /* send_cell_towers */, 136 false /* send_cell_towers */,
111 base::Bind(&NightLightClient::OnGeoposition, base::Unretained(this))); 137 base::Bind(&NightLightClient::OnGeoposition, base::Unretained(this)));
112 } 138 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698