OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef IPC_IPC_CHANNEL_H_ | 5 #ifndef IPC_IPC_CHANNEL_H_ |
6 #define IPC_IPC_CHANNEL_H_ | 6 #define IPC_IPC_CHANNEL_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include <memory> | 11 #include <memory> |
12 #include <string> | 12 #include <string> |
13 | 13 |
14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
15 #include "base/files/scoped_file.h" | 15 #include "base/files/scoped_file.h" |
16 #include "base/process/process.h" | 16 #include "base/process/process.h" |
17 #include "build/build_config.h" | 17 #include "build/build_config.h" |
18 #include "ipc/ipc_channel_handle.h" | 18 #include "ipc/ipc_channel_handle.h" |
19 #include "ipc/ipc_endpoint.h" | 19 #include "ipc/ipc_endpoint.h" |
20 #include "ipc/ipc_message.h" | 20 #include "ipc/ipc_message.h" |
| 21 #include "mojo/public/cpp/bindings/associated_group.h" |
| 22 #include "mojo/public/cpp/bindings/associated_interface_ptr.h" |
| 23 #include "mojo/public/cpp/bindings/associated_interface_request.h" |
| 24 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" |
21 | 25 |
22 #if defined(OS_POSIX) | 26 #if defined(OS_POSIX) |
23 #include <sys/types.h> | 27 #include <sys/types.h> |
24 #endif | 28 #endif |
25 | 29 |
26 namespace IPC { | 30 namespace IPC { |
27 | 31 |
28 class Listener; | 32 class Listener; |
29 | 33 |
30 //------------------------------------------------------------------------------ | 34 //------------------------------------------------------------------------------ |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 HELLO_MESSAGE_TYPE = UINT16_MAX, | 90 HELLO_MESSAGE_TYPE = UINT16_MAX, |
87 // The CLOSE_FD_MESSAGE_TYPE is used in the IPC class to | 91 // The CLOSE_FD_MESSAGE_TYPE is used in the IPC class to |
88 // work around a bug in sendmsg() on Mac. When an FD is sent | 92 // work around a bug in sendmsg() on Mac. When an FD is sent |
89 // over the socket, a CLOSE_FD_MESSAGE is sent with hops = 2. | 93 // over the socket, a CLOSE_FD_MESSAGE is sent with hops = 2. |
90 // The client will return the message with hops = 1, *after* it | 94 // The client will return the message with hops = 1, *after* it |
91 // has received the message that contains the FD. When we | 95 // has received the message that contains the FD. When we |
92 // receive it again on the sender side, we close the FD. | 96 // receive it again on the sender side, we close the FD. |
93 CLOSE_FD_MESSAGE_TYPE = HELLO_MESSAGE_TYPE - 1 | 97 CLOSE_FD_MESSAGE_TYPE = HELLO_MESSAGE_TYPE - 1 |
94 }; | 98 }; |
95 | 99 |
| 100 // Helper interface a Channel may implement to expose support for associated |
| 101 // Mojo interfaces. |
| 102 class IPC_EXPORT AssociatedInterfaceSupport { |
| 103 public: |
| 104 using GenericAssociatedInterfaceFactory = |
| 105 base::Callback<void(mojo::ScopedInterfaceEndpointHandle)>; |
| 106 |
| 107 virtual ~AssociatedInterfaceSupport() {} |
| 108 |
| 109 // Accesses the AssociatedGroup used to associate new interface endpoints |
| 110 // with this Channel. |
| 111 virtual mojo::AssociatedGroup* GetAssociatedGroup() = 0; |
| 112 |
| 113 // Adds an interface factory to this channel for interface |name|. |
| 114 virtual void AddGenericAssociatedInterface( |
| 115 const std::string& name, |
| 116 const GenericAssociatedInterfaceFactory& factory) = 0; |
| 117 |
| 118 // Requests an associated interface from the remote endpoint. |
| 119 virtual void GetGenericRemoteAssociatedInterface( |
| 120 const std::string& name, |
| 121 mojo::ScopedInterfaceEndpointHandle handle) = 0; |
| 122 |
| 123 // Template helper to add an interface factory to this channel. |
| 124 template <typename Interface> |
| 125 using AssociatedInterfaceFactory = |
| 126 base::Callback<void(mojo::AssociatedInterfaceRequest<Interface>)>; |
| 127 template <typename Interface> |
| 128 void AddAssociatedInterface( |
| 129 const AssociatedInterfaceFactory<Interface>& factory) { |
| 130 AddGenericAssociatedInterface( |
| 131 Interface::Name_, |
| 132 base::Bind(&BindAssociatedInterfaceRequest<Interface>, factory)); |
| 133 } |
| 134 |
| 135 // Template helper to request a remote associated interface. |
| 136 template <typename Interface> |
| 137 void GetRemoteAssociatedInterface( |
| 138 mojo::AssociatedInterfacePtr<Interface>* proxy) { |
| 139 mojo::AssociatedInterfaceRequest<Interface> request = |
| 140 mojo::GetProxy(proxy, GetAssociatedGroup()); |
| 141 GetGenericRemoteAssociatedInterface( |
| 142 Interface::Name_, request.PassHandle()); |
| 143 } |
| 144 |
| 145 private: |
| 146 template <typename Interface> |
| 147 static void BindAssociatedInterfaceRequest( |
| 148 const AssociatedInterfaceFactory<Interface>& factory, |
| 149 mojo::ScopedInterfaceEndpointHandle handle) { |
| 150 mojo::AssociatedInterfaceRequest<Interface> request; |
| 151 request.Bind(std::move(handle)); |
| 152 factory.Run(std::move(request)); |
| 153 } |
| 154 }; |
| 155 |
96 // The maximum message size in bytes. Attempting to receive a message of this | 156 // The maximum message size in bytes. Attempting to receive a message of this |
97 // size or bigger results in a channel error. | 157 // size or bigger results in a channel error. |
98 static const size_t kMaximumMessageSize = 128 * 1024 * 1024; | 158 static const size_t kMaximumMessageSize = 128 * 1024 * 1024; |
99 | 159 |
100 // Amount of data to read at once from the pipe. | 160 // Amount of data to read at once from the pipe. |
101 static const size_t kReadBufferSize = 4 * 1024; | 161 static const size_t kReadBufferSize = 4 * 1024; |
102 | 162 |
103 // Maximum persistent read buffer size. Read buffer can grow larger to | 163 // Maximum persistent read buffer size. Read buffer can grow larger to |
104 // accommodate large messages, but it's recommended to shrink back to this | 164 // accommodate large messages, but it's recommended to shrink back to this |
105 // value because it fits 99.9% of all messages (see issue 529940 for data). | 165 // value because it fits 99.9% of all messages (see issue 529940 for data). |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 // Close this Channel explicitly. May be called multiple times. | 234 // Close this Channel explicitly. May be called multiple times. |
175 // On POSIX calling close on an IPC channel that listens for connections will | 235 // On POSIX calling close on an IPC channel that listens for connections will |
176 // cause it to close any accepted connections, and it will stop listening for | 236 // cause it to close any accepted connections, and it will stop listening for |
177 // new connections. If you just want to close the currently accepted | 237 // new connections. If you just want to close the currently accepted |
178 // connection and listen for new ones, use ResetToAcceptingConnectionState. | 238 // connection and listen for new ones, use ResetToAcceptingConnectionState. |
179 virtual void Close() = 0; | 239 virtual void Close() = 0; |
180 | 240 |
181 // Get its own process id. This value is told to the peer. | 241 // Get its own process id. This value is told to the peer. |
182 virtual base::ProcessId GetSelfPID() const = 0; | 242 virtual base::ProcessId GetSelfPID() const = 0; |
183 | 243 |
| 244 // Gets a helper for associating Mojo interfaces with this Channel. |
| 245 // |
| 246 // NOTE: Not all implementations support this. |
| 247 virtual AssociatedInterfaceSupport* GetAssociatedInterfaceSupport(); |
| 248 |
184 // Overridden from ipc::Sender. | 249 // Overridden from ipc::Sender. |
185 // Send a message over the Channel to the listener on the other end. | 250 // Send a message over the Channel to the listener on the other end. |
186 // | 251 // |
187 // |message| must be allocated using operator new. This object will be | 252 // |message| must be allocated using operator new. This object will be |
188 // deleted once the contents of the Message have been sent. | 253 // deleted once the contents of the Message have been sent. |
189 bool Send(Message* message) override = 0; | 254 bool Send(Message* message) override = 0; |
190 | 255 |
191 // IsSendThreadSafe returns true iff it's safe to call |Send| from non-IO | 256 // IsSendThreadSafe returns true iff it's safe to call |Send| from non-IO |
192 // threads. This is constant for the lifetime of the |Channel|. | 257 // threads. This is constant for the lifetime of the |Channel|. |
193 virtual bool IsSendThreadSafe() const; | 258 virtual bool IsSendThreadSafe() const; |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 | 349 |
285 #if defined(OS_POSIX) | 350 #if defined(OS_POSIX) |
286 // SocketPair() creates a pair of socket FDs suitable for using with | 351 // SocketPair() creates a pair of socket FDs suitable for using with |
287 // IPC::Channel. | 352 // IPC::Channel. |
288 IPC_EXPORT bool SocketPair(int* fd1, int* fd2); | 353 IPC_EXPORT bool SocketPair(int* fd1, int* fd2); |
289 #endif | 354 #endif |
290 | 355 |
291 } // namespace IPC | 356 } // namespace IPC |
292 | 357 |
293 #endif // IPC_IPC_CHANNEL_H_ | 358 #endif // IPC_IPC_CHANNEL_H_ |
OLD | NEW |