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

Unified 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: Added missing dep. 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chromeos/components/tether/initializer.h ('k') | chromeos/components/tether/initializer_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chromeos/components/tether/initializer.cc
diff --git a/chromeos/components/tether/initializer.cc b/chromeos/components/tether/initializer.cc
index cc2d49cfac0046987bd682fc914417da6e87027a..4cb3cfaea0d6f7d65db0995aba09bc102a90607e 100644
--- a/chromeos/components/tether/initializer.cc
+++ b/chromeos/components/tether/initializer.cc
@@ -4,26 +4,190 @@
#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 "chromeos/network/network_connect.h"
+#include "chromeos/network/network_state_handler.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 {
+namespace {
+
+// TODO (hansberry): Experiment with intervals to determine ideal advertising
+// interval parameters.
+constexpr int64_t kMinAdvertisingIntervalMilliseconds = 100;
+constexpr int64_t kMaxAdvertisingIntervalMilliseconds = 100;
+
+} // namespace
+
// static
Initializer* Initializer::instance_ = nullptr;
-void Initializer::Initialize(cryptauth::CryptAuthService* cryptauth_service) {
+// static
+void Initializer::Init(
+ cryptauth::CryptAuthService* cryptauth_service,
+ std::unique_ptr<NotificationPresenter> notification_presenter,
+ PrefService* pref_service,
+ ProfileOAuth2TokenService* token_service,
+ NetworkStateHandler* network_state_handler,
+ NetworkConnect* network_connect) {
+ 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;
- } else {
- instance_ = new Initializer(cryptauth_service);
+ PA_LOG(WARNING) << "Tether initialization was triggered when the feature "
+ << "had already been initialized; exiting initialization "
+ << "early.";
+ return;
}
+
+ instance_ = new Initializer(
+ cryptauth_service, std::move(notification_presenter), pref_service,
+ token_service, network_state_handler, network_connect);
}
-Initializer::Initializer(cryptauth::CryptAuthService* cryptauth_service)
- : cryptauth_service_(cryptauth_service) {}
+// static
+void Initializer::Shutdown() {
+ if (instance_) {
+ PA_LOG(INFO) << "Shutting down tether feature.";
+ delete instance_;
+ instance_ = nullptr;
+ }
+}
+
+Initializer::Initializer(
+ cryptauth::CryptAuthService* cryptauth_service,
+ std::unique_ptr<NotificationPresenter> notification_presenter,
+ PrefService* pref_service,
+ ProfileOAuth2TokenService* token_service,
+ NetworkStateHandler* network_state_handler,
+ NetworkConnect* network_connect)
+ : cryptauth_service_(cryptauth_service),
+ notification_presenter_(std::move(notification_presenter)),
+ pref_service_(pref_service),
+ token_service_(token_service),
+ network_state_handler_(network_state_handler),
+ network_connect_(network_connect),
+ weak_ptr_factory_(this) {
+ if (!token_service_->RefreshTokenIsAvailable(
+ cryptauth_service_->GetAccountId())) {
+ PA_LOG(INFO) << "Refresh token not yet available; "
+ << "waiting for valid token to initializing tether feature.";
+ token_service_->AddObserver(this);
+ return;
+ }
+
+ PA_LOG(INFO) << "Refresh token is available; initializing tether feature.";
+ FetchBluetoothAdapter();
+}
-Initializer::~Initializer() {}
+Initializer::~Initializer() {
+ token_service_->RemoveObserver(this);
+}
+
+void Initializer::FetchBluetoothAdapter() {
+ device::BluetoothAdapterFactory::GetAdapter(base::Bind(
+ &Initializer::OnBluetoothAdapterFetched, weak_ptr_factory_.GetWeakPtr()));
+}
+
+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);
+ FetchBluetoothAdapter();
+}
+
+void Initializer::OnBluetoothAdapterFetched(
+ scoped_refptr<device::BluetoothAdapter> adapter) {
+ PA_LOG(INFO) << "Successfully fetched Bluetooth adapter. Setting advertising "
+ << "interval.";
+
+ adapter->SetAdvertisingInterval(
+ base::TimeDelta::FromMilliseconds(kMinAdvertisingIntervalMilliseconds),
+ base::TimeDelta::FromMilliseconds(kMaxAdvertisingIntervalMilliseconds),
+ base::Bind(&Initializer::OnBluetoothAdapterAdvertisingIntervalSet,
+ weak_ptr_factory_.GetWeakPtr(), adapter),
+ base::Bind(&Initializer::OnBluetoothAdapterAdvertisingIntervalError,
+ weak_ptr_factory_.GetWeakPtr()));
+}
+
+void Initializer::OnBluetoothAdapterAdvertisingIntervalSet(
+ scoped_refptr<device::BluetoothAdapter> adapter) {
+ PA_LOG(INFO) << "Successfully set Bluetooth advertisement interval. "
+ << "Initializing tether feature.";
+
+ 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>(
+ network_state_handler_, network_connect_);
+ active_host_ =
+ base::MakeUnique<ActiveHost>(tether_host_fetcher_.get(), pref_service_);
+ active_host_network_state_updater_ =
+ base::MakeUnique<ActiveHostNetworkStateUpdater>(active_host_.get(),
+ network_state_handler_);
+ device_id_tether_network_guid_map_ =
+ base::MakeUnique<DeviceIdTetherNetworkGuidMap>();
+ tether_connector_ = base::MakeUnique<TetherConnector>(
+ network_connect_, network_state_handler_, 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(), network_state_handler_,
+ notification_presenter_.get());
+
+ // TODO(khorimoto): Hook up HostScanScheduler. Currently, we simply start a
+ // new scan once the user logs in.
+ host_scanner_->StartScan();
+}
+
+void Initializer::OnBluetoothAdapterAdvertisingIntervalError(
+ device::BluetoothAdvertisement::ErrorCode status) {
+ PA_LOG(ERROR) << "Failed to set Bluetooth advertisement interval; "
+ << "cannot use tether feature. Error code: " << status;
+}
} // namespace tether
« no previous file with comments | « chromeos/components/tether/initializer.h ('k') | chromeos/components/tether/initializer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698