event_emitter_mixin.cc 1.3 KB

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