2014-10-31 11:17:05 -07:00
|
|
|
// Copyright (c) 2013 GitHub, Inc.
|
2014-04-25 17:49:37 +08:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
2013-04-25 16:30:31 +08:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2016-05-11 20:40:48 +09:00
|
|
|
#include <utility>
|
2015-12-29 10:29:48 +08:00
|
|
|
|
2019-04-20 13:20:37 -04:00
|
|
|
#include "base/hash/hash.h"
|
2022-02-25 13:17:35 -05:00
|
|
|
#include "base/run_loop.h"
|
2019-09-18 09:52:06 -07:00
|
|
|
#include "electron/buildflags/buildflags.h"
|
2020-05-13 12:10:03 +02:00
|
|
|
#include "shell/common/api/electron_api_key_weak_map.h"
|
2019-10-25 22:03:28 +09:00
|
|
|
#include "shell/common/gin_converters/content_converter.h"
|
2019-09-19 11:09:15 -04:00
|
|
|
#include "shell/common/gin_converters/gurl_converter.h"
|
2020-05-11 01:06:07 +02:00
|
|
|
#include "shell/common/gin_converters/std_converter.h"
|
2019-09-19 11:09:15 -04:00
|
|
|
#include "shell/common/gin_helper/dictionary.h"
|
2019-06-19 13:46:59 -07:00
|
|
|
#include "shell/common/node_includes.h"
|
2016-11-15 17:45:34 +09:00
|
|
|
#include "url/origin.h"
|
2013-08-27 17:47:44 +08:00
|
|
|
#include "v8/include/v8-profiler.h"
|
2013-12-11 15:48:19 +08:00
|
|
|
|
2016-05-11 20:40:48 +09: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 17:15:40 +09:00
|
|
|
return base::HashInts(base::Hash(value.first), value.second);
|
2016-05-11 20:40:48 +09:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace std
|
|
|
|
|
2019-09-19 11:09:15 -04:00
|
|
|
namespace gin {
|
2016-05-11 20:40:48 +09:00
|
|
|
|
2018-04-17 21:55:30 -04:00
|
|
|
template <typename Type1, typename Type2>
|
2016-05-11 20:40:48 +09: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 09:38:21 +02:00
|
|
|
v8::Local<v8::Array> array = val.As<v8::Array>();
|
2016-05-11 20:40:48 +09:00
|
|
|
if (array->Length() != 2)
|
|
|
|
return false;
|
2019-04-30 20:18:22 -04: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 20:40:48 +09:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-19 11:09:15 -04:00
|
|
|
} // namespace gin
|
2016-05-11 20:40:48 +09:00
|
|
|
|
2013-04-25 16:30:31 +08:00
|
|
|
namespace {
|
|
|
|
|
2016-03-24 01:24:01 +05:30
|
|
|
v8::Local<v8::Value> GetHiddenValue(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Object> object,
|
2015-12-28 22:51:01 +08:00
|
|
|
v8::Local<v8::String> key) {
|
2016-03-24 01:24:01 +05:30
|
|
|
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 16:30:31 +08:00
|
|
|
}
|
|
|
|
|
2016-03-24 01:24:01 +05:30
|
|
|
void SetHiddenValue(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Object> object,
|
2015-05-22 19:11:22 +08:00
|
|
|
v8::Local<v8::String> key,
|
|
|
|
v8::Local<v8::Value> value) {
|
2016-03-24 01:24:01 +05:30
|
|
|
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 16:30:31 +08:00
|
|
|
}
|
|
|
|
|
2016-03-24 01:24:01 +05:30
|
|
|
void DeleteHiddenValue(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Object> object,
|
2015-08-27 19:01:34 +08:00
|
|
|
v8::Local<v8::String> key) {
|
2016-03-24 01:24:01 +05:30
|
|
|
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 19:01:34 +08:00
|
|
|
}
|
|
|
|
|
2015-05-22 19:11:22 +08:00
|
|
|
int32_t GetObjectHash(v8::Local<v8::Object> object) {
|
2014-04-16 15:31:59 +08:00
|
|
|
return object->GetIdentityHash();
|
2013-04-25 16:30:31 +08:00
|
|
|
}
|
|
|
|
|
2014-06-28 22:33:00 +08:00
|
|
|
void TakeHeapSnapshot(v8::Isolate* isolate) {
|
2015-05-22 15:24:34 +08:00
|
|
|
isolate->GetHeapProfiler()->TakeHeapSnapshot();
|
2013-08-27 17:47:44 +08:00
|
|
|
}
|
|
|
|
|
2016-10-25 12:41:01 +02:00
|
|
|
void RequestGarbageCollectionForTesting(v8::Isolate* isolate) {
|
|
|
|
isolate->RequestGarbageCollectionForTesting(
|
2018-04-17 21:55:30 -04:00
|
|
|
v8::Isolate::GarbageCollectionType::kFullGarbageCollection);
|
2016-10-25 12:41:01 +02:00
|
|
|
}
|
|
|
|
|
2020-07-29 19:04:24 -07:00
|
|
|
// This causes a fatal error by creating a circular extension dependency.
|
|
|
|
void TriggerFatalErrorForTesting(v8::Isolate* isolate) {
|
|
|
|
static const char* aDeps[] = {"B"};
|
|
|
|
v8::RegisterExtension(std::make_unique<v8::Extension>("A", "", 1, aDeps));
|
|
|
|
static const char* bDeps[] = {"A"};
|
|
|
|
v8::RegisterExtension(std::make_unique<v8::Extension>("B", "", 1, bDeps));
|
|
|
|
v8::ExtensionConfiguration config(1, bDeps);
|
|
|
|
v8::Context::New(isolate, &config);
|
|
|
|
}
|
2021-03-12 16:35:57 +09:00
|
|
|
|
|
|
|
void RunUntilIdle() {
|
|
|
|
base::RunLoop().RunUntilIdle();
|
|
|
|
}
|
2020-05-15 11:57:40 -07:00
|
|
|
|
2018-04-17 21:55:30 -04:00
|
|
|
void Initialize(v8::Local<v8::Object> exports,
|
|
|
|
v8::Local<v8::Value> unused,
|
|
|
|
v8::Local<v8::Context> context,
|
|
|
|
void* priv) {
|
2019-09-19 11:09:15 -04:00
|
|
|
gin_helper::Dictionary dict(context->GetIsolate(), exports);
|
2014-04-16 15:31:59 +08:00
|
|
|
dict.SetMethod("getHiddenValue", &GetHiddenValue);
|
|
|
|
dict.SetMethod("setHiddenValue", &SetHiddenValue);
|
2015-08-27 19:01:34 +08:00
|
|
|
dict.SetMethod("deleteHiddenValue", &DeleteHiddenValue);
|
2014-04-16 15:31:59 +08:00
|
|
|
dict.SetMethod("getObjectHash", &GetObjectHash);
|
|
|
|
dict.SetMethod("takeHeapSnapshot", &TakeHeapSnapshot);
|
2016-10-25 12:41:01 +02:00
|
|
|
dict.SetMethod("requestGarbageCollectionForTesting",
|
|
|
|
&RequestGarbageCollectionForTesting);
|
2020-07-29 19:04:24 -07:00
|
|
|
dict.SetMethod("triggerFatalErrorForTesting", &TriggerFatalErrorForTesting);
|
2021-03-12 16:35:57 +09:00
|
|
|
dict.SetMethod("runUntilIdle", &RunUntilIdle);
|
2013-04-25 16:30:31 +08:00
|
|
|
}
|
|
|
|
|
2014-04-16 15:31:59 +08:00
|
|
|
} // namespace
|
2013-04-25 16:30:31 +08:00
|
|
|
|
2020-02-14 03:25:39 -08:00
|
|
|
NODE_LINKED_MODULE_CONTEXT_AWARE(electron_common_v8_util, Initialize)
|