| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chromeos/components/tether/initializer.h" | 5 #include "chromeos/components/tether/initializer.h" |
| 6 | 6 |
| 7 #include "base/bind.h" |
| 8 #include "chromeos/components/tether/active_host.h" |
| 9 #include "chromeos/components/tether/active_host_network_state_updater.h" |
| 10 #include "chromeos/components/tether/ble_connection_manager.h" |
| 11 #include "chromeos/components/tether/device_id_tether_network_guid_map.h" |
| 12 #include "chromeos/components/tether/host_scan_device_prioritizer.h" |
| 13 #include "chromeos/components/tether/host_scan_scheduler.h" |
| 14 #include "chromeos/components/tether/host_scanner.h" |
| 15 #include "chromeos/components/tether/local_device_data_provider.h" |
| 16 #include "chromeos/components/tether/notification_presenter.h" |
| 17 #include "chromeos/components/tether/tether_connector.h" |
| 18 #include "chromeos/components/tether/tether_host_fetcher.h" |
| 19 #include "chromeos/components/tether/wifi_hotspot_connector.h" |
| 20 #include "chromeos/network/network_connect.h" |
| 21 #include "chromeos/network/network_state_handler.h" |
| 22 #include "components/cryptauth/bluetooth_throttler_impl.h" |
| 23 #include "components/cryptauth/cryptauth_service.h" |
| 24 #include "components/cryptauth/remote_beacon_seed_fetcher.h" |
| 25 #include "components/prefs/pref_service.h" |
| 26 #include "components/proximity_auth/logging/logging.h" |
| 27 #include "device/bluetooth/bluetooth_adapter.h" |
| 28 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 29 |
| 7 namespace chromeos { | 30 namespace chromeos { |
| 8 | 31 |
| 9 namespace tether { | 32 namespace tether { |
| 10 | 33 |
| 34 namespace { |
| 35 |
| 36 // TODO (hansberry): Experiment with intervals to determine ideal advertising |
| 37 // interval parameters. |
| 38 constexpr int64_t kMinAdvertisingIntervalMilliseconds = 100; |
| 39 constexpr int64_t kMaxAdvertisingIntervalMilliseconds = 100; |
| 40 |
| 41 } // namespace |
| 42 |
| 11 // static | 43 // static |
| 12 Initializer* Initializer::instance_ = nullptr; | 44 Initializer* Initializer::instance_ = nullptr; |
| 13 | 45 |
| 14 void Initializer::Initialize(cryptauth::CryptAuthService* cryptauth_service) { | 46 // static |
| 47 void Initializer::Init( |
| 48 cryptauth::CryptAuthService* cryptauth_service, |
| 49 std::unique_ptr<NotificationPresenter> notification_presenter, |
| 50 PrefService* pref_service, |
| 51 ProfileOAuth2TokenService* token_service, |
| 52 NetworkStateHandler* network_state_handler, |
| 53 NetworkConnect* network_connect) { |
| 54 if (!device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { |
| 55 PA_LOG(WARNING) << "Bluetooth is unavailable on this device; cannot " |
| 56 << "initialize tether feature."; |
| 57 return; |
| 58 } |
| 59 |
| 15 if (instance_) { | 60 if (instance_) { |
| 16 // TODO(khorimoto): Determine if a new instance should be created. | 61 PA_LOG(WARNING) << "Tether initialization was triggered when the feature " |
| 17 instance_->cryptauth_service_ = cryptauth_service; | 62 << "had already been initialized; exiting initialization " |
| 18 } else { | 63 << "early."; |
| 19 instance_ = new Initializer(cryptauth_service); | 64 return; |
| 65 } |
| 66 |
| 67 instance_ = new Initializer( |
| 68 cryptauth_service, std::move(notification_presenter), pref_service, |
| 69 token_service, network_state_handler, network_connect); |
| 70 } |
| 71 |
| 72 // static |
| 73 void Initializer::Shutdown() { |
| 74 if (instance_) { |
| 75 PA_LOG(INFO) << "Shutting down tether feature."; |
| 76 delete instance_; |
| 77 instance_ = nullptr; |
| 20 } | 78 } |
| 21 } | 79 } |
| 22 | 80 |
| 23 Initializer::Initializer(cryptauth::CryptAuthService* cryptauth_service) | 81 Initializer::Initializer( |
| 24 : cryptauth_service_(cryptauth_service) {} | 82 cryptauth::CryptAuthService* cryptauth_service, |
| 83 std::unique_ptr<NotificationPresenter> notification_presenter, |
| 84 PrefService* pref_service, |
| 85 ProfileOAuth2TokenService* token_service, |
| 86 NetworkStateHandler* network_state_handler, |
| 87 NetworkConnect* network_connect) |
| 88 : cryptauth_service_(cryptauth_service), |
| 89 notification_presenter_(std::move(notification_presenter)), |
| 90 pref_service_(pref_service), |
| 91 token_service_(token_service), |
| 92 network_state_handler_(network_state_handler), |
| 93 network_connect_(network_connect), |
| 94 weak_ptr_factory_(this) { |
| 95 if (!token_service_->RefreshTokenIsAvailable( |
| 96 cryptauth_service_->GetAccountId())) { |
| 97 PA_LOG(INFO) << "Refresh token not yet available; " |
| 98 << "waiting for valid token to initializing tether feature."; |
| 99 token_service_->AddObserver(this); |
| 100 return; |
| 101 } |
| 25 | 102 |
| 26 Initializer::~Initializer() {} | 103 PA_LOG(INFO) << "Refresh token is available; initializing tether feature."; |
| 104 FetchBluetoothAdapter(); |
| 105 } |
| 106 |
| 107 Initializer::~Initializer() { |
| 108 token_service_->RemoveObserver(this); |
| 109 } |
| 110 |
| 111 void Initializer::FetchBluetoothAdapter() { |
| 112 device::BluetoothAdapterFactory::GetAdapter(base::Bind( |
| 113 &Initializer::OnBluetoothAdapterFetched, weak_ptr_factory_.GetWeakPtr())); |
| 114 } |
| 115 |
| 116 void Initializer::OnRefreshTokensLoaded() { |
| 117 if (!token_service_->RefreshTokenIsAvailable( |
| 118 cryptauth_service_->GetAccountId())) { |
| 119 // If a token for the active account is still not available, continue |
| 120 // waiting for a new token. |
| 121 return; |
| 122 } |
| 123 |
| 124 PA_LOG(INFO) << "Refresh token has loaded; initializing tether feature."; |
| 125 |
| 126 token_service_->RemoveObserver(this); |
| 127 FetchBluetoothAdapter(); |
| 128 } |
| 129 |
| 130 void Initializer::OnBluetoothAdapterFetched( |
| 131 scoped_refptr<device::BluetoothAdapter> adapter) { |
| 132 PA_LOG(INFO) << "Successfully fetched Bluetooth adapter. Setting advertising " |
| 133 << "interval."; |
| 134 |
| 135 adapter->SetAdvertisingInterval( |
| 136 base::TimeDelta::FromMilliseconds(kMinAdvertisingIntervalMilliseconds), |
| 137 base::TimeDelta::FromMilliseconds(kMaxAdvertisingIntervalMilliseconds), |
| 138 base::Bind(&Initializer::OnBluetoothAdapterAdvertisingIntervalSet, |
| 139 weak_ptr_factory_.GetWeakPtr(), adapter), |
| 140 base::Bind(&Initializer::OnBluetoothAdapterAdvertisingIntervalError, |
| 141 weak_ptr_factory_.GetWeakPtr())); |
| 142 } |
| 143 |
| 144 void Initializer::OnBluetoothAdapterAdvertisingIntervalSet( |
| 145 scoped_refptr<device::BluetoothAdapter> adapter) { |
| 146 PA_LOG(INFO) << "Successfully set Bluetooth advertisement interval. " |
| 147 << "Initializing tether feature."; |
| 148 |
| 149 tether_host_fetcher_ = |
| 150 base::MakeUnique<TetherHostFetcher>(cryptauth_service_); |
| 151 local_device_data_provider_ = |
| 152 base::MakeUnique<LocalDeviceDataProvider>(cryptauth_service_); |
| 153 remote_beacon_seed_fetcher_ = |
| 154 base::MakeUnique<cryptauth::RemoteBeaconSeedFetcher>( |
| 155 cryptauth_service_->GetCryptAuthDeviceManager()); |
| 156 ble_connection_manager_ = base::MakeUnique<BleConnectionManager>( |
| 157 cryptauth_service_, adapter, local_device_data_provider_.get(), |
| 158 remote_beacon_seed_fetcher_.get(), |
| 159 cryptauth::BluetoothThrottlerImpl::GetInstance()); |
| 160 host_scan_device_prioritizer_ = |
| 161 base::MakeUnique<HostScanDevicePrioritizer>(pref_service_); |
| 162 wifi_hotspot_connector_ = base::MakeUnique<WifiHotspotConnector>( |
| 163 network_state_handler_, network_connect_); |
| 164 active_host_ = |
| 165 base::MakeUnique<ActiveHost>(tether_host_fetcher_.get(), pref_service_); |
| 166 active_host_network_state_updater_ = |
| 167 base::MakeUnique<ActiveHostNetworkStateUpdater>(active_host_.get(), |
| 168 network_state_handler_); |
| 169 device_id_tether_network_guid_map_ = |
| 170 base::MakeUnique<DeviceIdTetherNetworkGuidMap>(); |
| 171 tether_connector_ = base::MakeUnique<TetherConnector>( |
| 172 network_connect_, network_state_handler_, wifi_hotspot_connector_.get(), |
| 173 active_host_.get(), tether_host_fetcher_.get(), |
| 174 ble_connection_manager_.get(), host_scan_device_prioritizer_.get(), |
| 175 device_id_tether_network_guid_map_.get()); |
| 176 host_scanner_ = base::MakeUnique<HostScanner>( |
| 177 tether_host_fetcher_.get(), ble_connection_manager_.get(), |
| 178 host_scan_device_prioritizer_.get(), network_state_handler_, |
| 179 notification_presenter_.get()); |
| 180 |
| 181 // TODO(khorimoto): Hook up HostScanScheduler. Currently, we simply start a |
| 182 // new scan once the user logs in. |
| 183 host_scanner_->StartScan(); |
| 184 } |
| 185 |
| 186 void Initializer::OnBluetoothAdapterAdvertisingIntervalError( |
| 187 device::BluetoothAdvertisement::ErrorCode status) { |
| 188 PA_LOG(ERROR) << "Failed to set Bluetooth advertisement interval; " |
| 189 << "cannot use tether feature. Error code: " << status; |
| 190 } |
| 27 | 191 |
| 28 } // namespace tether | 192 } // namespace tether |
| 29 | 193 |
| 30 } // namespace chromeos | 194 } // namespace chromeos |
| OLD | NEW |