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..437fe5a7cb1413ce583dbc3cea631edce4ec6692 |
--- /dev/null |
+++ b/chrome/browser/chromeos/policy/system_log_upload_job.cc |
@@ -0,0 +1,97 @@ |
+// 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, base::Unretained(this))); |
Andrew T Wilson (Slow)
2015/06/29 15:06:09
Why use base::Unretained() instead of the weak fac
Polina Bondarenko
2015/07/02 15:28:04
Done, changed to weak_factory_
|
+} |
+ |
+void SystemLogUploadJob::StartUploadSystemLogs( |
+ const std::map<std::string, std::string>& system_logs) { |
+ size_t i = 1; |
Andrew T Wilson (Slow)
2015/06/29 15:06:09
nit: name this something like file_number or somet
Polina Bondarenko
2015/07/02 15:28:04
Done.
|
+ 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, i), |
+ syslog_entry.first, header_fields, data.Pass()); |
+ ++i; |
+ } |
+ upload_job_->Start(); |
+} |
+ |
+void SystemLogUploadJob::Run(const base::Closure& succeeded_callback, |
+ const base::Closure& failed_callback) { |
+ succeeded_callback_ = succeeded_callback; |
Andrew T Wilson (Slow)
2015/06/29 15:06:09
Should you add a DCHECK(!upload_job_) to ensure no
Polina Bondarenko
2015/07/02 15:28:04
Done, added a comment.
|
+ 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(); |
+} |
+ |
+void SystemLogUploadJob::Terminate() { |
Andrew T Wilson (Slow)
2015/06/29 15:06:09
What's the use case for calling this instead of ju
Polina Bondarenko
2015/07/02 15:28:03
Done, removed.
|
+ weak_factory_.InvalidateWeakPtrs(); |
+} |
+ |
+} // namespace policy |