blob: 1959e0f800574e3170fb913885b23fc925011293 [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"
rockotcbca72f2015-03-03 16:31:0418#include "ipc/mojo/scoped_ipc_support.h"
blundell471b74f2015-01-23 16:27:1419#include "third_party/mojo/src/mojo/edk/embedder/channel_info_forward.h"
20#include "third_party/mojo/src/mojo/public/cpp/system/core.h"
[email protected]64860882014-08-04 23:44:1721
[email protected]64860882014-08-04 23:44:1722namespace IPC {
23
24// Mojo-based IPC::Channel implementation over a platform handle.
25//
26// ChannelMojo builds Mojo MessagePipe using underlying pipe given by
27// "bootstrap" IPC::Channel which creates and owns platform pipe like
28// named socket. The bootstrap Channel is used only for establishing
29// the underlying connection. ChannelMojo takes its handle over once
30// the it is made and puts MessagePipe on it.
31//
32// ChannelMojo has a couple of MessagePipes:
33//
34// * The first MessagePipe, which is built on top of bootstrap handle,
35// is the "control" pipe. It is used to communicate out-of-band
36// control messages that aren't visible from IPC::Listener.
37//
38// * The second MessagePipe, which is created by the server channel
39// and sent to client Channel over the control pipe, is used
40// to send IPC::Messages as an IPC::Sender.
41//
42// TODO(morrita): Extract handle creation part of IPC::Channel into
43// separate class to clarify what ChannelMojo relies
44// on.
45// TODO(morrita): Add APIs to create extra MessagePipes to let
46// Mojo-based objects talk over this Channel.
47//
morritad68bedf42014-11-25 23:35:5748class IPC_MOJO_EXPORT ChannelMojo
49 : public Channel,
50 public MojoBootstrap::Delegate,
51 public NON_EXPORTED_BASE(internal::MessagePipeReader::Delegate) {
[email protected]64860882014-08-04 23:44:1752 public:
morritae9453ea2014-09-26 03:20:4853 class Delegate {
54 public:
55 virtual ~Delegate() {}
56 virtual base::WeakPtr<Delegate> ToWeakPtr() = 0;
57 virtual scoped_refptr<base::TaskRunner> GetIOTaskRunner() = 0;
58 virtual void OnChannelCreated(base::WeakPtr<ChannelMojo> channel) = 0;
59 };
60
morrita49ca46d2014-10-21 01:16:3561 // True if ChannelMojo should be used regardless of the flag.
62 static bool ShouldBeUsed();
63
[email protected]64860882014-08-04 23:44:1764 // Create ChannelMojo. A bootstrap channel is created as well.
morritae9453ea2014-09-26 03:20:4865 // |host| must not be null for server channels.
66 static scoped_ptr<ChannelMojo> Create(Delegate* delegate,
morrita54f6f80c2014-09-23 21:16:0067 const ChannelHandle& channel_handle,
68 Mode mode,
69 Listener* listener);
[email protected]64860882014-08-04 23:44:1770
71 // Create a factory object for ChannelMojo.
72 // The factory is used to create Mojo-based ChannelProxy family.
morrita54f6f80c2014-09-23 21:16:0073 // |host| must not be null.
74 static scoped_ptr<ChannelFactory> CreateServerFactory(
morritae9453ea2014-09-26 03:20:4875 Delegate* delegate,
morrita54f6f80c2014-09-23 21:16:0076 const ChannelHandle& channel_handle);
77
78 static scoped_ptr<ChannelFactory> CreateClientFactory(
rockotcbca72f2015-03-03 16:31:0479 Delegate* delegate,
morrita54f6f80c2014-09-23 21:16:0080 const ChannelHandle& channel_handle);
[email protected]64860882014-08-04 23:44:1781
dchengfe61fca2014-10-22 02:29:5282 ~ChannelMojo() override;
[email protected]64860882014-08-04 23:44:1783
morrita54f6f80c2014-09-23 21:16:0084 // ChannelMojoHost tells the client handle using this API.
85 void OnClientLaunched(base::ProcessHandle handle);
86
[email protected]64860882014-08-04 23:44:1787 // Channel implementation
dchengfe61fca2014-10-22 02:29:5288 bool Connect() override;
89 void Close() override;
90 bool Send(Message* message) override;
91 base::ProcessId GetPeerPID() const override;
92 base::ProcessId GetSelfPID() const override;
[email protected]64860882014-08-04 23:44:1793
94#if defined(OS_POSIX) && !defined(OS_NACL)
dchengfe61fca2014-10-22 02:29:5295 int GetClientFileDescriptor() const override;
96 base::ScopedFD TakeClientFileDescriptor() override;
morrita81b17e02015-02-06 00:58:3097#endif // defined(OS_POSIX) && !defined(OS_NACL)
morrita3b41d6c2014-09-11 19:06:2998
99 // These access protected API of IPC::Message, which has ChannelMojo
100 // as a friend class.
morrita4b5c28e22015-01-14 21:17:06101 static MojoResult WriteToMessageAttachmentSet(
morrita3b41d6c2014-09-11 19:06:29102 const std::vector<MojoHandle>& handle_buffer,
103 Message* message);
morrita4b5c28e22015-01-14 21:17:06104 static MojoResult ReadFromMessageAttachmentSet(
105 Message* message,
106 std::vector<MojoHandle>* handles);
morrita3b41d6c2014-09-11 19:06:29107
morrita54f6f80c2014-09-23 21:16:00108 // MojoBootstrapDelegate implementation
dchengfe61fca2014-10-22 02:29:52109 void OnBootstrapError() override;
morrita54f6f80c2014-09-23 21:16:00110
morritad68bedf42014-11-25 23:35:57111 // MessagePipeReader::Delegate
112 void OnMessageReceived(Message& message) override;
113 void OnPipeClosed(internal::MessagePipeReader* reader) override;
114 void OnPipeError(internal::MessagePipeReader* reader) override;
[email protected]64860882014-08-04 23:44:17115
morrita3b41d6c2014-09-11 19:06:29116 protected:
morritae9453ea2014-09-26 03:20:48117 ChannelMojo(Delegate* delegate,
morrita54f6f80c2014-09-23 21:16:00118 const ChannelHandle& channel_handle,
morrita3b41d6c2014-09-11 19:06:29119 Mode mode,
morrita54f6f80c2014-09-23 21:16:00120 Listener* listener);
morrita3b41d6c2014-09-11 19:06:29121
morritaf8f92dcd2014-10-27 20:10:25122 mojo::ScopedMessagePipeHandle CreateMessagingPipe(
123 mojo::embedder::ScopedPlatformHandle handle);
124 void InitMessageReader(mojo::ScopedMessagePipeHandle pipe, int32_t peer_pid);
125
126 Listener* listener() const { return listener_; }
127 void set_peer_pid(base::ProcessId pid) { peer_pid_ = pid; }
128
[email protected]64860882014-08-04 23:44:17129 private:
130 struct ChannelInfoDeleter {
131 void operator()(mojo::embedder::ChannelInfo* ptr) const;
132 };
133
134 // ChannelMojo needs to kill its MessagePipeReader in delayed manner
135 // because the channel wants to kill these readers during the
136 // notifications invoked by them.
137 typedef internal::MessagePipeReader::DelayedDeleter ReaderDeleter;
138
morritae9453ea2014-09-26 03:20:48139 void InitDelegate(ChannelMojo::Delegate* delegate);
[email protected]64860882014-08-04 23:44:17140
morrita54f6f80c2014-09-23 21:16:00141 scoped_ptr<MojoBootstrap> bootstrap_;
morritae9453ea2014-09-26 03:20:48142 base::WeakPtr<Delegate> delegate_;
[email protected]64860882014-08-04 23:44:17143 Mode mode_;
144 Listener* listener_;
145 base::ProcessId peer_pid_;
146 scoped_ptr<mojo::embedder::ChannelInfo,
147 ChannelInfoDeleter> channel_info_;
148
morritad68bedf42014-11-25 23:35:57149 scoped_ptr<internal::MessagePipeReader, ReaderDeleter> message_reader_;
[email protected]64860882014-08-04 23:44:17150 ScopedVector<Message> pending_messages_;
151
rockotcbca72f2015-03-03 16:31:04152 scoped_ptr<ScopedIPCSupport> ipc_support_;
153
anujk.sharma0184ced2014-08-28 06:49:02154 base::WeakPtrFactory<ChannelMojo> weak_factory_;
155
[email protected]64860882014-08-04 23:44:17156 DISALLOW_COPY_AND_ASSIGN(ChannelMojo);
157};
158
159} // namespace IPC
160
161#endif // IPC_IPC_CHANNEL_MOJO_H_