File: hotspot_capabilities_provider.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 (338 lines) | stat: -rw-r--r-- 12,719 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// 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.

#include "chromeos/ash/components/network/hotspot_capabilities_provider.h"

#include "base/containers/contains.h"
#include "chromeos/ash/components/dbus/shill/shill_manager_client.h"
#include "chromeos/ash/components/network/hotspot_allowed_flag_handler.h"
#include "chromeos/ash/components/network/hotspot_util.h"
#include "chromeos/ash/components/network/metrics/hotspot_metrics_helper.h"
#include "chromeos/ash/components/network/network_event_log.h"
#include "third_party/cros_system_api/dbus/shill/dbus-constants.h"

namespace ash {

namespace {

// Convert the base::Value::List type of |allowed_security_modes_in_shill| to
// the corresponding mojom enum and update the value to the
// |allowed_security_modes|.
void UpdateAllowedSecurityList(
    std::vector<hotspot_config::mojom::WiFiSecurityMode>&
        allowed_security_modes,
    const base::Value::List& allowed_security_modes_in_shill) {
  allowed_security_modes.clear();
  for (const base::Value& allowed_security : allowed_security_modes_in_shill) {
    allowed_security_modes.push_back(
        ShillSecurityToMojom(allowed_security.GetString()));
  }
}

bool IsDisallowedByPlatformCapabilities(
    hotspot_config::mojom::HotspotAllowStatus allow_status) {
  using HotspotAllowStatus = hotspot_config::mojom::HotspotAllowStatus;
  return allow_status == HotspotAllowStatus::kDisallowedNoCellularUpstream ||
         allow_status == HotspotAllowStatus::kDisallowedNoWiFiDownstream ||
         allow_status == HotspotAllowStatus::kDisallowedNoWiFiSecurityModes;
}

HotspotCapabilitiesProvider::CheckTetheringReadinessResult
ShillResultToReadinessResult(const std::string& result) {
  if (result == shill::kTetheringReadinessReady) {
    return HotspotCapabilitiesProvider::CheckTetheringReadinessResult::kReady;
  }
  if (result == shill::kTetheringReadinessUpstreamNetworkNotAvailable) {
    return HotspotCapabilitiesProvider::CheckTetheringReadinessResult::
        kUpstreamNetworkNotAvailable;
  }
  if (result == shill::kTetheringReadinessNotAllowedByCarrier) {
    return HotspotCapabilitiesProvider::CheckTetheringReadinessResult::
        kNotAllowedByCarrier;
  }
  if (result == shill::kTetheringReadinessNotAllowedOnFw) {
    return HotspotCapabilitiesProvider::CheckTetheringReadinessResult::
        kNotAllowedOnFW;
  }
  if (result == shill::kTetheringReadinessNotAllowedOnVariant) {
    return HotspotCapabilitiesProvider::CheckTetheringReadinessResult::
        kNotAllowedOnVariant;
  }
  if (result == shill::kTetheringReadinessNotAllowedUserNotEntitled) {
    return HotspotCapabilitiesProvider::CheckTetheringReadinessResult::
        kNotAllowedUserNotEntitled;
  }
  if (result == shill::kTetheringReadinessNotAllowed) {
    return HotspotCapabilitiesProvider::CheckTetheringReadinessResult::
        kNotAllowed;
  }
  NET_LOG(ERROR) << "Unexpected check tethering readiness result: " << result;
  return HotspotCapabilitiesProvider::CheckTetheringReadinessResult::
      kUnknownResult;
}

}  // namespace

HotspotCapabilitiesProvider::HotspotCapabilities::HotspotCapabilities(
    const hotspot_config::mojom::HotspotAllowStatus allow_status)
    : allow_status(allow_status) {}

HotspotCapabilitiesProvider::HotspotCapabilities::~HotspotCapabilities() =
    default;

HotspotCapabilitiesProvider::HotspotCapabilitiesProvider() = default;

HotspotCapabilitiesProvider::~HotspotCapabilitiesProvider() {
  ResetNetworkStateHandler();

  if (ShillManagerClient::Get()) {
    ShillManagerClient::Get()->RemovePropertyChangedObserver(this);
  }
}

void HotspotCapabilitiesProvider::Init(
    NetworkStateHandler* network_state_handler,
    HotspotAllowedFlagHandler* hotspot_allowed_flag_handler) {
  network_state_handler_ = network_state_handler;
  network_state_handler_observer_.Observe(network_state_handler_.get());

  hotspot_allowed_flag_handler_ = hotspot_allowed_flag_handler;

  // Add as an observer here so that new hotspot state updated after this call
  // are recognized.
  ShillManagerClient::Get()->AddPropertyChangedObserver(this);
  ShillManagerClient::Get()->GetProperties(
      base::BindOnce(&HotspotCapabilitiesProvider::OnManagerProperties,
                     weak_ptr_factory_.GetWeakPtr()));
}

void HotspotCapabilitiesProvider::AddObserver(Observer* observer) {
  observer_list_.AddObserver(observer);
}

void HotspotCapabilitiesProvider::RemoveObserver(Observer* observer) {
  observer_list_.RemoveObserver(observer);
}

bool HotspotCapabilitiesProvider::HasObserver(Observer* observer) const {
  return observer_list_.HasObserver(observer);
}

const HotspotCapabilitiesProvider::HotspotCapabilities&
HotspotCapabilitiesProvider::GetHotspotCapabilities() const {
  return hotspot_capabilities_;
}

void HotspotCapabilitiesProvider::OnPropertyChanged(const std::string& key,
                                                    const base::Value& value) {
  if (key == shill::kTetheringCapabilitiesProperty) {
    UpdateHotspotCapabilities(value.GetDict());
  }
}

// The hotspot capabilities is re-calculated when a cellular network connection
// state is changed.
void HotspotCapabilitiesProvider::NetworkConnectionStateChanged(
    const NetworkState* network) {
  using HotspotAllowStatus = hotspot_config::mojom::HotspotAllowStatus;
  // Only check the Cellular connectivity as the upstream technology
  if (!network->Matches(NetworkTypePattern::Cellular())) {
    return;
  }

  // Exit early if the platform capabilities doesn't support hotspot.
  if (IsDisallowedByPlatformCapabilities(hotspot_capabilities_.allow_status)) {
    return;
  }

  if (!policy_allow_hotspot_) {
    SetHotspotAllowStatus(HotspotAllowStatus::kDisallowedByPolicy);
    return;
  }

  if (network->IsOnline()) {
    CheckTetheringReadiness(base::DoNothing());
    return;
  }

  SetHotspotAllowStatus(HotspotAllowStatus::kDisallowedNoMobileData);
}

void HotspotCapabilitiesProvider::OnShuttingDown() {
  ResetNetworkStateHandler();
}

void HotspotCapabilitiesProvider::ResetNetworkStateHandler() {
  if (!network_state_handler_) {
    return;
  }
  network_state_handler_observer_.Reset();
  network_state_handler_ = nullptr;
}

void HotspotCapabilitiesProvider::OnManagerProperties(
    std::optional<base::Value::Dict> properties) {
  if (!properties) {
    NET_LOG(ERROR)
        << "HotspotCapabilitiesProvider: Failed to get manager properties.";
    return;
  }

  const base::Value::Dict* capabilities =
      properties->FindDict(shill::kTetheringCapabilitiesProperty);
  if (!capabilities) {
    NET_LOG(EVENT) << "HotspotCapabilitiesProvider: No dict value for: "
                   << shill::kTetheringCapabilitiesProperty;
  } else {
    UpdateHotspotCapabilities(*capabilities);
  }
}

void HotspotCapabilitiesProvider::UpdateHotspotCapabilities(
    const base::Value::Dict& capabilities) {
  using HotspotAllowStatus = hotspot_config::mojom::HotspotAllowStatus;

  const base::Value::List* upstream_technologies =
      capabilities.FindList(shill::kTetheringCapUpstreamProperty);
  if (!upstream_technologies) {
    NET_LOG(ERROR) << "No list value for: "
                   << shill::kTetheringCapUpstreamProperty << " in "
                   << shill::kTetheringCapabilitiesProperty;
    SetHotspotAllowStatus(HotspotAllowStatus::kDisallowedNoCellularUpstream);
    return;
  }

  if (!base::Contains(*upstream_technologies, shill::kTypeCellular)) {
    SetHotspotAllowStatus(HotspotAllowStatus::kDisallowedNoCellularUpstream);
    return;
  }

  const base::Value::List* downstream_technologies =
      capabilities.FindList(shill::kTetheringCapDownstreamProperty);
  if (!downstream_technologies) {
    NET_LOG(ERROR) << "No list value for: "
                   << shill::kTetheringCapDownstreamProperty << " in "
                   << shill::kTetheringCapabilitiesProperty;
    SetHotspotAllowStatus(HotspotAllowStatus::kDisallowedNoWiFiDownstream);
    return;
  }

  if (!base::Contains(*downstream_technologies, shill::kTypeWifi)) {
    SetHotspotAllowStatus(HotspotAllowStatus::kDisallowedNoWiFiDownstream);
    return;
  }

  // Update allowed security modes for WiFi downstream
  const base::Value::List* allowed_security_modes_in_shill =
      capabilities.FindList(shill::kTetheringCapSecurityProperty);
  if (!allowed_security_modes_in_shill) {
    NET_LOG(ERROR) << "No list value for: "
                   << shill::kTetheringCapSecurityProperty << " in "
                   << shill::kTetheringCapabilitiesProperty;
    SetHotspotAllowStatus(HotspotAllowStatus::kDisallowedNoWiFiSecurityModes);
    return;
  }

  UpdateAllowedSecurityList(hotspot_capabilities_.allowed_security_modes,
                            *allowed_security_modes_in_shill);
  if (hotspot_capabilities_.allowed_security_modes.empty()) {
    SetHotspotAllowStatus(HotspotAllowStatus::kDisallowedNoWiFiSecurityModes);
    return;
  }

  if (!policy_allow_hotspot_) {
    SetHotspotAllowStatus(HotspotAllowStatus::kDisallowedByPolicy);
    return;
  }

  // Check if there's a connected cellular network
  const NetworkState* connected_cellular_network =
      network_state_handler_->ConnectedNetworkByType(
          NetworkTypePattern::Cellular());
  if (!connected_cellular_network) {
    SetHotspotAllowStatus(HotspotAllowStatus::kDisallowedNoMobileData);
    return;
  }

  CheckTetheringReadiness(base::DoNothing());
}

void HotspotCapabilitiesProvider::CheckTetheringReadiness(
    CheckTetheringReadinessCallback callback) {
  hotspot_allowed_flag_handler_->UpdateFlags();
  auto callback_split = base::SplitOnceCallback(std::move(callback));
  ShillManagerClient::Get()->CheckTetheringReadiness(
      base::BindOnce(&HotspotCapabilitiesProvider::OnCheckReadinessSuccess,
                     weak_ptr_factory_.GetWeakPtr(),
                     std::move(callback_split.first)),
      base::BindOnce(&HotspotCapabilitiesProvider::OnCheckReadinessFailure,
                     weak_ptr_factory_.GetWeakPtr(),
                     std::move(callback_split.second)));
}

void HotspotCapabilitiesProvider::OnCheckReadinessSuccess(
    CheckTetheringReadinessCallback callback,
    const std::string& result) {
  using HotspotAllowStatus = hotspot_config::mojom::HotspotAllowStatus;
  NET_LOG(EVENT) << "Check tethering readiness result: " << result;
  CheckTetheringReadinessResult readiness_result =
      ShillResultToReadinessResult(result);
  if (result == shill::kTetheringReadinessReady) {
    SetHotspotAllowStatus(HotspotAllowStatus::kAllowed);
  } else if (result == shill::kTetheringReadinessUpstreamNetworkNotAvailable) {
    SetHotspotAllowStatus(HotspotAllowStatus::kDisallowedNoMobileData);
  } else {
    SetHotspotAllowStatus(HotspotAllowStatus::kDisallowedReadinessCheckFail);
  }

  HotspotMetricsHelper::RecordCheckTetheringReadinessResult(readiness_result);
  std::move(callback).Run(readiness_result);
}

void HotspotCapabilitiesProvider::OnCheckReadinessFailure(
    CheckTetheringReadinessCallback callback,
    const std::string& error_name,
    const std::string& error_message) {
  NET_LOG(ERROR) << "Check tethering readiness failed, error name: "
                 << error_name << ", message: " << error_message;
  SetHotspotAllowStatus(
      hotspot_config::mojom::HotspotAllowStatus::kDisallowedReadinessCheckFail);
  HotspotMetricsHelper::RecordCheckTetheringReadinessResult(
      CheckTetheringReadinessResult::kShillOperationFailed);
  std::move(callback).Run(CheckTetheringReadinessResult::kShillOperationFailed);
}

void HotspotCapabilitiesProvider::SetHotspotAllowStatus(
    hotspot_config::mojom::HotspotAllowStatus new_allow_status) {
  if (hotspot_capabilities_.allow_status == new_allow_status &&
      new_allow_status == hotspot_config::mojom::HotspotAllowStatus::kAllowed) {
    return;
  }

  hotspot_capabilities_.allow_status = new_allow_status;
  NotifyHotspotCapabilitiesChanged();
}

void HotspotCapabilitiesProvider::NotifyHotspotCapabilitiesChanged() {
  for (auto& observer : observer_list_) {
    observer.OnHotspotCapabilitiesChanged();
  }
}

void HotspotCapabilitiesProvider::SetPolicyAllowed(bool allowed) {
  policy_allow_hotspot_ = allowed;
  if (!policy_allow_hotspot_ &&
      !IsDisallowedByPlatformCapabilities(hotspot_capabilities_.allow_status)) {
    SetHotspotAllowStatus(
        hotspot_config::mojom::HotspotAllowStatus::kDisallowedByPolicy);
    return;
  }
  if (policy_allow_hotspot_ &&
      hotspot_capabilities_.allow_status ==
          hotspot_config::mojom::HotspotAllowStatus::kDisallowedByPolicy) {
    CheckTetheringReadiness(base::DoNothing());
  }
}

}  // namespace ash