File: hotspot_metrics_helper.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (316 lines) | stat: -rw-r--r-- 13,486 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROMEOS_ASH_COMPONENTS_NETWORK_METRICS_HOTSPOT_METRICS_HELPER_H_
#define CHROMEOS_ASH_COMPONENTS_NETWORK_METRICS_HOTSPOT_METRICS_HELPER_H_

#include <optional>

#include "base/component_export.h"
#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "base/time/time.h"
#include "base/timer/elapsed_timer.h"
#include "base/timer/timer.h"
#include "chromeos/ash/components/login/login_state/login_state.h"
#include "chromeos/ash/components/network/hotspot_capabilities_provider.h"
#include "chromeos/ash/components/network/hotspot_enabled_state_notifier.h"
#include "chromeos/ash/components/network/hotspot_state_handler.h"
#include "chromeos/ash/services/hotspot_config/public/mojom/cros_hotspot_config.mojom.h"
#include "mojo/public/cpp/bindings/receiver.h"

namespace ash {

class EnterpriseManagedMetadataStore;
class HotspotConfigurationHandler;
class HotspotController;

// This class is used to track the hotspot capabilities and status update and
// emits UMA metrics to the related histogram.
class COMPONENT_EXPORT(CHROMEOS_NETWORK) HotspotMetricsHelper
    : public LoginState::Observer,
      public HotspotCapabilitiesProvider::Observer,
      public HotspotStateHandler::Observer,
      public hotspot_config::mojom::HotspotEnabledStateObserver {
 public:
  // Emits enable/disable hotspot operation result to related UMA histogram.
  static void RecordSetTetheringEnabledResult(
      bool enabled,
      hotspot_config::mojom::HotspotControlResult result);

  // Emits check tethering readiness operation result to related UMA histogram.
  static void RecordCheckTetheringReadinessResult(
      HotspotCapabilitiesProvider::CheckTetheringReadinessResult result);

  // Emits set hotspot configuration operation result to related UMA histogram.
  static void RecordSetHotspotConfigResult(
      hotspot_config::mojom::SetHotspotConfigResult result,
      const std::string& shill_error = "");

  // Emits hotspot enable operation latency to related UMA histogram.
  static void RecordEnableHotspotLatency(const base::TimeDelta& latency);

  HotspotMetricsHelper();
  HotspotMetricsHelper(const HotspotMetricsHelper&) = delete;
  HotspotMetricsHelper& operator=(const HotspotMetricsHelper&) = delete;
  ~HotspotMetricsHelper() override;

  void Init(EnterpriseManagedMetadataStore* enterprise_managed_metadata_store,
            HotspotCapabilitiesProvider* hotspot_capabilities_provider,
            HotspotStateHandler* hotspot_state_handler,
            HotspotController* hotspot_controller,
            HotspotConfigurationHandler* hotspot_configuration_handler,
            HotspotEnabledStateNotifier* hotspot_enabled_state_notifier,
            NetworkStateHandler* network_state_handler);

 private:
  friend class HotspotMetricsHelperTest;
  friend class HotspotControllerTest;
  friend class HotspotControllerConcurrencyApiTest;

  FRIEND_TEST_ALL_PREFIXES(HotspotMetricsHelperTest,
                           HotspotAllowStatusHistogram);
  FRIEND_TEST_ALL_PREFIXES(HotspotMetricsHelperTest,
                           HotspotUsageConfigHistogram);
  FRIEND_TEST_ALL_PREFIXES(HotspotMetricsHelperTest,
                           HotspotUsageDurationHistogram);
  FRIEND_TEST_ALL_PREFIXES(HotspotMetricsHelperTest,
                           HotspotMaxClientCountHistogram);
  FRIEND_TEST_ALL_PREFIXES(HotspotMetricsHelperTest,
                           HotspotIsDeviceManagedHistogram);
  FRIEND_TEST_ALL_PREFIXES(HotspotMetricsHelperTest,
                           HotspotEnabledUpstreamStatusHistogram);
  FRIEND_TEST_ALL_PREFIXES(HotspotMetricsHelperTest,
                           HotspotDisableReasonHistogram);
  FRIEND_TEST_ALL_PREFIXES(HotspotMetricsHelperTest, HotspotSetConfigHistogram);
  FRIEND_TEST_ALL_PREFIXES(HotspotControllerTest, EnableTetheringSuccess);
  FRIEND_TEST_ALL_PREFIXES(HotspotControllerTest, AbortEnableTethering);
  FRIEND_TEST_ALL_PREFIXES(HotspotControllerTest,
                           ShillOperationFailureWhileAborting);
  FRIEND_TEST_ALL_PREFIXES(HotspotControllerTest,
                           EnableTetheringReadinessCheckFailure);
  FRIEND_TEST_ALL_PREFIXES(HotspotControllerTest,
                           EnableTetheringNetworkSetupFailure);
  FRIEND_TEST_ALL_PREFIXES(HotspotControllerTest, DisableTetheringSuccess);
  FRIEND_TEST_ALL_PREFIXES(HotspotControllerConcurrencyApiTest,
                           EnableTetheringSuccess);
  FRIEND_TEST_ALL_PREFIXES(HotspotControllerConcurrencyApiTest,
                           AbortEnableTethering);
  FRIEND_TEST_ALL_PREFIXES(HotspotControllerConcurrencyApiTest,
                           ShillOperationFailureWhileAborting);
  FRIEND_TEST_ALL_PREFIXES(HotspotControllerConcurrencyApiTest,
                           EnableTetheringReadinessCheckFailure);
  FRIEND_TEST_ALL_PREFIXES(HotspotControllerConcurrencyApiTest,
                           EnableTetheringNetworkSetupFailure);
  FRIEND_TEST_ALL_PREFIXES(HotspotControllerConcurrencyApiTest,
                           DisableTetheringSuccess);
  FRIEND_TEST_ALL_PREFIXES(HotspotConfigurationHandlerTest,
                           SetAndGetHotspotConfig);
  FRIEND_TEST_ALL_PREFIXES(HotspotCapabilitiesProviderTest,
                           CheckTetheringReadiness_Ready);
  FRIEND_TEST_ALL_PREFIXES(HotspotCapabilitiesProviderTest,
                           CheckTetheringReadiness_NotAllowed);
  FRIEND_TEST_ALL_PREFIXES(HotspotCapabilitiesProviderTest,
                           CheckTetheringReadiness_NotAllowedByCarrier);
  FRIEND_TEST_ALL_PREFIXES(HotspotCapabilitiesProviderTest,
                           CheckTetheringReadiness_UpstreamNotAvailable);
  FRIEND_TEST_ALL_PREFIXES(HotspotCapabilitiesProviderTest,
                           CheckTetheringReadiness_EmptyResult);
  FRIEND_TEST_ALL_PREFIXES(HotspotCapabilitiesProviderTest,
                           CheckTetheringReadiness_Failure);

  enum class HotspotMetricsSetEnabledResult;
  enum class HotspotMetricsSetConfigResult;
  enum class HotspotMetricsCheckReadinessResult;
  enum class HotspotMetricsDisableReason;

  static const char kHotspotAllowStatusHistogram[];
  static const char kHotspotAllowStatusAtLoginHistogram[];
  static const char kHotspotEnableResultHistogram[];
  static const char kHotspotDisableResultHistogram[];
  static const char kHotspotSetConfigResultHistogram[];
  static const char kHotspotCheckReadinessResultHistogram[];
  static const char kHotspotUsageConfigAutoDisable[];
  static const char kHotspotUsageConfigMAR[];
  static const char kHotspotUsageConfigCompatibilityMode[];
  static const char kHotspotUsageDuration[];
  static const char kHotspotMaxClientCount[];
  static const char kHotspotIsDeviceManaged[];
  static const char kHotspotEnableLatency[];
  static const char kHotspotUpstreamStatusWhenEnabled[];
  static const char kHotspotDisableReasonHistogram[];
  static const base::TimeDelta kLogAllowStatusAtLoginTimeout;

  static HotspotMetricsCheckReadinessResult GetCheckReadinessMetricsResult(
      const HotspotCapabilitiesProvider::CheckTetheringReadinessResult& result);
  static HotspotMetricsSetEnabledResult GetSetEnabledMetricsResult(
      const hotspot_config::mojom::HotspotControlResult& result);
  static HotspotMetricsSetConfigResult GetSetConfigMetricsResult(
      const hotspot_config::mojom::SetHotspotConfigResult& result,
      const std::string& shill_error);
  static HotspotMetricsDisableReason GetMetricsDisableReason(
      const hotspot_config::mojom::DisableReason& reason);

  // Represents the hotspot allow status on device. Note:
  // kDisallowNoCellularUpstream is not logged in the metric because it means
  // the device is not cellular capable, and it would drown out the metric by
  // adding the bucket. These values are persisted to logs. Entries should not
  // be renumbered and numeric values should never be reused.
  enum class HotspotMetricsAllowStatus {
    kAllowed = 0,
    kDisallowedWiFiDownstreamNotSupported = 1,
    kDisallowedNoWiFiSecurityModes = 2,
    kDisallowedNoMobileData = 3,
    kDisallowedReadinessCheckFail = 4,
    kDisallowedByPolicy = 5,
    kMaxValue = kDisallowedByPolicy,
  };

  // Represents the operation result of set hotspot configuration used for
  // related UMA histogram. These values are persisted to logs. Entries should
  // not be renumbered and numeric values should never be reused.
  enum class HotspotMetricsSetConfigResult {
    kSuccess = 0,
    kFailedNotLogin = 1,
    kFailedInvalidConfiguration = 2,
    kFailedIllegalOperation = 3,
    kFailedPermissionDenied = 4,
    kFailedInvalidArgument = 5,
    kFailedShillOperation = 6,
    kFailedUnknownShillError = 7,
    kMaxValue = kFailedUnknownShillError,
  };

  // Represents the operation result of check tethering readiness used for
  // related UMA histogram. These values are persisted to logs. Entries should
  // not be renumbered and numeric values should never be reused.
  enum class HotspotMetricsCheckReadinessResult {
    kReady = 0,
    kNotAllowed = 1,
    kUpstreamNetworkNotAvailable = 2,
    kShillOperationFailed = 3,
    kUnknownResult = 4,
    kNotAllowedByCarrier = 5,
    kNotAllowedOnFW = 6,
    kNotAllowedOnVariant = 7,
    kNotAllowedUserNotEntitled = 8,
    kMaxValue = kNotAllowedUserNotEntitled,
  };

  // Represents the operation result of enable/disable hotspot used for related
  // UMA histograms. These values are persisted to logs. Entries should not be
  // renumbered and numeric values should never be reused.
  enum class HotspotMetricsSetEnabledResult {
    kSuccess = 0,
    kNotAllowed = 1,
    kReadinessCheckFailure = 2,
    kDisableWifiFailure = 3,
    kInvalidConfiguration = 4,
    kUpstreamNotAvailable = 5,
    kNetworkSetupFailure = 6,
    kDownstreamWifiFailure = 7,
    kUpstreamFailure = 8,
    kShillOperationFailure = 9,
    kUnknownFailure = 10,
    kAlreadyFulfilled = 11,
    kAborted = 12,
    kInvalid = 13,
    kBusy = 14,
    kConcurrencyNotSupported = 15,
    kOperationFailure = 16,
    kMaxValue = kOperationFailure,
  };

  // Represents the upstream status when hotspot is enabled. These values are
  // persisted to logs. Entries should not be renumbered and numeric values
  // should never be reused.
  enum class HotspotMetricsUpstreamStatus {
    kWifiWithCellularConnected = 0,
    kWifiWithCellularNotConnected = 1,
    kMaxValue = kWifiWithCellularNotConnected,
  };

  // Represents the hotspot disable reason. These values are persisted to logs.
  // Entries should not be renumbered and numeric values should never be used.
  enum class HotspotMetricsDisableReason {
    kAutoDisabled = 0,
    kInternalError = 1,
    kUserInitiated = 2,
    kWifiEnabled = 3,
    kProhibitedByPolicy = 4,
    kUpstreamNetworkNotAvailable = 5,
    kSuspended = 6,
    kRestart = 7,
    kUpstreamNoInternet = 8,
    kDownstreamLinkDisconnect = 9,
    kDownstreamNetworkDisconnect = 10,
    kStartTimeout = 11,
    kUpstreamNotAvailable = 12,
    kUnknownError = 13,
    kResourceBusy = 14,
    kMaxValue = kResourceBusy,
  };

  // HotspotCapabilitiesProvider::Observer:
  void OnHotspotCapabilitiesChanged() override;

  // HotspotStateHandler::Observer:
  void OnHotspotStatusChanged() override;

  // LoginState::Observer:
  void LoggedInStateChanged() override;

  // hotspot_config::mojom::HotspotEnabledStateObserver:
  void OnHotspotTurnedOn() override;
  void OnHotspotTurnedOff(hotspot_config::mojom::DisableReason reason) override;

  void LogAllowStatus();
  void LogAllowStatusAtLogin();
  void LogUsageConfig();
  void LogUsageDuration();
  void LogMaxClientCount();
  void LogIsDeviceManaged();
  void LogUpstreamStatus();
  void LogDisableReason(const hotspot_config::mojom::DisableReason& reason);

  // Retrieves the latest hotspot allow status and converts to
  // HotspotMetricsAllowStatus enum. Return std::nullopt if it is disallowed
  // due to device is not cellular capable.
  std::optional<HotspotMetricsAllowStatus> GetMetricsAllowStatus();

  raw_ptr<EnterpriseManagedMetadataStore> enterprise_managed_metadata_store_ =
      nullptr;
  raw_ptr<HotspotCapabilitiesProvider> hotspot_capabilities_provider_ = nullptr;
  raw_ptr<HotspotStateHandler> hotspot_state_handler_ = nullptr;
  raw_ptr<HotspotConfigurationHandler> hotspot_configuration_handler_ = nullptr;
  raw_ptr<HotspotEnabledStateNotifier, DanglingUntriaged>
      hotspot_enabled_state_notifier_ = nullptr;
  raw_ptr<NetworkStateHandler> network_state_handler_ = nullptr;

  // A timer to wait for user connecting to their upstream cellular network
  // after login.
  base::OneShotTimer timer_;

  // Tracks the maximum connected client count per hotspot session.
  size_t max_client_count_ = 0;

  // Tracks whether the metrics are already logged for this session.
  bool is_metrics_logged_ = false;

  // Tracks whether the hotspot is active.
  bool is_hotspot_active_ = false;

  // Tracks the usage time for each hotspot session.
  std::optional<base::ElapsedTimer> usage_timer_;

  // Tracks if the device is enterprise managed or not.
  bool is_enterprise_managed_ = false;

  mojo::Receiver<hotspot_config::mojom::HotspotEnabledStateObserver>
      hotspot_enabled_state_notifier_receiver_{this};
};

}  // namespace ash

#endif  // CHROMEOS_ASH_COMPONENTS_NETWORK_METRICS_HOTSPOT_METRICS_HELPER_H_