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, 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_
| |
56 } | |
57 | |
58 void SystemLogUploadJob::StartUploadSystemLogs( | |
59 const std::map<std::string, std::string>& system_logs) { | |
60 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.
| |
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(std::make_pair(kFileTypeHeaderName, kFileTypeLogFile)); | |
66 header_fields.insert(std::make_pair(net::HttpRequestHeaders::kContentType, | |
67 kContentTypePlainText)); | |
68 upload_job_->AddDataSegment(base::StringPrintf(kNameFieldTemplate, i), | |
69 syslog_entry.first, header_fields, data.Pass()); | |
70 ++i; | |
71 } | |
72 upload_job_->Start(); | |
73 } | |
74 | |
75 void SystemLogUploadJob::Run(const base::Closure& succeeded_callback, | |
76 const base::Closure& failed_callback) { | |
77 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.
| |
78 failed_callback_ = failed_callback; | |
79 | |
80 // Immediately fail if the upload url is invalid. | |
81 if (!upload_url_.is_valid()) { | |
82 LOG(ERROR) << upload_url_ << " is not a valid URL."; | |
83 base::Bind(failed_callback_); | |
84 return; | |
85 } | |
86 | |
87 upload_job_ = syslog_delegate_->CreateUploadJob(upload_url_, this); | |
88 DCHECK(upload_job_); | |
89 | |
90 StartLoadSystemLogs(); | |
91 } | |
92 | |
93 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.
| |
94 weak_factory_.InvalidateWeakPtrs(); | |
95 } | |
96 | |
97 } // namespace policy | |
OLD | NEW |