blob: e5a4e335541c7f46d194290f2b88bb51b4aecc5c [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"
morrita@chromium.org15996aa2014-08-05 08:44:17 +090018#include "mojo/public/cpp/system/core.h"
19
20namespace mojo {
21namespace embedder {
22struct ChannelInfo;
23}
24}
25
26namespace IPC {
27
morrita42bda252014-09-12 04:06:29 +090028namespace internal {
29class ControlReader;
30class ServerControlReader;
31class ClientControlReader;
32class MessageReader;
33}
34
morrita@chromium.org15996aa2014-08-05 08:44:17 +090035// Mojo-based IPC::Channel implementation over a platform handle.
36//
37// ChannelMojo builds Mojo MessagePipe using underlying pipe given by
38// "bootstrap" IPC::Channel which creates and owns platform pipe like
39// named socket. The bootstrap Channel is used only for establishing
40// the underlying connection. ChannelMojo takes its handle over once
41// the it is made and puts MessagePipe on it.
42//
43// ChannelMojo has a couple of MessagePipes:
44//
45// * The first MessagePipe, which is built on top of bootstrap handle,
46// is the "control" pipe. It is used to communicate out-of-band
47// control messages that aren't visible from IPC::Listener.
48//
49// * The second MessagePipe, which is created by the server channel
50// and sent to client Channel over the control pipe, is used
51// to send IPC::Messages as an IPC::Sender.
52//
53// TODO(morrita): Extract handle creation part of IPC::Channel into
54// separate class to clarify what ChannelMojo relies
55// on.
56// TODO(morrita): Add APIs to create extra MessagePipes to let
57// Mojo-based objects talk over this Channel.
58//
morrita7c48ab82014-09-24 06:16:00 +090059class IPC_MOJO_EXPORT ChannelMojo : public Channel,
60 public MojoBootstrap::Delegate {
morrita@chromium.org15996aa2014-08-05 08:44:17 +090061 public:
morrita98b6e4a2014-09-26 12:20:48 +090062 class Delegate {
63 public:
64 virtual ~Delegate() {}
65 virtual base::WeakPtr<Delegate> ToWeakPtr() = 0;
66 virtual scoped_refptr<base::TaskRunner> GetIOTaskRunner() = 0;
67 virtual void OnChannelCreated(base::WeakPtr<ChannelMojo> channel) = 0;
68 };
69
morrita952f4652014-10-21 10:16:35 +090070 // True if ChannelMojo should be used regardless of the flag.
71 static bool ShouldBeUsed();
72
morrita@chromium.org15996aa2014-08-05 08:44:17 +090073 // Create ChannelMojo. A bootstrap channel is created as well.
morrita98b6e4a2014-09-26 12:20:48 +090074 // |host| must not be null for server channels.
75 static scoped_ptr<ChannelMojo> Create(Delegate* delegate,
morrita7c48ab82014-09-24 06:16:00 +090076 const ChannelHandle& channel_handle,
77 Mode mode,
78 Listener* listener);
morrita@chromium.org15996aa2014-08-05 08:44:17 +090079
80 // Create a factory object for ChannelMojo.
81 // The factory is used to create Mojo-based ChannelProxy family.
morrita7c48ab82014-09-24 06:16:00 +090082 // |host| must not be null.
83 static scoped_ptr<ChannelFactory> CreateServerFactory(
morrita98b6e4a2014-09-26 12:20:48 +090084 Delegate* delegate,
morrita7c48ab82014-09-24 06:16:00 +090085 const ChannelHandle& channel_handle);
86
87 static scoped_ptr<ChannelFactory> CreateClientFactory(
88 const ChannelHandle& channel_handle);
morrita@chromium.org15996aa2014-08-05 08:44:17 +090089
dchengef7721a2014-10-22 11:29:52 +090090 ~ChannelMojo() override;
morrita@chromium.org15996aa2014-08-05 08:44:17 +090091
morrita7c48ab82014-09-24 06:16:00 +090092 // ChannelMojoHost tells the client handle using this API.
93 void OnClientLaunched(base::ProcessHandle handle);
94
morrita@chromium.org15996aa2014-08-05 08:44:17 +090095 // Channel implementation
dchengef7721a2014-10-22 11:29:52 +090096 bool Connect() override;
97 void Close() override;
98 bool Send(Message* message) override;
99 base::ProcessId GetPeerPID() const override;
100 base::ProcessId GetSelfPID() const override;
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900101
102#if defined(OS_POSIX) && !defined(OS_NACL)
dchengef7721a2014-10-22 11:29:52 +0900103 int GetClientFileDescriptor() const override;
104 base::ScopedFD TakeClientFileDescriptor() override;
morrita42bda252014-09-12 04:06:29 +0900105
106 // These access protected API of IPC::Message, which has ChannelMojo
107 // as a friend class.
108 static MojoResult WriteToFileDescriptorSet(
109 const std::vector<MojoHandle>& handle_buffer,
110 Message* message);
morritaab207252014-09-25 05:11:45 +0900111 static MojoResult ReadFromFileDescriptorSet(Message* message,
morrita42bda252014-09-12 04:06:29 +0900112 std::vector<MojoHandle>* handles);
113
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900114#endif // defined(OS_POSIX) && !defined(OS_NACL)
115
morrita7c48ab82014-09-24 06:16:00 +0900116 // MojoBootstrapDelegate implementation
dchengef7721a2014-10-22 11:29:52 +0900117 void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle) override;
118 void OnBootstrapError() override;
morrita7c48ab82014-09-24 06:16:00 +0900119
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900120 // Called from MessagePipeReader implementations
121 void OnMessageReceived(Message& message);
122 void OnConnected(mojo::ScopedMessagePipeHandle pipe);
123 void OnPipeClosed(internal::MessagePipeReader* reader);
124 void OnPipeError(internal::MessagePipeReader* reader);
125 void set_peer_pid(base::ProcessId pid) { peer_pid_ = pid; }
126
morrita42bda252014-09-12 04:06:29 +0900127 protected:
morrita98b6e4a2014-09-26 12:20:48 +0900128 ChannelMojo(Delegate* delegate,
morrita7c48ab82014-09-24 06:16:00 +0900129 const ChannelHandle& channel_handle,
morrita42bda252014-09-12 04:06:29 +0900130 Mode mode,
morrita7c48ab82014-09-24 06:16:00 +0900131 Listener* listener);
morrita42bda252014-09-12 04:06:29 +0900132
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900133 private:
134 struct ChannelInfoDeleter {
135 void operator()(mojo::embedder::ChannelInfo* ptr) const;
136 };
137
138 // ChannelMojo needs to kill its MessagePipeReader in delayed manner
139 // because the channel wants to kill these readers during the
140 // notifications invoked by them.
141 typedef internal::MessagePipeReader::DelayedDeleter ReaderDeleter;
142
morrita98b6e4a2014-09-26 12:20:48 +0900143 void InitDelegate(ChannelMojo::Delegate* delegate);
morrita7c48ab82014-09-24 06:16:00 +0900144 void InitControlReader(mojo::embedder::ScopedPlatformHandle handle);
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900145
morrita7c48ab82014-09-24 06:16:00 +0900146 scoped_ptr<MojoBootstrap> bootstrap_;
morrita98b6e4a2014-09-26 12:20:48 +0900147 base::WeakPtr<Delegate> delegate_;
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900148 Mode mode_;
149 Listener* listener_;
150 base::ProcessId peer_pid_;
151 scoped_ptr<mojo::embedder::ChannelInfo,
152 ChannelInfoDeleter> channel_info_;
153
morrita42bda252014-09-12 04:06:29 +0900154 scoped_ptr<internal::ControlReader, ReaderDeleter> control_reader_;
155 scoped_ptr<internal::MessageReader, ReaderDeleter> message_reader_;
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900156 ScopedVector<Message> pending_messages_;
157
anujk.sharma8461adf2014-08-28 15:49:02 +0900158 base::WeakPtrFactory<ChannelMojo> weak_factory_;
159
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900160 DISALLOW_COPY_AND_ASSIGN(ChannelMojo);
161};
162
163} // namespace IPC
164
165#endif // IPC_IPC_CHANNEL_MOJO_H_