[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" |
| 17 | #include "mojo/public/cpp/system/core.h" |
| 18 | |
| 19 | namespace mojo { |
| 20 | namespace embedder { |
| 21 | struct ChannelInfo; |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | namespace IPC { |
| 26 | |
morrita | 3b41d6c | 2014-09-11 19:06:29 | [diff] [blame^] | 27 | namespace internal { |
| 28 | class ControlReader; |
| 29 | class ServerControlReader; |
| 30 | class ClientControlReader; |
| 31 | class MessageReader; |
| 32 | } |
| 33 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 34 | // Mojo-based IPC::Channel implementation over a platform handle. |
| 35 | // |
| 36 | // ChannelMojo builds Mojo MessagePipe using underlying pipe given by |
| 37 | // "bootstrap" IPC::Channel which creates and owns platform pipe like |
| 38 | // named socket. The bootstrap Channel is used only for establishing |
| 39 | // the underlying connection. ChannelMojo takes its handle over once |
| 40 | // the it is made and puts MessagePipe on it. |
| 41 | // |
| 42 | // ChannelMojo has a couple of MessagePipes: |
| 43 | // |
| 44 | // * The first MessagePipe, which is built on top of bootstrap handle, |
| 45 | // is the "control" pipe. It is used to communicate out-of-band |
| 46 | // control messages that aren't visible from IPC::Listener. |
| 47 | // |
| 48 | // * The second MessagePipe, which is created by the server channel |
| 49 | // and sent to client Channel over the control pipe, is used |
| 50 | // to send IPC::Messages as an IPC::Sender. |
| 51 | // |
| 52 | // TODO(morrita): Extract handle creation part of IPC::Channel into |
| 53 | // separate class to clarify what ChannelMojo relies |
| 54 | // on. |
| 55 | // TODO(morrita): Add APIs to create extra MessagePipes to let |
| 56 | // Mojo-based objects talk over this Channel. |
| 57 | // |
| 58 | class IPC_MOJO_EXPORT ChannelMojo : public Channel { |
| 59 | public: |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 60 | // Create ChannelMojo. A bootstrap channel is created as well. |
| 61 | static scoped_ptr<ChannelMojo> Create( |
| 62 | const ChannelHandle &channel_handle, Mode mode, Listener* listener, |
| 63 | scoped_refptr<base::TaskRunner> io_thread_task_runner); |
| 64 | |
| 65 | // Create a factory object for ChannelMojo. |
| 66 | // The factory is used to create Mojo-based ChannelProxy family. |
| 67 | static scoped_ptr<ChannelFactory> CreateFactory( |
| 68 | const ChannelHandle &channel_handle, Mode mode, |
| 69 | scoped_refptr<base::TaskRunner> io_thread_task_runner); |
| 70 | |
| 71 | virtual ~ChannelMojo(); |
| 72 | |
| 73 | // Channel implementation |
| 74 | virtual bool Connect() OVERRIDE; |
| 75 | virtual void Close() OVERRIDE; |
| 76 | virtual bool Send(Message* message) OVERRIDE; |
| 77 | virtual base::ProcessId GetPeerPID() const OVERRIDE; |
| 78 | virtual base::ProcessId GetSelfPID() const OVERRIDE; |
| 79 | virtual ChannelHandle TakePipeHandle() OVERRIDE; |
| 80 | |
| 81 | #if defined(OS_POSIX) && !defined(OS_NACL) |
| 82 | virtual int GetClientFileDescriptor() const OVERRIDE; |
| 83 | virtual int TakeClientFileDescriptor() OVERRIDE; |
morrita | 3b41d6c | 2014-09-11 19:06:29 | [diff] [blame^] | 84 | |
| 85 | // These access protected API of IPC::Message, which has ChannelMojo |
| 86 | // as a friend class. |
| 87 | static MojoResult WriteToFileDescriptorSet( |
| 88 | const std::vector<MojoHandle>& handle_buffer, |
| 89 | Message* message); |
| 90 | static MojoResult ReadFromFileDescriptorSet(const Message& message, |
| 91 | std::vector<MojoHandle>* handles); |
| 92 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 93 | #endif // defined(OS_POSIX) && !defined(OS_NACL) |
| 94 | |
| 95 | // Called from MessagePipeReader implementations |
| 96 | void OnMessageReceived(Message& message); |
| 97 | void OnConnected(mojo::ScopedMessagePipeHandle pipe); |
| 98 | void OnPipeClosed(internal::MessagePipeReader* reader); |
| 99 | void OnPipeError(internal::MessagePipeReader* reader); |
| 100 | void set_peer_pid(base::ProcessId pid) { peer_pid_ = pid; } |
| 101 | |
morrita | 3b41d6c | 2014-09-11 19:06:29 | [diff] [blame^] | 102 | protected: |
| 103 | ChannelMojo(const ChannelHandle& channel_handle, |
| 104 | Mode mode, |
| 105 | Listener* listener, |
| 106 | scoped_refptr<base::TaskRunner> io_thread_task_runner); |
| 107 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 108 | private: |
| 109 | struct ChannelInfoDeleter { |
| 110 | void operator()(mojo::embedder::ChannelInfo* ptr) const; |
| 111 | }; |
| 112 | |
| 113 | // ChannelMojo needs to kill its MessagePipeReader in delayed manner |
| 114 | // because the channel wants to kill these readers during the |
| 115 | // notifications invoked by them. |
| 116 | typedef internal::MessagePipeReader::DelayedDeleter ReaderDeleter; |
| 117 | |
[email protected] | efbf95d | 2014-08-12 21:44:01 | [diff] [blame] | 118 | void InitOnIOThread(); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 119 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 120 | scoped_ptr<Channel> bootstrap_; |
| 121 | Mode mode_; |
| 122 | Listener* listener_; |
| 123 | base::ProcessId peer_pid_; |
| 124 | scoped_ptr<mojo::embedder::ChannelInfo, |
| 125 | ChannelInfoDeleter> channel_info_; |
| 126 | |
morrita | 3b41d6c | 2014-09-11 19:06:29 | [diff] [blame^] | 127 | scoped_ptr<internal::ControlReader, ReaderDeleter> control_reader_; |
| 128 | scoped_ptr<internal::MessageReader, ReaderDeleter> message_reader_; |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 129 | ScopedVector<Message> pending_messages_; |
| 130 | |
anujk.sharma | 0184ced | 2014-08-28 06:49:02 | [diff] [blame] | 131 | base::WeakPtrFactory<ChannelMojo> weak_factory_; |
| 132 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 133 | DISALLOW_COPY_AND_ASSIGN(ChannelMojo); |
| 134 | }; |
| 135 | |
| 136 | } // namespace IPC |
| 137 | |
| 138 | #endif // IPC_IPC_CHANNEL_MOJO_H_ |