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