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..86c11dc0cc878b7fb15dcbec8ea491e427288a68 |
--- /dev/null |
+++ b/chrome/browser/chromeos/policy/system_log_upload_job.cc |
@@ -0,0 +1,101 @@ |
+// 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/callback_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 "content/public/browser/browser_thread.h" |
+#include "net/http/http_request_headers.h" |
+ |
+namespace { |
+// String constant defining the upload url. |
Andrew T Wilson (Slow)
2015/07/17 15:43:57
So, I would not call it the "default upload url" b
Polina Bondarenko
2015/07/23 14:27:31
Done.
|
+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%d"; |
+} // namespace |
+ |
+namespace policy { |
+ |
+SystemLogUploadJob::SystemLogUploadJob(scoped_ptr<Delegate> syslog_delegate, |
+ const base::Closure& succeeded_callback, |
+ const base::Closure& failed_callback) |
+ : succeeded_callback_(succeeded_callback), |
+ failed_callback_(failed_callback), |
+ syslog_delegate_(syslog_delegate.Pass()), |
+ weak_factory_(this) { |
+ DCHECK(syslog_delegate_); |
+} |
+ |
+SystemLogUploadJob::~SystemLogUploadJob() { |
+} |
+ |
+void SystemLogUploadJob::OnSuccess() { |
+ // This callback can destroy the system log upload job. |
+ DCHECK(!succeeded_callback_.is_null()); |
+ base::ResetAndReturn(&succeeded_callback_).Run(); |
Andrew T Wilson (Slow)
2015/07/17 15:43:57
comment that we may be freed now.
Polina Bondarenko
2015/07/23 14:27:31
Done.
|
+} |
+ |
+void SystemLogUploadJob::OnFailure(UploadJob::ErrorCode error_code) { |
+ // This callback can destroy the system log upload job. |
+ DCHECK(!failed_callback_.is_null()); |
+ base::ResetAndReturn(&failed_callback_).Run(); |
Andrew T Wilson (Slow)
2015/07/17 15:43:57
comment that the callback may free this object so
Polina Bondarenko
2015/07/23 14:27:31
Done.
|
+} |
+ |
+void SystemLogUploadJob::StartLoadSystemLogs() { |
+ syslog_delegate_->LoadSystemLogs(base::Bind( |
+ &SystemLogUploadJob::StartUploadSystemLogs, weak_factory_.GetWeakPtr())); |
+} |
+ |
+void SystemLogUploadJob::StartUploadSystemLogs(const SystemLogs* system_logs) { |
+ // Must be called on the main thread. |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
Andrew T Wilson (Slow)
2015/07/17 15:43:57
The more I think about this, I think what you real
Polina Bondarenko
2015/07/23 14:27:31
Done.
|
+ |
+ 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 SystemLogUploadJob::Run() { |
+ // Ensure nobody calls Run() multiple times. |
+ DCHECK(!upload_job_); |
+ |
+ GURL upload_url(kDefaultUploadUrl); |
+ DCHECK(upload_url.is_valid()); |
+ |
+ upload_job_ = syslog_delegate_->CreateUploadJob(upload_url, this); |
+ DCHECK(upload_job_); |
+ |
+ StartLoadSystemLogs(); |
+} |
+ |
+} // namespace policy |