channel_posix.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef MOJO_CORE_CHANNEL_POSIX_H_
  5. #define MOJO_CORE_CHANNEL_POSIX_H_
  6. #include "mojo/core/channel.h"
  7. #include "base/containers/queue.h"
  8. #include "base/logging.h"
  9. #include "base/memory/ref_counted.h"
  10. #include "base/message_loop/message_pump_for_io.h"
  11. #include "base/synchronization/lock.h"
  12. #include "base/task/current_thread.h"
  13. #include "base/task/task_runner.h"
  14. #include "build/build_config.h"
  15. #include "mojo/core/core.h"
  16. namespace mojo {
  17. namespace core {
  18. class MessageView;
  19. class ChannelPosix : public Channel,
  20. public base::CurrentThread::DestructionObserver,
  21. public base::MessagePumpForIO::FdWatcher {
  22. public:
  23. ChannelPosix(Delegate* delegate,
  24. ConnectionParams connection_params,
  25. HandlePolicy handle_policy,
  26. scoped_refptr<base::SingleThreadTaskRunner> io_task_runner);
  27. ChannelPosix(const ChannelPosix&) = delete;
  28. ChannelPosix& operator=(const ChannelPosix&) = delete;
  29. void Start() override;
  30. void ShutDownImpl() override;
  31. void Write(MessagePtr message) override;
  32. void LeakHandle() override;
  33. bool GetReadPlatformHandles(const void* payload,
  34. size_t payload_size,
  35. size_t num_handles,
  36. const void* extra_header,
  37. size_t extra_header_size,
  38. std::vector<PlatformHandle>* handles,
  39. bool* deferred) override;
  40. bool GetReadPlatformHandlesForIpcz(
  41. size_t num_handles,
  42. std::vector<PlatformHandle>& handles) override;
  43. bool OnControlMessage(Message::MessageType message_type,
  44. const void* payload,
  45. size_t payload_size,
  46. std::vector<PlatformHandle> handles) override;
  47. protected:
  48. ~ChannelPosix() override;
  49. virtual void StartOnIOThread();
  50. virtual void ShutDownOnIOThread();
  51. virtual void OnWriteError(Error error);
  52. void RejectUpgradeOffer();
  53. void AcceptUpgradeOffer();
  54. // Keeps the Channel alive at least until explicit shutdown on the IO thread.
  55. scoped_refptr<Channel> self_;
  56. scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
  57. private:
  58. void WaitForWriteOnIOThread();
  59. void WaitForWriteOnIOThreadNoLock();
  60. // base::CurrentThread::DestructionObserver:
  61. void WillDestroyCurrentMessageLoop() override;
  62. // base::MessagePumpForIO::FdWatcher:
  63. void OnFileCanReadWithoutBlocking(int fd) override;
  64. void OnFileCanWriteWithoutBlocking(int fd) override;
  65. // Attempts to write a message directly to the channel. If the full message
  66. // cannot be written, it's queued and a wait is initiated to write the message
  67. // ASAP on the I/O thread.
  68. bool WriteNoLock(MessageView message_view);
  69. bool FlushOutgoingMessagesNoLock();
  70. #if !BUILDFLAG(IS_NACL)
  71. bool WriteOutgoingMessagesWithWritev();
  72. // FlushOutgoingMessagesWritevNoLock is equivalent to
  73. // FlushOutgoingMessagesNoLock except it looks for opportunities to make
  74. // only a single write syscall by using writev(2) instead of write(2). In
  75. // most situations this is very straight forward; however, when a handle
  76. // needs to be transferred we cannot use writev(2) and instead will fall
  77. // back to the standard write.
  78. bool FlushOutgoingMessagesWritevNoLock();
  79. #endif // !BUILDFLAG(IS_NACL)
  80. #if BUILDFLAG(IS_IOS)
  81. bool CloseHandles(const int* fds, size_t num_fds);
  82. #endif // BUILDFLAG(IS_IOS)
  83. // We may be initialized with a server socket, in which case this will be
  84. // valid until it accepts an incoming connection.
  85. PlatformChannelServerEndpoint server_;
  86. // The socket over which to communicate. May be passed in at construction time
  87. // or accepted over |server_|.
  88. base::ScopedFD socket_;
  89. // These watchers must only be accessed on the IO thread.
  90. std::unique_ptr<base::MessagePumpForIO::FdWatchController> read_watcher_;
  91. std::unique_ptr<base::MessagePumpForIO::FdWatchController> write_watcher_;
  92. base::circular_deque<base::ScopedFD> incoming_fds_;
  93. // Protects |pending_write_| and |outgoing_messages_|.
  94. base::Lock write_lock_;
  95. bool pending_write_ = false;
  96. bool reject_writes_ = false;
  97. base::circular_deque<MessageView> outgoing_messages_;
  98. bool leak_handle_ = false;
  99. #if BUILDFLAG(IS_IOS)
  100. base::Lock fds_to_close_lock_;
  101. std::vector<base::ScopedFD> fds_to_close_;
  102. #endif // BUILDFLAG(IS_IOS)
  103. };
  104. } // namespace core
  105. } // namespace mojo
  106. #endif // MOJO_CORE_CHANNEL_POSIX_H_