event_emitter_mixin.cc 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. #include "shell/browser/event_emitter_mixin.h"
  5. #include "gin/public/wrapper_info.h"
  6. #include "shell/browser/api/electron_api_event_emitter.h"
  7. namespace gin_helper {
  8. namespace internal {
  9. gin::WrapperInfo kWrapperInfo = {gin::kEmbedderNativeGin};
  10. v8::Local<v8::FunctionTemplate> GetEventEmitterTemplate(v8::Isolate* isolate) {
  11. gin::PerIsolateData* data = gin::PerIsolateData::From(isolate);
  12. v8::Local<v8::FunctionTemplate> tmpl =
  13. data->GetFunctionTemplate(&kWrapperInfo);
  14. if (tmpl.IsEmpty()) {
  15. tmpl = v8::FunctionTemplate::New(isolate);
  16. v8::Local<v8::Context> context = isolate->GetCurrentContext();
  17. v8::Local<v8::Function> func = tmpl->GetFunction(context).ToLocalChecked();
  18. v8::Local<v8::Object> eventemitter_prototype =
  19. electron::GetEventEmitterPrototype(isolate);
  20. v8::Local<v8::Value> func_prototype;
  21. CHECK(func->Get(context, gin::StringToSymbol(isolate, "prototype"))
  22. .ToLocal(&func_prototype));
  23. CHECK(func_prototype.As<v8::Object>()
  24. ->SetPrototype(context, eventemitter_prototype)
  25. .ToChecked());
  26. data->SetFunctionTemplate(&kWrapperInfo, tmpl);
  27. }
  28. return tmpl;
  29. }
  30. } // namespace internal
  31. } // namespace gin_helper