blob: b84a6e39a61862c6c77c64f7f20362a0992d1d57 [file] [log] [blame]
[email protected]64860882014-08-04 23:44:171// 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"
morritab4472142015-04-20 21:20:1213#include "base/synchronization/lock.h"
[email protected]64860882014-08-04 23:44:1714#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"
morrita54f6f80c2014-09-23 21:16:0018#include "ipc/mojo/ipc_mojo_bootstrap.h"
rockotcbca72f2015-03-03 16:31:0419#include "ipc/mojo/scoped_ipc_support.h"
blundell471b74f2015-01-23 16:27:1420#include "third_party/mojo/src/mojo/edk/embedder/channel_info_forward.h"
21#include "third_party/mojo/src/mojo/public/cpp/system/core.h"
[email protected]64860882014-08-04 23:44:1722
[email protected]64860882014-08-04 23:44:1723namespace IPC {
24
25// Mojo-based IPC::Channel implementation over a platform handle.
26//
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.
32//
33// ChannelMojo has a couple of MessagePipes:
34//
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.
38//
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.
42//
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.
48//
morritad68bedf42014-11-25 23:35:5749class IPC_MOJO_EXPORT ChannelMojo
50 : public Channel,
51 public MojoBootstrap::Delegate,
52 public NON_EXPORTED_BASE(internal::MessagePipeReader::Delegate) {
[email protected]64860882014-08-04 23:44:1753 public:
rockotdbb3bb6b2015-05-11 22:53:2254 using CreateMessagingPipeCallback =
55 base::Callback<void(mojo::ScopedMessagePipeHandle)>;
56 using CreateMessagingPipeOnIOThreadCallback =
57 base::Callback<void(mojo::ScopedMessagePipeHandle,
58 mojo::embedder::ChannelInfo*)>;
59
morritae9453ea2014-09-26 03:20:4860 class Delegate {
61 public:
62 virtual ~Delegate() {}
63 virtual base::WeakPtr<Delegate> ToWeakPtr() = 0;
morritae9453ea2014-09-26 03:20:4864 virtual void OnChannelCreated(base::WeakPtr<ChannelMojo> channel) = 0;
65 };
66
morrita49ca46d2014-10-21 01:16:3567 // True if ChannelMojo should be used regardless of the flag.
68 static bool ShouldBeUsed();
69
[email protected]64860882014-08-04 23:44:1770 // Create ChannelMojo. A bootstrap channel is created as well.
morritae9453ea2014-09-26 03:20:4871 // |host| must not be null for server channels.
erikchen27aa7d82015-06-16 21:21:0472 // |broker| must outlive the newly created channel.
morrita00194e782015-04-09 19:43:2773 static scoped_ptr<ChannelMojo> Create(
74 Delegate* delegate,
75 scoped_refptr<base::TaskRunner> io_runner,
76 const ChannelHandle& channel_handle,
77 Mode mode,
erikchen27aa7d82015-06-16 21:21:0478 Listener* listener,
79 AttachmentBroker* broker);
[email protected]64860882014-08-04 23:44:1780
81 // Create a factory object for ChannelMojo.
82 // The factory is used to create Mojo-based ChannelProxy family.
morrita54f6f80c2014-09-23 21:16:0083 // |host| must not be null.
erikchen27aa7d82015-06-16 21:21:0484 // TODO(erikchen): Remove default parameter for |broker|. It exists only to
85 // make the upcoming refactor decomposable into smaller CLs.
86 // http://crbug.com/493414.
87 // |broker| must outlive the factory and all channels it creates.
morrita54f6f80c2014-09-23 21:16:0088 static scoped_ptr<ChannelFactory> CreateServerFactory(
morritae9453ea2014-09-26 03:20:4889 Delegate* delegate,
morrita00194e782015-04-09 19:43:2790 scoped_refptr<base::TaskRunner> io_runner,
erikchen27aa7d82015-06-16 21:21:0491 const ChannelHandle& channel_handle,
92 AttachmentBroker* broker = nullptr);
morrita54f6f80c2014-09-23 21:16:0093
erikchen27aa7d82015-06-16 21:21:0494 // TODO(erikchen): Remove default parameter for |broker|. It exists only to
95 // make the upcoming refactor decomposable into smaller CLs.
96 // http://crbug.com/493414.
97 // |broker| must outlive the factory and all channels it creates.
morrita54f6f80c2014-09-23 21:16:0098 static scoped_ptr<ChannelFactory> CreateClientFactory(
rockotcbca72f2015-03-03 16:31:0499 Delegate* delegate,
morrita00194e782015-04-09 19:43:27100 scoped_refptr<base::TaskRunner> io_runner,
erikchen27aa7d82015-06-16 21:21:04101 const ChannelHandle& channel_handle,
102 AttachmentBroker* broker = nullptr);
[email protected]64860882014-08-04 23:44:17103
dchengfe61fca2014-10-22 02:29:52104 ~ChannelMojo() override;
[email protected]64860882014-08-04 23:44:17105
morrita54f6f80c2014-09-23 21:16:00106 // ChannelMojoHost tells the client handle using this API.
107 void OnClientLaunched(base::ProcessHandle handle);
108
[email protected]64860882014-08-04 23:44:17109 // Channel implementation
dchengfe61fca2014-10-22 02:29:52110 bool Connect() override;
111 void Close() override;
112 bool Send(Message* message) override;
morritab4472142015-04-20 21:20:12113 bool IsSendThreadSafe() const override;
dchengfe61fca2014-10-22 02:29:52114 base::ProcessId GetPeerPID() const override;
115 base::ProcessId GetSelfPID() const override;
[email protected]64860882014-08-04 23:44:17116
117#if defined(OS_POSIX) && !defined(OS_NACL)
dchengfe61fca2014-10-22 02:29:52118 int GetClientFileDescriptor() const override;
119 base::ScopedFD TakeClientFileDescriptor() override;
morrita81b17e02015-02-06 00:58:30120#endif // defined(OS_POSIX) && !defined(OS_NACL)
morrita3b41d6c2014-09-11 19:06:29121
122 // These access protected API of IPC::Message, which has ChannelMojo
123 // as a friend class.
morrita4b5c28e22015-01-14 21:17:06124 static MojoResult WriteToMessageAttachmentSet(
morrita3b41d6c2014-09-11 19:06:29125 const std::vector<MojoHandle>& handle_buffer,
126 Message* message);
morrita4b5c28e22015-01-14 21:17:06127 static MojoResult ReadFromMessageAttachmentSet(
128 Message* message,
129 std::vector<MojoHandle>* handles);
morrita3b41d6c2014-09-11 19:06:29130
morrita54f6f80c2014-09-23 21:16:00131 // MojoBootstrapDelegate implementation
dchengfe61fca2014-10-22 02:29:52132 void OnBootstrapError() override;
morrita54f6f80c2014-09-23 21:16:00133
morritad68bedf42014-11-25 23:35:57134 // MessagePipeReader::Delegate
135 void OnMessageReceived(Message& message) override;
136 void OnPipeClosed(internal::MessagePipeReader* reader) override;
137 void OnPipeError(internal::MessagePipeReader* reader) override;
[email protected]64860882014-08-04 23:44:17138
morrita3b41d6c2014-09-11 19:06:29139 protected:
morritae9453ea2014-09-26 03:20:48140 ChannelMojo(Delegate* delegate,
morrita00194e782015-04-09 19:43:27141 scoped_refptr<base::TaskRunner> io_runner,
morrita54f6f80c2014-09-23 21:16:00142 const ChannelHandle& channel_handle,
morrita3b41d6c2014-09-11 19:06:29143 Mode mode,
erikchen27aa7d82015-06-16 21:21:04144 Listener* listener,
145 AttachmentBroker* broker);
morrita3b41d6c2014-09-11 19:06:29146
rockotdbb3bb6b2015-05-11 22:53:22147 void CreateMessagingPipe(mojo::embedder::ScopedPlatformHandle handle,
148 const CreateMessagingPipeCallback& callback);
morritaf8f92dcd2014-10-27 20:10:25149 void InitMessageReader(mojo::ScopedMessagePipeHandle pipe, int32_t peer_pid);
150
151 Listener* listener() const { return listener_; }
152 void set_peer_pid(base::ProcessId pid) { peer_pid_ = pid; }
153
[email protected]64860882014-08-04 23:44:17154 private:
155 struct ChannelInfoDeleter {
rockotdbb3bb6b2015-05-11 22:53:22156 explicit ChannelInfoDeleter(scoped_refptr<base::TaskRunner> io_runner);
157 ~ChannelInfoDeleter();
158
[email protected]64860882014-08-04 23:44:17159 void operator()(mojo::embedder::ChannelInfo* ptr) const;
rockotdbb3bb6b2015-05-11 22:53:22160
161 scoped_refptr<base::TaskRunner> io_runner;
[email protected]64860882014-08-04 23:44:17162 };
163
164 // ChannelMojo needs to kill its MessagePipeReader in delayed manner
165 // because the channel wants to kill these readers during the
166 // notifications invoked by them.
167 typedef internal::MessagePipeReader::DelayedDeleter ReaderDeleter;
168
morrita00194e782015-04-09 19:43:27169 void InitOnIOThread(ChannelMojo::Delegate* delegate);
[email protected]64860882014-08-04 23:44:17170
rockotdbb3bb6b2015-05-11 22:53:22171 static void CreateMessagingPipeOnIOThread(
172 mojo::embedder::ScopedPlatformHandle handle,
173 scoped_refptr<base::TaskRunner> callback_runner,
174 const CreateMessagingPipeOnIOThreadCallback& callback);
175 void OnMessagingPipeCreated(const CreateMessagingPipeCallback& callback,
176 mojo::ScopedMessagePipeHandle handle,
177 mojo::embedder::ChannelInfo* channel_info);
178
morrita54f6f80c2014-09-23 21:16:00179 scoped_ptr<MojoBootstrap> bootstrap_;
morritae9453ea2014-09-26 03:20:48180 base::WeakPtr<Delegate> delegate_;
[email protected]64860882014-08-04 23:44:17181 Mode mode_;
182 Listener* listener_;
183 base::ProcessId peer_pid_;
morritab4472142015-04-20 21:20:12184 scoped_refptr<base::TaskRunner> io_runner_;
[email protected]64860882014-08-04 23:44:17185 scoped_ptr<mojo::embedder::ChannelInfo,
186 ChannelInfoDeleter> channel_info_;
187
morritab4472142015-04-20 21:20:12188 // Guards |message_reader_| and |pending_messages_|
189 //
190 // * The contents of |pending_messages_| can be modified from any thread.
191 // * |message_reader_| is modified only from the IO thread,
192 // but they can be referenced from other threads.
193 base::Lock lock_;
morritad68bedf42014-11-25 23:35:57194 scoped_ptr<internal::MessagePipeReader, ReaderDeleter> message_reader_;
[email protected]64860882014-08-04 23:44:17195 ScopedVector<Message> pending_messages_;
196
rockotcbca72f2015-03-03 16:31:04197 scoped_ptr<ScopedIPCSupport> ipc_support_;
198
anujk.sharma0184ced2014-08-28 06:49:02199 base::WeakPtrFactory<ChannelMojo> weak_factory_;
200
[email protected]64860882014-08-04 23:44:17201 DISALLOW_COPY_AND_ASSIGN(ChannelMojo);
202};
203
204} // namespace IPC
205
206#endif // IPC_IPC_CHANNEL_MOJO_H_