Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/policy/system_log_uploader.h" | |
| 6 | |
| 7 #include <map> | |
| 5 #include <utility> | 8 #include <utility> |
| 6 | 9 |
| 7 #include "base/bind.h" | 10 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 9 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 10 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 11 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
| 12 #include "base/sequenced_task_runner.h" | 15 #include "base/sequenced_task_runner.h" |
| 13 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
| 15 #include "base/syslog_logging.h" | 18 #include "base/syslog_logging.h" |
| 16 #include "base/task_scheduler/post_task.h" | 19 #include "base/task_scheduler/post_task.h" |
| 17 #include "chrome/browser/browser_process.h" | 20 #include "chrome/browser/browser_process.h" |
| 18 #include "chrome/browser/chromeos/policy/upload_job_impl.h" | 21 #include "chrome/browser/chromeos/policy/upload_job_impl.h" |
| 19 #include "chrome/browser/chromeos/settings/device_oauth2_token_service.h" | 22 #include "chrome/browser/chromeos/settings/device_oauth2_token_service.h" |
| 20 #include "chrome/browser/chromeos/settings/device_oauth2_token_service_factory.h " | 23 #include "chrome/browser/chromeos/settings/device_oauth2_token_service_factory.h " |
| 21 #include "chrome/common/chrome_switches.h" | 24 #include "chrome/common/chrome_switches.h" |
| 22 #include "components/feedback/anonymizer_tool.h" | 25 #include "components/feedback/anonymizer_tool.h" |
| 23 #include "components/policy/core/browser/browser_policy_connector.h" | 26 #include "components/policy/core/browser/browser_policy_connector.h" |
| 24 #include "net/http/http_request_headers.h" | 27 #include "net/http/http_request_headers.h" |
| 25 #include "system_log_uploader.h" | |
| 26 | 28 |
| 27 namespace { | 29 namespace { |
| 28 // The maximum number of successive retries. | 30 // The maximum number of successive retries. |
| 29 const int kMaxNumRetries = 1; | 31 const int kMaxNumRetries = 1; |
| 30 | 32 |
| 31 // String constant defining the url tail we upload system logs to. | 33 // String constant defining the url tail we upload system logs to. |
| 32 const char* kSystemLogUploadUrlTail = "/upload"; | 34 const char* kSystemLogUploadUrlTail = "/upload"; |
| 33 | 35 |
| 34 // The file names of the system logs to upload. | 36 // The file names of the system logs to upload. |
| 35 // Note: do not add anything to this list without checking for PII in the file. | 37 // Note: do not add anything to this list without checking for PII in the file. |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 193 // immediate future). | 195 // immediate future). |
| 194 ScheduleNextSystemLogUpload(upload_frequency_); | 196 ScheduleNextSystemLogUpload(upload_frequency_); |
| 195 } | 197 } |
| 196 | 198 |
| 197 SystemLogUploader::~SystemLogUploader() {} | 199 SystemLogUploader::~SystemLogUploader() {} |
| 198 | 200 |
| 199 void SystemLogUploader::OnSuccess() { | 201 void SystemLogUploader::OnSuccess() { |
| 200 SYSLOG(INFO) << "Upload successful."; | 202 SYSLOG(INFO) << "Upload successful."; |
| 201 upload_job_.reset(); | 203 upload_job_.reset(); |
| 202 last_upload_attempt_ = base::Time::NowFromSystemTime(); | 204 last_upload_attempt_ = base::Time::NowFromSystemTime(); |
| 205 log_upload_in_progress_ = false; | |
| 203 retry_count_ = 0; | 206 retry_count_ = 0; |
| 204 | 207 |
| 205 // On successful log upload schedule the next log upload after | 208 // On successful log upload schedule the next log upload after |
| 206 // upload_frequency_ time from now. | 209 // upload_frequency_ time from now. |
| 207 ScheduleNextSystemLogUpload(upload_frequency_); | 210 ScheduleNextSystemLogUpload(upload_frequency_); |
| 208 } | 211 } |
| 209 | 212 |
| 210 void SystemLogUploader::OnFailure(UploadJob::ErrorCode error_code) { | 213 void SystemLogUploader::OnFailure(UploadJob::ErrorCode error_code) { |
| 211 upload_job_.reset(); | 214 upload_job_.reset(); |
| 212 last_upload_attempt_ = base::Time::NowFromSystemTime(); | 215 last_upload_attempt_ = base::Time::NowFromSystemTime(); |
| 216 log_upload_in_progress_ = false; | |
| 213 | 217 |
| 214 // If we have hit the maximum number of retries, terminate this upload | 218 // If we have hit the maximum number of retries, terminate this upload |
| 215 // attempt and schedule the next one using the normal delay. Otherwise, retry | 219 // attempt and schedule the next one using the normal delay. Otherwise, retry |
| 216 // uploading after kErrorUploadDelayMs milliseconds. | 220 // uploading after kErrorUploadDelayMs milliseconds. |
| 217 if (retry_count_++ < kMaxNumRetries) { | 221 if (retry_count_++ < kMaxNumRetries) { |
| 218 SYSLOG(ERROR) << "Upload failed with error code " << error_code | 222 SYSLOG(ERROR) << "Upload failed with error code " << error_code |
| 219 << ", retrying later."; | 223 << ", retrying later."; |
| 220 ScheduleNextSystemLogUpload( | 224 ScheduleNextSystemLogUpload( |
| 221 base::TimeDelta::FromMilliseconds(kErrorUploadDelayMs)); | 225 base::TimeDelta::FromMilliseconds(kErrorUploadDelayMs)); |
| 222 } else { | 226 } else { |
| 223 // No more retries. | 227 // No more retries. |
| 224 SYSLOG(ERROR) << "Upload failed with error code " << error_code | 228 SYSLOG(ERROR) << "Upload failed with error code " << error_code |
| 225 << ", no more retries."; | 229 << ", no more retries."; |
| 226 retry_count_ = 0; | 230 retry_count_ = 0; |
| 227 ScheduleNextSystemLogUpload(upload_frequency_); | 231 ScheduleNextSystemLogUpload(upload_frequency_); |
| 228 } | 232 } |
| 229 } | 233 } |
| 230 | 234 |
| 231 // static | 235 // static |
| 232 std::string SystemLogUploader::RemoveSensitiveData( | 236 std::string SystemLogUploader::RemoveSensitiveData( |
| 233 feedback::AnonymizerTool* const anonymizer, | 237 feedback::AnonymizerTool* const anonymizer, |
| 234 const std::string& data) { | 238 const std::string& data) { |
| 235 return anonymizer->Anonymize(data); | 239 return anonymizer->Anonymize(data); |
| 236 } | 240 } |
| 237 | 241 |
| 242 void SystemLogUploader::ScheduleNextSystemLogUploadImmediately() { | |
| 243 ScheduleNextSystemLogUpload(base::TimeDelta()); | |
| 244 } | |
| 245 | |
| 238 void SystemLogUploader::RefreshUploadSettings() { | 246 void SystemLogUploader::RefreshUploadSettings() { |
| 239 // Attempt to fetch the current value of the reporting settings. | 247 // Attempt to fetch the current value of the reporting settings. |
| 240 // If trusted values are not available, register this function to be called | 248 // If trusted values are not available, register this function to be called |
| 241 // back when they are available. | 249 // back when they are available. |
| 242 chromeos::CrosSettings* settings = chromeos::CrosSettings::Get(); | 250 chromeos::CrosSettings* settings = chromeos::CrosSettings::Get(); |
| 243 if (chromeos::CrosSettingsProvider::TRUSTED != | 251 if (chromeos::CrosSettingsProvider::TRUSTED != |
| 244 settings->PrepareTrustedValues( | 252 settings->PrepareTrustedValues( |
| 245 base::Bind(&SystemLogUploader::RefreshUploadSettings, | 253 base::Bind(&SystemLogUploader::RefreshUploadSettings, |
| 246 weak_factory_.GetWeakPtr()))) { | 254 weak_factory_.GetWeakPtr()))) { |
| 247 return; | 255 return; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 282 } | 290 } |
| 283 upload_job_->Start(); | 291 upload_job_->Start(); |
| 284 } | 292 } |
| 285 | 293 |
| 286 void SystemLogUploader::StartLogUpload() { | 294 void SystemLogUploader::StartLogUpload() { |
| 287 // Must be called on the main thread. | 295 // Must be called on the main thread. |
| 288 DCHECK(thread_checker_.CalledOnValidThread()); | 296 DCHECK(thread_checker_.CalledOnValidThread()); |
| 289 | 297 |
| 290 if (upload_enabled_) { | 298 if (upload_enabled_) { |
| 291 SYSLOG(INFO) << "Starting system log upload."; | 299 SYSLOG(INFO) << "Starting system log upload."; |
| 300 log_upload_in_progress_ = true; | |
| 292 syslog_delegate_->LoadSystemLogs(base::Bind( | 301 syslog_delegate_->LoadSystemLogs(base::Bind( |
| 293 &SystemLogUploader::UploadSystemLogs, weak_factory_.GetWeakPtr())); | 302 &SystemLogUploader::UploadSystemLogs, weak_factory_.GetWeakPtr())); |
| 294 } else { | 303 } else { |
| 295 // If upload is disabled, schedule the next attempt after 12h. | 304 // If upload is disabled, schedule the next attempt after 12h. |
| 305 SYSLOG(INFO) << "System log upload is disabled, rescheduling."; | |
| 296 retry_count_ = 0; | 306 retry_count_ = 0; |
| 297 last_upload_attempt_ = base::Time::NowFromSystemTime(); | 307 last_upload_attempt_ = base::Time::NowFromSystemTime(); |
| 298 ScheduleNextSystemLogUpload(upload_frequency_); | 308 ScheduleNextSystemLogUpload(upload_frequency_); |
| 299 } | 309 } |
| 300 } | 310 } |
| 301 | 311 |
| 302 void SystemLogUploader::ScheduleNextSystemLogUpload(base::TimeDelta frequency) { | 312 void SystemLogUploader::ScheduleNextSystemLogUpload(base::TimeDelta frequency) { |
| 313 // Don't schedule a new system log upload if there's a log upload in progress | |
| 314 // (it will be scheduled once the current one completes). | |
| 315 if (log_upload_in_progress_) | |
| 316 return; | |
| 317 | |
| 303 // Calculate when to fire off the next update. | 318 // Calculate when to fire off the next update. |
| 304 base::TimeDelta delay = std::max( | 319 base::TimeDelta delay = std::max( |
| 305 (last_upload_attempt_ + frequency) - base::Time::NowFromSystemTime(), | 320 (last_upload_attempt_ + frequency) - base::Time::NowFromSystemTime(), |
| 306 base::TimeDelta()); | 321 base::TimeDelta()); |
| 307 SYSLOG(INFO) << "Scheduling next system log upload " << delay << " from now."; | 322 SYSLOG(INFO) << "Scheduling next system log upload " << delay << " from now."; |
| 308 // Ensure that we never have more than one pending delayed task. | 323 // Ensure that we never have more than one pending delayed task. |
| 309 weak_factory_.InvalidateWeakPtrs(); | 324 weak_factory_.InvalidateWeakPtrs(); |
|
Andrew T Wilson (Slow)
2017/03/21 18:03:42
I thought there was a race condition in this code,
Ivan Šandrk
2017/03/22 20:20:02
Done.
| |
| 310 task_runner_->PostDelayedTask(FROM_HERE, | 325 task_runner_->PostDelayedTask(FROM_HERE, |
| 311 base::Bind(&SystemLogUploader::StartLogUpload, | 326 base::Bind(&SystemLogUploader::StartLogUpload, |
| 312 weak_factory_.GetWeakPtr()), | 327 weak_factory_.GetWeakPtr()), |
| 313 delay); | 328 delay); |
| 314 } | 329 } |
| 315 | 330 |
| 316 } // namespace policy | 331 } // namespace policy |
| OLD | NEW |