[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 | |
| 8 | #include <vector> |
| 9 | |
| 10 | #include "base/memory/scoped_ptr.h" |
| 11 | #include "base/memory/scoped_vector.h" |
| 12 | #include "base/memory/weak_ptr.h" |
morrita | b447214 | 2015-04-20 21:20:12 | [diff] [blame] | 13 | #include "base/synchronization/lock.h" |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 14 | #include "ipc/ipc_channel.h" |
| 15 | #include "ipc/ipc_channel_factory.h" |
| 16 | #include "ipc/ipc_export.h" |
| 17 | #include "ipc/mojo/ipc_message_pipe_reader.h" |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 18 | #include "ipc/mojo/ipc_mojo_bootstrap.h" |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 19 | #include "ipc/mojo/scoped_ipc_support.h" |
blundell | 471b74f | 2015-01-23 16:27:14 | [diff] [blame] | 20 | #include "third_party/mojo/src/mojo/edk/embedder/channel_info_forward.h" |
| 21 | #include "third_party/mojo/src/mojo/public/cpp/system/core.h" |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 22 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 23 | namespace IPC { |
| 24 | |
| 25 | // Mojo-based IPC::Channel implementation over a platform handle. |
| 26 | // |
| 27 | // ChannelMojo builds Mojo MessagePipe using underlying pipe given by |
| 28 | // "bootstrap" IPC::Channel which creates and owns platform pipe like |
| 29 | // named socket. The bootstrap Channel is used only for establishing |
| 30 | // the underlying connection. ChannelMojo takes its handle over once |
| 31 | // the it is made and puts MessagePipe on it. |
| 32 | // |
| 33 | // ChannelMojo has a couple of MessagePipes: |
| 34 | // |
| 35 | // * The first MessagePipe, which is built on top of bootstrap handle, |
| 36 | // is the "control" pipe. It is used to communicate out-of-band |
| 37 | // control messages that aren't visible from IPC::Listener. |
| 38 | // |
| 39 | // * The second MessagePipe, which is created by the server channel |
| 40 | // and sent to client Channel over the control pipe, is used |
| 41 | // to send IPC::Messages as an IPC::Sender. |
| 42 | // |
| 43 | // TODO(morrita): Extract handle creation part of IPC::Channel into |
| 44 | // separate class to clarify what ChannelMojo relies |
| 45 | // on. |
| 46 | // TODO(morrita): Add APIs to create extra MessagePipes to let |
| 47 | // Mojo-based objects talk over this Channel. |
| 48 | // |
morrita | d68bedf4 | 2014-11-25 23:35:57 | [diff] [blame] | 49 | class IPC_MOJO_EXPORT ChannelMojo |
| 50 | : public Channel, |
| 51 | public MojoBootstrap::Delegate, |
| 52 | public NON_EXPORTED_BASE(internal::MessagePipeReader::Delegate) { |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 53 | public: |
rockot | dbb3bb6b | 2015-05-11 22:53:22 | [diff] [blame] | 54 | using CreateMessagingPipeCallback = |
| 55 | base::Callback<void(mojo::ScopedMessagePipeHandle)>; |
| 56 | using CreateMessagingPipeOnIOThreadCallback = |
| 57 | base::Callback<void(mojo::ScopedMessagePipeHandle, |
| 58 | mojo::embedder::ChannelInfo*)>; |
| 59 | |
morrita | e9453ea | 2014-09-26 03:20:48 | [diff] [blame] | 60 | class Delegate { |
| 61 | public: |
| 62 | virtual ~Delegate() {} |
| 63 | virtual base::WeakPtr<Delegate> ToWeakPtr() = 0; |
morrita | e9453ea | 2014-09-26 03:20:48 | [diff] [blame] | 64 | virtual void OnChannelCreated(base::WeakPtr<ChannelMojo> channel) = 0; |
| 65 | }; |
| 66 | |
morrita | 49ca46d | 2014-10-21 01:16:35 | [diff] [blame] | 67 | // True if ChannelMojo should be used regardless of the flag. |
| 68 | static bool ShouldBeUsed(); |
| 69 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 70 | // Create ChannelMojo. A bootstrap channel is created as well. |
morrita | e9453ea | 2014-09-26 03:20:48 | [diff] [blame] | 71 | // |host| must not be null for server channels. |
erikchen | 27aa7d8 | 2015-06-16 21:21:04 | [diff] [blame^] | 72 | // |broker| must outlive the newly created channel. |
morrita | 00194e78 | 2015-04-09 19:43:27 | [diff] [blame] | 73 | static scoped_ptr<ChannelMojo> Create( |
| 74 | Delegate* delegate, |
| 75 | scoped_refptr<base::TaskRunner> io_runner, |
| 76 | const ChannelHandle& channel_handle, |
| 77 | Mode mode, |
erikchen | 27aa7d8 | 2015-06-16 21:21:04 | [diff] [blame^] | 78 | Listener* listener, |
| 79 | AttachmentBroker* broker); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 80 | |
| 81 | // Create a factory object for ChannelMojo. |
| 82 | // The factory is used to create Mojo-based ChannelProxy family. |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 83 | // |host| must not be null. |
erikchen | 27aa7d8 | 2015-06-16 21:21:04 | [diff] [blame^] | 84 | // TODO(erikchen): Remove default parameter for |broker|. It exists only to |
| 85 | // make the upcoming refactor decomposable into smaller CLs. |
| 86 | // http://crbug.com/493414. |
| 87 | // |broker| must outlive the factory and all channels it creates. |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 88 | static scoped_ptr<ChannelFactory> CreateServerFactory( |
morrita | e9453ea | 2014-09-26 03:20:48 | [diff] [blame] | 89 | Delegate* delegate, |
morrita | 00194e78 | 2015-04-09 19:43:27 | [diff] [blame] | 90 | scoped_refptr<base::TaskRunner> io_runner, |
erikchen | 27aa7d8 | 2015-06-16 21:21:04 | [diff] [blame^] | 91 | const ChannelHandle& channel_handle, |
| 92 | AttachmentBroker* broker = nullptr); |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 93 | |
erikchen | 27aa7d8 | 2015-06-16 21:21:04 | [diff] [blame^] | 94 | // TODO(erikchen): Remove default parameter for |broker|. It exists only to |
| 95 | // make the upcoming refactor decomposable into smaller CLs. |
| 96 | // http://crbug.com/493414. |
| 97 | // |broker| must outlive the factory and all channels it creates. |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 98 | static scoped_ptr<ChannelFactory> CreateClientFactory( |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 99 | Delegate* delegate, |
morrita | 00194e78 | 2015-04-09 19:43:27 | [diff] [blame] | 100 | scoped_refptr<base::TaskRunner> io_runner, |
erikchen | 27aa7d8 | 2015-06-16 21:21:04 | [diff] [blame^] | 101 | const ChannelHandle& channel_handle, |
| 102 | AttachmentBroker* broker = nullptr); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 103 | |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 104 | ~ChannelMojo() override; |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 105 | |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 106 | // ChannelMojoHost tells the client handle using this API. |
| 107 | void OnClientLaunched(base::ProcessHandle handle); |
| 108 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 109 | // Channel implementation |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 110 | bool Connect() override; |
| 111 | void Close() override; |
| 112 | bool Send(Message* message) override; |
morrita | b447214 | 2015-04-20 21:20:12 | [diff] [blame] | 113 | bool IsSendThreadSafe() const override; |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 114 | base::ProcessId GetPeerPID() const override; |
| 115 | base::ProcessId GetSelfPID() const override; |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 116 | |
| 117 | #if defined(OS_POSIX) && !defined(OS_NACL) |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 118 | int GetClientFileDescriptor() const override; |
| 119 | base::ScopedFD TakeClientFileDescriptor() override; |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 120 | #endif // defined(OS_POSIX) && !defined(OS_NACL) |
morrita | 3b41d6c | 2014-09-11 19:06:29 | [diff] [blame] | 121 | |
| 122 | // These access protected API of IPC::Message, which has ChannelMojo |
| 123 | // as a friend class. |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 124 | static MojoResult WriteToMessageAttachmentSet( |
morrita | 3b41d6c | 2014-09-11 19:06:29 | [diff] [blame] | 125 | const std::vector<MojoHandle>& handle_buffer, |
| 126 | Message* message); |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 127 | static MojoResult ReadFromMessageAttachmentSet( |
| 128 | Message* message, |
| 129 | std::vector<MojoHandle>* handles); |
morrita | 3b41d6c | 2014-09-11 19:06:29 | [diff] [blame] | 130 | |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 131 | // MojoBootstrapDelegate implementation |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 132 | void OnBootstrapError() override; |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 133 | |
morrita | d68bedf4 | 2014-11-25 23:35:57 | [diff] [blame] | 134 | // MessagePipeReader::Delegate |
| 135 | void OnMessageReceived(Message& message) override; |
| 136 | void OnPipeClosed(internal::MessagePipeReader* reader) override; |
| 137 | void OnPipeError(internal::MessagePipeReader* reader) override; |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 138 | |
morrita | 3b41d6c | 2014-09-11 19:06:29 | [diff] [blame] | 139 | protected: |
morrita | e9453ea | 2014-09-26 03:20:48 | [diff] [blame] | 140 | ChannelMojo(Delegate* delegate, |
morrita | 00194e78 | 2015-04-09 19:43:27 | [diff] [blame] | 141 | scoped_refptr<base::TaskRunner> io_runner, |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 142 | const ChannelHandle& channel_handle, |
morrita | 3b41d6c | 2014-09-11 19:06:29 | [diff] [blame] | 143 | Mode mode, |
erikchen | 27aa7d8 | 2015-06-16 21:21:04 | [diff] [blame^] | 144 | Listener* listener, |
| 145 | AttachmentBroker* broker); |
morrita | 3b41d6c | 2014-09-11 19:06:29 | [diff] [blame] | 146 | |
rockot | dbb3bb6b | 2015-05-11 22:53:22 | [diff] [blame] | 147 | void CreateMessagingPipe(mojo::embedder::ScopedPlatformHandle handle, |
| 148 | const CreateMessagingPipeCallback& callback); |
morrita | f8f92dcd | 2014-10-27 20:10:25 | [diff] [blame] | 149 | void InitMessageReader(mojo::ScopedMessagePipeHandle pipe, int32_t peer_pid); |
| 150 | |
| 151 | Listener* listener() const { return listener_; } |
| 152 | void set_peer_pid(base::ProcessId pid) { peer_pid_ = pid; } |
| 153 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 154 | private: |
| 155 | struct ChannelInfoDeleter { |
rockot | dbb3bb6b | 2015-05-11 22:53:22 | [diff] [blame] | 156 | explicit ChannelInfoDeleter(scoped_refptr<base::TaskRunner> io_runner); |
| 157 | ~ChannelInfoDeleter(); |
| 158 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 159 | void operator()(mojo::embedder::ChannelInfo* ptr) const; |
rockot | dbb3bb6b | 2015-05-11 22:53:22 | [diff] [blame] | 160 | |
| 161 | scoped_refptr<base::TaskRunner> io_runner; |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | // ChannelMojo needs to kill its MessagePipeReader in delayed manner |
| 165 | // because the channel wants to kill these readers during the |
| 166 | // notifications invoked by them. |
| 167 | typedef internal::MessagePipeReader::DelayedDeleter ReaderDeleter; |
| 168 | |
morrita | 00194e78 | 2015-04-09 19:43:27 | [diff] [blame] | 169 | void InitOnIOThread(ChannelMojo::Delegate* delegate); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 170 | |
rockot | dbb3bb6b | 2015-05-11 22:53:22 | [diff] [blame] | 171 | static void CreateMessagingPipeOnIOThread( |
| 172 | mojo::embedder::ScopedPlatformHandle handle, |
| 173 | scoped_refptr<base::TaskRunner> callback_runner, |
| 174 | const CreateMessagingPipeOnIOThreadCallback& callback); |
| 175 | void OnMessagingPipeCreated(const CreateMessagingPipeCallback& callback, |
| 176 | mojo::ScopedMessagePipeHandle handle, |
| 177 | mojo::embedder::ChannelInfo* channel_info); |
| 178 | |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 179 | scoped_ptr<MojoBootstrap> bootstrap_; |
morrita | e9453ea | 2014-09-26 03:20:48 | [diff] [blame] | 180 | base::WeakPtr<Delegate> delegate_; |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 181 | Mode mode_; |
| 182 | Listener* listener_; |
| 183 | base::ProcessId peer_pid_; |
morrita | b447214 | 2015-04-20 21:20:12 | [diff] [blame] | 184 | scoped_refptr<base::TaskRunner> io_runner_; |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 185 | scoped_ptr<mojo::embedder::ChannelInfo, |
| 186 | ChannelInfoDeleter> channel_info_; |
| 187 | |
morrita | b447214 | 2015-04-20 21:20:12 | [diff] [blame] | 188 | // Guards |message_reader_| and |pending_messages_| |
| 189 | // |
| 190 | // * The contents of |pending_messages_| can be modified from any thread. |
| 191 | // * |message_reader_| is modified only from the IO thread, |
| 192 | // but they can be referenced from other threads. |
| 193 | base::Lock lock_; |
morrita | d68bedf4 | 2014-11-25 23:35:57 | [diff] [blame] | 194 | scoped_ptr<internal::MessagePipeReader, ReaderDeleter> message_reader_; |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 195 | ScopedVector<Message> pending_messages_; |
| 196 | |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 197 | scoped_ptr<ScopedIPCSupport> ipc_support_; |
| 198 | |
anujk.sharma | 0184ced | 2014-08-28 06:49:02 | [diff] [blame] | 199 | base::WeakPtrFactory<ChannelMojo> weak_factory_; |
| 200 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 201 | DISALLOW_COPY_AND_ASSIGN(ChannelMojo); |
| 202 | }; |
| 203 | |
| 204 | } // namespace IPC |
| 205 | |
| 206 | #endif // IPC_IPC_CHANNEL_MOJO_H_ |