Chromium Code Reviews| Index: chromeos/components/tether/initializer.cc |
| diff --git a/chromeos/components/tether/initializer.cc b/chromeos/components/tether/initializer.cc |
| index cc2d49cfac0046987bd682fc914417da6e87027a..46c95bcf6a1d8827ef6307b0dcf42cc3f0e00963 100644 |
| --- a/chromeos/components/tether/initializer.cc |
| +++ b/chromeos/components/tether/initializer.cc |
| @@ -4,6 +4,27 @@ |
| #include "chromeos/components/tether/initializer.h" |
| +#include "base/bind.h" |
| +#include "chromeos/components/tether/active_host.h" |
| +#include "chromeos/components/tether/active_host_network_state_updater.h" |
| +#include "chromeos/components/tether/ble_connection_manager.h" |
| +#include "chromeos/components/tether/device_id_tether_network_guid_map.h" |
| +#include "chromeos/components/tether/host_scan_device_prioritizer.h" |
| +#include "chromeos/components/tether/host_scan_scheduler.h" |
| +#include "chromeos/components/tether/host_scanner.h" |
| +#include "chromeos/components/tether/local_device_data_provider.h" |
| +#include "chromeos/components/tether/notification_presenter.h" |
| +#include "chromeos/components/tether/tether_connector.h" |
| +#include "chromeos/components/tether/tether_host_fetcher.h" |
| +#include "chromeos/components/tether/wifi_hotspot_connector.h" |
| +#include "components/cryptauth/bluetooth_throttler_impl.h" |
| +#include "components/cryptauth/cryptauth_service.h" |
| +#include "components/cryptauth/remote_beacon_seed_fetcher.h" |
| +#include "components/prefs/pref_service.h" |
| +#include "components/proximity_auth/logging/logging.h" |
| +#include "device/bluetooth/bluetooth_adapter.h" |
| +#include "device/bluetooth/bluetooth_adapter_factory.h" |
| + |
| namespace chromeos { |
| namespace tether { |
| @@ -11,19 +32,122 @@ namespace tether { |
| // static |
| Initializer* Initializer::instance_ = nullptr; |
| -void Initializer::Initialize(cryptauth::CryptAuthService* cryptauth_service) { |
| +void Initializer::Initialize( |
| + cryptauth::CryptAuthService* cryptauth_service, |
| + std::unique_ptr<NotificationPresenter> notification_presenter, |
| + PrefService* pref_service, |
| + ProfileOAuth2TokenService* token_service) { |
| + if (!device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { |
| + PA_LOG(WARNING) << "Bluetooth is unavailable on this device; cannot " |
| + << "initialize tether feature."; |
| + return; |
| + } |
| + |
| if (instance_) { |
| - // TODO(khorimoto): Determine if a new instance should be created. |
| - instance_->cryptauth_service_ = cryptauth_service; |
| + delete instance_; |
| + } |
| + |
| + instance_ = |
| + new Initializer(cryptauth_service, std::move(notification_presenter), |
| + pref_service, token_service); |
| +} |
| + |
| +Initializer::Initializer( |
| + cryptauth::CryptAuthService* cryptauth_service, |
| + std::unique_ptr<NotificationPresenter> notification_presenter, |
| + PrefService* pref_service, |
| + ProfileOAuth2TokenService* token_service) |
| + : cryptauth_service_(cryptauth_service), |
| + notification_presenter_(std::move(notification_presenter)), |
| + pref_service_(pref_service), |
| + token_service_(token_service), |
| + weak_ptr_factory_(this) { |
| + if (token_service_->RefreshTokenIsAvailable( |
| + cryptauth_service_->GetAccountId())) { |
| + PA_LOG(INFO) << "Refresh token is available; initializing tether feature."; |
| + device::BluetoothAdapterFactory::GetAdapter( |
| + base::Bind(&Initializer::OnBluetoothAdapterFetched, |
| + weak_ptr_factory_.GetWeakPtr())); |
| } else { |
| - instance_ = new Initializer(cryptauth_service); |
| + PA_LOG(INFO) << "Refresh token not yet available; " |
| + << "waiting for valid token to initializing tether feature."; |
| + token_service_->AddObserver(this); |
| + } |
|
stevenjb
2017/04/10 20:26:59
nit: invert, early exit, and share common code:
i
Kyle Horimoto
2017/04/11 01:40:40
Done.
|
| +} |
| + |
| +Initializer::~Initializer() { |
| + token_service_->RemoveObserver(this); |
| +} |
| + |
| +void Initializer::OnRefreshTokensLoaded() { |
| + if (!token_service_->RefreshTokenIsAvailable( |
| + cryptauth_service_->GetAccountId())) { |
| + // If a token for the active account is still not available, continue |
| + // waiting for a new token. |
| + return; |
| } |
| + |
| + PA_LOG(INFO) << "Refresh token has loaded; initializing tether feature."; |
| + |
| + token_service_->RemoveObserver(this); |
|
stevenjb
2017/04/10 20:26:59
}
void GetBluetoothAdapter() {
Kyle Horimoto
2017/04/11 01:40:40
Done.
|
| + device::BluetoothAdapterFactory::GetAdapter(base::Bind( |
| + &Initializer::OnBluetoothAdapterFetched, weak_ptr_factory_.GetWeakPtr())); |
| +} |
| + |
| +void Initializer::OnBluetoothAdapterFetched( |
| + scoped_refptr<device::BluetoothAdapter> adapter) { |
| + tether_host_fetcher_ = |
| + base::MakeUnique<TetherHostFetcher>(cryptauth_service_); |
| + local_device_data_provider_ = |
| + base::MakeUnique<LocalDeviceDataProvider>(cryptauth_service_); |
| + remote_beacon_seed_fetcher_ = |
| + base::MakeUnique<cryptauth::RemoteBeaconSeedFetcher>( |
| + cryptauth_service_->GetCryptAuthDeviceManager()); |
| + ble_connection_manager_ = base::MakeUnique<BleConnectionManager>( |
| + cryptauth_service_, adapter, local_device_data_provider_.get(), |
| + remote_beacon_seed_fetcher_.get(), |
| + cryptauth::BluetoothThrottlerImpl::GetInstance()); |
| + host_scan_device_prioritizer_ = |
| + base::MakeUnique<HostScanDevicePrioritizer>(pref_service_); |
| + wifi_hotspot_connector_ = base::MakeUnique<WifiHotspotConnector>(); |
| + active_host_ = |
| + base::MakeUnique<ActiveHost>(tether_host_fetcher_.get(), pref_service_); |
| + active_host_network_state_updater_ = |
| + base::MakeUnique<ActiveHostNetworkStateUpdater>(active_host_.get()); |
| + device_id_tether_network_guid_map_ = |
| + base::MakeUnique<DeviceIdTetherNetworkGuidMap>(); |
| + tether_connector_ = base::MakeUnique<TetherConnector>( |
| + wifi_hotspot_connector_.get(), active_host_.get(), |
| + tether_host_fetcher_.get(), ble_connection_manager_.get(), |
| + host_scan_device_prioritizer_.get(), |
| + device_id_tether_network_guid_map_.get()); |
| + host_scanner_ = base::MakeUnique<HostScanner>( |
| + tether_host_fetcher_.get(), ble_connection_manager_.get(), |
| + host_scan_device_prioritizer_.get(), notification_presenter_.get()); |
| + |
| + // TODO (hansberry): Experiment with intervals to determine ideal range. |
| + adapter->SetAdvertisingInterval( |
| + base::TimeDelta::FromMilliseconds(100), |
| + base::TimeDelta::FromMilliseconds(100), |
|
stevenjb
2017/04/10 20:26:59
Move these constants and the comment to the top of
Kyle Horimoto
2017/04/11 01:40:40
Done.
|
| + base::Bind(&Initializer::IntervalSuccessCallback, |
| + weak_ptr_factory_.GetWeakPtr()), |
| + base::Bind(&Initializer::IntervalErrorCallback, |
| + weak_ptr_factory_.GetWeakPtr())); |
| } |
| -Initializer::Initializer(cryptauth::CryptAuthService* cryptauth_service) |
| - : cryptauth_service_(cryptauth_service) {} |
| +void Initializer::IntervalSuccessCallback() { |
| + PA_LOG(INFO) << "Successfully set Bluetooth advertisement interval."; |
| -Initializer::~Initializer() {} |
| + // TODO(khorimoto): Hook up HostScanScheduler. Currently, we simply start a |
| + // new scan once the user logs in. |
| + host_scanner_->StartScan(); |
| +} |
| + |
| +void Initializer::IntervalErrorCallback( |
| + device::BluetoothAdvertisement::ErrorCode status) { |
| + PA_LOG(ERROR) << "Failed to set Bluetooth advertisement interval; " |
| + << "cannot use tether feature. Error code: " << status; |
| +} |
| } // namespace tether |