| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/data_usage/android/traffic_stats_amortizer.h" | 5 #include "components/data_usage/android/traffic_stats_amortizer.h" |
| 6 | 6 |
| 7 #include <algorithm> // For std::min. | 7 #include <algorithm> // For std::min. |
| 8 #include <cmath> // For std::modf. | 8 #include <cmath> // For std::modf. |
| 9 #include <utility> |
| 9 | 10 |
| 10 #include "base/location.h" | 11 #include "base/location.h" |
| 11 #include "base/metrics/histogram_base.h" | 12 #include "base/metrics/histogram_base.h" |
| 12 #include "base/metrics/histogram_macros.h" | 13 #include "base/metrics/histogram_macros.h" |
| 13 #include "base/time/default_tick_clock.h" | 14 #include "base/time/default_tick_clock.h" |
| 14 #include "base/timer/timer.h" | 15 #include "base/timer/timer.h" |
| 15 #include "components/data_usage/core/data_use.h" | 16 #include "components/data_usage/core/data_use.h" |
| 16 #include "net/android/traffic_stats.h" | 17 #include "net/android/traffic_stats.h" |
| 17 | 18 |
| 18 namespace data_usage { | 19 namespace data_usage { |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 scoped_ptr<DataUse> data_use, | 143 scoped_ptr<DataUse> data_use, |
| 143 const AmortizationCompleteCallback& callback) { | 144 const AmortizationCompleteCallback& callback) { |
| 144 DCHECK(thread_checker_.CalledOnValidThread()); | 145 DCHECK(thread_checker_.CalledOnValidThread()); |
| 145 DCHECK(!callback.is_null()); | 146 DCHECK(!callback.is_null()); |
| 146 int64_t tx_bytes = data_use->tx_bytes, rx_bytes = data_use->rx_bytes; | 147 int64_t tx_bytes = data_use->tx_bytes, rx_bytes = data_use->rx_bytes; |
| 147 | 148 |
| 148 // TODO(sclittle): Combine consecutive buffered DataUse objects that are | 149 // TODO(sclittle): Combine consecutive buffered DataUse objects that are |
| 149 // identical except for byte counts and have the same callback. | 150 // identical except for byte counts and have the same callback. |
| 150 buffered_data_use_.push_back( | 151 buffered_data_use_.push_back( |
| 151 std::pair<scoped_ptr<DataUse>, AmortizationCompleteCallback>( | 152 std::pair<scoped_ptr<DataUse>, AmortizationCompleteCallback>( |
| 152 data_use.Pass(), callback)); | 153 std::move(data_use), callback)); |
| 153 | 154 |
| 154 AddPreAmortizationBytes(tx_bytes, rx_bytes); | 155 AddPreAmortizationBytes(tx_bytes, rx_bytes); |
| 155 } | 156 } |
| 156 | 157 |
| 157 void TrafficStatsAmortizer::OnExtraBytes(int64_t extra_tx_bytes, | 158 void TrafficStatsAmortizer::OnExtraBytes(int64_t extra_tx_bytes, |
| 158 int64_t extra_rx_bytes) { | 159 int64_t extra_rx_bytes) { |
| 159 DCHECK(thread_checker_.CalledOnValidThread()); | 160 DCHECK(thread_checker_.CalledOnValidThread()); |
| 160 AddPreAmortizationBytes(extra_tx_bytes, extra_rx_bytes); | 161 AddPreAmortizationBytes(extra_tx_bytes, extra_rx_bytes); |
| 161 } | 162 } |
| 162 | 163 |
| 163 base::WeakPtr<TrafficStatsAmortizer> TrafficStatsAmortizer::GetWeakPtr() { | 164 base::WeakPtr<TrafficStatsAmortizer> TrafficStatsAmortizer::GetWeakPtr() { |
| 164 DCHECK(thread_checker_.CalledOnValidThread()); | 165 DCHECK(thread_checker_.CalledOnValidThread()); |
| 165 return weak_ptr_factory_.GetWeakPtr(); | 166 return weak_ptr_factory_.GetWeakPtr(); |
| 166 } | 167 } |
| 167 | 168 |
| 168 TrafficStatsAmortizer::TrafficStatsAmortizer( | 169 TrafficStatsAmortizer::TrafficStatsAmortizer( |
| 169 scoped_ptr<base::TickClock> tick_clock, | 170 scoped_ptr<base::TickClock> tick_clock, |
| 170 scoped_ptr<base::Timer> traffic_stats_query_timer, | 171 scoped_ptr<base::Timer> traffic_stats_query_timer, |
| 171 const base::TimeDelta& traffic_stats_query_delay, | 172 const base::TimeDelta& traffic_stats_query_delay, |
| 172 const base::TimeDelta& max_amortization_delay, | 173 const base::TimeDelta& max_amortization_delay, |
| 173 size_t max_data_use_buffer_size) | 174 size_t max_data_use_buffer_size) |
| 174 : tick_clock_(tick_clock.Pass()), | 175 : tick_clock_(std::move(tick_clock)), |
| 175 traffic_stats_query_timer_(traffic_stats_query_timer.Pass()), | 176 traffic_stats_query_timer_(std::move(traffic_stats_query_timer)), |
| 176 traffic_stats_query_delay_(traffic_stats_query_delay), | 177 traffic_stats_query_delay_(traffic_stats_query_delay), |
| 177 max_amortization_delay_(max_amortization_delay), | 178 max_amortization_delay_(max_amortization_delay), |
| 178 max_data_use_buffer_size_(max_data_use_buffer_size), | 179 max_data_use_buffer_size_(max_data_use_buffer_size), |
| 179 is_amortization_in_progress_(false), | 180 is_amortization_in_progress_(false), |
| 180 are_last_amortization_traffic_stats_available_(false), | 181 are_last_amortization_traffic_stats_available_(false), |
| 181 last_amortization_traffic_stats_tx_bytes_(-1), | 182 last_amortization_traffic_stats_tx_bytes_(-1), |
| 182 last_amortization_traffic_stats_rx_bytes_(-1), | 183 last_amortization_traffic_stats_rx_bytes_(-1), |
| 183 pre_amortization_tx_bytes_(0), | 184 pre_amortization_tx_bytes_(0), |
| 184 pre_amortization_rx_bytes_(0), | 185 pre_amortization_rx_bytes_(0), |
| 185 weak_ptr_factory_(this) {} | 186 weak_ptr_factory_(this) {} |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 are_current_traffic_stats_available; | 322 are_current_traffic_stats_available; |
| 322 | 323 |
| 323 pre_amortization_tx_bytes_ = 0; | 324 pre_amortization_tx_bytes_ = 0; |
| 324 pre_amortization_rx_bytes_ = 0; | 325 pre_amortization_rx_bytes_ = 0; |
| 325 | 326 |
| 326 DataUseBuffer data_use_sequence; | 327 DataUseBuffer data_use_sequence; |
| 327 data_use_sequence.swap(buffered_data_use_); | 328 data_use_sequence.swap(buffered_data_use_); |
| 328 | 329 |
| 329 // Pass post-amortization DataUse objects to their respective callbacks. | 330 // Pass post-amortization DataUse objects to their respective callbacks. |
| 330 for (auto& data_use_buffer_pair : data_use_sequence) | 331 for (auto& data_use_buffer_pair : data_use_sequence) |
| 331 data_use_buffer_pair.second.Run(data_use_buffer_pair.first.Pass()); | 332 data_use_buffer_pair.second.Run(std::move(data_use_buffer_pair.first)); |
| 332 } | 333 } |
| 333 | 334 |
| 334 } // namespace android | 335 } // namespace android |
| 335 } // namespace data_usage | 336 } // namespace data_usage |
| OLD | NEW |