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