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