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_HEARTBEAT_SCHEDULER_H_
6 #define CHROME_BROWSER_CHROMEOS_POLICY_HEARTBEAT_SCHEDULER_H_
10 #include "base/basictypes.h"
11 #include "base/cancelable_callback.h"
12 #include "base/macros.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/time/time.h"
16 #include "chrome/browser/chromeos/settings/cros_settings.h"
17 #include "components/gcm_driver/gcm_app_handler.h"
18 #include "components/gcm_driver/gcm_client.h"
19 #include "components/policy/core/common/cloud/cloud_policy_client.h"
22 class SequencedTaskRunner
;
31 class EnterpriseInstallAttributes
;
32 class HeartbeatRegistrationHelper
;
34 // Class responsible for periodically sending heartbeats to the policy service
35 // for monitoring device connectivity.
36 class HeartbeatScheduler
: public gcm::GCMAppHandler
{
38 // Default interval for how often we send up a heartbeat.
39 static const int64 kDefaultHeartbeatIntervalMs
;
41 // Constructor. |cloud_policy_client| will be used to send registered GCM id
42 // to DM server, and can be null. |driver| can be null for tests.
44 gcm::GCMDriver
* driver
,
45 policy::CloudPolicyClient
* cloud_policy_client
,
46 const std::string
& enrollment_domain
,
47 const std::string
& device_id
,
48 const scoped_refptr
<base::SequencedTaskRunner
>& task_runner
);
50 ~HeartbeatScheduler() override
;
52 // Returns the time of the last heartbeat, or Time(0) if no heartbeat
54 base::Time
last_heartbeat() const { return last_heartbeat_
; }
56 // GCMAppHandler overrides.
57 void ShutdownHandler() override
;
58 void OnMessage(const std::string
& app_id
,
59 const gcm::IncomingMessage
& message
) override
;
60 void OnMessagesDeleted(const std::string
& app_id
) override
;
61 void OnSendError(const std::string
& app_id
,
62 const gcm::GCMClient::SendErrorDetails
& details
) override
;
63 void OnSendAcknowledged(const std::string
& app_id
,
64 const std::string
& message_id
) override
;
67 // Callback invoked periodically to send a heartbeat to the policy service.
70 // Invoked after GCM registration has successfully completed.
71 void OnRegistrationComplete(const std::string
& registration_id
);
73 // Invoked after a heartbeat has been sent to the server.
74 void OnHeartbeatSent(const std::string
& message_id
,
75 gcm::GCMClient::Result result
);
77 // Helper method that figures out when the next heartbeat should
79 void ScheduleNextHeartbeat();
81 // Updates the heartbeat-enabled status and frequency from settings and
82 // schedules the next heartbeat.
83 void RefreshHeartbeatSettings();
85 // Ensures that the passed interval is within a valid range (not too large or
87 base::TimeDelta
EnsureValidHeartbeatInterval(const base::TimeDelta
& interval
);
89 // Shuts down our GCM connection (called when heartbeats are disabled).
92 // Callback for the GCM id update request.
93 void OnGcmIdUpdateRequestSent(bool status
);
95 // TaskRunner used for scheduling heartbeats.
96 const scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
98 // The domain that this device is enrolled to.
99 const std::string enrollment_domain_
;
101 // The device_id for this device - sent up with the enrollment domain with
102 // heartbeats to identify the device to the server.
103 const std::string device_id_
;
105 // True if heartbeats are enabled. Kept cached in this object because
106 // CrosSettings can switch to an untrusted state temporarily, and we want
107 // to use the last-known trusted values.
108 bool heartbeat_enabled_
;
110 // Cached copy of the current heartbeat interval, in milliseconds.
111 base::TimeDelta heartbeat_interval_
;
113 // Observers to changes in the heartbeat settings.
114 scoped_ptr
<chromeos::CrosSettings::ObserverSubscription
>
115 heartbeat_frequency_observer_
;
116 scoped_ptr
<chromeos::CrosSettings::ObserverSubscription
>
117 heartbeat_enabled_observer_
;
119 // The time the last heartbeat was sent.
120 base::Time last_heartbeat_
;
122 // Callback invoked via a delay to send a heartbeat.
123 base::CancelableClosure heartbeat_callback_
;
125 policy::CloudPolicyClient
* cloud_policy_client_
;
127 // The GCMDriver used to send heartbeat messages.
128 gcm::GCMDriver
* const gcm_driver_
;
130 // The GCM registration ID - if empty, we are not registered yet.
131 std::string registration_id_
;
133 // If true, we are already registered with GCM and should unregister when
135 bool registered_app_handler_
= false;
137 // Helper class to manage registering with the GCM server, including
139 scoped_ptr
<HeartbeatRegistrationHelper
> registration_helper_
;
141 // Note: This should remain the last member so it'll be destroyed and
142 // invalidate the weak pointers before any other members are destroyed.
143 base::WeakPtrFactory
<HeartbeatScheduler
> weak_factory_
;
145 DISALLOW_COPY_AND_ASSIGN(HeartbeatScheduler
);
148 } // namespace policy
150 #endif // CHROME_BROWSER_CHROMEOS_POLICY_HEARTBEAT_SCHEDULER_H_