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/status_uploader.h" | 5 #include "chrome/browser/chromeos/policy/status_uploader.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> |
8 #include <utility> | 9 #include <utility> |
9 | 10 |
10 #include "base/bind.h" | 11 #include "base/bind.h" |
11 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
12 #include "base/location.h" | 13 #include "base/location.h" |
13 #include "base/sequenced_task_runner.h" | 14 #include "base/sequenced_task_runner.h" |
14 #include "base/sys_info.h" | 15 #include "base/sys_info.h" |
15 #include "base/syslog_logging.h" | 16 #include "base/syslog_logging.h" |
16 #include "chrome/browser/chromeos/policy/device_local_account.h" | 17 #include "chrome/browser/chromeos/policy/device_local_account.h" |
17 #include "chrome/browser/chromeos/policy/device_status_collector.h" | 18 #include "chrome/browser/chromeos/policy/device_status_collector.h" |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 // Schedule our next status upload in a minute (last_upload_ is set to the | 78 // Schedule our next status upload in a minute (last_upload_ is set to the |
78 // start of the epoch, so this will trigger an update in | 79 // start of the epoch, so this will trigger an update in |
79 // kMinUploadScheduleDelayMs from now). | 80 // kMinUploadScheduleDelayMs from now). |
80 ScheduleNextStatusUpload(); | 81 ScheduleNextStatusUpload(); |
81 } | 82 } |
82 | 83 |
83 StatusUploader::~StatusUploader() { | 84 StatusUploader::~StatusUploader() { |
84 MediaCaptureDevicesDispatcher::GetInstance()->RemoveObserver(this); | 85 MediaCaptureDevicesDispatcher::GetInstance()->RemoveObserver(this); |
85 } | 86 } |
86 | 87 |
87 void StatusUploader::ScheduleNextStatusUpload() { | 88 void StatusUploader::ScheduleNextStatusUpload(bool immediately) { |
| 89 // Don't schedule a new status upload if there's a status upload in progress |
| 90 // (it will be scheduled once the current one completes). |
| 91 if (status_upload_in_progress_) { |
| 92 SYSLOG(INFO) << "In the middle of a status upload, not scheduling the next " |
| 93 << "one until this one finishes."; |
| 94 return; |
| 95 } |
| 96 |
88 // Calculate when to fire off the next update (if it should have already | 97 // Calculate when to fire off the next update (if it should have already |
89 // happened, this yields a TimeDelta of kMinUploadScheduleDelayMs). | 98 // happened, this yields a TimeDelta of kMinUploadScheduleDelayMs). |
90 base::TimeDelta delay = std::max( | 99 base::TimeDelta delay = std::max( |
91 (last_upload_ + upload_frequency_) - base::Time::NowFromSystemTime(), | 100 (last_upload_ + upload_frequency_) - base::Time::NowFromSystemTime(), |
92 base::TimeDelta::FromMilliseconds(kMinUploadScheduleDelayMs)); | 101 base::TimeDelta::FromMilliseconds(kMinUploadScheduleDelayMs)); |
| 102 // If we want an immediate status upload, set delay to 0. |
| 103 if (immediately) |
| 104 delay = base::TimeDelta(); |
| 105 |
93 upload_callback_.Reset(base::Bind(&StatusUploader::UploadStatus, | 106 upload_callback_.Reset(base::Bind(&StatusUploader::UploadStatus, |
94 base::Unretained(this))); | 107 base::Unretained(this))); |
95 task_runner_->PostDelayedTask(FROM_HERE, upload_callback_.callback(), delay); | 108 task_runner_->PostDelayedTask(FROM_HERE, upload_callback_.callback(), delay); |
96 } | 109 } |
97 | 110 |
98 void StatusUploader::RefreshUploadFrequency() { | 111 void StatusUploader::RefreshUploadFrequency() { |
99 // Attempt to fetch the current value of the reporting settings. | 112 // Attempt to fetch the current value of the reporting settings. |
100 // If trusted values are not available, register this function to be called | 113 // If trusted values are not available, register this function to be called |
101 // back when they are available. | 114 // back when they are available. |
102 chromeos::CrosSettings* settings = chromeos::CrosSettings::Get(); | 115 chromeos::CrosSettings* settings = chromeos::CrosSettings::Get(); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 178 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
166 // If a video or audio capture stream is opened, set a flag so we disallow | 179 // If a video or audio capture stream is opened, set a flag so we disallow |
167 // upload of potentially sensitive data. | 180 // upload of potentially sensitive data. |
168 if (state == content::MEDIA_REQUEST_STATE_OPENING && | 181 if (state == content::MEDIA_REQUEST_STATE_OPENING && |
169 (stream_type == content::MEDIA_DEVICE_AUDIO_CAPTURE || | 182 (stream_type == content::MEDIA_DEVICE_AUDIO_CAPTURE || |
170 stream_type == content::MEDIA_DEVICE_VIDEO_CAPTURE)) { | 183 stream_type == content::MEDIA_DEVICE_VIDEO_CAPTURE)) { |
171 has_captured_media_ = true; | 184 has_captured_media_ = true; |
172 } | 185 } |
173 } | 186 } |
174 | 187 |
| 188 void StatusUploader::ScheduleNextStatusUploadImmediately() { |
| 189 ScheduleNextStatusUpload(true); |
| 190 } |
| 191 |
175 void StatusUploader::UploadStatus() { | 192 void StatusUploader::UploadStatus() { |
| 193 status_upload_in_progress_ = true; |
176 // Gather status in the background. | 194 // Gather status in the background. |
177 collector_->GetDeviceAndSessionStatusAsync(base::Bind( | 195 collector_->GetDeviceAndSessionStatusAsync(base::Bind( |
178 &StatusUploader::OnStatusReceived, weak_factory_.GetWeakPtr())); | 196 &StatusUploader::OnStatusReceived, weak_factory_.GetWeakPtr())); |
179 } | 197 } |
180 | 198 |
181 void StatusUploader::OnStatusReceived( | 199 void StatusUploader::OnStatusReceived( |
182 std::unique_ptr<em::DeviceStatusReportRequest> device_status, | 200 std::unique_ptr<em::DeviceStatusReportRequest> device_status, |
183 std::unique_ptr<em::SessionStatusReportRequest> session_status) { | 201 std::unique_ptr<em::SessionStatusReportRequest> session_status) { |
184 bool have_device_status = device_status != nullptr; | 202 bool have_device_status = device_status != nullptr; |
185 bool have_session_status = session_status != nullptr; | 203 bool have_session_status = session_status != nullptr; |
186 if (!have_device_status && !have_session_status) { | 204 if (!have_device_status && !have_session_status) { |
187 SYSLOG(INFO) << "Skipping status upload because no data to upload"; | 205 SYSLOG(INFO) << "Skipping status upload because no data to upload"; |
188 // Don't have any status to upload - just set our timer for next time. | 206 // Don't have any status to upload - just set our timer for next time. |
189 last_upload_ = base::Time::NowFromSystemTime(); | 207 last_upload_ = base::Time::NowFromSystemTime(); |
| 208 status_upload_in_progress_ = false; |
190 ScheduleNextStatusUpload(); | 209 ScheduleNextStatusUpload(); |
191 return; | 210 return; |
192 } | 211 } |
193 | 212 |
194 SYSLOG(INFO) << "Starting status upload: have_device_status = " | 213 SYSLOG(INFO) << "Starting status upload: have_device_status = " |
195 << have_device_status; | 214 << have_device_status; |
196 client_->UploadDeviceStatus(device_status.get(), session_status.get(), | 215 client_->UploadDeviceStatus(device_status.get(), session_status.get(), |
197 base::Bind(&StatusUploader::OnUploadCompleted, | 216 base::Bind(&StatusUploader::OnUploadCompleted, |
198 weak_factory_.GetWeakPtr())); | 217 weak_factory_.GetWeakPtr())); |
199 } | 218 } |
200 | 219 |
201 void StatusUploader::OnUploadCompleted(bool success) { | 220 void StatusUploader::OnUploadCompleted(bool success) { |
202 // Set the last upload time, regardless of whether the upload was successful | 221 // Set the last upload time, regardless of whether the upload was successful |
203 // or not (we don't change the time of the next upload based on whether this | 222 // or not (we don't change the time of the next upload based on whether this |
204 // upload succeeded or not - if a status upload fails, we just skip it and | 223 // upload succeeded or not - if a status upload fails, we just skip it and |
205 // wait until it's time to try again. | 224 // wait until it's time to try again. |
206 if (success) { | 225 if (success) { |
207 SYSLOG(INFO) << "Status upload successful"; | 226 SYSLOG(INFO) << "Status upload successful"; |
208 } else { | 227 } else { |
209 SYSLOG(ERROR) << "Error uploading status: " << client_->status(); | 228 SYSLOG(ERROR) << "Error uploading status: " << client_->status(); |
210 } | 229 } |
211 last_upload_ = base::Time::NowFromSystemTime(); | 230 last_upload_ = base::Time::NowFromSystemTime(); |
| 231 status_upload_in_progress_ = false; |
212 | 232 |
213 // If the upload was successful, tell the collector so it can clear its cache | 233 // If the upload was successful, tell the collector so it can clear its cache |
214 // of pending items. | 234 // of pending items. |
215 if (success) | 235 if (success) |
216 collector_->OnSubmittedSuccessfully(); | 236 collector_->OnSubmittedSuccessfully(); |
217 | 237 |
218 ScheduleNextStatusUpload(); | 238 ScheduleNextStatusUpload(); |
219 } | 239 } |
220 | 240 |
221 } // namespace policy | 241 } // namespace policy |
OLD | NEW |