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

Unified Diff: chrome/browser/chromeos/policy/system_log_upload_job.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, 6 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_upload_job.cc
diff --git a/chrome/browser/chromeos/policy/system_log_upload_job.cc b/chrome/browser/chromeos/policy/system_log_upload_job.cc
new file mode 100644
index 0000000000000000000000000000000000000000..dbbfd296eb4624f9bb6f43ef239bf2a0c4504d97
--- /dev/null
+++ b/chrome/browser/chromeos/policy/system_log_upload_job.cc
@@ -0,0 +1,99 @@
+// 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/thread_task_runner_handle.h"
+#include "chrome/browser/chromeos/policy/system_log_upload_job.h"
+#include "net/http/http_request_headers.h"
+
+namespace {
+// String constant defining the upload url.
+const char* kDefaultUploadUrl =
+ "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%zu";
+} // namespace
+
+namespace policy {
+
+SystemLogUploadJob::SystemLogUploadJob(scoped_ptr<Delegate> syslog_delegate)
+ : upload_url_(GURL(kDefaultUploadUrl)),
+ syslog_delegate_(syslog_delegate.Pass()),
+ weak_factory_(this) {
+ DCHECK(syslog_delegate_);
+}
+
+SystemLogUploadJob::~SystemLogUploadJob() {
+}
+
+void SystemLogUploadJob::OnSuccess() {
+ base::ThreadTaskRunnerHandle::Get()->PostTask(
+ FROM_HERE, base::Bind(succeeded_callback_));
+}
+
+void SystemLogUploadJob::OnFailure(UploadJob::ErrorCode error_code) {
+ base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
+ base::Bind(failed_callback_));
+}
+
+void SystemLogUploadJob::StartLoadSystemLogs() {
+ syslog_delegate_->LoadSystemLogs(base::Bind(
+ &SystemLogUploadJob::StartUploadSystemLogs, weak_factory_.GetWeakPtr()));
+}
+
+void SystemLogUploadJob::StartUploadSystemLogs(const SystemLogs* system_logs) {
+ size_t 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 SystemLogUploadJob::Run(const base::Closure& succeeded_callback,
+ const base::Closure& failed_callback) {
+ // Ensure nobody calls Run() multiple times.
+ DCHECK(!upload_job_);
+
+ succeeded_callback_ = succeeded_callback;
+ failed_callback_ = failed_callback;
+
+ // Immediately fail if the upload url is invalid.
+ if (!upload_url_.is_valid()) {
+ LOG(ERROR) << upload_url_ << " is not a valid URL.";
+ base::Bind(failed_callback_);
+ return;
+ }
+
+ upload_job_ = syslog_delegate_->CreateUploadJob(upload_url_, this);
+ DCHECK(upload_job_);
+
+ StartLoadSystemLogs();
+}
+
+} // namespace policy

Powered by Google App Engine
This is Rietveld 408576698