From 4e387e7ea789766dce44826aef2b4fea566772aa Mon Sep 17 00:00:00 2001 From: Francis Tsui Date: Fri, 22 May 2026 15:31:52 -0700 Subject: [PATCH] Move platform dependent part of WebRtcMedium into WebRtcPlatform. PiperOrigin-RevId: 919888693 --- connections/implementation/mediums/webrtc.h | 4 ---- .../implementation/mediums/webrtc/BUILD | 1 + .../implementation/mediums/webrtc/webrtc.h | 5 ----- .../mediums/webrtc/webrtc_bwu_handler.cc | 5 +++-- .../mediums/webrtc/webrtc_impl.cc | 4 ---- .../mediums/webrtc/webrtc_impl.h | 1 - .../platform/implementation/apple/webrtc.h | 6 ------ .../platform/implementation/apple/webrtc.mm | 9 --------- .../implementation/apple/webrtc_platform.mm | 8 ++++++++ internal/platform/implementation/g3/BUILD | 1 + internal/platform/implementation/g3/webrtc.cc | 3 +-- internal/platform/implementation/g3/webrtc.h | 2 -- .../implementation/g3/webrtc_platform.cc | 5 +++++ internal/platform/implementation/webrtc.h | 5 ----- .../platform/implementation/webrtc_platform.h | 6 ++++++ .../platform/implementation/windows/webrtc.cc | 18 ------------------ .../platform/implementation/windows/webrtc.h | 6 ------ .../implementation/windows/webrtc_test.cc | 6 ------ 18 files changed, 25 insertions(+), 70 deletions(-) diff --git a/connections/implementation/mediums/webrtc.h b/connections/implementation/mediums/webrtc.h index 4e401036..93203d98 100644 --- a/connections/implementation/mediums/webrtc.h +++ b/connections/implementation/mediums/webrtc.h @@ -40,10 +40,6 @@ class WebRtc { virtual ~WebRtc() = default; - // Gets the default two-letter country code associated with current locale. - // For example, en_US locale resolves to "US". - virtual std::string GetDefaultCountryCode() { return ""; } - // Returns if WebRtc is available as a medium for nearby to transport data. // Runs on @MainThread. virtual bool IsAvailable() { return false; } diff --git a/connections/implementation/mediums/webrtc/BUILD b/connections/implementation/mediums/webrtc/BUILD index a1fae7a8..72ddc3b8 100644 --- a/connections/implementation/mediums/webrtc/BUILD +++ b/connections/implementation/mediums/webrtc/BUILD @@ -132,6 +132,7 @@ cc_library( "//internal/platform:cancellation_flag", "//internal/platform:logging", "//internal/platform:types", + "//internal/platform/implementation:webrtc_platform", "//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", diff --git a/connections/implementation/mediums/webrtc/webrtc.h b/connections/implementation/mediums/webrtc/webrtc.h index 27055b4d..6490f5de 100644 --- a/connections/implementation/mediums/webrtc/webrtc.h +++ b/connections/implementation/mediums/webrtc/webrtc.h @@ -17,7 +17,6 @@ #include #include -#include #include #include "absl/strings/string_view.h" @@ -72,10 +71,6 @@ class WebRtcMedium { WebRtcMedium(WebRtcMedium&&) = default; WebRtcMedium& operator=(WebRtcMedium&&) = delete; - // Gets the default two-letter country code associated with current locale. - // For example, en_US locale resolves to "US". - std::string GetDefaultCountryCode() { return impl_->GetDefaultCountryCode(); } - void SetNonCellular(bool non_cellular) { non_cellular_ = non_cellular; } // Creates and returns a new webrtc::PeerConnectionInterface object via diff --git a/connections/implementation/mediums/webrtc/webrtc_bwu_handler.cc b/connections/implementation/mediums/webrtc/webrtc_bwu_handler.cc index 33d898ec..6c6c1dc0 100644 --- a/connections/implementation/mediums/webrtc/webrtc_bwu_handler.cc +++ b/connections/implementation/mediums/webrtc/webrtc_bwu_handler.cc @@ -30,6 +30,7 @@ #include "connections/implementation/offline_frames.h" #include "connections/implementation/proto/offline_wire_formats.pb.h" #include "internal/platform/expected.h" +#include "internal/platform/implementation/webrtc_platform.h" #include "internal/platform/logging.h" namespace nearby { @@ -135,8 +136,8 @@ void WebrtcBwuHandler::HandleRevertInitiatorStateForService( std::string WebrtcBwuHandler::HandleInitializeUpgradedMediumForEndpoint( ClientProxy* client, const std::string& upgrade_service_id, const std::string& endpoint_id) { - LocationHint location_hint = - BuildLocationHint(webrtc_.GetDefaultCountryCode()); + LocationHint location_hint = BuildLocationHint( + api::WebRtcImplementationPlatform::GetDefaultCountryCode()); mediums::WebrtcPeerId self_id{mediums::WebrtcPeerId::FromRandom()}; if (!webrtc_.IsAcceptingConnections(upgrade_service_id)) { diff --git a/connections/implementation/mediums/webrtc/webrtc_impl.cc b/connections/implementation/mediums/webrtc/webrtc_impl.cc index 4da2c2dd..9732aac9 100644 --- a/connections/implementation/mediums/webrtc/webrtc_impl.cc +++ b/connections/implementation/mediums/webrtc/webrtc_impl.cc @@ -81,10 +81,6 @@ WebRtcImpl::~WebRtcImpl() { } } -std::string WebRtcImpl::GetDefaultCountryCode() { - return medium_->GetDefaultCountryCode(); -} - bool WebRtcImpl::IsAvailable() { return medium_->IsValid(); } bool WebRtcImpl::IsAcceptingConnections(const std::string& service_id) { diff --git a/connections/implementation/mediums/webrtc/webrtc_impl.h b/connections/implementation/mediums/webrtc/webrtc_impl.h index 979ebb48..92d9d5b4 100644 --- a/connections/implementation/mediums/webrtc/webrtc_impl.h +++ b/connections/implementation/mediums/webrtc/webrtc_impl.h @@ -52,7 +52,6 @@ class WebRtcImpl : public WebRtc { ~WebRtcImpl() override; // Overrides for WebRtc: - std::string GetDefaultCountryCode() override; bool IsAvailable() override; bool IsAcceptingConnections(const std::string& service_id) override ABSL_LOCKS_EXCLUDED(mutex_); diff --git a/internal/platform/implementation/apple/webrtc.h b/internal/platform/implementation/apple/webrtc.h index eb3af040..7fa67a98 100644 --- a/internal/platform/implementation/apple/webrtc.h +++ b/internal/platform/implementation/apple/webrtc.h @@ -19,7 +19,6 @@ #include #include -#include #include "absl/strings/string_view.h" #include "internal/platform/implementation/webrtc.h" @@ -31,11 +30,6 @@ class WebRtcMedium : public api::WebRtcMedium { public: ~WebRtcMedium() override = default; - // Gets the default two-letter country code associated with current locale. - // For example, en_US locale resolves to "US". - // This follows the ISO 3166-1 Alpha-2 standard. - std::string GetDefaultCountryCode() override; - // Creates and returns a new webrtc::PeerConnectionInterface object via // |callback|. void CreatePeerConnection(webrtc::PeerConnectionObserver* observer, diff --git a/internal/platform/implementation/apple/webrtc.mm b/internal/platform/implementation/apple/webrtc.mm index 755f0cb5..2479dc42 100644 --- a/internal/platform/implementation/apple/webrtc.mm +++ b/internal/platform/implementation/apple/webrtc.mm @@ -20,7 +20,6 @@ #include #include -#include #include #include "absl/status/status.h" @@ -36,14 +35,6 @@ namespace nearby::apple { -std::string WebRtcMedium::GetDefaultCountryCode() { - NSString* countryCode = [NSLocale.currentLocale objectForKey:NSLocaleCountryCode]; - if (countryCode) { - return std::string([countryCode UTF8String]); - } - return "US"; -} - void WebRtcMedium::CreatePeerConnection(webrtc::PeerConnectionObserver* observer, PeerConnectionCallback callback) { CreatePeerConnection(std::nullopt, observer, std::move(callback)); diff --git a/internal/platform/implementation/apple/webrtc_platform.mm b/internal/platform/implementation/apple/webrtc_platform.mm index 7cec489b..5ed01d91 100644 --- a/internal/platform/implementation/apple/webrtc_platform.mm +++ b/internal/platform/implementation/apple/webrtc_platform.mm @@ -27,5 +27,13 @@ std::unique_ptr WebRtcImplementationPlatform::CreateWebRtcMedium() return std::make_unique(); } +std::string WebRtcImplementationPlatform::GetDefaultCountryCode() { + NSString* countryCode = [NSLocale.currentLocale objectForKey:NSLocaleCountryCode]; + if (countryCode) { + return std::string([countryCode UTF8String]); + } + return "US"; +} + } // namespace api } // namespace nearby diff --git a/internal/platform/implementation/g3/BUILD b/internal/platform/implementation/g3/BUILD index 935599c0..ef0c9b3f 100644 --- a/internal/platform/implementation/g3/BUILD +++ b/internal/platform/implementation/g3/BUILD @@ -121,6 +121,7 @@ cc_library( "//third_party/webrtc/files/stable/webrtc/api:peer_connection_interface", "//third_party/webrtc/files/stable/webrtc/api:scoped_refptr", "//third_party/webrtc/files/stable/webrtc/rtc_base:checks", + "//third_party/webrtc/files/stable/webrtc/rtc_base:threading", "@com_google_absl//absl/base:core_headers", "@com_google_absl//absl/container:flat_hash_map", "@com_google_absl//absl/container:flat_hash_set", diff --git a/internal/platform/implementation/g3/webrtc.cc b/internal/platform/implementation/g3/webrtc.cc index c8385c0c..9659c50d 100644 --- a/internal/platform/implementation/g3/webrtc.cc +++ b/internal/platform/implementation/g3/webrtc.cc @@ -28,6 +28,7 @@ #include "webrtc/api/peer_connection_interface.h" #include "webrtc/api/scoped_refptr.h" #include "webrtc/rtc_base/checks.h" +#include "webrtc/rtc_base/thread.h" namespace nearby { namespace g3 { @@ -60,8 +61,6 @@ void WebRtcSignalingMessenger::StopReceivingMessages() { WebRtcMedium::~WebRtcMedium() { single_thread_executor_.Shutdown(); } -std::string WebRtcMedium::GetDefaultCountryCode() { return "US"; } - void WebRtcMedium::CreatePeerConnection( webrtc::PeerConnectionObserver* observer, PeerConnectionCallback callback) { CreatePeerConnection(std::nullopt, observer, std::move(callback)); diff --git a/internal/platform/implementation/g3/webrtc.h b/internal/platform/implementation/g3/webrtc.h index 048dabf2..435fa57e 100644 --- a/internal/platform/implementation/g3/webrtc.h +++ b/internal/platform/implementation/g3/webrtc.h @@ -59,8 +59,6 @@ class WebRtcMedium : public api::WebRtcMedium { WebRtcMedium() = default; ~WebRtcMedium() override; - std::string GetDefaultCountryCode() override; - // Creates and returns a new webrtc::PeerConnectionInterface object via // |callback|. void CreatePeerConnection(webrtc::PeerConnectionObserver* observer, diff --git a/internal/platform/implementation/g3/webrtc_platform.cc b/internal/platform/implementation/g3/webrtc_platform.cc index 5c42f5ee..227f2e69 100644 --- a/internal/platform/implementation/g3/webrtc_platform.cc +++ b/internal/platform/implementation/g3/webrtc_platform.cc @@ -15,6 +15,7 @@ #include "internal/platform/implementation/webrtc_platform.h" #include +#include #include "internal/platform/implementation/g3/webrtc.h" #include "internal/platform/implementation/webrtc.h" @@ -31,4 +32,8 @@ WebRtcImplementationPlatform::CreateWebRtcMedium() { } } +std::string WebRtcImplementationPlatform::GetDefaultCountryCode() { + return "US"; +} + } // namespace nearby::api diff --git a/internal/platform/implementation/webrtc.h b/internal/platform/implementation/webrtc.h index c9022e28..5b64dd50 100644 --- a/internal/platform/implementation/webrtc.h +++ b/internal/platform/implementation/webrtc.h @@ -17,7 +17,6 @@ #include #include -#include #include "absl/functional/any_invocable.h" #include "absl/strings/string_view.h" @@ -52,10 +51,6 @@ class WebRtcMedium { virtual ~WebRtcMedium() = default; - // Gets the default two-letter country code associated with current locale. - // For example, en_US locale resolves to "US". - virtual std::string GetDefaultCountryCode() = 0; - // Creates and returns a new webrtc::PeerConnectionInterface object via // |callback|. virtual void CreatePeerConnection(webrtc::PeerConnectionObserver* observer, diff --git a/internal/platform/implementation/webrtc_platform.h b/internal/platform/implementation/webrtc_platform.h index 6fdc3bbe..a765179f 100644 --- a/internal/platform/implementation/webrtc_platform.h +++ b/internal/platform/implementation/webrtc_platform.h @@ -16,6 +16,7 @@ #define PLATFORM_API_WEBRTC_PLATFORM_H_ #include +#include #include "internal/platform/implementation/webrtc.h" @@ -24,6 +25,11 @@ namespace nearby::api { class WebRtcImplementationPlatform { public: static std::unique_ptr CreateWebRtcMedium(); + + // Gets the default two-letter country code associated with current locale. + // For example, en_US locale resolves to "US". + // This follows the ISO 3166-1 Alpha-2 standard. + static std::string GetDefaultCountryCode(); }; } // namespace nearby::api diff --git a/internal/platform/implementation/windows/webrtc.cc b/internal/platform/implementation/windows/webrtc.cc index cd6e366c..6a61051a 100644 --- a/internal/platform/implementation/windows/webrtc.cc +++ b/internal/platform/implementation/windows/webrtc.cc @@ -14,17 +14,13 @@ #include "internal/platform/implementation/windows/webrtc.h" -#include - #include #include -#include #include #include "absl/strings/string_view.h" #include "connections/implementation/mediums/webrtc/tachyon_express_signaling_messenger.h" #include "internal/platform/implementation/webrtc.h" -#include "internal/platform/logging.h" #include "webrtc/api/create_modular_peer_connection_factory.h" #include "webrtc/api/peer_connection_interface.h" #include "webrtc/api/rtc_error.h" @@ -35,20 +31,6 @@ namespace nearby::windows { using ::nearby::connections::mediums::TachyonExpressSignalingMessenger; -std::string WebRtcMedium::GetDefaultCountryCode() { - wchar_t systemGeoName[LOCALE_NAME_MAX_LENGTH]; - - if (!GetUserDefaultGeoName(systemGeoName, LOCALE_NAME_MAX_LENGTH)) { - LOG(ERROR) << __func__ - << ": Failed to GetUserDefaultGeoName: " << ". Fall back to US."; - return "US"; - } - std::wstring wideGeo(systemGeoName); - std::string systemGeoNameString(wideGeo.begin(), wideGeo.end()); - VLOG(1) << "GetUserDefaultGeoName() returns: " << systemGeoNameString; - return systemGeoNameString; -} - void WebRtcMedium::CreatePeerConnection( webrtc::PeerConnectionObserver* observer, PeerConnectionCallback callback) { CreatePeerConnection(std::nullopt, observer, std::move(callback)); diff --git a/internal/platform/implementation/windows/webrtc.h b/internal/platform/implementation/windows/webrtc.h index 2c5b3fb3..15ff745c 100644 --- a/internal/platform/implementation/windows/webrtc.h +++ b/internal/platform/implementation/windows/webrtc.h @@ -17,7 +17,6 @@ #include #include -#include #include "absl/strings/string_view.h" #include "internal/platform/implementation/webrtc.h" @@ -29,11 +28,6 @@ class WebRtcMedium : public api::WebRtcMedium { public: ~WebRtcMedium() override = default; - // Gets the default two-letter country code associated with current locale. - // For example, en_US locale resolves to "US". - // This follows the ISO 3166-1 Alpha-2 standard. - std::string GetDefaultCountryCode() override; - // Creates and returns a new webrtc::PeerConnectionInterface object via // |callback|. void CreatePeerConnection(webrtc::PeerConnectionObserver* observer, diff --git a/internal/platform/implementation/windows/webrtc_test.cc b/internal/platform/implementation/windows/webrtc_test.cc index 708e0efc..0ccc4d1a 100644 --- a/internal/platform/implementation/windows/webrtc_test.cc +++ b/internal/platform/implementation/windows/webrtc_test.cc @@ -51,12 +51,6 @@ location::nearby::connections::LocationHint GetCountryCodeLocationHint( return location_hint; } -TEST(WebrtcTest, CountryCodeDefault) { - WebRtcMedium medium; - std::string result = medium.GetDefaultCountryCode(); - EXPECT_EQ(result, "US"); -} - TEST(WebrtcTest, CreatePeerConnectionSucceeds) { auto observer = std::make_unique(); WebRtcMedium medium;