2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc.
|
2014-04-25 09:49:37 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
2013-04-25 08:30:31 +00:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2022-06-22 08:15:27 +00:00
|
|
|
#include <iterator>
|
2016-05-11 11:40:48 +00:00
|
|
|
#include <utility>
|
2015-12-29 02:29:48 +00:00
|
|
|
|
2019-04-20 17:20:37 +00:00
|
|
|
#include "base/hash/hash.h"
|
2022-02-25 18:17:35 +00:00
|
|
|
#include "base/run_loop.h"
|
2019-09-18 16:52:06 +00:00
|
|
|
#include "electron/buildflags/buildflags.h"
|
2020-05-13 10:10:03 +00:00
|
|
|
#include "shell/common/api/electron_api_key_weak_map.h"
|
2019-10-25 13:03:28 +00:00
|
|
|
#include "shell/common/gin_converters/content_converter.h"
|
2019-09-19 15:09:15 +00:00
|
|
|
#include "shell/common/gin_converters/gurl_converter.h"
|
2020-05-10 23:06:07 +00:00
|
|
|
#include "shell/common/gin_converters/std_converter.h"
|
2019-09-19 15:09:15 +00:00
|
|
|
#include "shell/common/gin_helper/dictionary.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/common/node_includes.h"
|
2016-11-15 08:45:34 +00:00
|
|
|
#include "url/origin.h"
|
2013-08-27 09:47:44 +00:00
|
|
|
#include "v8/include/v8-profiler.h"
|
2013-12-11 07:48:19 +00:00
|
|
|
|
2016-05-11 11:40:48 +00:00
|
|
|
namespace std {
|
|
|
|
|
|
|
|
// The hash function used by DoubleIDWeakMap.
|
|
|
|
template <typename Type1, typename Type2>
|
|
|
|
struct hash<std::pair<Type1, Type2>> {
|
|
|
|
std::size_t operator()(std::pair<Type1, Type2> value) const {
|
2018-07-10 08:15:40 +00:00
|
|
|
return base::HashInts(base::Hash(value.first), value.second);
|
2016-05-11 11:40:48 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace std
|
|
|
|
|
2019-09-19 15:09:15 +00:00
|
|
|
namespace gin {
|
2016-05-11 11:40:48 +00:00
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
template <typename Type1, typename Type2>
|
2016-05-11 11:40:48 +00:00
|
|
|
struct Converter<std::pair<Type1, Type2>> {
|
|
|
|
static bool FromV8(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Value> val,
|
|
|
|
std::pair<Type1, Type2>* out) {
|
|
|
|
if (!val->IsArray())
|
|
|
|
return false;
|
|
|
|
|
2021-05-12 07:38:21 +00:00
|
|
|
v8::Local<v8::Array> array = val.As<v8::Array>();
|
2016-05-11 11:40:48 +00:00
|
|
|
if (array->Length() != 2)
|
|
|
|
return false;
|
2019-05-01 00:18:22 +00:00
|
|
|
|
|
|
|
auto context = isolate->GetCurrentContext();
|
|
|
|
return Converter<Type1>::FromV8(
|
|
|
|
isolate, array->Get(context, 0).ToLocalChecked(), &out->first) &&
|
|
|
|
Converter<Type2>::FromV8(
|
|
|
|
isolate, array->Get(context, 1).ToLocalChecked(), &out->second);
|
2016-05-11 11:40:48 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-19 15:09:15 +00:00
|
|
|
} // namespace gin
|
2016-05-11 11:40:48 +00:00
|
|
|
|
2013-04-25 08:30:31 +00:00
|
|
|
namespace {
|
|
|
|
|
2016-03-23 19:54:01 +00:00
|
|
|
v8::Local<v8::Value> GetHiddenValue(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Object> object,
|
2015-12-28 14:51:01 +00:00
|
|
|
v8::Local<v8::String> key) {
|
2016-03-23 19:54:01 +00:00
|
|
|
v8::Local<v8::Context> context = isolate->GetCurrentContext();
|
|
|
|
v8::Local<v8::Private> privateKey = v8::Private::ForApi(isolate, key);
|
|
|
|
v8::Local<v8::Value> value;
|
|
|
|
v8::Maybe<bool> result = object->HasPrivate(context, privateKey);
|
|
|
|
if (!(result.IsJust() && result.FromJust()))
|
|
|
|
return v8::Local<v8::Value>();
|
|
|
|
if (object->GetPrivate(context, privateKey).ToLocal(&value))
|
|
|
|
return value;
|
|
|
|
return v8::Local<v8::Value>();
|
2013-04-25 08:30:31 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 19:54:01 +00:00
|
|
|
void SetHiddenValue(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Object> object,
|
2015-05-22 11:11:22 +00:00
|
|
|
v8::Local<v8::String> key,
|
|
|
|
v8::Local<v8::Value> value) {
|
2016-03-23 19:54:01 +00:00
|
|
|
if (value.IsEmpty())
|
|
|
|
return;
|
|
|
|
v8::Local<v8::Context> context = isolate->GetCurrentContext();
|
|
|
|
v8::Local<v8::Private> privateKey = v8::Private::ForApi(isolate, key);
|
|
|
|
object->SetPrivate(context, privateKey, value);
|
2013-04-25 08:30:31 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 19:54:01 +00:00
|
|
|
void DeleteHiddenValue(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Object> object,
|
2015-08-27 11:01:34 +00:00
|
|
|
v8::Local<v8::String> key) {
|
2016-03-23 19:54:01 +00:00
|
|
|
v8::Local<v8::Context> context = isolate->GetCurrentContext();
|
|
|
|
v8::Local<v8::Private> privateKey = v8::Private::ForApi(isolate, key);
|
|
|
|
// Actually deleting the value would make force the object into
|
|
|
|
// dictionary mode which is unnecessarily slow. Instead, we replace
|
|
|
|
// the hidden value with "undefined".
|
|
|
|
object->SetPrivate(context, privateKey, v8::Undefined(isolate));
|
2015-08-27 11:01:34 +00:00
|
|
|
}
|
|
|
|
|
2015-05-22 11:11:22 +00:00
|
|
|
int32_t GetObjectHash(v8::Local<v8::Object> object) {
|
2014-04-16 07:31:59 +00:00
|
|
|
return object->GetIdentityHash();
|
2013-04-25 08:30:31 +00:00
|
|
|
}
|
|
|
|
|
2014-06-28 14:33:00 +00:00
|
|
|
void TakeHeapSnapshot(v8::Isolate* isolate) {
|
2015-05-22 07:24:34 +00:00
|
|
|
isolate->GetHeapProfiler()->TakeHeapSnapshot();
|
2013-08-27 09:47:44 +00:00
|
|
|
}
|
|
|
|
|
2016-10-25 10:41:01 +00:00
|
|
|
void RequestGarbageCollectionForTesting(v8::Isolate* isolate) {
|
|
|
|
isolate->RequestGarbageCollectionForTesting(
|
2018-04-18 01:55:30 +00:00
|
|
|
v8::Isolate::GarbageCollectionType::kFullGarbageCollection);
|
2016-10-25 10:41:01 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 02:04:24 +00:00
|
|
|
// This causes a fatal error by creating a circular extension dependency.
|
|
|
|
void TriggerFatalErrorForTesting(v8::Isolate* isolate) {
|
|
|
|
static const char* aDeps[] = {"B"};
|
2022-06-22 08:15:27 +00:00
|
|
|
v8::RegisterExtension(
|
|
|
|
std::make_unique<v8::Extension>("A", "", std::size(aDeps), aDeps));
|
2020-07-30 02:04:24 +00:00
|
|
|
static const char* bDeps[] = {"A"};
|
2022-06-22 08:15:27 +00:00
|
|
|
v8::RegisterExtension(
|
|
|
|
std::make_unique<v8::Extension>("B", "", std::size(aDeps), bDeps));
|
2020-07-30 02:04:24 +00:00
|
|
|
v8::ExtensionConfiguration config(1, bDeps);
|
|
|
|
v8::Context::New(isolate, &config);
|
|
|
|
}
|
2021-03-12 07:35:57 +00:00
|
|
|
|
|
|
|
void RunUntilIdle() {
|
|
|
|
base::RunLoop().RunUntilIdle();
|
|
|
|
}
|
2020-05-15 18:57:40 +00:00
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
void Initialize(v8::Local<v8::Object> exports,
|
|
|
|
v8::Local<v8::Value> unused,
|
|
|
|
v8::Local<v8::Context> context,
|
|
|
|
void* priv) {
|
2019-09-19 15:09:15 +00:00
|
|
|
gin_helper::Dictionary dict(context->GetIsolate(), exports);
|
2014-04-16 07:31:59 +00:00
|
|
|
dict.SetMethod("getHiddenValue", &GetHiddenValue);
|
|
|
|
dict.SetMethod("setHiddenValue", &SetHiddenValue);
|
2015-08-27 11:01:34 +00:00
|
|
|
dict.SetMethod("deleteHiddenValue", &DeleteHiddenValue);
|
2014-04-16 07:31:59 +00:00
|
|
|
dict.SetMethod("getObjectHash", &GetObjectHash);
|
|
|
|
dict.SetMethod("takeHeapSnapshot", &TakeHeapSnapshot);
|
2016-10-25 10:41:01 +00:00
|
|
|
dict.SetMethod("requestGarbageCollectionForTesting",
|
|
|
|
&RequestGarbageCollectionForTesting);
|
2020-07-30 02:04:24 +00:00
|
|
|
dict.SetMethod("triggerFatalErrorForTesting", &TriggerFatalErrorForTesting);
|
2021-03-12 07:35:57 +00:00
|
|
|
dict.SetMethod("runUntilIdle", &RunUntilIdle);
|
2013-04-25 08:30:31 +00:00
|
|
|
}
|
|
|
|
|
2014-04-16 07:31:59 +00:00
|
|
|
} // namespace
|
2013-04-25 08:30:31 +00:00
|
|
|
|
2023-02-09 01:31:38 +00:00
|
|
|
NODE_LINKED_BINDING_CONTEXT_AWARE(electron_common_v8_util, Initialize)
|