event_emitter_mixin.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright (c) 2019 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_BROWSER_EVENT_EMITTER_MIXIN_H_
  5. #define ELECTRON_SHELL_BROWSER_EVENT_EMITTER_MIXIN_H_
  6. #include <string_view>
  7. #include <utility>
  8. #include "gin/handle.h"
  9. #include "gin/object_template_builder.h"
  10. #include "shell/browser/javascript_environment.h"
  11. #include "shell/common/gin_helper/event.h"
  12. #include "shell/common/gin_helper/event_emitter.h"
  13. namespace gin_helper {
  14. template <typename T>
  15. class EventEmitterMixin {
  16. public:
  17. // disable copy
  18. EventEmitterMixin(const EventEmitterMixin&) = delete;
  19. EventEmitterMixin& operator=(const EventEmitterMixin&) = delete;
  20. // this.emit(name, new Event(), args...);
  21. // Returns true if event.preventDefault() was called during processing.
  22. template <typename... Args>
  23. bool Emit(const std::string_view name, Args&&... args) {
  24. v8::Isolate* isolate = electron::JavascriptEnvironment::GetIsolate();
  25. v8::HandleScope handle_scope(isolate);
  26. v8::Local<v8::Object> wrapper;
  27. if (!static_cast<T*>(this)->GetWrapper(isolate).ToLocal(&wrapper))
  28. return false;
  29. gin::Handle<internal::Event> event = internal::Event::New(isolate);
  30. gin_helper::EmitEvent(isolate, wrapper, name, event,
  31. std::forward<Args>(args)...);
  32. return event->GetDefaultPrevented();
  33. }
  34. // this.emit(name, args...);
  35. template <typename... Args>
  36. void EmitWithoutEvent(const std::string_view name, Args&&... args) {
  37. v8::Isolate* isolate = electron::JavascriptEnvironment::GetIsolate();
  38. v8::HandleScope handle_scope(isolate);
  39. v8::Local<v8::Object> wrapper;
  40. if (!static_cast<T*>(this)->GetWrapper(isolate).ToLocal(&wrapper))
  41. return;
  42. gin_helper::EmitEvent(isolate, wrapper, name, std::forward<Args>(args)...);
  43. }
  44. protected:
  45. EventEmitterMixin() = default;
  46. gin::ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate* isolate) {
  47. gin::PerIsolateData* data = gin::PerIsolateData::From(isolate);
  48. auto* wrapper_info = &(static_cast<T*>(this)->kWrapperInfo);
  49. v8::Local<v8::FunctionTemplate> constructor =
  50. data->GetFunctionTemplate(wrapper_info);
  51. if (constructor.IsEmpty()) {
  52. constructor = v8::FunctionTemplate::New(isolate);
  53. constructor->SetClassName(
  54. gin::StringToV8(isolate, static_cast<T*>(this)->GetTypeName()));
  55. constructor->Inherit(internal::GetEventEmitterTemplate(isolate));
  56. data->SetFunctionTemplate(wrapper_info, constructor);
  57. }
  58. return gin::ObjectTemplateBuilder(isolate,
  59. static_cast<T*>(this)->GetTypeName(),
  60. constructor->InstanceTemplate());
  61. }
  62. };
  63. } // namespace gin_helper
  64. #endif // ELECTRON_SHELL_BROWSER_EVENT_EMITTER_MIXIN_H_