From 97ea46900666fac13d22763652342b233810f4d7 Mon Sep 17 00:00:00 2001 From: Francis Tsui Date: Thu, 21 May 2026 11:07:13 -0700 Subject: [PATCH] Move BwuHandler creation into medium. PiperOrigin-RevId: 919149551 --- connections/implementation/bwu_manager.cc | 33 ++----- connections/implementation/mediums/BUILD | 6 +- connections/implementation/mediums/awdl.cc | 9 ++ connections/implementation/mediums/awdl.h | 5 ++ .../mediums/awdl_bwu_handler.cc | 7 +- .../implementation/mediums/awdl_bwu_handler.h | 9 +- .../mediums/awdl_bwu_handler_test.cc | 3 +- .../mediums/bluetooth_bwu_handler.cc | 11 ++- .../mediums/bluetooth_bwu_handler.h | 13 +-- .../mediums/bluetooth_bwu_handler_test.cc | 14 +-- .../mediums/bluetooth_classic.cc | 9 ++ .../mediums/bluetooth_classic.h | 4 + connections/implementation/mediums/webrtc.h | 6 ++ .../implementation/mediums/webrtc/BUILD | 19 +++- .../{ => webrtc}/webrtc_bwu_handler.cc | 14 ++- .../mediums/{ => webrtc}/webrtc_bwu_handler.h | 19 ++-- .../mediums/webrtc/webrtc_impl.cc | 8 ++ .../mediums/webrtc/webrtc_impl.h | 4 + .../mediums/webrtc_bwu_handler_stub.cc | 84 ------------------ .../mediums/webrtc_bwu_handler_stub.h | 86 ------------------- .../implementation/mediums/wifi_direct.cc | 10 +++ .../implementation/mediums/wifi_direct.h | 4 + .../mediums/wifi_direct_bwu_handler.cc | 8 +- .../mediums/wifi_direct_bwu_handler.h | 9 +- .../mediums/wifi_direct_bwu_handler_test.cc | 8 +- .../implementation/mediums/wifi_hotspot.cc | 11 +++ .../implementation/mediums/wifi_hotspot.h | 4 + .../mediums/wifi_hotspot_bwu_handler.cc | 8 +- .../mediums/wifi_hotspot_bwu_handler.h | 9 +- .../mediums/wifi_hotspot_bwu_handler_test.cc | 8 +- .../implementation/mediums/wifi_lan.cc | 10 +++ connections/implementation/mediums/wifi_lan.h | 5 ++ .../mediums/wifi_lan_bwu_handler.cc | 8 +- .../mediums/wifi_lan_bwu_handler.h | 9 +- .../mediums/wifi_lan_bwu_handler_test.cc | 3 +- 35 files changed, 202 insertions(+), 275 deletions(-) rename connections/implementation/mediums/{ => webrtc}/webrtc_bwu_handler.cc (95%) rename connections/implementation/mediums/{ => webrtc}/webrtc_bwu_handler.h (88%) delete mode 100644 connections/implementation/mediums/webrtc_bwu_handler_stub.cc delete mode 100644 connections/implementation/mediums/webrtc_bwu_handler_stub.h diff --git a/connections/implementation/bwu_manager.cc b/connections/implementation/bwu_manager.cc index a1082763..48a70718 100644 --- a/connections/implementation/bwu_manager.cc +++ b/connections/implementation/bwu_manager.cc @@ -32,20 +32,10 @@ #include "connections/implementation/endpoint_channel_manager.h" #include "connections/implementation/endpoint_manager.h" #include "connections/implementation/flags/nearby_connections_feature_flags.h" -#include "connections/implementation/mediums/awdl_bwu_handler.h" -#include "connections/implementation/mediums/bluetooth_bwu_handler.h" #include "connections/implementation/mediums/mediums.h" -#include "connections/implementation/mediums/wifi_lan_bwu_handler.h" #include "connections/implementation/offline_frames.h" #include "connections/implementation/service_id_constants.h" #include "internal/flags/nearby_flags.h" -#ifdef NO_WEBRTC -#include "connections/implementation/mediums/webrtc_bwu_handler_stub.h" -#else -#include "connections/implementation/mediums/webrtc_bwu_handler.h" -#endif -#include "connections/implementation/mediums/wifi_direct_bwu_handler.h" -#include "connections/implementation/mediums/wifi_hotspot_bwu_handler.h" #include "connections/medium_selector.h" #include "internal/platform/cancelable_alarm.h" #include "internal/platform/count_down_latch.h" @@ -134,43 +124,37 @@ void BwuManager::InitBwuHandlers() { if (config_.allow_upgrade_to.awdl) { handlers_.emplace( Medium::AWDL, - std::make_unique( - *mediums_, + mediums_->GetAwdl().CreateBwuHandler( absl::bind_front(&BwuManager::OnIncomingConnection, this))); } if (config_.allow_upgrade_to.wifi_hotspot) { handlers_.emplace( Medium::WIFI_HOTSPOT, - std::make_unique( - *mediums_, + mediums_->GetWifiHotspot().CreateBwuHandler( absl::bind_front(&BwuManager::OnIncomingConnection, this))); } if (config_.allow_upgrade_to.wifi_direct) { handlers_.emplace( Medium::WIFI_DIRECT, - std::make_unique( - *mediums_, + mediums_->GetWifiDirect().CreateBwuHandler( absl::bind_front(&BwuManager::OnIncomingConnection, this))); } if (config_.allow_upgrade_to.wifi_lan) { handlers_.emplace( Medium::WIFI_LAN, - std::make_unique( - *mediums_, + mediums_->GetWifiLan().CreateBwuHandler( absl::bind_front(&BwuManager::OnIncomingConnection, this))); } if (config_.allow_upgrade_to.web_rtc) { handlers_.emplace( Medium::WEB_RTC, - std::make_unique( - *mediums_, + mediums_->GetWebRtc().CreateBwuHandler( absl::bind_front(&BwuManager::OnIncomingConnection, this))); } if (config_.allow_upgrade_to.bluetooth) { handlers_.emplace( Medium::BLUETOOTH, - std::make_unique( - *mediums_, + mediums_->GetBluetoothClassic().CreateBwuHandler( absl::bind_front(&BwuManager::OnIncomingConnection, this))); } } @@ -196,8 +180,9 @@ void BwuManager::Shutdown() { medium_ = Medium::UNKNOWN_MEDIUM; endpoint_id_to_bwu_medium_.clear(); for (auto& medium_handler_pair : handlers_) { - assert(medium_handler_pair.second); - medium_handler_pair.second->RevertInitiatorState(); + if (medium_handler_pair.second != nullptr) { + medium_handler_pair.second->RevertInitiatorState(); + } } handlers_.clear(); diff --git a/connections/implementation/mediums/BUILD b/connections/implementation/mediums/BUILD index e10be0fe..f0af9ba8 100644 --- a/connections/implementation/mediums/BUILD +++ b/connections/implementation/mediums/BUILD @@ -31,8 +31,6 @@ cc_library( "bluetooth_endpoint_channel.cc", "bluetooth_radio.cc", "mediums.cc", - "webrtc_bwu_handler.cc", - "webrtc_bwu_handler_stub.cc", "webrtc_endpoint_channel.cc", "wifi_direct.cc", "wifi_direct_bwu_handler.cc", @@ -56,8 +54,6 @@ cc_library( "bluetooth_endpoint_channel.h", "bluetooth_radio.h", "mediums.h", - "webrtc_bwu_handler.h", - "webrtc_bwu_handler_stub.h", "webrtc_endpoint_channel.h", "wifi.h", "wifi_direct.h", @@ -110,6 +106,7 @@ cc_library( "//internal/platform/implementation:platform", "//internal/platform/implementation:wifi_utils", "@com_google_absl//absl/base:core_headers", + "@com_google_absl//absl/base:nullability", "@com_google_absl//absl/container:btree", "@com_google_absl//absl/container:flat_hash_map", "@com_google_absl//absl/container:flat_hash_set", @@ -190,6 +187,7 @@ cc_library( deps = [ ":webrtc_peer_id", ":webrtc_socket", + "//connections/implementation:bwu_handler", "//connections/implementation/proto:offline_wire_formats_cc_proto", "//internal/platform:base", "//internal/platform:cancellation_flag", diff --git a/connections/implementation/mediums/awdl.cc b/connections/implementation/mediums/awdl.cc index 81698ab3..ba2539f1 100644 --- a/connections/implementation/mediums/awdl.cc +++ b/connections/implementation/mediums/awdl.cc @@ -15,6 +15,7 @@ #include "connections/implementation/mediums/awdl.h" #include +#include #include #include #include @@ -22,6 +23,8 @@ #include "absl/strings/str_cat.h" #include "absl/strings/str_format.h" #include "absl/strings/string_view.h" +#include "connections/implementation/bwu_handler.h" +#include "connections/implementation/mediums/awdl_bwu_handler.h" #include "connections/implementation/mediums/utils.h" #include "internal/platform/awdl.h" #include "internal/platform/byte_array.h" @@ -471,5 +474,11 @@ ErrorOr Awdl::InternalConnect( return socket; } +std::unique_ptr Awdl::CreateBwuHandler( + BwuHandler::IncomingConnectionCallback incoming_connection_callback) { + return std::make_unique( + this, std::move(incoming_connection_callback)); +} + } // namespace connections } // namespace nearby diff --git a/connections/implementation/mediums/awdl.h b/connections/implementation/mediums/awdl.h index c6336946..cb4f1910 100644 --- a/connections/implementation/mediums/awdl.h +++ b/connections/implementation/mediums/awdl.h @@ -16,6 +16,7 @@ #define CORE_INTERNAL_MEDIUMS_AWDL_H_ #include +#include #include #include #include @@ -24,6 +25,7 @@ #include "absl/container/flat_hash_map.h" #include "absl/container/flat_hash_set.h" #include "absl/functional/any_invocable.h" +#include "connections/implementation/bwu_handler.h" #include "internal/platform/awdl.h" #include "internal/platform/cancellation_flag.h" #include "internal/platform/expected.h" @@ -129,6 +131,9 @@ class Awdl { AwdlCredential GetCredentials(const std::string& service_id) ABSL_LOCKS_EXCLUDED(mutex_); + std::unique_ptr CreateBwuHandler( + BwuHandler::IncomingConnectionCallback incoming_connection_callback); + private: struct AdvertisingInfo { bool Empty() const { return nsd_service_infos.empty(); } diff --git a/connections/implementation/mediums/awdl_bwu_handler.cc b/connections/implementation/mediums/awdl_bwu_handler.cc index 6aa9bca5..edff8dda 100644 --- a/connections/implementation/mediums/awdl_bwu_handler.cc +++ b/connections/implementation/mediums/awdl_bwu_handler.cc @@ -18,6 +18,7 @@ #include #include +#include "absl/base/nullability.h" #include "absl/functional/bind_front.h" #include "absl/strings/str_cat.h" #include "absl/strings/str_format.h" @@ -28,7 +29,6 @@ #include "connections/implementation/endpoint_channel.h" #include "connections/implementation/mediums/awdl.h" #include "connections/implementation/mediums/awdl_endpoint_channel.h" -#include "connections/implementation/mediums/mediums.h" #include "connections/implementation/mediums/utils.h" #include "connections/implementation/offline_frames.h" #include "connections/implementation/service_id_constants.h" @@ -56,9 +56,10 @@ constexpr absl::string_view kAwdlServiceIdSuffixForServiceType = "_AWDL"; } // namespace AwdlBwuHandler::AwdlBwuHandler( - Mediums& mediums, IncomingConnectionCallback incoming_connection_callback) + Awdl* absl_nonnull awdl_medium, + IncomingConnectionCallback incoming_connection_callback) : BaseBwuHandler(std::move(incoming_connection_callback)), - mediums_(mediums) {} + awdl_medium_(*awdl_medium) {} // Called by BWU target. Retrieves a new medium info from incoming message, // and establishes connection over AWDL using this info. diff --git a/connections/implementation/mediums/awdl_bwu_handler.h b/connections/implementation/mediums/awdl_bwu_handler.h index c3c27985..56a89290 100644 --- a/connections/implementation/mediums/awdl_bwu_handler.h +++ b/connections/implementation/mediums/awdl_bwu_handler.h @@ -18,12 +18,12 @@ #include #include +#include "absl/base/nullability.h" #include "connections/implementation/base_bwu_handler.h" #include "connections/implementation/bwu_handler.h" #include "connections/implementation/client_proxy.h" #include "connections/implementation/endpoint_channel.h" #include "connections/implementation/mediums/awdl.h" -#include "connections/implementation/mediums/mediums.h" #include "internal/platform/awdl.h" #include "internal/platform/expected.h" #include "internal/platform/nsd_service_info.h" @@ -35,8 +35,8 @@ namespace connections { // per-Medium-specific operations needed to upgrade an EndpointChannel. class AwdlBwuHandler : public BaseBwuHandler { public: - explicit AwdlBwuHandler( - Mediums& mediums, + AwdlBwuHandler( + Awdl* absl_nonnull awdl_medium, IncomingConnectionCallback incoming_connection_callback); private: @@ -80,8 +80,7 @@ class AwdlBwuHandler : public BaseBwuHandler { std::string GenerateServiceName(); std::string GeneratePassword(); - Mediums& mediums_; - Awdl& awdl_medium_{mediums_.GetAwdl()}; + Awdl& awdl_medium_; NsdServiceInfo nsd_service_info_; }; diff --git a/connections/implementation/mediums/awdl_bwu_handler_test.cc b/connections/implementation/mediums/awdl_bwu_handler_test.cc index 2908cf25..317f7b2c 100644 --- a/connections/implementation/mediums/awdl_bwu_handler_test.cc +++ b/connections/implementation/mediums/awdl_bwu_handler_test.cc @@ -127,7 +127,8 @@ constexpr absl::string_view kChannelName{"channel_name"}; class AwdlBwuHandlerTest : public ::testing::Test { protected: AwdlBwuHandlerTest() - : handler_(mediums_, incoming_connection_callback_.AsStdFunction()) {} + : handler_(&mediums_.GetAwdl(), + incoming_connection_callback_.AsStdFunction()) {} void SetUp() override { // By default, network is connected. diff --git a/connections/implementation/mediums/bluetooth_bwu_handler.cc b/connections/implementation/mediums/bluetooth_bwu_handler.cc index f2643049..2a7e9075 100644 --- a/connections/implementation/mediums/bluetooth_bwu_handler.cc +++ b/connections/implementation/mediums/bluetooth_bwu_handler.cc @@ -22,8 +22,10 @@ #include "connections/implementation/base_bwu_handler.h" #include "connections/implementation/client_proxy.h" #include "connections/implementation/endpoint_channel.h" +#include "connections/implementation/mediums/bluetooth_classic.h" #include "connections/implementation/mediums/bluetooth_endpoint_channel.h" -#include "connections/implementation/mediums/mediums.h" +#include "absl/base/nullability.h" +#include "connections/implementation/mediums/bluetooth_radio.h" #include "connections/implementation/offline_frames.h" #include "internal/platform/bluetooth_adapter.h" #include "internal/platform/bluetooth_classic.h" @@ -43,9 +45,12 @@ using ::location::nearby::proto::connections::OperationResultCode; } // namespace BluetoothBwuHandler::BluetoothBwuHandler( - Mediums& mediums, IncomingConnectionCallback incoming_connection_callback) + BluetoothRadio* absl_nonnull bluetooth_radio, + BluetoothClassic* absl_nonnull bluetooth_medium, + IncomingConnectionCallback incoming_connection_callback) : BaseBwuHandler(std::move(incoming_connection_callback)), - mediums_(mediums) {} + bluetooth_radio_(*bluetooth_radio), + bluetooth_medium_(*bluetooth_medium) {} // Called by BWU target. Retrieves a new medium info from incoming message, // and establishes connection over BT using this info. diff --git a/connections/implementation/mediums/bluetooth_bwu_handler.h b/connections/implementation/mediums/bluetooth_bwu_handler.h index 3c1a7b10..b1902362 100644 --- a/connections/implementation/mediums/bluetooth_bwu_handler.h +++ b/connections/implementation/mediums/bluetooth_bwu_handler.h @@ -18,12 +18,13 @@ #include #include +#include "absl/base/nullability.h" #include "connections/implementation/base_bwu_handler.h" +#include "connections/implementation/bwu_handler.h" #include "connections/implementation/client_proxy.h" #include "connections/implementation/endpoint_channel.h" #include "connections/implementation/mediums/bluetooth_classic.h" #include "connections/implementation/mediums/bluetooth_radio.h" -#include "connections/implementation/mediums/mediums.h" #include "connections/medium_selector.h" #include "internal/platform/bluetooth_classic.h" #include "internal/platform/expected.h" @@ -35,8 +36,9 @@ namespace connections { // per-Medium-specific operations needed to upgrade an EndpointChannel. class BluetoothBwuHandler : public BaseBwuHandler { public: - explicit BluetoothBwuHandler( - Mediums& mediums, + BluetoothBwuHandler( + BluetoothRadio* absl_nonnull bluetooth_radio, + BluetoothClassic* absl_nonnull bluetooth_medium, IncomingConnectionCallback incoming_connection_callback); private: @@ -75,9 +77,8 @@ class BluetoothBwuHandler : public BaseBwuHandler { const std::string& upgrade_service_id, BluetoothSocket socket); - Mediums& mediums_; - BluetoothRadio& bluetooth_radio_{mediums_.GetBluetoothRadio()}; - BluetoothClassic& bluetooth_medium_{mediums_.GetBluetoothClassic()}; + BluetoothRadio& bluetooth_radio_; + BluetoothClassic& bluetooth_medium_; }; } // namespace connections diff --git a/connections/implementation/mediums/bluetooth_bwu_handler_test.cc b/connections/implementation/mediums/bluetooth_bwu_handler_test.cc index 2144ed9d..4a5bac2c 100644 --- a/connections/implementation/mediums/bluetooth_bwu_handler_test.cc +++ b/connections/implementation/mediums/bluetooth_bwu_handler_test.cc @@ -54,7 +54,8 @@ TEST_F(BluetoothBwuTest, CanCreateBwuHandler) { ClientProxy client; Mediums mediums; - auto handler = std::make_unique(mediums, nullptr); + auto handler = std::make_unique( + &mediums.GetBluetoothRadio(), &mediums.GetBluetoothClassic(), nullptr); handler->InitializeUpgradedMediumForEndpoint(&client, /*service_id=*/"B", /*endpoint_id=*/"2"); @@ -73,9 +74,10 @@ TEST_F(BluetoothBwuTest, SoftAPBWUInit_STACreateEndpointChannel) { ExceptionOr upgrade_frame; auto handler_1 = std::make_unique( - mediums_1, [&](ClientProxy* client, - std::unique_ptr - mutable_connection) { + &mediums_1.GetBluetoothRadio(), &mediums_1.GetBluetoothClassic(), + [&](ClientProxy* client, + std::unique_ptr + mutable_connection) { LOG(WARNING) << "Server socket connection accept call back"; accept_latch.CountDown(); EXPECT_TRUE(end_latch.Await(kWaitDuration).result()); @@ -99,7 +101,9 @@ TEST_F(BluetoothBwuTest, SoftAPBWUInit_STACreateEndpointChannel) { // Wait till client_1 started as Bluetooth and then connect to it EXPECT_TRUE(start_latch.Await(kWaitDuration).result()); std::unique_ptr handler_2 = - std::make_unique(mediums_2, nullptr); + std::make_unique( + &mediums_2.GetBluetoothRadio(), &mediums_2.GetBluetoothClassic(), + nullptr); client_executor.Execute([&]() { auto bwu_frame = diff --git a/connections/implementation/mediums/bluetooth_classic.cc b/connections/implementation/mediums/bluetooth_classic.cc index 2a971d8a..d0dc386d 100644 --- a/connections/implementation/mediums/bluetooth_classic.cc +++ b/connections/implementation/mediums/bluetooth_classic.cc @@ -18,7 +18,9 @@ #include #include +#include "connections/implementation/bwu_handler.h" #include "connections/implementation/mediums/bluetooth_radio.h" +#include "connections/implementation/mediums/bluetooth_bwu_handler.h" #include "internal/platform/bluetooth_adapter.h" #include "internal/platform/bluetooth_classic.h" #include "internal/platform/cancellation_flag.h" @@ -565,5 +567,12 @@ std::string BluetoothClassic::GenerateUuidFromString(const std::string& data) { return std::string(Uuid(data)); } +std::unique_ptr BluetoothClassic::CreateBwuHandler( + BwuHandler::IncomingConnectionCallback incoming_connection_callback) { + MutexLock lock(&mutex_); + return std::make_unique( + &radio_, this, std::move(incoming_connection_callback)); +} + } // namespace connections } // namespace nearby diff --git a/connections/implementation/mediums/bluetooth_classic.h b/connections/implementation/mediums/bluetooth_classic.h index 37bf8bd3..63ae1e68 100644 --- a/connections/implementation/mediums/bluetooth_classic.h +++ b/connections/implementation/mediums/bluetooth_classic.h @@ -22,6 +22,7 @@ #include "absl/base/thread_annotations.h" #include "absl/container/flat_hash_map.h" #include "absl/functional/any_invocable.h" +#include "connections/implementation/bwu_handler.h" #include "connections/implementation/mediums/bluetooth_radio.h" #include "internal/platform/bluetooth_adapter.h" #include "internal/platform/bluetooth_classic.h" @@ -126,6 +127,9 @@ class BluetoothClassic { bool IsDiscovering(const std::string& serviceId) const ABSL_LOCKS_EXCLUDED(mutex_); + std::unique_ptr CreateBwuHandler( + BwuHandler::IncomingConnectionCallback incoming_connection_callback); + protected: // Use for unit tests only to inject a BluetoothClassicMedium. BluetoothClassic(BluetoothRadio& radio, diff --git a/connections/implementation/mediums/webrtc.h b/connections/implementation/mediums/webrtc.h index 579bccde..4e401036 100644 --- a/connections/implementation/mediums/webrtc.h +++ b/connections/implementation/mediums/webrtc.h @@ -19,6 +19,7 @@ #include #include "absl/functional/any_invocable.h" +#include "connections/implementation/bwu_handler.h" #include "connections/implementation/mediums/webrtc_peer_id.h" #include "connections/implementation/mediums/webrtc_socket.h" #include "connections/implementation/proto/offline_wire_formats.pb.h" @@ -79,6 +80,11 @@ class WebRtc { } virtual bool IsUsingCellular() { return false; } + + virtual std::unique_ptr CreateBwuHandler( + BwuHandler::IncomingConnectionCallback incoming_connection_callback) { + return nullptr; + } }; } // namespace mediums diff --git a/connections/implementation/mediums/webrtc/BUILD b/connections/implementation/mediums/webrtc/BUILD index 23dfffce..c1d34862 100644 --- a/connections/implementation/mediums/webrtc/BUILD +++ b/connections/implementation/mediums/webrtc/BUILD @@ -103,8 +103,14 @@ cc_library( cc_library( name = "webrtc_impl", - srcs = ["webrtc_impl.cc"], - hdrs = ["webrtc_impl.h"], + srcs = [ + "webrtc_bwu_handler.cc", + "webrtc_impl.cc", + ], + hdrs = [ + "webrtc_bwu_handler.h", + "webrtc_impl.h", + ], visibility = [ "//connections/implementation/mediums:__pkg__", ], @@ -113,18 +119,25 @@ cc_library( ":signaling_frames", ":webrtc", ":webrtc_medium", + "//connections:core_types", + "//connections/implementation:bwu_handler", + "//connections/implementation:client_proxy", + "//connections/implementation:endpoint_channel", + "//connections/implementation:offline_frames", + "//connections/implementation/mediums", "//connections/implementation/mediums:webrtc", "//connections/implementation/mediums:webrtc_peer_id", "//connections/implementation/mediums:webrtc_socket", + "//connections/implementation/proto:offline_wire_formats_cc_proto", "//internal/platform:base", "//internal/platform:cancellation_flag", - "//internal/platform:comm", "//internal/platform:logging", "//internal/platform:types", "//proto/mediums:web_rtc_signaling_frames_cc_proto", # "//third_party/webrtc/files/stable/webrtc/api:jsep", "//third_party/webrtc/files/stable/webrtc/rtc_base:network_constants", "@com_google_absl//absl/base:core_headers", + "@com_google_absl//absl/base:nullability", "@com_google_absl//absl/container:flat_hash_map", "@com_google_absl//absl/container:flat_hash_set", "@com_google_absl//absl/functional:bind_front", diff --git a/connections/implementation/mediums/webrtc_bwu_handler.cc b/connections/implementation/mediums/webrtc/webrtc_bwu_handler.cc similarity index 95% rename from connections/implementation/mediums/webrtc_bwu_handler.cc rename to connections/implementation/mediums/webrtc/webrtc_bwu_handler.cc index 31605073..8b041220 100644 --- a/connections/implementation/mediums/webrtc_bwu_handler.cc +++ b/connections/implementation/mediums/webrtc/webrtc_bwu_handler.cc @@ -12,9 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef NO_WEBRTC - -#include "connections/implementation/mediums/webrtc_bwu_handler.h" +#include "connections/implementation/mediums/webrtc/webrtc_bwu_handler.h" #include #include @@ -24,7 +22,8 @@ #include "connections/implementation/base_bwu_handler.h" #include "connections/implementation/client_proxy.h" #include "connections/implementation/endpoint_channel.h" -#include "connections/implementation/mediums/mediums.h" +#include "absl/base/nullability.h" +#include "connections/implementation/mediums/webrtc.h" #include "connections/implementation/mediums/webrtc_endpoint_channel.h" #include "connections/implementation/mediums/webrtc_peer_id.h" #include "connections/implementation/mediums/webrtc_socket.h" @@ -68,9 +67,10 @@ void WebrtcBwuHandler::WebrtcIncomingSocket::Close() { socket_->Close(); } std::string WebrtcBwuHandler::WebrtcIncomingSocket::ToString() { return name_; } WebrtcBwuHandler::WebrtcBwuHandler( - Mediums& mediums, IncomingConnectionCallback incoming_connection_callback) + mediums::WebRtc* absl_nonnull webrtc_medium, + IncomingConnectionCallback incoming_connection_callback) : BaseBwuHandler(std::move(incoming_connection_callback)), - mediums_(mediums) {} + webrtc_(*webrtc_medium) {} // Called by BWU target. Retrieves a new medium info from incoming message, // and establishes connection over WebRTC using this info. @@ -179,5 +179,3 @@ void WebrtcBwuHandler::OnIncomingWebrtcConnection( } // namespace connections } // namespace nearby - -#endif diff --git a/connections/implementation/mediums/webrtc_bwu_handler.h b/connections/implementation/mediums/webrtc/webrtc_bwu_handler.h similarity index 88% rename from connections/implementation/mediums/webrtc_bwu_handler.h rename to connections/implementation/mediums/webrtc/webrtc_bwu_handler.h index 725486ff..f7429a44 100644 --- a/connections/implementation/mediums/webrtc_bwu_handler.h +++ b/connections/implementation/mediums/webrtc/webrtc_bwu_handler.h @@ -12,19 +12,17 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef CORE_INTERNAL_MEDIUMS_WEBRTC_BWU_HANDLER_H_ -#define CORE_INTERNAL_MEDIUMS_WEBRTC_BWU_HANDLER_H_ - -#ifndef NO_WEBRTC +#ifndef CORE_INTERNAL_MEDIUMS_WEBRTC_WEBRTC_BWU_HANDLER_H_ +#define CORE_INTERNAL_MEDIUMS_WEBRTC_WEBRTC_BWU_HANDLER_H_ #include #include +#include "absl/base/nullability.h" #include "connections/implementation/base_bwu_handler.h" #include "connections/implementation/bwu_handler.h" #include "connections/implementation/client_proxy.h" #include "connections/implementation/endpoint_channel.h" -#include "connections/implementation/mediums/mediums.h" #include "connections/implementation/mediums/webrtc.h" #include "connections/implementation/mediums/webrtc_socket.h" #include "connections/medium_selector.h" @@ -37,8 +35,8 @@ namespace connections { // per-Medium-specific operations needed to upgrade an EndpointChannel. class WebrtcBwuHandler : public BaseBwuHandler { public: - explicit WebrtcBwuHandler( - Mediums& mediums, + WebrtcBwuHandler( + mediums::WebRtc* absl_nonnull webrtc_medium, IncomingConnectionCallback incoming_connection_callback); private: @@ -78,13 +76,10 @@ class WebrtcBwuHandler : public BaseBwuHandler { ClientProxy* client, const std::string& upgrade_service_id, std::shared_ptr socket); - Mediums& mediums_; - mediums::WebRtc& webrtc_{mediums_.GetWebRtc()}; + mediums::WebRtc& webrtc_; }; } // namespace connections } // namespace nearby -#endif - -#endif // CORE_INTERNAL_MEDIUMS_WEBRTC_BWU_HANDLER_H_ +#endif // CORE_INTERNAL_MEDIUMS_WEBRTC_WEBRTC_BWU_HANDLER_H_ diff --git a/connections/implementation/mediums/webrtc/webrtc_impl.cc b/connections/implementation/mediums/webrtc/webrtc_impl.cc index b7487bf7..4da2c2dd 100644 --- a/connections/implementation/mediums/webrtc/webrtc_impl.cc +++ b/connections/implementation/mediums/webrtc/webrtc_impl.cc @@ -23,10 +23,12 @@ #include "absl/container/flat_hash_set.h" #include "absl/functional/bind_front.h" #include "absl/time/time.h" +#include "connections/implementation/bwu_handler.h" #include "connections/implementation/mediums/webrtc/connection_flow.h" #include "connections/implementation/mediums/webrtc/session_description_wrapper.h" #include "connections/implementation/mediums/webrtc/signaling_frames.h" #include "connections/implementation/mediums/webrtc/webrtc.h" +#include "connections/implementation/mediums/webrtc/webrtc_bwu_handler.h" #include "connections/implementation/mediums/webrtc_peer_id.h" #include "connections/implementation/mediums/webrtc_socket.h" #include "internal/platform/byte_array.h" @@ -784,6 +786,12 @@ bool WebRtcImpl::IsUsingCellular() { return is_using_cellular_; } +std::unique_ptr WebRtcImpl::CreateBwuHandler( + BwuHandler::IncomingConnectionCallback incoming_connection_callback) { + return std::make_unique( + this, std::move(incoming_connection_callback)); +} + } // namespace mediums } // namespace connections } // namespace nearby diff --git a/connections/implementation/mediums/webrtc/webrtc_impl.h b/connections/implementation/mediums/webrtc/webrtc_impl.h index b4ec12fb..979ebb48 100644 --- a/connections/implementation/mediums/webrtc/webrtc_impl.h +++ b/connections/implementation/mediums/webrtc/webrtc_impl.h @@ -22,6 +22,7 @@ #include "absl/base/thread_annotations.h" #include "absl/container/flat_hash_map.h" +#include "connections/implementation/bwu_handler.h" #include "connections/implementation/mediums/webrtc.h" #include "connections/implementation/mediums/webrtc/connection_flow.h" #include "connections/implementation/mediums/webrtc/session_description_wrapper.h" @@ -68,6 +69,9 @@ class WebRtcImpl : public WebRtc { CancellationFlag* cancellation_flag, bool non_cellular) override ABSL_LOCKS_EXCLUDED(mutex_); bool IsUsingCellular() override ABSL_LOCKS_EXCLUDED(mutex_); + std::unique_ptr CreateBwuHandler( + BwuHandler::IncomingConnectionCallback incoming_connection_callback) + override; protected: // Use for unit tests only to inject a WebRtcMedium. diff --git a/connections/implementation/mediums/webrtc_bwu_handler_stub.cc b/connections/implementation/mediums/webrtc_bwu_handler_stub.cc deleted file mode 100644 index 5c3e4c57..00000000 --- a/connections/implementation/mediums/webrtc_bwu_handler_stub.cc +++ /dev/null @@ -1,84 +0,0 @@ -// Copyright 2020 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#ifdef NO_WEBRTC - -#include "connections/implementation/mediums/webrtc_bwu_handler_stub.h" - -#include -#include -#include - -#include "connections/implementation/base_bwu_handler.h" -#include "connections/implementation/client_proxy.h" -#include "connections/implementation/endpoint_channel.h" -#include "connections/implementation/mediums/mediums.h" -#include "connections/implementation/mediums/webrtc_socket.h" -#include "internal/platform/expected.h" - -namespace nearby { -namespace connections { - -namespace { -using ::location::nearby::connections::BandwidthUpgradeNegotiationFrame; -using ::location::nearby::proto::connections::OperationResultCode; -} // namespace - -WebrtcBwuHandler::WebrtcIncomingSocket::WebrtcIncomingSocket( - const std::string& name, std::shared_ptr socket) - : name_(name), socket_(std::move(socket)) {} - -void WebrtcBwuHandler::WebrtcIncomingSocket::Close() {} - -std::string WebrtcBwuHandler::WebrtcIncomingSocket::ToString() { return ""; } - -WebrtcBwuHandler::WebrtcBwuHandler( - Mediums& mediums, IncomingConnectionCallback incoming_connection_callback) - : BaseBwuHandler(std::move(incoming_connection_callback)), - mediums_(mediums) {} - -// Called by BWU target. Retrieves a new medium info from incoming message, -// and establishes connection over WebRTC using this info. -ErrorOr> -WebrtcBwuHandler::CreateUpgradedEndpointChannel( - ClientProxy* client, const std::string& service_id, - const std::string& endpoint_id, - const BandwidthUpgradeNegotiationFrame::UpgradePathInfo& - upgrade_path_info) { - return {Error(OperationResultCode::DETAIL_UNKNOWN)}; -} - -void WebrtcBwuHandler::HandleRevertInitiatorStateForService( - const std::string& upgrade_service_id) {} - -// Called by BWU initiator. Set up WebRTC upgraded medium for this endpoint, -// and returns a upgrade path info (PeerId, LocationHint) for remote party to -// perform discovery. -std::string WebrtcBwuHandler::HandleInitializeUpgradedMediumForEndpoint( - ClientProxy* client, const std::string& upgrade_service_id, - const std::string& endpoint_id) { - return {}; -} - -// Accept Connection Callback. -// Notifies that the remote party called WebRtc::Connect() -// for this socket. -void WebrtcBwuHandler::OnIncomingWebrtcConnection( - ClientProxy* client, const std::string& upgrade_service_id, - std::shared_ptr socket) {} - -} // namespace connections -} // namespace nearby - -#endif diff --git a/connections/implementation/mediums/webrtc_bwu_handler_stub.h b/connections/implementation/mediums/webrtc_bwu_handler_stub.h deleted file mode 100644 index 859eb6c6..00000000 --- a/connections/implementation/mediums/webrtc_bwu_handler_stub.h +++ /dev/null @@ -1,86 +0,0 @@ -// Copyright 2020 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#ifndef CORE_INTERNAL_MEDIUMS_WEBRTC_BWU_HANDLER_STUB_H_ -#define CORE_INTERNAL_MEDIUMS_WEBRTC_BWU_HANDLER_STUB_H_ - -#ifdef NO_WEBRTC - -#include - -#include "connections/implementation/base_bwu_handler.h" -#include "connections/implementation/client_proxy.h" -#include "connections/implementation/endpoint_channel_manager.h" -#include "connections/implementation/mediums/mediums.h" -#include "connections/implementation/mediums/webrtc_socket.h" -#include "internal/platform/expected.h" - -namespace nearby { -namespace connections { - -// Defines the set of methods that need to be implemented to handle the -// per-Medium-specific operations needed to upgrade an EndpointChannel. -class WebrtcBwuHandler : public BaseBwuHandler { - public: - explicit WebrtcBwuHandler( - Mediums& mediums, - IncomingConnectionCallback incoming_connection_callback); - - private: - class WebrtcIncomingSocket : public BwuHandler::IncomingSocket { - public: - explicit WebrtcIncomingSocket( - const std::string& name, std::shared_ptr socket); - - std::string ToString() override; - void Close() override; - - private: - std::string name_; - std::shared_ptr socket_; - }; - - // BwuHandler implementation: - ErrorOr> CreateUpgradedEndpointChannel( - ClientProxy* client, const std::string& service_id, - const std::string& endpoint_id, - const location::nearby::connections::BandwidthUpgradeNegotiationFrame:: - UpgradePathInfo& upgrade_path_info) final; - location::nearby::proto::connections::Medium GetUpgradeMedium() const final { - return Medium::WEB_RTC; - } - void OnEndpointDisconnect(ClientProxy* client, - const std::string& endpoint_id) final {} - - // BaseBwuHandler implementation: - std::string HandleInitializeUpgradedMediumForEndpoint( - ClientProxy* client, const std::string& upgrade_service_id, - const std::string& endpoint_id) final; - void HandleRevertInitiatorStateForService( - const std::string& upgrade_service_id) final; - - void OnIncomingWebrtcConnection( - ClientProxy* client, const std::string& upgrade_service_id, - std::shared_ptr socket); - - Mediums& mediums_; - mediums::WebRtc& webrtc_{mediums_.GetWebRtc()}; -}; - -} // namespace connections -} // namespace nearby - -#endif - -#endif // CORE_INTERNAL_MEDIUMS_WEBRTC_BWU_HANDLER_STUB_H_ diff --git a/connections/implementation/mediums/wifi_direct.cc b/connections/implementation/mediums/wifi_direct.cc index 75f7b35d..1d1559a8 100644 --- a/connections/implementation/mediums/wifi_direct.cc +++ b/connections/implementation/mediums/wifi_direct.cc @@ -14,12 +14,15 @@ #include "connections/implementation/mediums/wifi_direct.h" +#include #include #include #include #include #include "absl/strings/string_view.h" +#include "connections/implementation/bwu_handler.h" +#include "connections/implementation/mediums/wifi_direct_bwu_handler.h" #include "internal/platform/cancellation_flag.h" #include "internal/platform/expected.h" #include "internal/platform/logging.h" @@ -307,5 +310,12 @@ bool WifiDirect::SetPreferredWifiDirectAuthType(WifiDirectAuthType auth_type) { return true; } +std::unique_ptr WifiDirect::CreateBwuHandler( + BwuHandler::IncomingConnectionCallback incoming_connection_callback) { + MutexLock lock(&mutex_); + return std::make_unique( + this, std::move(incoming_connection_callback)); +} + } // namespace connections } // namespace nearby diff --git a/connections/implementation/mediums/wifi_direct.h b/connections/implementation/mediums/wifi_direct.h index e85d2e64..1ea6211d 100644 --- a/connections/implementation/mediums/wifi_direct.h +++ b/connections/implementation/mediums/wifi_direct.h @@ -19,6 +19,7 @@ #include #include "absl/base/thread_annotations.h" +#include "connections/implementation/bwu_handler.h" #include "absl/container/flat_hash_map.h" #include "absl/functional/any_invocable.h" #include "absl/strings/string_view.h" @@ -110,6 +111,9 @@ class WifiDirect { // Sets the preferred WifiDirect auth type. bool SetPreferredWifiDirectAuthType(WifiDirectAuthType auth_type); + std::unique_ptr CreateBwuHandler( + BwuHandler::IncomingConnectionCallback incoming_connection_callback); + private: mutable Mutex mutex_; static constexpr int kMaxConcurrentAcceptLoops = 5; diff --git a/connections/implementation/mediums/wifi_direct_bwu_handler.cc b/connections/implementation/mediums/wifi_direct_bwu_handler.cc index ad501815..5072e922 100644 --- a/connections/implementation/mediums/wifi_direct_bwu_handler.cc +++ b/connections/implementation/mediums/wifi_direct_bwu_handler.cc @@ -23,7 +23,8 @@ #include "connections/implementation/base_bwu_handler.h" #include "connections/implementation/client_proxy.h" #include "connections/implementation/endpoint_channel.h" -#include "connections/implementation/mediums/mediums.h" +#include "absl/base/nullability.h" +#include "connections/implementation/mediums/wifi_direct.h" #include "connections/implementation/mediums/wifi_direct_endpoint_channel.h" #include "connections/implementation/offline_frames.h" #include "connections/strategy.h" @@ -41,9 +42,10 @@ using ::location::nearby::connections::BandwidthUpgradeNegotiationFrame; using ::location::nearby::proto::connections::OperationResultCode; } // namespace WifiDirectBwuHandler::WifiDirectBwuHandler( - Mediums& mediums, IncomingConnectionCallback incoming_connection_callback) + WifiDirect* absl_nonnull wifi_direct_medium, + IncomingConnectionCallback incoming_connection_callback) : BaseBwuHandler(std::move(incoming_connection_callback)), - mediums_(mediums) {} + wifi_direct_medium_(*wifi_direct_medium) {} // Called by BWU initiator. Set up WifiDirect upgraded medium for this // endpoint, and returns an upgrade path info (ServiceName, Pin for Wifi WPS, diff --git a/connections/implementation/mediums/wifi_direct_bwu_handler.h b/connections/implementation/mediums/wifi_direct_bwu_handler.h index 2f6b86b9..6fec11d3 100644 --- a/connections/implementation/mediums/wifi_direct_bwu_handler.h +++ b/connections/implementation/mediums/wifi_direct_bwu_handler.h @@ -18,11 +18,11 @@ #include #include +#include "absl/base/nullability.h" #include "connections/implementation/base_bwu_handler.h" #include "connections/implementation/bwu_handler.h" #include "connections/implementation/client_proxy.h" #include "connections/implementation/endpoint_channel.h" -#include "connections/implementation/mediums/mediums.h" #include "connections/implementation/mediums/wifi_direct.h" #include "internal/platform/expected.h" #include "internal/platform/wifi_direct.h" @@ -34,8 +34,8 @@ namespace connections { // per-Medium-specific operations needed to upgrade an EndpointChannel. class WifiDirectBwuHandler : public BaseBwuHandler { public: - explicit WifiDirectBwuHandler( - Mediums& mediums, + WifiDirectBwuHandler( + WifiDirect* absl_nonnull wifi_direct_medium, IncomingConnectionCallback incoming_connection_callback); private: @@ -85,8 +85,7 @@ class WifiDirectBwuHandler : public BaseBwuHandler { const std::string& upgrade_service_id, WifiDirectSocket socket); - Mediums& mediums_; - WifiDirect& wifi_direct_medium_ = mediums_.GetWifiDirect(); + WifiDirect& wifi_direct_medium_; }; } // namespace connections diff --git a/connections/implementation/mediums/wifi_direct_bwu_handler_test.cc b/connections/implementation/mediums/wifi_direct_bwu_handler_test.cc index 00689ac9..fd3dccec 100644 --- a/connections/implementation/mediums/wifi_direct_bwu_handler_test.cc +++ b/connections/implementation/mediums/wifi_direct_bwu_handler_test.cc @@ -68,7 +68,8 @@ TEST_F(WifiDirectTest, CanCreateBwuHandler) { ClientProxy client; Mediums mediums; - auto handler = std::make_unique(mediums, nullptr); + auto handler = + std::make_unique(&mediums.GetWifiDirect(), nullptr); handler->InitializeUpgradedMediumForEndpoint(&client, std::string(kServiceID), std::string(kEndpointID)); @@ -87,7 +88,7 @@ TEST_F(WifiDirectTest, WFDGOBWUInit_GCCreateEndpointChannel) { ExceptionOr upgrade_frame; auto wfd_go_bwu_handler = std::make_unique( - mediums_wfd_go, [&](ClientProxy* client, + &mediums_wfd_go.GetWifiDirect(), [&](ClientProxy* client, std::unique_ptr mutable_connection) { LOG(INFO) << "Server socket connection accept call back, Socket name: " @@ -113,7 +114,8 @@ TEST_F(WifiDirectTest, WFDGOBWUInit_GCCreateEndpointChannel) { EXPECT_TRUE(start_latch.Await(kWaitDuration).result()); EXPECT_FALSE(mediums_wfd_gc.GetWifiDirect().IsConnectedToGO()); std::unique_ptr wfd_gc_bwu_handler = - std::make_unique(mediums_wfd_gc, nullptr); + std::make_unique(&mediums_wfd_gc.GetWifiDirect(), + nullptr); wfd_gc_executor.Execute([&]() { UpgradePathInfo upgrade_path_info; diff --git a/connections/implementation/mediums/wifi_hotspot.cc b/connections/implementation/mediums/wifi_hotspot.cc index 6d91e27e..4701a176 100644 --- a/connections/implementation/mediums/wifi_hotspot.cc +++ b/connections/implementation/mediums/wifi_hotspot.cc @@ -15,6 +15,7 @@ #include "connections/implementation/mediums/wifi_hotspot.h" #include +#include #include #include #include @@ -22,12 +23,15 @@ #include "absl/strings/string_view.h" #include "absl/time/clock.h" #include "absl/time/time.h" +#include "connections/implementation/bwu_handler.h" +#include "connections/implementation/mediums/wifi_hotspot_bwu_handler.h" #include "internal/flags/nearby_flags.h" #include "internal/platform/cancellation_flag.h" #include "internal/platform/expected.h" #include "internal/platform/flags/nearby_platform_feature_flags.h" #include "internal/platform/logging.h" #include "internal/platform/mutex_lock.h" +#include "internal/platform/service_address.h" #include "internal/platform/wifi_credential.h" #include "internal/platform/wifi_hotspot.h" @@ -325,5 +329,12 @@ ErrorOr WifiHotspot::Connect( return socket; } +std::unique_ptr WifiHotspot::CreateBwuHandler( + BwuHandler::IncomingConnectionCallback incoming_connection_callback) { + MutexLock lock(&mutex_); + return std::make_unique( + this, std::move(incoming_connection_callback)); +} + } // namespace connections } // namespace nearby diff --git a/connections/implementation/mediums/wifi_hotspot.h b/connections/implementation/mediums/wifi_hotspot.h index 11b0bd3e..06e622c9 100644 --- a/connections/implementation/mediums/wifi_hotspot.h +++ b/connections/implementation/mediums/wifi_hotspot.h @@ -21,6 +21,7 @@ #include "absl/base/thread_annotations.h" #include "absl/container/flat_hash_map.h" #include "absl/strings/string_view.h" +#include "connections/implementation/bwu_handler.h" #include "internal/platform/cancellation_flag.h" #include "internal/platform/expected.h" #include "internal/platform/multi_thread_executor.h" @@ -86,6 +87,9 @@ class WifiHotspot { HotspotCredentials* GetCredentials(absl::string_view service_id) ABSL_LOCKS_EXCLUDED(mutex_); + std::unique_ptr CreateBwuHandler( + BwuHandler::IncomingConnectionCallback incoming_connection_callback); + private: mutable Mutex mutex_; static constexpr int kMaxConcurrentAcceptLoops = 5; diff --git a/connections/implementation/mediums/wifi_hotspot_bwu_handler.cc b/connections/implementation/mediums/wifi_hotspot_bwu_handler.cc index 95b457a7..77d14c0d 100644 --- a/connections/implementation/mediums/wifi_hotspot_bwu_handler.cc +++ b/connections/implementation/mediums/wifi_hotspot_bwu_handler.cc @@ -27,11 +27,12 @@ #include #include +#include "absl/base/nullability.h" #include "absl/functional/bind_front.h" #include "connections/implementation/base_bwu_handler.h" #include "connections/implementation/client_proxy.h" #include "connections/implementation/endpoint_channel.h" -#include "connections/implementation/mediums/mediums.h" +#include "connections/implementation/mediums/wifi_hotspot.h" #include "connections/implementation/mediums/wifi_hotspot_endpoint_channel.h" #include "connections/implementation/offline_frames.h" #include "connections/implementation/proto/offline_wire_formats.pb.h" @@ -66,9 +67,10 @@ std::vector GatewayToAddressBytes(const std::string& gateway) { } // namespace WifiHotspotBwuHandler::WifiHotspotBwuHandler( - Mediums& mediums, IncomingConnectionCallback incoming_connection_callback) + WifiHotspot* absl_nonnull wifi_hotspot_medium, + IncomingConnectionCallback incoming_connection_callback) : BaseBwuHandler(std::move(incoming_connection_callback)), - mediums_(mediums) {} + wifi_hotspot_medium_(*wifi_hotspot_medium) {} // Called by BWU initiator. Set up WifiHotspot upgraded medium for this // endpoint, and returns a upgrade path info (SSID, Password, Gateway used as diff --git a/connections/implementation/mediums/wifi_hotspot_bwu_handler.h b/connections/implementation/mediums/wifi_hotspot_bwu_handler.h index 134c5cc2..1c3c8f83 100644 --- a/connections/implementation/mediums/wifi_hotspot_bwu_handler.h +++ b/connections/implementation/mediums/wifi_hotspot_bwu_handler.h @@ -18,11 +18,11 @@ #include #include +#include "absl/base/nullability.h" #include "connections/implementation/base_bwu_handler.h" #include "connections/implementation/bwu_handler.h" #include "connections/implementation/client_proxy.h" #include "connections/implementation/endpoint_channel.h" -#include "connections/implementation/mediums/mediums.h" #include "connections/implementation/mediums/wifi_hotspot.h" #include "internal/platform/expected.h" #include "internal/platform/wifi_hotspot.h" @@ -34,8 +34,8 @@ namespace connections { // per-Medium-specific operations needed to upgrade an EndpointChannel. class WifiHotspotBwuHandler : public BaseBwuHandler { public: - explicit WifiHotspotBwuHandler( - Mediums& mediums, + WifiHotspotBwuHandler( + WifiHotspot* absl_nonnull wifi_hotspot_medium, IncomingConnectionCallback incoming_connection_callback); // BwuHandler implementation: @@ -77,8 +77,7 @@ class WifiHotspotBwuHandler : public BaseBwuHandler { const std::string& upgrade_service_id, WifiHotspotSocket socket); - Mediums& mediums_; - WifiHotspot& wifi_hotspot_medium_{mediums_.GetWifiHotspot()}; + WifiHotspot& wifi_hotspot_medium_; }; } // namespace connections diff --git a/connections/implementation/mediums/wifi_hotspot_bwu_handler_test.cc b/connections/implementation/mediums/wifi_hotspot_bwu_handler_test.cc index 03978050..fcd42428 100644 --- a/connections/implementation/mediums/wifi_hotspot_bwu_handler_test.cc +++ b/connections/implementation/mediums/wifi_hotspot_bwu_handler_test.cc @@ -69,7 +69,8 @@ TEST_F(WifiHotspotTest, CanCreateBwuHandler) { ClientProxy client; Mediums mediums; - auto handler = std::make_unique(mediums, nullptr); + auto handler = std::make_unique( + &mediums.GetWifiHotspot(), nullptr); handler->InitializeUpgradedMediumForEndpoint(&client, std::string(kServiceID), std::string(kEndpointID)); @@ -88,7 +89,7 @@ TEST_F(WifiHotspotTest, SoftAPBWUInit_STACreateEndpointChannel) { ExceptionOr upgrade_frame; auto handler_1 = std::make_unique( - mediums_HS_ap, [&](ClientProxy* client, + &mediums_HS_ap.GetWifiHotspot(), [&](ClientProxy* client, std::unique_ptr mutable_connection) { LOG(INFO) << "Server socket connection accept call back, Socket name: " @@ -117,7 +118,8 @@ TEST_F(WifiHotspotTest, SoftAPBWUInit_STACreateEndpointChannel) { // Wait till client_hotspot_ap started as hotspot and then connect to it EXPECT_TRUE(start_latch.Await(kWaitDuration).result()); std::unique_ptr handler_2 = - std::make_unique(mediums_HS_sta, nullptr); + std::make_unique(&mediums_HS_sta.GetWifiHotspot(), + nullptr); client_executor.Execute([&]() { UpgradePathInfo upgrade_path_info; diff --git a/connections/implementation/mediums/wifi_lan.cc b/connections/implementation/mediums/wifi_lan.cc index 51ae771a..d789590b 100644 --- a/connections/implementation/mediums/wifi_lan.cc +++ b/connections/implementation/mediums/wifi_lan.cc @@ -15,13 +15,16 @@ #include "connections/implementation/mediums/wifi_lan.h" #include +#include #include #include #include #include "absl/strings/str_cat.h" #include "absl/strings/str_format.h" +#include "connections/implementation/bwu_handler.h" #include "connections/implementation/mediums/utils.h" +#include "connections/implementation/mediums/wifi_lan_bwu_handler.h" #include "internal/platform/byte_array.h" #include "internal/platform/cancellation_flag.h" #include "internal/platform/exception.h" @@ -517,5 +520,12 @@ int WifiLan::GeneratePort(const std::string& service_id, (uint_of_service_id_hash % (port_range.second - port_range.first)); } +std::unique_ptr WifiLan::CreateBwuHandler( + BwuHandler::IncomingConnectionCallback incoming_connection_callback) { + MutexLock lock(&mutex_); + return std::make_unique( + this, std::move(incoming_connection_callback)); +} + } // namespace connections } // namespace nearby diff --git a/connections/implementation/mediums/wifi_lan.h b/connections/implementation/mediums/wifi_lan.h index 39a5cc6a..7d6e6cb1 100644 --- a/connections/implementation/mediums/wifi_lan.h +++ b/connections/implementation/mediums/wifi_lan.h @@ -16,6 +16,7 @@ #define CORE_INTERNAL_MEDIUMS_WIFI_LAN_H_ #include +#include #include #include @@ -23,6 +24,7 @@ #include "absl/container/flat_hash_map.h" #include "absl/container/flat_hash_set.h" #include "absl/functional/any_invocable.h" +#include "connections/implementation/bwu_handler.h" #include "internal/platform/cancellation_flag.h" #include "internal/platform/exception.h" #include "internal/platform/expected.h" @@ -114,6 +116,9 @@ class WifiLan { api::UpgradeAddressInfo GetUpgradeAddressCandidates( const std::string& service_id) ABSL_LOCKS_EXCLUDED(mutex_); + std::unique_ptr CreateBwuHandler( + BwuHandler::IncomingConnectionCallback incoming_connection_callback); + private: struct AdvertisingInfo { bool Empty() const { return nsd_service_infos.empty(); } diff --git a/connections/implementation/mediums/wifi_lan_bwu_handler.cc b/connections/implementation/mediums/wifi_lan_bwu_handler.cc index 7b6e5604..e29e2f99 100644 --- a/connections/implementation/mediums/wifi_lan_bwu_handler.cc +++ b/connections/implementation/mediums/wifi_lan_bwu_handler.cc @@ -24,7 +24,8 @@ #include "connections/implementation/base_bwu_handler.h" #include "connections/implementation/client_proxy.h" #include "connections/implementation/endpoint_channel.h" -#include "connections/implementation/mediums/mediums.h" +#include "absl/base/nullability.h" +#include "connections/implementation/mediums/wifi_lan.h" #include "connections/implementation/mediums/wifi_lan_endpoint_channel.h" #include "connections/implementation/offline_frames.h" #include "internal/platform/expected.h" @@ -42,9 +43,10 @@ using ::location::nearby::proto::connections::OperationResultCode; } // namespace WifiLanBwuHandler::WifiLanBwuHandler( - Mediums& mediums, IncomingConnectionCallback incoming_connection_callback) + WifiLan* absl_nonnull wifi_lan_medium, + IncomingConnectionCallback incoming_connection_callback) : BaseBwuHandler(std::move(incoming_connection_callback)), - mediums_(mediums) {} + wifi_lan_medium_(*wifi_lan_medium) {} // Called by BWU target. Retrieves a new medium info from incoming message, // and establishes connection over WifiLan using this info. diff --git a/connections/implementation/mediums/wifi_lan_bwu_handler.h b/connections/implementation/mediums/wifi_lan_bwu_handler.h index a94a0554..46c4c0a0 100644 --- a/connections/implementation/mediums/wifi_lan_bwu_handler.h +++ b/connections/implementation/mediums/wifi_lan_bwu_handler.h @@ -18,11 +18,11 @@ #include #include +#include "absl/base/nullability.h" #include "connections/implementation/base_bwu_handler.h" #include "connections/implementation/bwu_handler.h" #include "connections/implementation/client_proxy.h" #include "connections/implementation/endpoint_channel.h" -#include "connections/implementation/mediums/mediums.h" #include "connections/implementation/mediums/wifi_lan.h" #include "internal/platform/expected.h" #include "internal/platform/wifi_lan.h" @@ -34,8 +34,8 @@ namespace connections { // per-Medium-specific operations needed to upgrade an EndpointChannel. class WifiLanBwuHandler : public BaseBwuHandler { public: - explicit WifiLanBwuHandler( - Mediums& mediums, + WifiLanBwuHandler( + WifiLan* absl_nonnull wifi_lan_medium, IncomingConnectionCallback incoming_connection_callback); // BwuHandler implementation: @@ -77,8 +77,7 @@ class WifiLanBwuHandler : public BaseBwuHandler { const std::string& upgrade_service_id, WifiLanSocket socket); - Mediums& mediums_; - WifiLan& wifi_lan_medium_{mediums_.GetWifiLan()}; + WifiLan& wifi_lan_medium_; }; } // namespace connections diff --git a/connections/implementation/mediums/wifi_lan_bwu_handler_test.cc b/connections/implementation/mediums/wifi_lan_bwu_handler_test.cc index ad03cd54..24074701 100644 --- a/connections/implementation/mediums/wifi_lan_bwu_handler_test.cc +++ b/connections/implementation/mediums/wifi_lan_bwu_handler_test.cc @@ -72,7 +72,8 @@ constexpr absl::string_view kEndpointId{"endpoint_id"}; class WifiLanBwuHandlerTest : public ::testing::Test { protected: WifiLanBwuHandlerTest() - : handler_(mediums_, incoming_connection_callback_.AsStdFunction()) {} + : handler_(&mediums_.GetWifiLan(), + incoming_connection_callback_.AsStdFunction()) {} Mediums mediums_; MockFunction