parent_port.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright (c) 2022 Microsoft, 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_SERVICES_NODE_PARENT_PORT_H_
  5. #define ELECTRON_SHELL_SERVICES_NODE_PARENT_PORT_H_
  6. #include <memory>
  7. #include "gin/wrappable.h"
  8. #include "mojo/public/cpp/bindings/connector.h"
  9. #include "mojo/public/cpp/bindings/message.h"
  10. #include "shell/browser/event_emitter_mixin.h"
  11. namespace v8 {
  12. template <class T>
  13. class Local;
  14. class Value;
  15. class Isolate;
  16. } // namespace v8
  17. namespace gin {
  18. class Arguments;
  19. template <typename T>
  20. class Handle;
  21. } // namespace gin
  22. namespace electron {
  23. // There is only a single instance of this class
  24. // for the lifetime of a Utility Process which
  25. // also means that GC lifecycle is ignored by this class.
  26. class ParentPort : public gin::Wrappable<ParentPort>,
  27. public mojo::MessageReceiver {
  28. public:
  29. static ParentPort* GetInstance();
  30. static gin::Handle<ParentPort> Create(v8::Isolate* isolate);
  31. ParentPort(const ParentPort&) = delete;
  32. ParentPort& operator=(const ParentPort&) = delete;
  33. ParentPort();
  34. ~ParentPort() override;
  35. void Initialize(blink::MessagePortDescriptor port);
  36. // gin::Wrappable
  37. static gin::WrapperInfo kWrapperInfo;
  38. gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
  39. v8::Isolate* isolate) override;
  40. const char* GetTypeName() override;
  41. private:
  42. void PostMessage(v8::Local<v8::Value> message_value);
  43. void Close();
  44. void Start();
  45. void Pause();
  46. // mojo::MessageReceiver
  47. bool Accept(mojo::Message* mojo_message) override;
  48. bool connector_closed_ = false;
  49. std::unique_ptr<mojo::Connector> connector_;
  50. blink::MessagePortDescriptor port_;
  51. };
  52. } // namespace electron
  53. #endif // ELECTRON_SHELL_SERVICES_NODE_PARENT_PORT_H_