Commit cf3da5fd authored by Shawn Quereshi's avatar Shawn Quereshi Committed by Commit Bot

Abstraction away from blink::WebMessagePort.

Update Cast API Bindings to be agnostic of MessagePort implementation
with platform-specific definitions. A common interface is exposed except
for construction, which requires different artifacts. On non-Fuchsia,
MessagePortCast still uses blink::WebMessagePort. On Fuchsia,
MessagePortFuchsia wraps fuchsia::web::MessagePort instead of requiring
an adapter to blink::WebMessagePort.

R=halliwell@chromium.org, jbroman@chromium.org, kmarshall@chromium.org

Bug: 1126571
Change-Id: Ice36b32949f45a8d1c12daa6b4a33e4b1e885a6b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2441663
Commit-Queue: Shawn Quereshi <shawnq@google.com>
Reviewed-by: default avatarYuchen Liu <yucliu@chromium.org>
Reviewed-by: default avatarKevin Marshall <kmarshall@chromium.org>
Reviewed-by: default avatarJeremy Roman <jbroman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#814346}
parent 86230c5b
...@@ -40,10 +40,10 @@ if (is_fuchsia) { ...@@ -40,10 +40,10 @@ if (is_fuchsia) {
"bindings_manager_fuchsia.cc", "bindings_manager_fuchsia.cc",
"bindings_manager_fuchsia.h", "bindings_manager_fuchsia.h",
] ]
deps = [ deps = [
":bindings_manager", ":bindings_manager",
"//base", "//base",
"//components/cast/message_port",
"//fuchsia:cast_fidl", "//fuchsia:cast_fidl",
"//fuchsia/base", "//fuchsia/base",
"//fuchsia/base:message_port", "//fuchsia/base:message_port",
......
include_rules = [ include_rules = [
"+chromecast/browser", "+chromecast/browser",
"+components/cast/api_bindings", "+components/cast/api_bindings",
"+components/cast/message_port",
"+components/cast/named_message_port_connector", "+components/cast/named_message_port_connector",
"+components/on_load_script_injector/browser", "+components/on_load_script_injector/browser",
"+content/public/common", "+content/public/common",
......
...@@ -24,8 +24,14 @@ BindingsManagerCast::BindingsManagerCast( ...@@ -24,8 +24,14 @@ BindingsManagerCast::BindingsManagerCast(
port_connector_ = port_connector_ =
std::make_unique<NamedMessagePortConnectorCast>(cast_web_contents_, this); std::make_unique<NamedMessagePortConnectorCast>(cast_web_contents_, this);
port_connector_->RegisterPortHandler(base::BindRepeating(
&BindingsManagerCast::OnPortConnected, base::Unretained(this))); // TODO(crbug.com/1135379): Remove static_cast with deprecated OnPortConnected
// overload
port_connector_->RegisterPortHandler(
base::BindRepeating(static_cast<bool (BindingsManagerCast::*)(
base::StringPiece, blink::WebMessagePort)>(
&BindingsManagerCast::OnPortConnected),
base::Unretained(this)));
} }
BindingsManagerCast::~BindingsManagerCast() = default; BindingsManagerCast::~BindingsManagerCast() = default;
......
...@@ -16,8 +16,7 @@ class NamedMessagePortConnectorCast; ...@@ -16,8 +16,7 @@ class NamedMessagePortConnectorCast;
// Implements the CastOS BindingsManager. // Implements the CastOS BindingsManager.
class BindingsManagerCast : public BindingsManager, class BindingsManagerCast : public BindingsManager,
public CastWebContents::Observer, public CastWebContents::Observer {
public blink::WebMessagePort::MessageReceiver {
public: public:
explicit BindingsManagerCast(chromecast::CastWebContents* cast_web_contents); explicit BindingsManagerCast(chromecast::CastWebContents* cast_web_contents);
~BindingsManagerCast() override; ~BindingsManagerCast() override;
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "base/check.h" #include "base/check.h"
#include "base/fuchsia/fuchsia_logging.h" #include "base/fuchsia/fuchsia_logging.h"
#include "components/cast/message_port/message_port_fuchsia.h"
#include "fuchsia/base/mem_buffer_util.h" #include "fuchsia/base/mem_buffer_util.h"
#include "fuchsia/base/message_port.h" #include "fuchsia/base/message_port.h"
...@@ -42,8 +43,9 @@ void BindingsManagerFuchsia::GetAll(GetAllCallback callback) { ...@@ -42,8 +43,9 @@ void BindingsManagerFuchsia::GetAll(GetAllCallback callback) {
void BindingsManagerFuchsia::Connect( void BindingsManagerFuchsia::Connect(
std::string port_name, std::string port_name,
fidl::InterfaceHandle<::fuchsia::web::MessagePort> message_port) { fidl::InterfaceHandle<::fuchsia::web::MessagePort> message_port) {
OnPortConnected( OnPortConnected(port_name, std::unique_ptr<cast_api_bindings::MessagePort>(
port_name, cr_fuchsia::BlinkMessagePortFromFidl(std::move(message_port))); cast_api_bindings::MessagePortFuchsia::Create(
std::move(message_port))));
} }
} // namespace bindings } // namespace bindings
......
...@@ -18,5 +18,8 @@ source_set("manager") { ...@@ -18,5 +18,8 @@ source_set("manager") {
"//third_party/blink/public/common", "//third_party/blink/public/common",
] ]
defines = [ "CAST_COMPONENT_IMPLEMENTATION" ] defines = [ "CAST_COMPONENT_IMPLEMENTATION" ]
public_deps = [ "//mojo/public/cpp/bindings" ] public_deps = [
"//components/cast/message_port",
"//mojo/public/cpp/bindings",
]
} }
...@@ -22,8 +22,15 @@ void Manager::RegisterPortHandler(base::StringPiece port_name, ...@@ -22,8 +22,15 @@ void Manager::RegisterPortHandler(base::StringPiece port_name,
DCHECK(result.second); DCHECK(result.second);
} }
void Manager::RegisterPortHandler(base::StringPiece port_name,
MessagePortProxyConnectedHandler handler) {
auto result = port_proxy_handlers_.try_emplace(port_name, std::move(handler));
DCHECK(result.second);
}
void Manager::UnregisterPortHandler(base::StringPiece port_name) { void Manager::UnregisterPortHandler(base::StringPiece port_name) {
size_t deleted = port_handlers_.erase(port_name); size_t deleted = port_handlers_.erase(port_name);
deleted += port_proxy_handlers_.erase(port_name);
DCHECK_EQ(deleted, 1u); DCHECK_EQ(deleted, 1u);
} }
...@@ -42,4 +49,20 @@ bool Manager::OnPortConnected(base::StringPiece port_name, ...@@ -42,4 +49,20 @@ bool Manager::OnPortConnected(base::StringPiece port_name,
return true; return true;
} }
bool Manager::OnPortConnected(
base::StringPiece port_name,
std::unique_ptr<cast_api_bindings::MessagePort> port) {
if (!port)
return false;
auto handler = port_proxy_handlers_.find(port_name);
if (handler == port_proxy_handlers_.end()) {
LOG(ERROR) << "No handler found for port " << port_name << ".";
return false;
}
handler->second.Run(std::move(port));
return true;
}
} // namespace cast_api_bindings } // namespace cast_api_bindings
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "base/containers/flat_map.h" #include "base/containers/flat_map.h"
#include "base/strings/string_piece.h" #include "base/strings/string_piece.h"
#include "components/cast/cast_component_export.h" #include "components/cast/cast_component_export.h"
#include "components/cast/message_port/message_port.h"
#include "third_party/blink/public/common/messaging/web_message_port.h" #include "third_party/blink/public/common/messaging/web_message_port.h"
namespace cast_api_bindings { namespace cast_api_bindings {
...@@ -20,18 +21,28 @@ namespace cast_api_bindings { ...@@ -20,18 +21,28 @@ namespace cast_api_bindings {
// and to register handlers for communication with the content. // and to register handlers for communication with the content.
class CAST_COMPONENT_EXPORT Manager { class CAST_COMPONENT_EXPORT Manager {
public: public:
// TODO(crbug.com/1135379): Deprecated; remove or redefine after fixing
// downstream dependencies
using MessagePortConnectedHandler = using MessagePortConnectedHandler =
base::RepeatingCallback<void(blink::WebMessagePort)>; base::RepeatingCallback<void(blink::WebMessagePort)>;
using MessagePortProxyConnectedHandler = base::RepeatingCallback<void(
std::unique_ptr<cast_api_bindings::MessagePort>)>;
Manager(); Manager();
virtual ~Manager(); virtual ~Manager();
// TODO(crbug.com/1135379): Deprecated; remove after fixing downstream
// dependencies
void RegisterPortHandler(base::StringPiece port_name,
MessagePortConnectedHandler handler);
// Registers a |handler| which will receive MessagePorts originating from // Registers a |handler| which will receive MessagePorts originating from
// the frame's web content. |port_name| is an alphanumeric string that is // the frame's web content. |port_name| is an alphanumeric string that is
// consistent across JS and native code. // consistent across JS and native code.
// All handlers must be Unregistered() before |this| is destroyed. // All handlers must be Unregistered() before |this| is destroyed.
void RegisterPortHandler(base::StringPiece port_name, void RegisterPortHandler(base::StringPiece port_name,
MessagePortConnectedHandler handler); MessagePortProxyConnectedHandler handler);
// Unregisters a previously registered handler. // Unregisters a previously registered handler.
// The owner of Manager is responsible for ensuring that all // The owner of Manager is responsible for ensuring that all
...@@ -44,15 +55,25 @@ class CAST_COMPONENT_EXPORT Manager { ...@@ -44,15 +55,25 @@ class CAST_COMPONENT_EXPORT Manager {
base::StringPiece binding_script) = 0; base::StringPiece binding_script) = 0;
protected: protected:
// TODO(crbug.com/1135379): Deprecated; remove after fixing downstream
// dependencies
bool OnPortConnected(base::StringPiece port_name, blink::WebMessagePort port);
// Called by platform-specific implementations when the content requests a // Called by platform-specific implementations when the content requests a
// connection to |port_name|. // connection to |port_name|.
// Returns |false| if the port was invalid or not registered in advance, at // Returns |false| if the port was invalid or not registered in advance, at
// which point the matchmaking port should be dropped. // which point the matchmaking port should be dropped.
bool OnPortConnected(base::StringPiece port_name, blink::WebMessagePort port); bool OnPortConnected(base::StringPiece port_name,
std::unique_ptr<cast_api_bindings::MessagePort> port);
private: private:
// TODO(crbug.com/1135379): Deprecated; remove after fixing downstream
// dependencies
base::flat_map<std::string, MessagePortConnectedHandler> port_handlers_; base::flat_map<std::string, MessagePortConnectedHandler> port_handlers_;
base::flat_map<std::string, MessagePortProxyConnectedHandler>
port_proxy_handlers_;
DISALLOW_COPY_AND_ASSIGN(Manager); DISALLOW_COPY_AND_ASSIGN(Manager);
}; };
......
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
#include <string> #include <string>
#include "base/logging.h" #include "base/logging.h"
#include "base/strings/utf_string_conversions.h"
#include "components/cast/api_bindings/manager.h" #include "components/cast/api_bindings/manager.h"
namespace cast_api_bindings { namespace cast_api_bindings {
...@@ -41,9 +40,10 @@ ScopedApiBinding::~ScopedApiBinding() { ...@@ -41,9 +40,10 @@ ScopedApiBinding::~ScopedApiBinding() {
} }
} }
void ScopedApiBinding::OnPortConnected(blink::WebMessagePort port) { void ScopedApiBinding::OnPortConnected(
std::unique_ptr<cast_api_bindings::MessagePort> port) {
message_port_ = std::move(port); message_port_ = std::move(port);
message_port_.SetReceiver(this, base::SequencedTaskRunnerHandle::Get()); message_port_->SetReceiver(this);
delegate_->OnConnected(); delegate_->OnConnected();
} }
...@@ -51,28 +51,23 @@ bool ScopedApiBinding::SendMessage(base::StringPiece data_utf8) { ...@@ -51,28 +51,23 @@ bool ScopedApiBinding::SendMessage(base::StringPiece data_utf8) {
DCHECK(delegate_); DCHECK(delegate_);
DVLOG(1) << "SendMessage: message=" << data_utf8; DVLOG(1) << "SendMessage: message=" << data_utf8;
if (!message_port_.IsValid()) { if (!message_port_->CanPostMessage()) {
LOG(WARNING) LOG(WARNING)
<< "Attempted to write to unconnected MessagePort, dropping message."; << "Attempted to write to unconnected MessagePort, dropping message.";
return false; return false;
} }
if (!message_port_.PostMessage( if (!message_port_->PostMessage(data_utf8.as_string())) {
blink::WebMessagePort::Message(base::UTF8ToUTF16(data_utf8)))) {
return false; return false;
} }
return true; return true;
} }
bool ScopedApiBinding::OnMessage(blink::WebMessagePort::Message message) { bool ScopedApiBinding::OnMessage(
std::string message_utf8; base::StringPiece message,
if (!base::UTF16ToUTF8(message.data.data(), message.data.size(), std::vector<std::unique_ptr<cast_api_bindings::MessagePort>> ports) {
&message_utf8)) { return delegate_->OnMessage(message);
return false;
}
return delegate_->OnMessage(message_utf8);
} }
void ScopedApiBinding::OnPipeError() { void ScopedApiBinding::OnPipeError() {
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
#include "base/optional.h" #include "base/optional.h"
#include "base/strings/string_piece.h" #include "base/strings/string_piece.h"
#include "components/cast/cast_component_export.h" #include "components/cast/cast_component_export.h"
#include "third_party/blink/public/common/messaging/web_message_port.h" #include "components/cast/message_port/message_port.h"
namespace cast_api_bindings { namespace cast_api_bindings {
...@@ -22,7 +22,7 @@ class Manager; ...@@ -22,7 +22,7 @@ class Manager;
// communication channels, as well as unregistration on object teardown, using // communication channels, as well as unregistration on object teardown, using
// RAII semantics. // RAII semantics.
class CAST_COMPONENT_EXPORT ScopedApiBinding class CAST_COMPONENT_EXPORT ScopedApiBinding
: public blink::WebMessagePort::MessageReceiver { : public cast_api_bindings::MessagePort::Receiver {
public: public:
// Methods for handling message I/O with bindings scripts. // Methods for handling message I/O with bindings scripts.
class Delegate { class Delegate {
...@@ -71,10 +71,12 @@ class CAST_COMPONENT_EXPORT ScopedApiBinding ...@@ -71,10 +71,12 @@ class CAST_COMPONENT_EXPORT ScopedApiBinding
private: private:
// Called when a port is received from the page. // Called when a port is received from the page.
void OnPortConnected(blink::WebMessagePort port); void OnPortConnected(std::unique_ptr<cast_api_bindings::MessagePort> port);
// blink::WebMessagePort::MessageReceiver implementation: // cast_api_bindings::MessagePort::Receiver implementation:
bool OnMessage(blink::WebMessagePort::Message message) final; bool OnMessage(
base::StringPiece message,
std::vector<std::unique_ptr<cast_api_bindings::MessagePort>> ports) final;
void OnPipeError() final; void OnPipeError() final;
Manager* const bindings_manager_; Manager* const bindings_manager_;
...@@ -82,7 +84,7 @@ class CAST_COMPONENT_EXPORT ScopedApiBinding ...@@ -82,7 +84,7 @@ class CAST_COMPONENT_EXPORT ScopedApiBinding
const std::string js_bindings_id_; const std::string js_bindings_id_;
// The MessagePort used to receive messages from the receiver JS. // The MessagePort used to receive messages from the receiver JS.
blink::WebMessagePort message_port_; std::unique_ptr<cast_api_bindings::MessagePort> message_port_;
}; };
} // namespace cast_api_bindings } // namespace cast_api_bindings
......
# 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.
import("//build/config/features.gni")
source_set("message_port") {
sources = [
"message_port.cc",
"message_port.h",
]
deps = [
"//base",
"//components/cast:export",
]
defines = [ "CAST_COMPONENT_IMPLEMENTATION" ]
if (is_fuchsia) {
sources += [
"message_port_fuchsia.cc",
"message_port_fuchsia.h",
]
public_deps = [
"//fuchsia:cast_fidl",
"//fuchsia/base",
]
} else if (is_chromecast) {
sources += [
"message_port_cast.cc",
"message_port_cast.h",
]
deps += [ "//third_party/blink/public/common" ]
}
}
include_rules = [
"+third_party/blink/public/common/messaging",
"+fuchsia",
]
// 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 "components/cast/message_port/message_port.h"
namespace cast_api_bindings {
MessagePort::Receiver::~Receiver() = default;
MessagePort::~MessagePort() = default;
} // namespace cast_api_bindings
\ No newline at end of file
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_CAST_MESSAGE_PORT_MESSAGE_PORT_H_
#define COMPONENTS_CAST_MESSAGE_PORT_MESSAGE_PORT_H_
#include <memory>
#include <vector>
#include "base/strings/string_piece.h"
namespace cast_api_bindings {
// HTML5 MessagePort abstraction; allows usage of the platform MessagePort type
// without exposing details of the message format, paired port creation, or
// transfer of ports.
class MessagePort {
public:
// Implemented by receivers of messages from the MessagePort class.
class Receiver {
public:
virtual ~Receiver();
// Receives a |message| and ownership of |ports|.
virtual bool OnMessage(base::StringPiece message,
std::vector<std::unique_ptr<MessagePort>> ports) = 0;
// Receives an error.
virtual void OnPipeError() = 0;
};
virtual ~MessagePort();
// Creates a pair of message ports. Clients must respect |client| and
// |server| semantics because they matter for some implementations.
static void CreatePair(std::unique_ptr<MessagePort>* client,
std::unique_ptr<MessagePort>* server);
// Sends a |message| from the port.
virtual bool PostMessage(base::StringPiece message) = 0;
// Sends a |message| from the port along with transferable |ports|.
virtual bool PostMessageWithTransferables(
base::StringPiece message,
std::vector<std::unique_ptr<MessagePort>> ports) = 0;
// Sets the |receiver| for messages arriving to this port. May only be set
// once.
virtual void SetReceiver(
cast_api_bindings::MessagePort::Receiver* receiver) = 0;
// Closes the underlying port.
virtual void Close() = 0;
// Whether a message can be posted; may be used to check the state of the port
// without posting a message.
virtual bool CanPostMessage() const = 0;
};
} // namespace cast_api_bindings
#endif // COMPONENTS_CAST_MESSAGE_PORT_MESSAGE_PORT_H_
// 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 "components/cast/message_port/message_port_cast.h"
#include <utility>
#include "base/strings/utf_string_conversions.h"
#include "base/threading/sequenced_task_runner_handle.h"
namespace cast_api_bindings {
// static
void MessagePort::CreatePair(std::unique_ptr<MessagePort>* client,
std::unique_ptr<MessagePort>* server) {
auto pair_raw = blink::WebMessagePort::CreatePair();
*client = MessagePortCast::Create(std::move(pair_raw.first));
*server = MessagePortCast::Create(std::move(pair_raw.second));
}
// static
std::unique_ptr<MessagePort> MessagePortCast::Create(
blink::WebMessagePort&& port) {
return std::make_unique<MessagePortCast>(std::move(port));
}
// static
MessagePortCast* MessagePortCast::FromMessagePort(MessagePort* port) {
DCHECK(port);
// This is safe because there is one MessagePort implementation per platform
// and this is called internally to the implementation.
return static_cast<MessagePortCast*>(port);
}
bool MessagePortCast::OnMessage(blink::WebMessagePort::Message message) {
DCHECK(receiver_);
std::string message_str;
if (!base::UTF16ToUTF8(message.data.data(), message.data.size(),
&message_str)) {
return false;
}
std::vector<std::unique_ptr<MessagePort>> transferables;
for (blink::WebMessagePort& port : message.ports) {
transferables.push_back(Create(std::move(port)));
}
return receiver_->OnMessage(message_str, std::move(transferables));
}
MessagePortCast::MessagePortCast(blink::WebMessagePort&& port)
: receiver_(nullptr), port_(std::move(port)) {}
MessagePortCast::~MessagePortCast() {}
void MessagePortCast::OnPipeError() {
DCHECK(receiver_);
receiver_->OnPipeError();
}
blink::WebMessagePort MessagePortCast::TakePort() {
return std::move(port_);
}
// cast_api_bindings::MessagePortCast implementation
bool MessagePortCast::PostMessage(base::StringPiece message) {
return PostMessageWithTransferables(message, {});
}
bool MessagePortCast::PostMessageWithTransferables(
base::StringPiece message,
std::vector<std::unique_ptr<MessagePort>> ports) {
DCHECK(port_.IsValid());
std::vector<blink::WebMessagePort> transferables;
for (auto& port : ports) {
MessagePortCast* port_cast = FromMessagePort(port.get());
transferables.push_back(port_cast->TakePort());
}
blink::WebMessagePort::Message msg = blink::WebMessagePort::Message(
base::UTF8ToUTF16(message), std::move(transferables));
return port_.PostMessage(std::move(msg));
}
void MessagePortCast::SetReceiver(
cast_api_bindings::MessagePort::Receiver* receiver) {
DCHECK(receiver);
DCHECK(!receiver_);
receiver_ = receiver;
port_.SetReceiver(this, base::SequencedTaskRunnerHandle::Get());
}
void MessagePortCast::Close() {
return port_.Close();
}
bool MessagePortCast::CanPostMessage() const {
return port_.CanPostMessage();
}
} // namespace cast_api_bindings
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_CAST_MESSAGE_PORT_MESSAGE_PORT_CAST_H_
#define COMPONENTS_CAST_MESSAGE_PORT_MESSAGE_PORT_CAST_H_
#include "components/cast/message_port/message_port.h"
#include "third_party/blink/public/common/messaging/web_message_port.h"
namespace cast_api_bindings {
// Abstraction of HTML MessagePortCast away from blink::WebMessagePort
// Represents one end of a message channel.
class MessagePortCast : public cast_api_bindings::MessagePort,
public blink::WebMessagePort::MessageReceiver {
public:
explicit MessagePortCast(blink::WebMessagePort&& port);
~MessagePortCast() override;
MessagePortCast(const MessagePortCast&) = delete;
MessagePortCast& operator=(const MessagePortCast&) = delete;
static std::unique_ptr<MessagePort> Create(blink::WebMessagePort&& port);
private:
// Gets the implementation of |port| for callers who know its platform type.
static MessagePortCast* FromMessagePort(MessagePort* port);
// Retrieves the platform-specific port and invalidates this object.
blink::WebMessagePort TakePort();
// cast_api_bindings::MessagePort implementation
bool PostMessage(base::StringPiece message) final;
bool PostMessageWithTransferables(
base::StringPiece message,
std::vector<std::unique_ptr<MessagePort>> ports) final;
void SetReceiver(cast_api_bindings::MessagePort::Receiver* receiver) final;
void Close() final;
bool CanPostMessage() const final;
// blink::WebMessagePort::MessageReceiver implementation
bool OnMessage(blink::WebMessagePort::Message message) final;
void OnPipeError() final;
cast_api_bindings::MessagePort::Receiver* receiver_;
blink::WebMessagePort port_;
};
} // namespace cast_api_bindings
#endif // COMPONENTS_CAST_MESSAGE_PORT_MESSAGE_PORT_CAST_H_
This diff is collapsed.
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_CAST_MESSAGE_PORT_MESSAGE_PORT_FUCHSIA_H_
#define COMPONENTS_CAST_MESSAGE_PORT_MESSAGE_PORT_FUCHSIA_H_
#include <fuchsia/web/cpp/fidl.h>
#include <lib/fidl/cpp/binding.h>
#include <lib/fidl/cpp/interface_handle.h>
#include <lib/fidl/cpp/interface_request.h>
#include "base/containers/circular_deque.h"
#include "components/cast/message_port/message_port.h"
#include "fuchsia/fidl/chromium/cast/cpp/fidl.h"
namespace cast_api_bindings {
// Implements the MessagePort abstraction for the FIDL interface
// fuchsia::web::WebMessagePort.
class MessagePortFuchsia : public cast_api_bindings::MessagePort {
public:
~MessagePortFuchsia() override;
MessagePortFuchsia(const MessagePortFuchsia&) = delete;
MessagePortFuchsia& operator=(const MessagePortFuchsia&) = delete;
static std::unique_ptr<MessagePort> Create(
fidl::InterfaceHandle<::fuchsia::web::MessagePort> port);
static std::unique_ptr<MessagePort> Create(
fidl::InterfaceRequest<::fuchsia::web::MessagePort> port);
protected:
// Represents whether a MessagePortFuchsia was created from an InterfaceHandle
// (PortType::HANDLE) or InterfaceRequest (PortType::REQUEST)
enum class PortType {
HANDLE = 1,
REQUEST = 2,
};
MessagePortFuchsia(PortType port_type);
// Creates a fuchsia::web::WebMessage containing |message| and transferring
// |ports|
static fuchsia::web::WebMessage CreateWebMessage(
base::StringPiece message,
std::vector<std::unique_ptr<MessagePort>> ports);
// Delivers a message to FIDL from |message_queue_|.
virtual void DeliverMessageToFidl() = 0;
// Receives a |message| from FIDL into |message_queue_|. Returns a value if
// an error occurred.
base::Optional<fuchsia::web::FrameError> ReceiveMessageFromFidl(
fuchsia::web::WebMessage message);
// Retrieves the platform-specific port and invalidates this object.
fidl::InterfaceHandle<::fuchsia::web::MessagePort> TakeHandle();
fidl::InterfaceRequest<::fuchsia::web::MessagePort> TakeRequest();
void OnZxError(zx_status_t status);
void ReportPipeError();
// cast_api_bindings::MessagePort implementation
bool PostMessage(base::StringPiece message) final;
bool PostMessageWithTransferables(
base::StringPiece message,
std::vector<std::unique_ptr<MessagePort>> ports) final;
cast_api_bindings::MessagePort::Receiver* receiver_;
base::circular_deque<fuchsia::web::WebMessage> message_queue_;
private:
// Gets the implementation of |port| for callers who know its platform type.
static MessagePortFuchsia* FromMessagePort(MessagePort* port);
PortType port_type() const;
const PortType port_type_;
};
} // namespace cast_api_bindings
#endif // COMPONENTS_CAST_MESSAGE_PORT_MESSAGE_PORT_FUCHSIA_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