message_port.h 3.0 KB

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