atom_api_v8_util.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.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. return Converter<Type1>::FromV8(isolate, array->Get(0), &out->first) &&
  37. Converter<Type2>::FromV8(isolate, array->Get(1), &out->second);
  38. }
  39. };
  40. } // namespace mate
  41. namespace {
  42. v8::Local<v8::Value> GetHiddenValue(v8::Isolate* isolate,
  43. v8::Local<v8::Object> object,
  44. v8::Local<v8::String> key) {
  45. v8::Local<v8::Context> context = isolate->GetCurrentContext();
  46. v8::Local<v8::Private> privateKey = v8::Private::ForApi(isolate, key);
  47. v8::Local<v8::Value> value;
  48. v8::Maybe<bool> result = object->HasPrivate(context, privateKey);
  49. if (!(result.IsJust() && result.FromJust()))
  50. return v8::Local<v8::Value>();
  51. if (object->GetPrivate(context, privateKey).ToLocal(&value))
  52. return value;
  53. return v8::Local<v8::Value>();
  54. }
  55. void SetHiddenValue(v8::Isolate* isolate,
  56. v8::Local<v8::Object> object,
  57. v8::Local<v8::String> key,
  58. v8::Local<v8::Value> value) {
  59. if (value.IsEmpty())
  60. return;
  61. v8::Local<v8::Context> context = isolate->GetCurrentContext();
  62. v8::Local<v8::Private> privateKey = v8::Private::ForApi(isolate, key);
  63. object->SetPrivate(context, privateKey, value);
  64. }
  65. void DeleteHiddenValue(v8::Isolate* isolate,
  66. v8::Local<v8::Object> object,
  67. v8::Local<v8::String> key) {
  68. v8::Local<v8::Context> context = isolate->GetCurrentContext();
  69. v8::Local<v8::Private> privateKey = v8::Private::ForApi(isolate, key);
  70. // Actually deleting the value would make force the object into
  71. // dictionary mode which is unnecessarily slow. Instead, we replace
  72. // the hidden value with "undefined".
  73. object->SetPrivate(context, privateKey, v8::Undefined(isolate));
  74. }
  75. int32_t GetObjectHash(v8::Local<v8::Object> object) {
  76. return object->GetIdentityHash();
  77. }
  78. void TakeHeapSnapshot(v8::Isolate* isolate) {
  79. isolate->GetHeapProfiler()->TakeHeapSnapshot();
  80. }
  81. void RequestGarbageCollectionForTesting(v8::Isolate* isolate) {
  82. isolate->RequestGarbageCollectionForTesting(
  83. v8::Isolate::GarbageCollectionType::kFullGarbageCollection);
  84. }
  85. bool IsSameOrigin(const GURL& l, const GURL& r) {
  86. return url::Origin::Create(l).IsSameOriginWith(url::Origin::Create(r));
  87. }
  88. void Initialize(v8::Local<v8::Object> exports,
  89. v8::Local<v8::Value> unused,
  90. v8::Local<v8::Context> context,
  91. void* priv) {
  92. mate::Dictionary dict(context->GetIsolate(), exports);
  93. dict.SetMethod("getHiddenValue", &GetHiddenValue);
  94. dict.SetMethod("setHiddenValue", &SetHiddenValue);
  95. dict.SetMethod("deleteHiddenValue", &DeleteHiddenValue);
  96. dict.SetMethod("getObjectHash", &GetObjectHash);
  97. dict.SetMethod("takeHeapSnapshot", &TakeHeapSnapshot);
  98. dict.SetMethod("setRemoteCallbackFreer", &atom::RemoteCallbackFreer::BindTo);
  99. dict.SetMethod("setRemoteObjectFreer", &atom::RemoteObjectFreer::BindTo);
  100. dict.SetMethod("addRemoteObjectRef", &atom::RemoteObjectFreer::AddRef);
  101. dict.SetMethod("createIDWeakMap", &atom::api::KeyWeakMap<int32_t>::Create);
  102. dict.SetMethod(
  103. "createDoubleIDWeakMap",
  104. &atom::api::KeyWeakMap<std::pair<std::string, int32_t>>::Create);
  105. dict.SetMethod("requestGarbageCollectionForTesting",
  106. &RequestGarbageCollectionForTesting);
  107. dict.SetMethod("isSameOrigin", &IsSameOrigin);
  108. }
  109. } // namespace
  110. NODE_LINKED_MODULE_CONTEXT_AWARE(atom_common_v8_util, Initialize)