diff --git a/cpp/core/internal/mediums/webrtc.cc b/cpp/core/internal/mediums/webrtc.cc index c91fc270..58639174 100644 --- a/cpp/core/internal/mediums/webrtc.cc +++ b/cpp/core/internal/mediums/webrtc.cc @@ -56,6 +56,10 @@ WebRtc::~WebRtc() { Disconnect(); } +const std::string WebRtc::GetDefaultCountryCode() { + return medium_.GetDefaultCountryCode(); +} + bool WebRtc::IsAvailable() { return medium_.IsValid(); } bool WebRtc::IsAcceptingConnections() { diff --git a/cpp/core/internal/mediums/webrtc.h b/cpp/core/internal/mediums/webrtc.h index 92832c5d..74849b6b 100644 --- a/cpp/core/internal/mediums/webrtc.h +++ b/cpp/core/internal/mediums/webrtc.h @@ -58,6 +58,10 @@ class WebRtc { WebRtc(); ~WebRtc(); + // Gets the default two-letter country code associated with current locale. + // For example, en_US locale resolves to "US". + const std::string GetDefaultCountryCode(); + // Returns if WebRtc is available as a medium for nearby to transport data. // Runs on @MainThread. bool IsAvailable(); diff --git a/cpp/core/internal/webrtc_bwu_handler.cc b/cpp/core/internal/webrtc_bwu_handler.cc index c88d25c5..c1de0fef 100644 --- a/cpp/core/internal/webrtc_bwu_handler.cc +++ b/cpp/core/internal/webrtc_bwu_handler.cc @@ -14,7 +14,6 @@ #include "core/internal/webrtc_bwu_handler.h" -#include #include #include "core/internal/client_proxy.h" @@ -75,7 +74,8 @@ ByteArray WebrtcBwuHandler::InitializeUpgradedMediumForEndpoint( // stop the advertising yet. std::string upgrade_service_id = Utils::WrapUpgradeServiceId(service_id); - LocationHint location_hint = Utils::BuildLocationHint(GetCountryCode()); + LocationHint location_hint = + Utils::BuildLocationHint(webrtc_.GetDefaultCountryCode()); mediums::PeerId self_id{mediums::PeerId::FromRandom()}; if (!webrtc_.IsAcceptingConnections()) { @@ -155,23 +155,6 @@ WebrtcBwuHandler::CreateUpgradedEndpointChannel( void WebrtcBwuHandler::OnEndpointDisconnect(ClientProxy* client, const std::string& endpoint_id) {} -std::string WebrtcBwuHandler::GetCountryCode() { - std::string default_locale_name = std::locale("").name(); - - // locale name has a format: _. - int s = default_locale_name.find("_"); - int e = default_locale_name.find("."); - - if (s == -1 || e == -1) { - return ""; - } - - auto country_code = default_locale_name.substr(s + 1, e - s - 1); - std::transform(country_code.begin(), country_code.end(), country_code.begin(), - std::towlower); - return country_code; -} - WebrtcBwuHandler::WebrtcIncomingSocket::WebrtcIncomingSocket( const std::string& name, mediums::WebRtcSocketWrapper socket) : name_(name), socket_(socket) {} diff --git a/cpp/platform/api/webrtc.h b/cpp/platform/api/webrtc.h index 48c98fc1..4ddfdb12 100644 --- a/cpp/platform/api/webrtc.h +++ b/cpp/platform/api/webrtc.h @@ -47,6 +47,10 @@ 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 const std::string GetDefaultCountryCode() = 0; + // Creates and returns a new webrtc::PeerConnectionInterface object via // |callback|. virtual void CreatePeerConnection(webrtc::PeerConnectionObserver* observer, diff --git a/cpp/platform/impl/g3/webrtc.cc b/cpp/platform/impl/g3/webrtc.cc index 090f1585..7035d67e 100644 --- a/cpp/platform/impl/g3/webrtc.cc +++ b/cpp/platform/impl/g3/webrtc.cc @@ -46,6 +46,10 @@ void WebRtcSignalingMessenger::StopReceivingMessages() { env.UnregisterWebRtcSignalingMessenger(self_id_); } +const std::string WebRtcMedium::GetDefaultCountryCode() { + return "US"; +} + void WebRtcMedium::CreatePeerConnection( webrtc::PeerConnectionObserver* observer, PeerConnectionCallback callback) { auto& env = MediumEnvironment::Instance(); diff --git a/cpp/platform/impl/g3/webrtc.h b/cpp/platform/impl/g3/webrtc.h index 97d458e7..9cd20215 100644 --- a/cpp/platform/impl/g3/webrtc.h +++ b/cpp/platform/impl/g3/webrtc.h @@ -52,6 +52,8 @@ class WebRtcMedium : public api::WebRtcMedium { WebRtcMedium() = default; ~WebRtcMedium() override = default; + const std::string GetDefaultCountryCode() override; + // Creates and returns a new webrtc::PeerConnectionInterface object via // |callback|. void CreatePeerConnection(webrtc::PeerConnectionObserver* observer, diff --git a/cpp/platform/public/webrtc.h b/cpp/platform/public/webrtc.h index d024290d..5d28d2e1 100644 --- a/cpp/platform/public/webrtc.h +++ b/cpp/platform/public/webrtc.h @@ -61,6 +61,12 @@ class WebRtcMedium final { WebRtcMedium(WebRtcMedium&&) = delete; WebRtcMedium& operator=(WebRtcMedium&&) = delete; + // Gets the default two-letter country code associated with current locale. + // For example, en_US locale resolves to "US". + const std::string GetDefaultCountryCode() { + return impl_->GetDefaultCountryCode(); + } + // Creates and returns a new webrtc::PeerConnectionInterface object via // |callback|. void CreatePeerConnection(webrtc::PeerConnectionObserver* observer,