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/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[] = {"/var/log/bios_info.txt", | |
| 49 "/var/log/chrome/chrome", | |
| 50 "/var/log/eventlog.txt", | |
| 51 "/var/log/messages", | |
| 52 "/var/log/net.log", | |
| 53 "/var/log/platform_info.txt", | |
| 54 "/var/log/ui/ui.LATEST", | |
| 55 "/var/log/update_engine.log"}; | |
| 56 | |
| 57 // Reads the system log files as binary files, stores the files as pairs | |
| 58 // (file name, data) and returns. Called on blocking thread. | |
| 59 policy::SystemLogUploader::SystemLogs ReadFiles() { | |
| 60 policy::SystemLogUploader::SystemLogs system_logs; | |
|
Andrew T Wilson (Slow)
2015/08/03 09:55:50
This is going to make multiple copies of all of th
Polina Bondarenko
2015/08/03 11:53:13
Done.
| |
| 61 for (auto const file_path : kSystemLogFileNames) { | |
| 62 if (!base::PathExists(base::FilePath(file_path))) | |
| 63 continue; | |
| 64 std::string data = std::string(); | |
| 65 if (!base::ReadFileToString(base::FilePath(file_path), &data)) { | |
| 66 LOG(ERROR) << "Failed to read the system log file from the disk " | |
| 67 << file_path << std::endl; | |
| 68 } | |
| 69 // TODO(pbond): add check |data| for common PII (email, IP addresses and | |
| 70 // etc.) and not upload |data| if any is found. http://crbug.com/515879. | |
|
Andrew T Wilson (Slow)
2015/08/03 09:55:50
Better than not uploading data would be to modify
Polina Bondarenko
2015/08/03 11:53:13
Done.
| |
| 71 system_logs.push_back(std::make_pair(file_path, data)); | |
| 72 } | |
| 73 return system_logs; | |
| 74 } | |
| 75 | |
| 76 // An implementation of the |SystemLogUploader::Delegate|, that is used to | |
| 77 // create an upload job and load system logs from the disk. | |
| 78 class SystemLogDelegate : public policy::SystemLogUploader::Delegate { | |
| 79 public: | |
| 80 SystemLogDelegate(); | |
| 81 ~SystemLogDelegate() override; | |
| 82 | |
| 83 // SystemLogUploader::Delegate: | |
| 84 void LoadSystemLogs(const LogUploadCallback& upload_callback) override; | |
| 85 | |
| 86 scoped_ptr<policy::UploadJob> CreateUploadJob( | |
| 87 const GURL& upload_url, | |
| 88 policy::UploadJob::Delegate* delegate) override; | |
| 89 | |
| 90 private: | |
| 91 DISALLOW_COPY_AND_ASSIGN(SystemLogDelegate); | |
| 92 }; | |
| 93 | |
| 94 SystemLogDelegate::SystemLogDelegate() { | |
| 95 } | |
| 96 | |
| 97 SystemLogDelegate::~SystemLogDelegate() { | |
| 98 } | |
| 99 | |
| 100 void SystemLogDelegate::LoadSystemLogs( | |
| 101 const LogUploadCallback& upload_callback) { | |
| 102 // Run ReadFiles() in the thread that interacts with the file system and | |
| 103 // return system logs to |upload_callback| on the current thread. | |
| 104 base::PostTaskAndReplyWithResult(content::BrowserThread::GetBlockingPool(), | |
| 105 FROM_HERE, base::Bind(&ReadFiles), | |
| 106 upload_callback); | |
| 107 } | |
| 108 | |
| 109 scoped_ptr<policy::UploadJob> SystemLogDelegate::CreateUploadJob( | |
| 110 const GURL& upload_url, | |
| 111 policy::UploadJob::Delegate* delegate) { | |
| 112 chromeos::DeviceOAuth2TokenService* device_oauth2_token_service = | |
| 113 chromeos::DeviceOAuth2TokenServiceFactory::Get(); | |
| 114 | |
| 115 scoped_refptr<net::URLRequestContextGetter> system_request_context = | |
| 116 g_browser_process->system_request_context(); | |
| 117 std::string robot_account_id = | |
| 118 device_oauth2_token_service->GetRobotAccountId(); | |
| 119 return scoped_ptr<policy::UploadJob>(new policy::UploadJobImpl( | |
| 120 upload_url, robot_account_id, device_oauth2_token_service, | |
| 121 system_request_context, delegate, | |
| 122 make_scoped_ptr(new policy::UploadJobImpl::RandomMimeBoundaryGenerator))); | |
| 123 } | |
| 124 | |
| 125 } // namespace | |
| 126 | |
| 127 namespace policy { | |
| 128 | |
| 129 SystemLogUploader::SystemLogUploader( | |
| 130 scoped_ptr<Delegate> syslog_delegate, | |
| 131 const scoped_refptr<base::SequencedTaskRunner>& task_runner) | |
| 132 : retry_count_(0), | |
| 133 upload_frequency_( | |
| 134 base::TimeDelta::FromMilliseconds(kDefaultUploadDelayMs)), | |
| 135 task_runner_(task_runner), | |
| 136 syslog_delegate_(syslog_delegate.Pass()), | |
| 137 weak_factory_(this) { | |
| 138 if (!syslog_delegate_) | |
| 139 syslog_delegate_.reset(new SystemLogDelegate()); | |
| 140 DCHECK(syslog_delegate_); | |
| 141 // Immediately schedule the next system log upload (last_upload_attempt_ is | |
| 142 // set to the start of the epoch, so this will trigger an update upload in the | |
| 143 // immediate future). | |
| 144 ScheduleNextSystemLogUpload(upload_frequency_); | |
| 145 } | |
| 146 | |
| 147 SystemLogUploader::~SystemLogUploader() { | |
| 148 } | |
| 149 | |
| 150 void SystemLogUploader::OnSuccess() { | |
| 151 upload_job_.reset(); | |
| 152 last_upload_attempt_ = base::Time::NowFromSystemTime(); | |
| 153 retry_count_ = 0; | |
| 154 | |
| 155 // On successful log upload schedule the next log upload after | |
| 156 // upload_frequency_ time from now. | |
| 157 ScheduleNextSystemLogUpload(upload_frequency_); | |
| 158 } | |
| 159 | |
| 160 void SystemLogUploader::OnFailure(UploadJob::ErrorCode error_code) { | |
| 161 upload_job_.reset(); | |
| 162 last_upload_attempt_ = base::Time::NowFromSystemTime(); | |
| 163 | |
| 164 // If we have hit the maximum number of retries, terminate this upload | |
| 165 // attempt and schedule the next one using the normal delay. Otherwise, retry | |
| 166 // uploading after kErrorUploadDelayMs milliseconds. | |
| 167 if (retry_count_++ < kMaxNumRetries) { | |
| 168 ScheduleNextSystemLogUpload( | |
| 169 base::TimeDelta::FromMilliseconds(kErrorUploadDelayMs)); | |
| 170 } else { | |
| 171 // No more retries. | |
| 172 retry_count_ = 0; | |
| 173 ScheduleNextSystemLogUpload(upload_frequency_); | |
| 174 } | |
| 175 } | |
| 176 | |
| 177 void SystemLogUploader::UploadSystemLogs(const SystemLogs& system_logs) { | |
| 178 // Must be called on the main thread. | |
| 179 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 180 DCHECK(!upload_job_); | |
| 181 | |
| 182 GURL upload_url(kSystemLogUploadUrl); | |
| 183 DCHECK(upload_url.is_valid()); | |
| 184 upload_job_ = syslog_delegate_->CreateUploadJob(upload_url, this); | |
| 185 | |
| 186 // Start a system log upload. | |
| 187 int file_number = 1; | |
| 188 for (const auto& syslog_entry : system_logs) { | |
| 189 std::map<std::string, std::string> header_fields; | |
| 190 scoped_ptr<std::string> data = | |
| 191 make_scoped_ptr(new std::string(syslog_entry.second)); | |
| 192 header_fields.insert(std::make_pair(kFileTypeHeaderName, kFileTypeLogFile)); | |
| 193 header_fields.insert(std::make_pair(net::HttpRequestHeaders::kContentType, | |
| 194 kContentTypePlainText)); | |
| 195 upload_job_->AddDataSegment( | |
| 196 base::StringPrintf(kNameFieldTemplate, file_number), syslog_entry.first, | |
| 197 header_fields, data.Pass()); | |
| 198 ++file_number; | |
| 199 } | |
| 200 upload_job_->Start(); | |
| 201 } | |
| 202 | |
| 203 void SystemLogUploader::StartLogUpload() { | |
| 204 // Must be called on the main thread. | |
| 205 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 206 | |
| 207 syslog_delegate_->LoadSystemLogs(base::Bind( | |
| 208 &SystemLogUploader::UploadSystemLogs, weak_factory_.GetWeakPtr())); | |
| 209 } | |
| 210 | |
| 211 void SystemLogUploader::ScheduleNextSystemLogUpload(base::TimeDelta frequency) { | |
|
Andrew T Wilson (Slow)
2015/08/03 09:55:50
BTW, would be nice if we had a DCHECK in place to
Polina Bondarenko
2015/08/03 11:53:13
Done, added weak_factory_.reset, because we use th
| |
| 212 // Calculate when to fire off the next update. | |
| 213 base::TimeDelta delay = std::max( | |
| 214 (last_upload_attempt_ + frequency) - base::Time::NowFromSystemTime(), | |
| 215 base::TimeDelta()); | |
| 216 task_runner_->PostDelayedTask(FROM_HERE, | |
| 217 base::Bind(&SystemLogUploader::StartLogUpload, | |
| 218 weak_factory_.GetWeakPtr()), | |
| 219 delay); | |
| 220 } | |
| 221 | |
| 222 } // namespace policy | |
| OLD | NEW |