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

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

Issue 845313008: Refactored device status uploading out of policy fetch infrastructure. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added tests, fixed existing tests. Created 5 years, 11 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
(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/location.h"
11 #include "chrome/browser/chromeos/policy/device_status_collector.h"
12 #include "chrome/common/pref_names.h"
13 #include "components/policy/core/common/cloud/cloud_policy_client.h"
14 #include "components/policy/core/common/cloud/device_management_service.h"
15
16 namespace {
17 const int kMinUploadDelayMs = 60 * 1000; // 60 seconds
18 } // namespace
19
20 namespace policy {
21
22 const int64 StatusUploader::kDefaultUploadDelayMs =
23 3 * 60 * 60 * 1000; // 3 hours
24
25 StatusUploader::StatusUploader(
26 PrefService* local_state,
27 CloudPolicyClient* client,
28 scoped_ptr<DeviceStatusCollector> collector,
29 const scoped_refptr<base::SequencedTaskRunner>& task_runner)
30 : client_(client),
31 collector_(collector.Pass()),
32 task_runner_(task_runner) {
33 // Listen for changes to the upload delay, and start sending updates to the
34 // server.
35 upload_delay_.reset(new IntegerPrefMember());
36 upload_delay_->Init(
37 prefs::kDeviceStatusUploadRate, local_state,
38 base::Bind(&StatusUploader::ScheduleNextStatusUpload,
39 base::Unretained(this)));
40 ScheduleNextStatusUpload();
41
42 // TODO(atwilson): Listen for changes to the active user and generate a
43 // 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
44 }
45
46 StatusUploader::~StatusUploader() {
47 }
48
49 void StatusUploader::ScheduleNextStatusUpload() {
50 // Calculate when to fire off the next update (if it should have already
51 // happened, this yields a TimeDelta of 0).
52 base::TimeDelta delta = base::TimeDelta::FromMilliseconds(
53 std::max(kMinUploadDelayMs, upload_delay_->GetValue()));
54 base::TimeDelta delay =
55 std::max((last_upload_ + delta) - base::Time::NowFromSystemTime(),
56 base::TimeDelta());
57 upload_callback_.Reset(base::Bind(&StatusUploader::UploadStatus,
58 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.
59 task_runner_->PostDelayedTask(FROM_HERE, upload_callback_.callback(), delay);
60 }
61
62 void StatusUploader::UploadStatus() {
63 // If we already have an upload request active, don't bother starting another
64 // one.
65 if (request_job_)
66 return;
67
68 enterprise_management::DeviceStatusReportRequest device_status;
69 bool have_device_status = collector_->GetDeviceStatus(&device_status);
70 enterprise_management::SessionStatusReportRequest session_status;
71 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
72 if (!have_device_status && !have_session_status) {
73 // Don't have any status to upload - just set our timer for next time.
74 last_upload_ = base::Time::NowFromSystemTime();
75 ScheduleNextStatusUpload();
76 return;
77 }
78 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
79 have_device_status ? &device_status : nullptr,
80 have_session_status ? &session_status : nullptr);
81 request_job_->Start(base::Bind(&StatusUploader::OnUploadCompleted,
82 base::Unretained(this)));
83 }
84
85 void StatusUploader::OnUploadCompleted(
86 DeviceManagementStatus status,
87 int net_error,
88 const enterprise_management::DeviceManagementResponse& response) {
89 // Set the last upload time, regardless of whether the upload was successful
90 // or not (we don't change the time of the next upload based on whether this
91 // upload succeeded or not - if a status upload fails, we just skip it and
92 // wait until it's time to try again.
93 last_upload_ = base::Time::NowFromSystemTime();
94 request_job_.reset();
95
96 // Figure out if the upload was successful - if so, tell the collector so it
97 // can clear its cache of pending items.
98 if (status == DM_STATUS_SUCCESS)
99 collector_->OnSubmittedSuccessfully();
100
101 ScheduleNextStatusUpload();
102 }
103
104 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698