Add ICU message format support
[chromium-blink-merge.git] / ipc / mojo / ipc_channel_mojo.h
blobe7b7b767a68a5a82d20fea25ad0a92121b3dffe5
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 "base/synchronization/lock.h"
14 #include "ipc/ipc_channel.h"
15 #include "ipc/ipc_channel_factory.h"
16 #include "ipc/ipc_export.h"
17 #include "ipc/mojo/ipc_message_pipe_reader.h"
18 #include "ipc/mojo/ipc_mojo_bootstrap.h"
19 #include "ipc/mojo/scoped_ipc_support.h"
20 #include "third_party/mojo/src/mojo/edk/embedder/channel_info_forward.h"
21 #include "third_party/mojo/src/mojo/public/cpp/system/core.h"
23 namespace IPC {
25 // Mojo-based IPC::Channel implementation over a platform handle.
27 // ChannelMojo builds Mojo MessagePipe using underlying pipe given by
28 // "bootstrap" IPC::Channel which creates and owns platform pipe like
29 // named socket. The bootstrap Channel is used only for establishing
30 // the underlying connection. ChannelMojo takes its handle over once
31 // the it is made and puts MessagePipe on it.
33 // ChannelMojo has a couple of MessagePipes:
35 // * The first MessagePipe, which is built on top of bootstrap handle,
36 // is the "control" pipe. It is used to communicate out-of-band
37 // control messages that aren't visible from IPC::Listener.
39 // * The second MessagePipe, which is created by the server channel
40 // and sent to client Channel over the control pipe, is used
41 // to send IPC::Messages as an IPC::Sender.
43 // TODO(morrita): Extract handle creation part of IPC::Channel into
44 // separate class to clarify what ChannelMojo relies
45 // on.
46 // TODO(morrita): Add APIs to create extra MessagePipes to let
47 // Mojo-based objects talk over this Channel.
49 class IPC_MOJO_EXPORT ChannelMojo
50 : public Channel,
51 public MojoBootstrap::Delegate,
52 public NON_EXPORTED_BASE(internal::MessagePipeReader::Delegate) {
53 public:
54 using CreateMessagingPipeCallback =
55 base::Callback<void(mojo::ScopedMessagePipeHandle)>;
56 using CreateMessagingPipeOnIOThreadCallback =
57 base::Callback<void(mojo::ScopedMessagePipeHandle,
58 mojo::embedder::ChannelInfo*)>;
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 // |broker| must outlive the newly created channel.
65 static scoped_ptr<ChannelMojo> Create(
66 scoped_refptr<base::TaskRunner> io_runner,
67 const ChannelHandle& channel_handle,
68 Mode mode,
69 Listener* listener,
70 AttachmentBroker* broker);
72 // Create a factory object for ChannelMojo.
73 // The factory is used to create Mojo-based ChannelProxy family.
74 // |host| must not be null.
75 // TODO(erikchen): Remove default parameter for |broker|. It exists only to
76 // make the upcoming refactor decomposable into smaller CLs.
77 // http://crbug.com/493414.
78 // |broker| must outlive the factory and all channels it creates.
79 static scoped_ptr<ChannelFactory> CreateServerFactory(
80 scoped_refptr<base::TaskRunner> io_runner,
81 const ChannelHandle& channel_handle,
82 AttachmentBroker* broker = nullptr);
84 // TODO(erikchen): Remove default parameter for |broker|. It exists only to
85 // make the upcoming refactor decomposable into smaller CLs.
86 // http://crbug.com/493414.
87 // |broker| must outlive the factory and all channels it creates.
88 static scoped_ptr<ChannelFactory> CreateClientFactory(
89 scoped_refptr<base::TaskRunner> io_runner,
90 const ChannelHandle& channel_handle,
91 AttachmentBroker* broker = nullptr);
93 ~ChannelMojo() override;
95 // Channel implementation
96 bool Connect() override;
97 void Close() override;
98 bool Send(Message* message) override;
99 bool IsSendThreadSafe() const override;
100 base::ProcessId GetPeerPID() const override;
101 base::ProcessId GetSelfPID() const override;
103 #if defined(OS_POSIX) && !defined(OS_NACL)
104 int GetClientFileDescriptor() const override;
105 base::ScopedFD TakeClientFileDescriptor() override;
106 #endif // defined(OS_POSIX) && !defined(OS_NACL)
108 // These access protected API of IPC::Message, which has ChannelMojo
109 // as a friend class.
110 static MojoResult WriteToMessageAttachmentSet(
111 const std::vector<MojoHandle>& handle_buffer,
112 Message* message);
113 static MojoResult ReadFromMessageAttachmentSet(
114 Message* message,
115 std::vector<MojoHandle>* handles);
117 // MojoBootstrapDelegate implementation
118 void OnBootstrapError() override;
120 // MessagePipeReader::Delegate
121 void OnMessageReceived(Message& message) override;
122 void OnPipeClosed(internal::MessagePipeReader* reader) override;
123 void OnPipeError(internal::MessagePipeReader* reader) override;
125 protected:
126 ChannelMojo(scoped_refptr<base::TaskRunner> io_runner,
127 const ChannelHandle& channel_handle,
128 Mode mode,
129 Listener* listener,
130 AttachmentBroker* broker);
132 void CreateMessagingPipe(mojo::embedder::ScopedPlatformHandle handle,
133 const CreateMessagingPipeCallback& callback);
134 void InitMessageReader(mojo::ScopedMessagePipeHandle pipe, int32_t peer_pid);
136 Listener* listener() const { return listener_; }
137 void set_peer_pid(base::ProcessId pid) { peer_pid_ = pid; }
139 private:
140 struct ChannelInfoDeleter {
141 explicit ChannelInfoDeleter(scoped_refptr<base::TaskRunner> io_runner);
142 ~ChannelInfoDeleter();
144 void operator()(mojo::embedder::ChannelInfo* ptr) const;
146 scoped_refptr<base::TaskRunner> io_runner;
149 // ChannelMojo needs to kill its MessagePipeReader in delayed manner
150 // because the channel wants to kill these readers during the
151 // notifications invoked by them.
152 typedef internal::MessagePipeReader::DelayedDeleter ReaderDeleter;
154 void InitOnIOThread();
156 static void CreateMessagingPipeOnIOThread(
157 mojo::embedder::ScopedPlatformHandle handle,
158 scoped_refptr<base::TaskRunner> callback_runner,
159 const CreateMessagingPipeOnIOThreadCallback& callback);
160 void OnMessagingPipeCreated(const CreateMessagingPipeCallback& callback,
161 mojo::ScopedMessagePipeHandle handle,
162 mojo::embedder::ChannelInfo* channel_info);
164 scoped_ptr<MojoBootstrap> bootstrap_;
165 Listener* listener_;
166 base::ProcessId peer_pid_;
167 scoped_refptr<base::TaskRunner> io_runner_;
168 scoped_ptr<mojo::embedder::ChannelInfo,
169 ChannelInfoDeleter> channel_info_;
171 // Guards |message_reader_|, |waiting_connect_| and |pending_messages_|
173 // * The contents of |pending_messages_| can be modified from any thread.
174 // * |message_reader_| is modified only from the IO thread,
175 // but they can be referenced from other threads.
176 base::Lock lock_;
177 scoped_ptr<internal::MessagePipeReader, ReaderDeleter> message_reader_;
178 ScopedVector<Message> pending_messages_;
179 bool waiting_connect_;
181 scoped_ptr<ScopedIPCSupport> ipc_support_;
183 base::WeakPtrFactory<ChannelMojo> weak_factory_;
185 DISALLOW_COPY_AND_ASSIGN(ChannelMojo);
188 } // namespace IPC
190 #endif // IPC_IPC_CHANNEL_MOJO_H_