Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / ipc / mojo / ipc_channel_mojo.h
blob29d5f29b3e7a5bb55907316cfa6eb29de503334e
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.
5 #ifndef IPC_IPC_CHANNEL_MOJO_H_
6 #define IPC_IPC_CHANNEL_MOJO_H_
8 #include <vector>
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 "ipc/mojo/ipc_mojo_bootstrap.h"
18 #include "mojo/edk/embedder/channel_info_forward.h"
19 #include "mojo/public/cpp/system/core.h"
21 namespace IPC {
23 namespace internal {
24 class ControlReader;
25 class ServerControlReader;
26 class ClientControlReader;
27 class MessageReader;
30 // Mojo-based IPC::Channel implementation over a platform handle.
32 // ChannelMojo builds Mojo MessagePipe using underlying pipe given by
33 // "bootstrap" IPC::Channel which creates and owns platform pipe like
34 // named socket. The bootstrap Channel is used only for establishing
35 // the underlying connection. ChannelMojo takes its handle over once
36 // the it is made and puts MessagePipe on it.
38 // ChannelMojo has a couple of MessagePipes:
40 // * The first MessagePipe, which is built on top of bootstrap handle,
41 // is the "control" pipe. It is used to communicate out-of-band
42 // control messages that aren't visible from IPC::Listener.
44 // * The second MessagePipe, which is created by the server channel
45 // and sent to client Channel over the control pipe, is used
46 // to send IPC::Messages as an IPC::Sender.
48 // TODO(morrita): Extract handle creation part of IPC::Channel into
49 // separate class to clarify what ChannelMojo relies
50 // on.
51 // TODO(morrita): Add APIs to create extra MessagePipes to let
52 // Mojo-based objects talk over this Channel.
54 class IPC_MOJO_EXPORT ChannelMojo : public Channel,
55 public MojoBootstrap::Delegate {
56 public:
57 class Delegate {
58 public:
59 virtual ~Delegate() {}
60 virtual base::WeakPtr<Delegate> ToWeakPtr() = 0;
61 virtual scoped_refptr<base::TaskRunner> GetIOTaskRunner() = 0;
62 virtual void OnChannelCreated(base::WeakPtr<ChannelMojo> channel) = 0;
65 // True if ChannelMojo should be used regardless of the flag.
66 static bool ShouldBeUsed();
68 // Create ChannelMojo. A bootstrap channel is created as well.
69 // |host| must not be null for server channels.
70 static scoped_ptr<ChannelMojo> Create(Delegate* delegate,
71 const ChannelHandle& channel_handle,
72 Mode mode,
73 Listener* listener);
75 // Create a factory object for ChannelMojo.
76 // The factory is used to create Mojo-based ChannelProxy family.
77 // |host| must not be null.
78 static scoped_ptr<ChannelFactory> CreateServerFactory(
79 Delegate* delegate,
80 const ChannelHandle& channel_handle);
82 static scoped_ptr<ChannelFactory> CreateClientFactory(
83 const ChannelHandle& channel_handle);
85 ~ChannelMojo() override;
87 // ChannelMojoHost tells the client handle using this API.
88 void OnClientLaunched(base::ProcessHandle handle);
90 // Channel implementation
91 bool Connect() override;
92 void Close() override;
93 bool Send(Message* message) override;
94 base::ProcessId GetPeerPID() const override;
95 base::ProcessId GetSelfPID() const override;
97 #if defined(OS_POSIX) && !defined(OS_NACL)
98 int GetClientFileDescriptor() const override;
99 base::ScopedFD TakeClientFileDescriptor() override;
101 // These access protected API of IPC::Message, which has ChannelMojo
102 // as a friend class.
103 static MojoResult WriteToFileDescriptorSet(
104 const std::vector<MojoHandle>& handle_buffer,
105 Message* message);
106 static MojoResult ReadFromFileDescriptorSet(Message* message,
107 std::vector<MojoHandle>* handles);
109 #endif // defined(OS_POSIX) && !defined(OS_NACL)
111 // MojoBootstrapDelegate implementation
112 void OnBootstrapError() override;
114 // Called from MessagePipeReader implementations
115 void OnMessageReceived(Message& message);
116 void OnPipeClosed(internal::MessagePipeReader* reader);
117 void OnPipeError(internal::MessagePipeReader* reader);
119 protected:
120 ChannelMojo(Delegate* delegate,
121 const ChannelHandle& channel_handle,
122 Mode mode,
123 Listener* listener);
125 mojo::ScopedMessagePipeHandle CreateMessagingPipe(
126 mojo::embedder::ScopedPlatformHandle handle);
127 void InitMessageReader(mojo::ScopedMessagePipeHandle pipe, int32_t peer_pid);
129 Listener* listener() const { return listener_; }
130 void set_peer_pid(base::ProcessId pid) { peer_pid_ = pid; }
132 private:
133 struct ChannelInfoDeleter {
134 void operator()(mojo::embedder::ChannelInfo* ptr) const;
137 // ChannelMojo needs to kill its MessagePipeReader in delayed manner
138 // because the channel wants to kill these readers during the
139 // notifications invoked by them.
140 typedef internal::MessagePipeReader::DelayedDeleter ReaderDeleter;
142 void InitDelegate(ChannelMojo::Delegate* delegate);
144 scoped_ptr<MojoBootstrap> bootstrap_;
145 base::WeakPtr<Delegate> delegate_;
146 Mode mode_;
147 Listener* listener_;
148 base::ProcessId peer_pid_;
149 scoped_ptr<mojo::embedder::ChannelInfo,
150 ChannelInfoDeleter> channel_info_;
152 scoped_ptr<internal::MessageReader, ReaderDeleter> message_reader_;
153 ScopedVector<Message> pending_messages_;
155 base::WeakPtrFactory<ChannelMojo> weak_factory_;
157 DISALLOW_COPY_AND_ASSIGN(ChannelMojo);
160 } // namespace IPC
162 #endif // IPC_IPC_CHANNEL_MOJO_H_