// Copyright (c) 2013 GitHub, Inc. // Use of this source code is governed by the MIT license that can be // found in the LICENSE file. #include #include #include "base/hash/hash.h" #include "electron/buildflags/buildflags.h" #include "shell/common/api/electron_api_key_weak_map.h" #include "shell/common/gin_converters/content_converter.h" #include "shell/common/gin_converters/gurl_converter.h" #include "shell/common/gin_converters/std_converter.h" #include "shell/common/gin_helper/dictionary.h" #include "shell/common/node_includes.h" #include "url/origin.h" #include "v8/include/v8-profiler.h" namespace std { // The hash function used by DoubleIDWeakMap. template struct hash> { std::size_t operator()(std::pair value) const { return base::HashInts(base::Hash(value.first), value.second); } }; } // namespace std namespace gin { template struct Converter> { static bool FromV8(v8::Isolate* isolate, v8::Local val, std::pair* out) { if (!val->IsArray()) return false; v8::Local array(v8::Local::Cast(val)); if (array->Length() != 2) return false; auto context = isolate->GetCurrentContext(); return Converter::FromV8( isolate, array->Get(context, 0).ToLocalChecked(), &out->first) && Converter::FromV8( isolate, array->Get(context, 1).ToLocalChecked(), &out->second); } }; } // namespace gin namespace { v8::Local GetHiddenValue(v8::Isolate* isolate, v8::Local object, v8::Local key) { v8::Local context = isolate->GetCurrentContext(); v8::Local privateKey = v8::Private::ForApi(isolate, key); v8::Local value; v8::Maybe result = object->HasPrivate(context, privateKey); if (!(result.IsJust() && result.FromJust())) return v8::Local(); if (object->GetPrivate(context, privateKey).ToLocal(&value)) return value; return v8::Local(); } void SetHiddenValue(v8::Isolate* isolate, v8::Local object, v8::Local key, v8::Local value) { if (value.IsEmpty()) return; v8::Local context = isolate->GetCurrentContext(); v8::Local privateKey = v8::Private::ForApi(isolate, key); object->SetPrivate(context, privateKey, value); } void DeleteHiddenValue(v8::Isolate* isolate, v8::Local object, v8::Local key) { v8::Local context = isolate->GetCurrentContext(); v8::Local 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)); } int32_t GetObjectHash(v8::Local object) { return object->GetIdentityHash(); } void TakeHeapSnapshot(v8::Isolate* isolate) { isolate->GetHeapProfiler()->TakeHeapSnapshot(); } void RequestGarbageCollectionForTesting(v8::Isolate* isolate) { isolate->RequestGarbageCollectionForTesting( v8::Isolate::GarbageCollectionType::kFullGarbageCollection); } bool IsSameOrigin(const GURL& l, const GURL& r) { return url::Origin::Create(l).IsSameOriginWith(url::Origin::Create(r)); } #ifdef DCHECK_IS_ON std::vector> weakly_tracked_values; void WeaklyTrackValue(v8::Isolate* isolate, v8::Local value) { v8::Global global_value(isolate, value); global_value.SetWeak(); weakly_tracked_values.push_back(std::move(global_value)); } void ClearWeaklyTrackedValues() { weakly_tracked_values.clear(); } std::vector> GetWeaklyTrackedValues(v8::Isolate* isolate) { std::vector> locals; for (const auto& value : weakly_tracked_values) { if (!value.IsEmpty()) locals.push_back(value.Get(isolate)); } return locals; } // 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("A", "", 1, aDeps)); static const char* bDeps[] = {"A"}; v8::RegisterExtension(std::make_unique("B", "", 1, bDeps)); v8::ExtensionConfiguration config(1, bDeps); v8::Context::New(isolate, &config); } #endif void Initialize(v8::Local exports, v8::Local unused, v8::Local context, void* priv) { gin_helper::Dictionary dict(context->GetIsolate(), exports); dict.SetMethod("getHiddenValue", &GetHiddenValue); dict.SetMethod("setHiddenValue", &SetHiddenValue); dict.SetMethod("deleteHiddenValue", &DeleteHiddenValue); dict.SetMethod("getObjectHash", &GetObjectHash); dict.SetMethod("takeHeapSnapshot", &TakeHeapSnapshot); dict.SetMethod("requestGarbageCollectionForTesting", &RequestGarbageCollectionForTesting); dict.SetMethod("isSameOrigin", &IsSameOrigin); #ifdef DCHECK_IS_ON dict.SetMethod("triggerFatalErrorForTesting", &TriggerFatalErrorForTesting); dict.SetMethod("getWeaklyTrackedValues", &GetWeaklyTrackedValues); dict.SetMethod("clearWeaklyTrackedValues", &ClearWeaklyTrackedValues); dict.SetMethod("weaklyTrackValue", &WeaklyTrackValue); #endif } } // namespace NODE_LINKED_MODULE_CONTEXT_AWARE(electron_common_v8_util, Initialize)