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/thread_task_runner_handle.h" |
| 10 #include "chrome/browser/chromeos/policy/system_log_upload_job.h" |
| 11 #include "net/http/http_request_headers.h" |
| 12 |
| 13 namespace { |
| 14 // String constant defining the upload url. |
| 15 const char* kDefaultUploadUrl = |
| 16 "https://m.google.com/devicemanagement/data/api/upload"; |
| 17 |
| 18 // String constant identifying the header field which stores the file type. |
| 19 const char* kFileTypeHeaderName = "File-Type"; |
| 20 |
| 21 // String constant signalling that the data segment contains log files. |
| 22 const char* const kFileTypeLogFile = "log_file"; |
| 23 |
| 24 // String constant signalling that the segment contains a plain text. |
| 25 const char* const kContentTypePlainText = "text/plain"; |
| 26 |
| 27 // Template string constant for populating the name field. |
| 28 const char* const kNameFieldTemplate = "file%zu"; |
| 29 } // namespace |
| 30 |
| 31 namespace policy { |
| 32 |
| 33 SystemLogUploadJob::SystemLogUploadJob(scoped_ptr<Delegate> syslog_delegate) |
| 34 : upload_url_(GURL(kDefaultUploadUrl)), |
| 35 syslog_delegate_(syslog_delegate.Pass()), |
| 36 weak_factory_(this) { |
| 37 DCHECK(syslog_delegate_); |
| 38 } |
| 39 |
| 40 SystemLogUploadJob::~SystemLogUploadJob() { |
| 41 } |
| 42 |
| 43 void SystemLogUploadJob::OnSuccess() { |
| 44 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 45 FROM_HERE, base::Bind(succeeded_callback_)); |
| 46 } |
| 47 |
| 48 void SystemLogUploadJob::OnFailure(UploadJob::ErrorCode error_code) { |
| 49 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 50 base::Bind(failed_callback_)); |
| 51 } |
| 52 |
| 53 void SystemLogUploadJob::StartLoadSystemLogs() { |
| 54 syslog_delegate_->LoadSystemLogs(base::Bind( |
| 55 &SystemLogUploadJob::StartUploadSystemLogs, weak_factory_.GetWeakPtr())); |
| 56 } |
| 57 |
| 58 void SystemLogUploadJob::StartUploadSystemLogs(const SystemLogs* system_logs) { |
| 59 size_t file_number = 1; |
| 60 if (system_logs) { |
| 61 for (const auto& syslog_entry : (*system_logs)) { |
| 62 std::map<std::string, std::string> header_fields; |
| 63 scoped_ptr<std::string> data = |
| 64 make_scoped_ptr(new std::string(syslog_entry.second)); |
| 65 header_fields.insert( |
| 66 std::make_pair(kFileTypeHeaderName, kFileTypeLogFile)); |
| 67 header_fields.insert(std::make_pair(net::HttpRequestHeaders::kContentType, |
| 68 kContentTypePlainText)); |
| 69 upload_job_->AddDataSegment( |
| 70 base::StringPrintf(kNameFieldTemplate, file_number), |
| 71 syslog_entry.first, header_fields, data.Pass()); |
| 72 ++file_number; |
| 73 } |
| 74 } |
| 75 upload_job_->Start(); |
| 76 } |
| 77 |
| 78 void SystemLogUploadJob::Run(const base::Closure& succeeded_callback, |
| 79 const base::Closure& failed_callback) { |
| 80 // Ensure nobody calls Run() multiple times. |
| 81 DCHECK(!upload_job_); |
| 82 |
| 83 succeeded_callback_ = succeeded_callback; |
| 84 failed_callback_ = failed_callback; |
| 85 |
| 86 // Immediately fail if the upload url is invalid. |
| 87 if (!upload_url_.is_valid()) { |
| 88 LOG(ERROR) << upload_url_ << " is not a valid URL."; |
| 89 base::Bind(failed_callback_); |
| 90 return; |
| 91 } |
| 92 |
| 93 upload_job_ = syslog_delegate_->CreateUploadJob(upload_url_, this); |
| 94 DCHECK(upload_job_); |
| 95 |
| 96 StartLoadSystemLogs(); |
| 97 } |
| 98 |
| 99 } // namespace policy |
OLD | NEW |