trackable_object.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Copyright (c) 2015 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #ifndef ATOM_BROWSER_API_TRACKABLE_OBJECT_H_
  5. #define ATOM_BROWSER_API_TRACKABLE_OBJECT_H_
  6. #include <vector>
  7. #include "atom/browser/api/event_emitter.h"
  8. #include "atom/common/key_weak_map.h"
  9. #include "base/bind.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "native_mate/object_template_builder.h"
  12. namespace base {
  13. class SupportsUserData;
  14. }
  15. namespace mate {
  16. // Users should use TrackableObject instead.
  17. class TrackableObjectBase {
  18. public:
  19. TrackableObjectBase();
  20. // The ID in weak map.
  21. int32_t weak_map_id() const { return weak_map_id_; }
  22. // Wrap TrackableObject into a class that SupportsUserData.
  23. void AttachAsUserData(base::SupportsUserData* wrapped);
  24. // Get the weak_map_id from SupportsUserData.
  25. static int32_t GetIDFromWrappedClass(base::SupportsUserData* wrapped);
  26. protected:
  27. virtual ~TrackableObjectBase();
  28. // Returns a closure that can destroy the native class.
  29. base::OnceClosure GetDestroyClosure();
  30. int32_t weak_map_id_ = 0;
  31. private:
  32. void Destroy();
  33. base::WeakPtrFactory<TrackableObjectBase> weak_factory_;
  34. DISALLOW_COPY_AND_ASSIGN(TrackableObjectBase);
  35. };
  36. // All instances of TrackableObject will be kept in a weak map and can be got
  37. // from its ID.
  38. template <typename T>
  39. class TrackableObject : public TrackableObjectBase,
  40. public mate::EventEmitter<T> {
  41. public:
  42. // Mark the JS object as destroyed.
  43. void MarkDestroyed() {
  44. v8::Local<v8::Object> wrapper = Wrappable<T>::GetWrapper();
  45. if (!wrapper.IsEmpty()) {
  46. wrapper->SetAlignedPointerInInternalField(0, nullptr);
  47. }
  48. }
  49. bool IsDestroyed() {
  50. v8::Local<v8::Object> wrapper = Wrappable<T>::GetWrapper();
  51. return wrapper->InternalFieldCount() == 0 ||
  52. wrapper->GetAlignedPointerFromInternalField(0) == nullptr;
  53. }
  54. // Finds out the TrackableObject from its ID in weak map.
  55. static T* FromWeakMapID(v8::Isolate* isolate, int32_t id) {
  56. if (!weak_map_)
  57. return nullptr;
  58. v8::MaybeLocal<v8::Object> object = weak_map_->Get(isolate, id);
  59. if (object.IsEmpty())
  60. return nullptr;
  61. T* self = nullptr;
  62. mate::ConvertFromV8(isolate, object.ToLocalChecked(), &self);
  63. return self;
  64. }
  65. // Finds out the TrackableObject from the class it wraps.
  66. static T* FromWrappedClass(v8::Isolate* isolate,
  67. base::SupportsUserData* wrapped) {
  68. int32_t id = GetIDFromWrappedClass(wrapped);
  69. if (!id)
  70. return nullptr;
  71. return FromWeakMapID(isolate, id);
  72. }
  73. // Returns all objects in this class's weak map.
  74. static std::vector<v8::Local<v8::Object>> GetAll(v8::Isolate* isolate) {
  75. if (weak_map_)
  76. return weak_map_->Values(isolate);
  77. else
  78. return std::vector<v8::Local<v8::Object>>();
  79. }
  80. // Removes this instance from the weak map.
  81. void RemoveFromWeakMap() {
  82. if (weak_map_ && weak_map_->Has(weak_map_id()))
  83. weak_map_->Remove(weak_map_id());
  84. }
  85. protected:
  86. TrackableObject() { weak_map_id_ = ++next_id_; }
  87. ~TrackableObject() override { RemoveFromWeakMap(); }
  88. void InitWith(v8::Isolate* isolate, v8::Local<v8::Object> wrapper) override {
  89. WrappableBase::InitWith(isolate, wrapper);
  90. if (!weak_map_) {
  91. weak_map_ = new atom::KeyWeakMap<int32_t>;
  92. }
  93. weak_map_->Set(isolate, weak_map_id_, wrapper);
  94. }
  95. private:
  96. static int32_t next_id_;
  97. static atom::KeyWeakMap<int32_t>* weak_map_; // leaked on purpose
  98. DISALLOW_COPY_AND_ASSIGN(TrackableObject);
  99. };
  100. template <typename T>
  101. int32_t TrackableObject<T>::next_id_ = 0;
  102. template <typename T>
  103. atom::KeyWeakMap<int32_t>* TrackableObject<T>::weak_map_ = nullptr;
  104. } // namespace mate
  105. #endif // ATOM_BROWSER_API_TRACKABLE_OBJECT_H_