pinnable.h 878 B

123456789101112131415161718192021222324252627282930313233
  1. // Copyright (c) 2020 Slack Technologies, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #ifndef ELECTRON_SHELL_COMMON_GIN_HELPER_PINNABLE_H_
  5. #define ELECTRON_SHELL_COMMON_GIN_HELPER_PINNABLE_H_
  6. #include "v8/include/v8.h"
  7. namespace gin_helper {
  8. template <typename T>
  9. class Pinnable {
  10. protected:
  11. // Prevent the object from being garbage collected until Unpin() is called.
  12. void Pin(v8::Isolate* isolate) {
  13. v8::HandleScope scope(isolate);
  14. v8::Local<v8::Value> wrapper;
  15. if (static_cast<T*>(this)->GetWrapper(isolate).ToLocal(&wrapper)) {
  16. pinned_.Reset(isolate, wrapper);
  17. }
  18. }
  19. // Allow the object to be garbage collected.
  20. void Unpin() { pinned_.Reset(); }
  21. private:
  22. v8::Global<v8::Value> pinned_;
  23. };
  24. } // namespace gin_helper
  25. #endif // ELECTRON_SHELL_COMMON_GIN_HELPER_PINNABLE_H_