Chromium Code Reviews| Index: chrome/browser/chromeos/policy/status_uploader.cc |
| diff --git a/chrome/browser/chromeos/policy/status_uploader.cc b/chrome/browser/chromeos/policy/status_uploader.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ad1445fd35ddc11b5552e09233a53e8dcaa03d2d |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/policy/status_uploader.cc |
| @@ -0,0 +1,104 @@ |
| +// Copyright (c) 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/chromeos/policy/status_uploader.h" |
| + |
| +#include <algorithm> |
| + |
| +#include "base/bind.h" |
| +#include "base/location.h" |
| +#include "chrome/browser/chromeos/policy/device_status_collector.h" |
| +#include "chrome/common/pref_names.h" |
| +#include "components/policy/core/common/cloud/cloud_policy_client.h" |
| +#include "components/policy/core/common/cloud/device_management_service.h" |
| + |
| +namespace { |
| +const int kMinUploadDelayMs = 60 * 1000; // 60 seconds |
| +} // namespace |
| + |
| +namespace policy { |
| + |
| +const int64 StatusUploader::kDefaultUploadDelayMs = |
| + 3 * 60 * 60 * 1000; // 3 hours |
| + |
| +StatusUploader::StatusUploader( |
| + PrefService* local_state, |
| + CloudPolicyClient* client, |
| + scoped_ptr<DeviceStatusCollector> collector, |
| + const scoped_refptr<base::SequencedTaskRunner>& task_runner) |
| + : client_(client), |
| + collector_(collector.Pass()), |
| + task_runner_(task_runner) { |
| + // Listen for changes to the upload delay, and start sending updates to the |
| + // server. |
| + upload_delay_.reset(new IntegerPrefMember()); |
| + upload_delay_->Init( |
| + prefs::kDeviceStatusUploadRate, local_state, |
| + base::Bind(&StatusUploader::ScheduleNextStatusUpload, |
| + base::Unretained(this))); |
| + ScheduleNextStatusUpload(); |
| + |
| + // TODO(atwilson): Listen for changes to the active user and generate a |
| + // status upload if it changes. |
|
Mattias Nissler (ping if slow)
2015/01/23 13:46:35
I don't understand this TODO. Does it refer to add
Andrew T Wilson (Slow)
2015/01/23 19:16:19
I'm planning to upload session status using the de
|
| +} |
| + |
| +StatusUploader::~StatusUploader() { |
| +} |
| + |
| +void StatusUploader::ScheduleNextStatusUpload() { |
| + // Calculate when to fire off the next update (if it should have already |
| + // happened, this yields a TimeDelta of 0). |
| + base::TimeDelta delta = base::TimeDelta::FromMilliseconds( |
| + std::max(kMinUploadDelayMs, upload_delay_->GetValue())); |
| + base::TimeDelta delay = |
| + std::max((last_upload_ + delta) - base::Time::NowFromSystemTime(), |
| + base::TimeDelta()); |
| + upload_callback_.Reset(base::Bind(&StatusUploader::UploadStatus, |
| + base::Unretained(this))); |
|
Mattias Nissler (ping if slow)
2015/01/23 13:46:35
#include "base/bind_helpers.h"
Andrew T Wilson (Slow)
2015/01/23 19:16:19
Done.
|
| + task_runner_->PostDelayedTask(FROM_HERE, upload_callback_.callback(), delay); |
| +} |
| + |
| +void StatusUploader::UploadStatus() { |
| + // If we already have an upload request active, don't bother starting another |
| + // one. |
| + if (request_job_) |
| + return; |
| + |
| + enterprise_management::DeviceStatusReportRequest device_status; |
| + bool have_device_status = collector_->GetDeviceStatus(&device_status); |
| + enterprise_management::SessionStatusReportRequest session_status; |
| + bool have_session_status = collector_->GetSessionStatus(&session_status); |
|
Mattias Nissler (ping if slow)
2015/01/23 13:46:34
Hm, session status doesn't make sense for device s
Andrew T Wilson (Slow)
2015/01/23 19:16:19
They are uploaded together. That's why this class
|
| + if (!have_device_status && !have_session_status) { |
| + // Don't have any status to upload - just set our timer for next time. |
| + last_upload_ = base::Time::NowFromSystemTime(); |
| + ScheduleNextStatusUpload(); |
| + return; |
| + } |
| + request_job_ = client_->CreateUploadStatusJob( |
|
Mattias Nissler (ping if slow)
2015/01/23 13:46:35
You're assuming that |client_| is registered with
Andrew T Wilson (Slow)
2015/01/23 19:16:19
Yes, I'm assuming it's registered already - I'll a
|
| + have_device_status ? &device_status : nullptr, |
| + have_session_status ? &session_status : nullptr); |
| + request_job_->Start(base::Bind(&StatusUploader::OnUploadCompleted, |
| + base::Unretained(this))); |
| +} |
| + |
| +void StatusUploader::OnUploadCompleted( |
| + DeviceManagementStatus status, |
| + int net_error, |
| + const enterprise_management::DeviceManagementResponse& response) { |
| + // Set the last upload time, regardless of whether the upload was successful |
| + // or not (we don't change the time of the next upload based on whether this |
| + // upload succeeded or not - if a status upload fails, we just skip it and |
| + // wait until it's time to try again. |
| + last_upload_ = base::Time::NowFromSystemTime(); |
| + request_job_.reset(); |
| + |
| + // Figure out if the upload was successful - if so, tell the collector so it |
| + // can clear its cache of pending items. |
| + if (status == DM_STATUS_SUCCESS) |
| + collector_->OnSubmittedSuccessfully(); |
| + |
| + ScheduleNextStatusUpload(); |
| +} |
| + |
| +} // namespace policy |