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