| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/metrics/metrics_reporting_scheduler.h" | 5 #include "chrome/browser/metrics/metrics_reporting_scheduler.h" |
| 6 | 6 |
| 7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "chrome/browser/metrics/metrics_service.h" | 8 #include "chrome/browser/metrics/metrics_service.h" |
| 9 | 9 |
| 10 using base::Time; | 10 using base::Time; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 // The following is the multiplier we use to expand that inter-log duration. | 26 // The following is the multiplier we use to expand that inter-log duration. |
| 27 static const double kBackoffMultiplier = 1.1; | 27 static const double kBackoffMultiplier = 1.1; |
| 28 | 28 |
| 29 // The maximum backoff multiplier. | 29 // The maximum backoff multiplier. |
| 30 static const int kMaxBackoffMultiplier = 10; | 30 static const int kMaxBackoffMultiplier = 10; |
| 31 | 31 |
| 32 | 32 |
| 33 MetricsReportingScheduler::MetricsReportingScheduler( | 33 MetricsReportingScheduler::MetricsReportingScheduler( |
| 34 const base::Closure& upload_callback) | 34 const base::Closure& upload_callback) |
| 35 : upload_callback_(upload_callback), | 35 : upload_callback_(upload_callback), |
| 36 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)), | |
| 37 upload_interval_(TimeDelta::FromSeconds(kInitialUploadIntervalSeconds)), | 36 upload_interval_(TimeDelta::FromSeconds(kInitialUploadIntervalSeconds)), |
| 38 running_(false), | 37 running_(false), |
| 39 timer_pending_(false), | |
| 40 callback_pending_(false) { | 38 callback_pending_(false) { |
| 41 } | 39 } |
| 42 | 40 |
| 43 MetricsReportingScheduler::~MetricsReportingScheduler() {} | 41 MetricsReportingScheduler::~MetricsReportingScheduler() {} |
| 44 | 42 |
| 45 void MetricsReportingScheduler::Start() { | 43 void MetricsReportingScheduler::Start() { |
| 46 running_ = true; | 44 running_ = true; |
| 47 ScheduleNextCallback(); | 45 ScheduleNextCallback(); |
| 48 } | 46 } |
| 49 | 47 |
| 50 void MetricsReportingScheduler::Stop() { | 48 void MetricsReportingScheduler::Stop() { |
| 51 running_ = false; | 49 running_ = false; |
| 50 if (upload_timer_.IsRunning()) |
| 51 upload_timer_.Stop(); |
| 52 } | 52 } |
| 53 | 53 |
| 54 void MetricsReportingScheduler::UploadFinished(bool server_is_healthy, | 54 void MetricsReportingScheduler::UploadFinished(bool server_is_healthy, |
| 55 bool more_logs_remaining) { | 55 bool more_logs_remaining) { |
| 56 DCHECK(callback_pending_); | 56 DCHECK(callback_pending_); |
| 57 callback_pending_ = false; | 57 callback_pending_ = false; |
| 58 // If the server is having issues, back off. Otherwise, reset to default | 58 // If the server is having issues, back off. Otherwise, reset to default |
| 59 // (unless there are more logs to send, in which case the next upload should | 59 // (unless there are more logs to send, in which case the next upload should |
| 60 // happen sooner). | 60 // happen sooner). |
| 61 if (!server_is_healthy) { | 61 if (!server_is_healthy) { |
| 62 BackOffUploadInterval(); | 62 BackOffUploadInterval(); |
| 63 } else if (more_logs_remaining) { | 63 } else if (more_logs_remaining) { |
| 64 upload_interval_ = TimeDelta::FromSeconds(kUnsentLogsIntervalSeconds); | 64 upload_interval_ = TimeDelta::FromSeconds(kUnsentLogsIntervalSeconds); |
| 65 } else { | 65 } else { |
| 66 upload_interval_ = TimeDelta::FromSeconds(kStandardUploadIntervalSeconds); | 66 upload_interval_ = TimeDelta::FromSeconds(kStandardUploadIntervalSeconds); |
| 67 } | 67 } |
| 68 | 68 |
| 69 if (running_) | 69 if (running_) |
| 70 ScheduleNextCallback(); | 70 ScheduleNextCallback(); |
| 71 } | 71 } |
| 72 | 72 |
| 73 void MetricsReportingScheduler::UploadCancelled() { | 73 void MetricsReportingScheduler::UploadCancelled() { |
| 74 DCHECK(callback_pending_); | 74 DCHECK(callback_pending_); |
| 75 callback_pending_ = false; | 75 callback_pending_ = false; |
| 76 if (running_) | 76 if (running_) |
| 77 ScheduleNextCallback(); | 77 ScheduleNextCallback(); |
| 78 } | 78 } |
| 79 | 79 |
| 80 void MetricsReportingScheduler::TriggerUpload() { | 80 void MetricsReportingScheduler::TriggerUpload() { |
| 81 timer_pending_ = false; | |
| 82 callback_pending_ = true; | 81 callback_pending_ = true; |
| 83 upload_callback_.Run(); | 82 upload_callback_.Run(); |
| 84 } | 83 } |
| 85 | 84 |
| 86 void MetricsReportingScheduler::ScheduleNextCallback() { | 85 void MetricsReportingScheduler::ScheduleNextCallback() { |
| 87 DCHECK(running_); | 86 DCHECK(running_); |
| 88 if (timer_pending_ || callback_pending_) | 87 if (upload_timer_.IsRunning() || callback_pending_) |
| 89 return; | 88 return; |
| 90 | 89 |
| 91 timer_pending_ = true; | 90 upload_timer_.Start(FROM_HERE, upload_interval_, this, |
| 92 | 91 &MetricsReportingScheduler::TriggerUpload); |
| 93 MessageLoop::current()->PostDelayedTask( | |
| 94 FROM_HERE, | |
| 95 base::Bind(&MetricsReportingScheduler::TriggerUpload, | |
| 96 weak_ptr_factory_.GetWeakPtr()), | |
| 97 upload_interval_); | |
| 98 } | 92 } |
| 99 | 93 |
| 100 void MetricsReportingScheduler::BackOffUploadInterval() { | 94 void MetricsReportingScheduler::BackOffUploadInterval() { |
| 101 DCHECK(kBackoffMultiplier > 1.0); | 95 DCHECK(kBackoffMultiplier > 1.0); |
| 102 upload_interval_ = TimeDelta::FromMicroseconds( | 96 upload_interval_ = TimeDelta::FromMicroseconds( |
| 103 static_cast<int64>(kBackoffMultiplier * | 97 static_cast<int64>(kBackoffMultiplier * |
| 104 upload_interval_.InMicroseconds())); | 98 upload_interval_.InMicroseconds())); |
| 105 | 99 |
| 106 TimeDelta max_interval = kMaxBackoffMultiplier * | 100 TimeDelta max_interval = kMaxBackoffMultiplier * |
| 107 TimeDelta::FromSeconds(kStandardUploadIntervalSeconds); | 101 TimeDelta::FromSeconds(kStandardUploadIntervalSeconds); |
| 108 if (upload_interval_ > max_interval || upload_interval_.InSeconds() < 0) { | 102 if (upload_interval_ > max_interval || upload_interval_.InSeconds() < 0) { |
| 109 upload_interval_ = max_interval; | 103 upload_interval_ = max_interval; |
| 110 } | 104 } |
| 111 } | 105 } |
| OLD | NEW |