OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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. |
| 4 |
| 5 #ifndef IPC_IPC_MESSAGE_PIPE_READER_H_ |
| 6 #define IPC_IPC_MESSAGE_PIPE_READER_H_ |
| 7 |
| 8 #include <vector> |
| 9 |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "mojo/public/c/environment/async_waiter.h" |
| 12 #include "mojo/public/cpp/system/core.h" |
| 13 |
| 14 namespace IPC { |
| 15 namespace internal { |
| 16 |
| 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. |
| 21 // |
| 22 // Each client has to: |
| 23 // |
| 24 // * Provide a subclass implemenation of a specific use of a MessagePipe |
| 25 // and implement callbacks. |
| 26 // * SetPipe() to activate the reader. |
| 27 // |
| 28 // MessageReader has to be used in IO thread. It isn't thread-safe. |
| 29 // |
| 30 class MessagePipeReader { |
| 31 public: |
| 32 // Delay the object deletion using the current message loop. |
| 33 // This is intended to used by MessagePipeReader owners. |
| 34 class DelayedDeleter { |
| 35 public: |
| 36 typedef base::DefaultDeleter<MessagePipeReader> DefaultType; |
| 37 |
| 38 static void DeleteNow(MessagePipeReader* ptr) { delete ptr; } |
| 39 |
| 40 DelayedDeleter() {} |
| 41 DelayedDeleter(const DefaultType&) {} |
| 42 DelayedDeleter& operator=(const DefaultType&) { return *this; } |
| 43 |
| 44 void operator()(MessagePipeReader* ptr) const; |
| 45 }; |
| 46 |
| 47 MessagePipeReader(); |
| 48 virtual ~MessagePipeReader(); |
| 49 |
| 50 MojoHandle handle() const { return pipe_.get().value(); } |
| 51 |
| 52 // Returns received bytes. |
| 53 const std::vector<char>& data_buffer() const { |
| 54 return data_buffer_; |
| 55 } |
| 56 |
| 57 // Returns received handles. The lifetime should be taken over by |
| 58 // the client before next OnMessageArrived(). They will leak |
| 59 // otherwise. |
| 60 const std::vector<MojoHandle>& handle_buffer() const { |
| 61 return handle_buffer_; |
| 62 } |
| 63 |
| 64 // The the pipe and participate to current event loop with the pipe. |
| 65 void SetPipe(mojo::ScopedMessagePipeHandle handle); |
| 66 |
| 67 // Close and destroy the MessagePipe. |
| 68 void Close(); |
| 69 // Close the mesage pipe with notifying the client with the error. |
| 70 void CloseWithError(MojoResult error); |
| 71 // Return true if the MessagePipe is alive. |
| 72 bool IsValid() { return pipe_.is_valid(); } |
| 73 |
| 74 // |
| 75 // The client have to implment these callback to get the readiness |
| 76 // event from the reader |
| 77 // |
| 78 virtual void OnMessageArrived() {} |
| 79 virtual void OnPipeClosed() {} |
| 80 virtual void OnPipeError(MojoResult error) {} |
| 81 |
| 82 protected: |
| 83 std::vector<char> data_buffer_; |
| 84 std::vector<MojoHandle> handle_buffer_; |
| 85 |
| 86 private: |
| 87 static void InvokePipeIsReady(void* closure, MojoResult result); |
| 88 |
| 89 MojoResult ReadMessageBytes(); |
| 90 void PipeIsReady(MojoResult wait_result); |
| 91 void StartWaiting(); |
| 92 void StopWaiting(); |
| 93 |
| 94 MojoAsyncWaitID pipe_wait_id_; |
| 95 mojo::ScopedMessagePipeHandle pipe_; |
| 96 }; |
| 97 |
| 98 } // namespace internal |
| 99 } // namespace IPC |
| 100 |
| 101 #endif // IPC_IPC_MESSAGE_PIPE_READER_H_ |
OLD | NEW |