Commit 98dd0269 authored by Himanshu Jaju's avatar Himanshu Jaju Committed by Commit Bot

Add mojo for signaling messenger

Implements mojo interactions b/w utility process and browser process for
signaling messenger.
IceConfigFetcher will be implemented along with webrtc medium in upcoming
CLs.

Design doc - go/nearby-chrome-webrtc
Master design doc - go/nearby-chrome

Bug: 1082305
Change-Id: Ia8cb9f3746f781e1ec941925ddbb46d4d9f6300a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2270092
Commit-Queue: Himanshu Jaju <himanshujaju@chromium.org>
Reviewed-by: default avatarAlex Chau <alexchau@chromium.org>
Reviewed-by: default avatarAlex Gough <ajgo@chromium.org>
Cr-Commit-Position: refs/heads/master@{#784439}
parent d40c7abd
...@@ -3325,6 +3325,8 @@ static_library("browser") { ...@@ -3325,6 +3325,8 @@ static_library("browser") {
"nearby_sharing/transfer_metadata.cc", "nearby_sharing/transfer_metadata.cc",
"nearby_sharing/transfer_metadata.h", "nearby_sharing/transfer_metadata.h",
"nearby_sharing/transfer_update_callback.h", "nearby_sharing/transfer_update_callback.h",
"nearby_sharing/webrtc_signaling_messenger.cc",
"nearby_sharing/webrtc_signaling_messenger.h",
"notifications/notification_system_observer.cc", "notifications/notification_system_observer.cc",
"notifications/notification_system_observer.h", "notifications/notification_system_observer.h",
"notifications/profile_notification.cc", "notifications/profile_notification.cc",
......
...@@ -12,13 +12,17 @@ ...@@ -12,13 +12,17 @@
#include "build/build_config.h" #include "build/build_config.h"
#include "chrome/browser/browser_process.h" #include "chrome/browser/browser_process.h"
#include "chrome/browser/nearby_sharing/nearby_sharing_prefs.h" #include "chrome/browser/nearby_sharing/nearby_sharing_prefs.h"
#include "chrome/browser/nearby_sharing/webrtc_signaling_messenger.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_attributes_entry.h" #include "chrome/browser/profiles/profile_attributes_entry.h"
#include "chrome/browser/profiles/profile_attributes_storage.h" #include "chrome/browser/profiles/profile_attributes_storage.h"
#include "chrome/browser/profiles/profile_manager.h" #include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/sharing/webrtc/sharing_mojo_service.h" #include "chrome/browser/sharing/webrtc/sharing_mojo_service.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "components/prefs/pref_service.h" #include "components/prefs/pref_service.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "content/public/browser/service_process_host.h" #include "content/public/browser/service_process_host.h"
#include "content/public/browser/storage_partition.h"
#include "device/bluetooth/adapter.h" #include "device/bluetooth/adapter.h"
#include "device/bluetooth/bluetooth_adapter_factory.h" #include "device/bluetooth/bluetooth_adapter_factory.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h" #include "mojo/public/cpp/bindings/self_owned_receiver.h"
...@@ -199,6 +203,28 @@ void NearbyProcessManager::GetBluetoothAdapter( ...@@ -199,6 +203,28 @@ void NearbyProcessManager::GetBluetoothAdapter(
} }
} }
void NearbyProcessManager::GetWebRtcSignalingMessenger(
location::nearby::connections::mojom::NearbyConnectionsHost::
GetWebRtcSignalingMessengerCallback callback) {
if (!IsAnyProfileActive()) {
std::move(callback).Run(/*messenger=*/mojo::NullRemote());
return;
}
auto url_loader_factory =
content::BrowserContext::GetDefaultStoragePartition(active_profile_)
->GetURLLoaderFactoryForBrowserProcess();
signin::IdentityManager* identity_manager =
IdentityManagerFactory::GetForProfile(active_profile_);
mojo::PendingRemote<sharing::mojom::WebRtcSignalingMessenger> messenger;
mojo::MakeSelfOwnedReceiver(
std::make_unique<WebRtcSignalingMessenger>(identity_manager,
std::move(url_loader_factory)),
messenger.InitWithNewPipeAndPassReceiver());
std::move(callback).Run(std::move(messenger));
}
NearbyProcessManager::NearbyProcessManager() { NearbyProcessManager::NearbyProcessManager() {
// profile_manager() might be null in tests or during shutdown. // profile_manager() might be null in tests or during shutdown.
if (auto* manager = g_browser_process->profile_manager()) if (auto* manager = g_browser_process->profile_manager())
......
...@@ -115,6 +115,8 @@ class NearbyProcessManager ...@@ -115,6 +115,8 @@ class NearbyProcessManager
// location::nearby::connections::mojom::NearbyConnectionsHost: // location::nearby::connections::mojom::NearbyConnectionsHost:
void GetBluetoothAdapter(GetBluetoothAdapterCallback callback) override; void GetBluetoothAdapter(GetBluetoothAdapterCallback callback) override;
void GetWebRtcSignalingMessenger(
GetWebRtcSignalingMessengerCallback callback) override;
private: private:
FRIEND_TEST_ALL_PREFIXES(NearbyProcessManagerTest, AddRemoveObserver); FRIEND_TEST_ALL_PREFIXES(NearbyProcessManagerTest, AddRemoveObserver);
......
...@@ -417,3 +417,13 @@ TEST_F(NearbyProcessManagerTest, GetBluetoothAdapter) { ...@@ -417,3 +417,13 @@ TEST_F(NearbyProcessManagerTest, GetBluetoothAdapter) {
pending_remote_adapter) { loop.Quit(); })); pending_remote_adapter) { loop.Quit(); }));
loop.Run(); loop.Run();
} }
TEST_F(NearbyProcessManagerTest, GetWebRtcSignalingMessenger) {
auto& manager = NearbyProcessManager::GetInstance();
base::RunLoop loop;
manager.GetWebRtcSignalingMessenger(base::BindLambdaForTesting(
[&](mojo::PendingRemote<sharing::mojom::WebRtcSignalingMessenger>
messenger) { loop.Quit(); }));
loop.Run();
}
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/nearby_sharing/webrtc_signaling_messenger.h"
#include "base/bind_helpers.h"
#include "base/token.h"
#include "chrome/browser/nearby_sharing/instantmessaging/proto/instantmessaging.pb.h"
namespace {
const char kAppName[] = "Nearby";
void BuildId(chrome_browser_nearby_sharing_instantmessaging::Id* req_id,
const std::string& id) {
DCHECK(req_id);
req_id->set_id(id);
req_id->set_app(kAppName);
req_id->set_type(
chrome_browser_nearby_sharing_instantmessaging::IdType::NEARBY_ID);
}
void BuildHeader(
chrome_browser_nearby_sharing_instantmessaging::RequestHeader* header,
const std::string& requester_id) {
DCHECK(header);
header->set_app(kAppName);
BuildId(header->mutable_requester_id(), requester_id);
chrome_browser_nearby_sharing_instantmessaging::ClientInfo* info =
header->mutable_client_info();
info->set_api_version(
chrome_browser_nearby_sharing_instantmessaging::ApiVersion::V4);
info->set_platform_type(
chrome_browser_nearby_sharing_instantmessaging::Platform::DESKTOP);
}
} // namespace
WebRtcSignalingMessenger::WebRtcSignalingMessenger(
signin::IdentityManager* identity_manager,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory)
: token_fetcher_(identity_manager),
send_message_express_(&token_fetcher_, url_loader_factory),
receive_messages_express_(&token_fetcher_, url_loader_factory) {}
WebRtcSignalingMessenger::~WebRtcSignalingMessenger() = default;
void WebRtcSignalingMessenger::SendMessage(const std::string& self_id,
const std::string& peer_id,
const std::string& message,
SendMessageCallback callback) {
chrome_browser_nearby_sharing_instantmessaging::SendMessageExpressRequest
request;
BuildId(request.mutable_dest_id(), peer_id);
BuildHeader(request.mutable_header(), self_id);
std::string message_id = base::Token::CreateRandom().ToString();
chrome_browser_nearby_sharing_instantmessaging::InboxMessage* inbox_message =
request.mutable_message();
inbox_message->set_message_id(message_id);
inbox_message->set_message(message);
send_message_express_.SendMessage(request, std::move(callback));
}
void WebRtcSignalingMessenger::StartReceivingMessages(
const std::string& self_id,
mojo::PendingRemote<sharing::mojom::IncomingMessagesListener>
incoming_messages_listener,
StartReceivingMessagesCallback callback) {
chrome_browser_nearby_sharing_instantmessaging::ReceiveMessagesExpressRequest
request;
BuildHeader(request.mutable_header(), self_id);
incoming_messages_listener_.reset();
incoming_messages_listener_.Bind(std::move(incoming_messages_listener));
// base::Unretained is safe since |this| owns |receive_messages_express_|.
receive_messages_express_.StartReceivingMessages(
request,
base::BindRepeating(&WebRtcSignalingMessenger::OnMessageReceived,
base::Unretained(this)),
base::BindOnce(&WebRtcSignalingMessenger::OnStartedReceivingMessages,
base::Unretained(this), std::move(callback)));
}
void WebRtcSignalingMessenger::StopReceivingMessages() {
incoming_messages_listener_.reset();
receive_messages_express_.StopReceivingMessages();
}
void WebRtcSignalingMessenger::OnStartedReceivingMessages(
StartReceivingMessagesCallback callback,
bool success) {
if (!success)
incoming_messages_listener_.reset();
std::move(callback).Run(success);
}
void WebRtcSignalingMessenger::OnMessageReceived(const std::string& message) {
if (!incoming_messages_listener_)
return;
incoming_messages_listener_->OnMessage(message);
}
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_NEARBY_SHARING_WEBRTC_SIGNALING_MESSENGER_H_
#define CHROME_BROWSER_NEARBY_SHARING_WEBRTC_SIGNALING_MESSENGER_H_
#include <string>
#include "base/callback.h"
#include "chrome/browser/nearby_sharing/instantmessaging/receive_messages_express.h"
#include "chrome/browser/nearby_sharing/instantmessaging/send_message_express.h"
#include "chrome/browser/nearby_sharing/instantmessaging/token_fetcher.h"
#include "chrome/services/sharing/public/mojom/webrtc_signaling_messenger.mojom.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
class WebRtcSignalingMessenger
: public sharing::mojom::WebRtcSignalingMessenger {
public:
WebRtcSignalingMessenger(
signin::IdentityManager* identity_manager,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory);
~WebRtcSignalingMessenger() override;
// sharing::mojom::WebRtcSignalingMessenger:
void SendMessage(const std::string& self_id,
const std::string& peer_id,
const std::string& message,
SendMessageCallback callback) override;
void StartReceivingMessages(
const std::string& self_id,
mojo::PendingRemote<sharing::mojom::IncomingMessagesListener>
incoming_messages_listener,
StartReceivingMessagesCallback callback) override;
void StopReceivingMessages() override;
private:
void OnStartedReceivingMessages(StartReceivingMessagesCallback callback,
bool success);
void OnMessageReceived(const std::string& message);
TokenFetcher token_fetcher_;
SendMessageExpress send_message_express_;
ReceiveMessagesExpress receive_messages_express_;
mojo::Remote<sharing::mojom::IncomingMessagesListener>
incoming_messages_listener_;
};
#endif // CHROME_BROWSER_NEARBY_SHARING_WEBRTC_SIGNALING_MESSENGER_H_
...@@ -6,6 +6,8 @@ source_set("sharing") { ...@@ -6,6 +6,8 @@ source_set("sharing") {
sources = [ sources = [
"nearby/nearby_connections.cc", "nearby/nearby_connections.cc",
"nearby/nearby_connections.h", "nearby/nearby_connections.h",
"nearby/webrtc/incoming_messages_listener.cc",
"nearby/webrtc/incoming_messages_listener.h",
"sharing_impl.cc", "sharing_impl.cc",
"sharing_impl.h", "sharing_impl.h",
"webrtc/ipc_network_manager.cc", "webrtc/ipc_network_manager.cc",
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include "chrome/services/sharing/nearby/test/mock_nearby_connections_host.h" #include "chrome/services/sharing/nearby/test/mock_nearby_connections_host.h"
#include "chrome/services/sharing/public/mojom/webrtc.mojom.h"
namespace location { namespace location {
namespace nearby { namespace nearby {
namespace connections { namespace connections {
...@@ -17,6 +19,11 @@ void MockNearbyConnectionsHost::GetBluetoothAdapter( ...@@ -17,6 +19,11 @@ void MockNearbyConnectionsHost::GetBluetoothAdapter(
std::move(callback).Run(/*adapter=*/mojo::NullRemote()); std::move(callback).Run(/*adapter=*/mojo::NullRemote());
} }
void MockNearbyConnectionsHost::GetWebRtcSignalingMessenger(
GetWebRtcSignalingMessengerCallback callback) {
std::move(callback).Run(mojo::NullRemote());
}
} // namespace connections } // namespace connections
} // namespace nearby } // namespace nearby
} // namespace location } // namespace location
...@@ -24,6 +24,9 @@ class MockNearbyConnectionsHost : public mojom::NearbyConnectionsHost { ...@@ -24,6 +24,9 @@ class MockNearbyConnectionsHost : public mojom::NearbyConnectionsHost {
// mojom::NearbyConnectionsHost // mojom::NearbyConnectionsHost
void GetBluetoothAdapter(GetBluetoothAdapterCallback callback) override; void GetBluetoothAdapter(GetBluetoothAdapterCallback callback) override;
void GetWebRtcSignalingMessenger(
GetWebRtcSignalingMessengerCallback callback) override;
mojo::Receiver<mojom::NearbyConnectionsHost> host{this}; mojo::Receiver<mojom::NearbyConnectionsHost> host{this};
}; };
......
per-file *_messages*.h=set noparent
per-file *_messages*.h=file://ipc/SECURITY_OWNERS
\ No newline at end of file
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/services/sharing/nearby/webrtc/incoming_messages_listener.h"
namespace sharing {
IncomingMessagesListener::IncomingMessagesListener(
base::RepeatingCallback<void(const std::string& message)> listener)
: listener_(listener) {}
IncomingMessagesListener::~IncomingMessagesListener() = default;
void IncomingMessagesListener::OnMessage(const std::string& message) {
listener_.Run(message);
}
} // namespace sharing
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_SERVICES_SHARING_NEARBY_WEBRTC_INCOMING_MESSAGES_LISTENER_H_
#define CHROME_SERVICES_SHARING_NEARBY_WEBRTC_INCOMING_MESSAGES_LISTENER_H_
#include <string>
#include "base/callback.h"
#include "chrome/services/sharing/public/mojom/webrtc_signaling_messenger.mojom.h"
namespace sharing {
class IncomingMessagesListener : public mojom::IncomingMessagesListener {
public:
explicit IncomingMessagesListener(
base::RepeatingCallback<void(const std::string& message)> listener);
~IncomingMessagesListener() override;
// mojom::IncomingMessagesListener:
void OnMessage(const std::string& message) override;
private:
base::RepeatingCallback<void(const std::string& message)> listener_;
};
} // namespace sharing
#endif // CHROME_SERVICES_SHARING_NEARBY_WEBRTC_INCOMING_MESSAGES_LISTENER_H_
\ No newline at end of file
...@@ -8,6 +8,7 @@ mojom("mojom") { ...@@ -8,6 +8,7 @@ mojom("mojom") {
"nearby_decoder_types.mojom", "nearby_decoder_types.mojom",
"sharing.mojom", "sharing.mojom",
"webrtc.mojom", "webrtc.mojom",
"webrtc_signaling_messenger.mojom",
] ]
public_deps = [ public_deps = [
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
module location.nearby.connections.mojom; module location.nearby.connections.mojom;
import "chrome/services/sharing/public/mojom/nearby_connections_types.mojom"; import "chrome/services/sharing/public/mojom/nearby_connections_types.mojom";
import "chrome/services/sharing/public/mojom/webrtc_signaling_messenger.mojom";
import "device/bluetooth/public/mojom/adapter.mojom"; import "device/bluetooth/public/mojom/adapter.mojom";
// Main interface to control the NearbyConnections library. Implemented in a // Main interface to control the NearbyConnections library. Implemented in a
...@@ -31,4 +32,8 @@ interface NearbyConnectionsHost { ...@@ -31,4 +32,8 @@ interface NearbyConnectionsHost {
// 3) If Bluetooth is supported by Chrome on the OS and there is valid adapter // 3) If Bluetooth is supported by Chrome on the OS and there is valid adapter
// we return an adapter instance with the AdapterInfo.present=true. // we return an adapter instance with the AdapterInfo.present=true.
GetBluetoothAdapter() => (pending_remote<bluetooth.mojom.Adapter>? adapter); GetBluetoothAdapter() => (pending_remote<bluetooth.mojom.Adapter>? adapter);
// Gets the current webrtc signaling messenger from the host.
GetWebRtcSignalingMessenger()
=> (pending_remote<sharing.mojom.WebRtcSignalingMessenger> messenger);
}; };
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
module sharing.mojom;
// Listens for incoming webrtc signaling messages from other peers.
interface IncomingMessagesListener {
// Called by the browser process when a new message is received.
OnMessage(string message);
};
// Runs in browser process and is used by the nearby library to send and
// receive messages.
interface WebRtcSignalingMessenger {
// Sends a signaling message |message| to |peer_id|.
SendMessage(string self_id, string peer_id, string message)
=> (bool success);
// Registers |listener| to start receiving messages from other devices.
StartReceivingMessages(string self_id,
pending_remote<IncomingMessagesListener> listener) => (bool success);
// Unregisters listener and stops receiving messages from other devices.
StopReceivingMessages();
};
\ No newline at end of file
...@@ -3633,6 +3633,7 @@ test("unit_tests") { ...@@ -3633,6 +3633,7 @@ test("unit_tests") {
"../browser/nearby_sharing/instantmessaging/stream_parser_unittest.cc", "../browser/nearby_sharing/instantmessaging/stream_parser_unittest.cc",
"../browser/nearby_sharing/nearby_process_manager_unittest.cc", "../browser/nearby_sharing/nearby_process_manager_unittest.cc",
"../browser/nearby_sharing/nearby_sharing_service_impl_unittest.cc", "../browser/nearby_sharing/nearby_sharing_service_impl_unittest.cc",
"../browser/nearby_sharing/webrtc_signaling_messenger_unittest.cc",
"../browser/profiles/profile_avatar_icon_util_unittest.cc", "../browser/profiles/profile_avatar_icon_util_unittest.cc",
"../browser/profiles/profile_destroyer_unittest.cc", "../browser/profiles/profile_destroyer_unittest.cc",
"../browser/safe_browsing/generated_safe_browsing_pref_unittest.cc", "../browser/safe_browsing/generated_safe_browsing_pref_unittest.cc",
......
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