refactor: use MessagePortDescriptor instead of raw mojo::MessagePipeHandles

Refs: https://chromium-review.googlesource.com/c/chromium/src/+/1952124
This commit is contained in:
Samuel Attard
2020-05-04 18:48:24 -07:00
committed by deepak1556
parent a14cdc465a
commit dce81ad06f
2 changed files with 28 additions and 12 deletions

View File

@@ -21,13 +21,20 @@
#include "third_party/blink/public/common/messaging/transferable_message.h"
#include "third_party/blink/public/common/messaging/transferable_message_mojom_traits.h"
#include "third_party/blink/public/mojom/messaging/transferable_message.mojom.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
namespace electron {
gin::WrapperInfo MessagePort::kWrapperInfo = {gin::kEmbedderNativeGin};
MessagePort::MessagePort() = default;
MessagePort::~MessagePort() = default;
MessagePort::~MessagePort() {
if (!IsNeutered()) {
// Disentangle before teardown. The MessagePortDescriptor will blow up if it
// hasn't had its underlying handle returned to it before teardown.
Disentangle();
}
}
// static
gin::Handle<MessagePort> MessagePort::Create(v8::Isolate* isolate) {
@@ -97,8 +104,9 @@ void MessagePort::Close() {
if (closed_)
return;
if (!IsNeutered()) {
connector_ = nullptr;
Entangle(mojo::MessagePipe().handle0);
Disentangle().ReleaseHandle();
blink::MessagePortDescriptorPair pipe;
Entangle(pipe.TakePort0());
}
closed_ = true;
if (!HasPendingActivity())
@@ -111,11 +119,14 @@ void MessagePort::Close() {
gin_helper::EmitEvent(isolate, self, "close");
}
void MessagePort::Entangle(mojo::ScopedMessagePipeHandle handle) {
DCHECK(handle.is_valid());
void MessagePort::Entangle(blink::MessagePortDescriptor port) {
DCHECK(port.IsValid());
DCHECK(!connector_);
port_ = std::move(port);
connector_ = std::make_unique<mojo::Connector>(
std::move(handle), mojo::Connector::SINGLE_THREADED_SEND,
port_.TakeHandleToEntangle(blink::ExecutionContext::From(
v8::Isolate::GetCurrent()->GetCurrentContext())),
mojo::Connector::SINGLE_THREADED_SEND,
base::ThreadTaskRunnerHandle::Get());
connector_->PauseIncomingMethodCallProcessing();
connector_->set_incoming_receiver(this);
@@ -131,11 +142,11 @@ void MessagePort::Entangle(blink::MessagePortChannel channel) {
blink::MessagePortChannel MessagePort::Disentangle() {
DCHECK(!IsNeutered());
auto result = blink::MessagePortChannel(connector_->PassMessagePipe());
port_.GiveDisentangledHandle(connector_->PassMessagePipe());
connector_ = nullptr;
if (!HasPendingActivity())
Unpin();
return result;
return blink::MessagePortChannel(std::move(port_));
}
bool MessagePort::HasPendingActivity() const {
@@ -260,9 +271,9 @@ using electron::MessagePort;
v8::Local<v8::Value> CreatePair(v8::Isolate* isolate) {
auto port1 = MessagePort::Create(isolate);
auto port2 = MessagePort::Create(isolate);
mojo::MessagePipe pipe;
port1->Entangle(std::move(pipe.handle0));
port2->Entangle(std::move(pipe.handle1));
blink::MessagePortDescriptorPair pipe;
port1->Entangle(pipe.TakePort0());
port2->Entangle(pipe.TakePort1());
return gin::DataObjectBuilder(isolate)
.Set("port1", port1)
.Set("port2", port2)

View File

@@ -12,6 +12,7 @@
#include "mojo/public/cpp/bindings/connector.h"
#include "mojo/public/cpp/bindings/message.h"
#include "third_party/blink/public/common/messaging/message_port_channel.h"
#include "third_party/blink/public/common/messaging/message_port_descriptor.h"
namespace gin {
class Arguments;
@@ -31,7 +32,7 @@ class MessagePort : public gin::Wrappable<MessagePort>, mojo::MessageReceiver {
void Start();
void Close();
void Entangle(mojo::ScopedMessagePipeHandle handle);
void Entangle(blink::MessagePortDescriptor handle);
void Entangle(blink::MessagePortChannel channel);
blink::MessagePortChannel Disentangle();
@@ -78,6 +79,10 @@ class MessagePort : public gin::Wrappable<MessagePort>, mojo::MessageReceiver {
v8::Global<v8::Value> pinned_;
// The internal port owned by this class. The handle itself is moved into the
// |connector_| while entangled.
blink::MessagePortDescriptor port_;
base::WeakPtrFactory<MessagePort> weak_factory_{this};
};