GPU workaround to simulate Out of Memory errors with large textures
[chromium-blink-merge.git] / ipc / mojo / ipc_channel_mojo.h
blob1959e0f800574e3170fb913885b23fc925011293
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 "ipc/mojo/scoped_ipc_support.h"
19 #include "third_party/mojo/src/mojo/edk/embedder/channel_info_forward.h"
20 #include "third_party/mojo/src/mojo/public/cpp/system/core.h"
22 namespace IPC {
24 // Mojo-based IPC::Channel implementation over a platform handle.
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.
32 // ChannelMojo has a couple of MessagePipes:
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.
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.
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.
48 class IPC_MOJO_EXPORT ChannelMojo
49 : public Channel,
50 public MojoBootstrap::Delegate,
51 public NON_EXPORTED_BASE(internal::MessagePipeReader::Delegate) {
52 public:
53 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;
61 // True if ChannelMojo should be used regardless of the flag.
62 static bool ShouldBeUsed();
64 // Create ChannelMojo. A bootstrap channel is created as well.
65 // |host| must not be null for server channels.
66 static scoped_ptr<ChannelMojo> Create(Delegate* delegate,
67 const ChannelHandle& channel_handle,
68 Mode mode,
69 Listener* listener);
71 // Create a factory object for ChannelMojo.
72 // The factory is used to create Mojo-based ChannelProxy family.
73 // |host| must not be null.
74 static scoped_ptr<ChannelFactory> CreateServerFactory(
75 Delegate* delegate,
76 const ChannelHandle& channel_handle);
78 static scoped_ptr<ChannelFactory> CreateClientFactory(
79 Delegate* delegate,
80 const ChannelHandle& channel_handle);
82 ~ChannelMojo() override;
84 // ChannelMojoHost tells the client handle using this API.
85 void OnClientLaunched(base::ProcessHandle handle);
87 // Channel implementation
88 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;
94 #if defined(OS_POSIX) && !defined(OS_NACL)
95 int GetClientFileDescriptor() const override;
96 base::ScopedFD TakeClientFileDescriptor() override;
97 #endif // defined(OS_POSIX) && !defined(OS_NACL)
99 // These access protected API of IPC::Message, which has ChannelMojo
100 // as a friend class.
101 static MojoResult WriteToMessageAttachmentSet(
102 const std::vector<MojoHandle>& handle_buffer,
103 Message* message);
104 static MojoResult ReadFromMessageAttachmentSet(
105 Message* message,
106 std::vector<MojoHandle>* handles);
108 // MojoBootstrapDelegate implementation
109 void OnBootstrapError() override;
111 // MessagePipeReader::Delegate
112 void OnMessageReceived(Message& message) override;
113 void OnPipeClosed(internal::MessagePipeReader* reader) override;
114 void OnPipeError(internal::MessagePipeReader* reader) override;
116 protected:
117 ChannelMojo(Delegate* delegate,
118 const ChannelHandle& channel_handle,
119 Mode mode,
120 Listener* listener);
122 mojo::ScopedMessagePipeHandle CreateMessagingPipe(
123 mojo::embedder::ScopedPlatformHandle handle);
124 void InitMessageReader(mojo::ScopedMessagePipeHandle pipe, int32_t peer_pid);
126 Listener* listener() const { return listener_; }
127 void set_peer_pid(base::ProcessId pid) { peer_pid_ = pid; }
129 private:
130 struct ChannelInfoDeleter {
131 void operator()(mojo::embedder::ChannelInfo* ptr) const;
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;
139 void InitDelegate(ChannelMojo::Delegate* delegate);
141 scoped_ptr<MojoBootstrap> bootstrap_;
142 base::WeakPtr<Delegate> delegate_;
143 Mode mode_;
144 Listener* listener_;
145 base::ProcessId peer_pid_;
146 scoped_ptr<mojo::embedder::ChannelInfo,
147 ChannelInfoDeleter> channel_info_;
149 scoped_ptr<internal::MessagePipeReader, ReaderDeleter> message_reader_;
150 ScopedVector<Message> pending_messages_;
152 scoped_ptr<ScopedIPCSupport> ipc_support_;
154 base::WeakPtrFactory<ChannelMojo> weak_factory_;
156 DISALLOW_COPY_AND_ASSIGN(ChannelMojo);
159 } // namespace IPC
161 #endif // IPC_IPC_CHANNEL_MOJO_H_