Commit 50199f11 authored by James Vecore's avatar James Vecore Committed by Commit Bot

[Nearby] Add BT adapter to NearbyConnectionsHost interface

The NearbyConnectionsHost mojo interface allows the utility process to
query for any browser owned mojo interfaces that it needs. The Bluetooth
adapter is the first example of a mojo interface provided for the
utility process.

Change-Id: Ia8065586f60fad0728cd7bdbd95ead630f9aaac4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2220711
Commit-Queue: James Vecore <vecore@google.com>
Reviewed-by: default avatarAlex Gough <ajgo@chromium.org>
Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Reviewed-by: default avatarRichard Knoll <knollr@chromium.org>
Reviewed-by: default avatarRyan Hansberry <hansberry@chromium.org>
Cr-Commit-Position: refs/heads/master@{#773860}
parent 8ba3e2a3
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "chrome/browser/nearby_sharing/nearby_process_manager.h" #include "chrome/browser/nearby_sharing/nearby_process_manager.h"
#include "base/bind.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "chrome/browser/browser_process.h" #include "chrome/browser/browser_process.h"
...@@ -15,6 +16,9 @@ ...@@ -15,6 +16,9 @@
#include "chrome/browser/sharing/webrtc/sharing_mojo_service.h" #include "chrome/browser/sharing/webrtc/sharing_mojo_service.h"
#include "components/prefs/pref_service.h" #include "components/prefs/pref_service.h"
#include "content/public/browser/service_process_host.h" #include "content/public/browser/service_process_host.h"
#include "device/bluetooth/adapter.h"
#include "device/bluetooth/bluetooth_adapter_factory.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
namespace { namespace {
...@@ -161,6 +165,24 @@ void NearbyProcessManager::BindSharingProcess( ...@@ -161,6 +165,24 @@ void NearbyProcessManager::BindSharingProcess(
&NearbyProcessManager::OnNearbyProcessStopped, base::Unretained(this))); &NearbyProcessManager::OnNearbyProcessStopped, base::Unretained(this)));
} }
void NearbyProcessManager::GetBluetoothAdapter(
location::nearby::connections::mojom::NearbyConnectionsHost::
GetBluetoothAdapterCallback callback) {
DVLOG(1) << __func__
<< " Request for Bluetooth "
"adapter received on the browser process.";
if (device::BluetoothAdapterFactory::IsBluetoothSupported()) {
device::BluetoothAdapterFactory::Get()->GetAdapter(
base::BindOnce(&NearbyProcessManager::OnGetBluetoothAdapter,
base::Unretained(this), std::move(callback)));
} else {
DVLOG(1)
<< __func__
<< " Bluetooth is not supported on this device, returning NullRemote";
std::move(callback).Run(/*adapter=*/mojo::NullRemote());
}
}
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())
...@@ -206,7 +228,7 @@ void NearbyProcessManager::OnNearbyConnections( ...@@ -206,7 +228,7 @@ void NearbyProcessManager::OnNearbyConnections(
mojo::PendingReceiver<NearbyConnectionsMojom> receiver, mojo::PendingReceiver<NearbyConnectionsMojom> receiver,
mojo::PendingRemote<NearbyConnectionsMojom> remote) { mojo::PendingRemote<NearbyConnectionsMojom> remote) {
if (!mojo::FusePipes(std::move(receiver), std::move(remote))) { if (!mojo::FusePipes(std::move(receiver), std::move(remote))) {
LOG(WARNING) << "Failed to initialize Nearby Connectins process"; LOG(WARNING) << "Failed to initialize Nearby Connections process";
StopProcess(active_profile_); StopProcess(active_profile_);
return; return;
} }
...@@ -218,3 +240,14 @@ void NearbyProcessManager::OnNearbyConnections( ...@@ -218,3 +240,14 @@ void NearbyProcessManager::OnNearbyConnections(
void NearbyProcessManager::OnNearbyProcessStopped() { void NearbyProcessManager::OnNearbyProcessStopped() {
StopProcess(active_profile_); StopProcess(active_profile_);
} }
void NearbyProcessManager::OnGetBluetoothAdapter(
location::nearby::connections::mojom::NearbyConnectionsHost::
GetBluetoothAdapterCallback callback,
scoped_refptr<device::BluetoothAdapter> adapter) {
DVLOG(1) << __func__ << " Got adapter instance, returning to utility process";
mojo::PendingRemote<bluetooth::mojom::Adapter> pending_adapter;
mojo::MakeSelfOwnedReceiver(std::make_unique<bluetooth::Adapter>(adapter),
pending_adapter.InitWithNewPipeAndPassReceiver());
std::move(callback).Run(std::move(pending_adapter));
}
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "chrome/browser/profiles/profile_manager_observer.h" #include "chrome/browser/profiles/profile_manager_observer.h"
#include "chrome/services/sharing/public/mojom/nearby_connections.mojom.h" #include "chrome/services/sharing/public/mojom/nearby_connections.mojom.h"
#include "chrome/services/sharing/public/mojom/sharing.mojom.h" #include "chrome/services/sharing/public/mojom/sharing.mojom.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "mojo/public/cpp/bindings/pending_receiver.h" #include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h" #include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver.h" #include "mojo/public/cpp/bindings/receiver.h"
...@@ -111,6 +112,9 @@ class NearbyProcessManager ...@@ -111,6 +112,9 @@ class NearbyProcessManager
// process running in a sandbox. // process running in a sandbox.
void BindSharingProcess(mojo::PendingRemote<sharing::mojom::Sharing> sharing); void BindSharingProcess(mojo::PendingRemote<sharing::mojom::Sharing> sharing);
// location::nearby::connections::mojom::NearbyConnectionsHost:
void GetBluetoothAdapter(GetBluetoothAdapterCallback callback) override;
private: private:
FRIEND_TEST_ALL_PREFIXES(NearbyProcessManagerTest, AddRemoveObserver); FRIEND_TEST_ALL_PREFIXES(NearbyProcessManagerTest, AddRemoveObserver);
FRIEND_TEST_ALL_PREFIXES(NearbySharingServiceImplTest, FRIEND_TEST_ALL_PREFIXES(NearbySharingServiceImplTest,
...@@ -144,6 +148,11 @@ class NearbyProcessManager ...@@ -144,6 +148,11 @@ class NearbyProcessManager
// Observer::OnNearbyProcessStopped(). // Observer::OnNearbyProcessStopped().
void OnNearbyProcessStopped(); void OnNearbyProcessStopped();
// Called when a bluetooth adapter is acquired and we can finish
// the GetBluetoothAdapter mojo call
void OnGetBluetoothAdapter(GetBluetoothAdapterCallback callback,
scoped_refptr<device::BluetoothAdapter> adapter);
// The bound remote to a sandboxed process. // The bound remote to a sandboxed process.
mojo::Remote<sharing::mojom::Sharing> sharing_process_; mojo::Remote<sharing::mojom::Sharing> sharing_process_;
// The bound remote to the Nearby Connections library inside the sandbox. // The bound remote to the Nearby Connections library inside the sandbox.
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/test/bind_test_util.h"
#include "chrome/browser/profiles/profile_attributes_entry.h" #include "chrome/browser/profiles/profile_attributes_entry.h"
#include "chrome/services/sharing/public/mojom/nearby_connections.mojom.h" #include "chrome/services/sharing/public/mojom/nearby_connections.mojom.h"
#include "chrome/services/sharing/public/mojom/nearby_connections_types.mojom.h" #include "chrome/services/sharing/public/mojom/nearby_connections_types.mojom.h"
...@@ -20,6 +21,8 @@ ...@@ -20,6 +21,8 @@
#include "chrome/test/base/testing_profile_manager.h" #include "chrome/test/base/testing_profile_manager.h"
#include "content/public/test/browser_task_environment.h" #include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_utils.h" #include "content/public/test/test_utils.h"
#include "device/bluetooth/bluetooth_adapter_factory.h"
#include "device/bluetooth/test/mock_bluetooth_adapter.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h" #include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -215,6 +218,10 @@ TEST_F(NearbyProcessManagerTest, StartStopProcess) { ...@@ -215,6 +218,10 @@ TEST_F(NearbyProcessManagerTest, StartStopProcess) {
Profile* profile = CreateProfile("name"); Profile* profile = CreateProfile("name");
manager.SetActiveProfile(profile); manager.SetActiveProfile(profile);
// Inject fake Nearby process mojo connection.
FakeSharingMojoService fake_sharing_service;
manager.BindSharingProcess(fake_sharing_service.BindSharingService());
MockNearbyProcessManagerObserver observer; MockNearbyProcessManagerObserver observer;
base::RunLoop run_loop_started; base::RunLoop run_loop_started;
base::RunLoop run_loop_stopped; base::RunLoop run_loop_stopped;
...@@ -276,3 +283,16 @@ TEST_F(NearbyProcessManagerTest, ResetNearbyProcess) { ...@@ -276,3 +283,16 @@ TEST_F(NearbyProcessManagerTest, ResetNearbyProcess) {
manager.RemoveObserver(&observer); manager.RemoveObserver(&observer);
} }
TEST_F(NearbyProcessManagerTest, GetBluetoothAdapter) {
auto& manager = NearbyProcessManager::GetInstance();
auto adapter = base::MakeRefCounted<device::MockBluetoothAdapter>();
device::BluetoothAdapterFactory::SetAdapterForTesting(adapter);
base::RunLoop loop;
manager.GetBluetoothAdapter(base::BindLambdaForTesting(
[&](mojo::PendingRemote<::bluetooth::mojom::Adapter>
pending_remote_adapter) { loop.Quit(); }));
loop.Run();
}
include_rules = [ include_rules = [
"+jingle/glue", "+jingle/glue",
"+device/bluetooth/public/mojom",
"+services/network/public/cpp", "+services/network/public/cpp",
"+services/network/public/mojom", "+services/network/public/mojom",
"+third_party/webrtc", "+third_party/webrtc",
......
...@@ -19,6 +19,9 @@ NearbyConnections::NearbyConnections( ...@@ -19,6 +19,9 @@ NearbyConnections::NearbyConnections(
&NearbyConnections::OnDisconnect, weak_ptr_factory_.GetWeakPtr())); &NearbyConnections::OnDisconnect, weak_ptr_factory_.GetWeakPtr()));
host_.set_disconnect_handler(base::BindOnce(&NearbyConnections::OnDisconnect, host_.set_disconnect_handler(base::BindOnce(&NearbyConnections::OnDisconnect,
weak_ptr_factory_.GetWeakPtr())); weak_ptr_factory_.GetWeakPtr()));
host_->GetBluetoothAdapter(
base::BindOnce(&NearbyConnections::OnGetBluetoothAdapter,
weak_ptr_factory_.GetWeakPtr()));
} }
NearbyConnections::~NearbyConnections() = default; NearbyConnections::~NearbyConnections() = default;
...@@ -29,6 +32,26 @@ void NearbyConnections::OnDisconnect() { ...@@ -29,6 +32,26 @@ void NearbyConnections::OnDisconnect() {
// Note: |this| might be destroyed here. // Note: |this| might be destroyed here.
} }
void NearbyConnections::OnGetBluetoothAdapter(
mojo::PendingRemote<::bluetooth::mojom::Adapter> pending_remote_adapter) {
if (!pending_remote_adapter.is_valid()) {
VLOG(1) << __func__
<< " Received invalid Bluetooh adapter in utility process";
return;
}
VLOG(1) << __func__ << " Received Bluetooh adapter in utility process";
bluetooth_adapter_.Bind(std::move(pending_remote_adapter));
bluetooth_adapter_->GetInfo(
base::BindOnce([](bluetooth::mojom::AdapterInfoPtr info) {
VLOG(1) << __func__ << "Bluetooh AdapterInfo name: '" << info->name
<< "' system_name: '" << info->system_name << "' address: '"
<< info->address << "' present: " << info->present
<< " powered: " << info->powered;
}));
}
} // namespace connections } // namespace connections
} // namespace nearby } // namespace nearby
} // namespace location } // namespace location
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "base/callback_forward.h" #include "base/callback_forward.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "chrome/services/sharing/public/mojom/nearby_connections.mojom.h" #include "chrome/services/sharing/public/mojom/nearby_connections.mojom.h"
#include "device/bluetooth/public/mojom/adapter.mojom.h"
#include "mojo/public/cpp/bindings/pending_receiver.h" #include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h" #include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver.h" #include "mojo/public/cpp/bindings/receiver.h"
...@@ -29,7 +30,7 @@ class NearbyConnections : public mojom::NearbyConnections { ...@@ -29,7 +30,7 @@ class NearbyConnections : public mojom::NearbyConnections {
// Creates a new instance of the NearbyConnections library. This will allocate // Creates a new instance of the NearbyConnections library. This will allocate
// and initialize a new instance and hold on to the passed mojo pipes. // and initialize a new instance and hold on to the passed mojo pipes.
// |on_disconnect| is called when either mojo interface disconnects and should // |on_disconnect| is called when either mojo interface disconnects and should
// destroy this instamce. // destroy this instance.
NearbyConnections( NearbyConnections(
mojo::PendingReceiver<mojom::NearbyConnections> nearby_connections, mojo::PendingReceiver<mojom::NearbyConnections> nearby_connections,
mojo::PendingRemote<mojom::NearbyConnectionsHost> host, mojo::PendingRemote<mojom::NearbyConnectionsHost> host,
...@@ -40,9 +41,12 @@ class NearbyConnections : public mojom::NearbyConnections { ...@@ -40,9 +41,12 @@ class NearbyConnections : public mojom::NearbyConnections {
private: private:
void OnDisconnect(); void OnDisconnect();
void OnGetBluetoothAdapter(
mojo::PendingRemote<::bluetooth::mojom::Adapter> pending_remote_adapter);
mojo::Receiver<mojom::NearbyConnections> nearby_connections_; mojo::Receiver<mojom::NearbyConnections> nearby_connections_;
mojo::Remote<mojom::NearbyConnectionsHost> host_; mojo::Remote<mojom::NearbyConnectionsHost> host_;
mojo::Remote<bluetooth::mojom::Adapter> bluetooth_adapter_;
base::OnceClosure on_disconnect_; base::OnceClosure on_disconnect_;
base::WeakPtrFactory<NearbyConnections> weak_ptr_factory_{this}; base::WeakPtrFactory<NearbyConnections> weak_ptr_factory_{this};
......
...@@ -12,6 +12,11 @@ MockNearbyConnectionsHost::MockNearbyConnectionsHost() = default; ...@@ -12,6 +12,11 @@ MockNearbyConnectionsHost::MockNearbyConnectionsHost() = default;
MockNearbyConnectionsHost::~MockNearbyConnectionsHost() = default; MockNearbyConnectionsHost::~MockNearbyConnectionsHost() = default;
void MockNearbyConnectionsHost::GetBluetoothAdapter(
GetBluetoothAdapterCallback callback) {
std::move(callback).Run(/*adapter=*/mojo::NullRemote());
}
} // namespace connections } // namespace connections
} // namespace nearby } // namespace nearby
} // namespace location } // namespace location
...@@ -6,7 +6,6 @@ ...@@ -6,7 +6,6 @@
#define CHROME_SERVICES_SHARING_NEARBY_TEST_MOCK_NEARBY_CONNECTIONS_HOST_H_ #define CHROME_SERVICES_SHARING_NEARBY_TEST_MOCK_NEARBY_CONNECTIONS_HOST_H_
#include "chrome/services/sharing/public/mojom/nearby_connections.mojom.h" #include "chrome/services/sharing/public/mojom/nearby_connections.mojom.h"
#include "mojo/public/cpp/bindings/receiver.h" #include "mojo/public/cpp/bindings/receiver.h"
namespace location { namespace location {
...@@ -22,6 +21,9 @@ class MockNearbyConnectionsHost : public mojom::NearbyConnectionsHost { ...@@ -22,6 +21,9 @@ class MockNearbyConnectionsHost : public mojom::NearbyConnectionsHost {
delete; delete;
~MockNearbyConnectionsHost() override; ~MockNearbyConnectionsHost() override;
// mojom::NearbyConnectionsHost
void GetBluetoothAdapter(GetBluetoothAdapterCallback callback) override;
mojo::Receiver<mojom::NearbyConnectionsHost> host{this}; mojo::Receiver<mojom::NearbyConnectionsHost> host{this};
}; };
......
...@@ -9,6 +9,7 @@ mojom("mojom") { ...@@ -9,6 +9,7 @@ mojom("mojom") {
] ]
public_deps = [ public_deps = [
"//device/bluetooth/public/mojom:deprecated_experimental_interfaces",
"//mojo/public/mojom/base", "//mojo/public/mojom/base",
"//services/network/public/mojom", "//services/network/public/mojom",
"//url/mojom:url_mojom_gurl", "//url/mojom:url_mojom_gurl",
......
...@@ -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 "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
// sandboxed process. This interface is used by the browser process to connect // sandboxed process. This interface is used by the browser process to connect
...@@ -20,4 +21,14 @@ interface NearbyConnections { ...@@ -20,4 +21,14 @@ interface NearbyConnections {
// process and called from a sandboxed process typically at startup to get all // process and called from a sandboxed process typically at startup to get all
// available mediums. // available mediums.
interface NearbyConnectionsHost { interface NearbyConnectionsHost {
// Gets the current Bluetooth adapter from the host.
// There are three cases for the return value:
// 1) If Bluetooth is not supported by Chrome on the OS (see
// BluetoothAdapterFactory::IsBluetoothSupported) we return a null remote.
// 2) If Bluetooth is supported by Chrome on the OS but there is not a valid
// adapter, we return an adapter with the AdapterInfo.present=false because
// this is the way BluetoothAdapterFactory currently behaves.
// 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.
GetBluetoothAdapter() => (pending_remote<bluetooth.mojom.Adapter>? adapter);
}; };
...@@ -58,7 +58,10 @@ mojom("deprecated_experimental_interfaces") { ...@@ -58,7 +58,10 @@ mojom("deprecated_experimental_interfaces") {
# Implementation of the mojom interfaces: # Implementation of the mojom interfaces:
"//device/bluetooth:deprecated_experimental_mojo", "//device/bluetooth:deprecated_experimental_mojo",
# Single approved client of the interfaces: # Bluetooth internals page
"//chrome/browser/ui/webui/bluetooth_internals:*", "//chrome/browser/ui/webui/bluetooth_internals:*",
# Nearby Sharing feature
"//chrome/services/sharing/public/mojom:*",
] ]
} }
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