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/memory/scoped_ptr.h"
11 #include "mojo/public/c/environment/async_waiter.h"
12 #include "mojo/public/cpp/system/core.h"
17 // A helper class to handle bytestream directly over mojo::MessagePipe
18 // in template-method pattern. MessagePipeReader manages the lifetime
19 // of given MessagePipe and participates the event loop, and
20 // read the stream and call the client when it is ready.
22 // Each client has to:
24 // * Provide a subclass implemenation of a specific use of a MessagePipe
25 // and implement callbacks.
26 // * Create the subclass instance with a MessagePipeHandle.
27 // The constructor automatically start listening on the pipe.
29 // MessageReader has to be used in IO thread. It isn't thread-safe.
31 class MessagePipeReader
{
33 // Delay the object deletion using the current message loop.
34 // This is intended to used by MessagePipeReader owners.
35 class DelayedDeleter
{
37 typedef base::DefaultDeleter
<MessagePipeReader
> DefaultType
;
39 static void DeleteNow(MessagePipeReader
* ptr
) { delete ptr
; }
42 DelayedDeleter(const DefaultType
&) {}
43 DelayedDeleter
& operator=(const DefaultType
&) { return *this; }
45 void operator()(MessagePipeReader
* ptr
) const;
48 explicit MessagePipeReader(mojo::ScopedMessagePipeHandle handle
);
49 virtual ~MessagePipeReader();
51 MojoHandle
handle() const { return pipe_
.get().value(); }
53 // Returns received bytes.
54 const std::vector
<char>& data_buffer() const {
58 // Delegate received handles ownership. The subclass should take the
59 // ownership over in its OnMessageReceived(). They will leak otherwise.
60 void TakeHandleBuffer(std::vector
<MojoHandle
>* handle_buffer
) {
61 handle_buffer_
.swap(*handle_buffer
);
64 // Close and destroy the MessagePipe.
66 // Close the mesage pipe with notifying the client with the error.
67 void CloseWithError(MojoResult error
);
68 // Return true if the MessagePipe is alive.
69 bool IsValid() { return pipe_
.is_valid(); }
72 // The client have to implment these callback to get the readiness
73 // event from the reader
75 virtual void OnMessageReceived() = 0;
76 virtual void OnPipeClosed() = 0;
77 virtual void OnPipeError(MojoResult error
) = 0;
80 static void InvokePipeIsReady(void* closure
, MojoResult result
);
82 MojoResult
ReadMessageBytes();
83 void PipeIsReady(MojoResult wait_result
);
87 std::vector
<char> data_buffer_
;
88 std::vector
<MojoHandle
> handle_buffer_
;
89 MojoAsyncWaitID pipe_wait_id_
;
90 mojo::ScopedMessagePipeHandle pipe_
;
92 DISALLOW_COPY_AND_ASSIGN(MessagePipeReader
);
95 } // namespace internal
98 #endif // IPC_IPC_MESSAGE_PIPE_READER_H_