Commit a4d9f90b authored by Chris Hamilton's avatar Chris Hamilton Committed by Commit Bot

Move content::MessagePort to blink/common/public/messaging.

This moves the recently created content::MessagePort concept
to blink. This is required because some intended downstream
clients of this class can not take a dependency on content, but
already have a dependency on a blink.

BUG=803242

Change-Id: I177bf5e3c53ca550783a0aa9f1f6207551d87e74
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2023470Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Commit-Queue: Chris Hamilton <chrisha@chromium.org>
Cr-Commit-Position: refs/heads/master@{#735605}
parent 9e383162
......@@ -1211,7 +1211,6 @@ jumbo_source_set("browser") {
"memory/swap_metrics_driver_impl_mac.cc",
"memory/swap_metrics_driver_impl_mac.h",
"memory/swap_metrics_driver_impl_win.cc",
"message_port.cc",
"message_port_provider.cc",
"mime_registry_impl.cc",
"mime_registry_impl.h",
......
......@@ -14,13 +14,9 @@ if (is_android) {
# See //content/BUILD.gn for how this works.
group("browser") {
if (is_component_build) {
public_deps = [
"//content",
]
public_deps = [ "//content" ]
} else {
public_deps = [
":browser_sources",
]
public_deps = [ ":browser_sources" ]
}
}
......@@ -211,7 +207,6 @@ jumbo_source_set("browser_sources") {
"media_session_service.h",
"media_stream_request.cc",
"media_stream_request.h",
"message_port.h",
"message_port_provider.h",
"mhtml_extra_parts.h",
"mhtml_generation_result.cc",
......@@ -510,8 +505,6 @@ jumbo_source_set("browser_sources") {
if (is_android) {
java_cpp_enum("contacts_picker_properties_requested_javagen") {
sources = [
"contacts_picker_properties_requested.h",
]
sources = [ "contacts_picker_properties_requested.h" ]
}
}
......@@ -1652,7 +1652,6 @@ test("content_unittests") {
"../browser/media/system_media_controls_notifier_unittest.cc",
"../browser/media/webaudio/audio_context_manager_impl_unittest.cc",
"../browser/memory/swap_metrics_driver_impl_unittest.cc",
"../browser/message_port_unittest.cc",
"../browser/native_file_system/file_system_chooser_unittest.cc",
"../browser/native_file_system/native_file_system_file_handle_impl_unittest.cc",
"../browser/native_file_system/native_file_system_file_writer_impl_unittest.cc",
......
......@@ -74,6 +74,7 @@ jumbo_source_set("common") {
"messaging/message_port_channel.cc",
"messaging/message_port_descriptor.cc",
"messaging/message_port_descriptor_mojom_traits.cc",
"messaging/simple_message_port.cc",
"messaging/string_message_codec.cc",
"messaging/transferable_message.cc",
"messaging/transferable_message_mojom_traits.cc",
......@@ -199,7 +200,10 @@ jumbo_source_set("common_unittests_sources") {
# TODO(crbug.com/845384): Enable these tests also on Android. Currently
# they are excluded as they require V8 environment but V8 snapshot is
# not available in the current minimum test setting.
sources += [ "messaging/string_message_codec_unittest.cc" ]
sources += [
"messaging/simple_message_port_unittest.cc",
"messaging/string_message_codec_unittest.cc",
]
}
if (is_android || is_win) {
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// 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 "content/public/browser/message_port.h"
#include "third_party/blink/public/common/messaging/simple_message_port.h"
#include "base/memory/ptr_util.h"
#include "third_party/blink/public/common/messaging/message_port_channel.h"
......@@ -11,63 +11,67 @@
#include "third_party/blink/public/common/messaging/transferable_message_mojom_traits.h"
#include "third_party/blink/public/mojom/messaging/transferable_message.mojom.h"
namespace content {
namespace blink {
MessagePort::Message::Message() = default;
MessagePort::Message::Message(Message&&) = default;
MessagePort::Message& MessagePort::Message::operator=(Message&&) = default;
MessagePort::Message::~Message() = default;
SimpleMessagePort::Message::Message() = default;
SimpleMessagePort::Message::Message(Message&&) = default;
SimpleMessagePort::Message& SimpleMessagePort::Message::operator=(Message&&) =
default;
SimpleMessagePort::Message::~Message() = default;
MessagePort::Message::Message(const base::string16& data) : data(data) {}
SimpleMessagePort::Message::Message(const base::string16& data) : data(data) {}
MessagePort::Message::Message(std::vector<MessagePort> ports)
SimpleMessagePort::Message::Message(std::vector<SimpleMessagePort> ports)
: ports(std::move(ports)) {}
MessagePort::Message::Message(MessagePort&& port) {
SimpleMessagePort::Message::Message(SimpleMessagePort&& port) {
ports.emplace_back(std::move(port));
}
MessagePort::Message::Message(const base::string16& data,
std::vector<MessagePort> ports)
SimpleMessagePort::Message::Message(const base::string16& data,
std::vector<SimpleMessagePort> ports)
: data(data), ports(std::move(ports)) {}
MessagePort::Message::Message(const base::string16& data, MessagePort port)
SimpleMessagePort::Message::Message(const base::string16& data,
SimpleMessagePort port)
: data(data) {
ports.emplace_back(std::move(port));
}
MessagePort::MessageReceiver::MessageReceiver() = default;
MessagePort::MessageReceiver::~MessageReceiver() = default;
bool MessagePort::MessageReceiver::OnMessage(Message) {
SimpleMessagePort::MessageReceiver::MessageReceiver() = default;
SimpleMessagePort::MessageReceiver::~MessageReceiver() = default;
bool SimpleMessagePort::MessageReceiver::OnMessage(Message) {
return true;
}
MessagePort::MessagePort() = default;
SimpleMessagePort::SimpleMessagePort() = default;
MessagePort::MessagePort(MessagePort&& other) {
SimpleMessagePort::SimpleMessagePort(SimpleMessagePort&& other) {
Take(std::move(other));
}
MessagePort& MessagePort::operator=(MessagePort&& other) {
SimpleMessagePort& SimpleMessagePort::operator=(SimpleMessagePort&& other) {
Take(std::move(other));
return *this;
}
MessagePort::~MessagePort() {
SimpleMessagePort::~SimpleMessagePort() {
CloseIfNecessary();
}
// static
std::pair<MessagePort, MessagePort> MessagePort::CreatePair() {
std::pair<SimpleMessagePort, SimpleMessagePort>
SimpleMessagePort::CreatePair() {
mojo::ScopedMessagePipeHandle handle0, handle1;
CHECK_EQ(MOJO_RESULT_OK,
mojo::CreateMessagePipe(nullptr, &handle0, &handle1));
return std::make_pair(MessagePort(std::move(handle0)),
MessagePort(std::move(handle1)));
return std::make_pair(SimpleMessagePort(std::move(handle0)),
SimpleMessagePort(std::move(handle1)));
}
void MessagePort::SetReceiver(MessageReceiver* receiver,
scoped_refptr<base::SequencedTaskRunner> runner) {
void SimpleMessagePort::SetReceiver(
MessageReceiver* receiver,
scoped_refptr<base::SequencedTaskRunner> runner) {
DCHECK(receiver);
DCHECK(runner.get());
......@@ -84,10 +88,10 @@ void MessagePort::SetReceiver(MessageReceiver* receiver,
std::move(runner));
connector_->set_incoming_receiver(this);
connector_->set_connection_error_handler(
base::BindOnce(&MessagePort::OnPipeError, base::Unretained(this)));
base::BindOnce(&SimpleMessagePort::OnPipeError, base::Unretained(this)));
}
void MessagePort::ClearReceiver() {
void SimpleMessagePort::ClearReceiver() {
if (!connector_)
return;
port_ = connector_->PassMessagePipe();
......@@ -95,13 +99,13 @@ void MessagePort::ClearReceiver() {
receiver_ = nullptr;
}
base::SequencedTaskRunner* MessagePort::GetTaskRunner() const {
base::SequencedTaskRunner* SimpleMessagePort::GetTaskRunner() const {
if (!connector_)
return nullptr;
return connector_->task_runner();
}
mojo::ScopedMessagePipeHandle MessagePort::PassHandle() {
mojo::ScopedMessagePipeHandle SimpleMessagePort::PassHandle() {
DCHECK(is_transferable_);
// Clear the receiver, which takes the handle out of the connector if it
......@@ -112,17 +116,17 @@ mojo::ScopedMessagePipeHandle MessagePort::PassHandle() {
return handle;
}
MessagePort::MessagePort(mojo::ScopedMessagePipeHandle&& port)
SimpleMessagePort::SimpleMessagePort(mojo::ScopedMessagePipeHandle&& port)
: port_(std::move(port)), is_closed_(false), is_transferable_(true) {
DCHECK(port_.is_valid());
}
bool MessagePort::CanPostMessage() const {
bool SimpleMessagePort::CanPostMessage() const {
return connector_ && connector_->is_valid() && !is_closed_ && !is_errored_ &&
receiver_;
}
bool MessagePort::PostMessage(Message&& message) {
bool SimpleMessagePort::PostMessage(Message&& message) {
if (!CanPostMessage())
return false;
......@@ -160,24 +164,24 @@ bool MessagePort::PostMessage(Message&& message) {
return true;
}
bool MessagePort::IsValid() const {
bool SimpleMessagePort::IsValid() const {
if (connector_)
return connector_->is_valid();
return port_.is_valid();
}
void MessagePort::Close() {
void SimpleMessagePort::Close() {
CloseIfNecessary();
}
void MessagePort::Reset() {
void SimpleMessagePort::Reset() {
CloseIfNecessary();
is_closed_ = true;
is_errored_ = false;
is_transferable_ = false;
}
void MessagePort::Take(MessagePort&& other) {
void SimpleMessagePort::Take(SimpleMessagePort&& other) {
port_ = std::move(other.port_);
connector_ = std::move(other.connector_);
is_closed_ = std::exchange(other.is_closed_, true);
......@@ -186,7 +190,7 @@ void MessagePort::Take(MessagePort&& other) {
receiver_ = std::exchange(other.receiver_, nullptr);
}
void MessagePort::OnPipeError() {
void SimpleMessagePort::OnPipeError() {
DCHECK(!is_transferable_);
if (is_errored_)
return;
......@@ -195,7 +199,7 @@ void MessagePort::OnPipeError() {
receiver_->OnPipeError();
}
void MessagePort::CloseIfNecessary() {
void SimpleMessagePort::CloseIfNecessary() {
if (is_closed_)
return;
is_closed_ = true;
......@@ -203,7 +207,7 @@ void MessagePort::CloseIfNecessary() {
port_.reset();
}
bool MessagePort::Accept(mojo::Message* mojo_message) {
bool SimpleMessagePort::Accept(mojo::Message* mojo_message) {
DCHECK(receiver_);
DCHECK(!is_transferable_);
......@@ -226,11 +230,11 @@ bool MessagePort::Accept(mojo::Message* mojo_message) {
auto handles =
blink::MessagePortChannel::ReleaseHandles(transferable_message.ports);
for (auto& handle : handles) {
message.ports.emplace_back(MessagePort(std::move(handle)));
message.ports.emplace_back(SimpleMessagePort(std::move(handle)));
}
// Pass the message on to the receiver.
return receiver_->OnMessage(std::move(message));
}
} // namespace content
} // namespace blink
// Copyright 2019 The Chromium Authors. All rights reserved.
// 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 "content/public/browser/message_port.h"
#include "third_party/blink/public/common/messaging/simple_message_port.h"
#include "base/run_loop.h"
#include "base/strings/string16.h"
......@@ -11,18 +11,18 @@
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace content {
namespace blink {
namespace {
using Message = MessagePort::Message;
using Message = SimpleMessagePort::Message;
class LenientMockReceiver : public MessagePort::MessageReceiver {
class LenientMockReceiver : public SimpleMessagePort::MessageReceiver {
public:
LenientMockReceiver() = default;
~LenientMockReceiver() override = default;
// MessagePort::MessageReceiver implementation:
// SimpleMessagePort::MessageReceiver implementation:
MOCK_METHOD1(OnMessage, bool(Message));
MOCK_METHOD0(OnPipeError, void());
};
......@@ -34,11 +34,11 @@ using testing::Invoke;
} // namespace
TEST(MessagePortTest, EndToEnd) {
TEST(SimpleMessagePortTest, EndToEnd) {
base::test::SingleThreadTaskEnvironment task_env;
// Create a dummy pipe and ensure it behaves as expected.
MessagePort port0;
SimpleMessagePort port0;
EXPECT_FALSE(port0.IsValid());
EXPECT_FALSE(port0.is_errored());
EXPECT_TRUE(port0.is_closed());
......@@ -47,9 +47,9 @@ TEST(MessagePortTest, EndToEnd) {
EXPECT_FALSE(port0.CanPostMessage());
// Create a pipe.
auto pipe = MessagePort::CreatePair();
auto pipe = SimpleMessagePort::CreatePair();
port0 = std::move(pipe.first);
MessagePort port1 = std::move(pipe.second);
SimpleMessagePort port1 = std::move(pipe.second);
EXPECT_TRUE(port0.IsValid());
EXPECT_FALSE(port0.is_errored());
......@@ -103,7 +103,7 @@ TEST(MessagePortTest, EndToEnd) {
}
// Send a message the other way (from 1 to 0) with a port.
auto pipe2 = MessagePort::CreatePair();
auto pipe2 = SimpleMessagePort::CreatePair();
{
base::RunLoop run_loop;
EXPECT_CALL(receiver0, OnMessage(_))
......@@ -154,4 +154,4 @@ TEST(MessagePortTest, EndToEnd) {
EXPECT_FALSE(port1.CanPostMessage());
}
} // namespace content
} // namespace blink
......@@ -113,6 +113,7 @@ source_set("headers") {
"messaging/message_port_channel.h",
"messaging/message_port_descriptor.h",
"messaging/message_port_descriptor_mojom_traits.h",
"messaging/simple_message_port.h",
"messaging/string_message_codec.h",
"messaging/transferable_message.h",
"messaging/transferable_message_mojom_traits.h",
......
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