morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [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 | 42ebda4 | 2015-12-22 11:39:04 +0900 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
rockot | a6edf83 | 2016-07-14 09:34:11 +0900 | [diff] [blame] | 10 | #include <map> |
danakj | c3fb6c5 | 2016-04-23 13:21:09 +0900 | [diff] [blame] | 11 | #include <memory> |
rockot | a6edf83 | 2016-07-14 09:34:11 +0900 | [diff] [blame] | 12 | #include <string> |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 13 | #include <vector> |
| 14 | |
avi | 42ebda4 | 2015-12-22 11:39:04 +0900 | [diff] [blame] | 15 | #include "base/macros.h" |
rockot | 6e3d492 | 2016-03-23 10:32:18 +0900 | [diff] [blame] | 16 | #include "base/memory/ref_counted.h" |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 17 | #include "base/memory/weak_ptr.h" |
rockot | 37e7fa4 | 2016-07-20 13:28:32 +0900 | [diff] [blame] | 18 | #include "base/single_thread_task_runner.h" |
rockot | 6e3d492 | 2016-03-23 10:32:18 +0900 | [diff] [blame] | 19 | #include "base/synchronization/lock.h" |
| 20 | #include "base/task_runner.h" |
rockot | 37e7fa4 | 2016-07-20 13:28:32 +0900 | [diff] [blame] | 21 | #include "base/threading/thread_task_runner_handle.h" |
avi | 42ebda4 | 2015-12-22 11:39:04 +0900 | [diff] [blame] | 22 | #include "build/build_config.h" |
rockot | 7900d34 | 2017-02-09 17:40:15 +0900 | [diff] [blame] | 23 | #include "ipc/ipc.mojom.h" |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 24 | #include "ipc/ipc_channel.h" |
| 25 | #include "ipc/ipc_channel_factory.h" |
| 26 | #include "ipc/ipc_export.h" |
amistry | 6555e69 | 2016-06-23 16:52:37 +0900 | [diff] [blame] | 27 | #include "ipc/ipc_message_pipe_reader.h" |
| 28 | #include "ipc/ipc_mojo_bootstrap.h" |
rockot | 7900d34 | 2017-02-09 17:40:15 +0900 | [diff] [blame] | 29 | #include "mojo/public/cpp/bindings/thread_safe_interface_ptr.h" |
rockot | af32acb | 2015-11-13 10:33:59 +0900 | [diff] [blame] | 30 | #include "mojo/public/cpp/system/core.h" |
Ken Rockot | b3a77c9 | 2017-09-14 13:23:41 +0900 | [diff] [blame] | 31 | #include "mojo/public/interfaces/bindings/native_struct.mojom.h" |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 32 | |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 33 | namespace IPC { |
| 34 | |
sammc | 6194d97 | 2016-03-08 07:38:04 +0900 | [diff] [blame] | 35 | // Mojo-based IPC::Channel implementation over a Mojo message pipe. |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 36 | // |
sammc | 0bcbfc7 | 2016-03-10 15:28:35 +0900 | [diff] [blame] | 37 | // ChannelMojo builds a Mojo MessagePipe using the provided message pipe |
| 38 | // |handle| and builds an associated interface for each direction on the |
| 39 | // channel. |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 40 | // |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 41 | // TODO(morrita): Add APIs to create extra MessagePipes to let |
| 42 | // Mojo-based objects talk over this Channel. |
| 43 | // |
Nico Weber | 1615c31 | 2017-08-16 04:19:27 +0900 | [diff] [blame] | 44 | class IPC_EXPORT ChannelMojo : public Channel, |
| 45 | public Channel::AssociatedInterfaceSupport, |
| 46 | public internal::MessagePipeReader::Delegate { |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 47 | public: |
sammc | 0bcbfc7 | 2016-03-10 15:28:35 +0900 | [diff] [blame] | 48 | // Creates a ChannelMojo. |
Hajime Hoshi | 595c856 | 2017-11-20 15:34:35 +0900 | [diff] [blame] | 49 | static std::unique_ptr<ChannelMojo> Create( |
| 50 | mojo::ScopedMessagePipeHandle handle, |
| 51 | Mode mode, |
| 52 | Listener* listener, |
Hajime Hoshi | 3816144 | 2017-11-28 12:34:13 +0900 | [diff] [blame] | 53 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
| 54 | const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner); |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 55 | |
| 56 | // Create a factory object for ChannelMojo. |
| 57 | // The factory is used to create Mojo-based ChannelProxy family. |
morrita | 7c48ab8 | 2014-09-24 06:16:00 +0900 | [diff] [blame] | 58 | // |host| must not be null. |
danakj | c3fb6c5 | 2016-04-23 13:21:09 +0900 | [diff] [blame] | 59 | static std::unique_ptr<ChannelFactory> CreateServerFactory( |
rockot | 37e7fa4 | 2016-07-20 13:28:32 +0900 | [diff] [blame] | 60 | mojo::ScopedMessagePipeHandle handle, |
Hajime Hoshi | 595c856 | 2017-11-20 15:34:35 +0900 | [diff] [blame] | 61 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
| 62 | const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner); |
morrita | 7c48ab8 | 2014-09-24 06:16:00 +0900 | [diff] [blame] | 63 | |
danakj | c3fb6c5 | 2016-04-23 13:21:09 +0900 | [diff] [blame] | 64 | static std::unique_ptr<ChannelFactory> CreateClientFactory( |
rockot | 37e7fa4 | 2016-07-20 13:28:32 +0900 | [diff] [blame] | 65 | mojo::ScopedMessagePipeHandle handle, |
Hajime Hoshi | 595c856 | 2017-11-20 15:34:35 +0900 | [diff] [blame] | 66 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
| 67 | const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner); |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 68 | |
dcheng | ef7721a | 2014-10-22 11:29:52 +0900 | [diff] [blame] | 69 | ~ChannelMojo() override; |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 70 | |
| 71 | // Channel implementation |
dcheng | ef7721a | 2014-10-22 11:29:52 +0900 | [diff] [blame] | 72 | bool Connect() override; |
rockot | e7c4790 | 2016-09-09 03:24:56 +0900 | [diff] [blame] | 73 | void Pause() override; |
rockot | 685505b | 2016-09-07 03:35:57 +0900 | [diff] [blame] | 74 | void Unpause(bool flush) override; |
| 75 | void Flush() override; |
dcheng | ef7721a | 2014-10-22 11:29:52 +0900 | [diff] [blame] | 76 | void Close() override; |
| 77 | bool Send(Message* message) override; |
rockot | a6edf83 | 2016-07-14 09:34:11 +0900 | [diff] [blame] | 78 | Channel::AssociatedInterfaceSupport* GetAssociatedInterfaceSupport() override; |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 79 | |
morrita | 42bda25 | 2014-09-12 04:06:29 +0900 | [diff] [blame] | 80 | // These access protected API of IPC::Message, which has ChannelMojo |
| 81 | // as a friend class. |
morrita | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 82 | static MojoResult WriteToMessageAttachmentSet( |
Ken Rockot | b3a77c9 | 2017-09-14 13:23:41 +0900 | [diff] [blame] | 83 | base::Optional<std::vector<mojo::native::SerializedHandlePtr>> handles, |
morrita | 42bda25 | 2014-09-12 04:06:29 +0900 | [diff] [blame] | 84 | Message* message); |
morrita | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 85 | static MojoResult ReadFromMessageAttachmentSet( |
| 86 | Message* message, |
Ken Rockot | b3a77c9 | 2017-09-14 13:23:41 +0900 | [diff] [blame] | 87 | base::Optional<std::vector<mojo::native::SerializedHandlePtr>>* handles); |
morrita | 42bda25 | 2014-09-12 04:06:29 +0900 | [diff] [blame] | 88 | |
rockot | a01dbc7 | 2016-07-23 06:18:07 +0900 | [diff] [blame] | 89 | // MessagePipeReader::Delegate |
sammc | b83485d | 2016-11-11 07:34:07 +0900 | [diff] [blame] | 90 | void OnPeerPidReceived(int32_t peer_pid) override; |
rockot | a01dbc7 | 2016-07-23 06:18:07 +0900 | [diff] [blame] | 91 | void OnMessageReceived(const Message& message) override; |
Roman Karasev | 02dec60 | 2017-12-21 12:06:02 +0900 | [diff] [blame^] | 92 | void OnBrokenDataReceived() override; |
rockot | a01dbc7 | 2016-07-23 06:18:07 +0900 | [diff] [blame] | 93 | void OnPipeError() override; |
rockot | a6edf83 | 2016-07-14 09:34:11 +0900 | [diff] [blame] | 94 | void OnAssociatedInterfaceRequest( |
| 95 | const std::string& name, |
| 96 | mojo::ScopedInterfaceEndpointHandle handle) override; |
morrita | 7c48ab8 | 2014-09-24 06:16:00 +0900 | [diff] [blame] | 97 | |
sammc | 6194d97 | 2016-03-08 07:38:04 +0900 | [diff] [blame] | 98 | private: |
rockot | 37e7fa4 | 2016-07-20 13:28:32 +0900 | [diff] [blame] | 99 | ChannelMojo( |
| 100 | mojo::ScopedMessagePipeHandle handle, |
| 101 | Mode mode, |
| 102 | Listener* listener, |
Hajime Hoshi | 595c856 | 2017-11-20 15:34:35 +0900 | [diff] [blame] | 103 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
| 104 | const scoped_refptr<base::SingleThreadTaskRunner>& proxy_task_runner); |
morrita | 42bda25 | 2014-09-12 04:06:29 +0900 | [diff] [blame] | 105 | |
rockot | 7900d34 | 2017-02-09 17:40:15 +0900 | [diff] [blame] | 106 | void ForwardMessageFromThreadSafePtr(mojo::Message message); |
| 107 | void ForwardMessageWithResponderFromThreadSafePtr( |
| 108 | mojo::Message message, |
| 109 | std::unique_ptr<mojo::MessageReceiver> responder); |
| 110 | |
rockot | a6edf83 | 2016-07-14 09:34:11 +0900 | [diff] [blame] | 111 | // Channel::AssociatedInterfaceSupport: |
rockot | 7900d34 | 2017-02-09 17:40:15 +0900 | [diff] [blame] | 112 | std::unique_ptr<mojo::ThreadSafeForwarder<mojom::Channel>> |
| 113 | CreateThreadSafeChannel() override; |
rockot | a6edf83 | 2016-07-14 09:34:11 +0900 | [diff] [blame] | 114 | void AddGenericAssociatedInterface( |
| 115 | const std::string& name, |
| 116 | const GenericAssociatedInterfaceFactory& factory) override; |
| 117 | void GetGenericRemoteAssociatedInterface( |
| 118 | const std::string& name, |
| 119 | mojo::ScopedInterfaceEndpointHandle handle) override; |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 120 | |
Wez | d7f09fa | 2017-10-31 01:46:06 +0900 | [diff] [blame] | 121 | base::WeakPtr<ChannelMojo> weak_ptr_; |
| 122 | |
rockot | 6e3d492 | 2016-03-23 10:32:18 +0900 | [diff] [blame] | 123 | // A TaskRunner which runs tasks on the ChannelMojo's owning thread. |
rockot | 7900d34 | 2017-02-09 17:40:15 +0900 | [diff] [blame] | 124 | scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
rockot | 6e3d492 | 2016-03-23 10:32:18 +0900 | [diff] [blame] | 125 | |
| 126 | const mojo::MessagePipeHandle pipe_; |
danakj | c3fb6c5 | 2016-04-23 13:21:09 +0900 | [diff] [blame] | 127 | std::unique_ptr<MojoBootstrap> bootstrap_; |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 128 | Listener* listener_; |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 129 | |
rockot | a01dbc7 | 2016-07-23 06:18:07 +0900 | [diff] [blame] | 130 | std::unique_ptr<internal::MessagePipeReader> message_reader_; |
| 131 | |
| 132 | base::Lock associated_interface_lock_; |
rockot | a6edf83 | 2016-07-14 09:34:11 +0900 | [diff] [blame] | 133 | std::map<std::string, GenericAssociatedInterfaceFactory> |
| 134 | associated_interfaces_; |
| 135 | |
anujk.sharma | 8461adf | 2014-08-28 15:49:02 +0900 | [diff] [blame] | 136 | base::WeakPtrFactory<ChannelMojo> weak_factory_; |
| 137 | |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 138 | DISALLOW_COPY_AND_ASSIGN(ChannelMojo); |
| 139 | }; |
| 140 | |
| 141 | } // namespace IPC |
| 142 | |
| 143 | #endif // IPC_IPC_CHANNEL_MOJO_H_ |