Repository: electron/electron
File: shell/services/node/parent_port.cc (C++)

Code before:
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.

#include "shell/services/node/parent_port.h"

#include <utility>

#include "base/no_destructor.h"
#include "gin/data_object_builder.h"
#include "gin/handle.h"
#include "gin/object_template_builder.h"
#include "shell/browser/api/message_port.h"
#include "shell/browser/javascript_environment.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/gin_helper/event_emitter_caller.h"
#include "shell/common/node_includes.h"
#include "shell/common/v8_util.h"
#include "third_party/blink/public/common/messaging/transferable_message_mojom_traits.h"

namespace electron {

gin::WrapperInfo ParentPort::kWrapperInfo = {gin::kEmbedderNativeGin};

ParentPort* ParentPort::GetInstance() {
  static base::NoDestructor<ParentPort> instance;
  return instance.get();
}

ParentPort::ParentPort() = default;
ParentPort::~ParentPort() = default;

void ParentPort::Initialize(blink::MessagePortDescriptor port) {
  port_ = std::move(port);
  connector_ = std::make_unique<mojo::Connector>(
      port_.TakeHandleToEntangleWithEmbedder(),
      mojo::Connector::SINGLE_THREADED_SEND,
      base::SingleThreadTaskRunner::GetCurrentDefault());
  connector_->PauseIncomingMethodCallProcessing();
  connector_->set_incoming_receiver(this);
  connector_->set_connection_error_handler(
      base::BindOnce(&ParentPort::Close, base::Unretained(this)));
}

void ParentPort::PostMessage(v8::Local<v8::Value> message_value) {
  if (!connector_closed_ && connector_ && connector_->is_valid()) {
    v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
    blink::TransferableMessage transferable_message;

    if (!electron::SerializeV8Value(isolate, message_value,
                                    &transferable_message)) {
      // SerializeV8Value sets an exception.

Review comment (suggestion):
@deepak1556 I think I've done this too once or twice, but we probably shouldn't :slightly_smiling_face: 

Even though `base::NoDestructor` is more prolix than a raw pointer, we should use it for globals & statics like this one.

Code after:
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.

#include "shell/services/node/parent_port.h"

#include <utility>

#include "base/no_destructor.h"
#include "gin/data_object_builder.h"
#include "gin/handle.h"
#include "gin/object_template_builder.h"
#include "shell/browser/api/message_port.h"
#include "shell/browser/javascript_environment.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/gin_helper/event_emitter_caller.h"
#include "shell/common/node_includes.h"
#include "shell/common/v8_util.h"
#include "third_party/blink/public/common/messaging/transferable_message_mojom_traits.h"

namespace electron {

gin::WrapperInfo ParentPort::kWrapperInfo = {gin::kEmbedderNativeGin};

ParentPort* ParentPort::GetInstance() {
  static ParentPort* instance = new ParentPort();
  return instance;
}

ParentPort::ParentPort() = default;
ParentPort::~ParentPort() = default;

void ParentPort::Initialize(blink::MessagePortDescriptor port) {
  port_ = std::move(port);
  connector_ = std::make_unique<mojo::Connector>(
      port_.TakeHandleToEntangleWithEmbedder(),
      mojo::Connector::SINGLE_THREADED_SEND,
      base::SingleThreadTaskRunner::GetCurrentDefault());
  connector_->PauseIncomingMethodCallProcessing();
  connector_->set_incoming_receiver(this);
  connector_->set_connection_error_handler(
      base::BindOnce(&ParentPort::Close, base::Unretained(this)));
}

void ParentPort::PostMessage(v8::Local<v8::Value> message_value) {
  if (!connector_closed_ && connector_ && connector_->is_valid()) {
    v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
    blink::TransferableMessage transferable_message;

    if (!electron::SerializeV8Value(isolate, message_value,
                                    &transferable_message)) {
      // SerializeV8Value sets an exception.