blob: 1ef32c6bf2704e7f796b77dcdd2bff3b648662e8 [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
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"
morrita7c48ab82014-09-24 06:16:00 +090017#include "ipc/mojo/ipc_mojo_bootstrap.h"
jamesrdbb52dc2014-11-07 10:24:51 +090018#include "mojo/edk/embedder/channel_info_forward.h"
morrita@chromium.org15996aa2014-08-05 08:44:17 +090019#include "mojo/public/cpp/system/core.h"
20
morrita@chromium.org15996aa2014-08-05 08:44:17 +090021namespace IPC {
22
23// Mojo-based IPC::Channel implementation over a platform handle.
24//
25// ChannelMojo builds Mojo MessagePipe using underlying pipe given by
26// "bootstrap" IPC::Channel which creates and owns platform pipe like
27// named socket. The bootstrap Channel is used only for establishing
28// the underlying connection. ChannelMojo takes its handle over once
29// the it is made and puts MessagePipe on it.
30//
31// ChannelMojo has a couple of MessagePipes:
32//
33// * The first MessagePipe, which is built on top of bootstrap handle,
34// is the "control" pipe. It is used to communicate out-of-band
35// control messages that aren't visible from IPC::Listener.
36//
37// * The second MessagePipe, which is created by the server channel
38// and sent to client Channel over the control pipe, is used
39// to send IPC::Messages as an IPC::Sender.
40//
41// TODO(morrita): Extract handle creation part of IPC::Channel into
42// separate class to clarify what ChannelMojo relies
43// on.
44// TODO(morrita): Add APIs to create extra MessagePipes to let
45// Mojo-based objects talk over this Channel.
46//
morrita1f42dc62014-11-26 08:35:57 +090047class IPC_MOJO_EXPORT ChannelMojo
48 : public Channel,
49 public MojoBootstrap::Delegate,
50 public NON_EXPORTED_BASE(internal::MessagePipeReader::Delegate) {
morrita@chromium.org15996aa2014-08-05 08:44:17 +090051 public:
morrita98b6e4a2014-09-26 12:20:48 +090052 class Delegate {
53 public:
54 virtual ~Delegate() {}
55 virtual base::WeakPtr<Delegate> ToWeakPtr() = 0;
56 virtual scoped_refptr<base::TaskRunner> GetIOTaskRunner() = 0;
57 virtual void OnChannelCreated(base::WeakPtr<ChannelMojo> channel) = 0;
58 };
59
morrita952f4652014-10-21 10:16:35 +090060 // True if ChannelMojo should be used regardless of the flag.
61 static bool ShouldBeUsed();
62
morrita@chromium.org15996aa2014-08-05 08:44:17 +090063 // Create ChannelMojo. A bootstrap channel is created as well.
morrita98b6e4a2014-09-26 12:20:48 +090064 // |host| must not be null for server channels.
65 static scoped_ptr<ChannelMojo> Create(Delegate* delegate,
morrita7c48ab82014-09-24 06:16:00 +090066 const ChannelHandle& channel_handle,
67 Mode mode,
68 Listener* listener);
morrita@chromium.org15996aa2014-08-05 08:44:17 +090069
70 // Create a factory object for ChannelMojo.
71 // The factory is used to create Mojo-based ChannelProxy family.
morrita7c48ab82014-09-24 06:16:00 +090072 // |host| must not be null.
73 static scoped_ptr<ChannelFactory> CreateServerFactory(
morrita98b6e4a2014-09-26 12:20:48 +090074 Delegate* delegate,
morrita7c48ab82014-09-24 06:16:00 +090075 const ChannelHandle& channel_handle);
76
77 static scoped_ptr<ChannelFactory> CreateClientFactory(
78 const ChannelHandle& channel_handle);
morrita@chromium.org15996aa2014-08-05 08:44:17 +090079
dchengef7721a2014-10-22 11:29:52 +090080 ~ChannelMojo() override;
morrita@chromium.org15996aa2014-08-05 08:44:17 +090081
morrita7c48ab82014-09-24 06:16:00 +090082 // ChannelMojoHost tells the client handle using this API.
83 void OnClientLaunched(base::ProcessHandle handle);
84
morrita@chromium.org15996aa2014-08-05 08:44:17 +090085 // Channel implementation
dchengef7721a2014-10-22 11:29:52 +090086 bool Connect() override;
87 void Close() override;
88 bool Send(Message* message) override;
89 base::ProcessId GetPeerPID() const override;
90 base::ProcessId GetSelfPID() const override;
morrita@chromium.org15996aa2014-08-05 08:44:17 +090091
92#if defined(OS_POSIX) && !defined(OS_NACL)
dchengef7721a2014-10-22 11:29:52 +090093 int GetClientFileDescriptor() const override;
94 base::ScopedFD TakeClientFileDescriptor() override;
morrita42bda252014-09-12 04:06:29 +090095
96 // These access protected API of IPC::Message, which has ChannelMojo
97 // as a friend class.
98 static MojoResult WriteToFileDescriptorSet(
99 const std::vector<MojoHandle>& handle_buffer,
100 Message* message);
morritaab207252014-09-25 05:11:45 +0900101 static MojoResult ReadFromFileDescriptorSet(Message* message,
morrita42bda252014-09-12 04:06:29 +0900102 std::vector<MojoHandle>* handles);
103
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900104#endif // defined(OS_POSIX) && !defined(OS_NACL)
105
morrita7c48ab82014-09-24 06:16:00 +0900106 // MojoBootstrapDelegate implementation
dchengef7721a2014-10-22 11:29:52 +0900107 void OnBootstrapError() override;
morrita7c48ab82014-09-24 06:16:00 +0900108
morrita1f42dc62014-11-26 08:35:57 +0900109 // MessagePipeReader::Delegate
110 void OnMessageReceived(Message& message) override;
111 void OnPipeClosed(internal::MessagePipeReader* reader) override;
112 void OnPipeError(internal::MessagePipeReader* reader) override;
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900113
morrita42bda252014-09-12 04:06:29 +0900114 protected:
morrita98b6e4a2014-09-26 12:20:48 +0900115 ChannelMojo(Delegate* delegate,
morrita7c48ab82014-09-24 06:16:00 +0900116 const ChannelHandle& channel_handle,
morrita42bda252014-09-12 04:06:29 +0900117 Mode mode,
morrita7c48ab82014-09-24 06:16:00 +0900118 Listener* listener);
morrita42bda252014-09-12 04:06:29 +0900119
morrita88543a22014-10-28 05:10:25 +0900120 mojo::ScopedMessagePipeHandle CreateMessagingPipe(
121 mojo::embedder::ScopedPlatformHandle handle);
122 void InitMessageReader(mojo::ScopedMessagePipeHandle pipe, int32_t peer_pid);
123
124 Listener* listener() const { return listener_; }
125 void set_peer_pid(base::ProcessId pid) { peer_pid_ = pid; }
126
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900127 private:
128 struct ChannelInfoDeleter {
129 void operator()(mojo::embedder::ChannelInfo* ptr) const;
130 };
131
132 // ChannelMojo needs to kill its MessagePipeReader in delayed manner
133 // because the channel wants to kill these readers during the
134 // notifications invoked by them.
135 typedef internal::MessagePipeReader::DelayedDeleter ReaderDeleter;
136
morrita98b6e4a2014-09-26 12:20:48 +0900137 void InitDelegate(ChannelMojo::Delegate* delegate);
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900138
morrita7c48ab82014-09-24 06:16:00 +0900139 scoped_ptr<MojoBootstrap> bootstrap_;
morrita98b6e4a2014-09-26 12:20:48 +0900140 base::WeakPtr<Delegate> delegate_;
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900141 Mode mode_;
142 Listener* listener_;
143 base::ProcessId peer_pid_;
144 scoped_ptr<mojo::embedder::ChannelInfo,
145 ChannelInfoDeleter> channel_info_;
146
morrita1f42dc62014-11-26 08:35:57 +0900147 scoped_ptr<internal::MessagePipeReader, ReaderDeleter> message_reader_;
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900148 ScopedVector<Message> pending_messages_;
149
anujk.sharma8461adf2014-08-28 15:49:02 +0900150 base::WeakPtrFactory<ChannelMojo> weak_factory_;
151
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900152 DISALLOW_COPY_AND_ASSIGN(ChannelMojo);
153};
154
155} // namespace IPC
156
157#endif // IPC_IPC_CHANNEL_MOJO_H_