File: synced_network_metrics_logger.cc

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 (309 lines) | stat: -rw-r--r-- 12,013 bytes parent folder | download | duplicates (8)
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/sync_wifi/synced_network_metrics_logger.h"

#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_functions.h"
#include "chromeos/ash/components/network/network_configuration_handler.h"
#include "chromeos/ash/components/network/network_connection_handler.h"
#include "chromeos/ash/components/network/network_metadata_store.h"
#include "chromeos/ash/components/network/network_state_handler.h"
#include "chromeos/ash/components/sync_wifi/network_eligibility_checker.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
#include "third_party/cros_system_api/dbus/shill/dbus-constants.h"

namespace ash::sync_wifi {

namespace {

bool IsAuthenticationError(ConnectionFailureReason reason) {
  switch (reason) {
    case ConnectionFailureReason::kFailedToConnect:
    case ConnectionFailureReason::kDhcpFailure:
    case ConnectionFailureReason::kDnsLookupFailure:
    case ConnectionFailureReason::kOutOfRange:
    case ConnectionFailureReason::kNotAssociated:
    case ConnectionFailureReason::kTooManySTAs:
      return false;

    case ConnectionFailureReason::kBadPassphrase:
    case ConnectionFailureReason::kNoFailure:
    case ConnectionFailureReason::kBadWepKey:
    case ConnectionFailureReason::kEapAuthentication:
    case ConnectionFailureReason::kEapLocalTls:
    case ConnectionFailureReason::kEapRemoteTls:
    case ConnectionFailureReason::kPinMissing:
    case ConnectionFailureReason::kNotAuthenticated:
    case ConnectionFailureReason::kUnknown:
    default:
      return true;
  }
}

}  // namespace

// static
ConnectionFailureReason
SyncedNetworkMetricsLogger::ConnectionFailureReasonToEnum(
    const std::string& reason) {
  if (reason == shill::kErrorBadPassphrase)
    return ConnectionFailureReason::kBadPassphrase;
  else if (reason == shill::kErrorBadWEPKey)
    return ConnectionFailureReason::kBadWepKey;
  else if (reason == shill::kErrorConnectFailed)
    return ConnectionFailureReason::kFailedToConnect;
  else if (reason == shill::kErrorDhcpFailed)
    return ConnectionFailureReason::kDhcpFailure;
  else if (reason == shill::kErrorDNSLookupFailed)
    return ConnectionFailureReason::kDnsLookupFailure;
  else if (reason == shill::kErrorEapAuthenticationFailed)
    return ConnectionFailureReason::kEapAuthentication;
  else if (reason == shill::kErrorEapLocalTlsFailed)
    return ConnectionFailureReason::kEapLocalTls;
  else if (reason == shill::kErrorEapRemoteTlsFailed)
    return ConnectionFailureReason::kEapRemoteTls;
  else if (reason == shill::kErrorOutOfRange)
    return ConnectionFailureReason::kOutOfRange;
  else if (reason == shill::kErrorPinMissing)
    return ConnectionFailureReason::kPinMissing;
  else if (reason == shill::kErrorNoFailure)
    return ConnectionFailureReason::kNoFailure;
  else if (reason == shill::kErrorNotAssociated)
    return ConnectionFailureReason::kNotAssociated;
  else if (reason == shill::kErrorNotAuthenticated)
    return ConnectionFailureReason::kNotAuthenticated;
  else if (reason == shill::kErrorTooManySTAs)
    return ConnectionFailureReason::kTooManySTAs;
  else
    return ConnectionFailureReason::kUnknown;
}

// static
ApplyNetworkFailureReason SyncedNetworkMetricsLogger::ApplyFailureReasonToEnum(
    const std::string& reason) {
  if (reason == shill::kErrorResultAlreadyExists) {
    return ApplyNetworkFailureReason::kAlreadyExists;
  } else if (reason == shill::kErrorResultInProgress) {
    return ApplyNetworkFailureReason::kInProgress;
  } else if (reason == shill::kErrorInternal ||
             reason == shill::kErrorResultInternalError) {
    return ApplyNetworkFailureReason::kInternalError;
  } else if (reason == shill::kErrorResultInvalidArguments) {
    return ApplyNetworkFailureReason::kInvalidArguments;
  } else if (reason == shill::kErrorResultInvalidNetworkName) {
    return ApplyNetworkFailureReason::kInvalidNetworkName;
  } else if (reason == shill::kErrorResultInvalidPassphrase ||
             reason == shill::kErrorBadPassphrase) {
    return ApplyNetworkFailureReason::kInvalidPassphrase;
  } else if (reason == shill::kErrorResultInvalidProperty) {
    return ApplyNetworkFailureReason::kInvalidProperty;
  } else if (reason == shill::kErrorResultNotSupported) {
    return ApplyNetworkFailureReason::kNotSupported;
  } else if (reason == shill::kErrorResultPassphraseRequired) {
    return ApplyNetworkFailureReason::kPassphraseRequired;
  } else if (reason == shill::kErrorResultPermissionDenied) {
    return ApplyNetworkFailureReason::kPermissionDenied;
  } else {
    return ApplyNetworkFailureReason::kUnknown;
  }
}

SyncedNetworkMetricsLogger::SyncedNetworkMetricsLogger(
    NetworkStateHandler* network_state_handler,
    NetworkConnectionHandler* network_connection_handler) {
  initialized_timestamp_ = base::Time::Now();

  if (network_state_handler) {
    network_state_handler_ = network_state_handler;
    network_state_handler_observer_.Observe(network_state_handler_.get());
  }

  if (network_connection_handler) {
    network_connection_handler_ = network_connection_handler;
    network_connection_handler_->AddObserver(this);
  }

  const NetworkState* active_wifi =
      NetworkHandler::Get()->network_state_handler()->ConnectedNetworkByType(
          NetworkTypePattern::WiFi());
  if (active_wifi && IsEligible(active_wifi)) {
    base::UmaHistogramBoolean(kConnectionResultAllHistogram, true);
  }
}

SyncedNetworkMetricsLogger::~SyncedNetworkMetricsLogger() {
  OnShuttingDown();
}

void SyncedNetworkMetricsLogger::OnShuttingDown() {
  network_state_handler_observer_.Reset();
  network_state_handler_ = nullptr;

  if (network_connection_handler_) {
    network_connection_handler_->RemoveObserver(this);
    network_connection_handler_ = nullptr;
  }
}

void SyncedNetworkMetricsLogger::ConnectSucceeded(
    const std::string& service_path) {
  const NetworkState* network =
      network_state_handler_->GetNetworkState(service_path);
  if (!IsEligible(network))
    return;

  base::UmaHistogramBoolean(kConnectionResultManualHistogram, true);
}

void SyncedNetworkMetricsLogger::ConnectFailed(const std::string& service_path,
                                               const std::string& error_name) {
  const NetworkState* network =
      network_state_handler_->GetNetworkState(service_path);
  if (!IsEligible(network))
    return;

  // Get the the current state and error info.
  NetworkHandler::Get()->network_configuration_handler()->GetShillProperties(
      service_path,
      base::BindOnce(&SyncedNetworkMetricsLogger::OnConnectErrorGetProperties,
                     weak_ptr_factory_.GetWeakPtr(), error_name));
}

void SyncedNetworkMetricsLogger::NetworkConnectionStateChanged(
    const NetworkState* network) {
  if (!IsEligible(network)) {
    return;
  }

  if (network->IsConnectingState()) {
    if (!connecting_guids_.contains(network->guid())) {
      connecting_guids_.insert(network->guid());
    }
    return;
  }

  // Require that the network was previously in the 'connecting' state before
  // transitioning to 'connected' in order to prevent double-counting a network
  // if this function is executed multiple times while connected.  This is
  // skipped when this class was recently created since 'connecting' may have
  // happened before we were tracking.
  if (!connecting_guids_.contains(network->guid()) &&
      (base::Time::Now() - initialized_timestamp_) > base::Seconds(5)) {
    return;
  }

  if (network->connection_state() == shill::kStateFailure) {
    ConnectionFailureReason reason =
        ConnectionFailureReasonToEnum(network->GetError());

    // Don't consider non-auth errors as failures.
    if (IsAuthenticationError(reason)) {
      base::UmaHistogramBoolean(kConnectionResultAllHistogram, false);
    }
    base::UmaHistogramEnumeration(kConnectionFailureReasonAllHistogram, reason);
  } else if (network->IsConnectedState()) {
    base::UmaHistogramBoolean(kConnectionResultAllHistogram, true);
  }
  connecting_guids_.erase(network->guid());
}

bool SyncedNetworkMetricsLogger::IsEligible(const NetworkState* network) {
  // Only non-tether Wi-Fi networks are eligible for logging.
  if (!network || network->type() != shill::kTypeWifi ||
      !network->tether_guid().empty()) {
    return false;
  }

  NetworkMetadataStore* metadata_store =
      NetworkHandler::Get()->network_metadata_store();
  if (!metadata_store) {
    return false;
  }

  return metadata_store->GetIsConfiguredBySync(network->guid());
}

void SyncedNetworkMetricsLogger::OnConnectErrorGetProperties(
    const std::string& error_name,
    const std::string& service_path,
    std::optional<base::Value::Dict> shill_properties) {
  if (!shill_properties) {
    base::UmaHistogramBoolean(kConnectionResultManualHistogram, false);
    base::UmaHistogramEnumeration(kConnectionFailureReasonManualHistogram,
                                  ConnectionFailureReasonToEnum(error_name));
    return;
  }
  const std::string* state =
      shill_properties->FindString(shill::kStateProperty);
  if (state && (NetworkState::StateIsConnected(*state) ||
                NetworkState::StateIsConnecting(*state))) {
    // If network is no longer in an error state, don't record it.
    return;
  }

  const std::string* shill_error =
      shill_properties->FindString(shill::kErrorProperty);
  if (!shill_error || !NetworkState::ErrorIsValid(*shill_error)) {
    shill_error = shill_properties->FindString(shill::kPreviousErrorProperty);
    if (!shill_error || !NetworkState::ErrorIsValid(*shill_error))
      shill_error = &error_name;
  }
  base::UmaHistogramBoolean(kConnectionResultManualHistogram, false);
  base::UmaHistogramEnumeration(kConnectionFailureReasonManualHistogram,
                                ConnectionFailureReasonToEnum(*shill_error));
}

void SyncedNetworkMetricsLogger::RecordApplyNetworkSuccess() {
  base::UmaHistogramBoolean(kApplyResultHistogram, true);
}
void SyncedNetworkMetricsLogger::RecordApplyNetworkFailed() {
  base::UmaHistogramBoolean(kApplyResultHistogram, false);
}

void SyncedNetworkMetricsLogger::RecordApplyGenerateLocalNetworkConfig(
    bool success) {
  base::UmaHistogramBoolean(kApplyGenerateLocalNetworkConfigHistogram, success);
}

void SyncedNetworkMetricsLogger::RecordApplyNetworkFailureReason(
    ApplyNetworkFailureReason error_enum,
    const std::string& error_string) {
  // Get a specific error from the |error_string| if available.
  ApplyNetworkFailureReason reason = ApplyFailureReasonToEnum(error_string);
  if (reason == ApplyNetworkFailureReason::kUnknown) {
    // Fallback on the provided enum.
    reason = error_enum;
  }

  base::UmaHistogramEnumeration(kApplyFailureReasonHistogram, reason);
}

void SyncedNetworkMetricsLogger::RecordTotalCount(int count) {
  base::UmaHistogramCounts1000(kTotalCountHistogram, count);
}

void SyncedNetworkMetricsLogger::RecordZeroNetworksEligibleForSync(
    base::flat_set<NetworkEligibilityStatus> network_eligibility_status_codes) {
  // There is an eligible network that was not synced for some reason.
  if (base::Contains(network_eligibility_status_codes,
                     NetworkEligibilityStatus::kNetworkIsEligible)) {
    base::UmaHistogramEnumeration(kZeroNetworksSyncedReasonHistogram,
                                  NetworkEligibilityStatus::kNetworkIsEligible);
    return;
  }

  if (network_eligibility_status_codes.size() == 0) {
    network_eligibility_status_codes.insert(
        NetworkEligibilityStatus::kNoWifiNetworksAvailable);
  }
  for (const NetworkEligibilityStatus network_eligibility_status_code :
       network_eligibility_status_codes) {
    base::UmaHistogramEnumeration(kZeroNetworksSyncedReasonHistogram,
                                  network_eligibility_status_code);
  }
}

}  // namespace ash::sync_wifi