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_MESSAGE_PIPE_READER_H_
6 #define IPC_IPC_MESSAGE_PIPE_READER_H_
10 #include "base/compiler_specific.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "ipc/ipc_message.h"
13 #include "third_party/mojo/src/mojo/public/c/environment/async_waiter.h"
14 #include "third_party/mojo/src/mojo/public/cpp/system/core.h"
19 class AsyncHandleWaiter
;
21 // A helper class to handle bytestream directly over mojo::MessagePipe
22 // in template-method pattern. MessagePipeReader manages the lifetime
23 // of given MessagePipe and participates the event loop, and
24 // read the stream and call the client when it is ready.
26 // Each client has to:
28 // * Provide a subclass implemenation of a specific use of a MessagePipe
29 // and implement callbacks.
30 // * Create the subclass instance with a MessagePipeHandle.
31 // The constructor automatically start listening on the pipe.
33 // MessageReader has to be used in IO thread. It isn't thread-safe.
35 class MessagePipeReader
{
39 virtual void OnMessageReceived(Message
& message
) = 0;
40 virtual void OnPipeClosed(MessagePipeReader
* reader
) = 0;
41 virtual void OnPipeError(MessagePipeReader
* reader
) = 0;
44 // Delay the object deletion using the current message loop.
45 // This is intended to used by MessagePipeReader owners.
46 class DelayedDeleter
{
48 typedef base::DefaultDeleter
<MessagePipeReader
> DefaultType
;
50 static void DeleteNow(MessagePipeReader
* ptr
) { delete ptr
; }
53 explicit DelayedDeleter(const DefaultType
&) {}
54 DelayedDeleter
& operator=(const DefaultType
&) { return *this; }
56 void operator()(MessagePipeReader
* ptr
) const;
59 // Both parameters must be non-null.
60 // Build a reader that reads messages from |handle| and lets |delegate| know.
61 // Note that MessagePipeReader doesn't delete |delete|.
62 MessagePipeReader(mojo::ScopedMessagePipeHandle handle
, Delegate
* delegate
);
63 virtual ~MessagePipeReader();
65 MojoHandle
handle() const { return pipe_
.get().value(); }
67 // Returns received bytes.
68 const std::vector
<char>& data_buffer() const {
72 // Delegate received handles ownership. The subclass should take the
73 // ownership over in its OnMessageReceived(). They will leak otherwise.
74 void TakeHandleBuffer(std::vector
<MojoHandle
>* handle_buffer
) {
75 handle_buffer_
.swap(*handle_buffer
);
78 // Close and destroy the MessagePipe.
80 // Close the mesage pipe with notifying the client with the error.
81 void CloseWithError(MojoResult error
);
82 // Return true if the MessagePipe is alive.
83 bool IsValid() { return pipe_
.is_valid(); }
85 bool Send(scoped_ptr
<Message
> message
);
86 void ReadMessagesThenWait();
89 void OnMessageReceived();
91 void OnPipeError(MojoResult error
);
93 MojoResult
ReadMessageBytes();
94 void PipeIsReady(MojoResult wait_result
);
95 void ReadAvailableMessages();
97 std::vector
<char> data_buffer_
;
98 std::vector
<MojoHandle
> handle_buffer_
;
99 mojo::ScopedMessagePipeHandle pipe_
;
100 // |delegate_| and |async_waiter_| are null once the message pipe is closed.
102 scoped_ptr
<AsyncHandleWaiter
> async_waiter_
;
104 DISALLOW_COPY_AND_ASSIGN(MessagePipeReader
);
107 } // namespace internal
110 #endif // IPC_IPC_MESSAGE_PIPE_READER_H_