OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/bind.h" |
| 6 #include "base/bind_helpers.h" |
| 7 #include "base/location.h" |
| 8 #include "chrome/browser/chromeos/policy/system_log_delegate.h" |
| 9 #include "chrome/browser/chromeos/policy/system_log_uploader.h" |
| 10 |
| 11 namespace { |
| 12 // Determines the time between log uploads. |
| 13 const int64 kDefaultUploadDelayMs = 12 * 60 * 60 * 1000; // 12 hours |
| 14 |
| 15 // Determines the time, measured from the time of last failed upload, |
| 16 // after which the log upload is retried. |
| 17 const int64 kErrorUploadDelayMs = 120 * 1000; // 120 seconds |
| 18 |
| 19 } // namespace |
| 20 |
| 21 namespace policy { |
| 22 |
| 23 SystemLogUploader::SystemLogUploader( |
| 24 const scoped_refptr<base::SequencedTaskRunner>& task_runner) |
| 25 : retrying_(true), |
| 26 upload_frequency_( |
| 27 base::TimeDelta::FromMilliseconds(kDefaultUploadDelayMs)), |
| 28 last_upload_attempt_(base::Time::NowFromSystemTime()), |
| 29 task_runner_(task_runner), |
| 30 weak_factory_(this) { |
| 31 upload_job_.reset(); |
| 32 |
| 33 // Immediately schedule the next system log upload after upload_frequency_ |
| 34 // time from now. |
| 35 ScheduleNextSystemLogUpload(upload_frequency_); |
| 36 } |
| 37 |
| 38 SystemLogUploader::~SystemLogUploader() { |
| 39 upload_job_.reset(); |
| 40 } |
| 41 |
| 42 SystemLogUploadJob* SystemLogUploader::CreateSystemLogUploadJob() { |
| 43 return new SystemLogUploadJob(make_scoped_ptr(new SystemLogDelegate())); |
| 44 } |
| 45 |
| 46 void SystemLogUploader::OnSuccess() { |
| 47 // On successful log upload schedule the next log upload after |
| 48 // upload_frequency_ time from now. |
| 49 upload_job_.reset(); |
| 50 retrying_ = true; |
| 51 last_upload_attempt_ = base::Time::NowFromSystemTime(); |
| 52 |
| 53 ScheduleNextSystemLogUpload(upload_frequency_); |
| 54 } |
| 55 |
| 56 void SystemLogUploader::OnFailure() { |
| 57 // On first failure log upload try to re-upload logs after kErrorUploadDelayMs |
| 58 // time from now. |
| 59 upload_job_.reset(); |
| 60 last_upload_attempt_ = base::Time::NowFromSystemTime(); |
| 61 |
| 62 if (retrying_) { |
| 63 ScheduleNextSystemLogUpload( |
| 64 base::TimeDelta::FromMilliseconds(kErrorUploadDelayMs)); |
| 65 } else { |
| 66 ScheduleNextSystemLogUpload(upload_frequency_); |
| 67 } |
| 68 |
| 69 retrying_ = !retrying_; |
| 70 } |
| 71 |
| 72 void SystemLogUploader::StartLogUpload() { |
| 73 upload_job_.reset(CreateSystemLogUploadJob()); |
| 74 upload_job_->Run( |
| 75 base::Bind(&SystemLogUploader::OnSuccess, weak_factory_.GetWeakPtr()), |
| 76 base::Bind(&SystemLogUploader::OnFailure, weak_factory_.GetWeakPtr())); |
| 77 } |
| 78 |
| 79 void SystemLogUploader::ScheduleNextSystemLogUpload(base::TimeDelta frequency) { |
| 80 // Calculate when to fire off the next update. |
| 81 base::TimeDelta delay = std::max( |
| 82 (last_upload_attempt_ + frequency) - base::Time::NowFromSystemTime(), |
| 83 base::TimeDelta()); |
| 84 task_runner_->PostDelayedTask(FROM_HERE, |
| 85 base::Bind(&SystemLogUploader::StartLogUpload, |
| 86 weak_factory_.GetWeakPtr()), |
| 87 delay); |
| 88 } |
| 89 |
| 90 } // namespace policy |
OLD | NEW |