2020-03-12 01:07:54 +00:00
|
|
|
// Copyright (c) 2020 Slack Technologies, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2021-11-22 07:34:31 +00:00
|
|
|
#ifndef ELECTRON_SHELL_BROWSER_API_MESSAGE_PORT_H_
|
|
|
|
#define ELECTRON_SHELL_BROWSER_API_MESSAGE_PORT_H_
|
2020-03-12 01:07:54 +00:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "gin/wrappable.h"
|
|
|
|
#include "mojo/public/cpp/bindings/connector.h"
|
|
|
|
#include "mojo/public/cpp/bindings/message.h"
|
2023-08-03 12:30:19 +00:00
|
|
|
#include "shell/common/gin_helper/cleaned_up_at_exit.h"
|
2020-03-12 01:07:54 +00:00
|
|
|
#include "third_party/blink/public/common/messaging/message_port_channel.h"
|
2020-05-26 20:06:26 +00:00
|
|
|
#include "third_party/blink/public/common/messaging/message_port_descriptor.h"
|
2020-03-12 01:07:54 +00:00
|
|
|
|
|
|
|
namespace gin {
|
|
|
|
class Arguments;
|
|
|
|
template <typename T>
|
|
|
|
class Handle;
|
|
|
|
} // namespace gin
|
|
|
|
|
|
|
|
namespace electron {
|
|
|
|
|
|
|
|
// A non-blink version of blink::MessagePort.
|
2023-08-03 12:30:19 +00:00
|
|
|
class MessagePort : public gin::Wrappable<MessagePort>,
|
|
|
|
public gin_helper::CleanedUpAtExit,
|
|
|
|
public mojo::MessageReceiver {
|
2020-03-12 01:07:54 +00:00
|
|
|
public:
|
|
|
|
~MessagePort() override;
|
|
|
|
static gin::Handle<MessagePort> Create(v8::Isolate* isolate);
|
|
|
|
|
|
|
|
void PostMessage(gin::Arguments* args);
|
|
|
|
void Start();
|
|
|
|
void Close();
|
|
|
|
|
2020-10-27 15:18:36 +00:00
|
|
|
void Entangle(blink::MessagePortDescriptor port);
|
2020-03-12 01:07:54 +00:00
|
|
|
void Entangle(blink::MessagePortChannel channel);
|
|
|
|
|
|
|
|
blink::MessagePortChannel Disentangle();
|
|
|
|
|
|
|
|
bool IsEntangled() const { return !closed_ && !IsNeutered(); }
|
|
|
|
bool IsNeutered() const { return !connector_ || !connector_->is_valid(); }
|
|
|
|
|
|
|
|
static std::vector<gin::Handle<MessagePort>> EntanglePorts(
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
std::vector<blink::MessagePortChannel> channels);
|
|
|
|
|
|
|
|
static std::vector<blink::MessagePortChannel> DisentanglePorts(
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
const std::vector<gin::Handle<MessagePort>>& ports,
|
|
|
|
bool* threw_exception);
|
|
|
|
|
|
|
|
// gin::Wrappable
|
2024-03-06 11:45:28 +00:00
|
|
|
static gin::WrapperInfo kWrapperInfo;
|
2020-03-12 01:07:54 +00:00
|
|
|
gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
|
|
|
|
v8::Isolate* isolate) override;
|
|
|
|
const char* GetTypeName() override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
MessagePort();
|
|
|
|
|
|
|
|
// The blink version of MessagePort uses the very nice "ActiveScriptWrapper"
|
|
|
|
// class, which keeps the object alive through the V8 embedder hooks into the
|
|
|
|
// GC lifecycle: see
|
2023-02-12 02:52:54 +00:00
|
|
|
// https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/platform/heap/thread_state.cc;l=258;drc=b892cf58e162a8f66cd76d7472f129fe0fb6a7d1
|
2020-03-12 01:07:54 +00:00
|
|
|
// We do not have that luxury, so we brutishly use v8::Global to accomplish
|
|
|
|
// something similar. Critically, whenever the value of
|
|
|
|
// "HasPendingActivity()" changes, we must call Pin() or Unpin() as
|
|
|
|
// appropriate.
|
|
|
|
bool HasPendingActivity() const;
|
|
|
|
void Pin();
|
|
|
|
void Unpin();
|
|
|
|
|
|
|
|
// mojo::MessageReceiver
|
|
|
|
bool Accept(mojo::Message* mojo_message) override;
|
|
|
|
|
|
|
|
std::unique_ptr<mojo::Connector> connector_;
|
|
|
|
bool started_ = false;
|
|
|
|
bool closed_ = false;
|
|
|
|
|
|
|
|
v8::Global<v8::Value> pinned_;
|
|
|
|
|
2020-05-26 20:06:26 +00:00
|
|
|
// The internal port owned by this class. The handle itself is moved into the
|
|
|
|
// |connector_| while entangled.
|
|
|
|
blink::MessagePortDescriptor port_;
|
|
|
|
|
2020-03-12 01:07:54 +00:00
|
|
|
base::WeakPtrFactory<MessagePort> weak_factory_{this};
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace electron
|
|
|
|
|
2021-11-22 07:34:31 +00:00
|
|
|
#endif // ELECTRON_SHELL_BROWSER_API_MESSAGE_PORT_H_
|