Commit 6de4fbdb authored by James Hawkins's avatar James Hawkins Committed by Commit Bot

Instant Tethering: Remove unused BleServiceDataHelperImpl.

R=hansberry@chromium.org

Bug: 903991
Test: none
Change-Id: I2591923b254094ccc3318106b4c54be290d161e5
Reviewed-on: https://chromium-review.googlesource.com/c/1343241Reviewed-by: default avatarRyan Hansberry <hansberry@chromium.org>
Commit-Queue: James Hawkins <jhawkins@chromium.org>
Cr-Commit-Position: refs/heads/master@{#609585}
parent e87a9c39
......@@ -27,8 +27,6 @@ static_library("tether") {
"ble_scanner.h",
"ble_scanner_impl.cc",
"ble_scanner_impl.h",
"ble_service_data_helper_impl.cc",
"ble_service_data_helper_impl.h",
"connect_tethering_operation.cc",
"connect_tethering_operation.h",
"connection_preserver.cc",
......@@ -256,7 +254,6 @@ source_set("unit_tests") {
"ble_connection_manager_unittest.cc",
"ble_connection_metrics_logger_unittest.cc",
"ble_scanner_impl_unittest.cc",
"ble_service_data_helper_impl_unittest.cc",
"connect_tethering_operation_unittest.cc",
"connection_preserver_impl_unittest.cc",
"crash_recovery_manager_impl_unittest.cc",
......
......@@ -10,7 +10,6 @@
#include "chromeos/components/tether/ble_advertiser_impl.h"
#include "chromeos/components/tether/ble_connection_metrics_logger.h"
#include "chromeos/components/tether/ble_scanner_impl.h"
#include "chromeos/components/tether/ble_service_data_helper_impl.h"
#include "chromeos/components/tether/disconnect_tethering_request_sender_impl.h"
#include "chromeos/components/tether/network_configuration_remover.h"
#include "chromeos/components/tether/wifi_hotspot_disconnector_impl.h"
......@@ -96,13 +95,6 @@ AsynchronousShutdownObjectContainerImpl::
local_device_data_provider_(
std::make_unique<cryptauth::LocalDeviceDataProvider>(
cryptauth_service)),
ble_service_data_helper_(
base::FeatureList::IsEnabled(chromeos::features::kMultiDeviceApi)
? nullptr
: BleServiceDataHelperImpl::Factory::Get()->BuildInstance(
tether_host_fetcher_,
local_device_data_provider_.get(),
device_sync_client)),
ble_advertisement_device_queue_(
base::FeatureList::IsEnabled(chromeos::features::kMultiDeviceApi)
? nullptr
......@@ -116,14 +108,13 @@ AsynchronousShutdownObjectContainerImpl::
base::FeatureList::IsEnabled(chromeos::features::kMultiDeviceApi)
? nullptr
: BleAdvertiserImpl::Factory::NewInstance(
ble_service_data_helper_.get(),
nullptr,
ble_synchronizer_.get())),
ble_scanner_(
base::FeatureList::IsEnabled(chromeos::features::kMultiDeviceApi)
? nullptr
: BleScannerImpl::Factory::NewInstance(
adapter,
ble_service_data_helper_.get(),
: BleScannerImpl::Factory::NewInstance(adapter,
nullptr,
ble_synchronizer_.get(),
tether_host_fetcher_)),
ble_connection_metrics_logger_(
......@@ -142,8 +133,7 @@ AsynchronousShutdownObjectContainerImpl::
network_connection_handler,
network_state_handler,
pref_service,
network_configuration_remover_.get())) {
}
network_configuration_remover_.get())) {}
AsynchronousShutdownObjectContainerImpl::
~AsynchronousShutdownObjectContainerImpl() {
......
......@@ -44,10 +44,6 @@ namespace secure_channel {
class SecureChannelClient;
} // namespace secure_channel
namespace secure_channel {
class BleServiceDataHelper;
} // namespace secure_channel
namespace tether {
class BleAdvertisementDeviceQueue;
......@@ -143,8 +139,6 @@ class AsynchronousShutdownObjectContainerImpl
TetherHostFetcher* tether_host_fetcher_;
std::unique_ptr<cryptauth::LocalDeviceDataProvider>
local_device_data_provider_;
std::unique_ptr<secure_channel::BleServiceDataHelper>
ble_service_data_helper_;
std::unique_ptr<BleAdvertisementDeviceQueue> ble_advertisement_device_queue_;
std::unique_ptr<secure_channel::BleSynchronizerBase> ble_synchronizer_;
std::unique_ptr<BleAdvertiser> ble_advertiser_;
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/components/tether/ble_service_data_helper_impl.h"
#include <algorithm>
#include <iterator>
#include "base/memory/ptr_util.h"
#include "base/no_destructor.h"
#include "chromeos/chromeos_features.h"
#include "chromeos/chromeos_switches.h"
#include "chromeos/components/proximity_auth/logging/logging.h"
#include "chromeos/services/device_sync/public/cpp/device_sync_client.h"
#include "chromeos/services/secure_channel/ble_constants.h"
#include "components/cryptauth/background_eid_generator.h"
#include "components/cryptauth/ble/ble_advertisement_generator.h"
#include "components/cryptauth/foreground_eid_generator.h"
#include "components/cryptauth/local_device_data_provider.h"
namespace chromeos {
namespace tether {
namespace {
// Valid advertisement service data must be at least 2 bytes.
// As of March 2018, valid background advertisement service data is exactly 2
// bytes, which identify the advertising device to the scanning device.
// Valid foreground advertisement service data must include at least 4 bytes:
// 2 bytes associated with the scanning device (used as a scan filter) and 2
// bytes which identify the advertising device to the scanning device.
const size_t kMinNumBytesInServiceData = 2;
const size_t kMaxNumBytesInBackgroundServiceData = 3;
const size_t kMinNumBytesInForegroundServiceData = 4;
} // namespace
// static
BleServiceDataHelperImpl::Factory*
BleServiceDataHelperImpl::Factory::test_factory_ = nullptr;
// static
BleServiceDataHelperImpl::Factory* BleServiceDataHelperImpl::Factory::Get() {
if (test_factory_)
return test_factory_;
static base::NoDestructor<Factory> factory;
return factory.get();
}
// static
void BleServiceDataHelperImpl::Factory::SetFactoryForTesting(
Factory* test_factory) {
test_factory_ = test_factory;
}
BleServiceDataHelperImpl::Factory::~Factory() = default;
std::unique_ptr<secure_channel::BleServiceDataHelper>
BleServiceDataHelperImpl::Factory::BuildInstance(
TetherHostFetcher* tether_host_fetcher,
cryptauth::LocalDeviceDataProvider* local_device_data_provider,
device_sync::DeviceSyncClient* device_sync_client) {
return base::WrapUnique(new BleServiceDataHelperImpl(
tether_host_fetcher, local_device_data_provider, device_sync_client));
}
BleServiceDataHelperImpl::BleServiceDataHelperImpl(
TetherHostFetcher* tether_host_fetcher,
cryptauth::LocalDeviceDataProvider* local_device_data_provider,
device_sync::DeviceSyncClient* device_sync_client)
: tether_host_fetcher_(tether_host_fetcher),
local_device_data_provider_(local_device_data_provider),
device_sync_client_(device_sync_client),
background_eid_generator_(
std::make_unique<cryptauth::BackgroundEidGenerator>()),
foreground_eid_generator_(
std::make_unique<cryptauth::ForegroundEidGenerator>()),
weak_ptr_factory_(this) {
tether_host_fetcher_->AddObserver(this);
OnTetherHostsUpdated();
}
BleServiceDataHelperImpl::~BleServiceDataHelperImpl() {
tether_host_fetcher_->RemoveObserver(this);
}
base::Optional<secure_channel::BleServiceDataHelper::DeviceWithBackgroundBool>
BleServiceDataHelperImpl::IdentifyRemoteDevice(
const std::string& service_data,
const std::vector<std::string>& remote_device_ids) {
base::Optional<std::string> local_device_public_key =
GetLocalDevicePublicKey();
if (!local_device_public_key)
return base::nullopt;
// BleServiceDataHelper::IdentifyRemoteDevice() verifies that the devices its
// subclasses return are actually in |device_id_pair_set|. However, clients of
// BleServiceDataHelperImpl don't have easy access to local device metadata;
// therefore, pass along that required data here.
secure_channel::DeviceIdPairSet device_id_pair_set;
for (const auto& remote_device_id : remote_device_ids) {
device_id_pair_set.insert(secure_channel::DeviceIdPair(
remote_device_id, cryptauth::RemoteDeviceRef::GenerateDeviceId(
*local_device_public_key)));
}
return secure_channel::BleServiceDataHelper::IdentifyRemoteDevice(
service_data, device_id_pair_set);
}
std::unique_ptr<cryptauth::DataWithTimestamp>
BleServiceDataHelperImpl::GenerateForegroundAdvertisement(
const secure_channel::DeviceIdPair& device_id_pair) {
base::Optional<std::string> local_device_public_key =
GetLocalDevicePublicKey();
if (!local_device_public_key) {
PA_LOG(ERROR) << "Local device public key is invalid.";
return nullptr;
}
base::Optional<cryptauth::RemoteDeviceRef> remote_device;
const auto remote_device_it = std::find_if(
tether_hosts_from_last_fetch_.begin(),
tether_hosts_from_last_fetch_.end(),
[&device_id_pair](auto remote_device) {
return device_id_pair.remote_device_id() == remote_device.GetDeviceId();
});
if (remote_device_it == tether_hosts_from_last_fetch_.end()) {
PA_LOG(WARNING) << "Requested remote device ID is not a valid host: "
<< cryptauth::RemoteDeviceRef::TruncateDeviceIdForLogs(
device_id_pair.remote_device_id());
return nullptr;
}
return cryptauth::BleAdvertisementGenerator::GenerateBleAdvertisement(
*remote_device_it, *local_device_public_key);
}
base::Optional<secure_channel::BleServiceDataHelper::DeviceWithBackgroundBool>
BleServiceDataHelperImpl::PerformIdentifyRemoteDevice(
const std::string& service_data,
const secure_channel::DeviceIdPairSet& device_id_pair_set) {
std::vector<std::string> remote_device_ids;
for (const auto& device_id_pair : device_id_pair_set) {
// It's fine to ignore device_id_pair.local_device_id(); it's the same for
// each entry.
remote_device_ids.push_back(device_id_pair.remote_device_id());
}
std::string device_id;
bool is_background_advertisement = false;
// First try, identifying |service_data| as a foreground advertisement.
if (service_data.size() >= kMinNumBytesInForegroundServiceData) {
std::vector<cryptauth::BeaconSeed> beacon_seeds;
if (base::FeatureList::IsEnabled(chromeos::features::kMultiDeviceApi)) {
beacon_seeds =
device_sync_client_->GetLocalDeviceMetadata()->beacon_seeds();
} else if (!local_device_data_provider_->GetLocalDeviceData(
nullptr, &beacon_seeds)) {
PA_LOG(ERROR) << "Cannot fetch local beacon seeds.";
return base::nullopt;
}
device_id = foreground_eid_generator_->IdentifyRemoteDeviceByAdvertisement(
service_data, remote_device_ids, beacon_seeds);
}
// If the device has not yet been identified, try identifying |service_data|
// as a background advertisement.
if (chromeos::switches::IsInstantTetheringBackgroundAdvertisingSupported() &&
device_id.empty() && service_data.size() >= kMinNumBytesInServiceData &&
service_data.size() <= kMaxNumBytesInBackgroundServiceData) {
cryptauth::RemoteDeviceRefList remote_devices;
for (auto remote_device : tether_hosts_from_last_fetch_) {
if (base::ContainsValue(remote_device_ids, remote_device.GetDeviceId()))
remote_devices.push_back(remote_device);
}
device_id = background_eid_generator_->IdentifyRemoteDeviceByAdvertisement(
service_data, remote_devices);
is_background_advertisement = true;
}
// If the service data does not correspond to an advertisement from a device
// on this account, ignore it.
if (device_id.empty())
return base::nullopt;
for (const auto& remote_device_ref : tether_hosts_from_last_fetch_) {
if (remote_device_ref.GetDeviceId() == device_id) {
return secure_channel::BleServiceDataHelper::DeviceWithBackgroundBool(
remote_device_ref, is_background_advertisement);
}
}
PA_LOG(ERROR) << "Identified remote device ID is not a valid host: "
<< cryptauth::RemoteDeviceRef::TruncateDeviceIdForLogs(
device_id);
NOTREACHED();
return base::nullopt;
}
void BleServiceDataHelperImpl::OnTetherHostsUpdated() {
tether_host_fetcher_->FetchAllTetherHosts(
base::Bind(&BleServiceDataHelperImpl::OnTetherHostsFetched,
weak_ptr_factory_.GetWeakPtr()));
}
void BleServiceDataHelperImpl::OnTetherHostsFetched(
const cryptauth::RemoteDeviceRefList& tether_hosts) {
tether_hosts_from_last_fetch_ = tether_hosts;
}
base::Optional<std::string>
BleServiceDataHelperImpl::GetLocalDevicePublicKey() {
std::string local_device_public_key;
if (base::FeatureList::IsEnabled(chromeos::features::kMultiDeviceApi)) {
if (!device_sync_client_->GetLocalDeviceMetadata())
return base::nullopt;
local_device_public_key =
device_sync_client_->GetLocalDeviceMetadata()->public_key();
} else if (!local_device_data_provider_->GetLocalDeviceData(
&local_device_public_key, nullptr)) {
return base::nullopt;
}
if (local_device_public_key.empty())
return base::nullopt;
return local_device_public_key;
}
void BleServiceDataHelperImpl::SetTestDoubles(
std::unique_ptr<cryptauth::BackgroundEidGenerator> background_eid_generator,
std::unique_ptr<cryptauth::ForegroundEidGenerator>
foreground_eid_generator) {
background_eid_generator_ = std::move(background_eid_generator);
foreground_eid_generator_ = std::move(foreground_eid_generator);
}
} // namespace tether
} // namespace chromeos
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROMEOS_COMPONENTS_TETHER_BLE_SERVICE_DATA_HELPER_IMPL_H_
#define CHROMEOS_COMPONENTS_TETHER_BLE_SERVICE_DATA_HELPER_IMPL_H_
#include <memory>
#include <string>
#include <vector>
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/optional.h"
#include "chromeos/components/tether/tether_host_fetcher.h"
#include "chromeos/services/secure_channel/ble_service_data_helper.h"
#include "chromeos/services/secure_channel/device_id_pair.h"
#include "components/cryptauth/data_with_timestamp.h"
#include "components/cryptauth/remote_device_ref.h"
namespace cryptauth {
class BackgroundEidGenerator;
class ForegroundEidGenerator;
class LocalDeviceDataProvider;
} // namespace cryptauth
namespace chromeos {
namespace device_sync {
class DeviceSyncClient;
} // namespace device_sync
namespace tether {
// chromeos::tether BleServiceDataHelper implementation.
class BleServiceDataHelperImpl : public secure_channel::BleServiceDataHelper,
public TetherHostFetcher::Observer {
public:
class Factory {
public:
static Factory* Get();
static void SetFactoryForTesting(Factory* test_factory);
virtual ~Factory();
virtual std::unique_ptr<secure_channel::BleServiceDataHelper> BuildInstance(
TetherHostFetcher* tether_host_fetcher,
cryptauth::LocalDeviceDataProvider* local_device_data_provider,
device_sync::DeviceSyncClient* device_sync_client);
private:
static Factory* test_factory_;
};
~BleServiceDataHelperImpl() override;
base::Optional<DeviceWithBackgroundBool> IdentifyRemoteDevice(
const std::string& service_data,
const std::vector<std::string>& remote_device_ids);
private:
friend class BleServiceDataHelperImplTest;
BleServiceDataHelperImpl(
TetherHostFetcher* tether_host_fetcher,
cryptauth::LocalDeviceDataProvider* local_device_data_provider,
device_sync::DeviceSyncClient* device_sync_client);
// secure_channel::BleServiceDataHelper:
std::unique_ptr<cryptauth::DataWithTimestamp> GenerateForegroundAdvertisement(
const secure_channel::DeviceIdPair& device_id_pair) override;
base::Optional<DeviceWithBackgroundBool> PerformIdentifyRemoteDevice(
const std::string& service_data,
const secure_channel::DeviceIdPairSet& device_id_pair_set) override;
// TetherHostFetcher::Observer:
void OnTetherHostsUpdated() override;
void OnTetherHostsFetched(const cryptauth::RemoteDeviceRefList& tether_hosts);
base::Optional<std::string> GetLocalDevicePublicKey();
void SetTestDoubles(std::unique_ptr<cryptauth::BackgroundEidGenerator>
background_eid_generator,
std::unique_ptr<cryptauth::ForegroundEidGenerator>
foreground_eid_generator);
TetherHostFetcher* tether_host_fetcher_;
cryptauth::LocalDeviceDataProvider* local_device_data_provider_;
device_sync::DeviceSyncClient* device_sync_client_;
std::unique_ptr<cryptauth::BackgroundEidGenerator> background_eid_generator_;
std::unique_ptr<cryptauth::ForegroundEidGenerator> foreground_eid_generator_;
cryptauth::RemoteDeviceRefList tether_hosts_from_last_fetch_;
base::WeakPtrFactory<BleServiceDataHelperImpl> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(BleServiceDataHelperImpl);
};
} // namespace tether
} // namespace chromeos
#endif // CHROMEOS_COMPONENTS_TETHER_BLE_SERVICE_DATA_HELPER_IMPL_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment