Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(665)

Side by Side Diff: chrome/browser/chromeos/policy/system_log_uploader.cc

Issue 1193333017: Added system log uploader. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed failed browser tests. Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/files/file_util.h"
8 #include "base/location.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/task_runner_util.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/chromeos/policy/system_log_uploader.h"
13 #include "chrome/browser/chromeos/policy/upload_job_impl.h"
14 #include "chrome/browser/chromeos/settings/device_oauth2_token_service.h"
15 #include "chrome/browser/chromeos/settings/device_oauth2_token_service_factory.h "
16 #include "content/public/browser/browser_thread.h"
17 #include "net/http/http_request_headers.h"
18
19 namespace {
20 // Determines the time between log uploads.
21 const int64 kDefaultUploadDelayMs = 12 * 60 * 60 * 1000; // 12 hours
22
23 // Determines the time, measured from the time of last failed upload,
24 // after which the log upload is retried.
25 const int64 kErrorUploadDelayMs = 120 * 1000; // 120 seconds
26
27 // The maximum number of successive retries.
28 const int kMaxNumRetries = 1;
29
30 // String constant defining the url we upload system logs to.
31 const char* kSystemLogUploadUrl =
32 "https://m.google.com/devicemanagement/data/api/upload";
33
34 // String constant identifying the header field which stores the file type.
35 const char* kFileTypeHeaderName = "File-Type";
36
37 // String constant signalling that the data segment contains log files.
38 const char* const kFileTypeLogFile = "log_file";
39
40 // String constant signalling that the segment contains a plain text.
41 const char* const kContentTypePlainText = "text/plain";
42
43 // Template string constant for populating the name field.
44 const char* const kNameFieldTemplate = "file%d";
45
46 // The file names of the system logs to upload.
47 // Note: do not add anything to this list without checking for PII in the file.
48 const char* const kSystemLogFileNames[] = {
49 "/var/log/bios_info.txt", "/var/log/chrome/chrome",
50 "/var/log/eventlog.txt", "/var/log/messages",
51 "/var/log/net.log", "/var/log/platform_info.txt",
52 "/var/log/ui/ui.LATEST", "/var/log/update_engine.log"};
53
54 // Reads the system log files as binary files, stores the files as pairs
55 // (file name, data) and returns. Called on blocking thread.
56 scoped_ptr<policy::SystemLogUploader::SystemLogs> ReadFiles() {
57 scoped_ptr<policy::SystemLogUploader::SystemLogs> system_logs(
58 new policy::SystemLogUploader::SystemLogs());
59 for (auto const file_path : kSystemLogFileNames) {
60 if (!base::PathExists(base::FilePath(file_path)))
61 continue;
62 std::string data = std::string();
63 if (!base::ReadFileToString(base::FilePath(file_path), &data)) {
64 LOG(ERROR) << "Failed to read the system log file from the disk "
65 << file_path << std::endl;
66 }
67 // TODO(pbond): add check |data| for common PII (email, IP addresses and
68 // etc.) and modify the |data| to remove/obfuscate the PII if any found.
69 // http://crbug.com/515879.
70 system_logs->push_back(std::make_pair(file_path, data));
71 }
72 return system_logs.Pass();
73 }
74
75 // An implementation of the |SystemLogUploader::Delegate|, that is used to
76 // create an upload job and load system logs from the disk.
77 class SystemLogDelegate : public policy::SystemLogUploader::Delegate {
78 public:
79 SystemLogDelegate();
80 ~SystemLogDelegate() override;
81
82 // SystemLogUploader::Delegate:
83 void LoadSystemLogs(const LogUploadCallback& upload_callback) override;
84
85 scoped_ptr<policy::UploadJob> CreateUploadJob(
86 const GURL& upload_url,
87 policy::UploadJob::Delegate* delegate) override;
88
89 private:
90 DISALLOW_COPY_AND_ASSIGN(SystemLogDelegate);
91 };
92
93 SystemLogDelegate::SystemLogDelegate() {}
94
95 SystemLogDelegate::~SystemLogDelegate() {}
96
97 void SystemLogDelegate::LoadSystemLogs(
98 const LogUploadCallback& upload_callback) {
99 // Run ReadFiles() in the thread that interacts with the file system and
100 // return system logs to |upload_callback| on the current thread.
101 base::PostTaskAndReplyWithResult(content::BrowserThread::GetBlockingPool(),
102 FROM_HERE, base::Bind(&ReadFiles),
103 upload_callback);
104 }
105
106 scoped_ptr<policy::UploadJob> SystemLogDelegate::CreateUploadJob(
107 const GURL& upload_url,
108 policy::UploadJob::Delegate* delegate) {
109 chromeos::DeviceOAuth2TokenService* device_oauth2_token_service =
110 chromeos::DeviceOAuth2TokenServiceFactory::Get();
111
112 scoped_refptr<net::URLRequestContextGetter> system_request_context =
113 g_browser_process->system_request_context();
114 std::string robot_account_id =
115 device_oauth2_token_service->GetRobotAccountId();
116 return scoped_ptr<policy::UploadJob>(new policy::UploadJobImpl(
117 upload_url, robot_account_id, device_oauth2_token_service,
118 system_request_context, delegate,
119 make_scoped_ptr(new policy::UploadJobImpl::RandomMimeBoundaryGenerator)));
120 }
121
122 } // namespace
123
124 namespace policy {
125
126 SystemLogUploader::SystemLogUploader(
127 scoped_ptr<Delegate> syslog_delegate,
128 const scoped_refptr<base::SequencedTaskRunner>& task_runner)
129 : retry_count_(0),
130 upload_frequency_(
131 base::TimeDelta::FromMilliseconds(kDefaultUploadDelayMs)),
132 task_runner_(task_runner),
133 syslog_delegate_(syslog_delegate.Pass()),
134 weak_factory_(this) {
135 if (!syslog_delegate_)
136 syslog_delegate_.reset(new SystemLogDelegate());
137 DCHECK(syslog_delegate_);
138 // Immediately schedule the next system log upload (last_upload_attempt_ is
139 // set to the start of the epoch, so this will trigger an update upload in the
140 // immediate future).
141 ScheduleNextSystemLogUpload(upload_frequency_);
142 }
143
144 SystemLogUploader::~SystemLogUploader() {}
145
146 void SystemLogUploader::OnSuccess() {
147 upload_job_.reset();
148 last_upload_attempt_ = base::Time::NowFromSystemTime();
149 retry_count_ = 0;
150
151 // On successful log upload schedule the next log upload after
152 // upload_frequency_ time from now.
153 ScheduleNextSystemLogUpload(upload_frequency_);
154 }
155
156 void SystemLogUploader::OnFailure(UploadJob::ErrorCode error_code) {
157 upload_job_.reset();
158 last_upload_attempt_ = base::Time::NowFromSystemTime();
159
160 // If we have hit the maximum number of retries, terminate this upload
161 // attempt and schedule the next one using the normal delay. Otherwise, retry
162 // uploading after kErrorUploadDelayMs milliseconds.
163 if (retry_count_++ < kMaxNumRetries) {
164 ScheduleNextSystemLogUpload(
165 base::TimeDelta::FromMilliseconds(kErrorUploadDelayMs));
166 } else {
167 // No more retries.
168 retry_count_ = 0;
169 ScheduleNextSystemLogUpload(upload_frequency_);
170 }
171 }
172
173 void SystemLogUploader::UploadSystemLogs(scoped_ptr<SystemLogs> system_logs) {
174 // Must be called on the main thread.
175 DCHECK(thread_checker_.CalledOnValidThread());
176 DCHECK(!upload_job_);
177
178 GURL upload_url(kSystemLogUploadUrl);
179 DCHECK(upload_url.is_valid());
180 upload_job_ = syslog_delegate_->CreateUploadJob(upload_url, this);
181
182 // Start a system log upload.
183 int file_number = 1;
184 for (const auto& syslog_entry : *system_logs) {
185 std::map<std::string, std::string> header_fields;
186 scoped_ptr<std::string> data =
187 make_scoped_ptr(new std::string(syslog_entry.second));
188 header_fields.insert(std::make_pair(kFileTypeHeaderName, kFileTypeLogFile));
189 header_fields.insert(std::make_pair(net::HttpRequestHeaders::kContentType,
190 kContentTypePlainText));
191 upload_job_->AddDataSegment(
192 base::StringPrintf(kNameFieldTemplate, file_number), syslog_entry.first,
193 header_fields, data.Pass());
194 ++file_number;
195 }
196 upload_job_->Start();
197 }
198
199 void SystemLogUploader::StartLogUpload() {
200 // Must be called on the main thread.
201 DCHECK(thread_checker_.CalledOnValidThread());
202
203 syslog_delegate_->LoadSystemLogs(base::Bind(
204 &SystemLogUploader::UploadSystemLogs, weak_factory_.GetWeakPtr()));
205 }
206
207 void SystemLogUploader::ScheduleNextSystemLogUpload(base::TimeDelta frequency) {
208 // Calculate when to fire off the next update.
209 base::TimeDelta delay = std::max(
210 (last_upload_attempt_ + frequency) - base::Time::NowFromSystemTime(),
211 base::TimeDelta());
212 // Ensure that we never have more than one pending delayed task.
213 weak_factory_.InvalidateWeakPtrs();
214 task_runner_->PostDelayedTask(FROM_HERE,
215 base::Bind(&SystemLogUploader::StartLogUpload,
216 weak_factory_.GetWeakPtr()),
217 delay);
218 }
219
220 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/policy/system_log_uploader.h ('k') | chrome/chrome_browser_chromeos.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698