From 75a951665f0a314d873f2d893fda3aa077ef9352 Mon Sep 17 00:00:00 2001 From: hai007 Date: Tue, 3 Nov 2020 15:29:35 -0800 Subject: [PATCH] Roll forward to cl/340537221 Signed-off-by: hai007 --- cpp/core/internal/mediums/webrtc.cc | 4 ++++ cpp/core/internal/mediums/webrtc.h | 4 ++++ cpp/core/internal/webrtc_bwu_handler.cc | 21 ++------------------- cpp/platform/api/webrtc.h | 4 ++++ cpp/platform/impl/g3/webrtc.cc | 4 ++++ cpp/platform/impl/g3/webrtc.h | 2 ++ cpp/platform/public/webrtc.h | 6 ++++++ 7 files changed, 26 insertions(+), 19 deletions(-) diff --git a/cpp/core/internal/mediums/webrtc.cc b/cpp/core/internal/mediums/webrtc.cc index 9c46feec..2ba4ce0e 100644 --- a/cpp/core/internal/mediums/webrtc.cc +++ b/cpp/core/internal/mediums/webrtc.cc @@ -42,6 +42,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 92f3d95e..b2831fae 100644 --- a/cpp/core/internal/mediums/webrtc.h +++ b/cpp/core/internal/mediums/webrtc.h @@ -44,6 +44,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 22eef4d6..40b9fa08 100644 --- a/cpp/core/internal/webrtc_bwu_handler.cc +++ b/cpp/core/internal/webrtc_bwu_handler.cc @@ -1,6 +1,5 @@ #include "core/internal/webrtc_bwu_handler.h" -#include #include #include "core/internal/client_proxy.h" @@ -61,7 +60,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()) { @@ -141,23 +141,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 5b9bbf5d..43805f42 100644 --- a/cpp/platform/api/webrtc.h +++ b/cpp/platform/api/webrtc.h @@ -33,6 +33,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 bd6d2ac1..9d4966e4 100644 --- a/cpp/platform/impl/g3/webrtc.cc +++ b/cpp/platform/impl/g3/webrtc.cc @@ -32,6 +32,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 b9a6c4ea..be2a75ae 100644 --- a/cpp/platform/impl/g3/webrtc.h +++ b/cpp/platform/impl/g3/webrtc.h @@ -38,6 +38,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 ad7d4f18..f975916c 100644 --- a/cpp/platform/public/webrtc.h +++ b/cpp/platform/public/webrtc.h @@ -47,6 +47,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,