File: heartbeat_scheduler.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (167 lines) | stat: -rw-r--r-- 6,039 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_ASH_POLICY_UPLOADING_HEARTBEAT_SCHEDULER_H_
#define CHROME_BROWSER_ASH_POLICY_UPLOADING_HEARTBEAT_SCHEDULER_H_

#include <stdint.h>

#include <memory>
#include <string>

#include "base/cancelable_callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "chromeos/ash/components/settings/cros_settings.h"
#include "components/gcm_driver/gcm_app_handler.h"
#include "components/gcm_driver/gcm_client.h"
#include "components/gcm_driver/gcm_connection_observer.h"
#include "components/policy/core/common/cloud/cloud_policy_client.h"
#include "components/policy/core/common/cloud/cloud_policy_store.h"

namespace base {
class SequencedTaskRunner;
}

namespace gcm {
class GCMDriver;
}

namespace policy {

class HeartbeatRegistrationHelper;

// Class responsible for periodically sending heartbeats to the policy service
// for monitoring device connectivity.
class HeartbeatScheduler : public gcm::GCMAppHandler,
                           gcm::GCMConnectionObserver {
 public:
  // Default interval for how often we send up a heartbeat.
  static const base::TimeDelta kDefaultHeartbeatInterval;

  // Constructor. |cloud_policy_client| will be used to send registered GCM id
  // to DM server, and can be null. |driver| can be null for tests.
  HeartbeatScheduler(
      gcm::GCMDriver* driver,
      CloudPolicyClient* cloud_policy_client,
      CloudPolicyStore* cloud_policy_store,
      const std::string& device_id,
      const scoped_refptr<base::SequencedTaskRunner>& task_runner);

  HeartbeatScheduler(const HeartbeatScheduler&) = delete;
  HeartbeatScheduler& operator=(const HeartbeatScheduler&) = delete;

  ~HeartbeatScheduler() override;

  // Returns the time of the last heartbeat, or Time(0) if no heartbeat
  // has ever happened.
  base::Time last_heartbeat() const { return last_heartbeat_; }

  // GCMAppHandler overrides.
  void ShutdownHandler() override;
  void OnStoreReset() override;
  void OnMessage(const std::string& app_id,
                 const gcm::IncomingMessage& message) override;
  void OnMessagesDeleted(const std::string& app_id) override;
  void OnSendError(const std::string& app_id,
                   const gcm::GCMClient::SendErrorDetails& details) override;
  void OnSendAcknowledged(const std::string& app_id,
                          const std::string& message_id) override;

  // GCMConnectionObserver overrides.
  void OnConnected(const net::IPEndPoint&) override;

 private:
  // Callback invoked periodically to send a heartbeat to the policy service.
  void SendHeartbeat();

  // Invoked after GCM registration has successfully completed.
  void OnRegistrationComplete(const std::string& registration_id);

  // Invoked after a heartbeat has been sent to the server.
  void OnHeartbeatSent(const std::string& message_id,
                       gcm::GCMClient::Result result);

  // Invoked after a upstream notification sign up message has been sent.
  void OnUpstreamNotificationSent(const std::string& message_id,
                                  gcm::GCMClient::Result result);

  // Helper method that figures out when the next heartbeat should
  // be scheduled.
  void ScheduleNextHeartbeat();

  // Updates the heartbeat-enabled status and frequency from settings and
  // schedules the next heartbeat.
  void RefreshHeartbeatSettings();

  // Ensures that the passed interval is within a valid range (not too large or
  // too small).
  base::TimeDelta EnsureValidHeartbeatInterval(const base::TimeDelta& interval);

  // Shuts down our GCM connection (called when heartbeats are disabled).
  void ShutdownGCM();

  // Callback for the GCM id update request.
  void OnGcmIdUpdateRequestSent(bool status);

  // Helper function to signup for upstream notification.
  void SignUpUpstreamNotification();

  // TaskRunner used for scheduling heartbeats.
  const scoped_refptr<base::SequencedTaskRunner> task_runner_;

  // Obfuscated customerId the device is enrolled into.
  // Only set for device policy.
  std::string customer_id_;

  // The device_id for this device - sent up with the enrollment domain with
  // heartbeats to identify the device to the server.
  const std::string device_id_;

  // True if heartbeats are enabled. Kept cached in this object because
  // CrosSettings can switch to an untrusted state temporarily, and we want
  // to use the last-known trusted values.
  bool heartbeat_enabled_;

  // Cached copy of the current heartbeat interval, in milliseconds.
  base::TimeDelta heartbeat_interval_;

  // Subscriptions for callbacks for changes in the heartbeat settings.
  base::CallbackListSubscription heartbeat_frequency_subscription_;
  base::CallbackListSubscription heartbeat_enabled_subscription_;

  // The time the last heartbeat was sent.
  base::Time last_heartbeat_;

  // Callback invoked via a delay to send a heartbeat.
  base::CancelableOnceClosure heartbeat_callback_;

  raw_ptr<CloudPolicyClient> cloud_policy_client_;

  raw_ptr<CloudPolicyStore> cloud_policy_store_;

  // The GCMDriver used to send heartbeat messages.
  const raw_ptr<gcm::GCMDriver> gcm_driver_;

  // The GCM registration ID - if empty, we are not registered yet.
  std::string registration_id_;

  // If true, we are already registered with GCM and should unregister when
  // destroyed.
  bool registered_app_handler_ = false;

  // Helper class to manage registering with the GCM server, including
  // retries, etc.
  std::unique_ptr<HeartbeatRegistrationHelper> registration_helper_;

  // Note: This should remain the last member so it'll be destroyed and
  // invalidate the weak pointers before any other members are destroyed.
  base::WeakPtrFactory<HeartbeatScheduler> weak_factory_{this};
};

}  // namespace policy

#endif  // CHROME_BROWSER_ASH_POLICY_UPLOADING_HEARTBEAT_SCHEDULER_H_