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