electron/shell/common/v8_value_serializer.cc
Electron Bot 2eb3bddb05
chore: bump chromium to 92.0.4505.0 (master) (#29058)
* chore: bump chromium in DEPS to 92.0.4500.2

* resolve conflicts

* update patches

* chore: cherry-pick 82434206f306 from chromium (#29060)

* fix patch

* chore: bump chromium in DEPS to 92.0.4501.0

* chore: bump chromium in DEPS to 92.0.4502.0

* chore: bump chromium in DEPS to 92.0.4503.0

* chore: update patches

* 2869869: [Code Health] Refactor ListValue::Insert in gpu compositor

https://chromium-review.googlesource.com/c/chromium/src/+/2869869

* 2877924: Separate InkDropHost from InkDropHostView

https://chromium-review.googlesource.com/c/chromium/src/+/2877924

* chore: bump chromium in DEPS to 92.0.4504.0

* update patches

* Fixup for Separate InkDropHost from InkDropHostView

https://chromium-review.googlesource.com/c/chromium/src/+/2877924

* 2873469: Compute hashes of .pak files during the build, and check it at runtime.

https://chromium-review.googlesource.com/c/chromium/src/+/2873469

* 2874397: Remove flag to disable microtasks scope consistency checks

https://chromium-review.googlesource.com/c/v8/v8/+/2874397

* 2881471: Remove unneeded trace_event.h includes in headers.

https://chromium-review.googlesource.com/c/chromium/src/+/2881471

* 2844717: [Keyboard Tooltip] Rename RWHV*::SetTooltipText to UpdateTooltipUnderCursor

https://chromium-review.googlesource.com/c/chromium/src/+/2844717

* chore: bump chromium in DEPS to 92.0.4505.0

* chore: update patches

* 2883887: Retire ScopedObserver in /chrome/browser/predictors.

https://chromium-review.googlesource.com/c/chromium/src/+/2883887

* 2883694: Retire ScopedObserver in /chrome/browser.

https://chromium-review.googlesource.com/c/chromium/src/+/2883694

* fixup after merge

* fixup: Remove flag to disable microtasks scope consistency checks

* Temporarily disable setcallhandler-test.js nan test

This test should be renabled once https://github.com/electron/electron/pull/29028 lands

* Use gin_helper::MicrotasksScope instead of v8::MicrotasksScope

* chore: bump chromium in DEPS to 92.0.4506.0

* update patches

* Revert "update patches"

This reverts commit 333ec0d4c205bd3cbee28d2bc3d068871dbb900a.

* Revert "chore: bump chromium in DEPS to 92.0.4506.0"

This reverts commit 2bd52f8cd89b173c8b15a61d74fa7539cdbf574b.

* Fixup: Use gin_helper::MicrotasksScope instead of v8::MicrotasksScope

* Fixup: Use gin_helper::MicrotasksScope instead of v8::MicrotasksScope

Co-authored-by: Jeremy Rose <nornagon@nornagon.net>
Co-authored-by: Jeremy Rose <jeremya@chromium.org>
Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>
Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>
2021-05-13 21:21:36 -04:00

150 lines
4.6 KiB
C++

// 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 "shell/common/gin_helper/microtasks_scope.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) {
gin_helper::MicrotasksScope microtasks_scope(
isolate_, v8::MicrotasksScope::kDoNotRunMicrotasks);
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