feat: MessagePorts in the main process (#22404)
This commit is contained in:
parent
c4c0888972
commit
b4d07f76d3
34 changed files with 1316 additions and 113 deletions
|
@ -3,6 +3,7 @@ module electron.mojom;
|
|||
import "mojo/public/mojom/base/string16.mojom";
|
||||
import "ui/gfx/geometry/mojom/geometry.mojom";
|
||||
import "third_party/blink/public/mojom/messaging/cloneable_message.mojom";
|
||||
import "third_party/blink/public/mojom/messaging/transferable_message.mojom";
|
||||
|
||||
interface ElectronRenderer {
|
||||
Message(
|
||||
|
@ -12,6 +13,8 @@ interface ElectronRenderer {
|
|||
blink.mojom.CloneableMessage arguments,
|
||||
int32 sender_id);
|
||||
|
||||
ReceivePostMessage(string channel, blink.mojom.TransferableMessage message);
|
||||
|
||||
UpdateCrashpadPipeName(string pipe_name);
|
||||
|
||||
// This is an API specific to the "remote" module, and will ultimately be
|
||||
|
@ -53,6 +56,8 @@ interface ElectronBrowser {
|
|||
string channel,
|
||||
blink.mojom.CloneableMessage arguments) => (blink.mojom.CloneableMessage result);
|
||||
|
||||
ReceivePostMessage(string channel, blink.mojom.TransferableMessage message);
|
||||
|
||||
// Emits an event on |channel| from the ipcMain JavaScript object in the main
|
||||
// process, and waits synchronously for a response.
|
||||
[Sync]
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
#include "shell/common/gin_converters/value_converter.h"
|
||||
#include "shell/common/gin_helper/dictionary.h"
|
||||
#include "shell/common/keyboard_util.h"
|
||||
#include "shell/common/v8_value_serializer.h"
|
||||
#include "third_party/blink/public/common/context_menu_data/edit_flags.h"
|
||||
#include "third_party/blink/public/common/input/web_input_event.h"
|
||||
#include "third_party/blink/public/common/input/web_keyboard_event.h"
|
||||
|
@ -481,98 +482,16 @@ bool Converter<network::mojom::ReferrerPolicy>::FromV8(
|
|||
return true;
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
class V8Serializer : public v8::ValueSerializer::Delegate {
|
||||
public:
|
||||
explicit V8Serializer(v8::Isolate* isolate)
|
||||
: isolate_(isolate), serializer_(isolate, this) {}
|
||||
~V8Serializer() override = default;
|
||||
|
||||
bool Serialize(v8::Local<v8::Value> value, blink::CloneableMessage* out) {
|
||||
serializer_.WriteHeader();
|
||||
bool wrote_value;
|
||||
if (!serializer_.WriteValue(isolate_->GetCurrentContext(), value)
|
||||
.To(&wrote_value)) {
|
||||
isolate_->ThrowException(v8::Exception::Error(
|
||||
StringToV8(isolate_, "An object could not be cloned.")));
|
||||
return false;
|
||||
}
|
||||
DCHECK(wrote_value);
|
||||
|
||||
std::pair<uint8_t*, size_t> buffer = serializer_.Release();
|
||||
DCHECK_EQ(buffer.first, data_.data());
|
||||
out->encoded_message = base::make_span(buffer.first, buffer.second);
|
||||
out->owned_encoded_message = std::move(data_);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// v8::ValueSerializer::Delegate
|
||||
void* ReallocateBufferMemory(void* old_buffer,
|
||||
size_t size,
|
||||
size_t* actual_size) override {
|
||||
DCHECK_EQ(old_buffer, data_.data());
|
||||
data_.resize(size);
|
||||
*actual_size = data_.capacity();
|
||||
return data_.data();
|
||||
}
|
||||
|
||||
void FreeBufferMemory(void* buffer) override {
|
||||
DCHECK_EQ(buffer, data_.data());
|
||||
data_ = {};
|
||||
}
|
||||
|
||||
void ThrowDataCloneError(v8::Local<v8::String> message) override {
|
||||
isolate_->ThrowException(v8::Exception::Error(message));
|
||||
}
|
||||
|
||||
private:
|
||||
v8::Isolate* isolate_;
|
||||
std::vector<uint8_t> data_;
|
||||
v8::ValueSerializer serializer_;
|
||||
};
|
||||
|
||||
class V8Deserializer : public v8::ValueDeserializer::Delegate {
|
||||
public:
|
||||
V8Deserializer(v8::Isolate* isolate, const blink::CloneableMessage& message)
|
||||
: isolate_(isolate),
|
||||
deserializer_(isolate,
|
||||
message.encoded_message.data(),
|
||||
message.encoded_message.size(),
|
||||
this) {}
|
||||
|
||||
v8::Local<v8::Value> Deserialize() {
|
||||
v8::EscapableHandleScope scope(isolate_);
|
||||
auto context = isolate_->GetCurrentContext();
|
||||
bool read_header;
|
||||
if (!deserializer_.ReadHeader(context).To(&read_header))
|
||||
return v8::Null(isolate_);
|
||||
DCHECK(read_header);
|
||||
v8::Local<v8::Value> value;
|
||||
if (!deserializer_.ReadValue(context).ToLocal(&value)) {
|
||||
return v8::Null(isolate_);
|
||||
}
|
||||
return scope.Escape(value);
|
||||
}
|
||||
|
||||
private:
|
||||
v8::Isolate* isolate_;
|
||||
v8::ValueDeserializer deserializer_;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
v8::Local<v8::Value> Converter<blink::CloneableMessage>::ToV8(
|
||||
v8::Isolate* isolate,
|
||||
const blink::CloneableMessage& in) {
|
||||
return V8Deserializer(isolate, in).Deserialize();
|
||||
return electron::DeserializeV8Value(isolate, in);
|
||||
}
|
||||
|
||||
bool Converter<blink::CloneableMessage>::FromV8(v8::Isolate* isolate,
|
||||
v8::Handle<v8::Value> val,
|
||||
blink::CloneableMessage* out) {
|
||||
return V8Serializer(isolate).Serialize(val, out);
|
||||
return electron::SerializeV8Value(isolate, val, out);
|
||||
}
|
||||
|
||||
} // namespace gin
|
||||
|
|
|
@ -37,7 +37,7 @@ v8::Persistent<v8::FunctionTemplate> g_call_translater;
|
|||
void CallTranslater(v8::Local<v8::External> external,
|
||||
v8::Local<v8::Object> state,
|
||||
gin::Arguments* args) {
|
||||
// Whether the callback should only be called for once.
|
||||
// Whether the callback should only be called once.
|
||||
v8::Isolate* isolate = args->isolate();
|
||||
auto context = isolate->GetCurrentContext();
|
||||
bool one_time =
|
||||
|
@ -47,7 +47,7 @@ void CallTranslater(v8::Local<v8::External> external,
|
|||
if (one_time) {
|
||||
auto called_symbol = gin::StringToSymbol(isolate, "called");
|
||||
if (state->Has(context, called_symbol).ToChecked()) {
|
||||
args->ThrowTypeError("callback can only be called for once");
|
||||
args->ThrowTypeError("One-time callback was called more than once");
|
||||
return;
|
||||
} else {
|
||||
state->Set(context, called_symbol, v8::Boolean::New(isolate, true))
|
||||
|
|
42
shell/common/gin_helper/function_template_extensions.h
Normal file
42
shell/common/gin_helper/function_template_extensions.h
Normal file
|
@ -0,0 +1,42 @@
|
|||
// Copyright 2020 Slack Technologies, Inc.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE.chromium file.
|
||||
|
||||
#ifndef SHELL_COMMON_GIN_HELPER_FUNCTION_TEMPLATE_EXTENSIONS_H_
|
||||
#define SHELL_COMMON_GIN_HELPER_FUNCTION_TEMPLATE_EXTENSIONS_H_
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "gin/function_template.h"
|
||||
#include "shell/common/gin_helper/error_thrower.h"
|
||||
|
||||
// This extends the functionality in //gin/function_template.h for "special"
|
||||
// arguments to gin-bound methods.
|
||||
// It's the counterpart to function_template.h, which includes these methods
|
||||
// in the gin_helper namespace.
|
||||
namespace gin {
|
||||
|
||||
// Support base::Optional as an argument.
|
||||
template <typename T>
|
||||
bool GetNextArgument(Arguments* args,
|
||||
const InvokerOptions& invoker_options,
|
||||
bool is_first,
|
||||
base::Optional<T>* result) {
|
||||
T converted;
|
||||
// Use gin::Arguments::GetNext which always advances |next| counter.
|
||||
if (args->GetNext(&converted))
|
||||
result->emplace(std::move(converted));
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool GetNextArgument(Arguments* args,
|
||||
const InvokerOptions& invoker_options,
|
||||
bool is_first,
|
||||
gin_helper::ErrorThrower* result) {
|
||||
*result = gin_helper::ErrorThrower(args->isolate());
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace gin
|
||||
|
||||
#endif // SHELL_COMMON_GIN_HELPER_FUNCTION_TEMPLATE_EXTENSIONS_H_
|
|
@ -44,6 +44,7 @@
|
|||
V(electron_browser_global_shortcut) \
|
||||
V(electron_browser_in_app_purchase) \
|
||||
V(electron_browser_menu) \
|
||||
V(electron_browser_message_port) \
|
||||
V(electron_browser_net) \
|
||||
V(electron_browser_power_monitor) \
|
||||
V(electron_browser_power_save_blocker) \
|
||||
|
|
147
shell/common/v8_value_serializer.cc
Normal file
147
shell/common/v8_value_serializer.cc
Normal file
|
@ -0,0 +1,147 @@
|
|||
// 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.
|
||||
|
||||
#include "shell/common/v8_value_serializer.h"
|
||||
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "gin/converter.h"
|
||||
#include "third_party/blink/public/common/messaging/cloneable_message.h"
|
||||
#include "v8/include/v8.h"
|
||||
|
||||
namespace electron {
|
||||
|
||||
namespace {
|
||||
const uint8_t kVersionTag = 0xFF;
|
||||
} // namespace
|
||||
|
||||
class V8Serializer : public v8::ValueSerializer::Delegate {
|
||||
public:
|
||||
explicit V8Serializer(v8::Isolate* isolate)
|
||||
: isolate_(isolate), serializer_(isolate, this) {}
|
||||
~V8Serializer() override = default;
|
||||
|
||||
bool Serialize(v8::Local<v8::Value> value, blink::CloneableMessage* out) {
|
||||
WriteBlinkEnvelope(19);
|
||||
|
||||
serializer_.WriteHeader();
|
||||
bool wrote_value;
|
||||
if (!serializer_.WriteValue(isolate_->GetCurrentContext(), value)
|
||||
.To(&wrote_value)) {
|
||||
isolate_->ThrowException(v8::Exception::Error(
|
||||
gin::StringToV8(isolate_, "An object could not be cloned.")));
|
||||
return false;
|
||||
}
|
||||
DCHECK(wrote_value);
|
||||
|
||||
std::pair<uint8_t*, size_t> buffer = serializer_.Release();
|
||||
DCHECK_EQ(buffer.first, data_.data());
|
||||
out->encoded_message = base::make_span(buffer.first, buffer.second);
|
||||
out->owned_encoded_message = std::move(data_);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// v8::ValueSerializer::Delegate
|
||||
void* ReallocateBufferMemory(void* old_buffer,
|
||||
size_t size,
|
||||
size_t* actual_size) override {
|
||||
DCHECK_EQ(old_buffer, data_.data());
|
||||
data_.resize(size);
|
||||
*actual_size = data_.capacity();
|
||||
return data_.data();
|
||||
}
|
||||
|
||||
void FreeBufferMemory(void* buffer) override {
|
||||
DCHECK_EQ(buffer, data_.data());
|
||||
data_ = {};
|
||||
}
|
||||
|
||||
void ThrowDataCloneError(v8::Local<v8::String> message) override {
|
||||
isolate_->ThrowException(v8::Exception::Error(message));
|
||||
}
|
||||
|
||||
private:
|
||||
void WriteTag(uint8_t tag) { serializer_.WriteRawBytes(&tag, 1); }
|
||||
|
||||
void WriteBlinkEnvelope(uint32_t blink_version) {
|
||||
// Write a dummy blink version envelope for compatibility with
|
||||
// blink::V8ScriptValueSerializer
|
||||
WriteTag(kVersionTag);
|
||||
serializer_.WriteUint32(blink_version);
|
||||
}
|
||||
|
||||
v8::Isolate* isolate_;
|
||||
std::vector<uint8_t> data_;
|
||||
v8::ValueSerializer serializer_;
|
||||
};
|
||||
|
||||
class V8Deserializer : public v8::ValueDeserializer::Delegate {
|
||||
public:
|
||||
V8Deserializer(v8::Isolate* isolate, base::span<const uint8_t> data)
|
||||
: isolate_(isolate),
|
||||
deserializer_(isolate, data.data(), data.size(), this) {}
|
||||
V8Deserializer(v8::Isolate* isolate, const blink::CloneableMessage& message)
|
||||
: V8Deserializer(isolate, message.encoded_message) {}
|
||||
|
||||
v8::Local<v8::Value> Deserialize() {
|
||||
v8::EscapableHandleScope scope(isolate_);
|
||||
auto context = isolate_->GetCurrentContext();
|
||||
|
||||
uint32_t blink_version;
|
||||
if (!ReadBlinkEnvelope(&blink_version))
|
||||
return v8::Null(isolate_);
|
||||
|
||||
bool read_header;
|
||||
if (!deserializer_.ReadHeader(context).To(&read_header))
|
||||
return v8::Null(isolate_);
|
||||
DCHECK(read_header);
|
||||
v8::Local<v8::Value> value;
|
||||
if (!deserializer_.ReadValue(context).ToLocal(&value))
|
||||
return v8::Null(isolate_);
|
||||
return scope.Escape(value);
|
||||
}
|
||||
|
||||
private:
|
||||
bool ReadTag(uint8_t* tag) {
|
||||
const void* tag_bytes = nullptr;
|
||||
if (!deserializer_.ReadRawBytes(1, &tag_bytes))
|
||||
return false;
|
||||
*tag = *reinterpret_cast<const uint8_t*>(tag_bytes);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ReadBlinkEnvelope(uint32_t* blink_version) {
|
||||
// Read a dummy blink version envelope for compatibility with
|
||||
// blink::V8ScriptValueDeserializer
|
||||
uint8_t tag = 0;
|
||||
if (!ReadTag(&tag) || tag != kVersionTag)
|
||||
return false;
|
||||
if (!deserializer_.ReadUint32(blink_version))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
v8::Isolate* isolate_;
|
||||
v8::ValueDeserializer deserializer_;
|
||||
};
|
||||
|
||||
bool SerializeV8Value(v8::Isolate* isolate,
|
||||
v8::Local<v8::Value> value,
|
||||
blink::CloneableMessage* out) {
|
||||
return V8Serializer(isolate).Serialize(value, out);
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> DeserializeV8Value(v8::Isolate* isolate,
|
||||
const blink::CloneableMessage& in) {
|
||||
return V8Deserializer(isolate, in).Deserialize();
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> DeserializeV8Value(v8::Isolate* isolate,
|
||||
base::span<const uint8_t> data) {
|
||||
return V8Deserializer(isolate, data).Deserialize();
|
||||
}
|
||||
|
||||
} // namespace electron
|
33
shell/common/v8_value_serializer.h
Normal file
33
shell/common/v8_value_serializer.h
Normal file
|
@ -0,0 +1,33 @@
|
|||
// 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.
|
||||
|
||||
#ifndef SHELL_COMMON_V8_VALUE_SERIALIZER_H_
|
||||
#define SHELL_COMMON_V8_VALUE_SERIALIZER_H_
|
||||
|
||||
#include "base/containers/span.h"
|
||||
|
||||
namespace v8 {
|
||||
class Isolate;
|
||||
template <class T>
|
||||
class Local;
|
||||
class Value;
|
||||
} // namespace v8
|
||||
|
||||
namespace blink {
|
||||
struct CloneableMessage;
|
||||
}
|
||||
|
||||
namespace electron {
|
||||
|
||||
bool SerializeV8Value(v8::Isolate* isolate,
|
||||
v8::Local<v8::Value> value,
|
||||
blink::CloneableMessage* out);
|
||||
v8::Local<v8::Value> DeserializeV8Value(v8::Isolate* isolate,
|
||||
const blink::CloneableMessage& in);
|
||||
v8::Local<v8::Value> DeserializeV8Value(v8::Isolate* isolate,
|
||||
base::span<const uint8_t> data);
|
||||
|
||||
} // namespace electron
|
||||
|
||||
#endif // SHELL_COMMON_V8_VALUE_SERIALIZER_H_
|
Loading…
Add table
Add a link
Reference in a new issue