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

Side by Side Diff: chrome/browser/chromeos/policy/heartbeat_scheduler.h

Issue 1258313002: Send GCM id to DMServer (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: add tests Created 5 years, 4 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
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/policy/core/common/cloud/cloud_policy_client.h"
19 20
20 namespace base { 21 namespace base {
21 class SequencedTaskRunner; 22 class SequencedTaskRunner;
22 } 23 }
23 24
24 namespace gcm { 25 namespace gcm {
25 class GCMDriver; 26 class GCMDriver;
26 } 27 }
27 28
28 namespace policy { 29 namespace policy {
29 30
30 class EnterpriseInstallAttributes; 31 class EnterpriseInstallAttributes;
31 class HeartbeatRegistrationHelper; 32 class HeartbeatRegistrationHelper;
32 33
33 // Class responsible for periodically sending heartbeats to the policy service 34 // Class responsible for periodically sending heartbeats to the policy service
34 // for monitoring device connectivity. 35 // for monitoring device connectivity.
35 class HeartbeatScheduler : public gcm::GCMAppHandler { 36 class HeartbeatScheduler : public gcm::GCMAppHandler {
36 public: 37 public:
37 // Default interval for how often we send up a heartbeat. 38 // Default interval for how often we send up a heartbeat.
38 static const int64 kDefaultHeartbeatIntervalMs; 39 static const int64 kDefaultHeartbeatIntervalMs;
39 40
40 // Constructor. |driver| can be null for tests. 41 // Constructor. |cloud_policy_client| will be used to send registred GCM id
Andrew T Wilson (Slow) 2015/08/03 13:52:26 nit: registered
binjin 2015/08/03 17:54:25 Done.
42 // to DM server, and can be null. |driver| can be null for tests.
41 HeartbeatScheduler( 43 HeartbeatScheduler(
42 gcm::GCMDriver* driver, 44 gcm::GCMDriver* driver,
45 policy::CloudPolicyClient* cloud_policy_client,
43 const std::string& enrollment_domain, 46 const std::string& enrollment_domain,
44 const std::string& device_id, 47 const std::string& device_id,
45 const scoped_refptr<base::SequencedTaskRunner>& task_runner); 48 const scoped_refptr<base::SequencedTaskRunner>& task_runner);
46 49
47 ~HeartbeatScheduler() override; 50 ~HeartbeatScheduler() override;
48 51
49 // Returns the time of the last heartbeat, or Time(0) if no heartbeat 52 // Returns the time of the last heartbeat, or Time(0) if no heartbeat
50 // has ever happened. 53 // has ever happened.
51 base::Time last_heartbeat() const { return last_heartbeat_; } 54 base::Time last_heartbeat() const { return last_heartbeat_; }
52 55
53 // GCMAppHandler overrides. 56 // GCMAppHandler overrides.
54 void ShutdownHandler() override; 57 void ShutdownHandler() override;
55 void OnMessage(const std::string& app_id, 58 void OnMessage(const std::string& app_id,
56 const gcm::IncomingMessage& message) override; 59 const gcm::IncomingMessage& message) override;
57 void OnMessagesDeleted(const std::string& app_id) override; 60 void OnMessagesDeleted(const std::string& app_id) override;
58 void OnSendError(const std::string& app_id, 61 void OnSendError(const std::string& app_id,
59 const gcm::GCMClient::SendErrorDetails& details) override; 62 const gcm::GCMClient::SendErrorDetails& details) override;
60 void OnSendAcknowledged(const std::string& app_id, 63 void OnSendAcknowledged(const std::string& app_id,
61 const std::string& message_id) override; 64 const std::string& message_id) override;
62 65
66 void SetCloudPolicyClientForTesting(policy::CloudPolicyClient* client);
67
68 bool latest_gcm_id_mapping_succeeded() const {
69 return latest_gcm_id_mapping_succeeded_;
70 }
71
63 private: 72 private:
64 // Callback invoked periodically to send a heartbeat to the policy service. 73 // Callback invoked periodically to send a heartbeat to the policy service.
65 void SendHeartbeat(); 74 void SendHeartbeat();
66 75
67 // Invoked after GCM registration has successfully completed. 76 // Invoked after GCM registration has successfully completed.
68 void OnRegistrationComplete(const std::string& registration_id); 77 void OnRegistrationComplete(const std::string& registration_id);
69 78
70 // Invoked after a heartbeat has been sent to the server. 79 // Invoked after a heartbeat has been sent to the server.
71 void OnHeartbeatSent(const std::string& message_id, 80 void OnHeartbeatSent(const std::string& message_id,
72 gcm::GCMClient::Result result); 81 gcm::GCMClient::Result result);
73 82
74 // Helper method that figures out when the next heartbeat should 83 // Helper method that figures out when the next heartbeat should
75 // be scheduled. 84 // be scheduled.
76 void ScheduleNextHeartbeat(); 85 void ScheduleNextHeartbeat();
77 86
78 // Updates the heartbeat-enabled status and frequency from settings and 87 // Updates the heartbeat-enabled status and frequency from settings and
79 // schedules the next heartbeat. 88 // schedules the next heartbeat.
80 void RefreshHeartbeatSettings(); 89 void RefreshHeartbeatSettings();
81 90
82 // Ensures that the passed interval is within a valid range (not too large or 91 // Ensures that the passed interval is within a valid range (not too large or
83 // too small). 92 // too small).
84 base::TimeDelta EnsureValidHeartbeatInterval(const base::TimeDelta& interval); 93 base::TimeDelta EnsureValidHeartbeatInterval(const base::TimeDelta& interval);
85 94
86 // Shuts down our GCM connection (called when heartbeats are disabled). 95 // Shuts down our GCM connection (called when heartbeats are disabled).
87 void ShutdownGCM(); 96 void ShutdownGCM();
88 97
98 // Callback for the update GCM id mapping request.
99 void OnGcmIdMappingRequestSent(bool status);
100
89 // TaskRunner used for scheduling heartbeats. 101 // TaskRunner used for scheduling heartbeats.
90 const scoped_refptr<base::SequencedTaskRunner> task_runner_; 102 const scoped_refptr<base::SequencedTaskRunner> task_runner_;
91 103
92 // The domain that this device is enrolled to. 104 // The domain that this device is enrolled to.
93 const std::string enrollment_domain_; 105 const std::string enrollment_domain_;
94 106
95 // The device_id for this device - sent up with the enrollment domain with 107 // The device_id for this device - sent up with the enrollment domain with
96 // heartbeats to identify the device to the server. 108 // heartbeats to identify the device to the server.
97 const std::string device_id_; 109 const std::string device_id_;
98 110
(...skipping 10 matching lines...) Expand all
109 heartbeat_frequency_observer_; 121 heartbeat_frequency_observer_;
110 scoped_ptr<chromeos::CrosSettings::ObserverSubscription> 122 scoped_ptr<chromeos::CrosSettings::ObserverSubscription>
111 heartbeat_enabled_observer_; 123 heartbeat_enabled_observer_;
112 124
113 // The time the last heartbeat was sent. 125 // The time the last heartbeat was sent.
114 base::Time last_heartbeat_; 126 base::Time last_heartbeat_;
115 127
116 // Callback invoked via a delay to send a heartbeat. 128 // Callback invoked via a delay to send a heartbeat.
117 base::CancelableClosure heartbeat_callback_; 129 base::CancelableClosure heartbeat_callback_;
118 130
131 policy::CloudPolicyClient* cloud_policy_client_;
132
119 // The GCMDriver used to send heartbeat messages. 133 // The GCMDriver used to send heartbeat messages.
120 gcm::GCMDriver* const gcm_driver_; 134 gcm::GCMDriver* const gcm_driver_;
121 135
122 // The GCM registration ID - if empty, we are not registered yet. 136 // The GCM registration ID - if empty, we are not registered yet.
123 std::string registration_id_; 137 std::string registration_id_;
124 138
125 // If true, we are already registered with GCM and should unregister when 139 // If true, we are already registered with GCM and should unregister when
126 // destroyed. 140 // destroyed.
127 bool registered_app_handler_ = false; 141 bool registered_app_handler_ = false;
128 142
143 // Whether latest GCM id mapping request is successful or not.
144 bool latest_gcm_id_mapping_succeeded_ = false;
145
129 // Helper class to manage registering with the GCM server, including 146 // Helper class to manage registering with the GCM server, including
130 // retries, etc. 147 // retries, etc.
131 scoped_ptr<HeartbeatRegistrationHelper> registration_helper_; 148 scoped_ptr<HeartbeatRegistrationHelper> registration_helper_;
132 149
133 // Note: This should remain the last member so it'll be destroyed and 150 // Note: This should remain the last member so it'll be destroyed and
134 // invalidate the weak pointers before any other members are destroyed. 151 // invalidate the weak pointers before any other members are destroyed.
135 base::WeakPtrFactory<HeartbeatScheduler> weak_factory_; 152 base::WeakPtrFactory<HeartbeatScheduler> weak_factory_;
136 153
137 DISALLOW_COPY_AND_ASSIGN(HeartbeatScheduler); 154 DISALLOW_COPY_AND_ASSIGN(HeartbeatScheduler);
138 }; 155 };
139 156
140 } // namespace policy 157 } // namespace policy
141 158
142 #endif // CHROME_BROWSER_CHROMEOS_POLICY_HEARTBEAT_SCHEDULER_H_ 159 #endif // CHROME_BROWSER_CHROMEOS_POLICY_HEARTBEAT_SCHEDULER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698