message_port.h 2.9 KB

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