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