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