Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(180)

Side by Side Diff: chrome/browser/chromeos/policy/status_uploader.cc

Issue 2314813002: Refactored DeviceStatusCollector to enable truely asynchronous status queries (Closed)
Patch Set: Initialize *status_ in the C++11'y way Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <utility> 8 #include <utility>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 // If a video or audio capture stream is opened, set a flag so we disallow 169 // If a video or audio capture stream is opened, set a flag so we disallow
170 // upload of potentially sensitive data. 170 // upload of potentially sensitive data.
171 if (state == content::MEDIA_REQUEST_STATE_OPENING && 171 if (state == content::MEDIA_REQUEST_STATE_OPENING &&
172 (stream_type == content::MEDIA_DEVICE_AUDIO_CAPTURE || 172 (stream_type == content::MEDIA_DEVICE_AUDIO_CAPTURE ||
173 stream_type == content::MEDIA_DEVICE_VIDEO_CAPTURE)) { 173 stream_type == content::MEDIA_DEVICE_VIDEO_CAPTURE)) {
174 has_captured_media_ = true; 174 has_captured_media_ = true;
175 } 175 }
176 } 176 }
177 177
178 void StatusUploader::UploadStatus() { 178 void StatusUploader::UploadStatus() {
179 // Submit the responses of the asynchronous calls to collector_ 179 // Gather status in the background.
180 // in a small ref-counted state tracker class, so that we can 180 collector_->GetDeviceAndSessionStatusAsync(base::Bind(
181 // a) track that both responses fired and 181 &StatusUploader::OnStatusReceived, weak_factory_.GetWeakPtr()));
182 // b) quick subsequent calls to UploadStatus() won't mess up state.
183 scoped_refptr<StatusGetter> getter(
184 new StatusGetter(weak_factory_.GetWeakPtr()));
185
186 // Note that the two base::Binds keep references to getter,
187 // so getter stays alive until both callbacks are called.
188 collector_->GetDeviceStatusAsync(
189 base::Bind(&StatusGetter::OnDeviceStatusReceived, getter));
190
191 collector_->GetDeviceSessionStatusAsync(
192 base::Bind(&StatusGetter::OnSessionStatusReceived, getter));
193 }
194
195 StatusUploader::StatusGetter::StatusGetter(
196 const base::WeakPtr<StatusUploader>& uploader)
197 : uploader_(uploader),
198 device_status_response_received_(false),
199 session_status_response_received_(false) {}
200
201 StatusUploader::StatusGetter::~StatusGetter() {}
202
203 void StatusUploader::StatusGetter::OnDeviceStatusReceived(
204 std::unique_ptr<em::DeviceStatusReportRequest> device_status) {
205 DCHECK(!device_status_response_received_);
206 device_status_ = std::move(device_status);
207 device_status_response_received_ = true;
208 CheckDone();
209 }
210
211 void StatusUploader::StatusGetter::OnSessionStatusReceived(
212 std::unique_ptr<em::SessionStatusReportRequest> session_status) {
213 DCHECK(!session_status_response_received_);
214 session_status_ = std::move(session_status);
215 session_status_response_received_ = true;
216 CheckDone();
217 }
218
219 void StatusUploader::StatusGetter::CheckDone() {
220 // Did we receive BOTH responses?
221 if (device_status_response_received_ && session_status_response_received_) {
222 // Notify the uploader if it's still alive
223 StatusUploader* uploader = uploader_.get();
224 if (uploader) {
225 uploader->OnStatusReceived(std::move(device_status_),
226 std::move(session_status_));
227 // Reset just to make sure this doesn't get called multiple times
228 uploader_.reset();
229 }
230 }
231 } 182 }
232 183
233 void StatusUploader::OnStatusReceived( 184 void StatusUploader::OnStatusReceived(
234 std::unique_ptr<em::DeviceStatusReportRequest> device_status, 185 std::unique_ptr<em::DeviceStatusReportRequest> device_status,
235 std::unique_ptr<em::SessionStatusReportRequest> session_status) { 186 std::unique_ptr<em::SessionStatusReportRequest> session_status) {
236 bool have_device_status = device_status != nullptr; 187 bool have_device_status = device_status != nullptr;
237 bool have_session_status = session_status != nullptr; 188 bool have_session_status = session_status != nullptr;
238 if (!have_device_status && !have_session_status) { 189 if (!have_device_status && !have_session_status) {
239 CHROMEOS_SYSLOG(WARNING) 190 CHROMEOS_SYSLOG(WARNING)
240 << "Skipping status upload because no data to upload"; 191 << "Skipping status upload because no data to upload";
(...skipping 22 matching lines...) Expand all
263 214
264 // If the upload was successful, tell the collector so it can clear its cache 215 // If the upload was successful, tell the collector so it can clear its cache
265 // of pending items. 216 // of pending items.
266 if (success) 217 if (success)
267 collector_->OnSubmittedSuccessfully(); 218 collector_->OnSubmittedSuccessfully();
268 219
269 ScheduleNextStatusUpload(); 220 ScheduleNextStatusUpload();
270 } 221 }
271 222
272 } // namespace policy 223 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/policy/status_uploader.h ('k') | chrome/browser/chromeos/policy/status_uploader_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698