event_emitter_mixin.h 2.7 KB

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