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