xref: /aosp_15_r20/external/libchrome/ipc/ipc_channel_mojo.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_CHANNEL_MOJO_H_
6*635a8641SAndroid Build Coastguard Worker #define IPC_IPC_CHANNEL_MOJO_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 <map>
11*635a8641SAndroid Build Coastguard Worker #include <memory>
12*635a8641SAndroid Build Coastguard Worker #include <string>
13*635a8641SAndroid Build Coastguard Worker #include <vector>
14*635a8641SAndroid Build Coastguard Worker 
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/memory/ref_counted.h"
18*635a8641SAndroid Build Coastguard Worker #include "base/memory/weak_ptr.h"
19*635a8641SAndroid Build Coastguard Worker #include "base/single_thread_task_runner.h"
20*635a8641SAndroid Build Coastguard Worker #include "base/synchronization/lock.h"
21*635a8641SAndroid Build Coastguard Worker #include "base/task_runner.h"
22*635a8641SAndroid Build Coastguard Worker #include "base/threading/thread_task_runner_handle.h"
23*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h"
24*635a8641SAndroid Build Coastguard Worker #include "ipc/ipc.mojom.h"
25*635a8641SAndroid Build Coastguard Worker #include "ipc/ipc_channel.h"
26*635a8641SAndroid Build Coastguard Worker #include "ipc/ipc_channel_factory.h"
27*635a8641SAndroid Build Coastguard Worker #include "ipc/ipc_message_pipe_reader.h"
28*635a8641SAndroid Build Coastguard Worker #include "ipc/ipc_mojo_bootstrap.h"
29*635a8641SAndroid Build Coastguard Worker #include "mojo/public/cpp/bindings/thread_safe_interface_ptr.h"
30*635a8641SAndroid Build Coastguard Worker #include "mojo/public/cpp/system/core.h"
31*635a8641SAndroid Build Coastguard Worker 
32*635a8641SAndroid Build Coastguard Worker namespace IPC {
33*635a8641SAndroid Build Coastguard Worker 
34*635a8641SAndroid Build Coastguard Worker // Mojo-based IPC::Channel implementation over a Mojo message pipe.
35*635a8641SAndroid Build Coastguard Worker //
36*635a8641SAndroid Build Coastguard Worker // ChannelMojo builds a Mojo MessagePipe using the provided message pipe
37*635a8641SAndroid Build Coastguard Worker // |handle| and builds an associated interface for each direction on the
38*635a8641SAndroid Build Coastguard Worker // channel.
39*635a8641SAndroid Build Coastguard Worker //
40*635a8641SAndroid Build Coastguard Worker // TODO(morrita): Add APIs to create extra MessagePipes to let
41*635a8641SAndroid Build Coastguard Worker //                Mojo-based objects talk over this Channel.
42*635a8641SAndroid Build Coastguard Worker //
COMPONENT_EXPORT(IPC)43*635a8641SAndroid Build Coastguard Worker class COMPONENT_EXPORT(IPC) ChannelMojo
44*635a8641SAndroid Build Coastguard Worker     : public Channel,
45*635a8641SAndroid Build Coastguard Worker       public Channel::AssociatedInterfaceSupport,
46*635a8641SAndroid Build Coastguard Worker       public internal::MessagePipeReader::Delegate {
47*635a8641SAndroid Build Coastguard Worker  public:
48*635a8641SAndroid Build Coastguard Worker   // Creates a ChannelMojo.
49*635a8641SAndroid Build Coastguard Worker   static std::unique_ptr<ChannelMojo> Create(
50*635a8641SAndroid Build Coastguard Worker       mojo::ScopedMessagePipeHandle handle,
51*635a8641SAndroid Build Coastguard Worker       Mode mode,
52*635a8641SAndroid Build Coastguard Worker       Listener* listener,
53*635a8641SAndroid Build Coastguard Worker       const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
54*635a8641SAndroid Build Coastguard Worker       const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner);
55*635a8641SAndroid Build Coastguard Worker 
56*635a8641SAndroid Build Coastguard Worker   // Create a factory object for ChannelMojo.
57*635a8641SAndroid Build Coastguard Worker   // The factory is used to create Mojo-based ChannelProxy family.
58*635a8641SAndroid Build Coastguard Worker   // |host| must not be null.
59*635a8641SAndroid Build Coastguard Worker   static std::unique_ptr<ChannelFactory> CreateServerFactory(
60*635a8641SAndroid Build Coastguard Worker       mojo::ScopedMessagePipeHandle handle,
61*635a8641SAndroid Build Coastguard Worker       const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
62*635a8641SAndroid Build Coastguard Worker       const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner);
63*635a8641SAndroid Build Coastguard Worker 
64*635a8641SAndroid Build Coastguard Worker   static std::unique_ptr<ChannelFactory> CreateClientFactory(
65*635a8641SAndroid Build Coastguard Worker       mojo::ScopedMessagePipeHandle handle,
66*635a8641SAndroid Build Coastguard Worker       const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
67*635a8641SAndroid Build Coastguard Worker       const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner);
68*635a8641SAndroid Build Coastguard Worker 
69*635a8641SAndroid Build Coastguard Worker   ~ChannelMojo() override;
70*635a8641SAndroid Build Coastguard Worker 
71*635a8641SAndroid Build Coastguard Worker   // Channel implementation
72*635a8641SAndroid Build Coastguard Worker   bool Connect() override;
73*635a8641SAndroid Build Coastguard Worker   void Pause() override;
74*635a8641SAndroid Build Coastguard Worker   void Unpause(bool flush) override;
75*635a8641SAndroid Build Coastguard Worker   void Flush() override;
76*635a8641SAndroid Build Coastguard Worker   void Close() override;
77*635a8641SAndroid Build Coastguard Worker   bool Send(Message* message) override;
78*635a8641SAndroid Build Coastguard Worker   Channel::AssociatedInterfaceSupport* GetAssociatedInterfaceSupport() override;
79*635a8641SAndroid Build Coastguard Worker 
80*635a8641SAndroid Build Coastguard Worker   // These access protected API of IPC::Message, which has ChannelMojo
81*635a8641SAndroid Build Coastguard Worker   // as a friend class.
82*635a8641SAndroid Build Coastguard Worker   static MojoResult WriteToMessageAttachmentSet(
83*635a8641SAndroid Build Coastguard Worker       base::Optional<std::vector<mojo::native::SerializedHandlePtr>> handles,
84*635a8641SAndroid Build Coastguard Worker       Message* message);
85*635a8641SAndroid Build Coastguard Worker   static MojoResult ReadFromMessageAttachmentSet(
86*635a8641SAndroid Build Coastguard Worker       Message* message,
87*635a8641SAndroid Build Coastguard Worker       base::Optional<std::vector<mojo::native::SerializedHandlePtr>>* handles);
88*635a8641SAndroid Build Coastguard Worker 
89*635a8641SAndroid Build Coastguard Worker   // MessagePipeReader::Delegate
90*635a8641SAndroid Build Coastguard Worker   void OnPeerPidReceived(int32_t peer_pid) override;
91*635a8641SAndroid Build Coastguard Worker   void OnMessageReceived(const Message& message) override;
92*635a8641SAndroid Build Coastguard Worker   void OnBrokenDataReceived() override;
93*635a8641SAndroid Build Coastguard Worker   void OnPipeError() override;
94*635a8641SAndroid Build Coastguard Worker   void OnAssociatedInterfaceRequest(
95*635a8641SAndroid Build Coastguard Worker       const std::string& name,
96*635a8641SAndroid Build Coastguard Worker       mojo::ScopedInterfaceEndpointHandle handle) override;
97*635a8641SAndroid Build Coastguard Worker 
98*635a8641SAndroid Build Coastguard Worker  private:
99*635a8641SAndroid Build Coastguard Worker   ChannelMojo(
100*635a8641SAndroid Build Coastguard Worker       mojo::ScopedMessagePipeHandle handle,
101*635a8641SAndroid Build Coastguard Worker       Mode mode,
102*635a8641SAndroid Build Coastguard Worker       Listener* listener,
103*635a8641SAndroid Build Coastguard Worker       const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner,
104*635a8641SAndroid Build Coastguard Worker       const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner);
105*635a8641SAndroid Build Coastguard Worker 
106*635a8641SAndroid Build Coastguard Worker   void ForwardMessageFromThreadSafePtr(mojo::Message message);
107*635a8641SAndroid Build Coastguard Worker   void ForwardMessageWithResponderFromThreadSafePtr(
108*635a8641SAndroid Build Coastguard Worker       mojo::Message message,
109*635a8641SAndroid Build Coastguard Worker       std::unique_ptr<mojo::MessageReceiver> responder);
110*635a8641SAndroid Build Coastguard Worker 
111*635a8641SAndroid Build Coastguard Worker   // Channel::AssociatedInterfaceSupport:
112*635a8641SAndroid Build Coastguard Worker   std::unique_ptr<mojo::ThreadSafeForwarder<mojom::Channel>>
113*635a8641SAndroid Build Coastguard Worker   CreateThreadSafeChannel() override;
114*635a8641SAndroid Build Coastguard Worker   void AddGenericAssociatedInterface(
115*635a8641SAndroid Build Coastguard Worker       const std::string& name,
116*635a8641SAndroid Build Coastguard Worker       const GenericAssociatedInterfaceFactory& factory) override;
117*635a8641SAndroid Build Coastguard Worker   void GetGenericRemoteAssociatedInterface(
118*635a8641SAndroid Build Coastguard Worker       const std::string& name,
119*635a8641SAndroid Build Coastguard Worker       mojo::ScopedInterfaceEndpointHandle handle) override;
120*635a8641SAndroid Build Coastguard Worker 
121*635a8641SAndroid Build Coastguard Worker   base::WeakPtr<ChannelMojo> weak_ptr_;
122*635a8641SAndroid Build Coastguard Worker 
123*635a8641SAndroid Build Coastguard Worker   // A TaskRunner which runs tasks on the ChannelMojo's owning thread.
124*635a8641SAndroid Build Coastguard Worker   scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
125*635a8641SAndroid Build Coastguard Worker 
126*635a8641SAndroid Build Coastguard Worker   const mojo::MessagePipeHandle pipe_;
127*635a8641SAndroid Build Coastguard Worker   std::unique_ptr<MojoBootstrap> bootstrap_;
128*635a8641SAndroid Build Coastguard Worker   Listener* listener_;
129*635a8641SAndroid Build Coastguard Worker 
130*635a8641SAndroid Build Coastguard Worker   std::unique_ptr<internal::MessagePipeReader> message_reader_;
131*635a8641SAndroid Build Coastguard Worker 
132*635a8641SAndroid Build Coastguard Worker   base::Lock associated_interface_lock_;
133*635a8641SAndroid Build Coastguard Worker   std::map<std::string, GenericAssociatedInterfaceFactory>
134*635a8641SAndroid Build Coastguard Worker       associated_interfaces_;
135*635a8641SAndroid Build Coastguard Worker 
136*635a8641SAndroid Build Coastguard Worker   base::WeakPtrFactory<ChannelMojo> weak_factory_;
137*635a8641SAndroid Build Coastguard Worker 
138*635a8641SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(ChannelMojo);
139*635a8641SAndroid Build Coastguard Worker };
140*635a8641SAndroid Build Coastguard Worker 
141*635a8641SAndroid Build Coastguard Worker }  // namespace IPC
142*635a8641SAndroid Build Coastguard Worker 
143*635a8641SAndroid Build Coastguard Worker #endif  // IPC_IPC_CHANNEL_MOJO_H_
144