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