Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(483)

Side by Side Diff: chromeos/components/tether/initializer.cc

Issue 2801353002: [CrOS Tether] Fill out the Initializer class. Tether will now initialize fully once the flag is ena… (Closed)
Patch Set: hansberry@ comment. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 int64_t kMinAdvertisingIntervalMilliseconds = 100;
39 int64_t kMaxAdvertisingIntervalMilliseconds = 100;
stevenjb 2017/04/11 19:52:00 constexpr
Kyle Horimoto 2017/04/11 21:43:20 Done.
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::Start(
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 // Tether should only be initialized once.
stevenjb 2017/04/11 19:52:00 Maybe log an error here (or just DCHECK)
Kyle Horimoto 2017/04/11 21:43:20 Done.
17 instance_->cryptauth_service_ = cryptauth_service; 62 return;
18 } else { 63 }
19 instance_ = new Initializer(cryptauth_service); 64
65 instance_ = new Initializer(
66 cryptauth_service, std::move(notification_presenter), pref_service,
67 token_service, network_state_handler, network_connect);
68 }
69
70 // static
71 void Initializer::Shutdown() {
72 if (instance_) {
73 PA_LOG(INFO) << "Shutting down tether feature.";
74 delete instance_;
75 instance_ = nullptr;
20 } 76 }
21 } 77 }
22 78
23 Initializer::Initializer(cryptauth::CryptAuthService* cryptauth_service) 79 Initializer::Initializer(
24 : cryptauth_service_(cryptauth_service) {} 80 cryptauth::CryptAuthService* cryptauth_service,
81 std::unique_ptr<NotificationPresenter> notification_presenter,
82 PrefService* pref_service,
83 ProfileOAuth2TokenService* token_service,
84 NetworkStateHandler* network_state_handler,
85 NetworkConnect* network_connect)
86 : cryptauth_service_(cryptauth_service),
87 notification_presenter_(std::move(notification_presenter)),
88 pref_service_(pref_service),
89 token_service_(token_service),
90 network_state_handler_(network_state_handler),
91 network_connect_(network_connect),
92 weak_ptr_factory_(this) {
93 if (!token_service_->RefreshTokenIsAvailable(
94 cryptauth_service_->GetAccountId())) {
95 PA_LOG(INFO) << "Refresh token not yet available; "
96 << "waiting for valid token to initializing tether feature.";
97 token_service_->AddObserver(this);
98 return;
99 }
25 100
26 Initializer::~Initializer() {} 101 PA_LOG(INFO) << "Refresh token is available; initializing tether feature.";
102 FetchBluetoothAdapter();
103 }
104
105 Initializer::~Initializer() {
106 token_service_->RemoveObserver(this);
107 host_scanner_.reset();
108 tether_connector_.reset();
109 device_id_tether_network_guid_map_.reset();
110 active_host_network_state_updater_.reset();
111 active_host_.reset();
112 wifi_hotspot_connector_.reset();
113 host_scan_device_prioritizer_.reset();
114 ble_connection_manager_.reset();
115 remote_beacon_seed_fetcher_.reset();
116 local_device_data_provider_.reset();
117 tether_host_fetcher_.reset();
stevenjb 2017/04/11 19:52:00 We shouldn't need to explicitly reset these in the
Kyle Horimoto 2017/04/11 21:43:20 Done.
118 }
119
120 void Initializer::FetchBluetoothAdapter() {
121 device::BluetoothAdapterFactory::GetAdapter(base::Bind(
122 &Initializer::OnBluetoothAdapterFetched, weak_ptr_factory_.GetWeakPtr()));
123 }
124
125 void Initializer::OnRefreshTokensLoaded() {
126 if (!token_service_->RefreshTokenIsAvailable(
127 cryptauth_service_->GetAccountId())) {
128 // If a token for the active account is still not available, continue
129 // waiting for a new token.
130 return;
131 }
132
133 PA_LOG(INFO) << "Refresh token has loaded; initializing tether feature.";
134
135 token_service_->RemoveObserver(this);
136 FetchBluetoothAdapter();
137 }
138
139 void Initializer::OnBluetoothAdapterFetched(
140 scoped_refptr<device::BluetoothAdapter> adapter) {
141 PA_LOG(INFO) << "Successfully fetched Bluetooth adapter. Setting advertising "
142 << "interval.";
143
144 adapter->SetAdvertisingInterval(
145 base::TimeDelta::FromMilliseconds(kMinAdvertisingIntervalMilliseconds),
146 base::TimeDelta::FromMilliseconds(kMaxAdvertisingIntervalMilliseconds),
147 base::Bind(&Initializer::OnBluetoothAdapterAdvertisingIntervalSet,
148 weak_ptr_factory_.GetWeakPtr(), adapter),
149 base::Bind(&Initializer::OnBluetoothAdapterAdvertisingIntervalError,
150 weak_ptr_factory_.GetWeakPtr()));
151 }
152
153 void Initializer::OnBluetoothAdapterAdvertisingIntervalSet(
154 scoped_refptr<device::BluetoothAdapter> adapter) {
155 PA_LOG(INFO) << "Successfully set Bluetooth advertisement interval. "
156 << "Initializing tether feature.";
157
158 tether_host_fetcher_ =
159 base::MakeUnique<TetherHostFetcher>(cryptauth_service_);
160 local_device_data_provider_ =
161 base::MakeUnique<LocalDeviceDataProvider>(cryptauth_service_);
162 remote_beacon_seed_fetcher_ =
163 base::MakeUnique<cryptauth::RemoteBeaconSeedFetcher>(
164 cryptauth_service_->GetCryptAuthDeviceManager());
165 ble_connection_manager_ = base::MakeUnique<BleConnectionManager>(
166 cryptauth_service_, adapter, local_device_data_provider_.get(),
167 remote_beacon_seed_fetcher_.get(),
168 cryptauth::BluetoothThrottlerImpl::GetInstance());
169 host_scan_device_prioritizer_ =
170 base::MakeUnique<HostScanDevicePrioritizer>(pref_service_);
171 wifi_hotspot_connector_ = base::MakeUnique<WifiHotspotConnector>(
172 network_state_handler_, network_connect_);
173 active_host_ =
174 base::MakeUnique<ActiveHost>(tether_host_fetcher_.get(), pref_service_);
175 active_host_network_state_updater_ =
176 base::MakeUnique<ActiveHostNetworkStateUpdater>(active_host_.get(),
177 network_state_handler_);
178 device_id_tether_network_guid_map_ =
179 base::MakeUnique<DeviceIdTetherNetworkGuidMap>();
180 tether_connector_ = base::MakeUnique<TetherConnector>(
181 network_connect_, network_state_handler_, wifi_hotspot_connector_.get(),
182 active_host_.get(), tether_host_fetcher_.get(),
183 ble_connection_manager_.get(), host_scan_device_prioritizer_.get(),
184 device_id_tether_network_guid_map_.get());
185 host_scanner_ = base::MakeUnique<HostScanner>(
186 tether_host_fetcher_.get(), ble_connection_manager_.get(),
187 host_scan_device_prioritizer_.get(), network_state_handler_,
188 notification_presenter_.get());
189
190 // TODO(khorimoto): Hook up HostScanScheduler. Currently, we simply start a
191 // new scan once the user logs in.
192 host_scanner_->StartScan();
193 }
194
195 void Initializer::OnBluetoothAdapterAdvertisingIntervalError(
196 device::BluetoothAdvertisement::ErrorCode status) {
197 PA_LOG(ERROR) << "Failed to set Bluetooth advertisement interval; "
198 << "cannot use tether feature. Error code: " << status;
199 }
27 200
28 } // namespace tether 201 } // namespace tether
29 202
30 } // namespace chromeos 203 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698