blob: 45d248231fceb31aec8479f93936e9578d3e696d [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"
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"
morrita54f6f80c2014-09-23 21:16:0017#include "ipc/mojo/ipc_mojo_bootstrap.h"
[email protected]64860882014-08-04 23:44:1718#include "mojo/public/cpp/system/core.h"
19
20namespace mojo {
21namespace embedder {
22struct ChannelInfo;
23}
24}
25
26namespace IPC {
27
morrita3b41d6c2014-09-11 19:06:2928namespace internal {
29class ControlReader;
30class ServerControlReader;
31class ClientControlReader;
32class MessageReader;
33}
34
[email protected]64860882014-08-04 23:44:1735// 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//
morrita54f6f80c2014-09-23 21:16:0059class IPC_MOJO_EXPORT ChannelMojo : public Channel,
60 public MojoBootstrap::Delegate {
[email protected]64860882014-08-04 23:44:1761 public:
morritae9453ea2014-09-26 03:20:4862 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
morrita49ca46d2014-10-21 01:16:3570 // True if ChannelMojo should be used regardless of the flag.
71 static bool ShouldBeUsed();
72
[email protected]64860882014-08-04 23:44:1773 // Create ChannelMojo. A bootstrap channel is created as well.
morritae9453ea2014-09-26 03:20:4874 // |host| must not be null for server channels.
75 static scoped_ptr<ChannelMojo> Create(Delegate* delegate,
morrita54f6f80c2014-09-23 21:16:0076 const ChannelHandle& channel_handle,
77 Mode mode,
78 Listener* listener);
[email protected]64860882014-08-04 23:44:1779
80 // Create a factory object for ChannelMojo.
81 // The factory is used to create Mojo-based ChannelProxy family.
morrita54f6f80c2014-09-23 21:16:0082 // |host| must not be null.
83 static scoped_ptr<ChannelFactory> CreateServerFactory(
morritae9453ea2014-09-26 03:20:4884 Delegate* delegate,
morrita54f6f80c2014-09-23 21:16:0085 const ChannelHandle& channel_handle);
86
87 static scoped_ptr<ChannelFactory> CreateClientFactory(
88 const ChannelHandle& channel_handle);
[email protected]64860882014-08-04 23:44:1789
dchengfe61fca2014-10-22 02:29:5290 ~ChannelMojo() override;
[email protected]64860882014-08-04 23:44:1791
morrita54f6f80c2014-09-23 21:16:0092 // ChannelMojoHost tells the client handle using this API.
93 void OnClientLaunched(base::ProcessHandle handle);
94
[email protected]64860882014-08-04 23:44:1795 // Channel implementation
dchengfe61fca2014-10-22 02:29:5296 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;
[email protected]64860882014-08-04 23:44:17101
102#if defined(OS_POSIX) && !defined(OS_NACL)
dchengfe61fca2014-10-22 02:29:52103 int GetClientFileDescriptor() const override;
104 base::ScopedFD TakeClientFileDescriptor() override;
morrita3b41d6c2014-09-11 19:06:29105
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);
morrita96693852014-09-24 20:11:45111 static MojoResult ReadFromFileDescriptorSet(Message* message,
morrita3b41d6c2014-09-11 19:06:29112 std::vector<MojoHandle>* handles);
113
[email protected]64860882014-08-04 23:44:17114#endif // defined(OS_POSIX) && !defined(OS_NACL)
115
morrita54f6f80c2014-09-23 21:16:00116 // MojoBootstrapDelegate implementation
dchengfe61fca2014-10-22 02:29:52117 void OnBootstrapError() override;
morrita54f6f80c2014-09-23 21:16:00118
[email protected]64860882014-08-04 23:44:17119 // Called from MessagePipeReader implementations
120 void OnMessageReceived(Message& message);
[email protected]64860882014-08-04 23:44:17121 void OnPipeClosed(internal::MessagePipeReader* reader);
122 void OnPipeError(internal::MessagePipeReader* reader);
[email protected]64860882014-08-04 23:44:17123
morrita3b41d6c2014-09-11 19:06:29124 protected:
morritae9453ea2014-09-26 03:20:48125 ChannelMojo(Delegate* delegate,
morrita54f6f80c2014-09-23 21:16:00126 const ChannelHandle& channel_handle,
morrita3b41d6c2014-09-11 19:06:29127 Mode mode,
morrita54f6f80c2014-09-23 21:16:00128 Listener* listener);
morrita3b41d6c2014-09-11 19:06:29129
morritaf8f92dcd2014-10-27 20:10:25130 mojo::ScopedMessagePipeHandle CreateMessagingPipe(
131 mojo::embedder::ScopedPlatformHandle handle);
132 void InitMessageReader(mojo::ScopedMessagePipeHandle pipe, int32_t peer_pid);
133
134 Listener* listener() const { return listener_; }
135 void set_peer_pid(base::ProcessId pid) { peer_pid_ = pid; }
136
[email protected]64860882014-08-04 23:44:17137 private:
138 struct ChannelInfoDeleter {
139 void operator()(mojo::embedder::ChannelInfo* ptr) const;
140 };
141
142 // ChannelMojo needs to kill its MessagePipeReader in delayed manner
143 // because the channel wants to kill these readers during the
144 // notifications invoked by them.
145 typedef internal::MessagePipeReader::DelayedDeleter ReaderDeleter;
146
morritae9453ea2014-09-26 03:20:48147 void InitDelegate(ChannelMojo::Delegate* delegate);
[email protected]64860882014-08-04 23:44:17148
morrita54f6f80c2014-09-23 21:16:00149 scoped_ptr<MojoBootstrap> bootstrap_;
morritae9453ea2014-09-26 03:20:48150 base::WeakPtr<Delegate> delegate_;
[email protected]64860882014-08-04 23:44:17151 Mode mode_;
152 Listener* listener_;
153 base::ProcessId peer_pid_;
154 scoped_ptr<mojo::embedder::ChannelInfo,
155 ChannelInfoDeleter> channel_info_;
156
morrita3b41d6c2014-09-11 19:06:29157 scoped_ptr<internal::MessageReader, ReaderDeleter> message_reader_;
[email protected]64860882014-08-04 23:44:17158 ScopedVector<Message> pending_messages_;
159
anujk.sharma0184ced2014-08-28 06:49:02160 base::WeakPtrFactory<ChannelMojo> weak_factory_;
161
[email protected]64860882014-08-04 23:44:17162 DISALLOW_COPY_AND_ASSIGN(ChannelMojo);
163};
164
165} // namespace IPC
166
167#endif // IPC_IPC_CHANNEL_MOJO_H_