Index: chrome/browser/chromeos/policy/system_log_uploader.cc |
diff --git a/chrome/browser/chromeos/policy/system_log_uploader.cc b/chrome/browser/chromeos/policy/system_log_uploader.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..1679f0cf65895d1a556a707fbf7fa275392d9984 |
--- /dev/null |
+++ b/chrome/browser/chromeos/policy/system_log_uploader.cc |
@@ -0,0 +1,82 @@ |
+// Copyright (c) 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/bind.h" |
+#include "base/bind_helpers.h" |
+#include "base/location.h" |
+#include "chrome/browser/chromeos/policy/system_log_uploader.h" |
+ |
+namespace { |
+// Determines the time between log uploads. |
+const int64 kDefaultUploadDelayMs = 12 * 60 * 60 * 1000; // 12 hours |
+ |
+// Determines the time, measured from the time of last failed upload, |
+// after which the log upload is retried. |
+const int64 kErrorUploadDelayMs = 120 * 1000; // 120 seconds |
+ |
+} // namespace |
+ |
+namespace policy { |
+ |
+SystemLogUploader::SystemLogUploader( |
+ const scoped_refptr<base::SequencedTaskRunner>& task_runner, |
+ scoped_ptr<SystemLogUploadJob> upload_job) |
+ : last_success_(true), |
+ upload_frequency_( |
+ base::TimeDelta::FromMilliseconds(kDefaultUploadDelayMs)), |
+ last_upload_(base::Time::NowFromSystemTime()), |
+ task_runner_(task_runner), |
+ upload_job_(upload_job.Pass()), |
+ weak_factory_(this) { |
+ DCHECK(upload_job_); |
+ // Immediately schedule the next system log upload after upload_frequency_ |
+ // time from now. |
+ ScheduleNextSystemLogUpload(upload_frequency_); |
+} |
+ |
+SystemLogUploader::~SystemLogUploader() { |
+} |
+ |
+void SystemLogUploader::OnSuccess() { |
+ // On successful log upload schedule next log upload after upload_frequency_ |
+ // time from now. |
+ last_success_ = true; |
+ last_upload_ = base::Time::NowFromSystemTime(); |
+ |
+ upload_job_->Terminate(); |
+ ScheduleNextSystemLogUpload(upload_frequency_); |
+} |
+ |
+void SystemLogUploader::OnFailure() { |
+ // On first failure log upload try to re-upload logs after kErrorUploadDelayMs |
+ // time from now. |
+ last_upload_ = base::Time::NowFromSystemTime(); |
+ |
+ upload_job_->Terminate(); |
+ if (last_success_) |
+ ScheduleNextSystemLogUpload( |
Andrew T Wilson (Slow)
2015/06/29 15:06:09
You have a multiple line statement here, so you ha
Polina Bondarenko
2015/07/02 15:28:04
Done.
|
+ base::TimeDelta::FromMilliseconds(kErrorUploadDelayMs)); |
+ else |
+ ScheduleNextSystemLogUpload(upload_frequency_); |
+ |
+ last_success_ = !last_success_; |
Andrew T Wilson (Slow)
2015/06/29 15:06:09
This logic works, but I think it'd be easier to re
Polina Bondarenko
2015/07/02 15:28:04
Done.
|
+} |
+ |
+void SystemLogUploader::StartLogUpload() { |
+ upload_job_->Run( |
+ base::Bind(&SystemLogUploader::OnSuccess, base::Unretained(this)), |
Andrew T Wilson (Slow)
2015/06/29 15:06:09
Why use base::Unretained() here? Why do you have a
Polina Bondarenko
2015/07/02 15:28:04
Understood, changed to weak_factory_ to prevent fa
|
+ base::Bind(&SystemLogUploader::OnFailure, base::Unretained(this))); |
+} |
+void SystemLogUploader::ScheduleNextSystemLogUpload(base::TimeDelta frequency) { |
Andrew T Wilson (Slow)
2015/06/29 15:06:09
nit: blank line between methods
Polina Bondarenko
2015/07/02 15:28:04
Done.
|
+ // Calculate when to fire off the next update. |
+ base::TimeDelta delay = |
+ std::max((last_upload_ + frequency) - base::Time::NowFromSystemTime(), |
+ base::TimeDelta()); |
+ task_runner_->PostDelayedTask( |
+ FROM_HERE, |
+ base::Bind(&SystemLogUploader::StartLogUpload, base::Unretained(this)), |
Andrew T Wilson (Slow)
2015/06/29 15:06:09
Should use weak_ptr_factory instead of base::unret
Polina Bondarenko
2015/07/02 15:28:04
Done.
|
+ delay); |
+} |
+ |
+} // namespace policy |