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