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 "base/strings/stringprintf.h" | |
| 9 #include "base/task/cancelable_task_tracker.h" | |
| 10 #include "base/task_runner_util.h" | |
| 11 #include "base/thread_task_runner_handle.h" | |
| 12 #include "chrome/browser/browser_process.h" | |
| 13 #include "chrome/browser/chromeos/policy/system_log_uploader.h" | |
| 14 #include "content/public/browser/browser_thread.h" | |
| 15 #include "net/http/http_request_headers.h" | |
| 16 | |
| 17 namespace { | |
| 18 // Determines the time between log uploads. | |
| 19 const int64 kDefaultUploadDelayMs = 12 * 60 * 60 * 1000; // 12 hours | |
| 20 | |
| 21 // Determines the time, measured from the time of last failed upload, | |
| 22 // after which the log upload is retried. | |
| 23 const int64 kErrorUploadDelayMs = 120 * 1000; // 120 seconds | |
| 24 | |
| 25 // The maximum number of successive retries. | |
| 26 const int kMaxNumRetries = 1; | |
| 27 | |
| 28 // String constant defining the url we upload system logs to. | |
| 29 const char* kSystemLogUploadUrl = | |
| 30 "https://m.google.com/devicemanagement/data/api/upload"; | |
| 31 | |
| 32 // String constant identifying the header field which stores the file type. | |
| 33 const char* kFileTypeHeaderName = "File-Type"; | |
| 34 | |
| 35 // String constant signalling that the data segment contains log files. | |
| 36 const char* const kFileTypeLogFile = "log_file"; | |
| 37 | |
| 38 // String constant signalling that the segment contains a plain text. | |
| 39 const char* const kContentTypePlainText = "text/plain"; | |
| 40 | |
| 41 // Template string constant for populating the name field. | |
| 42 const char* const kNameFieldTemplate = "file%d"; | |
| 43 | |
| 44 } // namespace | |
| 45 | |
| 46 namespace policy { | |
| 47 | |
| 48 SystemLogUploader::SystemLogUploader( | |
| 49 scoped_ptr<Delegate> syslog_delegate, | |
| 50 const scoped_refptr<base::SequencedTaskRunner>& task_runner) | |
| 51 : retry_count_(0), | |
| 52 upload_frequency_( | |
| 53 base::TimeDelta::FromMilliseconds(kDefaultUploadDelayMs)), | |
| 54 task_runner_(task_runner), | |
| 55 syslog_delegate_(syslog_delegate.Pass()), | |
| 56 weak_factory_(this) { | |
| 57 DCHECK(syslog_delegate_); | |
| 58 // Immediately schedule the next system log upload (last_upload_attempt_ is | |
| 59 // set to the start of the epoch, so this will trigger an update upload in the | |
| 60 // immediate future). | |
| 61 ScheduleNextSystemLogUpload(upload_frequency_); | |
| 62 } | |
| 63 | |
| 64 SystemLogUploader::~SystemLogUploader() { | |
| 65 } | |
| 66 | |
| 67 void SystemLogUploader::OnSuccess() { | |
| 68 // On successful log upload schedule the next log upload after | |
| 69 // upload_frequency_ time from now. | |
| 70 upload_job_.reset(); | |
| 71 last_upload_attempt_ = base::Time::NowFromSystemTime(); | |
| 72 retry_count_ = 0; | |
| 73 | |
| 74 ScheduleNextSystemLogUpload(upload_frequency_); | |
| 75 } | |
| 76 | |
| 77 void SystemLogUploader::OnFailure(UploadJob::ErrorCode error_code) { | |
| 78 // On first failure log upload try to re-upload logs after kErrorUploadDelayMs | |
| 79 // time from now. | |
|
Andrew T Wilson (Slow)
2015/07/31 13:05:06
nit: This comment will get obsolete if kMaxNumRetr
Polina Bondarenko
2015/07/31 13:52:03
Done.
| |
| 80 upload_job_.reset(); | |
| 81 last_upload_attempt_ = base::Time::NowFromSystemTime(); | |
| 82 | |
| 83 if (retry_count_++ < kMaxNumRetries) { | |
| 84 ScheduleNextSystemLogUpload( | |
| 85 base::TimeDelta::FromMilliseconds(kErrorUploadDelayMs)); | |
| 86 } else { | |
| 87 // No more retries. | |
| 88 retry_count_ = 0; | |
| 89 ScheduleNextSystemLogUpload(upload_frequency_); | |
| 90 } | |
| 91 } | |
| 92 | |
| 93 void SystemLogUploader::UploadSystemLogs( | |
| 94 const Delegate::SystemLogs* system_logs) { | |
| 95 // Must be called on the main thread. | |
| 96 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 97 DCHECK(!upload_job_); | |
| 98 | |
| 99 GURL upload_url(kSystemLogUploadUrl); | |
| 100 DCHECK(upload_url.is_valid()); | |
| 101 upload_job_ = syslog_delegate_->CreateUploadJob(upload_url, this); | |
| 102 | |
| 103 // Start a system log upload. | |
| 104 int file_number = 1; | |
| 105 if (system_logs) { | |
| 106 for (const auto& syslog_entry : (*system_logs)) { | |
| 107 std::map<std::string, std::string> header_fields; | |
| 108 scoped_ptr<std::string> data = | |
| 109 make_scoped_ptr(new std::string(syslog_entry.second)); | |
| 110 header_fields.insert( | |
| 111 std::make_pair(kFileTypeHeaderName, kFileTypeLogFile)); | |
| 112 header_fields.insert(std::make_pair(net::HttpRequestHeaders::kContentType, | |
| 113 kContentTypePlainText)); | |
| 114 upload_job_->AddDataSegment( | |
| 115 base::StringPrintf(kNameFieldTemplate, file_number), | |
| 116 syslog_entry.first, header_fields, data.Pass()); | |
| 117 ++file_number; | |
| 118 } | |
| 119 } | |
| 120 upload_job_->Start(); | |
| 121 } | |
| 122 | |
| 123 void SystemLogUploader::StartLogUpload() { | |
| 124 // Must be called on the main thread. | |
| 125 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 126 | |
| 127 syslog_delegate_->LoadSystemLogs(base::Bind( | |
| 128 &SystemLogUploader::UploadSystemLogs, weak_factory_.GetWeakPtr())); | |
| 129 } | |
| 130 | |
| 131 void SystemLogUploader::ScheduleNextSystemLogUpload(base::TimeDelta frequency) { | |
| 132 // Calculate when to fire off the next update. | |
| 133 base::TimeDelta delay = std::max( | |
| 134 (last_upload_attempt_ + frequency) - base::Time::NowFromSystemTime(), | |
| 135 base::TimeDelta()); | |
| 136 task_runner_->PostDelayedTask(FROM_HERE, | |
| 137 base::Bind(&SystemLogUploader::StartLogUpload, | |
| 138 weak_factory_.GetWeakPtr()), | |
| 139 delay); | |
| 140 } | |
| 141 | |
| 142 } // namespace policy | |
| OLD | NEW |