Chromium Code Reviews| 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 "components/cryptauth/bluetooth_throttler_impl.h" | |
| 21 #include "components/cryptauth/cryptauth_service.h" | |
| 22 #include "components/cryptauth/remote_beacon_seed_fetcher.h" | |
| 23 #include "components/prefs/pref_service.h" | |
| 24 #include "components/proximity_auth/logging/logging.h" | |
| 25 #include "device/bluetooth/bluetooth_adapter.h" | |
| 26 #include "device/bluetooth/bluetooth_adapter_factory.h" | |
| 27 | |
| 7 namespace chromeos { | 28 namespace chromeos { |
| 8 | 29 |
| 9 namespace tether { | 30 namespace tether { |
| 10 | 31 |
| 11 // static | 32 // static |
| 12 Initializer* Initializer::instance_ = nullptr; | 33 Initializer* Initializer::instance_ = nullptr; |
| 13 | 34 |
| 14 void Initializer::Initialize(cryptauth::CryptAuthService* cryptauth_service) { | 35 void Initializer::Initialize( |
| 36 cryptauth::CryptAuthService* cryptauth_service, | |
| 37 std::unique_ptr<NotificationPresenter> notification_presenter, | |
| 38 PrefService* pref_service, | |
| 39 ProfileOAuth2TokenService* token_service) { | |
| 40 if (!device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { | |
| 41 PA_LOG(WARNING) << "Bluetooth is unavailable on this device; cannot " | |
| 42 << "initialize tether feature."; | |
| 43 return; | |
| 44 } | |
| 45 | |
| 15 if (instance_) { | 46 if (instance_) { |
| 16 // TODO(khorimoto): Determine if a new instance should be created. | 47 delete instance_; |
| 17 instance_->cryptauth_service_ = cryptauth_service; | 48 } |
| 49 | |
| 50 instance_ = | |
| 51 new Initializer(cryptauth_service, std::move(notification_presenter), | |
| 52 pref_service, token_service); | |
| 53 } | |
| 54 | |
| 55 Initializer::Initializer( | |
| 56 cryptauth::CryptAuthService* cryptauth_service, | |
| 57 std::unique_ptr<NotificationPresenter> notification_presenter, | |
| 58 PrefService* pref_service, | |
| 59 ProfileOAuth2TokenService* token_service) | |
| 60 : cryptauth_service_(cryptauth_service), | |
| 61 notification_presenter_(std::move(notification_presenter)), | |
| 62 pref_service_(pref_service), | |
| 63 token_service_(token_service), | |
| 64 weak_ptr_factory_(this) { | |
| 65 if (token_service_->RefreshTokenIsAvailable( | |
| 66 cryptauth_service_->GetAccountId())) { | |
| 67 PA_LOG(INFO) << "Refresh token is available; initializing tether feature."; | |
| 68 device::BluetoothAdapterFactory::GetAdapter( | |
| 69 base::Bind(&Initializer::OnBluetoothAdapterFetched, | |
| 70 weak_ptr_factory_.GetWeakPtr())); | |
| 18 } else { | 71 } else { |
| 19 instance_ = new Initializer(cryptauth_service); | 72 PA_LOG(INFO) << "Refresh token not yet available; " |
| 73 << "waiting for valid token to initializing tether feature."; | |
| 74 token_service_->AddObserver(this); | |
| 20 } | 75 } |
|
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.
| |
| 21 } | 76 } |
| 22 | 77 |
| 23 Initializer::Initializer(cryptauth::CryptAuthService* cryptauth_service) | 78 Initializer::~Initializer() { |
| 24 : cryptauth_service_(cryptauth_service) {} | 79 token_service_->RemoveObserver(this); |
| 80 } | |
| 25 | 81 |
| 26 Initializer::~Initializer() {} | 82 void Initializer::OnRefreshTokensLoaded() { |
| 83 if (!token_service_->RefreshTokenIsAvailable( | |
| 84 cryptauth_service_->GetAccountId())) { | |
| 85 // If a token for the active account is still not available, continue | |
| 86 // waiting for a new token. | |
| 87 return; | |
| 88 } | |
| 89 | |
| 90 PA_LOG(INFO) << "Refresh token has loaded; initializing tether feature."; | |
| 91 | |
| 92 token_service_->RemoveObserver(this); | |
|
stevenjb
2017/04/10 20:26:59
}
void GetBluetoothAdapter() {
Kyle Horimoto
2017/04/11 01:40:40
Done.
| |
| 93 device::BluetoothAdapterFactory::GetAdapter(base::Bind( | |
| 94 &Initializer::OnBluetoothAdapterFetched, weak_ptr_factory_.GetWeakPtr())); | |
| 95 } | |
| 96 | |
| 97 void Initializer::OnBluetoothAdapterFetched( | |
| 98 scoped_refptr<device::BluetoothAdapter> adapter) { | |
| 99 tether_host_fetcher_ = | |
| 100 base::MakeUnique<TetherHostFetcher>(cryptauth_service_); | |
| 101 local_device_data_provider_ = | |
| 102 base::MakeUnique<LocalDeviceDataProvider>(cryptauth_service_); | |
| 103 remote_beacon_seed_fetcher_ = | |
| 104 base::MakeUnique<cryptauth::RemoteBeaconSeedFetcher>( | |
| 105 cryptauth_service_->GetCryptAuthDeviceManager()); | |
| 106 ble_connection_manager_ = base::MakeUnique<BleConnectionManager>( | |
| 107 cryptauth_service_, adapter, local_device_data_provider_.get(), | |
| 108 remote_beacon_seed_fetcher_.get(), | |
| 109 cryptauth::BluetoothThrottlerImpl::GetInstance()); | |
| 110 host_scan_device_prioritizer_ = | |
| 111 base::MakeUnique<HostScanDevicePrioritizer>(pref_service_); | |
| 112 wifi_hotspot_connector_ = base::MakeUnique<WifiHotspotConnector>(); | |
| 113 active_host_ = | |
| 114 base::MakeUnique<ActiveHost>(tether_host_fetcher_.get(), pref_service_); | |
| 115 active_host_network_state_updater_ = | |
| 116 base::MakeUnique<ActiveHostNetworkStateUpdater>(active_host_.get()); | |
| 117 device_id_tether_network_guid_map_ = | |
| 118 base::MakeUnique<DeviceIdTetherNetworkGuidMap>(); | |
| 119 tether_connector_ = base::MakeUnique<TetherConnector>( | |
| 120 wifi_hotspot_connector_.get(), active_host_.get(), | |
| 121 tether_host_fetcher_.get(), ble_connection_manager_.get(), | |
| 122 host_scan_device_prioritizer_.get(), | |
| 123 device_id_tether_network_guid_map_.get()); | |
| 124 host_scanner_ = base::MakeUnique<HostScanner>( | |
| 125 tether_host_fetcher_.get(), ble_connection_manager_.get(), | |
| 126 host_scan_device_prioritizer_.get(), notification_presenter_.get()); | |
| 127 | |
| 128 // TODO (hansberry): Experiment with intervals to determine ideal range. | |
| 129 adapter->SetAdvertisingInterval( | |
| 130 base::TimeDelta::FromMilliseconds(100), | |
| 131 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.
| |
| 132 base::Bind(&Initializer::IntervalSuccessCallback, | |
| 133 weak_ptr_factory_.GetWeakPtr()), | |
| 134 base::Bind(&Initializer::IntervalErrorCallback, | |
| 135 weak_ptr_factory_.GetWeakPtr())); | |
| 136 } | |
| 137 | |
| 138 void Initializer::IntervalSuccessCallback() { | |
| 139 PA_LOG(INFO) << "Successfully set Bluetooth advertisement interval."; | |
| 140 | |
| 141 // TODO(khorimoto): Hook up HostScanScheduler. Currently, we simply start a | |
| 142 // new scan once the user logs in. | |
| 143 host_scanner_->StartScan(); | |
| 144 } | |
| 145 | |
| 146 void Initializer::IntervalErrorCallback( | |
| 147 device::BluetoothAdvertisement::ErrorCode status) { | |
| 148 PA_LOG(ERROR) << "Failed to set Bluetooth advertisement interval; " | |
| 149 << "cannot use tether feature. Error code: " << status; | |
| 150 } | |
| 27 | 151 |
| 28 } // namespace tether | 152 } // namespace tether |
| 29 | 153 |
| 30 } // namespace chromeos | 154 } // namespace chromeos |
| OLD | NEW |