Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4541)

Unified Diff: chrome/browser/chromeos/policy/system_log_uploader.cc

Issue 1193333017: Added system log uploader. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed comments. Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..a9165f63b3bfb30e7b3cff147197a694f76cdeca
--- /dev/null
+++ b/chrome/browser/chromeos/policy/system_log_uploader.cc
@@ -0,0 +1,142 @@
+// 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 "base/strings/stringprintf.h"
+#include "base/task/cancelable_task_tracker.h"
+#include "base/task_runner_util.h"
+#include "base/thread_task_runner_handle.h"
+#include "chrome/browser/browser_process.h"
+#include "chrome/browser/chromeos/policy/system_log_uploader.h"
+#include "content/public/browser/browser_thread.h"
+#include "net/http/http_request_headers.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
+
+// The maximum number of successive retries.
+const int kMaxNumRetries = 1;
+
+// String constant defining the url we upload system logs to.
+const char* kSystemLogUploadUrl =
+ "https://m.google.com/devicemanagement/data/api/upload";
+
+// String constant identifying the header field which stores the file type.
+const char* kFileTypeHeaderName = "File-Type";
+
+// String constant signalling that the data segment contains log files.
+const char* const kFileTypeLogFile = "log_file";
+
+// String constant signalling that the segment contains a plain text.
+const char* const kContentTypePlainText = "text/plain";
+
+// Template string constant for populating the name field.
+const char* const kNameFieldTemplate = "file%d";
+
+} // namespace
+
+namespace policy {
+
+SystemLogUploader::SystemLogUploader(
+ scoped_ptr<Delegate> syslog_delegate,
+ const scoped_refptr<base::SequencedTaskRunner>& task_runner)
+ : retry_count_(0),
+ upload_frequency_(
+ base::TimeDelta::FromMilliseconds(kDefaultUploadDelayMs)),
+ task_runner_(task_runner),
+ syslog_delegate_(syslog_delegate.Pass()),
+ weak_factory_(this) {
+ DCHECK(syslog_delegate_);
+ // Immediately schedule the next system log upload (last_upload_attempt_ is
+ // set to the start of the epoch, so this will trigger an update upload in the
+ // immediate future).
+ ScheduleNextSystemLogUpload(upload_frequency_);
+}
+
+SystemLogUploader::~SystemLogUploader() {
+}
+
+void SystemLogUploader::OnSuccess() {
+ // On successful log upload schedule the next log upload after
+ // upload_frequency_ time from now.
+ upload_job_.reset();
+ last_upload_attempt_ = base::Time::NowFromSystemTime();
+ retry_count_ = 0;
+
+ ScheduleNextSystemLogUpload(upload_frequency_);
+}
+
+void SystemLogUploader::OnFailure(UploadJob::ErrorCode error_code) {
+ // On first failure log upload try to re-upload logs after kErrorUploadDelayMs
+ // 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.
+ upload_job_.reset();
+ last_upload_attempt_ = base::Time::NowFromSystemTime();
+
+ if (retry_count_++ < kMaxNumRetries) {
+ ScheduleNextSystemLogUpload(
+ base::TimeDelta::FromMilliseconds(kErrorUploadDelayMs));
+ } else {
+ // No more retries.
+ retry_count_ = 0;
+ ScheduleNextSystemLogUpload(upload_frequency_);
+ }
+}
+
+void SystemLogUploader::UploadSystemLogs(
+ const Delegate::SystemLogs* system_logs) {
+ // Must be called on the main thread.
+ DCHECK(thread_checker_.CalledOnValidThread());
+ DCHECK(!upload_job_);
+
+ GURL upload_url(kSystemLogUploadUrl);
+ DCHECK(upload_url.is_valid());
+ upload_job_ = syslog_delegate_->CreateUploadJob(upload_url, this);
+
+ // Start a system log upload.
+ int file_number = 1;
+ if (system_logs) {
+ for (const auto& syslog_entry : (*system_logs)) {
+ std::map<std::string, std::string> header_fields;
+ scoped_ptr<std::string> data =
+ make_scoped_ptr(new std::string(syslog_entry.second));
+ header_fields.insert(
+ std::make_pair(kFileTypeHeaderName, kFileTypeLogFile));
+ header_fields.insert(std::make_pair(net::HttpRequestHeaders::kContentType,
+ kContentTypePlainText));
+ upload_job_->AddDataSegment(
+ base::StringPrintf(kNameFieldTemplate, file_number),
+ syslog_entry.first, header_fields, data.Pass());
+ ++file_number;
+ }
+ }
+ upload_job_->Start();
+}
+
+void SystemLogUploader::StartLogUpload() {
+ // Must be called on the main thread.
+ DCHECK(thread_checker_.CalledOnValidThread());
+
+ syslog_delegate_->LoadSystemLogs(base::Bind(
+ &SystemLogUploader::UploadSystemLogs, weak_factory_.GetWeakPtr()));
+}
+
+void SystemLogUploader::ScheduleNextSystemLogUpload(base::TimeDelta frequency) {
+ // Calculate when to fire off the next update.
+ base::TimeDelta delay = std::max(
+ (last_upload_attempt_ + frequency) - base::Time::NowFromSystemTime(),
+ base::TimeDelta());
+ task_runner_->PostDelayedTask(FROM_HERE,
+ base::Bind(&SystemLogUploader::StartLogUpload,
+ weak_factory_.GetWeakPtr()),
+ delay);
+}
+
+} // namespace policy

Powered by Google App Engine
This is Rietveld 408576698