Using "static_assert" in lieu of "COMPILE_ASSERT" in courgete module
[chromium-blink-merge.git] / ipc / mojo / ipc_channel_mojo.h
blob43dd0d015bbb9795a6e139f5d2ed9b04ede328a6
1 // 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.
5 #ifndef IPC_IPC_CHANNEL_MOJO_H_
6 #define IPC_IPC_CHANNEL_MOJO_H_
8 #include <vector>
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"
17 #include "ipc/mojo/ipc_mojo_bootstrap.h"
18 #include "mojo/edk/embedder/channel_info_forward.h"
19 #include "mojo/public/cpp/system/core.h"
21 namespace IPC {
23 // Mojo-based IPC::Channel implementation over a platform handle.
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.
31 // ChannelMojo has a couple of MessagePipes:
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.
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.
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.
47 class IPC_MOJO_EXPORT ChannelMojo
48 : public Channel,
49 public MojoBootstrap::Delegate,
50 public NON_EXPORTED_BASE(internal::MessagePipeReader::Delegate) {
51 public:
52 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;
60 // True if ChannelMojo should be used regardless of the flag.
61 static bool ShouldBeUsed();
63 // Create ChannelMojo. A bootstrap channel is created as well.
64 // |host| must not be null for server channels.
65 static scoped_ptr<ChannelMojo> Create(Delegate* delegate,
66 const ChannelHandle& channel_handle,
67 Mode mode,
68 Listener* listener);
70 // Create a factory object for ChannelMojo.
71 // The factory is used to create Mojo-based ChannelProxy family.
72 // |host| must not be null.
73 static scoped_ptr<ChannelFactory> CreateServerFactory(
74 Delegate* delegate,
75 const ChannelHandle& channel_handle);
77 static scoped_ptr<ChannelFactory> CreateClientFactory(
78 const ChannelHandle& channel_handle);
80 ~ChannelMojo() override;
82 // ChannelMojoHost tells the client handle using this API.
83 void OnClientLaunched(base::ProcessHandle handle);
85 // Channel implementation
86 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;
92 #if defined(OS_POSIX) && !defined(OS_NACL)
93 int GetClientFileDescriptor() const override;
94 base::ScopedFD TakeClientFileDescriptor() override;
96 // These access protected API of IPC::Message, which has ChannelMojo
97 // as a friend class.
98 static MojoResult WriteToMessageAttachmentSet(
99 const std::vector<MojoHandle>& handle_buffer,
100 Message* message);
101 static MojoResult ReadFromMessageAttachmentSet(
102 Message* message,
103 std::vector<MojoHandle>* handles);
105 #endif // defined(OS_POSIX) && !defined(OS_NACL)
107 // MojoBootstrapDelegate implementation
108 void OnBootstrapError() override;
110 // MessagePipeReader::Delegate
111 void OnMessageReceived(Message& message) override;
112 void OnPipeClosed(internal::MessagePipeReader* reader) override;
113 void OnPipeError(internal::MessagePipeReader* reader) override;
115 protected:
116 ChannelMojo(Delegate* delegate,
117 const ChannelHandle& channel_handle,
118 Mode mode,
119 Listener* listener);
121 mojo::ScopedMessagePipeHandle CreateMessagingPipe(
122 mojo::embedder::ScopedPlatformHandle handle);
123 void InitMessageReader(mojo::ScopedMessagePipeHandle pipe, int32_t peer_pid);
125 Listener* listener() const { return listener_; }
126 void set_peer_pid(base::ProcessId pid) { peer_pid_ = pid; }
128 private:
129 struct ChannelInfoDeleter {
130 void operator()(mojo::embedder::ChannelInfo* ptr) const;
133 // ChannelMojo needs to kill its MessagePipeReader in delayed manner
134 // because the channel wants to kill these readers during the
135 // notifications invoked by them.
136 typedef internal::MessagePipeReader::DelayedDeleter ReaderDeleter;
138 void InitDelegate(ChannelMojo::Delegate* delegate);
140 scoped_ptr<MojoBootstrap> bootstrap_;
141 base::WeakPtr<Delegate> delegate_;
142 Mode mode_;
143 Listener* listener_;
144 base::ProcessId peer_pid_;
145 scoped_ptr<mojo::embedder::ChannelInfo,
146 ChannelInfoDeleter> channel_info_;
148 scoped_ptr<internal::MessagePipeReader, ReaderDeleter> message_reader_;
149 ScopedVector<Message> pending_messages_;
151 base::WeakPtrFactory<ChannelMojo> weak_factory_;
153 DISALLOW_COPY_AND_ASSIGN(ChannelMojo);
156 } // namespace IPC
158 #endif // IPC_IPC_CHANNEL_MOJO_H_