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.
|
|
|
|
|
2015-12-29 02:29:48 +00:00
|
|
|
#include <string>
|
2016-05-11 11:40:48 +00:00
|
|
|
#include <utility>
|
2015-12-29 02:29:48 +00:00
|
|
|
|
2016-05-11 10:30:06 +00:00
|
|
|
#include "atom/common/api/atom_api_key_weak_map.h"
|
2016-04-26 07:10:27 +00:00
|
|
|
#include "atom/common/api/remote_callback_freer.h"
|
|
|
|
#include "atom/common/api/remote_object_freer.h"
|
|
|
|
#include "atom/common/native_mate_converters/content_converter.h"
|
2016-11-15 08:45:34 +00:00
|
|
|
#include "atom/common/native_mate_converters/gurl_converter.h"
|
2015-09-07 08:29:54 +00:00
|
|
|
#include "atom/common/node_includes.h"
|
2016-05-11 11:40:48 +00:00
|
|
|
#include "base/hash.h"
|
2014-04-16 07:31:59 +00:00
|
|
|
#include "native_mate/dictionary.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 {
|
|
|
|
return base::HashInts<Type1, Type2>(value.first, value.second);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace std
|
|
|
|
|
|
|
|
namespace mate {
|
|
|
|
|
|
|
|
template<typename Type1, typename Type2>
|
|
|
|
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;
|
|
|
|
|
|
|
|
v8::Local<v8::Array> array(v8::Local<v8::Array>::Cast(val));
|
|
|
|
if (array->Length() != 2)
|
|
|
|
return false;
|
|
|
|
return Converter<Type1>::FromV8(isolate, array->Get(0), &out->first) &&
|
|
|
|
Converter<Type2>::FromV8(isolate, array->Get(1), &out->second);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mate
|
|
|
|
|
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(
|
|
|
|
v8::Isolate::GarbageCollectionType::kFullGarbageCollection);
|
|
|
|
}
|
|
|
|
|
2016-11-15 08:45:34 +00:00
|
|
|
bool IsSameOrigin(const GURL& l, const GURL& r) {
|
|
|
|
return url::Origin(l).IsSameOriginWith(url::Origin(r));
|
|
|
|
}
|
|
|
|
|
2015-05-22 11:11:22 +00:00
|
|
|
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
|
|
|
|
v8::Local<v8::Context> context, void* priv) {
|
2014-06-29 12:48:44 +00:00
|
|
|
mate::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-04-26 07:10:27 +00:00
|
|
|
dict.SetMethod("setRemoteCallbackFreer", &atom::RemoteCallbackFreer::BindTo);
|
|
|
|
dict.SetMethod("setRemoteObjectFreer", &atom::RemoteObjectFreer::BindTo);
|
2016-05-11 10:30:06 +00:00
|
|
|
dict.SetMethod("createIDWeakMap", &atom::api::KeyWeakMap<int32_t>::Create);
|
2016-05-11 11:40:48 +00:00
|
|
|
dict.SetMethod("createDoubleIDWeakMap",
|
2016-09-06 01:28:40 +00:00
|
|
|
&atom::api::KeyWeakMap<std::pair<int64_t, int32_t>>::Create);
|
2016-10-25 10:41:01 +00:00
|
|
|
dict.SetMethod("requestGarbageCollectionForTesting",
|
|
|
|
&RequestGarbageCollectionForTesting);
|
2016-11-15 08:45:34 +00:00
|
|
|
dict.SetMethod("isSameOrigin", &IsSameOrigin);
|
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
|
|
|
|
2018-01-06 15:58:24 +00:00
|
|
|
NODE_BUILTIN_MODULE_CONTEXT_AWARE(atom_common_v8_util, Initialize)
|