blob: a5ddf1e4077fc07077ed10248d632a00fe7fff81 [file] [log] [blame]
morrita@chromium.org15996aa2014-08-05 08:44:17 +09001// 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
avi42ebda42015-12-22 11:39:04 +09008#include <stdint.h>
9
morrita@chromium.org15996aa2014-08-05 08:44:17 +090010#include <vector>
11
avi42ebda42015-12-22 11:39:04 +090012#include "base/macros.h"
morrita@chromium.org15996aa2014-08-05 08:44:17 +090013#include "base/memory/scoped_ptr.h"
14#include "base/memory/scoped_vector.h"
15#include "base/memory/weak_ptr.h"
morrita5138bf52015-04-21 06:20:12 +090016#include "base/synchronization/lock.h"
avi42ebda42015-12-22 11:39:04 +090017#include "build/build_config.h"
morrita@chromium.org15996aa2014-08-05 08:44:17 +090018#include "ipc/ipc_channel.h"
19#include "ipc/ipc_channel_factory.h"
20#include "ipc/ipc_export.h"
21#include "ipc/mojo/ipc_message_pipe_reader.h"
morrita7c48ab82014-09-24 06:16:00 +090022#include "ipc/mojo/ipc_mojo_bootstrap.h"
rockot79524372015-03-04 01:31:04 +090023#include "ipc/mojo/scoped_ipc_support.h"
rockotaf32acb2015-11-13 10:33:59 +090024#include "mojo/public/cpp/system/core.h"
blundell4bb80352015-01-24 01:27:14 +090025#include "third_party/mojo/src/mojo/edk/embedder/channel_info_forward.h"
morrita@chromium.org15996aa2014-08-05 08:44:17 +090026
morrita@chromium.org15996aa2014-08-05 08:44:17 +090027namespace IPC {
28
29// Mojo-based IPC::Channel implementation over a platform handle.
30//
31// ChannelMojo builds Mojo MessagePipe using underlying pipe given by
32// "bootstrap" IPC::Channel which creates and owns platform pipe like
33// named socket. The bootstrap Channel is used only for establishing
34// the underlying connection. ChannelMojo takes its handle over once
35// the it is made and puts MessagePipe on it.
36//
37// ChannelMojo has a couple of MessagePipes:
38//
39// * The first MessagePipe, which is built on top of bootstrap handle,
40// is the "control" pipe. It is used to communicate out-of-band
41// control messages that aren't visible from IPC::Listener.
42//
43// * The second MessagePipe, which is created by the server channel
44// and sent to client Channel over the control pipe, is used
45// to send IPC::Messages as an IPC::Sender.
46//
47// TODO(morrita): Extract handle creation part of IPC::Channel into
48// separate class to clarify what ChannelMojo relies
49// on.
50// TODO(morrita): Add APIs to create extra MessagePipes to let
51// Mojo-based objects talk over this Channel.
52//
morrita1f42dc62014-11-26 08:35:57 +090053class IPC_MOJO_EXPORT ChannelMojo
54 : public Channel,
55 public MojoBootstrap::Delegate,
56 public NON_EXPORTED_BASE(internal::MessagePipeReader::Delegate) {
morrita@chromium.org15996aa2014-08-05 08:44:17 +090057 public:
rockotab992272015-05-12 07:53:22 +090058 using CreateMessagingPipeCallback =
59 base::Callback<void(mojo::ScopedMessagePipeHandle)>;
60 using CreateMessagingPipeOnIOThreadCallback =
61 base::Callback<void(mojo::ScopedMessagePipeHandle,
62 mojo::embedder::ChannelInfo*)>;
63
morrita952f4652014-10-21 10:16:35 +090064 // True if ChannelMojo should be used regardless of the flag.
65 static bool ShouldBeUsed();
66
morrita@chromium.org15996aa2014-08-05 08:44:17 +090067 // Create ChannelMojo. A bootstrap channel is created as well.
morritad7325c22015-04-10 04:43:27 +090068 static scoped_ptr<ChannelMojo> Create(
morritad7325c22015-04-10 04:43:27 +090069 scoped_refptr<base::TaskRunner> io_runner,
70 const ChannelHandle& channel_handle,
71 Mode mode,
erikchenc596f542015-09-24 12:26:38 +090072 Listener* listener);
morrita@chromium.org15996aa2014-08-05 08:44:17 +090073
74 // Create a factory object for ChannelMojo.
75 // The factory is used to create Mojo-based ChannelProxy family.
morrita7c48ab82014-09-24 06:16:00 +090076 // |host| must not be null.
77 static scoped_ptr<ChannelFactory> CreateServerFactory(
morritad7325c22015-04-10 04:43:27 +090078 scoped_refptr<base::TaskRunner> io_runner,
erikchenc596f542015-09-24 12:26:38 +090079 const ChannelHandle& channel_handle);
morrita7c48ab82014-09-24 06:16:00 +090080
81 static scoped_ptr<ChannelFactory> CreateClientFactory(
morritad7325c22015-04-10 04:43:27 +090082 scoped_refptr<base::TaskRunner> io_runner,
erikchenc596f542015-09-24 12:26:38 +090083 const ChannelHandle& channel_handle);
morrita@chromium.org15996aa2014-08-05 08:44:17 +090084
dchengef7721a2014-10-22 11:29:52 +090085 ~ChannelMojo() override;
morrita@chromium.org15996aa2014-08-05 08:44:17 +090086
87 // Channel implementation
dchengef7721a2014-10-22 11:29:52 +090088 bool Connect() override;
89 void Close() override;
90 bool Send(Message* message) override;
morrita5138bf52015-04-21 06:20:12 +090091 bool IsSendThreadSafe() const override;
dchengef7721a2014-10-22 11:29:52 +090092 base::ProcessId GetPeerPID() const override;
93 base::ProcessId GetSelfPID() const override;
morrita@chromium.org15996aa2014-08-05 08:44:17 +090094
95#if defined(OS_POSIX) && !defined(OS_NACL)
dchengef7721a2014-10-22 11:29:52 +090096 int GetClientFileDescriptor() const override;
97 base::ScopedFD TakeClientFileDescriptor() override;
morrita1b52e4c2015-02-06 09:58:30 +090098#endif // defined(OS_POSIX) && !defined(OS_NACL)
morrita42bda252014-09-12 04:06:29 +090099
100 // These access protected API of IPC::Message, which has ChannelMojo
101 // as a friend class.
morrita33a35902015-01-15 06:17:06 +0900102 static MojoResult WriteToMessageAttachmentSet(
morrita42bda252014-09-12 04:06:29 +0900103 const std::vector<MojoHandle>& handle_buffer,
104 Message* message);
morrita33a35902015-01-15 06:17:06 +0900105 static MojoResult ReadFromMessageAttachmentSet(
106 Message* message,
107 std::vector<MojoHandle>* handles);
morrita42bda252014-09-12 04:06:29 +0900108
morrita7c48ab82014-09-24 06:16:00 +0900109 // MojoBootstrapDelegate implementation
dchengef7721a2014-10-22 11:29:52 +0900110 void OnBootstrapError() override;
morrita7c48ab82014-09-24 06:16:00 +0900111
morrita1f42dc62014-11-26 08:35:57 +0900112 // MessagePipeReader::Delegate
113 void OnMessageReceived(Message& message) override;
114 void OnPipeClosed(internal::MessagePipeReader* reader) override;
115 void OnPipeError(internal::MessagePipeReader* reader) override;
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900116
morrita42bda252014-09-12 04:06:29 +0900117 protected:
leon.han5dd52cd2015-06-19 11:25:48 +0900118 ChannelMojo(scoped_refptr<base::TaskRunner> io_runner,
morrita7c48ab82014-09-24 06:16:00 +0900119 const ChannelHandle& channel_handle,
morrita42bda252014-09-12 04:06:29 +0900120 Mode mode,
erikchenc596f542015-09-24 12:26:38 +0900121 Listener* listener);
morrita42bda252014-09-12 04:06:29 +0900122
rockotab992272015-05-12 07:53:22 +0900123 void CreateMessagingPipe(mojo::embedder::ScopedPlatformHandle handle,
124 const CreateMessagingPipeCallback& callback);
morrita88543a22014-10-28 05:10:25 +0900125 void InitMessageReader(mojo::ScopedMessagePipeHandle pipe, int32_t peer_pid);
126
127 Listener* listener() const { return listener_; }
128 void set_peer_pid(base::ProcessId pid) { peer_pid_ = pid; }
129
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900130 private:
131 struct ChannelInfoDeleter {
rockotab992272015-05-12 07:53:22 +0900132 explicit ChannelInfoDeleter(scoped_refptr<base::TaskRunner> io_runner);
133 ~ChannelInfoDeleter();
134
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900135 void operator()(mojo::embedder::ChannelInfo* ptr) const;
rockotab992272015-05-12 07:53:22 +0900136
137 scoped_refptr<base::TaskRunner> io_runner;
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900138 };
139
140 // ChannelMojo needs to kill its MessagePipeReader in delayed manner
141 // because the channel wants to kill these readers during the
142 // notifications invoked by them.
143 typedef internal::MessagePipeReader::DelayedDeleter ReaderDeleter;
144
leon.han5dd52cd2015-06-19 11:25:48 +0900145 void InitOnIOThread();
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900146
rockotab992272015-05-12 07:53:22 +0900147 static void CreateMessagingPipeOnIOThread(
148 mojo::embedder::ScopedPlatformHandle handle,
149 scoped_refptr<base::TaskRunner> callback_runner,
150 const CreateMessagingPipeOnIOThreadCallback& callback);
151 void OnMessagingPipeCreated(const CreateMessagingPipeCallback& callback,
152 mojo::ScopedMessagePipeHandle handle,
153 mojo::embedder::ChannelInfo* channel_info);
154
morrita7c48ab82014-09-24 06:16:00 +0900155 scoped_ptr<MojoBootstrap> bootstrap_;
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900156 Listener* listener_;
157 base::ProcessId peer_pid_;
morrita5138bf52015-04-21 06:20:12 +0900158 scoped_refptr<base::TaskRunner> io_runner_;
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900159 scoped_ptr<mojo::embedder::ChannelInfo,
160 ChannelInfoDeleter> channel_info_;
161
morrita7ce235f2015-06-24 07:29:36 +0900162 // Guards |message_reader_|, |waiting_connect_| and |pending_messages_|
morrita5138bf52015-04-21 06:20:12 +0900163 //
164 // * The contents of |pending_messages_| can be modified from any thread.
165 // * |message_reader_| is modified only from the IO thread,
166 // but they can be referenced from other threads.
167 base::Lock lock_;
morrita1f42dc62014-11-26 08:35:57 +0900168 scoped_ptr<internal::MessagePipeReader, ReaderDeleter> message_reader_;
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900169 ScopedVector<Message> pending_messages_;
morrita7ce235f2015-06-24 07:29:36 +0900170 bool waiting_connect_;
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900171
rockot79524372015-03-04 01:31:04 +0900172 scoped_ptr<ScopedIPCSupport> ipc_support_;
173
anujk.sharma8461adf2014-08-28 15:49:02 +0900174 base::WeakPtrFactory<ChannelMojo> weak_factory_;
175
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900176 DISALLOW_COPY_AND_ASSIGN(ChannelMojo);
177};
178
179} // namespace IPC
180
181#endif // IPC_IPC_CHANNEL_MOJO_H_