Revert of Output closure-compiled JavaScript files (patchset #10 id:180001 of https...
[chromium-blink-merge.git] / ipc / mojo / ipc_message_pipe_reader.h
blob74de71b80ba87ef9f02d159409c6e8efd70bc5c1
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_
8 #include <vector>
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"
16 namespace IPC {
17 namespace internal {
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 {
36 public:
37 class Delegate {
38 public:
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 {
47 public:
48 typedef base::DefaultDeleter<MessagePipeReader> DefaultType;
50 static void DeleteNow(MessagePipeReader* ptr) { delete ptr; }
52 DelayedDeleter() {}
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 {
69 return data_buffer_;
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.
79 void Close();
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();
88 private:
89 void OnMessageReceived();
90 void OnPipeClosed();
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.
101 Delegate* delegate_;
102 scoped_ptr<AsyncHandleWaiter> async_waiter_;
104 DISALLOW_COPY_AND_ASSIGN(MessagePipeReader);
107 } // namespace internal
108 } // namespace IPC
110 #endif // IPC_IPC_MESSAGE_PIPE_READER_H_