[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| 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 | |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
rockot | 7c6bf95 | 2016-07-14 00:34:11 | [diff] [blame] | 10 | #include <map> |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 11 | #include <memory> |
rockot | 7c6bf95 | 2016-07-14 00:34:11 | [diff] [blame] | 12 | #include <string> |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 13 | #include <vector> |
| 14 | |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 15 | #include "base/component_export.h" |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 16 | #include "base/macros.h" |
rockot | 506f92fa2 | 2016-03-23 01:32:18 | [diff] [blame] | 17 | #include "base/memory/ref_counted.h" |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 18 | #include "base/memory/weak_ptr.h" |
rockot | a34707ca | 2016-07-20 04:28:32 | [diff] [blame] | 19 | #include "base/single_thread_task_runner.h" |
rockot | 506f92fa2 | 2016-03-23 01:32:18 | [diff] [blame] | 20 | #include "base/synchronization/lock.h" |
| 21 | #include "base/task_runner.h" |
rockot | a34707ca | 2016-07-20 04:28:32 | [diff] [blame] | 22 | #include "base/threading/thread_task_runner_handle.h" |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 23 | #include "build/build_config.h" |
rockot | a628d0b | 2017-02-09 08:40:15 | [diff] [blame] | 24 | #include "ipc/ipc.mojom.h" |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 25 | #include "ipc/ipc_channel.h" |
| 26 | #include "ipc/ipc_channel_factory.h" |
amistry | d4aa70d | 2016-06-23 07:52:37 | [diff] [blame] | 27 | #include "ipc/ipc_message_pipe_reader.h" |
| 28 | #include "ipc/ipc_mojo_bootstrap.h" |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 29 | #include "mojo/public/cpp/system/core.h" |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 30 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 31 | namespace IPC { |
| 32 | |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 33 | // Mojo-based IPC::Channel implementation over a Mojo message pipe. |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 34 | // |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 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. |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 38 | // |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 39 | // TODO(morrita): Add APIs to create extra MessagePipes to let |
| 40 | // Mojo-based objects talk over this Channel. |
| 41 | // |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 42 | class COMPONENT_EXPORT(IPC) ChannelMojo |
| 43 | : public Channel, |
| 44 | public Channel::AssociatedInterfaceSupport, |
| 45 | public internal::MessagePipeReader::Delegate { |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 46 | public: |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 47 | // Creates a ChannelMojo. |
Hajime Hoshi | a98f110 | 2017-11-20 06:34:35 | [diff] [blame] | 48 | static std::unique_ptr<ChannelMojo> Create( |
| 49 | mojo::ScopedMessagePipeHandle handle, |
| 50 | Mode mode, |
| 51 | Listener* listener, |
Hajime Hoshi | fe3ecdc | 2017-11-28 03:34:13 | [diff] [blame] | 52 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
Sigurdur Asgeirsson | d655dd65f | 2019-11-12 19:32:20 | [diff] [blame] | 53 | const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner, |
| 54 | const scoped_refptr<mojo::internal::MessageQuotaChecker>& quota_checker); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 55 | |
| 56 | // Create a factory object for ChannelMojo. |
| 57 | // The factory is used to create Mojo-based ChannelProxy family. |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 58 | // |host| must not be null. |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 59 | static std::unique_ptr<ChannelFactory> CreateServerFactory( |
rockot | a34707ca | 2016-07-20 04:28:32 | [diff] [blame] | 60 | mojo::ScopedMessagePipeHandle handle, |
Hajime Hoshi | a98f110 | 2017-11-20 06:34:35 | [diff] [blame] | 61 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
| 62 | const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner); |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 63 | |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 64 | static std::unique_ptr<ChannelFactory> CreateClientFactory( |
rockot | a34707ca | 2016-07-20 04:28:32 | [diff] [blame] | 65 | mojo::ScopedMessagePipeHandle handle, |
Hajime Hoshi | a98f110 | 2017-11-20 06:34:35 | [diff] [blame] | 66 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
| 67 | const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 68 | |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 69 | ~ChannelMojo() override; |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 70 | |
| 71 | // Channel implementation |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 72 | bool Connect() override; |
rockot | 1018875 | 2016-09-08 18:24:56 | [diff] [blame] | 73 | void Pause() override; |
rockot | 401fb2c | 2016-09-06 18:35:57 | [diff] [blame] | 74 | void Unpause(bool flush) override; |
| 75 | void Flush() override; |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 76 | void Close() override; |
| 77 | bool Send(Message* message) override; |
rockot | 7c6bf95 | 2016-07-14 00:34:11 | [diff] [blame] | 78 | Channel::AssociatedInterfaceSupport* GetAssociatedInterfaceSupport() override; |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 79 | |
morrita | 3b41d6c | 2014-09-11 19:06:29 | [diff] [blame] | 80 | // These access protected API of IPC::Message, which has ChannelMojo |
| 81 | // as a friend class. |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 82 | static MojoResult WriteToMessageAttachmentSet( |
Eve Martin-Jones | 475e7e6 | 2018-02-13 22:57:25 | [diff] [blame] | 83 | base::Optional<std::vector<mojo::native::SerializedHandlePtr>> handles, |
morrita | 3b41d6c | 2014-09-11 19:06:29 | [diff] [blame] | 84 | Message* message); |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 85 | static MojoResult ReadFromMessageAttachmentSet( |
| 86 | Message* message, |
Eve Martin-Jones | 475e7e6 | 2018-02-13 22:57:25 | [diff] [blame] | 87 | base::Optional<std::vector<mojo::native::SerializedHandlePtr>>* handles); |
morrita | 3b41d6c | 2014-09-11 19:06:29 | [diff] [blame] | 88 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 89 | // MessagePipeReader::Delegate |
sammc | f810f07f | 2016-11-10 22:34:07 | [diff] [blame] | 90 | void OnPeerPidReceived(int32_t peer_pid) override; |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 91 | void OnMessageReceived(const Message& message) override; |
Roman Karasev | a43d5b4e | 2017-12-21 03:06:02 | [diff] [blame] | 92 | void OnBrokenDataReceived() override; |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 93 | void OnPipeError() override; |
rockot | 7c6bf95 | 2016-07-14 00:34:11 | [diff] [blame] | 94 | void OnAssociatedInterfaceRequest( |
| 95 | const std::string& name, |
| 96 | mojo::ScopedInterfaceEndpointHandle handle) override; |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 97 | |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 98 | private: |
rockot | a34707ca | 2016-07-20 04:28:32 | [diff] [blame] | 99 | ChannelMojo( |
| 100 | mojo::ScopedMessagePipeHandle handle, |
| 101 | Mode mode, |
| 102 | Listener* listener, |
Hajime Hoshi | a98f110 | 2017-11-20 06:34:35 | [diff] [blame] | 103 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
Sigurdur Asgeirsson | d655dd65f | 2019-11-12 19:32:20 | [diff] [blame] | 104 | const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner, |
| 105 | const scoped_refptr<mojo::internal::MessageQuotaChecker>& quota_checker); |
morrita | 3b41d6c | 2014-09-11 19:06:29 | [diff] [blame] | 106 | |
rockot | a628d0b | 2017-02-09 08:40:15 | [diff] [blame] | 107 | void ForwardMessageFromThreadSafePtr(mojo::Message message); |
| 108 | void ForwardMessageWithResponderFromThreadSafePtr( |
| 109 | mojo::Message message, |
| 110 | std::unique_ptr<mojo::MessageReceiver> responder); |
| 111 | |
rockot | 7c6bf95 | 2016-07-14 00:34:11 | [diff] [blame] | 112 | // Channel::AssociatedInterfaceSupport: |
rockot | a628d0b | 2017-02-09 08:40:15 | [diff] [blame] | 113 | std::unique_ptr<mojo::ThreadSafeForwarder<mojom::Channel>> |
| 114 | CreateThreadSafeChannel() override; |
rockot | 7c6bf95 | 2016-07-14 00:34:11 | [diff] [blame] | 115 | void AddGenericAssociatedInterface( |
| 116 | const std::string& name, |
| 117 | const GenericAssociatedInterfaceFactory& factory) override; |
| 118 | void GetGenericRemoteAssociatedInterface( |
| 119 | const std::string& name, |
| 120 | mojo::ScopedInterfaceEndpointHandle handle) override; |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 121 | |
Wez | 888ef82 | 2017-10-30 16:46:06 | [diff] [blame] | 122 | base::WeakPtr<ChannelMojo> weak_ptr_; |
| 123 | |
rockot | 506f92fa2 | 2016-03-23 01:32:18 | [diff] [blame] | 124 | // A TaskRunner which runs tasks on the ChannelMojo's owning thread. |
rockot | a628d0b | 2017-02-09 08:40:15 | [diff] [blame] | 125 | scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
rockot | 506f92fa2 | 2016-03-23 01:32:18 | [diff] [blame] | 126 | |
| 127 | const mojo::MessagePipeHandle pipe_; |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 128 | std::unique_ptr<MojoBootstrap> bootstrap_; |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 129 | Listener* listener_; |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 130 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 131 | std::unique_ptr<internal::MessagePipeReader> message_reader_; |
| 132 | |
| 133 | base::Lock associated_interface_lock_; |
rockot | 7c6bf95 | 2016-07-14 00:34:11 | [diff] [blame] | 134 | std::map<std::string, GenericAssociatedInterfaceFactory> |
| 135 | associated_interfaces_; |
| 136 | |
Jeremy Roman | 7c5cfabd | 2019-08-12 15:45:27 | [diff] [blame] | 137 | base::WeakPtrFactory<ChannelMojo> weak_factory_{this}; |
anujk.sharma | 0184ced | 2014-08-28 06:49:02 | [diff] [blame] | 138 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 139 | DISALLOW_COPY_AND_ASSIGN(ChannelMojo); |
| 140 | }; |
| 141 | |
| 142 | } // namespace IPC |
| 143 | |
| 144 | #endif // IPC_IPC_CHANNEL_MOJO_H_ |