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 // should be destroyed by the owner if the client is ever unregistered. If we | |
Mattias Nissler (ping if slow)
2015/01/28 15:44:06
nit: "should be destroyed" is ambiguous and might
Andrew T Wilson (Slow)
2015/01/28 16:13:09
Done.
| |
37 // want to support unregistered clients, then this class should be updated | |
38 // to skip status uploads for unregistered clients, and to observe the client | |
39 // and kick off an upload when registration happens. | |
40 DCHECK(client->is_registered()); | |
41 | |
42 // Listen for changes to the upload delay, and start sending updates to the | |
43 // server. | |
44 upload_delay_.reset(new IntegerPrefMember()); | |
45 upload_delay_->Init( | |
46 prefs::kDeviceStatusUploadRate, local_state, | |
47 base::Bind(&StatusUploader::ScheduleNextStatusUpload, | |
48 base::Unretained(this))); | |
49 ScheduleNextStatusUpload(); | |
50 } | |
51 | |
52 StatusUploader::~StatusUploader() { | |
53 } | |
54 | |
55 void StatusUploader::ScheduleNextStatusUpload() { | |
56 // Calculate when to fire off the next update (if it should have already | |
57 // happened, this yields a TimeDelta of 0). | |
58 base::TimeDelta delta = base::TimeDelta::FromMilliseconds( | |
59 std::max(kMinUploadDelayMs, upload_delay_->GetValue())); | |
60 base::TimeDelta delay = | |
61 std::max((last_upload_ + delta) - base::Time::NowFromSystemTime(), | |
62 base::TimeDelta()); | |
63 upload_callback_.Reset(base::Bind(&StatusUploader::UploadStatus, | |
64 base::Unretained(this))); | |
65 task_runner_->PostDelayedTask(FROM_HERE, upload_callback_.callback(), delay); | |
66 } | |
67 | |
68 void StatusUploader::UploadStatus() { | |
69 // If we already have an upload request active, don't bother starting another | |
70 // one. | |
71 if (request_job_) | |
72 return; | |
73 | |
74 enterprise_management::DeviceStatusReportRequest device_status; | |
75 bool have_device_status = collector_->GetDeviceStatus(&device_status); | |
76 enterprise_management::SessionStatusReportRequest session_status; | |
77 bool have_session_status = collector_->GetDeviceSessionStatus( | |
78 &session_status); | |
79 if (!have_device_status && !have_session_status) { | |
80 // Don't have any status to upload - just set our timer for next time. | |
81 last_upload_ = base::Time::NowFromSystemTime(); | |
82 ScheduleNextStatusUpload(); | |
83 return; | |
84 } | |
85 | |
86 client_->UploadDeviceStatus( | |
87 have_device_status ? &device_status : nullptr, | |
88 have_session_status ? &session_status : nullptr, | |
89 base::Bind(&StatusUploader::OnUploadCompleted, | |
90 weak_factory_.GetWeakPtr())); | |
91 } | |
92 | |
93 void StatusUploader::OnUploadCompleted(bool success) { | |
94 // Set the last upload time, regardless of whether the upload was successful | |
95 // or not (we don't change the time of the next upload based on whether this | |
96 // upload succeeded or not - if a status upload fails, we just skip it and | |
97 // wait until it's time to try again. | |
98 last_upload_ = base::Time::NowFromSystemTime(); | |
99 | |
100 // If the upload was successful, tell the collector so it can clear its cache | |
101 // of pending items. | |
Mattias Nissler (ping if slow)
2015/01/28 15:44:06
Tangential comment: If we allow multiple status up
Andrew T Wilson (Slow)
2015/01/28 16:13:09
Yeah, I'll probably reset the WeakPtrFactory so on
| |
102 if (success) | |
103 collector_->OnSubmittedSuccessfully(); | |
104 | |
105 ScheduleNextStatusUpload(); | |
106 } | |
107 | |
108 } // namespace policy | |
OLD | NEW |