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 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
|
// Copyright 2020 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/phonehub/tether_controller_impl.h"
#include "chromeos/ash/components/multidevice/logging/logging.h"
#include "chromeos/ash/components/phonehub/phone_status_model.h"
#include "chromeos/ash/components/phonehub/user_action_recorder.h"
#include "chromeos/ash/components/phonehub/util/histogram_util.h"
#include "chromeos/ash/services/network_config/in_process_instance.h"
namespace ash::phonehub {
namespace {
using ::chromeos::network_config::mojom::ConnectionStateType;
using ::chromeos::network_config::mojom::DeviceStatePropertiesPtr;
using ::chromeos::network_config::mojom::FilterType;
using ::chromeos::network_config::mojom::NetworkType;
using ::chromeos::network_config::mojom::StartConnectResult;
using multidevice_setup::MultiDeviceSetupClient;
using multidevice_setup::mojom::Feature;
using multidevice_setup::mojom::FeatureState;
} // namespace
TetherControllerImpl::TetherNetworkConnector::TetherNetworkConnector() {
network_config::BindToInProcessInstance(
cros_network_config_.BindNewPipeAndPassReceiver());
}
TetherControllerImpl::TetherNetworkConnector::~TetherNetworkConnector() =
default;
void TetherControllerImpl::TetherNetworkConnector::StartConnect(
const std::string& guid,
StartConnectCallback callback) {
cros_network_config_->StartConnect(guid, std::move(callback));
}
void TetherControllerImpl::TetherNetworkConnector::StartDisconnect(
const std::string& guid,
StartDisconnectCallback callback) {
cros_network_config_->StartDisconnect(guid, std::move(callback));
}
void TetherControllerImpl::TetherNetworkConnector::GetNetworkStateList(
chromeos::network_config::mojom::NetworkFilterPtr filter,
GetNetworkStateListCallback callback) {
cros_network_config_->GetNetworkStateList(std::move(filter),
std::move(callback));
}
TetherControllerImpl::TetherControllerImpl(
PhoneModel* phone_model,
UserActionRecorder* user_action_recorder,
MultiDeviceSetupClient* multidevice_setup_client)
: TetherControllerImpl(
phone_model,
user_action_recorder,
multidevice_setup_client,
std::make_unique<TetherControllerImpl::TetherNetworkConnector>()) {}
TetherControllerImpl::TetherControllerImpl(
PhoneModel* phone_model,
UserActionRecorder* user_action_recorder,
MultiDeviceSetupClient* multidevice_setup_client,
std::unique_ptr<TetherControllerImpl::TetherNetworkConnector> connector)
: phone_model_(phone_model),
user_action_recorder_(user_action_recorder),
multidevice_setup_client_(multidevice_setup_client),
connector_(std::move(connector)) {
// Receive updates when devices (e.g., Tether, Ethernet, Wi-Fi) go on/offline
// This class only cares about Tether devices.
network_config::BindToInProcessInstance(
cros_network_config_.BindNewPipeAndPassReceiver());
cros_network_config_->AddObserver(receiver_.BindNewPipeAndPassRemote());
phone_model_->AddObserver(this);
multidevice_setup_client_->AddObserver(this);
// Compute current status.
status_ = ComputeStatus();
// Load the current tether network if it exists.
FetchVisibleTetherNetwork();
}
TetherControllerImpl::~TetherControllerImpl() {
phone_model_->RemoveObserver(this);
multidevice_setup_client_->RemoveObserver(this);
}
TetherController::Status TetherControllerImpl::GetStatus() const {
PA_LOG(VERBOSE) << __func__ << ": status = " << status_;
return status_;
}
void TetherControllerImpl::ScanForAvailableConnection() {
if (status_ != Status::kConnectionUnavailable) {
PA_LOG(WARNING) << "Received request to scan for available connection, but "
<< "a scan cannot be performed because the current status "
<< "is " << status_;
return;
}
PA_LOG(INFO) << "Scanning for available connection.";
cros_network_config_->RequestNetworkScan(NetworkType::kTether);
}
void TetherControllerImpl::AttemptConnection() {
if (status_ != Status::kConnectionUnavailable &&
status_ != Status::kConnectionAvailable) {
PA_LOG(WARNING) << "Received request to attempt a connection, but a "
<< "connection cannot be attempted because the current "
<< "status is " << status_;
return;
}
PA_LOG(INFO) << "Attempting connection; current status is " << status_;
user_action_recorder_->RecordTetherConnectionAttempt();
util::LogTetherConnectionResult(
util::TetherConnectionResult::kAttemptConnection);
is_attempting_connection_ = true;
FeatureState feature_state =
multidevice_setup_client_->GetFeatureState(Feature::kInstantTethering);
if (feature_state == FeatureState::kEnabledByUser) {
PerformConnectionAttempt();
return;
}
// The Tethering feature was disabled and must be enabled first, before a
// connection attempt can be made.
DCHECK(feature_state == FeatureState::kDisabledByUser);
AttemptTurningOnTethering();
}
void TetherControllerImpl::AttemptTurningOnTethering() {
SetConnectDisconnectStatus(
ConnectDisconnectStatus::kTurningOnInstantTethering);
multidevice_setup_client_->SetFeatureEnabledState(
Feature::kInstantTethering,
/*enabled=*/true,
/*auth_token=*/std::nullopt,
base::BindOnce(&TetherControllerImpl::OnSetFeatureEnabled,
weak_ptr_factory_.GetWeakPtr()));
}
void TetherControllerImpl::OnSetFeatureEnabled(bool success) {
if (connect_disconnect_status_ !=
ConnectDisconnectStatus::kTurningOnInstantTethering) {
return;
}
if (success) {
PerformConnectionAttempt();
return;
}
PA_LOG(WARNING) << "Failed to enable InstantTethering";
SetConnectDisconnectStatus(ConnectDisconnectStatus::kIdle);
}
void TetherControllerImpl::OnFeatureStatesChanged(
const MultiDeviceSetupClient::FeatureStatesMap& feature_states_map) {
FeatureState feature_state =
multidevice_setup_client_->GetFeatureState(Feature::kInstantTethering);
// The |connect_disconnect_status_| should always be
// ConnectDisconnectStatus::kIdle if the |feature_state| is anything other
// than |FeatureState::kEnabledByUser|. A |feature_status| other than
// |FeatureState::kEnabledByUser| would indicate that Instant Tethering became
// disabled or disallowed.
if (feature_state != FeatureState::kEnabledByUser) {
SetConnectDisconnectStatus(ConnectDisconnectStatus::kIdle);
} else if (connect_disconnect_status_ !=
ConnectDisconnectStatus::kTurningOnInstantTethering) {
UpdateStatus();
}
}
void TetherControllerImpl::PerformConnectionAttempt() {
if (!tether_network_.is_null()) {
StartConnect();
return;
}
SetConnectDisconnectStatus(
ConnectDisconnectStatus::kScanningForEligiblePhone);
cros_network_config_->RequestNetworkScan(NetworkType::kTether);
}
void TetherControllerImpl::StartConnect() {
DCHECK(!tether_network_.is_null());
SetConnectDisconnectStatus(
ConnectDisconnectStatus::kConnectingToEligiblePhone);
connector_->StartConnect(
tether_network_->guid,
base::BindOnce(&TetherControllerImpl::OnStartConnectCompleted,
weak_ptr_factory_.GetWeakPtr()));
}
void TetherControllerImpl::OnStartConnectCompleted(StartConnectResult result,
const std::string& message) {
if (result != StartConnectResult::kSuccess) {
PA_LOG(WARNING) << "Start connect failed with result " << result
<< " and message " << message;
}
if (connect_disconnect_status_ !=
ConnectDisconnectStatus::kConnectingToEligiblePhone) {
return;
}
// Note that OnVisibleTetherNetworkFetched() may not have called
// SetConnectDisconnectStatus() with kIdle at this point, so this should go
// ahead and do it.
SetConnectDisconnectStatus(ConnectDisconnectStatus::kIdle);
}
void TetherControllerImpl::Disconnect() {
if (status_ != Status::kConnecting && status_ != Status::kConnected) {
PA_LOG(WARNING) << "Received request to disconnect, but no connection or "
<< "connection attempt is in progress. Current status is "
<< status_;
return;
}
// If |status_| is Status::kConnecting, a tether network may not be available
// yet e.g this class may be in the process of enabling Instant Tethering.
if (tether_network_.is_null()) {
SetConnectDisconnectStatus(ConnectDisconnectStatus::kIdle);
return;
}
PA_LOG(INFO) << "Attempting disconnection; current status is " << status_;
SetConnectDisconnectStatus(ConnectDisconnectStatus::kDisconnecting);
connector_->StartDisconnect(
tether_network_->guid,
base::BindOnce(&TetherControllerImpl::OnDisconnectCompleted,
weak_ptr_factory_.GetWeakPtr()));
}
void TetherControllerImpl::OnModelChanged() {
UpdateStatus();
}
void TetherControllerImpl::OnDisconnectCompleted(bool success) {
if (connect_disconnect_status_ != ConnectDisconnectStatus::kDisconnecting)
return;
SetConnectDisconnectStatus(ConnectDisconnectStatus::kIdle);
// Fetch the tether network and its updated connection state, if it exists.
// By the time OnDisconnectCompleted() is called, the connection state is
// properly updated to ConnectionStateType::kDisconnected, so a fetch may be
// necessary to promptly update |tether_network_|, as neither
// OnActiveNetworksChanged() nor OnNetworkStateListChanged() may be called
// shortly afterwards with the latest network information.
FetchVisibleTetherNetwork();
if (!success)
PA_LOG(WARNING) << "Failed to disconnect tether network";
}
void TetherControllerImpl::OnActiveNetworksChanged(
std::vector<chromeos::network_config::mojom::NetworkStatePropertiesPtr>
networks) {
// Active networks either changed externally (e.g via OS Settings or a new
// actve network added), or as a result of a call to AttemptConnection() or
// Disconnect(). This is needed for the case of
// ConnectionStateType::kConnecting in ComputeStatus().
//
// Note: When OnActiveNetworksChanged() is called shortly after starting a
// disconnect to a ConnectionStateType::kConnecting |tether_network_|, the
// |tether_network_|'s ConnectionStateType may still remain in the
// ConnectionStateType::kConnecting state. This may happen if on the phone,
// hotspot is off but bluetooth tethering is on, and a connection attempt is
// made, but the user does not acknowledge the notification to connect on
// their phone, and subsequently decides to disconnect while
// |tether_network_|'s ConnectionStateType is still
// ConnectionStateType::kConnecting.
FetchVisibleTetherNetwork();
}
void TetherControllerImpl::OnNetworkStateListChanged() {
// Any network change whether caused externally or within this class should
// be reflected to the state of this class (e.g user makes changes to Tether
// network in OS Settings).
FetchVisibleTetherNetwork();
}
void TetherControllerImpl::OnDeviceStateListChanged() {
if (connect_disconnect_status_ !=
ConnectDisconnectStatus::kScanningForEligiblePhone) {
return;
}
cros_network_config_->GetDeviceStateList(
base::BindOnce(&TetherControllerImpl::OnGetDeviceStateList,
weak_ptr_factory_.GetWeakPtr()));
}
void TetherControllerImpl::OnGetDeviceStateList(
std::vector<DeviceStatePropertiesPtr> devices) {
if (connect_disconnect_status_ !=
ConnectDisconnectStatus::kScanningForEligiblePhone) {
return;
}
// There should only be one Tether device in the list.
bool is_tether_device_scanning = false;
for (const auto& device : devices) {
NetworkType type = device->type;
if (type != NetworkType::kTether)
continue;
is_tether_device_scanning = device->scanning;
break;
}
if (!is_tether_device_scanning) {
NotifyAttemptConnectionScanFailed();
SetConnectDisconnectStatus(ConnectDisconnectStatus::kIdle);
}
}
void TetherControllerImpl::FetchVisibleTetherNetwork() {
// Return the connected, connecting, or connectable Tether network.
connector_->GetNetworkStateList(
chromeos::network_config::mojom::NetworkFilter::New(FilterType::kVisible,
NetworkType::kTether,
/*limit=*/0),
base::BindOnce(&TetherControllerImpl::OnVisibleTetherNetworkFetched,
weak_ptr_factory_.GetWeakPtr()));
}
void TetherControllerImpl::OnVisibleTetherNetworkFetched(
std::vector<chromeos::network_config::mojom::NetworkStatePropertiesPtr>
networks) {
chromeos::network_config::mojom::NetworkStatePropertiesPtr
previous_tether_network = std::move(tether_network_);
if (!networks.empty()) {
// The number of tether networks is expected to be at most 1, though some
// tests do use multiple networks.
tether_network_ = std::move(networks[0]);
} else {
tether_network_ = nullptr;
}
// No observeable changes to the tether network specifically. This fetch
// was initiated by a change in a non Tether network type.
if (tether_network_.Equals(previous_tether_network))
return;
// If AttemptConnection() was called when Instant Tethering was disabled.
// The feature must be enabled before scanning can occur.
if (connect_disconnect_status_ ==
ConnectDisconnectStatus::kTurningOnInstantTethering) {
UpdateStatus();
return;
}
// If AttemptConnection() was called when there was no available tether
// connection.
if (connect_disconnect_status_ ==
ConnectDisconnectStatus::kScanningForEligiblePhone &&
!tether_network_.is_null()) {
StartConnect();
return;
}
// If there is no attempt connection in progress, or an attempt connection
// caused OnVisibleTetherNetworkFetched() to be fired. This case also occurs
// in the event that Tethering settings are changed externally from this class
// (e.g user connects via Settings).
SetConnectDisconnectStatus(ConnectDisconnectStatus::kIdle);
}
void TetherControllerImpl::SetConnectDisconnectStatus(
ConnectDisconnectStatus connect_disconnect_status) {
if (connect_disconnect_status_ != connect_disconnect_status)
weak_ptr_factory_.InvalidateWeakPtrs();
connect_disconnect_status_ = connect_disconnect_status;
UpdateStatus();
}
void TetherControllerImpl::UpdateStatus() {
Status status = ComputeStatus();
if (status_ == status)
return;
PA_LOG(INFO) << "TetherController status update: " << status_ << " => "
<< status;
status_ = status;
if (is_attempting_connection_ && status_ == Status::kConnected)
util::LogTetherConnectionResult(util::TetherConnectionResult::kSuccess);
if (status_ != Status::kConnecting)
is_attempting_connection_ = false;
NotifyStatusChanged();
}
TetherController::Status TetherControllerImpl::ComputeStatus() const {
FeatureState feature_state =
multidevice_setup_client_->GetFeatureState(Feature::kInstantTethering);
if (feature_state != FeatureState::kDisabledByUser &&
feature_state != FeatureState::kEnabledByUser) {
// Tethering may be for instance, prohibited by policy or not supported
// by the phone or Chromebook.
return Status::kIneligibleForFeature;
}
if (phone_model_->phone_status_model().has_value()) {
// If the phone status exists, and it indicates that the phone
// does not have reception, the status becomes no kNoReception.
bool does_sim_exist_with_reception =
phone_model_->phone_status_model()->mobile_status() ==
PhoneStatusModel::MobileStatus::kSimWithReception;
if (!does_sim_exist_with_reception)
return Status::kNoReception;
}
if (connect_disconnect_status_ ==
ConnectDisconnectStatus::kTurningOnInstantTethering ||
connect_disconnect_status_ ==
ConnectDisconnectStatus::kScanningForEligiblePhone ||
connect_disconnect_status_ ==
ConnectDisconnectStatus::kConnectingToEligiblePhone) {
return Status::kConnecting;
}
if (feature_state == FeatureState::kDisabledByUser)
return Status::kConnectionUnavailable;
if (tether_network_.is_null())
return Status::kConnectionUnavailable;
ConnectionStateType connection_state = tether_network_->connection_state;
switch (connection_state) {
case ConnectionStateType::kOnline:
[[fallthrough]];
case ConnectionStateType::kConnected:
[[fallthrough]];
case ConnectionStateType::kPortal:
return Status::kConnected;
case ConnectionStateType::kConnecting:
return Status::kConnecting;
case ConnectionStateType::kNotConnected:
return Status::kConnectionAvailable;
}
return Status::kConnectionUnavailable;
}
} // namespace ash::phonehub
|