| 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/bind_helpers.h" | 6 #include "base/bind_helpers.h" |
| 7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 10 #include "base/task_runner_util.h" | 10 #include "base/task_runner_util.h" |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 const char* const SystemLogUploader::kNameFieldTemplate = "file%d"; | 125 const char* const SystemLogUploader::kNameFieldTemplate = "file%d"; |
| 126 | 126 |
| 127 SystemLogUploader::SystemLogUploader( | 127 SystemLogUploader::SystemLogUploader( |
| 128 scoped_ptr<Delegate> syslog_delegate, | 128 scoped_ptr<Delegate> syslog_delegate, |
| 129 const scoped_refptr<base::SequencedTaskRunner>& task_runner) | 129 const scoped_refptr<base::SequencedTaskRunner>& task_runner) |
| 130 : retry_count_(0), | 130 : retry_count_(0), |
| 131 upload_frequency_( | 131 upload_frequency_( |
| 132 base::TimeDelta::FromMilliseconds(kDefaultUploadDelayMs)), | 132 base::TimeDelta::FromMilliseconds(kDefaultUploadDelayMs)), |
| 133 task_runner_(task_runner), | 133 task_runner_(task_runner), |
| 134 syslog_delegate_(syslog_delegate.Pass()), | 134 syslog_delegate_(syslog_delegate.Pass()), |
| 135 upload_enabled_(false), |
| 135 weak_factory_(this) { | 136 weak_factory_(this) { |
| 136 if (!syslog_delegate_) | 137 if (!syslog_delegate_) |
| 137 syslog_delegate_.reset(new SystemLogDelegate()); | 138 syslog_delegate_.reset(new SystemLogDelegate()); |
| 138 DCHECK(syslog_delegate_); | 139 DCHECK(syslog_delegate_); |
| 140 |
| 141 // Watch for policy changes. |
| 142 upload_enabled_observer_ = chromeos::CrosSettings::Get()->AddSettingsObserver( |
| 143 chromeos::kLogUploadEnabled, |
| 144 base::Bind(&SystemLogUploader::RefreshUploadSettings, |
| 145 weak_factory_.GetWeakPtr())); |
| 146 |
| 147 // Fetch the current value of the policy. |
| 148 RefreshUploadSettings(); |
| 149 |
| 139 // Immediately schedule the next system log upload (last_upload_attempt_ is | 150 // Immediately schedule the next system log upload (last_upload_attempt_ is |
| 140 // set to the start of the epoch, so this will trigger an update upload in the | 151 // set to the start of the epoch, so this will trigger an update upload in the |
| 141 // immediate future). | 152 // immediate future). |
| 142 ScheduleNextSystemLogUpload(upload_frequency_); | 153 ScheduleNextSystemLogUpload(upload_frequency_); |
| 143 } | 154 } |
| 144 | 155 |
| 145 SystemLogUploader::~SystemLogUploader() {} | 156 SystemLogUploader::~SystemLogUploader() {} |
| 146 | 157 |
| 147 void SystemLogUploader::OnSuccess() { | 158 void SystemLogUploader::OnSuccess() { |
| 148 upload_job_.reset(); | 159 upload_job_.reset(); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 164 if (retry_count_++ < kMaxNumRetries) { | 175 if (retry_count_++ < kMaxNumRetries) { |
| 165 ScheduleNextSystemLogUpload( | 176 ScheduleNextSystemLogUpload( |
| 166 base::TimeDelta::FromMilliseconds(kErrorUploadDelayMs)); | 177 base::TimeDelta::FromMilliseconds(kErrorUploadDelayMs)); |
| 167 } else { | 178 } else { |
| 168 // No more retries. | 179 // No more retries. |
| 169 retry_count_ = 0; | 180 retry_count_ = 0; |
| 170 ScheduleNextSystemLogUpload(upload_frequency_); | 181 ScheduleNextSystemLogUpload(upload_frequency_); |
| 171 } | 182 } |
| 172 } | 183 } |
| 173 | 184 |
| 185 void SystemLogUploader::RefreshUploadSettings() { |
| 186 // Attempt to fetch the current value of the reporting settings. |
| 187 // If trusted values are not available, register this function to be called |
| 188 // back when they are available. |
| 189 chromeos::CrosSettings* settings = chromeos::CrosSettings::Get(); |
| 190 if (chromeos::CrosSettingsProvider::TRUSTED != |
| 191 settings->PrepareTrustedValues( |
| 192 base::Bind(&SystemLogUploader::RefreshUploadSettings, |
| 193 weak_factory_.GetWeakPtr()))) { |
| 194 return; |
| 195 } |
| 196 |
| 197 // CrosSettings are trusted - we want to use the last trusted values, by |
| 198 // default do not upload system logs. |
| 199 if (!settings->GetBoolean(chromeos::kLogUploadEnabled, &upload_enabled_)) |
| 200 upload_enabled_ = false; |
| 201 } |
| 202 |
| 174 void SystemLogUploader::UploadSystemLogs(scoped_ptr<SystemLogs> system_logs) { | 203 void SystemLogUploader::UploadSystemLogs(scoped_ptr<SystemLogs> system_logs) { |
| 175 // Must be called on the main thread. | 204 // Must be called on the main thread. |
| 176 DCHECK(thread_checker_.CalledOnValidThread()); | 205 DCHECK(thread_checker_.CalledOnValidThread()); |
| 177 DCHECK(!upload_job_); | 206 DCHECK(!upload_job_); |
| 178 | 207 |
| 179 GURL upload_url(kSystemLogUploadUrl); | 208 GURL upload_url(kSystemLogUploadUrl); |
| 180 DCHECK(upload_url.is_valid()); | 209 DCHECK(upload_url.is_valid()); |
| 181 upload_job_ = syslog_delegate_->CreateUploadJob(upload_url, this); | 210 upload_job_ = syslog_delegate_->CreateUploadJob(upload_url, this); |
| 182 | 211 |
| 183 // Start a system log upload. | 212 // Start a system log upload. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 194 header_fields, data.Pass()); | 223 header_fields, data.Pass()); |
| 195 ++file_number; | 224 ++file_number; |
| 196 } | 225 } |
| 197 upload_job_->Start(); | 226 upload_job_->Start(); |
| 198 } | 227 } |
| 199 | 228 |
| 200 void SystemLogUploader::StartLogUpload() { | 229 void SystemLogUploader::StartLogUpload() { |
| 201 // Must be called on the main thread. | 230 // Must be called on the main thread. |
| 202 DCHECK(thread_checker_.CalledOnValidThread()); | 231 DCHECK(thread_checker_.CalledOnValidThread()); |
| 203 | 232 |
| 204 syslog_delegate_->LoadSystemLogs(base::Bind( | 233 if (upload_enabled_) { |
| 205 &SystemLogUploader::UploadSystemLogs, weak_factory_.GetWeakPtr())); | 234 syslog_delegate_->LoadSystemLogs(base::Bind( |
| 235 &SystemLogUploader::UploadSystemLogs, weak_factory_.GetWeakPtr())); |
| 236 } else { |
| 237 // If upload is disabled, schedule the next attempt after 12h. |
| 238 retry_count_ = 0; |
| 239 ScheduleNextSystemLogUpload(upload_frequency_); |
| 240 } |
| 206 } | 241 } |
| 207 | 242 |
| 208 void SystemLogUploader::ScheduleNextSystemLogUpload(base::TimeDelta frequency) { | 243 void SystemLogUploader::ScheduleNextSystemLogUpload(base::TimeDelta frequency) { |
| 209 // Calculate when to fire off the next update. | 244 // Calculate when to fire off the next update. |
| 210 base::TimeDelta delay = std::max( | 245 base::TimeDelta delay = std::max( |
| 211 (last_upload_attempt_ + frequency) - base::Time::NowFromSystemTime(), | 246 (last_upload_attempt_ + frequency) - base::Time::NowFromSystemTime(), |
| 212 base::TimeDelta()); | 247 base::TimeDelta()); |
| 213 // Ensure that we never have more than one pending delayed task. | 248 // Ensure that we never have more than one pending delayed task. |
| 214 weak_factory_.InvalidateWeakPtrs(); | 249 weak_factory_.InvalidateWeakPtrs(); |
| 215 task_runner_->PostDelayedTask(FROM_HERE, | 250 task_runner_->PostDelayedTask(FROM_HERE, |
| 216 base::Bind(&SystemLogUploader::StartLogUpload, | 251 base::Bind(&SystemLogUploader::StartLogUpload, |
| 217 weak_factory_.GetWeakPtr()), | 252 weak_factory_.GetWeakPtr()), |
| 218 delay); | 253 delay); |
| 219 } | 254 } |
| 220 | 255 |
| 221 } // namespace policy | 256 } // namespace policy |
| OLD | NEW |