Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/policy/status_uploader.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/bind_helpers.h" | |
| 11 #include "base/location.h" | |
| 12 #include "chrome/browser/chromeos/policy/device_status_collector.h" | |
| 13 #include "chrome/common/pref_names.h" | |
| 14 #include "components/policy/core/common/cloud/cloud_policy_client.h" | |
| 15 #include "components/policy/core/common/cloud/device_management_service.h" | |
| 16 | |
| 17 namespace { | |
| 18 const int kMinUploadDelayMs = 60 * 1000; // 60 seconds | |
| 19 } // namespace | |
| 20 | |
| 21 namespace policy { | |
| 22 | |
| 23 const int64 StatusUploader::kDefaultUploadDelayMs = | |
| 24 3 * 60 * 60 * 1000; // 3 hours | |
| 25 | |
| 26 StatusUploader::StatusUploader( | |
| 27 PrefService* local_state, | |
| 28 CloudPolicyClient* client, | |
| 29 scoped_ptr<DeviceStatusCollector> collector, | |
| 30 const scoped_refptr<base::SequencedTaskRunner>& task_runner) | |
| 31 : client_(client), | |
| 32 collector_(collector.Pass()), | |
| 33 task_runner_(task_runner) { | |
| 34 DCHECK(client->is_registered()); | |
|
Mattias Nissler (ping if slow)
2015/01/26 10:37:02
If you don't think it's worthwhile to make this co
Andrew T Wilson (Slow)
2015/01/28 13:19:12
Comment added.
| |
| 35 // Listen for changes to the upload delay, and start sending updates to the | |
| 36 // server. | |
| 37 upload_delay_.reset(new IntegerPrefMember()); | |
| 38 upload_delay_->Init( | |
| 39 prefs::kDeviceStatusUploadRate, local_state, | |
| 40 base::Bind(&StatusUploader::ScheduleNextStatusUpload, | |
| 41 base::Unretained(this))); | |
| 42 ScheduleNextStatusUpload(); | |
| 43 } | |
| 44 | |
| 45 StatusUploader::~StatusUploader() { | |
| 46 } | |
| 47 | |
| 48 void StatusUploader::ScheduleNextStatusUpload() { | |
| 49 // Calculate when to fire off the next update (if it should have already | |
| 50 // happened, this yields a TimeDelta of 0). | |
| 51 base::TimeDelta delta = base::TimeDelta::FromMilliseconds( | |
| 52 std::max(kMinUploadDelayMs, upload_delay_->GetValue())); | |
| 53 base::TimeDelta delay = | |
| 54 std::max((last_upload_ + delta) - base::Time::NowFromSystemTime(), | |
| 55 base::TimeDelta()); | |
| 56 upload_callback_.Reset(base::Bind(&StatusUploader::UploadStatus, | |
| 57 base::Unretained(this))); | |
| 58 task_runner_->PostDelayedTask(FROM_HERE, upload_callback_.callback(), delay); | |
| 59 } | |
| 60 | |
| 61 void StatusUploader::UploadStatus() { | |
| 62 // If we already have an upload request active, don't bother starting another | |
| 63 // one. | |
| 64 if (request_job_) | |
| 65 return; | |
| 66 | |
| 67 enterprise_management::DeviceStatusReportRequest device_status; | |
| 68 bool have_device_status = collector_->GetDeviceStatus(&device_status); | |
| 69 enterprise_management::SessionStatusReportRequest session_status; | |
| 70 bool have_session_status = collector_->GetSessionStatus(&session_status); | |
| 71 if (!have_device_status && !have_session_status) { | |
| 72 // Don't have any status to upload - just set our timer for next time. | |
| 73 last_upload_ = base::Time::NowFromSystemTime(); | |
| 74 ScheduleNextStatusUpload(); | |
| 75 return; | |
| 76 } | |
| 77 request_job_ = client_->CreateUploadStatusJob( | |
| 78 have_device_status ? &device_status : nullptr, | |
| 79 have_session_status ? &session_status : nullptr); | |
| 80 request_job_->Start(base::Bind(&StatusUploader::OnUploadCompleted, | |
| 81 base::Unretained(this))); | |
| 82 } | |
| 83 | |
| 84 void StatusUploader::OnUploadCompleted( | |
| 85 DeviceManagementStatus status, | |
| 86 int net_error, | |
| 87 const enterprise_management::DeviceManagementResponse& response) { | |
| 88 // Set the last upload time, regardless of whether the upload was successful | |
| 89 // or not (we don't change the time of the next upload based on whether this | |
| 90 // upload succeeded or not - if a status upload fails, we just skip it and | |
| 91 // wait until it's time to try again. | |
| 92 last_upload_ = base::Time::NowFromSystemTime(); | |
| 93 request_job_.reset(); | |
| 94 | |
| 95 // Figure out if the upload was successful - if so, tell the collector so it | |
| 96 // can clear its cache of pending items. | |
| 97 if (status == DM_STATUS_SUCCESS) | |
| 98 collector_->OnSubmittedSuccessfully(); | |
| 99 | |
| 100 ScheduleNextStatusUpload(); | |
| 101 } | |
| 102 | |
| 103 } // namespace policy | |
| OLD | NEW |