Make sure the soft-keyboard shows up in the Add Exception dialog.
[chromium-blink-merge.git] / ipc / mojo / ipc_channel_mojo.h
blob3a1d98a8a97445cb9c72c7455c18a6ce2c20386a
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 "base/synchronization/lock.h"
14 #include "ipc/ipc_channel.h"
15 #include "ipc/ipc_channel_factory.h"
16 #include "ipc/ipc_export.h"
17 #include "ipc/mojo/ipc_message_pipe_reader.h"
18 #include "ipc/mojo/ipc_mojo_bootstrap.h"
19 #include "ipc/mojo/scoped_ipc_support.h"
20 #include "third_party/mojo/src/mojo/edk/embedder/channel_info_forward.h"
21 #include "third_party/mojo/src/mojo/public/cpp/system/core.h"
23 namespace IPC {
25 // Mojo-based IPC::Channel implementation over a platform handle.
27 // ChannelMojo builds Mojo MessagePipe using underlying pipe given by
28 // "bootstrap" IPC::Channel which creates and owns platform pipe like
29 // named socket. The bootstrap Channel is used only for establishing
30 // the underlying connection. ChannelMojo takes its handle over once
31 // the it is made and puts MessagePipe on it.
33 // ChannelMojo has a couple of MessagePipes:
35 // * The first MessagePipe, which is built on top of bootstrap handle,
36 // is the "control" pipe. It is used to communicate out-of-band
37 // control messages that aren't visible from IPC::Listener.
39 // * The second MessagePipe, which is created by the server channel
40 // and sent to client Channel over the control pipe, is used
41 // to send IPC::Messages as an IPC::Sender.
43 // TODO(morrita): Extract handle creation part of IPC::Channel into
44 // separate class to clarify what ChannelMojo relies
45 // on.
46 // TODO(morrita): Add APIs to create extra MessagePipes to let
47 // Mojo-based objects talk over this Channel.
49 class IPC_MOJO_EXPORT ChannelMojo
50 : public Channel,
51 public MojoBootstrap::Delegate,
52 public NON_EXPORTED_BASE(internal::MessagePipeReader::Delegate) {
53 public:
54 using CreateMessagingPipeCallback =
55 base::Callback<void(mojo::ScopedMessagePipeHandle)>;
56 using CreateMessagingPipeOnIOThreadCallback =
57 base::Callback<void(mojo::ScopedMessagePipeHandle,
58 mojo::embedder::ChannelInfo*)>;
60 class Delegate {
61 public:
62 virtual ~Delegate() {}
63 virtual base::WeakPtr<Delegate> ToWeakPtr() = 0;
64 virtual void OnChannelCreated(base::WeakPtr<ChannelMojo> channel) = 0;
67 // True if ChannelMojo should be used regardless of the flag.
68 static bool ShouldBeUsed();
70 // Create ChannelMojo. A bootstrap channel is created as well.
71 // |host| must not be null for server channels.
72 static scoped_ptr<ChannelMojo> Create(
73 Delegate* delegate,
74 scoped_refptr<base::TaskRunner> io_runner,
75 const ChannelHandle& channel_handle,
76 Mode mode,
77 Listener* listener);
79 // Create a factory object for ChannelMojo.
80 // The factory is used to create Mojo-based ChannelProxy family.
81 // |host| must not be null.
82 static scoped_ptr<ChannelFactory> CreateServerFactory(
83 Delegate* delegate,
84 scoped_refptr<base::TaskRunner> io_runner,
85 const ChannelHandle& channel_handle);
87 static scoped_ptr<ChannelFactory> CreateClientFactory(
88 Delegate* delegate,
89 scoped_refptr<base::TaskRunner> io_runner,
90 const ChannelHandle& channel_handle);
92 ~ChannelMojo() override;
94 // ChannelMojoHost tells the client handle using this API.
95 void OnClientLaunched(base::ProcessHandle handle);
97 // Channel implementation
98 bool Connect() override;
99 void Close() override;
100 bool Send(Message* message) override;
101 bool IsSendThreadSafe() const override;
102 base::ProcessId GetPeerPID() const override;
103 base::ProcessId GetSelfPID() const override;
105 #if defined(OS_POSIX) && !defined(OS_NACL)
106 int GetClientFileDescriptor() const override;
107 base::ScopedFD TakeClientFileDescriptor() override;
108 #endif // defined(OS_POSIX) && !defined(OS_NACL)
110 // These access protected API of IPC::Message, which has ChannelMojo
111 // as a friend class.
112 static MojoResult WriteToMessageAttachmentSet(
113 const std::vector<MojoHandle>& handle_buffer,
114 Message* message);
115 static MojoResult ReadFromMessageAttachmentSet(
116 Message* message,
117 std::vector<MojoHandle>* handles);
119 // MojoBootstrapDelegate implementation
120 void OnBootstrapError() override;
122 // MessagePipeReader::Delegate
123 void OnMessageReceived(Message& message) override;
124 void OnPipeClosed(internal::MessagePipeReader* reader) override;
125 void OnPipeError(internal::MessagePipeReader* reader) override;
127 protected:
128 ChannelMojo(Delegate* delegate,
129 scoped_refptr<base::TaskRunner> io_runner,
130 const ChannelHandle& channel_handle,
131 Mode mode,
132 Listener* listener);
134 void CreateMessagingPipe(mojo::embedder::ScopedPlatformHandle handle,
135 const CreateMessagingPipeCallback& callback);
136 void InitMessageReader(mojo::ScopedMessagePipeHandle pipe, int32_t peer_pid);
138 Listener* listener() const { return listener_; }
139 void set_peer_pid(base::ProcessId pid) { peer_pid_ = pid; }
141 private:
142 struct ChannelInfoDeleter {
143 explicit ChannelInfoDeleter(scoped_refptr<base::TaskRunner> io_runner);
144 ~ChannelInfoDeleter();
146 void operator()(mojo::embedder::ChannelInfo* ptr) const;
148 scoped_refptr<base::TaskRunner> io_runner;
151 // ChannelMojo needs to kill its MessagePipeReader in delayed manner
152 // because the channel wants to kill these readers during the
153 // notifications invoked by them.
154 typedef internal::MessagePipeReader::DelayedDeleter ReaderDeleter;
156 void InitOnIOThread(ChannelMojo::Delegate* delegate);
158 static void CreateMessagingPipeOnIOThread(
159 mojo::embedder::ScopedPlatformHandle handle,
160 scoped_refptr<base::TaskRunner> callback_runner,
161 const CreateMessagingPipeOnIOThreadCallback& callback);
162 void OnMessagingPipeCreated(const CreateMessagingPipeCallback& callback,
163 mojo::ScopedMessagePipeHandle handle,
164 mojo::embedder::ChannelInfo* channel_info);
166 scoped_ptr<MojoBootstrap> bootstrap_;
167 base::WeakPtr<Delegate> delegate_;
168 Mode mode_;
169 Listener* listener_;
170 base::ProcessId peer_pid_;
171 scoped_refptr<base::TaskRunner> io_runner_;
172 scoped_ptr<mojo::embedder::ChannelInfo,
173 ChannelInfoDeleter> channel_info_;
175 // Guards |message_reader_| and |pending_messages_|
177 // * The contents of |pending_messages_| can be modified from any thread.
178 // * |message_reader_| is modified only from the IO thread,
179 // but they can be referenced from other threads.
180 base::Lock lock_;
181 scoped_ptr<internal::MessagePipeReader, ReaderDeleter> message_reader_;
182 ScopedVector<Message> pending_messages_;
184 scoped_ptr<ScopedIPCSupport> ipc_support_;
186 base::WeakPtrFactory<ChannelMojo> weak_factory_;
188 DISALLOW_COPY_AND_ASSIGN(ChannelMojo);
191 } // namespace IPC
193 #endif // IPC_IPC_CHANNEL_MOJO_H_