Commit 5810031b authored by James Vecore's avatar James Vecore Committed by Chromium LUCI CQ

[Nearby] Plumb location hint for WebRtc signaling

This CL plumbs the location hint from Nearby Connections to the
SendMessage and StartReceivingMessages calls and puts the information
into the Tachyon server requests. This allows Tachyon to use the
location hint for geo sharding when available.

Fixed: 1142001
Change-Id: I3345ea4e6936c934be46178b2128bca66a69af87
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2568228Reviewed-by: default avatarAlex Gough <ajgo@chromium.org>
Reviewed-by: default avatarJosh Nohle <nohle@chromium.org>
Commit-Queue: James Vecore <vecore@google.com>
Cr-Commit-Position: refs/heads/master@{#833368}
parent 7420a23d
...@@ -15,30 +15,45 @@ constexpr int kMajorVersion = 1; ...@@ -15,30 +15,45 @@ constexpr int kMajorVersion = 1;
constexpr int kMinorVersion = 24; constexpr int kMinorVersion = 24;
constexpr int kPointVersion = 0; constexpr int kPointVersion = 0;
using LocationStandard_Format =
chrome_browser_nearby_sharing_instantmessaging::LocationStandard_Format;
LocationStandard_Format ToProto(sharing::mojom::LocationStandardFormat format) {
switch (format) {
case sharing::mojom::LocationStandardFormat::E164_CALLING:
return LocationStandard_Format::LocationStandard_Format_E164_CALLING;
case sharing::mojom::LocationStandardFormat::ISO_3166_1_ALPHA_2:
return LocationStandard_Format::
LocationStandard_Format_ISO_3166_1_ALPHA_2;
}
}
void BuildLocationHint( void BuildLocationHint(
chrome_browser_nearby_sharing_instantmessaging::LocationHint* chrome_browser_nearby_sharing_instantmessaging::LocationHint* location_hint,
location_hint) { sharing::mojom::LocationHintPtr location_hint_ptr) {
location_hint->set_location(base::CountryCodeForCurrentTimezone()); location_hint->set_location(location_hint_ptr->location);
location_hint->set_format(chrome_browser_nearby_sharing_instantmessaging:: location_hint->set_format(ToProto(location_hint_ptr->format));
LocationStandard_Format_ISO_3166_1_ALPHA_2);
} }
void BuildId(chrome_browser_nearby_sharing_instantmessaging::Id* req_id, void BuildId(chrome_browser_nearby_sharing_instantmessaging::Id* req_id,
const std::string& id) { const std::string& id,
sharing::mojom::LocationHintPtr location_hint) {
DCHECK(req_id); DCHECK(req_id);
req_id->set_id(id); req_id->set_id(id);
req_id->set_app(kAppName); req_id->set_app(kAppName);
req_id->set_type( req_id->set_type(
chrome_browser_nearby_sharing_instantmessaging::IdType::NEARBY_ID); chrome_browser_nearby_sharing_instantmessaging::IdType::NEARBY_ID);
BuildLocationHint(req_id->mutable_location_hint()); BuildLocationHint(req_id->mutable_location_hint(), std::move(location_hint));
} }
void BuildHeader( void BuildHeader(
chrome_browser_nearby_sharing_instantmessaging::RequestHeader* header, chrome_browser_nearby_sharing_instantmessaging::RequestHeader* header,
const std::string& requester_id) { const std::string& requester_id,
sharing::mojom::LocationHintPtr location_hint) {
DCHECK(header); DCHECK(header);
header->set_app(kAppName); header->set_app(kAppName);
BuildId(header->mutable_requester_id(), requester_id); BuildId(header->mutable_requester_id(), requester_id,
std::move(location_hint));
chrome_browser_nearby_sharing_instantmessaging::ClientInfo* info = chrome_browser_nearby_sharing_instantmessaging::ClientInfo* info =
header->mutable_client_info(); header->mutable_client_info();
info->set_api_version( info->set_api_version(
...@@ -53,18 +68,21 @@ void BuildHeader( ...@@ -53,18 +68,21 @@ void BuildHeader(
} // namespace } // namespace
chrome_browser_nearby_sharing_instantmessaging::SendMessageExpressRequest chrome_browser_nearby_sharing_instantmessaging::SendMessageExpressRequest
BuildSendRequest(const std::string& self_id, const std::string& peer_id) { BuildSendRequest(const std::string& self_id,
const std::string& peer_id,
sharing::mojom::LocationHintPtr location_hint) {
chrome_browser_nearby_sharing_instantmessaging::SendMessageExpressRequest chrome_browser_nearby_sharing_instantmessaging::SendMessageExpressRequest
request; request;
BuildId(request.mutable_dest_id(), peer_id); BuildId(request.mutable_dest_id(), peer_id, location_hint->Clone());
BuildHeader(request.mutable_header(), self_id); BuildHeader(request.mutable_header(), self_id, location_hint->Clone());
return request; return request;
} }
chrome_browser_nearby_sharing_instantmessaging::ReceiveMessagesExpressRequest chrome_browser_nearby_sharing_instantmessaging::ReceiveMessagesExpressRequest
BuildReceiveRequest(const std::string& self_id) { BuildReceiveRequest(const std::string& self_id,
sharing::mojom::LocationHintPtr location_hint) {
chrome_browser_nearby_sharing_instantmessaging::ReceiveMessagesExpressRequest chrome_browser_nearby_sharing_instantmessaging::ReceiveMessagesExpressRequest
request; request;
BuildHeader(request.mutable_header(), self_id); BuildHeader(request.mutable_header(), self_id, std::move(location_hint));
return request; return request;
} }
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#define CHROME_BROWSER_NEARBY_SHARING_WEBRTC_REQUEST_BUILDER_H_ #define CHROME_BROWSER_NEARBY_SHARING_WEBRTC_REQUEST_BUILDER_H_
#include <string> #include <string>
#include "chromeos/services/nearby/public/mojom/webrtc_signaling_messenger.mojom.h"
namespace chrome_browser_nearby_sharing_instantmessaging { namespace chrome_browser_nearby_sharing_instantmessaging {
...@@ -15,9 +16,12 @@ class ReceiveMessagesExpressRequest; ...@@ -15,9 +16,12 @@ class ReceiveMessagesExpressRequest;
} // namespace chrome_browser_nearby_sharing_instantmessaging } // namespace chrome_browser_nearby_sharing_instantmessaging
chrome_browser_nearby_sharing_instantmessaging::SendMessageExpressRequest chrome_browser_nearby_sharing_instantmessaging::SendMessageExpressRequest
BuildSendRequest(const std::string& self_id, const std::string& peer_id); BuildSendRequest(const std::string& self_id,
const std::string& peer_id,
sharing::mojom::LocationHintPtr hint);
chrome_browser_nearby_sharing_instantmessaging::ReceiveMessagesExpressRequest chrome_browser_nearby_sharing_instantmessaging::ReceiveMessagesExpressRequest
BuildReceiveRequest(const std::string& self_id); BuildReceiveRequest(const std::string& self_id,
sharing::mojom::LocationHintPtr hint);
#endif // CHROME_BROWSER_NEARBY_SHARING_WEBRTC_REQUEST_BUILDER_H_ #endif // CHROME_BROWSER_NEARBY_SHARING_WEBRTC_REQUEST_BUILDER_H_
...@@ -6,6 +6,8 @@ ...@@ -6,6 +6,8 @@
#include "base/i18n/timezone.h" #include "base/i18n/timezone.h"
#include "chrome/browser/nearby_sharing/instantmessaging/proto/instantmessaging.pb.h" #include "chrome/browser/nearby_sharing/instantmessaging/proto/instantmessaging.pb.h"
#include "chromeos/services/nearby/public/mojom/webrtc_signaling_messenger.mojom-shared.h"
#include "chromeos/services/nearby/public/mojom/webrtc_signaling_messenger.mojom.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "third_party/icu/source/i18n/unicode/timezone.h" #include "third_party/icu/source/i18n/unicode/timezone.h"
...@@ -23,27 +25,49 @@ class WebRtcRequestBuilderTest : public testing::Test { ...@@ -23,27 +25,49 @@ class WebRtcRequestBuilderTest : public testing::Test {
icu::TimeZone::createTimeZone("America/Los_Angeles")); icu::TimeZone::createTimeZone("America/Los_Angeles"));
} }
sharing::mojom::LocationHintPtr CountryCodeLocationHint(
const std::string& country_code) {
sharing::mojom::LocationHintPtr location_hint_ptr =
sharing::mojom::LocationHint::New();
location_hint_ptr->location = country_code;
location_hint_ptr->format =
sharing::mojom::LocationStandardFormat::ISO_3166_1_ALPHA_2;
return location_hint_ptr;
}
sharing::mojom::LocationHintPtr CallingCodeLocationHint(
const std::string& calling_code) {
sharing::mojom::LocationHintPtr location_hint_ptr =
sharing::mojom::LocationHint::New();
location_hint_ptr->location = calling_code;
location_hint_ptr->format =
sharing::mojom::LocationStandardFormat::E164_CALLING;
return location_hint_ptr;
}
void VerifyLocationHint( void VerifyLocationHint(
sharing::mojom::LocationHintPtr expected_location_hint,
chrome_browser_nearby_sharing_instantmessaging::Id id) { chrome_browser_nearby_sharing_instantmessaging::Id id) {
EXPECT_EQ(chrome_browser_nearby_sharing_instantmessaging:: EXPECT_EQ(static_cast<int>(expected_location_hint->format),
LocationStandard_Format_ISO_3166_1_ALPHA_2, static_cast<int>(id.location_hint().format()));
id.location_hint().format()); EXPECT_EQ(expected_location_hint->location, id.location_hint().location());
EXPECT_EQ("US", id.location_hint().location());
} }
}; };
TEST_F(WebRtcRequestBuilderTest, BuildSendRequest) { TEST_F(WebRtcRequestBuilderTest, BuildSendRequest) {
sharing::mojom::LocationHintPtr location_hint = CountryCodeLocationHint("ZZ");
chrome_browser_nearby_sharing_instantmessaging::SendMessageExpressRequest chrome_browser_nearby_sharing_instantmessaging::SendMessageExpressRequest
request = BuildSendRequest(kSelfId, kPeerId); request = BuildSendRequest(kSelfId, kPeerId, location_hint.Clone());
EXPECT_EQ(kSelfId, request.header().requester_id().id()); EXPECT_EQ(kSelfId, request.header().requester_id().id());
EXPECT_EQ(kPeerId, request.dest_id().id()); EXPECT_EQ(kPeerId, request.dest_id().id());
VerifyLocationHint(request.dest_id()); VerifyLocationHint(location_hint.Clone(), request.dest_id());
VerifyLocationHint(request.header().requester_id()); VerifyLocationHint(location_hint.Clone(), request.header().requester_id());
} }
TEST_F(WebRtcRequestBuilderTest, BuildReceiveRequest) { TEST_F(WebRtcRequestBuilderTest, BuildReceiveRequest) {
sharing::mojom::LocationHintPtr location_hint = CallingCodeLocationHint("+1");
chrome_browser_nearby_sharing_instantmessaging::ReceiveMessagesExpressRequest chrome_browser_nearby_sharing_instantmessaging::ReceiveMessagesExpressRequest
request = BuildReceiveRequest(kSelfId); request = BuildReceiveRequest(kSelfId, location_hint.Clone());
EXPECT_EQ(kSelfId, request.header().requester_id().id()); EXPECT_EQ(kSelfId, request.header().requester_id().id());
VerifyLocationHint(request.header().requester_id()); VerifyLocationHint(location_hint.Clone(), request.header().requester_id());
} }
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "base/callback_helpers.h" #include "base/callback_helpers.h"
#include "base/token.h" #include "base/token.h"
#include "chrome/browser/nearby_sharing/instantmessaging/proto/instantmessaging.pb.h" #include "chrome/browser/nearby_sharing/instantmessaging/proto/instantmessaging.pb.h"
#include "chrome/browser/nearby_sharing/logging/logging.h"
#include "chrome/browser/nearby_sharing/webrtc_request_builder.h" #include "chrome/browser/nearby_sharing/webrtc_request_builder.h"
WebRtcSignalingMessenger::WebRtcSignalingMessenger( WebRtcSignalingMessenger::WebRtcSignalingMessenger(
...@@ -18,12 +19,20 @@ WebRtcSignalingMessenger::WebRtcSignalingMessenger( ...@@ -18,12 +19,20 @@ WebRtcSignalingMessenger::WebRtcSignalingMessenger(
WebRtcSignalingMessenger::~WebRtcSignalingMessenger() = default; WebRtcSignalingMessenger::~WebRtcSignalingMessenger() = default;
void WebRtcSignalingMessenger::SendMessage(const std::string& self_id, void WebRtcSignalingMessenger::SendMessage(
const std::string& peer_id, const std::string& self_id,
const std::string& message, const std::string& peer_id,
SendMessageCallback callback) { sharing::mojom::LocationHintPtr location_hint,
const std::string& message,
SendMessageCallback callback) {
NS_LOG(VERBOSE) << __func__ << ": self_id=" << self_id
<< ", peer_id=" << peer_id
<< ", location hint=" << location_hint->location
<< ", location format=" << location_hint->format
<< ", message size=" << message.size();
chrome_browser_nearby_sharing_instantmessaging::SendMessageExpressRequest chrome_browser_nearby_sharing_instantmessaging::SendMessageExpressRequest
request = BuildSendRequest(self_id, peer_id); request = BuildSendRequest(self_id, peer_id, std::move(location_hint));
chrome_browser_nearby_sharing_instantmessaging::InboxMessage* inbox_message = chrome_browser_nearby_sharing_instantmessaging::InboxMessage* inbox_message =
request.mutable_message(); request.mutable_message();
...@@ -39,11 +48,16 @@ void WebRtcSignalingMessenger::SendMessage(const std::string& self_id, ...@@ -39,11 +48,16 @@ void WebRtcSignalingMessenger::SendMessage(const std::string& self_id,
void WebRtcSignalingMessenger::StartReceivingMessages( void WebRtcSignalingMessenger::StartReceivingMessages(
const std::string& self_id, const std::string& self_id,
sharing::mojom::LocationHintPtr location_hint,
mojo::PendingRemote<sharing::mojom::IncomingMessagesListener> mojo::PendingRemote<sharing::mojom::IncomingMessagesListener>
incoming_messages_listener, incoming_messages_listener,
StartReceivingMessagesCallback callback) { StartReceivingMessagesCallback callback) {
NS_LOG(INFO) << __func__ << ": self_id=" << self_id
<< ", location hint=" << location_hint->location
<< ", location format=" << location_hint->format;
chrome_browser_nearby_sharing_instantmessaging::ReceiveMessagesExpressRequest chrome_browser_nearby_sharing_instantmessaging::ReceiveMessagesExpressRequest
request = BuildReceiveRequest(self_id); request = BuildReceiveRequest(self_id, std::move(location_hint));
incoming_messages_listener_.reset(); incoming_messages_listener_.reset();
incoming_messages_listener_.Bind(std::move(incoming_messages_listener)); incoming_messages_listener_.Bind(std::move(incoming_messages_listener));
...@@ -58,6 +72,7 @@ void WebRtcSignalingMessenger::StartReceivingMessages( ...@@ -58,6 +72,7 @@ void WebRtcSignalingMessenger::StartReceivingMessages(
} }
void WebRtcSignalingMessenger::StopReceivingMessages() { void WebRtcSignalingMessenger::StopReceivingMessages() {
NS_LOG(VERBOSE) << __func__;
incoming_messages_listener_.reset(); incoming_messages_listener_.reset();
receive_messages_express_.StopReceivingMessages(); receive_messages_express_.StopReceivingMessages();
} }
...@@ -65,15 +80,21 @@ void WebRtcSignalingMessenger::StopReceivingMessages() { ...@@ -65,15 +80,21 @@ void WebRtcSignalingMessenger::StopReceivingMessages() {
void WebRtcSignalingMessenger::OnStartedReceivingMessages( void WebRtcSignalingMessenger::OnStartedReceivingMessages(
StartReceivingMessagesCallback callback, StartReceivingMessagesCallback callback,
bool success) { bool success) {
if (!success) if (success) {
NS_LOG(VERBOSE) << __func__ << ": started receiving messages successfully";
} else {
NS_LOG(ERROR) << __func__ << ": failed to start receiving messages";
incoming_messages_listener_.reset(); incoming_messages_listener_.reset();
}
std::move(callback).Run(success); std::move(callback).Run(success);
} }
void WebRtcSignalingMessenger::OnMessageReceived(const std::string& message) { void WebRtcSignalingMessenger::OnMessageReceived(const std::string& message) {
if (!incoming_messages_listener_) if (!incoming_messages_listener_) {
NS_LOG(WARNING) << __func__ << ": no listener available to receive message";
return; return;
}
incoming_messages_listener_->OnMessage(message); incoming_messages_listener_->OnMessage(message);
} }
...@@ -26,10 +26,12 @@ class WebRtcSignalingMessenger ...@@ -26,10 +26,12 @@ class WebRtcSignalingMessenger
// sharing::mojom::WebRtcSignalingMessenger: // sharing::mojom::WebRtcSignalingMessenger:
void SendMessage(const std::string& self_id, void SendMessage(const std::string& self_id,
const std::string& peer_id, const std::string& peer_id,
sharing::mojom::LocationHintPtr location_hint,
const std::string& message, const std::string& message,
SendMessageCallback callback) override; SendMessageCallback callback) override;
void StartReceivingMessages( void StartReceivingMessages(
const std::string& self_id, const std::string& self_id,
sharing::mojom::LocationHintPtr location_hint,
mojo::PendingRemote<sharing::mojom::IncomingMessagesListener> mojo::PendingRemote<sharing::mojom::IncomingMessagesListener>
incoming_messages_listener, incoming_messages_listener,
StartReceivingMessagesCallback callback) override; StartReceivingMessagesCallback callback) override;
......
...@@ -23,6 +23,7 @@ namespace { ...@@ -23,6 +23,7 @@ namespace {
const char kSelfId[] = "self_id"; const char kSelfId[] = "self_id";
const char kOAuthToken[] = "oauth_token"; const char kOAuthToken[] = "oauth_token";
const char kTestAccount[] = "test@test.test"; const char kTestAccount[] = "test@test.test";
const char kCountryCode[] = "ZZ";
chrome_browser_nearby_sharing_instantmessaging::ReceiveMessagesResponse chrome_browser_nearby_sharing_instantmessaging::ReceiveMessagesResponse
CreateReceiveMessagesResponse(const std::string& msg) { CreateReceiveMessagesResponse(const std::string& msg) {
...@@ -87,6 +88,16 @@ class WebRtcSignalingMessengerTest : public testing::Test { ...@@ -87,6 +88,16 @@ class WebRtcSignalingMessengerTest : public testing::Test {
return test_url_loader_factory_; return test_url_loader_factory_;
} }
sharing::mojom::LocationHintPtr CountryCodeLocationHint(
std::string country_code) {
sharing::mojom::LocationHintPtr location_hint_ptr =
sharing::mojom::LocationHint::New();
location_hint_ptr->location = country_code;
location_hint_ptr->format =
sharing::mojom::LocationStandardFormat::ISO_3166_1_ALPHA_2;
return location_hint_ptr;
}
// Required to ensure that the listener has received all messages before we // Required to ensure that the listener has received all messages before we
// can continue with our tests. // can continue with our tests.
void RunUntilIdle() { task_environment_.RunUntilIdle(); } void RunUntilIdle() { task_environment_.RunUntilIdle(); }
...@@ -100,7 +111,8 @@ class WebRtcSignalingMessengerTest : public testing::Test { ...@@ -100,7 +111,8 @@ class WebRtcSignalingMessengerTest : public testing::Test {
TEST_F(WebRtcSignalingMessengerTest, UnsuccessfulSendMessage_EmptyToken) { TEST_F(WebRtcSignalingMessengerTest, UnsuccessfulSendMessage_EmptyToken) {
base::RunLoop loop; base::RunLoop loop;
GetMessenger().SendMessage(kSelfId, "peer_id", "message", GetMessenger().SendMessage(kSelfId, "peer_id",
CountryCodeLocationHint(kCountryCode), "message",
base::BindLambdaForTesting([&](bool success) { base::BindLambdaForTesting([&](bool success) {
EXPECT_FALSE(success); EXPECT_FALSE(success);
loop.Quit(); loop.Quit();
...@@ -111,7 +123,8 @@ TEST_F(WebRtcSignalingMessengerTest, UnsuccessfulSendMessage_EmptyToken) { ...@@ -111,7 +123,8 @@ TEST_F(WebRtcSignalingMessengerTest, UnsuccessfulSendMessage_EmptyToken) {
TEST_F(WebRtcSignalingMessengerTest, UnsuccessfulSendMessage_HttpError) { TEST_F(WebRtcSignalingMessengerTest, UnsuccessfulSendMessage_HttpError) {
base::RunLoop loop; base::RunLoop loop;
GetMessenger().SendMessage(kSelfId, "peer_id", "message", GetMessenger().SendMessage(kSelfId, "peer_id",
CountryCodeLocationHint(kCountryCode), "message",
base::BindLambdaForTesting([&](bool success) { base::BindLambdaForTesting([&](bool success) {
EXPECT_FALSE(success); EXPECT_FALSE(success);
loop.Quit(); loop.Quit();
...@@ -128,7 +141,8 @@ TEST_F(WebRtcSignalingMessengerTest, UnsuccessfulSendMessage_HttpError) { ...@@ -128,7 +141,8 @@ TEST_F(WebRtcSignalingMessengerTest, UnsuccessfulSendMessage_HttpError) {
TEST_F(WebRtcSignalingMessengerTest, SuccessfulSendMessage) { TEST_F(WebRtcSignalingMessengerTest, SuccessfulSendMessage) {
base::RunLoop loop; base::RunLoop loop;
GetMessenger().SendMessage(kSelfId, "peer_id", "message", GetMessenger().SendMessage(kSelfId, "peer_id",
CountryCodeLocationHint(kCountryCode), "message",
base::BindLambdaForTesting([&](bool success) { base::BindLambdaForTesting([&](bool success) {
EXPECT_TRUE(success); EXPECT_TRUE(success);
loop.Quit(); loop.Quit();
...@@ -150,7 +164,8 @@ TEST_F(WebRtcSignalingMessengerTest, UnsuccessfulReceiveMessages_EmptyToken) { ...@@ -150,7 +164,8 @@ TEST_F(WebRtcSignalingMessengerTest, UnsuccessfulReceiveMessages_EmptyToken) {
base::RunLoop loop; base::RunLoop loop;
GetMessenger().StartReceivingMessages( GetMessenger().StartReceivingMessages(
kSelfId, mojo_receiver.BindNewPipeAndPassRemote(), kSelfId, CountryCodeLocationHint(kCountryCode),
mojo_receiver.BindNewPipeAndPassRemote(),
base::BindLambdaForTesting([&](bool success) { base::BindLambdaForTesting([&](bool success) {
EXPECT_FALSE(success); EXPECT_FALSE(success);
loop.Quit(); loop.Quit();
...@@ -166,7 +181,8 @@ TEST_F(WebRtcSignalingMessengerTest, UnsuccessfulReceiveMessages_HttpError) { ...@@ -166,7 +181,8 @@ TEST_F(WebRtcSignalingMessengerTest, UnsuccessfulReceiveMessages_HttpError) {
base::RunLoop loop; base::RunLoop loop;
GetMessenger().StartReceivingMessages( GetMessenger().StartReceivingMessages(
kSelfId, mojo_receiver.BindNewPipeAndPassRemote(), kSelfId, CountryCodeLocationHint(kCountryCode),
mojo_receiver.BindNewPipeAndPassRemote(),
base::BindLambdaForTesting([&](bool success) { base::BindLambdaForTesting([&](bool success) {
EXPECT_FALSE(success); EXPECT_FALSE(success);
loop.Quit(); loop.Quit();
...@@ -191,7 +207,8 @@ TEST_F(WebRtcSignalingMessengerTest, SuccessfulReceiveMessages) { ...@@ -191,7 +207,8 @@ TEST_F(WebRtcSignalingMessengerTest, SuccessfulReceiveMessages) {
base::RunLoop loop; base::RunLoop loop;
GetMessenger().StartReceivingMessages( GetMessenger().StartReceivingMessages(
kSelfId, mojo_receiver.BindNewPipeAndPassRemote(), kSelfId, CountryCodeLocationHint(kCountryCode),
mojo_receiver.BindNewPipeAndPassRemote(),
base::BindLambdaForTesting([&](bool success) { base::BindLambdaForTesting([&](bool success) {
EXPECT_TRUE(success); EXPECT_TRUE(success);
loop.Quit(); loop.Quit();
...@@ -220,7 +237,8 @@ TEST_F(WebRtcSignalingMessengerTest, ...@@ -220,7 +237,8 @@ TEST_F(WebRtcSignalingMessengerTest,
base::RunLoop loop_1; base::RunLoop loop_1;
GetMessenger().StartReceivingMessages( GetMessenger().StartReceivingMessages(
kSelfId, mojo_receiver_1.BindNewPipeAndPassRemote(), kSelfId, CountryCodeLocationHint(kCountryCode),
mojo_receiver_1.BindNewPipeAndPassRemote(),
base::BindLambdaForTesting([&](bool success) { base::BindLambdaForTesting([&](bool success) {
EXPECT_TRUE(success); EXPECT_TRUE(success);
loop_1.Quit(); loop_1.Quit();
...@@ -240,7 +258,8 @@ TEST_F(WebRtcSignalingMessengerTest, ...@@ -240,7 +258,8 @@ TEST_F(WebRtcSignalingMessengerTest,
base::RunLoop loop_2; base::RunLoop loop_2;
GetMessenger().StartReceivingMessages( GetMessenger().StartReceivingMessages(
kSelfId, mojo_receiver_2.BindNewPipeAndPassRemote(), kSelfId, CountryCodeLocationHint(kCountryCode),
mojo_receiver_2.BindNewPipeAndPassRemote(),
base::BindLambdaForTesting([&](bool success) { base::BindLambdaForTesting([&](bool success) {
EXPECT_TRUE(success); EXPECT_TRUE(success);
loop_2.Quit(); loop_2.Quit();
...@@ -268,7 +287,8 @@ TEST_F(WebRtcSignalingMessengerTest, StopReceivingMessages) { ...@@ -268,7 +287,8 @@ TEST_F(WebRtcSignalingMessengerTest, StopReceivingMessages) {
base::RunLoop loop; base::RunLoop loop;
GetMessenger().StartReceivingMessages( GetMessenger().StartReceivingMessages(
kSelfId, mojo_receiver.BindNewPipeAndPassRemote(), kSelfId, CountryCodeLocationHint(kCountryCode),
mojo_receiver.BindNewPipeAndPassRemote(),
base::BindLambdaForTesting([&](bool success) { base::BindLambdaForTesting([&](bool success) {
EXPECT_TRUE(success); EXPECT_TRUE(success);
loop.Quit(); loop.Quit();
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "chrome/services/sharing/webrtc/ipc_packet_socket_factory.h" #include "chrome/services/sharing/webrtc/ipc_packet_socket_factory.h"
#include "chrome/services/sharing/webrtc/mdns_responder_adapter.h" #include "chrome/services/sharing/webrtc/mdns_responder_adapter.h"
#include "chrome/services/sharing/webrtc/p2p_port_allocator.h" #include "chrome/services/sharing/webrtc/p2p_port_allocator.h"
#include "chromeos/services/nearby/public/mojom/webrtc_signaling_messenger.mojom-shared.h"
#include "jingle/glue/thread_wrapper.h" #include "jingle/glue/thread_wrapper.h"
#include "mojo/public/cpp/bindings/receiver.h" #include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h" #include "mojo/public/cpp/bindings/self_owned_receiver.h"
...@@ -109,9 +110,11 @@ class WebRtcSignalingMessengerImpl : public api::WebRtcSignalingMessenger { ...@@ -109,9 +110,11 @@ class WebRtcSignalingMessengerImpl : public api::WebRtcSignalingMessenger {
public: public:
WebRtcSignalingMessengerImpl( WebRtcSignalingMessengerImpl(
const std::string& self_id, const std::string& self_id,
const connections::LocationHint& location_hint,
const mojo::SharedRemote<sharing::mojom::WebRtcSignalingMessenger>& const mojo::SharedRemote<sharing::mojom::WebRtcSignalingMessenger>&
messenger) messenger)
: self_id_(self_id), : self_id_(self_id),
location_hint_(location_hint),
messenger_(messenger), messenger_(messenger),
task_runner_( task_runner_(
base::ThreadPool::CreateSequencedTaskRunner({base::MayBlock()})) {} base::ThreadPool::CreateSequencedTaskRunner({base::MayBlock()})) {}
...@@ -123,12 +126,39 @@ class WebRtcSignalingMessengerImpl : public api::WebRtcSignalingMessenger { ...@@ -123,12 +126,39 @@ class WebRtcSignalingMessengerImpl : public api::WebRtcSignalingMessenger {
WebRtcSignalingMessengerImpl& operator=( WebRtcSignalingMessengerImpl& operator=(
const WebRtcSignalingMessengerImpl& other) = delete; const WebRtcSignalingMessengerImpl& other) = delete;
sharing::mojom::LocationHintPtr CreateLocationHint() {
sharing::mojom::LocationHintPtr location_hint_ptr =
sharing::mojom::LocationHint::New();
location_hint_ptr->location = location_hint_.location();
switch (location_hint_.format()) {
case location::nearby::connections::LocationStandard_Format::
LocationStandard_Format_E164_CALLING:
location_hint_ptr->format =
sharing::mojom::LocationStandardFormat::E164_CALLING;
break;
case location::nearby::connections::LocationStandard_Format::
LocationStandard_Format_ISO_3166_1_ALPHA_2:
location_hint_ptr->format =
sharing::mojom::LocationStandardFormat::ISO_3166_1_ALPHA_2;
break;
case location::nearby::connections::LocationStandard_Format::
LocationStandard_Format_UNKNOWN:
// Here we default to the current default country code before sending.
location_hint_ptr->location = base::CountryCodeForCurrentTimezone();
location_hint_ptr->format =
sharing::mojom::LocationStandardFormat::ISO_3166_1_ALPHA_2;
break;
}
return location_hint_ptr;
}
// api::WebRtcSignalingMessenger: // api::WebRtcSignalingMessenger:
bool SendMessage(absl::string_view peer_id, bool SendMessage(absl::string_view peer_id,
const ByteArray& message) override { const ByteArray& message) override {
bool success = false; bool success = false;
if (!messenger_->SendMessage(self_id_, std::string(peer_id), if (!messenger_->SendMessage(self_id_, std::string(peer_id),
std::string(message), &success)) { CreateLocationHint(), std::string(message),
&success)) {
return false; return false;
} }
...@@ -155,7 +185,8 @@ class WebRtcSignalingMessengerImpl : public api::WebRtcSignalingMessenger { ...@@ -155,7 +185,8 @@ class WebRtcSignalingMessengerImpl : public api::WebRtcSignalingMessenger {
pending_remote; pending_remote;
mojo::PendingReceiver<sharing::mojom::IncomingMessagesListener> mojo::PendingReceiver<sharing::mojom::IncomingMessagesListener>
pending_receiver = pending_remote.InitWithNewPipeAndPassReceiver(); pending_receiver = pending_remote.InitWithNewPipeAndPassReceiver();
if (!messenger_->StartReceivingMessages(self_id_, std::move(pending_remote), if (!messenger_->StartReceivingMessages(self_id_, CreateLocationHint(),
std::move(pending_remote),
&success) || &success) ||
!success) { !success) {
receiving_messages_ = false; receiving_messages_ = false;
...@@ -186,6 +217,7 @@ class WebRtcSignalingMessengerImpl : public api::WebRtcSignalingMessenger { ...@@ -186,6 +217,7 @@ class WebRtcSignalingMessengerImpl : public api::WebRtcSignalingMessenger {
private: private:
bool receiving_messages_ = false; bool receiving_messages_ = false;
std::string self_id_; std::string self_id_;
connections::LocationHint location_hint_;
mojo::SharedRemote<sharing::mojom::WebRtcSignalingMessenger> messenger_; mojo::SharedRemote<sharing::mojom::WebRtcSignalingMessenger> messenger_;
scoped_refptr<base::SequencedTaskRunner> task_runner_; scoped_refptr<base::SequencedTaskRunner> task_runner_;
}; };
...@@ -307,9 +339,8 @@ std::unique_ptr<api::WebRtcSignalingMessenger> ...@@ -307,9 +339,8 @@ std::unique_ptr<api::WebRtcSignalingMessenger>
WebRtcMedium::GetSignalingMessenger( WebRtcMedium::GetSignalingMessenger(
absl::string_view self_id, absl::string_view self_id,
const connections::LocationHint& location_hint) { const connections::LocationHint& location_hint) {
// TODO(https://crbug.com/1142001): Use |location_hint|.
return std::make_unique<WebRtcSignalingMessengerImpl>( return std::make_unique<WebRtcSignalingMessengerImpl>(
std::string(self_id), webrtc_signaling_messenger_); std::string(self_id), location_hint, webrtc_signaling_messenger_);
} }
} // namespace chrome } // namespace chrome
......
...@@ -71,6 +71,7 @@ class MockWebRtcDependencies : public network::mojom::P2PSocketManager, ...@@ -71,6 +71,7 @@ class MockWebRtcDependencies : public network::mojom::P2PSocketManager,
SendMessage, SendMessage,
(const std::string& self_id, (const std::string& self_id,
const std::string& peer_id, const std::string& peer_id,
sharing::mojom::LocationHintPtr location_hint,
const std::string& message, const std::string& message,
SendMessageCallback callback), SendMessageCallback callback),
(override)); (override));
...@@ -78,6 +79,7 @@ class MockWebRtcDependencies : public network::mojom::P2PSocketManager, ...@@ -78,6 +79,7 @@ class MockWebRtcDependencies : public network::mojom::P2PSocketManager,
MOCK_METHOD(void, MOCK_METHOD(void,
StartReceivingMessages, StartReceivingMessages,
(const std::string& self_id, (const std::string& self_id,
sharing::mojom::LocationHintPtr location_hint,
mojo::PendingRemote<sharing::mojom::IncomingMessagesListener> mojo::PendingRemote<sharing::mojom::IncomingMessagesListener>
incoming_messages_listener, incoming_messages_listener,
StartReceivingMessagesCallback callback), StartReceivingMessagesCallback callback),
......
...@@ -10,6 +10,34 @@ interface IncomingMessagesListener { ...@@ -10,6 +10,34 @@ interface IncomingMessagesListener {
OnMessage(string message); OnMessage(string message);
}; };
// NOTE: Keep in sync with:
// third_party/nearby/src/proto/connections/offline_wire_formats.proto
enum LocationStandardFormat {
// NOTE: UNKNOWN = 0 is intentionally omitted.
// E164 country codes:
// https://en.wikipedia.org/wiki/List_of_country_calling_codes
// e.g. +1 for USA
E164_CALLING = 1,
// ISO 3166-1 alpha-2 country codes:
// https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
ISO_3166_1_ALPHA_2 = 2,
};
// |LocationHint| is used by Tachyon to choose the right geo shard. During
// bandwidth upgrade the requester sends its location hint over the connection
// and this is used to ensure both parties are talking to the same Tachyon
// instance.
// NOTE: Keep in sync with:
// third_party/nearby/src/proto/connections/offline_wire_formats.proto
struct LocationHint {
// The location as an E164_CALLING code or an ISO 3166-1 alpha-2 country code.
string location;
// Defines the format of the location string.
LocationStandardFormat format;
};
// Runs in browser process and is used by the nearby library to send and // Runs in browser process and is used by the nearby library to send and
// receive messages. // receive messages.
// TODO(crbug.com/1106387) : Update Nearby library to use async callbacks // TODO(crbug.com/1106387) : Update Nearby library to use async callbacks
...@@ -17,12 +45,12 @@ interface IncomingMessagesListener { ...@@ -17,12 +45,12 @@ interface IncomingMessagesListener {
interface WebRtcSignalingMessenger { interface WebRtcSignalingMessenger {
// Sends a signaling message |message| to |peer_id|. // Sends a signaling message |message| to |peer_id|.
[Sync] [Sync]
SendMessage(string self_id, string peer_id, string message) SendMessage(string self_id, string peer_id, LocationHint location_hint,
=> (bool success); string message) => (bool success);
// Registers |listener| to start receiving messages from other devices. // Registers |listener| to start receiving messages from other devices.
[Sync] [Sync]
StartReceivingMessages(string self_id, StartReceivingMessages(string self_id, LocationHint location_hint,
pending_remote<IncomingMessagesListener> listener) => (bool success); pending_remote<IncomingMessagesListener> listener) => (bool success);
// Unregisters listener and stops receiving messages from other devices. // Unregisters listener and stops receiving messages from other devices.
......
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