ipc_message_pipe_reader.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2014 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 IPC_IPC_MESSAGE_PIPE_READER_H_
  5. #define IPC_IPC_MESSAGE_PIPE_READER_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <vector>
  9. #include "base/atomicops.h"
  10. #include "base/compiler_specific.h"
  11. #include "base/component_export.h"
  12. #include "base/memory/raw_ptr.h"
  13. #include "base/memory/weak_ptr.h"
  14. #include "base/process/process_handle.h"
  15. #include "base/threading/thread_checker.h"
  16. #include "ipc/ipc.mojom.h"
  17. #include "ipc/ipc_message.h"
  18. #include "mojo/public/cpp/bindings/associated_receiver.h"
  19. #include "mojo/public/cpp/bindings/associated_remote.h"
  20. #include "mojo/public/cpp/bindings/generic_pending_associated_receiver.h"
  21. #include "mojo/public/cpp/bindings/pending_associated_receiver.h"
  22. #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h"
  23. #include "mojo/public/cpp/bindings/shared_remote.h"
  24. #include "mojo/public/cpp/system/core.h"
  25. #include "mojo/public/cpp/system/message_pipe.h"
  26. namespace IPC {
  27. namespace internal {
  28. // A helper class to handle bytestream directly over mojo::MessagePipe
  29. // in template-method pattern. MessagePipeReader manages the lifetime
  30. // of given MessagePipe and participates the event loop, and
  31. // read the stream and call the client when it is ready.
  32. //
  33. // Each client has to:
  34. //
  35. // * Provide a subclass implemenation of a specific use of a MessagePipe
  36. // and implement callbacks.
  37. // * Create the subclass instance with a MessagePipeHandle.
  38. // The constructor automatically start listening on the pipe.
  39. //
  40. // All functions must be called on the IO thread, except for Send(), which can
  41. // be called on any thread. All |Delegate| functions will be called on the IO
  42. // thread.
  43. //
  44. class COMPONENT_EXPORT(IPC) MessagePipeReader : public mojom::Channel {
  45. public:
  46. class Delegate {
  47. public:
  48. virtual void OnPeerPidReceived(int32_t peer_pid) = 0;
  49. virtual void OnMessageReceived(const Message& message) = 0;
  50. virtual void OnBrokenDataReceived() = 0;
  51. virtual void OnPipeError() = 0;
  52. virtual void OnAssociatedInterfaceRequest(
  53. mojo::GenericPendingAssociatedReceiver receiver) = 0;
  54. };
  55. // Builds a reader that reads messages from |receive_handle| and lets
  56. // |delegate| know.
  57. //
  58. // |pipe| is the message pipe handle corresponding to the channel's primary
  59. // interface. This is the message pipe underlying both |sender| and
  60. // |receiver|.
  61. //
  62. // Both |sender| and |receiver| must be non-null.
  63. //
  64. // Note that MessagePipeReader doesn't delete |delegate|.
  65. MessagePipeReader(mojo::MessagePipeHandle pipe,
  66. mojo::PendingAssociatedRemote<mojom::Channel> sender,
  67. mojo::PendingAssociatedReceiver<mojom::Channel> receiver,
  68. scoped_refptr<base::SequencedTaskRunner> task_runner,
  69. Delegate* delegate);
  70. MessagePipeReader(const MessagePipeReader&) = delete;
  71. MessagePipeReader& operator=(const MessagePipeReader&) = delete;
  72. ~MessagePipeReader() override;
  73. void FinishInitializationOnIOThread(base::ProcessId self_pid);
  74. // Close and destroy the MessagePipe.
  75. void Close();
  76. // Return true if the MessagePipe is alive.
  77. bool IsValid() { return sender_.is_bound(); }
  78. // Sends an IPC::Message to the other end of the pipe. Safe to call from any
  79. // thread.
  80. bool Send(std::unique_ptr<Message> message);
  81. // Requests an associated interface from the other end of the pipe.
  82. void GetRemoteInterface(mojo::GenericPendingAssociatedReceiver receiver);
  83. mojo::AssociatedRemote<mojom::Channel>& sender() { return sender_; }
  84. mojom::Channel& thread_safe_sender() { return thread_safe_sender_->proxy(); }
  85. protected:
  86. void OnPipeClosed();
  87. void OnPipeError(MojoResult error);
  88. private:
  89. // mojom::Channel:
  90. void SetPeerPid(int32_t peer_pid) override;
  91. void Receive(MessageView message_view) override;
  92. void GetAssociatedInterface(
  93. mojo::GenericPendingAssociatedReceiver receiver) override;
  94. void ForwardMessage(mojo::Message message);
  95. // |delegate_| is null once the message pipe is closed.
  96. raw_ptr<Delegate> delegate_;
  97. mojo::AssociatedRemote<mojom::Channel> sender_;
  98. std::unique_ptr<mojo::ThreadSafeForwarder<mojom::Channel>>
  99. thread_safe_sender_;
  100. mojo::AssociatedReceiver<mojom::Channel> receiver_;
  101. base::ThreadChecker thread_checker_;
  102. base::WeakPtrFactory<MessagePipeReader> weak_ptr_factory_{this};
  103. };
  104. } // namespace internal
  105. } // namespace IPC
  106. #endif // IPC_IPC_MESSAGE_PIPE_READER_H_