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 weak_factory_(this) { |
| 35 // StatusUploader is currently only created for registered clients, and |
| 36 // it is currently safe to assume that the client will not unregister while |
| 37 // StatusUploader is alive. |
| 38 // |
| 39 // If future changes result in StatusUploader's lifetime extending beyond |
| 40 // unregistration events, then this class should be updated |
| 41 // to skip status uploads for unregistered clients, and to observe the client |
| 42 // and kick off an upload when registration happens. |
| 43 DCHECK(client->is_registered()); |
| 44 |
| 45 // Listen for changes to the upload delay, and start sending updates to the |
| 46 // server. |
| 47 upload_delay_.reset(new IntegerPrefMember()); |
| 48 upload_delay_->Init( |
| 49 prefs::kDeviceStatusUploadRate, local_state, |
| 50 base::Bind(&StatusUploader::ScheduleNextStatusUpload, |
| 51 base::Unretained(this))); |
| 52 ScheduleNextStatusUpload(); |
| 53 } |
| 54 |
| 55 StatusUploader::~StatusUploader() { |
| 56 } |
| 57 |
| 58 void StatusUploader::ScheduleNextStatusUpload() { |
| 59 // Calculate when to fire off the next update (if it should have already |
| 60 // happened, this yields a TimeDelta of 0). |
| 61 base::TimeDelta delta = base::TimeDelta::FromMilliseconds( |
| 62 std::max(kMinUploadDelayMs, upload_delay_->GetValue())); |
| 63 base::TimeDelta delay = |
| 64 std::max((last_upload_ + delta) - base::Time::NowFromSystemTime(), |
| 65 base::TimeDelta()); |
| 66 upload_callback_.Reset(base::Bind(&StatusUploader::UploadStatus, |
| 67 base::Unretained(this))); |
| 68 task_runner_->PostDelayedTask(FROM_HERE, upload_callback_.callback(), delay); |
| 69 } |
| 70 |
| 71 void StatusUploader::UploadStatus() { |
| 72 // If we already have an upload request active, don't bother starting another |
| 73 // one. |
| 74 if (request_job_) |
| 75 return; |
| 76 |
| 77 enterprise_management::DeviceStatusReportRequest device_status; |
| 78 bool have_device_status = collector_->GetDeviceStatus(&device_status); |
| 79 enterprise_management::SessionStatusReportRequest session_status; |
| 80 bool have_session_status = collector_->GetDeviceSessionStatus( |
| 81 &session_status); |
| 82 if (!have_device_status && !have_session_status) { |
| 83 // Don't have any status to upload - just set our timer for next time. |
| 84 last_upload_ = base::Time::NowFromSystemTime(); |
| 85 ScheduleNextStatusUpload(); |
| 86 return; |
| 87 } |
| 88 |
| 89 client_->UploadDeviceStatus( |
| 90 have_device_status ? &device_status : nullptr, |
| 91 have_session_status ? &session_status : nullptr, |
| 92 base::Bind(&StatusUploader::OnUploadCompleted, |
| 93 weak_factory_.GetWeakPtr())); |
| 94 } |
| 95 |
| 96 void StatusUploader::OnUploadCompleted(bool success) { |
| 97 // Set the last upload time, regardless of whether the upload was successful |
| 98 // or not (we don't change the time of the next upload based on whether this |
| 99 // upload succeeded or not - if a status upload fails, we just skip it and |
| 100 // wait until it's time to try again. |
| 101 last_upload_ = base::Time::NowFromSystemTime(); |
| 102 |
| 103 // If the upload was successful, tell the collector so it can clear its cache |
| 104 // of pending items. |
| 105 if (success) |
| 106 collector_->OnSubmittedSuccessfully(); |
| 107 |
| 108 ScheduleNextStatusUpload(); |
| 109 } |
| 110 |
| 111 } // namespace policy |
OLD | NEW |