Extension syncing: Introduce a NeedsSync pref
[chromium-blink-merge.git] / ipc / mojo / ipc_message_pipe_reader.h
blob937930eef33b6f6785bb911f365894a31a63e6b2
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 void CloseWithErrorLater(MojoResult error);
83 void CloseWithErrorIfPending();
85 // Return true if the MessagePipe is alive.
86 bool IsValid() { return pipe_.is_valid(); }
88 bool Send(scoped_ptr<Message> message);
89 void ReadMessagesThenWait();
91 private:
92 void OnMessageReceived();
93 void OnPipeClosed();
94 void OnPipeError(MojoResult error);
96 MojoResult ReadMessageBytes();
97 void PipeIsReady(MojoResult wait_result);
98 void ReadAvailableMessages();
100 std::vector<char> data_buffer_;
101 std::vector<MojoHandle> handle_buffer_;
102 mojo::ScopedMessagePipeHandle pipe_;
103 // |delegate_| and |async_waiter_| are null once the message pipe is closed.
104 Delegate* delegate_;
105 scoped_ptr<AsyncHandleWaiter> async_waiter_;
106 MojoResult pending_send_error_;
108 DISALLOW_COPY_AND_ASSIGN(MessagePipeReader);
111 } // namespace internal
112 } // namespace IPC
114 #endif // IPC_IPC_MESSAGE_PIPE_READER_H_