Chromium Code Reviews| 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_uploader.h" | |
| 9 | |
| 10 namespace { | |
| 11 // Determines the time between log uploads. | |
| 12 const int64 kDefaultUploadDelayMs = 12 * 60 * 60 * 1000; // 12 hours | |
| 13 | |
| 14 // Determines the time, measured from the time of last failed upload, | |
| 15 // after which the log upload is retried. | |
| 16 const int64 kErrorUploadDelayMs = 120 * 1000; // 120 seconds | |
| 17 | |
| 18 } // namespace | |
| 19 | |
| 20 namespace policy { | |
| 21 | |
| 22 SystemLogUploader::SystemLogUploader( | |
| 23 const scoped_refptr<base::SequencedTaskRunner>& task_runner, | |
| 24 scoped_ptr<SystemLogUploadJob> upload_job) | |
| 25 : last_success_(true), | |
| 26 upload_frequency_( | |
| 27 base::TimeDelta::FromMilliseconds(kDefaultUploadDelayMs)), | |
| 28 last_upload_(base::Time::NowFromSystemTime()), | |
| 29 task_runner_(task_runner), | |
| 30 upload_job_(upload_job.Pass()), | |
| 31 weak_factory_(this) { | |
| 32 DCHECK(upload_job_); | |
| 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 } | |
| 40 | |
| 41 void SystemLogUploader::OnSuccess() { | |
| 42 // On successful log upload schedule next log upload after upload_frequency_ | |
| 43 // time from now. | |
| 44 last_success_ = true; | |
| 45 last_upload_ = base::Time::NowFromSystemTime(); | |
| 46 | |
| 47 upload_job_->Terminate(); | |
| 48 ScheduleNextSystemLogUpload(upload_frequency_); | |
| 49 } | |
| 50 | |
| 51 void SystemLogUploader::OnFailure() { | |
| 52 // On first failure log upload try to re-upload logs after kErrorUploadDelayMs | |
| 53 // time from now. | |
| 54 last_upload_ = base::Time::NowFromSystemTime(); | |
| 55 | |
| 56 upload_job_->Terminate(); | |
| 57 if (last_success_) | |
| 58 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.
| |
| 59 base::TimeDelta::FromMilliseconds(kErrorUploadDelayMs)); | |
| 60 else | |
| 61 ScheduleNextSystemLogUpload(upload_frequency_); | |
| 62 | |
| 63 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.
| |
| 64 } | |
| 65 | |
| 66 void SystemLogUploader::StartLogUpload() { | |
| 67 upload_job_->Run( | |
| 68 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
| |
| 69 base::Bind(&SystemLogUploader::OnFailure, base::Unretained(this))); | |
| 70 } | |
| 71 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.
| |
| 72 // Calculate when to fire off the next update. | |
| 73 base::TimeDelta delay = | |
| 74 std::max((last_upload_ + frequency) - base::Time::NowFromSystemTime(), | |
| 75 base::TimeDelta()); | |
| 76 task_runner_->PostDelayedTask( | |
| 77 FROM_HERE, | |
| 78 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.
| |
| 79 delay); | |
| 80 } | |
| 81 | |
| 82 } // namespace policy | |
| OLD | NEW |