Chromium Code Reviews| 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 #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 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 83 elapsed > kGeolocationRequestTimeout) { | 85 elapsed > kGeolocationRequestTimeout) { |
| 84 // Don't send invalid positions to ash. | 86 // Don't send invalid positions to ash. |
| 85 // On failure, we schedule another request after the current backoff delay. | 87 // On failure, we schedule another request after the current backoff delay. |
| 86 ScheduleNextRequest(backoff_delay_); | 88 ScheduleNextRequest(backoff_delay_); |
| 87 | 89 |
| 88 // If another failure occurs next, our backoff delay should double. | 90 // If another failure occurs next, our backoff delay should double. |
| 89 backoff_delay_ *= 2; | 91 backoff_delay_ *= 2; |
| 90 return; | 92 return; |
| 91 } | 93 } |
| 92 | 94 |
| 93 night_light_controller_->SetCurrentGeoposition( | 95 last_successful_request_ = base::Time::Now(); |
| 94 ash::mojom::SimpleGeoposition::New(position.latitude, | 96 |
| 95 position.longitude)); | 97 latitude_ = position.latitude; |
| 98 longitude_ = position.longitude; | |
| 99 SendCurrentGeoposition(); | |
| 96 | 100 |
| 97 // On success, reset the backoff delay to its minimum value, and schedule | 101 // On success, reset the backoff delay to its minimum value, and schedule |
| 98 // another request. | 102 // another request. |
| 99 backoff_delay_ = kMinimumDelayAfterFailure; | 103 backoff_delay_ = kMinimumDelayAfterFailure; |
| 100 ScheduleNextRequest(kNextRequestDelayAfterSuccess); | 104 ScheduleNextRequest(kNextRequestDelayAfterSuccess); |
| 101 } | 105 } |
| 102 | 106 |
| 107 void NightLightClient::SendCurrentGeoposition() { | |
| 108 night_light_controller_->SetCurrentGeoposition( | |
| 109 ash::mojom::SimpleGeoposition::New(latitude_, longitude_)); | |
| 110 } | |
| 111 | |
| 103 void NightLightClient::ScheduleNextRequest(base::TimeDelta delay) { | 112 void NightLightClient::ScheduleNextRequest(base::TimeDelta delay) { |
| 104 timer_.Start(FROM_HERE, delay, this, &NightLightClient::RequestGeoposition); | 113 timer_.Start(FROM_HERE, delay, this, &NightLightClient::RequestGeoposition); |
| 105 } | 114 } |
| 106 | 115 |
| 107 void NightLightClient::RequestGeoposition() { | 116 void NightLightClient::RequestGeoposition() { |
| 117 // No need to request a new position if we already have a valid one from a | |
|
James Cook
2017/06/30 19:12:26
nit: Either document how this can happen (from sch
afakhry
2017/06/30 22:28:28
Makes sense. Done.
| |
| 118 // request less than kNextRequestDelayAfterSuccess ago. | |
| 119 base::Time now = base::Time::Now(); | |
| 120 if ((now - last_successful_request_) < kNextRequestDelayAfterSuccess) { | |
| 121 SendCurrentGeoposition(); | |
| 122 | |
| 123 // Next request is either immediate or kNextRequestDelayAfterSuccess later | |
| 124 // than the last success time, whichever is greater. | |
| 125 ScheduleNextRequest(std::max( | |
| 126 base::TimeDelta::FromSeconds(0), | |
| 127 last_successful_request_ + kNextRequestDelayAfterSuccess - now)); | |
| 128 return; | |
| 129 } | |
|
James Cook
2017/06/30 19:12:26
Is it possible to write a test for this behavior?
afakhry
2017/06/30 22:28:28
Now that we moved this code outside RequestGeoposi
| |
| 130 | |
| 108 provider_.RequestGeolocation( | 131 provider_.RequestGeolocation( |
| 109 kGeolocationRequestTimeout, false /* send_wifi_access_points */, | 132 kGeolocationRequestTimeout, false /* send_wifi_access_points */, |
| 110 false /* send_cell_towers */, | 133 false /* send_cell_towers */, |
| 111 base::Bind(&NightLightClient::OnGeoposition, base::Unretained(this))); | 134 base::Bind(&NightLightClient::OnGeoposition, base::Unretained(this))); |
| 112 } | 135 } |
| OLD | NEW |