Commit 5325d37c authored by Miyoung Shin's avatar Miyoung Shin Committed by Commit Bot

Convert SerialPort to new Mojo types

This CL converts SerialPort{Ptr, Request} in services, extensions
and content to the new Mojo type, and uses
pending_receiver<SerialPort> in serial.mojom.

Bug: 955171
Change-Id: I6911df8bd057e0e7e8fa81ce00273c4cd049de6d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1810441
Commit-Queue: Miyoung Shin <myid.shin@igalia.com>
Reviewed-by: default avatarKent Tamura <tkent@chromium.org>
Reviewed-by: default avatarOksana Zhuravlova <oksamyt@chromium.org>
Reviewed-by: default avatarDominick Ng <dominickn@chromium.org>
Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#698448}
parent 3a92df7d
...@@ -69,8 +69,8 @@ TEST_F(SerialTest, OpenAndClosePort) { ...@@ -69,8 +69,8 @@ TEST_F(SerialTest, OpenAndClosePort) {
EXPECT_FALSE(contents()->IsConnectedToSerialPort()); EXPECT_FALSE(contents()->IsConnectedToSerialPort());
device::mojom::SerialPortPtr port; mojo::Remote<device::mojom::SerialPort> port;
service->GetPort(token, mojo::MakeRequest(&port)); service->GetPort(token, port.BindNewPipeAndPassReceiver());
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
EXPECT_TRUE(contents()->IsConnectedToSerialPort()); EXPECT_TRUE(contents()->IsConnectedToSerialPort());
...@@ -94,8 +94,8 @@ TEST_F(SerialTest, OpenAndNavigateCrossOrigin) { ...@@ -94,8 +94,8 @@ TEST_F(SerialTest, OpenAndNavigateCrossOrigin) {
EXPECT_FALSE(contents()->IsConnectedToSerialPort()); EXPECT_FALSE(contents()->IsConnectedToSerialPort());
device::mojom::SerialPortPtr port; mojo::Remote<device::mojom::SerialPort> port;
service->GetPort(token, mojo::MakeRequest(&port)); service->GetPort(token, port.BindNewPipeAndPassReceiver());
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
EXPECT_TRUE(contents()->IsConnectedToSerialPort()); EXPECT_TRUE(contents()->IsConnectedToSerialPort());
...@@ -103,7 +103,7 @@ TEST_F(SerialTest, OpenAndNavigateCrossOrigin) { ...@@ -103,7 +103,7 @@ TEST_F(SerialTest, OpenAndNavigateCrossOrigin) {
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
EXPECT_FALSE(contents()->IsConnectedToSerialPort()); EXPECT_FALSE(contents()->IsConnectedToSerialPort());
port.FlushForTesting(); port.FlushForTesting();
EXPECT_TRUE(port.encountered_error()); EXPECT_FALSE(port.is_connected());
} }
} // namespace content } // namespace content
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
#include "extensions/browser/api/serial/serial_port_manager.h" #include "extensions/browser/api/serial/serial_port_manager.h"
#include "extensions/common/api/serial.h" #include "extensions/common/api/serial.h"
#include "mojo/public/cpp/bindings/callback_helpers.h" #include "mojo/public/cpp/bindings/callback_helpers.h"
#include "mojo/public/cpp/bindings/interface_request.h" #include "mojo/public/cpp/bindings/pending_remote.h"
namespace extensions { namespace extensions {
...@@ -115,8 +115,8 @@ ExtensionFunction::ResponseAction SerialConnectFunction::Run() { ...@@ -115,8 +115,8 @@ ExtensionFunction::ResponseAction SerialConnectFunction::Run() {
auto* manager = SerialPortManager::Get(browser_context()); auto* manager = SerialPortManager::Get(browser_context());
DCHECK(manager); DCHECK(manager);
device::mojom::SerialPortPtr serial_port; mojo::PendingRemote<device::mojom::SerialPort> serial_port;
manager->GetPort(params->path, mojo::MakeRequest(&serial_port)); manager->GetPort(params->path, serial_port.InitWithNewPipeAndPassReceiver());
connection_ = std::make_unique<SerialConnection>(extension_->id(), connection_ = std::make_unique<SerialConnection>(extension_->id(),
std::move(serial_port)); std::move(serial_port));
......
...@@ -78,8 +78,8 @@ class FakeSerialPort : public device::mojom::SerialPort { ...@@ -78,8 +78,8 @@ class FakeSerialPort : public device::mojom::SerialPort {
const device::mojom::SerialPortInfo& info() { return *info_; } const device::mojom::SerialPortInfo& info() { return *info_; }
void Bind(device::mojom::SerialPortRequest request) { void Bind(mojo::PendingReceiver<device::mojom::SerialPort> receiver) {
bindings_.AddBinding(this, std::move(request)); receivers_.Add(this, std::move(receiver));
} }
private: private:
...@@ -266,7 +266,7 @@ class FakeSerialPort : public device::mojom::SerialPort { ...@@ -266,7 +266,7 @@ class FakeSerialPort : public device::mojom::SerialPort {
} }
device::mojom::SerialPortInfoPtr info_; device::mojom::SerialPortInfoPtr info_;
mojo::BindingSet<device::mojom::SerialPort> bindings_; mojo::ReceiverSet<device::mojom::SerialPort> receivers_;
// Currently applied connection options. // Currently applied connection options.
device::mojom::SerialConnectionOptions options_; device::mojom::SerialConnectionOptions options_;
...@@ -305,13 +305,13 @@ class FakeSerialPortManager : public device::mojom::SerialPortManager { ...@@ -305,13 +305,13 @@ class FakeSerialPortManager : public device::mojom::SerialPortManager {
} }
void GetPort(const base::UnguessableToken& token, void GetPort(const base::UnguessableToken& token,
device::mojom::SerialPortRequest request, mojo::PendingReceiver<device::mojom::SerialPort> receiver,
mojo::PendingRemote<device::mojom::SerialPortConnectionWatcher> mojo::PendingRemote<device::mojom::SerialPortConnectionWatcher>
watcher) override { watcher) override {
DCHECK(!watcher); DCHECK(!watcher);
auto it = ports_.find(token); auto it = ports_.find(token);
DCHECK(it != ports_.end()); DCHECK(it != ports_.end());
it->second->Bind(std::move(request)); it->second->Bind(std::move(receiver));
} }
void AddPort(const base::FilePath& path) { void AddPort(const base::FilePath& path) {
......
...@@ -159,8 +159,9 @@ ApiResourceManager<SerialConnection>::GetFactoryInstance() { ...@@ -159,8 +159,9 @@ ApiResourceManager<SerialConnection>::GetFactoryInstance() {
return g_factory.Pointer(); return g_factory.Pointer();
} }
SerialConnection::SerialConnection(const std::string& owner_extension_id, SerialConnection::SerialConnection(
device::mojom::SerialPortPtr serial_port) const std::string& owner_extension_id,
mojo::PendingRemote<device::mojom::SerialPort> serial_port)
: ApiResource(owner_extension_id), : ApiResource(owner_extension_id),
persistent_(false), persistent_(false),
buffer_size_(kDefaultBufferSize), buffer_size_(kDefaultBufferSize),
...@@ -174,7 +175,7 @@ SerialConnection::SerialConnection(const std::string& owner_extension_id, ...@@ -174,7 +175,7 @@ SerialConnection::SerialConnection(const std::string& owner_extension_id,
mojo::SimpleWatcher::ArmingPolicy::MANUAL), mojo::SimpleWatcher::ArmingPolicy::MANUAL),
send_pipe_watcher_(FROM_HERE, mojo::SimpleWatcher::ArmingPolicy::MANUAL) { send_pipe_watcher_(FROM_HERE, mojo::SimpleWatcher::ArmingPolicy::MANUAL) {
DCHECK(serial_port_); DCHECK(serial_port_);
serial_port_.set_connection_error_handler(base::BindOnce( serial_port_.set_disconnect_handler(base::BindOnce(
&SerialConnection::OnConnectionError, base::Unretained(this))); &SerialConnection::OnConnectionError, base::Unretained(this)));
} }
...@@ -220,7 +221,7 @@ void SerialConnection::SetPaused(bool paused) { ...@@ -220,7 +221,7 @@ void SerialConnection::SetPaused(bool paused) {
void SerialConnection::SetConnectionErrorHandler( void SerialConnection::SetConnectionErrorHandler(
base::OnceClosure connection_error_handler) { base::OnceClosure connection_error_handler) {
if (serial_port_.encountered_error()) { if (!serial_port_.is_connected()) {
// Already being disconnected, run client's error handler immediatelly. // Already being disconnected, run client's error handler immediatelly.
std::move(connection_error_handler).Run(); std::move(connection_error_handler).Run();
return; return;
......
...@@ -19,7 +19,9 @@ ...@@ -19,7 +19,9 @@
#include "extensions/browser/api/api_resource_manager.h" #include "extensions/browser/api/api_resource_manager.h"
#include "extensions/common/api/serial.h" #include "extensions/common/api/serial.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/receiver.h" #include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/system/data_pipe.h" #include "mojo/public/cpp/system/data_pipe.h"
#include "mojo/public/cpp/system/simple_watcher.h" #include "mojo/public/cpp/system/simple_watcher.h"
#include "net/base/io_buffer.h" #include "net/base/io_buffer.h"
...@@ -66,7 +68,7 @@ class SerialConnection : public ApiResource, ...@@ -66,7 +68,7 @@ class SerialConnection : public ApiResource,
device::mojom::SerialPort::SetControlSignalsCallback; device::mojom::SerialPort::SetControlSignalsCallback;
SerialConnection(const std::string& owner_extension_id, SerialConnection(const std::string& owner_extension_id,
device::mojom::SerialPortPtr serial_port); mojo::PendingRemote<device::mojom::SerialPort> serial_port);
~SerialConnection() override; ~SerialConnection() override;
// ApiResource override. // ApiResource override.
...@@ -220,8 +222,8 @@ class SerialConnection : public ApiResource, ...@@ -220,8 +222,8 @@ class SerialConnection : public ApiResource,
// Send(). // Send().
base::CancelableClosure send_timeout_task_; base::CancelableClosure send_timeout_task_;
// Mojo interface ptr corresponding with remote asynchronous I/O handler. // Mojo interface remote corresponding with remote asynchronous I/O handler.
device::mojom::SerialPortPtr serial_port_; mojo::Remote<device::mojom::SerialPort> serial_port_;
// Pipe for read. // Pipe for read.
mojo::ScopedDataPipeConsumerHandle receive_pipe_; mojo::ScopedDataPipeConsumerHandle receive_pipe_;
......
...@@ -75,13 +75,14 @@ void SerialPortManager::GetDevices( ...@@ -75,13 +75,14 @@ void SerialPortManager::GetDevices(
port_manager_->GetDevices(std::move(callback)); port_manager_->GetDevices(std::move(callback));
} }
void SerialPortManager::GetPort(const std::string& path, void SerialPortManager::GetPort(
device::mojom::SerialPortRequest request) { const std::string& path,
mojo::PendingReceiver<device::mojom::SerialPort> receiver) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
EnsureConnection(); EnsureConnection();
port_manager_->GetDevices( port_manager_->GetDevices(
base::BindOnce(&SerialPortManager::OnGotDevicesToGetPort, base::BindOnce(&SerialPortManager::OnGotDevicesToGetPort,
weak_factory_.GetWeakPtr(), path, std::move(request))); weak_factory_.GetWeakPtr(), path, std::move(receiver)));
} }
void SerialPortManager::StartConnectionPolling(const std::string& extension_id, void SerialPortManager::StartConnectionPolling(const std::string& extension_id,
...@@ -168,13 +169,13 @@ void SerialPortManager::EnsureConnection() { ...@@ -168,13 +169,13 @@ void SerialPortManager::EnsureConnection() {
void SerialPortManager::OnGotDevicesToGetPort( void SerialPortManager::OnGotDevicesToGetPort(
const std::string& path, const std::string& path,
device::mojom::SerialPortRequest request, mojo::PendingReceiver<device::mojom::SerialPort> receiver,
std::vector<device::mojom::SerialPortInfoPtr> devices) { std::vector<device::mojom::SerialPortInfoPtr> devices) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
for (auto& device : devices) { for (auto& device : devices) {
if (device->path.AsUTF8Unsafe() == path) { if (device->path.AsUTF8Unsafe() == path) {
port_manager_->GetPort(device->token, std::move(request), port_manager_->GetPort(device->token, std::move(receiver),
/*watcher=*/mojo::NullRemote()); /*watcher=*/mojo::NullRemote());
return; return;
} }
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include "extensions/browser/api/api_resource_manager.h" #include "extensions/browser/api/api_resource_manager.h"
#include "extensions/browser/browser_context_keyed_api_factory.h" #include "extensions/browser/browser_context_keyed_api_factory.h"
#include "extensions/common/api/serial.h" #include "extensions/common/api/serial.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/remote.h" #include "mojo/public/cpp/bindings/remote.h"
#include "services/device/public/mojom/serial.mojom.h" #include "services/device/public/mojom/serial.mojom.h"
...@@ -42,7 +43,7 @@ class SerialPortManager : public BrowserContextKeyedAPI { ...@@ -42,7 +43,7 @@ class SerialPortManager : public BrowserContextKeyedAPI {
device::mojom::SerialPortManager::GetDevicesCallback callback); device::mojom::SerialPortManager::GetDevicesCallback callback);
void GetPort(const std::string& path, void GetPort(const std::string& path,
device::mojom::SerialPortRequest request); mojo::PendingReceiver<device::mojom::SerialPort> receiver);
// Start the poilling process for the connection. // Start the poilling process for the connection.
void StartConnectionPolling(const std::string& extension_id, void StartConnectionPolling(const std::string& extension_id,
...@@ -77,7 +78,7 @@ class SerialPortManager : public BrowserContextKeyedAPI { ...@@ -77,7 +78,7 @@ class SerialPortManager : public BrowserContextKeyedAPI {
void EnsureConnection(); void EnsureConnection();
void OnGotDevicesToGetPort( void OnGotDevicesToGetPort(
const std::string& path, const std::string& path,
device::mojom::SerialPortRequest request, mojo::PendingReceiver<device::mojom::SerialPort> receiver,
std::vector<device::mojom::SerialPortInfoPtr> devices); std::vector<device::mojom::SerialPortInfoPtr> devices);
void OnPortManagerConnectionError(); void OnPortManagerConnectionError();
......
...@@ -158,7 +158,7 @@ ViscaWebcam::ViscaWebcam() = default; ...@@ -158,7 +158,7 @@ ViscaWebcam::ViscaWebcam() = default;
ViscaWebcam::~ViscaWebcam() = default; ViscaWebcam::~ViscaWebcam() = default;
void ViscaWebcam::Open(const std::string& extension_id, void ViscaWebcam::Open(const std::string& extension_id,
device::mojom::SerialPortPtr port_ptr, mojo::PendingRemote<device::mojom::SerialPort> port,
const OpenCompleteCallback& open_callback) { const OpenCompleteCallback& open_callback) {
api::serial::ConnectionOptions options; api::serial::ConnectionOptions options;
...@@ -174,8 +174,8 @@ void ViscaWebcam::Open(const std::string& extension_id, ...@@ -174,8 +174,8 @@ void ViscaWebcam::Open(const std::string& extension_id,
options.parity_bit = api::serial::PARITY_BIT_NO; options.parity_bit = api::serial::PARITY_BIT_NO;
options.stop_bits = api::serial::STOP_BITS_ONE; options.stop_bits = api::serial::STOP_BITS_ONE;
serial_connection_.reset( serial_connection_ =
new SerialConnection(extension_id, std::move(port_ptr))); std::make_unique<SerialConnection>(extension_id, std::move(port));
serial_connection_->Open( serial_connection_->Open(
options, base::BindOnce(&ViscaWebcam::OnConnected, base::Unretained(this), options, base::BindOnce(&ViscaWebcam::OnConnected, base::Unretained(this),
open_callback)); open_callback));
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include "extensions/browser/api/serial/serial_connection.h" #include "extensions/browser/api/serial/serial_connection.h"
#include "extensions/browser/api/webcam_private/webcam.h" #include "extensions/browser/api/webcam_private/webcam.h"
#include "extensions/common/api/serial.h" #include "extensions/common/api/serial.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "services/device/public/mojom/serial.mojom.h" #include "services/device/public/mojom/serial.mojom.h"
namespace extensions { namespace extensions {
...@@ -32,7 +33,7 @@ class ViscaWebcam : public Webcam { ...@@ -32,7 +33,7 @@ class ViscaWebcam : public Webcam {
// command buffer. After these three steps completes, |open_callback| will be // command buffer. After these three steps completes, |open_callback| will be
// called. // called.
void Open(const std::string& extension_id, void Open(const std::string& extension_id,
device::mojom::SerialPortPtr port_ptr, mojo::PendingRemote<device::mojom::SerialPort> port,
const OpenCompleteCallback& open_callback); const OpenCompleteCallback& open_callback);
private: private:
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
#include "base/macros.h" #include "base/macros.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "content/public/test/browser_task_environment.h" #include "content/public/test/browser_task_environment.h"
#include "mojo/public/cpp/bindings/interface_request.h" #include "mojo/public/cpp/bindings/pending_remote.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
namespace extensions { namespace extensions {
...@@ -19,8 +19,9 @@ namespace { ...@@ -19,8 +19,9 @@ namespace {
class TestSerialConnection : public SerialConnection { class TestSerialConnection : public SerialConnection {
public: public:
explicit TestSerialConnection(device::mojom::SerialPortPtr port_ptr) explicit TestSerialConnection(
: SerialConnection("dummy_id", std::move(port_ptr)) {} mojo::PendingRemote<device::mojom::SerialPort> port)
: SerialConnection("dummy_id", std::move(port)) {}
~TestSerialConnection() override {} ~TestSerialConnection() override {}
void SetReceiveBuffer(const std::vector<uint8_t>& receive_buffer) { void SetReceiveBuffer(const std::vector<uint8_t>& receive_buffer) {
...@@ -103,11 +104,11 @@ std::vector<uint8_t> ToByteVector(const char (&array)[N]) { ...@@ -103,11 +104,11 @@ std::vector<uint8_t> ToByteVector(const char (&array)[N]) {
class ViscaWebcamTest : public testing::Test { class ViscaWebcamTest : public testing::Test {
protected: protected:
ViscaWebcamTest() { ViscaWebcamTest() {
device::mojom::SerialPortPtr port_ptr; mojo::PendingRemote<device::mojom::SerialPort> port;
mojo::MakeRequest(&port_ptr); ignore_result(port.InitWithNewPipeAndPassReceiver());
webcam_ = new ViscaWebcam; webcam_ = new ViscaWebcam;
webcam_->OpenForTesting( webcam_->OpenForTesting(
std::make_unique<TestSerialConnection>(std::move(port_ptr))); std::make_unique<TestSerialConnection>(std::move(port)));
} }
~ViscaWebcamTest() override {} ~ViscaWebcamTest() override {}
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include "extensions/browser/process_manager.h" #include "extensions/browser/process_manager.h"
#include "extensions/browser/process_manager_factory.h" #include "extensions/browser/process_manager_factory.h"
#include "extensions/common/api/webcam_private.h" #include "extensions/common/api/webcam_private.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "url/origin.h" #include "url/origin.h"
namespace webcam_private = extensions::api::webcam_private; namespace webcam_private = extensions::api::webcam_private;
...@@ -79,13 +80,13 @@ bool WebcamPrivateAPI::OpenSerialWebcam( ...@@ -79,13 +80,13 @@ bool WebcamPrivateAPI::OpenSerialWebcam(
return false; return false;
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
device::mojom::SerialPortPtr port_ptr; mojo::PendingRemote<device::mojom::SerialPort> port;
auto* port_manager = api::SerialPortManager::Get(browser_context_); auto* port_manager = api::SerialPortManager::Get(browser_context_);
DCHECK(port_manager); DCHECK(port_manager);
port_manager->GetPort(device_path, mojo::MakeRequest(&port_ptr)); port_manager->GetPort(device_path, port.InitWithNewPipeAndPassReceiver());
auto visca_webcam = base::MakeRefCounted<ViscaWebcam>(); auto visca_webcam = base::MakeRefCounted<ViscaWebcam>();
visca_webcam->Open(extension_id, std::move(port_ptr), visca_webcam->Open(extension_id, std::move(port),
base::Bind(&WebcamPrivateAPI::OnOpenSerialWebcam, base::Bind(&WebcamPrivateAPI::OnOpenSerialWebcam,
weak_ptr_factory_.GetWeakPtr(), extension_id, weak_ptr_factory_.GetWeakPtr(), extension_id,
device_path, visca_webcam, callback)); device_path, visca_webcam, callback));
......
...@@ -8,8 +8,8 @@ ...@@ -8,8 +8,8 @@
#include <vector> #include <vector>
#include "base/callback.h" #include "base/callback.h"
#include "mojo/public/cpp/bindings/binding.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/remote.h" #include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/system/data_pipe.h" #include "mojo/public/cpp/system/data_pipe.h"
...@@ -20,10 +20,10 @@ namespace { ...@@ -20,10 +20,10 @@ namespace {
class FakeSerialPort : public mojom::SerialPort { class FakeSerialPort : public mojom::SerialPort {
public: public:
FakeSerialPort( FakeSerialPort(
mojom::SerialPortRequest request, mojo::PendingReceiver<mojom::SerialPort> receiver,
mojo::PendingRemote<mojom::SerialPortConnectionWatcher> watcher) mojo::PendingRemote<mojom::SerialPortConnectionWatcher> watcher)
: binding_(this, std::move(request)), watcher_(std::move(watcher)) { : receiver_(this, std::move(receiver)), watcher_(std::move(watcher)) {
binding_.set_connection_error_handler(base::BindOnce( receiver_.set_disconnect_handler(base::BindOnce(
[](FakeSerialPort* self) { delete self; }, base::Unretained(this))); [](FakeSerialPort* self) { delete self; }, base::Unretained(this)));
watcher_.set_disconnect_handler(base::BindOnce( watcher_.set_disconnect_handler(base::BindOnce(
[](FakeSerialPort* self) { delete self; }, base::Unretained(this))); [](FakeSerialPort* self) { delete self; }, base::Unretained(this)));
...@@ -72,7 +72,7 @@ class FakeSerialPort : public mojom::SerialPort { ...@@ -72,7 +72,7 @@ class FakeSerialPort : public mojom::SerialPort {
void Close(CloseCallback callback) override { std::move(callback).Run(); } void Close(CloseCallback callback) override { std::move(callback).Run(); }
private: private:
mojo::Binding<mojom::SerialPort> binding_; mojo::Receiver<mojom::SerialPort> receiver_;
mojo::Remote<mojom::SerialPortConnectionWatcher> watcher_; mojo::Remote<mojom::SerialPortConnectionWatcher> watcher_;
// Mojo handles to keep open in order to simulate an active connection. // Mojo handles to keep open in order to simulate an active connection.
...@@ -108,11 +108,11 @@ void FakeSerialPortManager::GetDevices(GetDevicesCallback callback) { ...@@ -108,11 +108,11 @@ void FakeSerialPortManager::GetDevices(GetDevicesCallback callback) {
void FakeSerialPortManager::GetPort( void FakeSerialPortManager::GetPort(
const base::UnguessableToken& token, const base::UnguessableToken& token,
mojom::SerialPortRequest request, mojo::PendingReceiver<mojom::SerialPort> receiver,
mojo::PendingRemote<mojom::SerialPortConnectionWatcher> watcher) { mojo::PendingRemote<mojom::SerialPortConnectionWatcher> watcher) {
// The new FakeSerialPort instance is owned by the |request| and |watcher| // The new FakeSerialPort instance is owned by the |receiver| and |watcher|
// pipes. // pipes.
new FakeSerialPort(std::move(request), std::move(watcher)); new FakeSerialPort(std::move(receiver), std::move(watcher));
} }
} // namespace device } // namespace device
...@@ -28,7 +28,7 @@ class FakeSerialPortManager : public mojom::SerialPortManager { ...@@ -28,7 +28,7 @@ class FakeSerialPortManager : public mojom::SerialPortManager {
void GetDevices(GetDevicesCallback callback) override; void GetDevices(GetDevicesCallback callback) override;
void GetPort( void GetPort(
const base::UnguessableToken& token, const base::UnguessableToken& token,
mojom::SerialPortRequest request, mojo::PendingReceiver<mojom::SerialPort> receiver,
mojo::PendingRemote<mojom::SerialPortConnectionWatcher> watcher) override; mojo::PendingRemote<mojom::SerialPortConnectionWatcher> watcher) override;
private: private:
......
...@@ -95,10 +95,10 @@ interface SerialPortManager { ...@@ -95,10 +95,10 @@ interface SerialPortManager {
GetDevices() => (array<SerialPortInfo> devices); GetDevices() => (array<SerialPortInfo> devices);
// Creates a SerialPort instance attached to the port represented by |token|. // Creates a SerialPort instance attached to the port represented by |token|.
// When the pipe passed in |port_request| is closed the optional pipe passed // When the pipe passed in |port_receiver| is closed the optional pipe passed
// in |watcher| will also be closed. // in |watcher| will also be closed.
GetPort(mojo_base.mojom.UnguessableToken token, GetPort(mojo_base.mojom.UnguessableToken token,
SerialPort& port_request, pending_receiver<SerialPort> port_receiver,
pending_remote<SerialPortConnectionWatcher>? watcher); pending_remote<SerialPortConnectionWatcher>? watcher);
}; };
......
...@@ -19,26 +19,26 @@ namespace device { ...@@ -19,26 +19,26 @@ namespace device {
// static // static
void SerialPortImpl::Create( void SerialPortImpl::Create(
const base::FilePath& path, const base::FilePath& path,
mojom::SerialPortRequest request, mojo::PendingReceiver<mojom::SerialPort> receiver,
mojo::PendingRemote<mojom::SerialPortConnectionWatcher> watcher, mojo::PendingRemote<mojom::SerialPortConnectionWatcher> watcher,
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) { scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) {
// This SerialPortImpl is owned by |request| and |watcher|. // This SerialPortImpl is owned by |receiver| and |watcher|.
new SerialPortImpl(path, std::move(request), std::move(watcher), new SerialPortImpl(path, std::move(receiver), std::move(watcher),
std::move(ui_task_runner)); std::move(ui_task_runner));
} }
SerialPortImpl::SerialPortImpl( SerialPortImpl::SerialPortImpl(
const base::FilePath& path, const base::FilePath& path,
mojom::SerialPortRequest request, mojo::PendingReceiver<mojom::SerialPort> receiver,
mojo::PendingRemote<mojom::SerialPortConnectionWatcher> watcher, mojo::PendingRemote<mojom::SerialPortConnectionWatcher> watcher,
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner)
: binding_(this, std::move(request)), : receiver_(this, std::move(receiver)),
io_handler_(device::SerialIoHandler::Create(path, ui_task_runner)), io_handler_(device::SerialIoHandler::Create(path, ui_task_runner)),
watcher_(std::move(watcher)), watcher_(std::move(watcher)),
in_stream_watcher_(FROM_HERE, mojo::SimpleWatcher::ArmingPolicy::MANUAL), in_stream_watcher_(FROM_HERE, mojo::SimpleWatcher::ArmingPolicy::MANUAL),
out_stream_watcher_(FROM_HERE, out_stream_watcher_(FROM_HERE,
mojo::SimpleWatcher::ArmingPolicy::MANUAL) { mojo::SimpleWatcher::ArmingPolicy::MANUAL) {
binding_.set_connection_error_handler(base::BindOnce( receiver_.set_disconnect_handler(base::BindOnce(
[](SerialPortImpl* self) { delete self; }, base::Unretained(this))); [](SerialPortImpl* self) { delete self; }, base::Unretained(this)));
if (watcher_.is_bound()) { if (watcher_.is_bound()) {
watcher_.set_disconnect_handler(base::BindOnce( watcher_.set_disconnect_handler(base::BindOnce(
......
...@@ -11,8 +11,9 @@ ...@@ -11,8 +11,9 @@
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "mojo/public/cpp/bindings/binding.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/remote.h" #include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/system/data_pipe.h" #include "mojo/public/cpp/system/data_pipe.h"
#include "mojo/public/cpp/system/simple_watcher.h" #include "mojo/public/cpp/system/simple_watcher.h"
...@@ -34,14 +35,14 @@ class SerialPortImpl : public mojom::SerialPort { ...@@ -34,14 +35,14 @@ class SerialPortImpl : public mojom::SerialPort {
public: public:
static void Create( static void Create(
const base::FilePath& path, const base::FilePath& path,
mojom::SerialPortRequest request, mojo::PendingReceiver<mojom::SerialPort> receiver,
mojo::PendingRemote<mojom::SerialPortConnectionWatcher> watcher, mojo::PendingRemote<mojom::SerialPortConnectionWatcher> watcher,
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner); scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner);
private: private:
SerialPortImpl( SerialPortImpl(
const base::FilePath& path, const base::FilePath& path,
mojom::SerialPortRequest request, mojo::PendingReceiver<mojom::SerialPort> receiver,
mojo::PendingRemote<mojom::SerialPortConnectionWatcher> watcher, mojo::PendingRemote<mojom::SerialPortConnectionWatcher> watcher,
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner); scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner);
~SerialPortImpl() override; ~SerialPortImpl() override;
...@@ -72,7 +73,7 @@ class SerialPortImpl : public mojom::SerialPort { ...@@ -72,7 +73,7 @@ class SerialPortImpl : public mojom::SerialPort {
const mojo::HandleSignalsState& state); const mojo::HandleSignalsState& state);
void WriteToOutStream(uint32_t bytes_read, mojom::SerialReceiveError error); void WriteToOutStream(uint32_t bytes_read, mojom::SerialReceiveError error);
mojo::Binding<mojom::SerialPort> binding_; mojo::Receiver<mojom::SerialPort> receiver_;
// Underlying connection to the serial port. // Underlying connection to the serial port.
scoped_refptr<SerialIoHandler> io_handler_; scoped_refptr<SerialIoHandler> io_handler_;
......
...@@ -5,8 +5,8 @@ ...@@ -5,8 +5,8 @@
#include "services/device/serial/serial_port_impl.h" #include "services/device/serial/serial_port_impl.h"
#include "base/macros.h" #include "base/macros.h"
#include "mojo/public/cpp/bindings/interface_request.h"
#include "mojo/public/cpp/bindings/pending_remote.h" #include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h" #include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "services/device/device_service_test_base.h" #include "services/device/device_service_test_base.h"
#include "services/device/public/mojom/serial.mojom.h" #include "services/device/public/mojom/serial.mojom.h"
...@@ -25,19 +25,19 @@ class SerialPortImplTest : public DeviceServiceTestBase { ...@@ -25,19 +25,19 @@ class SerialPortImplTest : public DeviceServiceTestBase {
}; };
TEST_F(SerialPortImplTest, WatcherClosedWhenPortClosed) { TEST_F(SerialPortImplTest, WatcherClosedWhenPortClosed) {
mojom::SerialPortPtr serial_port; mojo::Remote<mojom::SerialPort> serial_port;
mojo::PendingRemote<mojom::SerialPortConnectionWatcher> watcher; mojo::PendingRemote<mojom::SerialPortConnectionWatcher> watcher;
auto watcher_receiver = mojo::MakeSelfOwnedReceiver( auto watcher_receiver = mojo::MakeSelfOwnedReceiver(
std::make_unique<mojom::SerialPortConnectionWatcher>(), std::make_unique<mojom::SerialPortConnectionWatcher>(),
watcher.InitWithNewPipeAndPassReceiver()); watcher.InitWithNewPipeAndPassReceiver());
SerialPortImpl::Create(base::FilePath(), mojo::MakeRequest(&serial_port), SerialPortImpl::Create(
std::move(watcher), base::FilePath(), serial_port.BindNewPipeAndPassReceiver(),
base::ThreadTaskRunnerHandle::Get()); std::move(watcher), base::ThreadTaskRunnerHandle::Get());
// To start with both the serial port connection and the connection watcher // To start with both the serial port connection and the connection watcher
// connection should remain open. // connection should remain open.
serial_port.FlushForTesting(); serial_port.FlushForTesting();
EXPECT_FALSE(serial_port.encountered_error()); EXPECT_TRUE(serial_port.is_connected());
watcher_receiver->FlushForTesting(); watcher_receiver->FlushForTesting();
EXPECT_TRUE(watcher_receiver); EXPECT_TRUE(watcher_receiver);
...@@ -49,19 +49,19 @@ TEST_F(SerialPortImplTest, WatcherClosedWhenPortClosed) { ...@@ -49,19 +49,19 @@ TEST_F(SerialPortImplTest, WatcherClosedWhenPortClosed) {
} }
TEST_F(SerialPortImplTest, PortClosedWhenWatcherClosed) { TEST_F(SerialPortImplTest, PortClosedWhenWatcherClosed) {
mojom::SerialPortPtr serial_port; mojo::Remote<mojom::SerialPort> serial_port;
mojo::PendingRemote<mojom::SerialPortConnectionWatcher> watcher; mojo::PendingRemote<mojom::SerialPortConnectionWatcher> watcher;
auto watcher_receiver = mojo::MakeSelfOwnedReceiver( auto watcher_receiver = mojo::MakeSelfOwnedReceiver(
std::make_unique<mojom::SerialPortConnectionWatcher>(), std::make_unique<mojom::SerialPortConnectionWatcher>(),
watcher.InitWithNewPipeAndPassReceiver()); watcher.InitWithNewPipeAndPassReceiver());
SerialPortImpl::Create(base::FilePath(), mojo::MakeRequest(&serial_port), SerialPortImpl::Create(
std::move(watcher), base::FilePath(), serial_port.BindNewPipeAndPassReceiver(),
base::ThreadTaskRunnerHandle::Get()); std::move(watcher), base::ThreadTaskRunnerHandle::Get());
// To start with both the serial port connection and the connection watcher // To start with both the serial port connection and the connection watcher
// connection should remain open. // connection should remain open.
serial_port.FlushForTesting(); serial_port.FlushForTesting();
EXPECT_FALSE(serial_port.encountered_error()); EXPECT_TRUE(serial_port.is_connected());
watcher_receiver->FlushForTesting(); watcher_receiver->FlushForTesting();
EXPECT_TRUE(watcher_receiver); EXPECT_TRUE(watcher_receiver);
...@@ -69,7 +69,7 @@ TEST_F(SerialPortImplTest, PortClosedWhenWatcherClosed) { ...@@ -69,7 +69,7 @@ TEST_F(SerialPortImplTest, PortClosedWhenWatcherClosed) {
// connection should also be closed. // connection should also be closed.
watcher_receiver->Close(); watcher_receiver->Close();
serial_port.FlushForTesting(); serial_port.FlushForTesting();
EXPECT_TRUE(serial_port.encountered_error()); EXPECT_FALSE(serial_port.is_connected());
} }
} // namespace } // namespace
......
...@@ -42,7 +42,7 @@ void SerialPortManagerImpl::GetDevices(GetDevicesCallback callback) { ...@@ -42,7 +42,7 @@ void SerialPortManagerImpl::GetDevices(GetDevicesCallback callback) {
void SerialPortManagerImpl::GetPort( void SerialPortManagerImpl::GetPort(
const base::UnguessableToken& token, const base::UnguessableToken& token,
mojom::SerialPortRequest request, mojo::PendingReceiver<mojom::SerialPort> receiver,
mojo::PendingRemote<mojom::SerialPortConnectionWatcher> watcher) { mojo::PendingRemote<mojom::SerialPortConnectionWatcher> watcher) {
if (!enumerator_) if (!enumerator_)
enumerator_ = SerialDeviceEnumerator::Create(); enumerator_ = SerialDeviceEnumerator::Create();
...@@ -50,7 +50,7 @@ void SerialPortManagerImpl::GetPort( ...@@ -50,7 +50,7 @@ void SerialPortManagerImpl::GetPort(
if (path) { if (path) {
io_task_runner_->PostTask( io_task_runner_->PostTask(
FROM_HERE, FROM_HERE,
base::BindOnce(&SerialPortImpl::Create, *path, std::move(request), base::BindOnce(&SerialPortImpl::Create, *path, std::move(receiver),
std::move(watcher), ui_task_runner_)); std::move(watcher), ui_task_runner_));
} }
} }
......
...@@ -42,7 +42,7 @@ class SerialPortManagerImpl : public mojom::SerialPortManager { ...@@ -42,7 +42,7 @@ class SerialPortManagerImpl : public mojom::SerialPortManager {
void GetDevices(GetDevicesCallback callback) override; void GetDevices(GetDevicesCallback callback) override;
void GetPort( void GetPort(
const base::UnguessableToken& token, const base::UnguessableToken& token,
mojom::SerialPortRequest request, mojo::PendingReceiver<mojom::SerialPort> receiver,
mojo::PendingRemote<mojom::SerialPortConnectionWatcher> watcher) override; mojo::PendingRemote<mojom::SerialPortConnectionWatcher> watcher) override;
std::unique_ptr<SerialDeviceEnumerator> enumerator_; std::unique_ptr<SerialDeviceEnumerator> enumerator_;
......
...@@ -78,13 +78,14 @@ TEST_F(SerialPortManagerImplTest, SimpleConnectTest) { ...@@ -78,13 +78,14 @@ TEST_F(SerialPortManagerImplTest, SimpleConnectTest) {
port_manager->GetDevices(base::BindLambdaForTesting( port_manager->GetDevices(base::BindLambdaForTesting(
[&](std::vector<mojom::SerialPortInfoPtr> results) { [&](std::vector<mojom::SerialPortInfoPtr> results) {
for (auto& device : results) { for (auto& device : results) {
mojom::SerialPortPtr serial_port; mojo::Remote<mojom::SerialPort> serial_port;
port_manager->GetPort(device->token, mojo::MakeRequest(&serial_port), port_manager->GetPort(device->token,
serial_port.BindNewPipeAndPassReceiver(),
/*watcher=*/mojo::NullRemote()); /*watcher=*/mojo::NullRemote());
// Send a message on the pipe and wait for the response to make sure // Send a message on the pipe and wait for the response to make sure
// that the interface request was bound successfully. // that the interface request was bound successfully.
serial_port.FlushForTesting(); serial_port.FlushForTesting();
EXPECT_FALSE(serial_port.encountered_error()); EXPECT_TRUE(serial_port.is_connected());
} }
loop.Quit(); loop.Quit();
})); }));
...@@ -119,14 +120,14 @@ TEST_F(SerialPortManagerImplTest, GetPort) { ...@@ -119,14 +120,14 @@ TEST_F(SerialPortManagerImplTest, GetPort) {
[&](std::vector<mojom::SerialPortInfoPtr> results) { [&](std::vector<mojom::SerialPortInfoPtr> results) {
EXPECT_GT(results.size(), 0u); EXPECT_GT(results.size(), 0u);
mojom::SerialPortPtr serial_port; mojo::Remote<mojom::SerialPort> serial_port;
port_manager->GetPort(results[0]->token, port_manager->GetPort(results[0]->token,
mojo::MakeRequest(&serial_port), serial_port.BindNewPipeAndPassReceiver(),
/*watcher=*/mojo::NullRemote()); /*watcher=*/mojo::NullRemote());
// Send a message on the pipe and wait for the response to make sure // Send a message on the pipe and wait for the response to make sure
// that the interface request was bound successfully. // that the interface request was bound successfully.
serial_port.FlushForTesting(); serial_port.FlushForTesting();
EXPECT_FALSE(serial_port.encountered_error()); EXPECT_TRUE(serial_port.is_connected());
loop.Quit(); loop.Quit();
})); }));
loop.Run(); loop.Run();
......
...@@ -36,5 +36,5 @@ interface SerialService { ...@@ -36,5 +36,5 @@ interface SerialService {
// Connects an instance of the SerialPort interface attached to the serial // Connects an instance of the SerialPort interface attached to the serial
// port identified by |token|. // port identified by |token|.
GetPort(mojo_base.mojom.UnguessableToken token, GetPort(mojo_base.mojom.UnguessableToken token,
pending_receiver<device.mojom.SerialPort> port_request); pending_receiver<device.mojom.SerialPort> port_receiver);
}; };
...@@ -328,12 +328,12 @@ class FakeSerialService { ...@@ -328,12 +328,12 @@ class FakeSerialService {
return { port: null }; return { port: null };
} }
async getPort(token, port_request) { async getPort(token, port_receiver) {
let record = this.ports_.get(token.low); let record = this.ports_.get(token.low);
if (record !== undefined) { if (record !== undefined) {
record.fakePort.bind(port_request); record.fakePort.bind(port_receiver);
} else { } else {
port_request.close(); port_receiver.close();
} }
} }
} }
......
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