| OLD | NEW |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_CHROMEOS_POLICY_HEARTBEAT_SCHEDULER_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_POLICY_HEARTBEAT_SCHEDULER_H_ |
| 6 #define CHROME_BROWSER_CHROMEOS_POLICY_HEARTBEAT_SCHEDULER_H_ | 6 #define CHROME_BROWSER_CHROMEOS_POLICY_HEARTBEAT_SCHEDULER_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/cancelable_callback.h" | 11 #include "base/cancelable_callback.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 16 #include "chrome/browser/chromeos/settings/cros_settings.h" | 16 #include "chrome/browser/chromeos/settings/cros_settings.h" |
| 17 #include "components/gcm_driver/gcm_app_handler.h" | 17 #include "components/gcm_driver/gcm_app_handler.h" |
| 18 #include "components/gcm_driver/gcm_client.h" | 18 #include "components/gcm_driver/gcm_client.h" |
| 19 #include "components/gcm_driver/gcm_connection_observer.h" |
| 19 #include "components/policy/core/common/cloud/cloud_policy_client.h" | 20 #include "components/policy/core/common/cloud/cloud_policy_client.h" |
| 20 | 21 |
| 21 namespace base { | 22 namespace base { |
| 22 class SequencedTaskRunner; | 23 class SequencedTaskRunner; |
| 23 } | 24 } |
| 24 | 25 |
| 25 namespace gcm { | 26 namespace gcm { |
| 26 class GCMDriver; | 27 class GCMDriver; |
| 27 } | 28 } |
| 28 | 29 |
| 29 namespace policy { | 30 namespace policy { |
| 30 | 31 |
| 31 class EnterpriseInstallAttributes; | 32 class EnterpriseInstallAttributes; |
| 32 class HeartbeatRegistrationHelper; | 33 class HeartbeatRegistrationHelper; |
| 33 | 34 |
| 34 // Class responsible for periodically sending heartbeats to the policy service | 35 // Class responsible for periodically sending heartbeats to the policy service |
| 35 // for monitoring device connectivity. | 36 // for monitoring device connectivity. |
| 36 class HeartbeatScheduler : public gcm::GCMAppHandler { | 37 class HeartbeatScheduler : public gcm::GCMAppHandler, |
| 38 gcm::GCMConnectionObserver { |
| 37 public: | 39 public: |
| 38 // Default interval for how often we send up a heartbeat. | 40 // Default interval for how often we send up a heartbeat. |
| 39 static const int64 kDefaultHeartbeatIntervalMs; | 41 static const int64 kDefaultHeartbeatIntervalMs; |
| 40 | 42 |
| 41 // Constructor. |cloud_policy_client| will be used to send registered GCM id | 43 // 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 // to DM server, and can be null. |driver| can be null for tests. |
| 43 HeartbeatScheduler( | 45 HeartbeatScheduler( |
| 44 gcm::GCMDriver* driver, | 46 gcm::GCMDriver* driver, |
| 45 policy::CloudPolicyClient* cloud_policy_client, | 47 policy::CloudPolicyClient* cloud_policy_client, |
| 46 const std::string& enrollment_domain, | 48 const std::string& enrollment_domain, |
| 47 const std::string& device_id, | 49 const std::string& device_id, |
| 48 const scoped_refptr<base::SequencedTaskRunner>& task_runner); | 50 const scoped_refptr<base::SequencedTaskRunner>& task_runner); |
| 49 | 51 |
| 50 ~HeartbeatScheduler() override; | 52 ~HeartbeatScheduler() override; |
| 51 | 53 |
| 52 // Returns the time of the last heartbeat, or Time(0) if no heartbeat | 54 // Returns the time of the last heartbeat, or Time(0) if no heartbeat |
| 53 // has ever happened. | 55 // has ever happened. |
| 54 base::Time last_heartbeat() const { return last_heartbeat_; } | 56 base::Time last_heartbeat() const { return last_heartbeat_; } |
| 55 | 57 |
| 56 // GCMAppHandler overrides. | 58 // GCMAppHandler overrides. |
| 57 void ShutdownHandler() override; | 59 void ShutdownHandler() override; |
| 58 void OnMessage(const std::string& app_id, | 60 void OnMessage(const std::string& app_id, |
| 59 const gcm::IncomingMessage& message) override; | 61 const gcm::IncomingMessage& message) override; |
| 60 void OnMessagesDeleted(const std::string& app_id) override; | 62 void OnMessagesDeleted(const std::string& app_id) override; |
| 61 void OnSendError(const std::string& app_id, | 63 void OnSendError(const std::string& app_id, |
| 62 const gcm::GCMClient::SendErrorDetails& details) override; | 64 const gcm::GCMClient::SendErrorDetails& details) override; |
| 63 void OnSendAcknowledged(const std::string& app_id, | 65 void OnSendAcknowledged(const std::string& app_id, |
| 64 const std::string& message_id) override; | 66 const std::string& message_id) override; |
| 65 | 67 |
| 68 // GCMConnectionObserver overrides. |
| 69 void OnConnected(const net::IPEndPoint&) override; |
| 70 |
| 66 private: | 71 private: |
| 67 // Callback invoked periodically to send a heartbeat to the policy service. | 72 // Callback invoked periodically to send a heartbeat to the policy service. |
| 68 void SendHeartbeat(); | 73 void SendHeartbeat(); |
| 69 | 74 |
| 70 // Invoked after GCM registration has successfully completed. | 75 // Invoked after GCM registration has successfully completed. |
| 71 void OnRegistrationComplete(const std::string& registration_id); | 76 void OnRegistrationComplete(const std::string& registration_id); |
| 72 | 77 |
| 73 // Invoked after a heartbeat has been sent to the server. | 78 // Invoked after a heartbeat has been sent to the server. |
| 74 void OnHeartbeatSent(const std::string& message_id, | 79 void OnHeartbeatSent(const std::string& message_id, |
| 75 gcm::GCMClient::Result result); | 80 gcm::GCMClient::Result result); |
| 76 | 81 |
| 82 // Invoked after a upstream notification sign up message has been sent. |
| 83 void OnUpstreamNotificationSent(const std::string& message_id, |
| 84 gcm::GCMClient::Result result); |
| 85 |
| 77 // Helper method that figures out when the next heartbeat should | 86 // Helper method that figures out when the next heartbeat should |
| 78 // be scheduled. | 87 // be scheduled. |
| 79 void ScheduleNextHeartbeat(); | 88 void ScheduleNextHeartbeat(); |
| 80 | 89 |
| 81 // Updates the heartbeat-enabled status and frequency from settings and | 90 // Updates the heartbeat-enabled status and frequency from settings and |
| 82 // schedules the next heartbeat. | 91 // schedules the next heartbeat. |
| 83 void RefreshHeartbeatSettings(); | 92 void RefreshHeartbeatSettings(); |
| 84 | 93 |
| 85 // Ensures that the passed interval is within a valid range (not too large or | 94 // Ensures that the passed interval is within a valid range (not too large or |
| 86 // too small). | 95 // too small). |
| 87 base::TimeDelta EnsureValidHeartbeatInterval(const base::TimeDelta& interval); | 96 base::TimeDelta EnsureValidHeartbeatInterval(const base::TimeDelta& interval); |
| 88 | 97 |
| 89 // Shuts down our GCM connection (called when heartbeats are disabled). | 98 // Shuts down our GCM connection (called when heartbeats are disabled). |
| 90 void ShutdownGCM(); | 99 void ShutdownGCM(); |
| 91 | 100 |
| 92 // Callback for the GCM id update request. | 101 // Callback for the GCM id update request. |
| 93 void OnGcmIdUpdateRequestSent(bool status); | 102 void OnGcmIdUpdateRequestSent(bool status); |
| 94 | 103 |
| 104 // Helper function to signup for upstream notification. |
| 105 void SignUpUpstreamNotification(); |
| 106 |
| 95 // TaskRunner used for scheduling heartbeats. | 107 // TaskRunner used for scheduling heartbeats. |
| 96 const scoped_refptr<base::SequencedTaskRunner> task_runner_; | 108 const scoped_refptr<base::SequencedTaskRunner> task_runner_; |
| 97 | 109 |
| 98 // The domain that this device is enrolled to. | 110 // The domain that this device is enrolled to. |
| 99 const std::string enrollment_domain_; | 111 const std::string enrollment_domain_; |
| 100 | 112 |
| 101 // The device_id for this device - sent up with the enrollment domain with | 113 // The device_id for this device - sent up with the enrollment domain with |
| 102 // heartbeats to identify the device to the server. | 114 // heartbeats to identify the device to the server. |
| 103 const std::string device_id_; | 115 const std::string device_id_; |
| 104 | 116 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 127 // The GCMDriver used to send heartbeat messages. | 139 // The GCMDriver used to send heartbeat messages. |
| 128 gcm::GCMDriver* const gcm_driver_; | 140 gcm::GCMDriver* const gcm_driver_; |
| 129 | 141 |
| 130 // The GCM registration ID - if empty, we are not registered yet. | 142 // The GCM registration ID - if empty, we are not registered yet. |
| 131 std::string registration_id_; | 143 std::string registration_id_; |
| 132 | 144 |
| 133 // If true, we are already registered with GCM and should unregister when | 145 // If true, we are already registered with GCM and should unregister when |
| 134 // destroyed. | 146 // destroyed. |
| 135 bool registered_app_handler_ = false; | 147 bool registered_app_handler_ = false; |
| 136 | 148 |
| 149 // Last upstream notification sign up time, a null value represents never |
| 150 // signed up before. |
| 151 base::TimeTicks last_upstream_notification_signup_time_; |
| 152 |
| 137 // Helper class to manage registering with the GCM server, including | 153 // Helper class to manage registering with the GCM server, including |
| 138 // retries, etc. | 154 // retries, etc. |
| 139 scoped_ptr<HeartbeatRegistrationHelper> registration_helper_; | 155 scoped_ptr<HeartbeatRegistrationHelper> registration_helper_; |
| 140 | 156 |
| 141 // Note: This should remain the last member so it'll be destroyed and | 157 // Note: This should remain the last member so it'll be destroyed and |
| 142 // invalidate the weak pointers before any other members are destroyed. | 158 // invalidate the weak pointers before any other members are destroyed. |
| 143 base::WeakPtrFactory<HeartbeatScheduler> weak_factory_; | 159 base::WeakPtrFactory<HeartbeatScheduler> weak_factory_; |
| 144 | 160 |
| 145 DISALLOW_COPY_AND_ASSIGN(HeartbeatScheduler); | 161 DISALLOW_COPY_AND_ASSIGN(HeartbeatScheduler); |
| 146 }; | 162 }; |
| 147 | 163 |
| 148 } // namespace policy | 164 } // namespace policy |
| 149 | 165 |
| 150 #endif // CHROME_BROWSER_CHROMEOS_POLICY_HEARTBEAT_SCHEDULER_H_ | 166 #endif // CHROME_BROWSER_CHROMEOS_POLICY_HEARTBEAT_SCHEDULER_H_ |
| OLD | NEW |