atom_api_v8_util.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright (c) 2013 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include <string>
  5. #include <utility>
  6. #include "atom/common/api/atom_api_key_weak_map.h"
  7. #include "atom/common/api/remote_callback_freer.h"
  8. #include "atom/common/api/remote_object_freer.h"
  9. #include "atom/common/native_mate_converters/content_converter.h"
  10. #include "atom/common/native_mate_converters/gurl_converter.h"
  11. #include "atom/common/node_includes.h"
  12. #include "base/hash/hash.h"
  13. #include "native_mate/dictionary.h"
  14. #include "url/origin.h"
  15. #include "v8/include/v8-profiler.h"
  16. namespace std {
  17. // The hash function used by DoubleIDWeakMap.
  18. template <typename Type1, typename Type2>
  19. struct hash<std::pair<Type1, Type2>> {
  20. std::size_t operator()(std::pair<Type1, Type2> value) const {
  21. return base::HashInts(base::Hash(value.first), value.second);
  22. }
  23. };
  24. } // namespace std
  25. namespace mate {
  26. template <typename Type1, typename Type2>
  27. struct Converter<std::pair<Type1, Type2>> {
  28. static bool FromV8(v8::Isolate* isolate,
  29. v8::Local<v8::Value> val,
  30. std::pair<Type1, Type2>* out) {
  31. if (!val->IsArray())
  32. return false;
  33. v8::Local<v8::Array> array(v8::Local<v8::Array>::Cast(val));
  34. if (array->Length() != 2)
  35. return false;
  36. auto context = isolate->GetCurrentContext();
  37. return Converter<Type1>::FromV8(
  38. isolate, array->Get(context, 0).ToLocalChecked(), &out->first) &&
  39. Converter<Type2>::FromV8(
  40. isolate, array->Get(context, 1).ToLocalChecked(), &out->second);
  41. }
  42. };
  43. } // namespace mate
  44. namespace {
  45. v8::Local<v8::Value> GetHiddenValue(v8::Isolate* isolate,
  46. v8::Local<v8::Object> object,
  47. v8::Local<v8::String> key) {
  48. v8::Local<v8::Context> context = isolate->GetCurrentContext();
  49. v8::Local<v8::Private> privateKey = v8::Private::ForApi(isolate, key);
  50. v8::Local<v8::Value> value;
  51. v8::Maybe<bool> result = object->HasPrivate(context, privateKey);
  52. if (!(result.IsJust() && result.FromJust()))
  53. return v8::Local<v8::Value>();
  54. if (object->GetPrivate(context, privateKey).ToLocal(&value))
  55. return value;
  56. return v8::Local<v8::Value>();
  57. }
  58. void SetHiddenValue(v8::Isolate* isolate,
  59. v8::Local<v8::Object> object,
  60. v8::Local<v8::String> key,
  61. v8::Local<v8::Value> value) {
  62. if (value.IsEmpty())
  63. return;
  64. v8::Local<v8::Context> context = isolate->GetCurrentContext();
  65. v8::Local<v8::Private> privateKey = v8::Private::ForApi(isolate, key);
  66. object->SetPrivate(context, privateKey, value);
  67. }
  68. void DeleteHiddenValue(v8::Isolate* isolate,
  69. v8::Local<v8::Object> object,
  70. v8::Local<v8::String> key) {
  71. v8::Local<v8::Context> context = isolate->GetCurrentContext();
  72. v8::Local<v8::Private> privateKey = v8::Private::ForApi(isolate, key);
  73. // Actually deleting the value would make force the object into
  74. // dictionary mode which is unnecessarily slow. Instead, we replace
  75. // the hidden value with "undefined".
  76. object->SetPrivate(context, privateKey, v8::Undefined(isolate));
  77. }
  78. int32_t GetObjectHash(v8::Local<v8::Object> object) {
  79. return object->GetIdentityHash();
  80. }
  81. void TakeHeapSnapshot(v8::Isolate* isolate) {
  82. isolate->GetHeapProfiler()->TakeHeapSnapshot();
  83. }
  84. void RequestGarbageCollectionForTesting(v8::Isolate* isolate) {
  85. isolate->RequestGarbageCollectionForTesting(
  86. v8::Isolate::GarbageCollectionType::kFullGarbageCollection);
  87. }
  88. bool IsSameOrigin(const GURL& l, const GURL& r) {
  89. return url::Origin::Create(l).IsSameOriginWith(url::Origin::Create(r));
  90. }
  91. void Initialize(v8::Local<v8::Object> exports,
  92. v8::Local<v8::Value> unused,
  93. v8::Local<v8::Context> context,
  94. void* priv) {
  95. mate::Dictionary dict(context->GetIsolate(), exports);
  96. dict.SetMethod("getHiddenValue", &GetHiddenValue);
  97. dict.SetMethod("setHiddenValue", &SetHiddenValue);
  98. dict.SetMethod("deleteHiddenValue", &DeleteHiddenValue);
  99. dict.SetMethod("getObjectHash", &GetObjectHash);
  100. dict.SetMethod("takeHeapSnapshot", &TakeHeapSnapshot);
  101. dict.SetMethod("setRemoteCallbackFreer", &atom::RemoteCallbackFreer::BindTo);
  102. dict.SetMethod("setRemoteObjectFreer", &atom::RemoteObjectFreer::BindTo);
  103. dict.SetMethod("addRemoteObjectRef", &atom::RemoteObjectFreer::AddRef);
  104. dict.SetMethod("createIDWeakMap", &atom::api::KeyWeakMap<int32_t>::Create);
  105. dict.SetMethod(
  106. "createDoubleIDWeakMap",
  107. &atom::api::KeyWeakMap<std::pair<std::string, int32_t>>::Create);
  108. dict.SetMethod("requestGarbageCollectionForTesting",
  109. &RequestGarbageCollectionForTesting);
  110. dict.SetMethod("isSameOrigin", &IsSameOrigin);
  111. }
  112. } // namespace
  113. NODE_LINKED_MODULE_CONTEXT_AWARE(atom_common_v8_util, Initialize)