2019-10-18 19:57:09 +00:00
|
|
|
// Copyright (c) 2019 Slack Technologies, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
#include "shell/renderer/api/electron_api_context_bridge.h"
|
2019-10-18 19:57:09 +00:00
|
|
|
|
2019-10-31 07:56:00 +00:00
|
|
|
#include <map>
|
2020-04-29 22:51:41 +00:00
|
|
|
#include <memory>
|
2019-10-18 19:57:09 +00:00
|
|
|
#include <set>
|
2019-10-31 07:56:00 +00:00
|
|
|
#include <string>
|
2019-10-18 19:57:09 +00:00
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "base/no_destructor.h"
|
|
|
|
#include "base/strings/string_number_conversions.h"
|
2019-10-31 07:56:00 +00:00
|
|
|
#include "content/public/renderer/render_frame.h"
|
|
|
|
#include "content/public/renderer/render_frame_observer.h"
|
2020-05-12 16:32:04 +00:00
|
|
|
#include "shell/common/api/object_life_monitor.h"
|
2019-10-31 07:56:00 +00:00
|
|
|
#include "shell/common/gin_converters/blink_converter.h"
|
|
|
|
#include "shell/common/gin_converters/callback_converter.h"
|
|
|
|
#include "shell/common/gin_helper/dictionary.h"
|
2019-11-01 06:10:32 +00:00
|
|
|
#include "shell/common/gin_helper/promise.h"
|
2019-10-31 07:56:00 +00:00
|
|
|
#include "shell/common/node_includes.h"
|
2020-02-24 04:33:58 +00:00
|
|
|
#include "shell/common/world_ids.h"
|
2019-10-31 07:56:00 +00:00
|
|
|
#include "third_party/blink/public/web/web_local_frame.h"
|
2019-10-18 19:57:09 +00:00
|
|
|
|
|
|
|
namespace electron {
|
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
2020-05-15 18:57:40 +00:00
|
|
|
namespace context_bridge {
|
|
|
|
|
|
|
|
const char* const kProxyFunctionPrivateKey = "electron_contextBridge_proxy_fn";
|
|
|
|
const char* const kSupportsDynamicPropertiesPrivateKey =
|
|
|
|
"electron_contextBridge_supportsDynamicProperties";
|
|
|
|
|
|
|
|
} // namespace context_bridge
|
|
|
|
|
2019-10-18 19:57:09 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
static int kMaxRecursion = 1000;
|
|
|
|
|
2019-10-31 07:56:00 +00:00
|
|
|
// Returns true if |maybe| is both a value, and that value is true.
|
|
|
|
inline bool IsTrue(v8::Maybe<bool> maybe) {
|
|
|
|
return maybe.IsJust() && maybe.FromJust();
|
|
|
|
}
|
|
|
|
|
2019-10-18 19:57:09 +00:00
|
|
|
content::RenderFrame* GetRenderFrame(const v8::Local<v8::Object>& value) {
|
|
|
|
v8::Local<v8::Context> context = value->CreationContext();
|
|
|
|
if (context.IsEmpty())
|
|
|
|
return nullptr;
|
|
|
|
blink::WebLocalFrame* frame = blink::WebLocalFrame::FrameForContext(context);
|
|
|
|
if (!frame)
|
|
|
|
return nullptr;
|
|
|
|
return content::RenderFrame::FromWebFrame(frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sourced from "extensions/renderer/v8_schema_registry.cc"
|
|
|
|
// Recursively freezes every v8 object on |object|.
|
|
|
|
bool DeepFreeze(const v8::Local<v8::Object>& object,
|
|
|
|
const v8::Local<v8::Context>& context,
|
|
|
|
std::set<int> frozen = std::set<int>()) {
|
|
|
|
int hash = object->GetIdentityHash();
|
|
|
|
if (frozen.find(hash) != frozen.end())
|
|
|
|
return true;
|
|
|
|
frozen.insert(hash);
|
|
|
|
|
|
|
|
v8::Local<v8::Array> property_names =
|
|
|
|
object->GetOwnPropertyNames(context).ToLocalChecked();
|
|
|
|
for (uint32_t i = 0; i < property_names->Length(); ++i) {
|
|
|
|
v8::Local<v8::Value> child =
|
|
|
|
object->Get(context, property_names->Get(context, i).ToLocalChecked())
|
|
|
|
.ToLocalChecked();
|
|
|
|
if (child->IsObject() && !child->IsTypedArray()) {
|
|
|
|
if (!DeepFreeze(v8::Local<v8::Object>::Cast(child), context, frozen))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-10-31 07:56:00 +00:00
|
|
|
return IsTrue(
|
2019-10-18 19:57:09 +00:00
|
|
|
object->SetIntegrityLevel(context, v8::IntegrityLevel::kFrozen));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsPlainObject(const v8::Local<v8::Value>& object) {
|
|
|
|
if (!object->IsObject())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return !(object->IsNullOrUndefined() || object->IsDate() ||
|
|
|
|
object->IsArgumentsObject() || object->IsBigIntObject() ||
|
|
|
|
object->IsBooleanObject() || object->IsNumberObject() ||
|
|
|
|
object->IsStringObject() || object->IsSymbolObject() ||
|
|
|
|
object->IsNativeError() || object->IsRegExp() ||
|
|
|
|
object->IsPromise() || object->IsMap() || object->IsSet() ||
|
|
|
|
object->IsMapIterator() || object->IsSetIterator() ||
|
|
|
|
object->IsWeakMap() || object->IsWeakSet() ||
|
|
|
|
object->IsArrayBuffer() || object->IsArrayBufferView() ||
|
|
|
|
object->IsArray() || object->IsDataView() ||
|
|
|
|
object->IsSharedArrayBuffer() || object->IsProxy() ||
|
2020-03-03 21:35:05 +00:00
|
|
|
object->IsWasmModuleObject() || object->IsModuleNamespaceObject());
|
2019-10-18 19:57:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IsPlainArray(const v8::Local<v8::Value>& arr) {
|
|
|
|
if (!arr->IsArray())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return !arr->IsTypedArray();
|
|
|
|
}
|
|
|
|
|
2020-05-15 18:57:40 +00:00
|
|
|
void SetPrivate(v8::Local<v8::Context> context,
|
|
|
|
v8::Local<v8::Object> target,
|
|
|
|
const std::string& key,
|
|
|
|
v8::Local<v8::Value> value) {
|
|
|
|
target
|
|
|
|
->SetPrivate(
|
|
|
|
context,
|
|
|
|
v8::Private::ForApi(context->GetIsolate(),
|
|
|
|
gin::StringToV8(context->GetIsolate(), key)),
|
|
|
|
value)
|
|
|
|
.Check();
|
|
|
|
}
|
2019-10-18 19:57:09 +00:00
|
|
|
|
2020-05-15 18:57:40 +00:00
|
|
|
v8::MaybeLocal<v8::Value> GetPrivate(v8::Local<v8::Context> context,
|
|
|
|
v8::Local<v8::Object> target,
|
|
|
|
const std::string& key) {
|
|
|
|
return target->GetPrivate(
|
|
|
|
context,
|
|
|
|
v8::Private::ForApi(context->GetIsolate(),
|
|
|
|
gin::StringToV8(context->GetIsolate(), key)));
|
|
|
|
}
|
2019-10-18 19:57:09 +00:00
|
|
|
|
2020-07-23 21:32:38 +00:00
|
|
|
// Where the context bridge should create the exception it is about to throw
|
|
|
|
enum BridgeErrorTarget {
|
|
|
|
// The source / calling context. This is default and correct 99% of the time,
|
|
|
|
// the caller / context asking for the conversion will receive the error and
|
|
|
|
// therefore the error should be made in that context
|
|
|
|
kSource,
|
|
|
|
// The destination / target context. This should only be used when the source
|
|
|
|
// won't catch the error that results from the value it is passing over the
|
|
|
|
// bridge. This can **only** occur when returning a value from a function as
|
|
|
|
// we convert the return value after the method has terminated and execution
|
|
|
|
// has been returned to the caller. In this scenario the error will the be
|
|
|
|
// catchable in the "destination" context and therefore we create the error
|
|
|
|
// there.
|
|
|
|
kDestination
|
|
|
|
};
|
|
|
|
|
2019-10-18 19:57:09 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
v8::MaybeLocal<v8::Value> PassValueToOtherContext(
|
|
|
|
v8::Local<v8::Context> source_context,
|
|
|
|
v8::Local<v8::Context> destination_context,
|
|
|
|
v8::Local<v8::Value> value,
|
2020-03-04 07:18:22 +00:00
|
|
|
context_bridge::ObjectCache* object_cache,
|
2020-04-27 19:46:04 +00:00
|
|
|
bool support_dynamic_properties,
|
2020-07-23 21:32:38 +00:00
|
|
|
int recursion_depth,
|
|
|
|
BridgeErrorTarget error_target = BridgeErrorTarget::kSource) {
|
2020-05-11 21:24:43 +00:00
|
|
|
TRACE_EVENT0("electron", "ContextBridge::PassValueToOtherContext");
|
2019-10-18 19:57:09 +00:00
|
|
|
if (recursion_depth >= kMaxRecursion) {
|
2020-07-23 21:32:38 +00:00
|
|
|
v8::Context::Scope error_scope(error_target == BridgeErrorTarget::kSource
|
|
|
|
? source_context
|
|
|
|
: destination_context);
|
2020-05-11 20:41:42 +00:00
|
|
|
source_context->GetIsolate()->ThrowException(v8::Exception::TypeError(
|
|
|
|
gin::StringToV8(source_context->GetIsolate(),
|
|
|
|
"Electron contextBridge recursion depth exceeded. "
|
|
|
|
"Nested objects "
|
|
|
|
"deeper than 1000 are not supported.")));
|
|
|
|
return v8::MaybeLocal<v8::Value>();
|
2019-10-18 19:57:09 +00:00
|
|
|
}
|
2020-07-15 01:38:54 +00:00
|
|
|
|
|
|
|
// Certain primitives always use the current contexts prototype and we can
|
|
|
|
// pass these through directly which is significantly more performant than
|
|
|
|
// copying them. This list of primitives is based on the classification of
|
|
|
|
// "primitive value" as defined in the ECMA262 spec
|
|
|
|
// https://tc39.es/ecma262/#sec-primitive-value
|
|
|
|
if (value->IsString() || value->IsNumber() || value->IsNullOrUndefined() ||
|
|
|
|
value->IsBoolean() || value->IsSymbol() || value->IsBigInt()) {
|
|
|
|
return v8::MaybeLocal<v8::Value>(value);
|
|
|
|
}
|
|
|
|
|
2019-10-18 19:57:09 +00:00
|
|
|
// Check Cache
|
2020-03-04 07:18:22 +00:00
|
|
|
auto cached_value = object_cache->GetCachedProxiedObject(value);
|
2019-10-18 19:57:09 +00:00
|
|
|
if (!cached_value.IsEmpty()) {
|
|
|
|
return cached_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Proxy functions and monitor the lifetime in the new context to release
|
|
|
|
// the global handle at the right time.
|
|
|
|
if (value->IsFunction()) {
|
|
|
|
auto func = v8::Local<v8::Function>::Cast(value);
|
|
|
|
|
|
|
|
{
|
2020-05-11 20:41:42 +00:00
|
|
|
v8::Context::Scope destination_scope(destination_context);
|
2020-05-15 18:57:40 +00:00
|
|
|
v8::Local<v8::Object> state =
|
|
|
|
v8::Object::New(destination_context->GetIsolate());
|
|
|
|
SetPrivate(destination_context, state,
|
|
|
|
context_bridge::kProxyFunctionPrivateKey, func);
|
|
|
|
SetPrivate(destination_context, state,
|
|
|
|
context_bridge::kSupportsDynamicPropertiesPrivateKey,
|
|
|
|
gin::ConvertToV8(destination_context->GetIsolate(),
|
|
|
|
support_dynamic_properties));
|
|
|
|
v8::Local<v8::Value> proxy_func;
|
|
|
|
if (!v8::Function::New(destination_context, ProxyFunctionWrapper, state)
|
|
|
|
.ToLocal(&proxy_func))
|
|
|
|
return v8::MaybeLocal<v8::Value>();
|
2020-03-04 07:18:22 +00:00
|
|
|
object_cache->CacheProxiedObject(value, proxy_func);
|
2019-10-18 19:57:09 +00:00
|
|
|
return v8::MaybeLocal<v8::Value>(proxy_func);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Proxy promises as they have a safe and guaranteed memory lifecycle
|
|
|
|
if (value->IsPromise()) {
|
|
|
|
v8::Context::Scope destination_scope(destination_context);
|
2020-05-11 20:41:42 +00:00
|
|
|
auto source_promise = v8::Local<v8::Promise>::Cast(value);
|
|
|
|
// Make the promise a shared_ptr so that when the original promise is
|
|
|
|
// freed the proxy promise is correctly freed as well instead of being
|
|
|
|
// left dangling
|
|
|
|
std::shared_ptr<gin_helper::Promise<v8::Local<v8::Value>>> proxied_promise(
|
|
|
|
new gin_helper::Promise<v8::Local<v8::Value>>(
|
|
|
|
destination_context->GetIsolate()));
|
|
|
|
v8::Local<v8::Promise> proxied_promise_handle =
|
|
|
|
proxied_promise->GetHandle();
|
|
|
|
|
|
|
|
v8::Global<v8::Context> global_then_source_context(
|
|
|
|
source_context->GetIsolate(), source_context);
|
|
|
|
v8::Global<v8::Context> global_then_destination_context(
|
|
|
|
destination_context->GetIsolate(), destination_context);
|
|
|
|
global_then_source_context.SetWeak();
|
|
|
|
global_then_destination_context.SetWeak();
|
|
|
|
auto then_cb = base::BindOnce(
|
|
|
|
[](std::shared_ptr<gin_helper::Promise<v8::Local<v8::Value>>>
|
|
|
|
proxied_promise,
|
|
|
|
v8::Isolate* isolate, v8::Global<v8::Context> global_source_context,
|
|
|
|
v8::Global<v8::Context> global_destination_context,
|
|
|
|
v8::Local<v8::Value> result) {
|
|
|
|
if (global_source_context.IsEmpty() ||
|
|
|
|
global_destination_context.IsEmpty())
|
|
|
|
return;
|
|
|
|
context_bridge::ObjectCache object_cache;
|
|
|
|
auto val =
|
|
|
|
PassValueToOtherContext(global_source_context.Get(isolate),
|
|
|
|
global_destination_context.Get(isolate),
|
2020-05-15 18:57:40 +00:00
|
|
|
result, &object_cache, false, 0);
|
2020-05-11 20:41:42 +00:00
|
|
|
if (!val.IsEmpty())
|
|
|
|
proxied_promise->Resolve(val.ToLocalChecked());
|
|
|
|
},
|
|
|
|
proxied_promise, destination_context->GetIsolate(),
|
|
|
|
std::move(global_then_source_context),
|
2020-05-15 18:57:40 +00:00
|
|
|
std::move(global_then_destination_context));
|
2020-05-11 20:41:42 +00:00
|
|
|
|
|
|
|
v8::Global<v8::Context> global_catch_source_context(
|
|
|
|
source_context->GetIsolate(), source_context);
|
|
|
|
v8::Global<v8::Context> global_catch_destination_context(
|
|
|
|
destination_context->GetIsolate(), destination_context);
|
|
|
|
global_catch_source_context.SetWeak();
|
|
|
|
global_catch_destination_context.SetWeak();
|
|
|
|
auto catch_cb = base::BindOnce(
|
|
|
|
[](std::shared_ptr<gin_helper::Promise<v8::Local<v8::Value>>>
|
|
|
|
proxied_promise,
|
|
|
|
v8::Isolate* isolate, v8::Global<v8::Context> global_source_context,
|
|
|
|
v8::Global<v8::Context> global_destination_context,
|
|
|
|
v8::Local<v8::Value> result) {
|
|
|
|
if (global_source_context.IsEmpty() ||
|
|
|
|
global_destination_context.IsEmpty())
|
|
|
|
return;
|
|
|
|
context_bridge::ObjectCache object_cache;
|
|
|
|
auto val =
|
|
|
|
PassValueToOtherContext(global_source_context.Get(isolate),
|
|
|
|
global_destination_context.Get(isolate),
|
2020-05-15 18:57:40 +00:00
|
|
|
result, &object_cache, false, 0);
|
2020-05-11 20:41:42 +00:00
|
|
|
if (!val.IsEmpty())
|
|
|
|
proxied_promise->Reject(val.ToLocalChecked());
|
|
|
|
},
|
|
|
|
proxied_promise, destination_context->GetIsolate(),
|
|
|
|
std::move(global_catch_source_context),
|
2020-05-15 18:57:40 +00:00
|
|
|
std::move(global_catch_destination_context));
|
2020-05-11 20:41:42 +00:00
|
|
|
|
|
|
|
ignore_result(source_promise->Then(
|
|
|
|
source_context,
|
|
|
|
v8::Local<v8::Function>::Cast(
|
|
|
|
gin::ConvertToV8(destination_context->GetIsolate(), then_cb)),
|
|
|
|
v8::Local<v8::Function>::Cast(
|
|
|
|
gin::ConvertToV8(destination_context->GetIsolate(), catch_cb))));
|
|
|
|
|
|
|
|
object_cache->CacheProxiedObject(value, proxied_promise_handle);
|
|
|
|
return v8::MaybeLocal<v8::Value>(proxied_promise_handle);
|
2019-10-18 19:57:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Errors aren't serializable currently, we need to pull the message out and
|
|
|
|
// re-construct in the destination context
|
|
|
|
if (value->IsNativeError()) {
|
|
|
|
v8::Context::Scope destination_context_scope(destination_context);
|
|
|
|
return v8::MaybeLocal<v8::Value>(v8::Exception::Error(
|
|
|
|
v8::Exception::CreateMessage(destination_context->GetIsolate(), value)
|
|
|
|
->Get()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Manually go through the array and pass each value individually into a new
|
|
|
|
// array so that functions deep inside arrays get proxied or arrays of
|
|
|
|
// promises are proxied correctly.
|
|
|
|
if (IsPlainArray(value)) {
|
|
|
|
v8::Context::Scope destination_context_scope(destination_context);
|
2020-05-11 20:41:42 +00:00
|
|
|
v8::Local<v8::Array> arr = v8::Local<v8::Array>::Cast(value);
|
|
|
|
size_t length = arr->Length();
|
|
|
|
v8::Local<v8::Array> cloned_arr =
|
|
|
|
v8::Array::New(destination_context->GetIsolate(), length);
|
|
|
|
for (size_t i = 0; i < length; i++) {
|
|
|
|
auto value_for_array = PassValueToOtherContext(
|
|
|
|
source_context, destination_context,
|
2020-05-15 18:57:40 +00:00
|
|
|
arr->Get(source_context, i).ToLocalChecked(), object_cache,
|
2020-05-11 20:41:42 +00:00
|
|
|
support_dynamic_properties, recursion_depth + 1);
|
|
|
|
if (value_for_array.IsEmpty())
|
|
|
|
return v8::MaybeLocal<v8::Value>();
|
|
|
|
|
|
|
|
if (!IsTrue(cloned_arr->Set(destination_context, static_cast<int>(i),
|
|
|
|
value_for_array.ToLocalChecked()))) {
|
|
|
|
return v8::MaybeLocal<v8::Value>();
|
2019-10-18 19:57:09 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-11 20:41:42 +00:00
|
|
|
object_cache->CacheProxiedObject(value, cloned_arr);
|
|
|
|
return v8::MaybeLocal<v8::Value>(cloned_arr);
|
2019-10-18 19:57:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Proxy all objects
|
|
|
|
if (IsPlainObject(value)) {
|
|
|
|
auto object_value = v8::Local<v8::Object>::Cast(value);
|
2020-04-27 19:46:04 +00:00
|
|
|
auto passed_value = CreateProxyForAPI(
|
2020-05-15 18:57:40 +00:00
|
|
|
object_value, source_context, destination_context, object_cache,
|
2020-04-27 19:46:04 +00:00
|
|
|
support_dynamic_properties, recursion_depth + 1);
|
2019-10-18 19:57:09 +00:00
|
|
|
if (passed_value.IsEmpty())
|
|
|
|
return v8::MaybeLocal<v8::Value>();
|
|
|
|
return v8::MaybeLocal<v8::Value>(passed_value.ToLocalChecked());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serializable objects
|
|
|
|
blink::CloneableMessage ret;
|
|
|
|
{
|
2020-07-23 21:32:38 +00:00
|
|
|
v8::Local<v8::Context> error_context =
|
|
|
|
error_target == BridgeErrorTarget::kSource ? source_context
|
|
|
|
: destination_context;
|
|
|
|
v8::Context::Scope error_scope(error_context);
|
2020-05-11 20:41:42 +00:00
|
|
|
// V8 serializer will throw an error if required
|
2020-07-23 21:32:38 +00:00
|
|
|
if (!gin::ConvertFromV8(error_context->GetIsolate(), value, &ret))
|
2020-05-11 20:41:42 +00:00
|
|
|
return v8::MaybeLocal<v8::Value>();
|
2019-10-18 19:57:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2020-05-11 20:41:42 +00:00
|
|
|
v8::Context::Scope destination_context_scope(destination_context);
|
2019-10-18 19:57:09 +00:00
|
|
|
v8::Local<v8::Value> cloned_value =
|
2019-10-31 07:56:00 +00:00
|
|
|
gin::ConvertToV8(destination_context->GetIsolate(), ret);
|
2020-03-04 07:18:22 +00:00
|
|
|
object_cache->CacheProxiedObject(value, cloned_value);
|
2019-10-18 19:57:09 +00:00
|
|
|
return v8::MaybeLocal<v8::Value>(cloned_value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-15 18:57:40 +00:00
|
|
|
void ProxyFunctionWrapper(const v8::FunctionCallbackInfo<v8::Value>& info) {
|
2020-05-11 21:24:43 +00:00
|
|
|
TRACE_EVENT0("electron", "ContextBridge::ProxyFunctionWrapper");
|
2020-05-15 18:57:40 +00:00
|
|
|
CHECK(info.Data()->IsObject());
|
|
|
|
v8::Local<v8::Object> data = info.Data().As<v8::Object>();
|
|
|
|
bool support_dynamic_properties = false;
|
|
|
|
gin::Arguments args(info);
|
2019-10-18 19:57:09 +00:00
|
|
|
// Context the proxy function was called from
|
2020-05-15 18:57:40 +00:00
|
|
|
v8::Local<v8::Context> calling_context = args.isolate()->GetCurrentContext();
|
|
|
|
|
|
|
|
// Pull the original function and its context off of the data private key
|
|
|
|
v8::MaybeLocal<v8::Value> sdp_value =
|
|
|
|
GetPrivate(calling_context, data,
|
|
|
|
context_bridge::kSupportsDynamicPropertiesPrivateKey);
|
|
|
|
v8::MaybeLocal<v8::Value> maybe_func = GetPrivate(
|
|
|
|
calling_context, data, context_bridge::kProxyFunctionPrivateKey);
|
|
|
|
v8::Local<v8::Value> func_value;
|
|
|
|
if (sdp_value.IsEmpty() || maybe_func.IsEmpty() ||
|
|
|
|
!gin::ConvertFromV8(args.isolate(), sdp_value.ToLocalChecked(),
|
|
|
|
&support_dynamic_properties) ||
|
|
|
|
!maybe_func.ToLocal(&func_value))
|
|
|
|
return;
|
|
|
|
|
|
|
|
v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(func_value);
|
|
|
|
v8::Local<v8::Context> func_owning_context = func->CreationContext();
|
2019-10-18 19:57:09 +00:00
|
|
|
|
|
|
|
{
|
2020-05-11 20:41:42 +00:00
|
|
|
v8::Context::Scope func_owning_context_scope(func_owning_context);
|
|
|
|
context_bridge::ObjectCache object_cache;
|
|
|
|
|
2019-10-18 19:57:09 +00:00
|
|
|
std::vector<v8::Local<v8::Value>> original_args;
|
|
|
|
std::vector<v8::Local<v8::Value>> proxied_args;
|
2020-05-15 18:57:40 +00:00
|
|
|
args.GetRemaining(&original_args);
|
2019-10-18 19:57:09 +00:00
|
|
|
|
|
|
|
for (auto value : original_args) {
|
2020-05-15 18:57:40 +00:00
|
|
|
auto arg =
|
|
|
|
PassValueToOtherContext(calling_context, func_owning_context, value,
|
|
|
|
&object_cache, support_dynamic_properties, 0);
|
2019-10-18 19:57:09 +00:00
|
|
|
if (arg.IsEmpty())
|
2020-05-15 18:57:40 +00:00
|
|
|
return;
|
2019-10-18 19:57:09 +00:00
|
|
|
proxied_args.push_back(arg.ToLocalChecked());
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::MaybeLocal<v8::Value> maybe_return_value;
|
|
|
|
bool did_error = false;
|
|
|
|
std::string error_message;
|
|
|
|
{
|
2020-05-15 18:57:40 +00:00
|
|
|
v8::TryCatch try_catch(args.isolate());
|
2019-10-18 19:57:09 +00:00
|
|
|
maybe_return_value = func->Call(func_owning_context, func,
|
|
|
|
proxied_args.size(), proxied_args.data());
|
|
|
|
if (try_catch.HasCaught()) {
|
|
|
|
did_error = true;
|
|
|
|
auto message = try_catch.Message();
|
|
|
|
|
|
|
|
if (message.IsEmpty() ||
|
2020-05-15 18:57:40 +00:00
|
|
|
!gin::ConvertFromV8(args.isolate(), message->Get(),
|
2019-10-31 07:56:00 +00:00
|
|
|
&error_message)) {
|
2019-10-18 19:57:09 +00:00
|
|
|
error_message =
|
|
|
|
"An unknown exception occurred in the isolated context, an error "
|
|
|
|
"occurred but a valid exception was not thrown.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (did_error) {
|
|
|
|
v8::Context::Scope calling_context_scope(calling_context);
|
2020-05-15 18:57:40 +00:00
|
|
|
args.isolate()->ThrowException(
|
|
|
|
v8::Exception::Error(gin::StringToV8(args.isolate(), error_message)));
|
|
|
|
return;
|
2019-10-18 19:57:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (maybe_return_value.IsEmpty())
|
2020-05-15 18:57:40 +00:00
|
|
|
return;
|
2019-10-18 19:57:09 +00:00
|
|
|
|
2020-07-23 21:32:38 +00:00
|
|
|
auto ret = PassValueToOtherContext(
|
|
|
|
func_owning_context, calling_context,
|
|
|
|
maybe_return_value.ToLocalChecked(), &object_cache,
|
|
|
|
support_dynamic_properties, 0, BridgeErrorTarget::kDestination);
|
2019-10-18 19:57:09 +00:00
|
|
|
if (ret.IsEmpty())
|
2020-05-15 18:57:40 +00:00
|
|
|
return;
|
|
|
|
info.GetReturnValue().Set(ret.ToLocalChecked());
|
2019-10-18 19:57:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::MaybeLocal<v8::Object> CreateProxyForAPI(
|
|
|
|
const v8::Local<v8::Object>& api_object,
|
|
|
|
const v8::Local<v8::Context>& source_context,
|
|
|
|
const v8::Local<v8::Context>& destination_context,
|
2020-03-04 07:18:22 +00:00
|
|
|
context_bridge::ObjectCache* object_cache,
|
2020-04-27 19:46:04 +00:00
|
|
|
bool support_dynamic_properties,
|
2019-10-18 19:57:09 +00:00
|
|
|
int recursion_depth) {
|
2019-10-31 07:56:00 +00:00
|
|
|
gin_helper::Dictionary api(source_context->GetIsolate(), api_object);
|
2020-05-11 20:41:42 +00:00
|
|
|
|
2019-10-18 19:57:09 +00:00
|
|
|
{
|
2020-05-11 20:41:42 +00:00
|
|
|
v8::Context::Scope destination_context_scope(destination_context);
|
2020-04-16 21:10:18 +00:00
|
|
|
gin_helper::Dictionary proxy =
|
|
|
|
gin::Dictionary::CreateEmpty(destination_context->GetIsolate());
|
|
|
|
object_cache->CacheProxiedObject(api.GetHandle(), proxy.GetHandle());
|
|
|
|
auto maybe_keys = api.GetHandle()->GetOwnPropertyNames(
|
2020-07-21 22:26:16 +00:00
|
|
|
source_context, static_cast<v8::PropertyFilter>(v8::ONLY_ENUMERABLE));
|
2020-04-16 21:10:18 +00:00
|
|
|
if (maybe_keys.IsEmpty())
|
|
|
|
return v8::MaybeLocal<v8::Object>(proxy.GetHandle());
|
|
|
|
auto keys = maybe_keys.ToLocalChecked();
|
|
|
|
|
2019-10-18 19:57:09 +00:00
|
|
|
uint32_t length = keys->Length();
|
|
|
|
for (uint32_t i = 0; i < length; i++) {
|
|
|
|
v8::Local<v8::Value> key =
|
|
|
|
keys->Get(destination_context, i).ToLocalChecked();
|
2020-07-21 22:26:16 +00:00
|
|
|
|
2020-04-27 19:46:04 +00:00
|
|
|
if (support_dynamic_properties) {
|
|
|
|
v8::Context::Scope source_context_scope(source_context);
|
|
|
|
auto maybe_desc = api.GetHandle()->GetOwnPropertyDescriptor(
|
|
|
|
source_context, v8::Local<v8::Name>::Cast(key));
|
|
|
|
v8::Local<v8::Value> desc_value;
|
|
|
|
if (!maybe_desc.ToLocal(&desc_value) || !desc_value->IsObject())
|
|
|
|
continue;
|
|
|
|
gin_helper::Dictionary desc(api.isolate(), desc_value.As<v8::Object>());
|
|
|
|
if (desc.Has("get") || desc.Has("set")) {
|
|
|
|
v8::Local<v8::Value> getter;
|
|
|
|
v8::Local<v8::Value> setter;
|
|
|
|
desc.Get("get", &getter);
|
|
|
|
desc.Get("set", &setter);
|
|
|
|
|
|
|
|
{
|
|
|
|
v8::Context::Scope destination_context_scope(destination_context);
|
|
|
|
v8::Local<v8::Value> getter_proxy;
|
|
|
|
v8::Local<v8::Value> setter_proxy;
|
|
|
|
if (!getter.IsEmpty()) {
|
|
|
|
if (!PassValueToOtherContext(source_context, destination_context,
|
2020-05-15 18:57:40 +00:00
|
|
|
getter, object_cache, false, 1)
|
2020-04-27 19:46:04 +00:00
|
|
|
.ToLocal(&getter_proxy))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!setter.IsEmpty()) {
|
|
|
|
if (!PassValueToOtherContext(source_context, destination_context,
|
2020-05-15 18:57:40 +00:00
|
|
|
setter, object_cache, false, 1)
|
2020-04-27 19:46:04 +00:00
|
|
|
.ToLocal(&setter_proxy))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::PropertyDescriptor desc(getter_proxy, setter_proxy);
|
|
|
|
ignore_result(proxy.GetHandle()->DefineProperty(
|
2020-07-21 22:26:16 +00:00
|
|
|
destination_context, key.As<v8::Name>(), desc));
|
2020-04-27 19:46:04 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2019-10-18 19:57:09 +00:00
|
|
|
v8::Local<v8::Value> value;
|
2020-07-21 22:26:16 +00:00
|
|
|
if (!api.Get(key, &value))
|
2019-10-18 19:57:09 +00:00
|
|
|
continue;
|
|
|
|
|
2020-04-27 19:46:04 +00:00
|
|
|
auto passed_value = PassValueToOtherContext(
|
2020-05-15 18:57:40 +00:00
|
|
|
source_context, destination_context, value, object_cache,
|
2020-04-27 19:46:04 +00:00
|
|
|
support_dynamic_properties, recursion_depth + 1);
|
2019-10-18 19:57:09 +00:00
|
|
|
if (passed_value.IsEmpty())
|
|
|
|
return v8::MaybeLocal<v8::Object>();
|
2020-07-21 22:26:16 +00:00
|
|
|
proxy.Set(key, passed_value.ToLocalChecked());
|
2019-10-18 19:57:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return proxy.GetHandle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExposeAPIInMainWorld(const std::string& key,
|
|
|
|
v8::Local<v8::Object> api_object,
|
2019-10-31 07:56:00 +00:00
|
|
|
gin_helper::Arguments* args) {
|
2020-05-11 21:24:43 +00:00
|
|
|
TRACE_EVENT1("electron", "ContextBridge::ExposeAPIInMainWorld", "key", key);
|
2019-10-18 19:57:09 +00:00
|
|
|
auto* render_frame = GetRenderFrame(api_object);
|
|
|
|
CHECK(render_frame);
|
|
|
|
auto* frame = render_frame->GetWebFrame();
|
|
|
|
CHECK(frame);
|
|
|
|
v8::Local<v8::Context> main_context = frame->MainWorldScriptContext();
|
2019-10-31 07:56:00 +00:00
|
|
|
gin_helper::Dictionary global(main_context->GetIsolate(),
|
|
|
|
main_context->Global());
|
2019-10-18 19:57:09 +00:00
|
|
|
|
|
|
|
if (global.Has(key)) {
|
|
|
|
args->ThrowError(
|
|
|
|
"Cannot bind an API on top of an existing property on the window "
|
|
|
|
"object");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::Local<v8::Context> isolated_context =
|
2020-02-24 04:33:58 +00:00
|
|
|
frame->WorldScriptContext(args->isolate(), WorldIDs::ISOLATED_WORLD_ID);
|
2019-10-18 19:57:09 +00:00
|
|
|
|
|
|
|
{
|
2020-05-11 20:41:42 +00:00
|
|
|
context_bridge::ObjectCache object_cache;
|
|
|
|
v8::Context::Scope main_context_scope(main_context);
|
|
|
|
|
2020-05-15 18:57:40 +00:00
|
|
|
v8::MaybeLocal<v8::Object> maybe_proxy = CreateProxyForAPI(
|
|
|
|
api_object, isolated_context, main_context, &object_cache, false, 0);
|
2019-10-18 19:57:09 +00:00
|
|
|
if (maybe_proxy.IsEmpty())
|
|
|
|
return;
|
|
|
|
auto proxy = maybe_proxy.ToLocalChecked();
|
|
|
|
if (!DeepFreeze(proxy, main_context))
|
|
|
|
return;
|
|
|
|
|
|
|
|
global.SetReadOnlyNonConfigurable(key, proxy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-22 19:42:51 +00:00
|
|
|
gin_helper::Dictionary TraceKeyPath(const gin_helper::Dictionary& start,
|
|
|
|
const std::vector<std::string>& key_path) {
|
|
|
|
gin_helper::Dictionary current = start;
|
|
|
|
for (size_t i = 0; i < key_path.size() - 1; i++) {
|
|
|
|
CHECK(current.Get(key_path[i], ¤t));
|
|
|
|
}
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
2020-04-27 19:46:04 +00:00
|
|
|
void OverrideGlobalValueFromIsolatedWorld(
|
2020-04-22 19:42:51 +00:00
|
|
|
const std::vector<std::string>& key_path,
|
2020-04-27 19:46:04 +00:00
|
|
|
v8::Local<v8::Object> value,
|
|
|
|
bool support_dynamic_properties) {
|
2020-10-22 20:24:59 +00:00
|
|
|
if (key_path.empty())
|
2020-04-22 19:42:51 +00:00
|
|
|
return;
|
|
|
|
|
2020-04-27 19:46:04 +00:00
|
|
|
auto* render_frame = GetRenderFrame(value);
|
2020-04-22 19:42:51 +00:00
|
|
|
CHECK(render_frame);
|
|
|
|
auto* frame = render_frame->GetWebFrame();
|
|
|
|
CHECK(frame);
|
|
|
|
v8::Local<v8::Context> main_context = frame->MainWorldScriptContext();
|
|
|
|
gin_helper::Dictionary global(main_context->GetIsolate(),
|
|
|
|
main_context->Global());
|
|
|
|
|
|
|
|
const std::string final_key = key_path[key_path.size() - 1];
|
|
|
|
gin_helper::Dictionary target_object = TraceKeyPath(global, key_path);
|
|
|
|
|
|
|
|
{
|
|
|
|
v8::Context::Scope main_context_scope(main_context);
|
|
|
|
context_bridge::ObjectCache object_cache;
|
2020-05-15 18:57:40 +00:00
|
|
|
v8::MaybeLocal<v8::Value> maybe_proxy =
|
|
|
|
PassValueToOtherContext(value->CreationContext(), main_context, value,
|
|
|
|
&object_cache, support_dynamic_properties, 1);
|
2020-04-22 19:42:51 +00:00
|
|
|
DCHECK(!maybe_proxy.IsEmpty());
|
|
|
|
auto proxy = maybe_proxy.ToLocalChecked();
|
|
|
|
|
|
|
|
target_object.Set(final_key, proxy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OverrideGlobalPropertyFromIsolatedWorld(
|
|
|
|
const std::vector<std::string>& key_path,
|
|
|
|
v8::Local<v8::Object> getter,
|
|
|
|
v8::Local<v8::Value> setter,
|
|
|
|
gin_helper::Arguments* args) {
|
2020-10-22 20:24:59 +00:00
|
|
|
if (key_path.empty())
|
2020-04-22 19:42:51 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
auto* render_frame = GetRenderFrame(getter);
|
|
|
|
CHECK(render_frame);
|
|
|
|
auto* frame = render_frame->GetWebFrame();
|
|
|
|
CHECK(frame);
|
|
|
|
v8::Local<v8::Context> main_context = frame->MainWorldScriptContext();
|
|
|
|
gin_helper::Dictionary global(main_context->GetIsolate(),
|
|
|
|
main_context->Global());
|
|
|
|
|
|
|
|
const std::string final_key = key_path[key_path.size() - 1];
|
|
|
|
v8::Local<v8::Object> target_object =
|
|
|
|
TraceKeyPath(global, key_path).GetHandle();
|
|
|
|
|
|
|
|
{
|
|
|
|
v8::Context::Scope main_context_scope(main_context);
|
|
|
|
context_bridge::ObjectCache object_cache;
|
|
|
|
v8::Local<v8::Value> getter_proxy;
|
|
|
|
v8::Local<v8::Value> setter_proxy;
|
|
|
|
if (!getter->IsNullOrUndefined()) {
|
|
|
|
v8::MaybeLocal<v8::Value> maybe_getter_proxy =
|
|
|
|
PassValueToOtherContext(getter->CreationContext(), main_context,
|
2020-05-15 18:57:40 +00:00
|
|
|
getter, &object_cache, false, 1);
|
2020-04-22 19:42:51 +00:00
|
|
|
DCHECK(!maybe_getter_proxy.IsEmpty());
|
|
|
|
getter_proxy = maybe_getter_proxy.ToLocalChecked();
|
|
|
|
}
|
|
|
|
if (!setter->IsNullOrUndefined() && setter->IsObject()) {
|
|
|
|
v8::MaybeLocal<v8::Value> maybe_setter_proxy =
|
|
|
|
PassValueToOtherContext(getter->CreationContext(), main_context,
|
2020-05-15 18:57:40 +00:00
|
|
|
setter, &object_cache, false, 1);
|
2020-04-22 19:42:51 +00:00
|
|
|
DCHECK(!maybe_setter_proxy.IsEmpty());
|
|
|
|
setter_proxy = maybe_setter_proxy.ToLocalChecked();
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::PropertyDescriptor desc(getter_proxy, setter_proxy);
|
|
|
|
bool success = IsTrue(target_object->DefineProperty(
|
|
|
|
main_context, gin::StringToV8(args->isolate(), final_key), desc));
|
|
|
|
DCHECK(success);
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsCalledFromMainWorld(v8::Isolate* isolate) {
|
|
|
|
auto* render_frame = GetRenderFrame(isolate->GetCurrentContext()->Global());
|
|
|
|
CHECK(render_frame);
|
|
|
|
auto* frame = render_frame->GetWebFrame();
|
|
|
|
CHECK(frame);
|
|
|
|
v8::Local<v8::Context> main_context = frame->MainWorldScriptContext();
|
|
|
|
return isolate->GetCurrentContext() == main_context;
|
|
|
|
}
|
|
|
|
|
2019-10-18 19:57:09 +00:00
|
|
|
} // namespace api
|
|
|
|
|
|
|
|
} // namespace electron
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
void Initialize(v8::Local<v8::Object> exports,
|
|
|
|
v8::Local<v8::Value> unused,
|
|
|
|
v8::Local<v8::Context> context,
|
|
|
|
void* priv) {
|
|
|
|
v8::Isolate* isolate = context->GetIsolate();
|
2019-10-31 07:56:00 +00:00
|
|
|
gin_helper::Dictionary dict(isolate, exports);
|
2019-10-18 19:57:09 +00:00
|
|
|
dict.SetMethod("exposeAPIInMainWorld", &electron::api::ExposeAPIInMainWorld);
|
2020-04-27 19:46:04 +00:00
|
|
|
dict.SetMethod("_overrideGlobalValueFromIsolatedWorld",
|
|
|
|
&electron::api::OverrideGlobalValueFromIsolatedWorld);
|
2020-04-22 19:42:51 +00:00
|
|
|
dict.SetMethod("_overrideGlobalPropertyFromIsolatedWorld",
|
|
|
|
&electron::api::OverrideGlobalPropertyFromIsolatedWorld);
|
|
|
|
dict.SetMethod("_isCalledFromMainWorld",
|
|
|
|
&electron::api::IsCalledFromMainWorld);
|
2019-10-18 19:57:09 +00:00
|
|
|
#ifdef DCHECK_IS_ON
|
2020-05-15 18:57:40 +00:00
|
|
|
dict.Set("_isDebug", true);
|
2019-10-18 19:57:09 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2020-02-14 11:25:39 +00:00
|
|
|
NODE_LINKED_MODULE_CONTEXT_AWARE(electron_renderer_context_bridge, Initialize)
|