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 "chrome/browser/android/data_usage/external_data_use_observer.h" | 5 #include "chrome/browser/android/data_usage/external_data_use_observer.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/containers/hash_tables.h" | 9 #include "base/containers/hash_tables.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 ui_task_runner_(ui_task_runner), | 139 ui_task_runner_(ui_task_runner), |
140 previous_report_time_(base::Time::Now()), | 140 previous_report_time_(base::Time::Now()), |
141 last_matching_rules_fetch_time_(base::TimeTicks::Now()), | 141 last_matching_rules_fetch_time_(base::TimeTicks::Now()), |
142 external_data_use_observer_bridge_(new ExternalDataUseObserverBridge()), | 142 external_data_use_observer_bridge_(new ExternalDataUseObserverBridge()), |
143 total_bytes_buffered_(0), | 143 total_bytes_buffered_(0), |
144 fetch_matching_rules_duration_( | 144 fetch_matching_rules_duration_( |
145 base::TimeDelta::FromSeconds(GetFetchMatchingRulesDurationSeconds())), | 145 base::TimeDelta::FromSeconds(GetFetchMatchingRulesDurationSeconds())), |
146 data_use_report_min_bytes_(GetMinBytes()), | 146 data_use_report_min_bytes_(GetMinBytes()), |
147 data_report_submit_timeout_( | 147 data_report_submit_timeout_( |
148 base::TimeDelta::FromMilliseconds(GetDataReportSubmitTimeoutMsec())), | 148 base::TimeDelta::FromMilliseconds(GetDataReportSubmitTimeoutMsec())), |
| 149 #if defined(OS_ANDROID) |
| 150 app_state_listener_(new base::android::ApplicationStatusListener( |
| 151 base::Bind(&ExternalDataUseObserver::OnApplicationStateChange, |
| 152 base::Unretained(this)))), |
| 153 #endif |
149 registered_as_data_use_observer_(false), | 154 registered_as_data_use_observer_(false), |
150 weak_factory_(this) { | 155 weak_factory_(this) { |
151 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 156 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
152 DCHECK(data_use_aggregator_); | 157 DCHECK(data_use_aggregator_); |
153 DCHECK(io_task_runner); | 158 DCHECK(io_task_runner); |
154 DCHECK(ui_task_runner_); | 159 DCHECK(ui_task_runner_); |
155 DCHECK(last_data_report_submitted_ticks_.is_null()); | 160 DCHECK(last_data_report_submitted_ticks_.is_null()); |
156 | 161 |
157 ui_task_runner_->PostTask(FROM_HERE, | 162 ui_task_runner_->PostTask(FROM_HERE, |
158 base::Bind(&DataUseTabModel::InitOnUIThread, | 163 base::Bind(&DataUseTabModel::InitOnUIThread, |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 RecordDataUsageReportSubmission(DATAUSAGE_REPORT_SUBMISSION_SUCCESSFUL, | 202 RecordDataUsageReportSubmission(DATAUSAGE_REPORT_SUBMISSION_SUCCESSFUL, |
198 pending_report_bytes_); | 203 pending_report_bytes_); |
199 } else { | 204 } else { |
200 RecordDataUsageReportSubmission(DATAUSAGE_REPORT_SUBMISSION_FAILED, | 205 RecordDataUsageReportSubmission(DATAUSAGE_REPORT_SUBMISSION_FAILED, |
201 pending_report_bytes_); | 206 pending_report_bytes_); |
202 } | 207 } |
203 | 208 |
204 last_data_report_submitted_ticks_ = base::TimeTicks(); | 209 last_data_report_submitted_ticks_ = base::TimeTicks(); |
205 pending_report_bytes_ = 0; | 210 pending_report_bytes_ = 0; |
206 | 211 |
207 SubmitBufferedDataUseReport(); | 212 SubmitBufferedDataUseReport(false); |
208 } | 213 } |
209 | 214 |
| 215 #if defined(OS_ANDROID) |
| 216 void ExternalDataUseObserver::OnApplicationStateChange( |
| 217 base::android::ApplicationState new_state) { |
| 218 DCHECK(thread_checker_.CalledOnValidThread()); |
| 219 if (new_state == base::android::APPLICATION_STATE_HAS_PAUSED_ACTIVITIES) |
| 220 SubmitBufferedDataUseReport(true); |
| 221 } |
| 222 #endif |
| 223 |
210 void ExternalDataUseObserver::OnDataUse(const data_usage::DataUse& data_use) { | 224 void ExternalDataUseObserver::OnDataUse(const data_usage::DataUse& data_use) { |
211 DCHECK(thread_checker_.CalledOnValidThread()); | 225 DCHECK(thread_checker_.CalledOnValidThread()); |
212 DCHECK(registered_as_data_use_observer_); | 226 DCHECK(registered_as_data_use_observer_); |
213 | 227 |
214 const base::TimeTicks now_ticks = base::TimeTicks::Now(); | 228 const base::TimeTicks now_ticks = base::TimeTicks::Now(); |
215 const base::Time now_time = base::Time::Now(); | 229 const base::Time now_time = base::Time::Now(); |
216 | 230 |
217 // If the time when the matching rules were last fetched is more than | 231 // If the time when the matching rules were last fetched is more than |
218 // |fetch_matching_rules_duration_|, fetch them again. | 232 // |fetch_matching_rules_duration_|, fetch them again. |
219 if (now_ticks - last_matching_rules_fetch_time_ >= | 233 if (now_ticks - last_matching_rules_fetch_time_ >= |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 const base::Time& start_time, | 277 const base::Time& start_time, |
264 const base::Time& end_time, | 278 const base::Time& end_time, |
265 const std::string* label, | 279 const std::string* label, |
266 bool label_applied) { | 280 bool label_applied) { |
267 DCHECK(thread_checker_.CalledOnValidThread()); | 281 DCHECK(thread_checker_.CalledOnValidThread()); |
268 | 282 |
269 if (!label_applied) | 283 if (!label_applied) |
270 return; | 284 return; |
271 | 285 |
272 BufferDataUseReport(data_use, *label, start_time, end_time); | 286 BufferDataUseReport(data_use, *label, start_time, end_time); |
273 SubmitBufferedDataUseReport(); | 287 SubmitBufferedDataUseReport(false); |
274 } | 288 } |
275 | 289 |
276 void ExternalDataUseObserver::BufferDataUseReport( | 290 void ExternalDataUseObserver::BufferDataUseReport( |
277 const data_usage::DataUse& data_use, | 291 const data_usage::DataUse& data_use, |
278 const std::string& label, | 292 const std::string& label, |
279 const base::Time& start_time, | 293 const base::Time& start_time, |
280 const base::Time& end_time) { | 294 const base::Time& end_time) { |
281 DCHECK(thread_checker_.CalledOnValidThread()); | 295 DCHECK(thread_checker_.CalledOnValidThread()); |
282 DCHECK(!label.empty()); | 296 DCHECK(!label.empty()); |
283 DCHECK_LE(0, data_use.rx_bytes); | 297 DCHECK_LE(0, data_use.rx_bytes); |
(...skipping 29 matching lines...) Expand all Loading... |
313 buffered_data_reports_.erase(it); | 327 buffered_data_reports_.erase(it); |
314 buffered_data_reports_.insert( | 328 buffered_data_reports_.insert( |
315 std::make_pair(data_use_report_key, merged_report)); | 329 std::make_pair(data_use_report_key, merged_report)); |
316 } | 330 } |
317 total_bytes_buffered_ += (data_use.rx_bytes + data_use.tx_bytes); | 331 total_bytes_buffered_ += (data_use.rx_bytes + data_use.tx_bytes); |
318 | 332 |
319 DCHECK_LT(0U, buffered_data_reports_.size()); | 333 DCHECK_LT(0U, buffered_data_reports_.size()); |
320 DCHECK_LE(buffered_data_reports_.size(), kMaxBufferSize); | 334 DCHECK_LE(buffered_data_reports_.size(), kMaxBufferSize); |
321 } | 335 } |
322 | 336 |
323 void ExternalDataUseObserver::SubmitBufferedDataUseReport() { | 337 void ExternalDataUseObserver::SubmitBufferedDataUseReport(bool immediate) { |
324 DCHECK(thread_checker_.CalledOnValidThread()); | 338 DCHECK(thread_checker_.CalledOnValidThread()); |
325 | 339 |
326 const base::TimeTicks ticks_now = base::TimeTicks::Now(); | 340 const base::TimeTicks ticks_now = base::TimeTicks::Now(); |
327 | 341 |
328 // Return if a data use report has been pending for less than | 342 // Return if a data use report has been pending for less than |
329 // |data_report_submit_timeout_| duration. | 343 // |data_report_submit_timeout_| duration. |
330 if (!last_data_report_submitted_ticks_.is_null() && | 344 if (!last_data_report_submitted_ticks_.is_null() && |
331 ticks_now - last_data_report_submitted_ticks_ < | 345 ticks_now - last_data_report_submitted_ticks_ < |
332 data_report_submit_timeout_) { | 346 data_report_submit_timeout_) { |
333 return; | 347 return; |
334 } | 348 } |
335 | 349 |
336 if (buffered_data_reports_.empty()) | 350 if (buffered_data_reports_.empty()) |
337 return; | 351 return; |
338 | 352 |
339 if (total_bytes_buffered_ < data_use_report_min_bytes_) | 353 if (!immediate && total_bytes_buffered_ < data_use_report_min_bytes_) |
340 return; | 354 return; |
341 | 355 |
342 if (!last_data_report_submitted_ticks_.is_null()) { | 356 if (!last_data_report_submitted_ticks_.is_null()) { |
343 // Mark the pending DataUsage report as timed out. | 357 // Mark the pending DataUsage report as timed out. |
344 RecordDataUsageReportSubmission(DATAUSAGE_REPORT_SUBMISSION_TIMED_OUT, | 358 RecordDataUsageReportSubmission(DATAUSAGE_REPORT_SUBMISSION_TIMED_OUT, |
345 pending_report_bytes_); | 359 pending_report_bytes_); |
346 pending_report_bytes_ = 0; | 360 pending_report_bytes_ = 0; |
347 last_data_report_submitted_ticks_ = base::TimeTicks(); | 361 last_data_report_submitted_ticks_ = base::TimeTicks(); |
348 } | 362 } |
349 | 363 |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
418 size_t hash = 1; | 432 size_t hash = 1; |
419 hash = hash * 23 + hash_function(k.label); | 433 hash = hash * 23 + hash_function(k.label); |
420 hash = hash * 43 + k.connection_type; | 434 hash = hash * 43 + k.connection_type; |
421 hash = hash * 83 + hash_function(k.mcc_mnc); | 435 hash = hash * 83 + hash_function(k.mcc_mnc); |
422 return hash; | 436 return hash; |
423 } | 437 } |
424 | 438 |
425 } // namespace android | 439 } // namespace android |
426 | 440 |
427 } // namespace chrome | 441 } // namespace chrome |
OLD | NEW |