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.
5 #ifndef CHROME_BROWSER_CHROMEOS_POLICY_STATUS_UPLOADER_H_
6 #define CHROME_BROWSER_CHROMEOS_POLICY_STATUS_UPLOADER_H_
8 #include "base/cancelable_callback.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/time/time.h"
13 #include "chrome/browser/chromeos/settings/cros_settings.h"
14 #include "components/policy/core/common/cloud/cloud_policy_constants.h"
15 #include "policy/proto/device_management_backend.pb.h"
18 class SequencedTaskRunner
;
23 class CloudPolicyClient
;
24 class DeviceStatusCollector
;
26 // Class responsible for periodically uploading device status from the
27 // passed DeviceStatusCollector.
28 class StatusUploader
{
31 static const int64 kDefaultUploadDelayMs
;
33 // Constructor. |client| must be registered and must stay
34 // valid and registered through the lifetime of this StatusUploader
37 CloudPolicyClient
* client
,
38 scoped_ptr
<DeviceStatusCollector
> collector
,
39 const scoped_refptr
<base::SequencedTaskRunner
>& task_runner
);
43 // Returns the time of the last successful upload, or Time(0) if no upload
45 base::Time
last_upload() const { return last_upload_
; }
48 // Callback invoked periodically to upload the device status from the
49 // DeviceStatusCollector.
52 // Invoked once a status upload has completed.
53 void OnUploadCompleted(bool success
);
55 // Helper method that figures out when the next status upload should
57 void ScheduleNextStatusUpload();
59 // Updates the upload frequency from settings and schedules a new upload
61 void RefreshUploadFrequency();
63 // CloudPolicyClient used to issue requests to the server.
64 CloudPolicyClient
* client_
;
66 // DeviceStatusCollector that provides status for uploading.
67 scoped_ptr
<DeviceStatusCollector
> collector_
;
69 // TaskRunner used for scheduling upload tasks.
70 const scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
72 // How long to wait between status uploads.
73 base::TimeDelta upload_frequency_
;
75 // Observer to changes in the upload frequency.
76 scoped_ptr
<chromeos::CrosSettings::ObserverSubscription
>
77 upload_frequency_observer_
;
79 // The time the last upload was performed.
80 base::Time last_upload_
;
82 // Callback invoked via a delay to upload device status.
83 base::CancelableClosure upload_callback_
;
85 // Note: This should remain the last member so it'll be destroyed and
86 // invalidate the weak pointers before any other members are destroyed.
87 base::WeakPtrFactory
<StatusUploader
> weak_factory_
;
89 DISALLOW_COPY_AND_ASSIGN(StatusUploader
);
94 #endif // CHROME_BROWSER_CHROMEOS_POLICY_STATUS_UPLOADER_H_