message_port.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright (c) 2020 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_API_MESSAGE_PORT_H_
  5. #define ELECTRON_SHELL_BROWSER_API_MESSAGE_PORT_H_
  6. #include <memory>
  7. #include <vector>
  8. #include "gin/wrappable.h"
  9. #include "mojo/public/cpp/bindings/message.h"
  10. #include "shell/common/gin_helper/cleaned_up_at_exit.h"
  11. #include "third_party/blink/public/common/messaging/message_port_channel.h"
  12. #include "third_party/blink/public/common/messaging/message_port_descriptor.h"
  13. namespace gin {
  14. class Arguments;
  15. template <typename T>
  16. class Handle;
  17. } // namespace gin
  18. namespace mojo {
  19. class Connector;
  20. } // namespace mojo
  21. namespace electron {
  22. // A non-blink version of blink::MessagePort.
  23. class MessagePort : public gin::Wrappable<MessagePort>,
  24. public gin_helper::CleanedUpAtExit,
  25. public mojo::MessageReceiver {
  26. public:
  27. ~MessagePort() override;
  28. static gin::Handle<MessagePort> Create(v8::Isolate* isolate);
  29. void PostMessage(gin::Arguments* args);
  30. void Start();
  31. void Close();
  32. void Entangle(blink::MessagePortDescriptor port);
  33. void Entangle(blink::MessagePortChannel channel);
  34. blink::MessagePortChannel Disentangle();
  35. [[nodiscard]] bool IsEntangled() const;
  36. [[nodiscard]] bool IsNeutered() const;
  37. static std::vector<gin::Handle<MessagePort>> EntanglePorts(
  38. v8::Isolate* isolate,
  39. std::vector<blink::MessagePortChannel> channels);
  40. static std::vector<blink::MessagePortChannel> DisentanglePorts(
  41. v8::Isolate* isolate,
  42. const std::vector<gin::Handle<MessagePort>>& ports,
  43. bool* threw_exception);
  44. // gin::Wrappable
  45. static gin::WrapperInfo kWrapperInfo;
  46. gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
  47. v8::Isolate* isolate) override;
  48. const char* GetTypeName() override;
  49. private:
  50. MessagePort();
  51. // The blink version of MessagePort uses the very nice "ActiveScriptWrapper"
  52. // class, which keeps the object alive through the V8 embedder hooks into the
  53. // GC lifecycle: see
  54. // https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/platform/heap/thread_state.cc;l=258;drc=b892cf58e162a8f66cd76d7472f129fe0fb6a7d1
  55. // We do not have that luxury, so we brutishly use v8::Global to accomplish
  56. // something similar. Critically, whenever the value of
  57. // "HasPendingActivity()" changes, we must call Pin() or Unpin() as
  58. // appropriate.
  59. bool HasPendingActivity() const;
  60. void Pin();
  61. void Unpin();
  62. // mojo::MessageReceiver
  63. bool Accept(mojo::Message* mojo_message) override;
  64. std::unique_ptr<mojo::Connector> connector_;
  65. bool started_ = false;
  66. bool closed_ = false;
  67. v8::Global<v8::Value> pinned_;
  68. // The internal port owned by this class. The handle itself is moved into the
  69. // |connector_| while entangled.
  70. blink::MessagePortDescriptor port_;
  71. base::WeakPtrFactory<MessagePort> weak_factory_{this};
  72. };
  73. } // namespace electron
  74. #endif // ELECTRON_SHELL_BROWSER_API_MESSAGE_PORT_H_