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 | |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 10 | #include <vector> |
| 11 | |
avi | 42ebda4 | 2015-12-22 11:39:04 +0900 | [diff] [blame^] | 12 | #include "base/macros.h" |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 13 | #include "base/memory/scoped_ptr.h" |
| 14 | #include "base/memory/scoped_vector.h" |
| 15 | #include "base/memory/weak_ptr.h" |
morrita | 5138bf5 | 2015-04-21 06:20:12 +0900 | [diff] [blame] | 16 | #include "base/synchronization/lock.h" |
avi | 42ebda4 | 2015-12-22 11:39:04 +0900 | [diff] [blame^] | 17 | #include "build/build_config.h" |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 18 | #include "ipc/ipc_channel.h" |
| 19 | #include "ipc/ipc_channel_factory.h" |
| 20 | #include "ipc/ipc_export.h" |
| 21 | #include "ipc/mojo/ipc_message_pipe_reader.h" |
morrita | 7c48ab8 | 2014-09-24 06:16:00 +0900 | [diff] [blame] | 22 | #include "ipc/mojo/ipc_mojo_bootstrap.h" |
rockot | 7952437 | 2015-03-04 01:31:04 +0900 | [diff] [blame] | 23 | #include "ipc/mojo/scoped_ipc_support.h" |
rockot | af32acb | 2015-11-13 10:33:59 +0900 | [diff] [blame] | 24 | #include "mojo/public/cpp/system/core.h" |
blundell | 4bb8035 | 2015-01-24 01:27:14 +0900 | [diff] [blame] | 25 | #include "third_party/mojo/src/mojo/edk/embedder/channel_info_forward.h" |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 26 | |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 27 | namespace IPC { |
| 28 | |
| 29 | // Mojo-based IPC::Channel implementation over a platform handle. |
| 30 | // |
| 31 | // ChannelMojo builds Mojo MessagePipe using underlying pipe given by |
| 32 | // "bootstrap" IPC::Channel which creates and owns platform pipe like |
| 33 | // named socket. The bootstrap Channel is used only for establishing |
| 34 | // the underlying connection. ChannelMojo takes its handle over once |
| 35 | // the it is made and puts MessagePipe on it. |
| 36 | // |
| 37 | // ChannelMojo has a couple of MessagePipes: |
| 38 | // |
| 39 | // * The first MessagePipe, which is built on top of bootstrap handle, |
| 40 | // is the "control" pipe. It is used to communicate out-of-band |
| 41 | // control messages that aren't visible from IPC::Listener. |
| 42 | // |
| 43 | // * The second MessagePipe, which is created by the server channel |
| 44 | // and sent to client Channel over the control pipe, is used |
| 45 | // to send IPC::Messages as an IPC::Sender. |
| 46 | // |
| 47 | // TODO(morrita): Extract handle creation part of IPC::Channel into |
| 48 | // separate class to clarify what ChannelMojo relies |
| 49 | // on. |
| 50 | // TODO(morrita): Add APIs to create extra MessagePipes to let |
| 51 | // Mojo-based objects talk over this Channel. |
| 52 | // |
morrita | 1f42dc6 | 2014-11-26 08:35:57 +0900 | [diff] [blame] | 53 | class IPC_MOJO_EXPORT ChannelMojo |
| 54 | : public Channel, |
| 55 | public MojoBootstrap::Delegate, |
| 56 | public NON_EXPORTED_BASE(internal::MessagePipeReader::Delegate) { |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 57 | public: |
rockot | ab99227 | 2015-05-12 07:53:22 +0900 | [diff] [blame] | 58 | using CreateMessagingPipeCallback = |
| 59 | base::Callback<void(mojo::ScopedMessagePipeHandle)>; |
| 60 | using CreateMessagingPipeOnIOThreadCallback = |
| 61 | base::Callback<void(mojo::ScopedMessagePipeHandle, |
| 62 | mojo::embedder::ChannelInfo*)>; |
| 63 | |
morrita | 952f465 | 2014-10-21 10:16:35 +0900 | [diff] [blame] | 64 | // True if ChannelMojo should be used regardless of the flag. |
| 65 | static bool ShouldBeUsed(); |
| 66 | |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 67 | // Create ChannelMojo. A bootstrap channel is created as well. |
morrita | d7325c2 | 2015-04-10 04:43:27 +0900 | [diff] [blame] | 68 | static scoped_ptr<ChannelMojo> Create( |
morrita | d7325c2 | 2015-04-10 04:43:27 +0900 | [diff] [blame] | 69 | scoped_refptr<base::TaskRunner> io_runner, |
| 70 | const ChannelHandle& channel_handle, |
| 71 | Mode mode, |
erikchen | c596f54 | 2015-09-24 12:26:38 +0900 | [diff] [blame] | 72 | Listener* listener); |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 73 | |
| 74 | // Create a factory object for ChannelMojo. |
| 75 | // The factory is used to create Mojo-based ChannelProxy family. |
morrita | 7c48ab8 | 2014-09-24 06:16:00 +0900 | [diff] [blame] | 76 | // |host| must not be null. |
| 77 | static scoped_ptr<ChannelFactory> CreateServerFactory( |
morrita | d7325c2 | 2015-04-10 04:43:27 +0900 | [diff] [blame] | 78 | scoped_refptr<base::TaskRunner> io_runner, |
erikchen | c596f54 | 2015-09-24 12:26:38 +0900 | [diff] [blame] | 79 | const ChannelHandle& channel_handle); |
morrita | 7c48ab8 | 2014-09-24 06:16:00 +0900 | [diff] [blame] | 80 | |
| 81 | static scoped_ptr<ChannelFactory> CreateClientFactory( |
morrita | d7325c2 | 2015-04-10 04:43:27 +0900 | [diff] [blame] | 82 | scoped_refptr<base::TaskRunner> io_runner, |
erikchen | c596f54 | 2015-09-24 12:26:38 +0900 | [diff] [blame] | 83 | const ChannelHandle& channel_handle); |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 84 | |
dcheng | ef7721a | 2014-10-22 11:29:52 +0900 | [diff] [blame] | 85 | ~ChannelMojo() override; |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 86 | |
| 87 | // Channel implementation |
dcheng | ef7721a | 2014-10-22 11:29:52 +0900 | [diff] [blame] | 88 | bool Connect() override; |
| 89 | void Close() override; |
| 90 | bool Send(Message* message) override; |
morrita | 5138bf5 | 2015-04-21 06:20:12 +0900 | [diff] [blame] | 91 | bool IsSendThreadSafe() const override; |
dcheng | ef7721a | 2014-10-22 11:29:52 +0900 | [diff] [blame] | 92 | base::ProcessId GetPeerPID() const override; |
| 93 | base::ProcessId GetSelfPID() const override; |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 94 | |
| 95 | #if defined(OS_POSIX) && !defined(OS_NACL) |
dcheng | ef7721a | 2014-10-22 11:29:52 +0900 | [diff] [blame] | 96 | int GetClientFileDescriptor() const override; |
| 97 | base::ScopedFD TakeClientFileDescriptor() override; |
morrita | 1b52e4c | 2015-02-06 09:58:30 +0900 | [diff] [blame] | 98 | #endif // defined(OS_POSIX) && !defined(OS_NACL) |
morrita | 42bda25 | 2014-09-12 04:06:29 +0900 | [diff] [blame] | 99 | |
| 100 | // These access protected API of IPC::Message, which has ChannelMojo |
| 101 | // as a friend class. |
morrita | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 102 | static MojoResult WriteToMessageAttachmentSet( |
morrita | 42bda25 | 2014-09-12 04:06:29 +0900 | [diff] [blame] | 103 | const std::vector<MojoHandle>& handle_buffer, |
| 104 | Message* message); |
morrita | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 105 | static MojoResult ReadFromMessageAttachmentSet( |
| 106 | Message* message, |
| 107 | std::vector<MojoHandle>* handles); |
morrita | 42bda25 | 2014-09-12 04:06:29 +0900 | [diff] [blame] | 108 | |
morrita | 7c48ab8 | 2014-09-24 06:16:00 +0900 | [diff] [blame] | 109 | // MojoBootstrapDelegate implementation |
dcheng | ef7721a | 2014-10-22 11:29:52 +0900 | [diff] [blame] | 110 | void OnBootstrapError() override; |
morrita | 7c48ab8 | 2014-09-24 06:16:00 +0900 | [diff] [blame] | 111 | |
morrita | 1f42dc6 | 2014-11-26 08:35:57 +0900 | [diff] [blame] | 112 | // MessagePipeReader::Delegate |
| 113 | void OnMessageReceived(Message& message) override; |
| 114 | void OnPipeClosed(internal::MessagePipeReader* reader) override; |
| 115 | void OnPipeError(internal::MessagePipeReader* reader) override; |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 116 | |
morrita | 42bda25 | 2014-09-12 04:06:29 +0900 | [diff] [blame] | 117 | protected: |
leon.han | 5dd52cd | 2015-06-19 11:25:48 +0900 | [diff] [blame] | 118 | ChannelMojo(scoped_refptr<base::TaskRunner> io_runner, |
morrita | 7c48ab8 | 2014-09-24 06:16:00 +0900 | [diff] [blame] | 119 | const ChannelHandle& channel_handle, |
morrita | 42bda25 | 2014-09-12 04:06:29 +0900 | [diff] [blame] | 120 | Mode mode, |
erikchen | c596f54 | 2015-09-24 12:26:38 +0900 | [diff] [blame] | 121 | Listener* listener); |
morrita | 42bda25 | 2014-09-12 04:06:29 +0900 | [diff] [blame] | 122 | |
rockot | ab99227 | 2015-05-12 07:53:22 +0900 | [diff] [blame] | 123 | void CreateMessagingPipe(mojo::embedder::ScopedPlatformHandle handle, |
| 124 | const CreateMessagingPipeCallback& callback); |
morrita | 88543a2 | 2014-10-28 05:10:25 +0900 | [diff] [blame] | 125 | void InitMessageReader(mojo::ScopedMessagePipeHandle pipe, int32_t peer_pid); |
| 126 | |
| 127 | Listener* listener() const { return listener_; } |
| 128 | void set_peer_pid(base::ProcessId pid) { peer_pid_ = pid; } |
| 129 | |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 130 | private: |
| 131 | struct ChannelInfoDeleter { |
rockot | ab99227 | 2015-05-12 07:53:22 +0900 | [diff] [blame] | 132 | explicit ChannelInfoDeleter(scoped_refptr<base::TaskRunner> io_runner); |
| 133 | ~ChannelInfoDeleter(); |
| 134 | |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 135 | void operator()(mojo::embedder::ChannelInfo* ptr) const; |
rockot | ab99227 | 2015-05-12 07:53:22 +0900 | [diff] [blame] | 136 | |
| 137 | scoped_refptr<base::TaskRunner> io_runner; |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | // ChannelMojo needs to kill its MessagePipeReader in delayed manner |
| 141 | // because the channel wants to kill these readers during the |
| 142 | // notifications invoked by them. |
| 143 | typedef internal::MessagePipeReader::DelayedDeleter ReaderDeleter; |
| 144 | |
leon.han | 5dd52cd | 2015-06-19 11:25:48 +0900 | [diff] [blame] | 145 | void InitOnIOThread(); |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 146 | |
rockot | ab99227 | 2015-05-12 07:53:22 +0900 | [diff] [blame] | 147 | static void CreateMessagingPipeOnIOThread( |
| 148 | mojo::embedder::ScopedPlatformHandle handle, |
| 149 | scoped_refptr<base::TaskRunner> callback_runner, |
| 150 | const CreateMessagingPipeOnIOThreadCallback& callback); |
| 151 | void OnMessagingPipeCreated(const CreateMessagingPipeCallback& callback, |
| 152 | mojo::ScopedMessagePipeHandle handle, |
| 153 | mojo::embedder::ChannelInfo* channel_info); |
| 154 | |
morrita | 7c48ab8 | 2014-09-24 06:16:00 +0900 | [diff] [blame] | 155 | scoped_ptr<MojoBootstrap> bootstrap_; |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 156 | Listener* listener_; |
| 157 | base::ProcessId peer_pid_; |
morrita | 5138bf5 | 2015-04-21 06:20:12 +0900 | [diff] [blame] | 158 | scoped_refptr<base::TaskRunner> io_runner_; |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 159 | scoped_ptr<mojo::embedder::ChannelInfo, |
| 160 | ChannelInfoDeleter> channel_info_; |
| 161 | |
morrita | 7ce235f | 2015-06-24 07:29:36 +0900 | [diff] [blame] | 162 | // Guards |message_reader_|, |waiting_connect_| and |pending_messages_| |
morrita | 5138bf5 | 2015-04-21 06:20:12 +0900 | [diff] [blame] | 163 | // |
| 164 | // * The contents of |pending_messages_| can be modified from any thread. |
| 165 | // * |message_reader_| is modified only from the IO thread, |
| 166 | // but they can be referenced from other threads. |
| 167 | base::Lock lock_; |
morrita | 1f42dc6 | 2014-11-26 08:35:57 +0900 | [diff] [blame] | 168 | scoped_ptr<internal::MessagePipeReader, ReaderDeleter> message_reader_; |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 169 | ScopedVector<Message> pending_messages_; |
morrita | 7ce235f | 2015-06-24 07:29:36 +0900 | [diff] [blame] | 170 | bool waiting_connect_; |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 171 | |
rockot | 7952437 | 2015-03-04 01:31:04 +0900 | [diff] [blame] | 172 | scoped_ptr<ScopedIPCSupport> ipc_support_; |
| 173 | |
anujk.sharma | 8461adf | 2014-08-28 15:49:02 +0900 | [diff] [blame] | 174 | base::WeakPtrFactory<ChannelMojo> weak_factory_; |
| 175 | |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 176 | DISALLOW_COPY_AND_ASSIGN(ChannelMojo); |
| 177 | }; |
| 178 | |
| 179 | } // namespace IPC |
| 180 | |
| 181 | #endif // IPC_IPC_CHANNEL_MOJO_H_ |