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
|
// Copyright 2016 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/tether/tether_component_impl.h"
#include <memory>
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/ptr_util.h"
#include "chromeos/ash/components/multidevice/logging/logging.h"
#include "chromeos/ash/components/tether/active_host.h"
#include "chromeos/ash/components/tether/asynchronous_shutdown_object_container_impl.h"
#include "chromeos/ash/components/tether/crash_recovery_manager_impl.h"
#include "chromeos/ash/components/tether/host_scan_scheduler.h"
#include "chromeos/ash/components/tether/persistent_host_scan_cache_impl.h"
#include "chromeos/ash/components/tether/synchronous_shutdown_object_container_impl.h"
#include "chromeos/ash/components/tether/tether_disconnector.h"
#include "chromeos/ash/components/tether/tether_host_response_recorder.h"
#include "chromeos/ash/components/tether/tether_session_completion_logger.h"
#include "chromeos/ash/components/tether/wifi_hotspot_disconnector_impl.h"
#include "chromeos/ash/services/device_sync/public/cpp/device_sync_client.h"
#include "chromeos/ash/services/secure_channel/public/cpp/client/secure_channel_client.h"
#include "components/pref_registry/pref_registry_syncable.h"
namespace ash {
namespace tether {
namespace {
void OnDisconnectErrorDuringShutdown(const std::string& error_name) {
PA_LOG(WARNING) << "Error disconnecting from Tether network during shutdown; "
<< "Error name: " << error_name;
}
TetherSessionCompletionLogger::SessionCompletionReason
GetSessionCompletionReasonFromShutdownReason(
TetherComponent::ShutdownReason shutdown_reason) {
switch (shutdown_reason) {
case TetherComponent::ShutdownReason::OTHER:
return TetherSessionCompletionLogger::SessionCompletionReason::OTHER;
case TetherComponent::ShutdownReason::USER_LOGGED_OUT:
return TetherSessionCompletionLogger::SessionCompletionReason::
USER_LOGGED_OUT;
case TetherComponent::ShutdownReason::USER_CLOSED_LID:
return TetherSessionCompletionLogger::SessionCompletionReason::
USER_CLOSED_LID;
case TetherComponent::ShutdownReason::PREF_DISABLED:
return TetherSessionCompletionLogger::SessionCompletionReason::
PREF_DISABLED;
case TetherComponent::ShutdownReason::BLUETOOTH_DISABLED:
return TetherSessionCompletionLogger::SessionCompletionReason::
BLUETOOTH_DISABLED;
case TetherComponent::ShutdownReason::CELLULAR_DISABLED:
return TetherSessionCompletionLogger::SessionCompletionReason::
CELLULAR_DISABLED;
case TetherComponent::ShutdownReason::MULTIDEVICE_HOST_UNVERIFIED:
return TetherSessionCompletionLogger::SessionCompletionReason::
MULTIDEVICE_HOST_UNVERIFIED;
case TetherComponent::ShutdownReason::BETTER_TOGETHER_SUITE_DISABLED:
return TetherSessionCompletionLogger::SessionCompletionReason::
BETTER_TOGETHER_SUITE_DISABLED;
default:
break;
}
return TetherSessionCompletionLogger::SessionCompletionReason::OTHER;
}
} // namespace
// static
TetherComponentImpl::Factory* TetherComponentImpl::Factory::factory_instance_ =
nullptr;
// static
std::unique_ptr<TetherComponent> TetherComponentImpl::Factory::Create(
device_sync::DeviceSyncClient* device_sync_client,
secure_channel::SecureChannelClient* secure_channel_client,
TetherHostFetcher* tether_host_fetcher,
NotificationPresenter* notification_presenter,
GmsCoreNotificationsStateTrackerImpl* gms_core_notifications_state_tracker,
PrefService* pref_service,
NetworkHandler* network_handler,
NetworkConnect* network_connect,
scoped_refptr<device::BluetoothAdapter> adapter,
session_manager::SessionManager* session_manager) {
if (factory_instance_) {
return factory_instance_->CreateInstance(
device_sync_client, secure_channel_client, tether_host_fetcher,
notification_presenter, gms_core_notifications_state_tracker,
pref_service, network_handler, network_connect, adapter,
session_manager);
}
return base::WrapUnique(new TetherComponentImpl(
device_sync_client, secure_channel_client, tether_host_fetcher,
notification_presenter, gms_core_notifications_state_tracker,
pref_service, network_handler, network_connect, adapter,
session_manager));
}
// static
void TetherComponentImpl::Factory::SetFactoryForTesting(Factory* factory) {
factory_instance_ = factory;
}
// static
void TetherComponentImpl::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) {
ActiveHost::RegisterPrefs(registry);
PersistentHostScanCacheImpl::RegisterPrefs(registry);
TetherHostResponseRecorder::RegisterPrefs(registry);
WifiHotspotDisconnectorImpl::RegisterPrefs(registry);
}
TetherComponentImpl::TetherComponentImpl(
device_sync::DeviceSyncClient* device_sync_client,
secure_channel::SecureChannelClient* secure_channel_client,
TetherHostFetcher* tether_host_fetcher,
NotificationPresenter* notification_presenter,
GmsCoreNotificationsStateTrackerImpl* gms_core_notifications_state_tracker,
PrefService* pref_service,
NetworkHandler* network_handler,
NetworkConnect* network_connect,
scoped_refptr<device::BluetoothAdapter> adapter,
session_manager::SessionManager* session_manager)
: asynchronous_shutdown_object_container_(
AsynchronousShutdownObjectContainerImpl::Factory::Create(
device_sync_client,
secure_channel_client,
tether_host_fetcher,
network_handler->network_state_handler(),
network_handler->managed_network_configuration_handler(),
network_handler->network_connection_handler(),
pref_service)),
synchronous_shutdown_object_container_(
SynchronousShutdownObjectContainerImpl::Factory::Create(
asynchronous_shutdown_object_container_.get(),
notification_presenter,
gms_core_notifications_state_tracker,
pref_service,
network_handler,
network_connect,
session_manager,
device_sync_client,
secure_channel_client)),
crash_recovery_manager_(CrashRecoveryManagerImpl::Factory::Create(
network_handler->network_state_handler(),
synchronous_shutdown_object_container_->active_host(),
synchronous_shutdown_object_container_->host_scan_cache())) {
crash_recovery_manager_->RestorePreCrashStateIfNecessary(
base::BindOnce(&TetherComponentImpl::OnPreCrashStateRestored,
weak_ptr_factory_.GetWeakPtr()));
}
TetherComponentImpl::~TetherComponentImpl() = default;
void TetherComponentImpl::RequestShutdown(
const ShutdownReason& shutdown_reason) {
has_shutdown_been_requested_ = true;
// If shutdown has already happened, there is nothing else to do.
if (status() != TetherComponent::Status::ACTIVE) {
return;
}
shutdown_reason_ = shutdown_reason;
// If crash recovery has not yet completed, wait for it to complete before
// continuing.
if (crash_recovery_manager_) {
return;
}
InitiateShutdown();
}
void TetherComponentImpl::OnPreCrashStateRestored() {
// |crash_recovery_manager_| is no longer needed since it has completed.
crash_recovery_manager_.reset();
if (has_shutdown_been_requested_) {
InitiateShutdown();
return;
}
// Start a scan now that the Tether module has started up.
synchronous_shutdown_object_container_->host_scan_scheduler()
->AttemptScanIfOffline();
}
void TetherComponentImpl::InitiateShutdown() {
DCHECK(has_shutdown_been_requested_);
DCHECK(!crash_recovery_manager_);
DCHECK(status() == TetherComponent::Status::ACTIVE);
ActiveHost* active_host =
synchronous_shutdown_object_container_->active_host();
TetherDisconnector* tether_disconnector =
synchronous_shutdown_object_container_->tether_disconnector();
// If there is an active connection, it needs to be disconnected before the
// Tether component is shut down.
if (active_host->GetActiveHostStatus() !=
ActiveHost::ActiveHostStatus::DISCONNECTED) {
PA_LOG(VERBOSE) << "There was an active connection during Tether shutdown. "
<< "Initiating disconnection from device ID \""
<< multidevice::RemoteDeviceRef::TruncateDeviceIdForLogs(
active_host->GetActiveHostDeviceId())
<< "\".";
tether_disconnector->DisconnectFromNetwork(
active_host->GetTetherNetworkGuid(), base::DoNothing(),
base::BindOnce(&OnDisconnectErrorDuringShutdown),
GetSessionCompletionReasonFromShutdownReason(shutdown_reason_));
}
TransitionToStatus(TetherComponent::Status::SHUTTING_DOWN);
// Delete objects which can shutdown synchronously immediately.
synchronous_shutdown_object_container_.reset();
// Start the shutdown process for objects which shutdown asynchronously.
asynchronous_shutdown_object_container_->Shutdown(
base::BindOnce(&TetherComponentImpl::OnShutdownComplete,
weak_ptr_factory_.GetWeakPtr()));
}
void TetherComponentImpl::OnShutdownComplete() {
DCHECK(status() == TetherComponent::Status::SHUTTING_DOWN);
// Shutdown has completed. The asynchronous objects can now be deleted as
// well.
asynchronous_shutdown_object_container_.reset();
TransitionToStatus(TetherComponent::Status::SHUT_DOWN);
}
} // namespace tether
} // namespace ash
|