| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef IPC_IPC_MESSAGE_PIPE_READER_H_ | 5 #ifndef IPC_IPC_MESSAGE_PIPE_READER_H_ |
| 6 #define IPC_IPC_MESSAGE_PIPE_READER_H_ | 6 #define IPC_IPC_MESSAGE_PIPE_READER_H_ |
| 7 | 7 |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "ipc/ipc_message.h" | 12 #include "ipc/ipc_message.h" |
| 13 #include "ipc/mojo/async_handle_waiter.h" |
| 13 #include "third_party/mojo/src/mojo/public/c/environment/async_waiter.h" | 14 #include "third_party/mojo/src/mojo/public/c/environment/async_waiter.h" |
| 14 #include "third_party/mojo/src/mojo/public/cpp/system/core.h" | 15 #include "third_party/mojo/src/mojo/public/cpp/system/core.h" |
| 15 | 16 |
| 16 namespace IPC { | 17 namespace IPC { |
| 17 namespace internal { | 18 namespace internal { |
| 18 | 19 |
| 19 class AsyncHandleWaiter; | |
| 20 | |
| 21 // A helper class to handle bytestream directly over mojo::MessagePipe | 20 // A helper class to handle bytestream directly over mojo::MessagePipe |
| 22 // in template-method pattern. MessagePipeReader manages the lifetime | 21 // in template-method pattern. MessagePipeReader manages the lifetime |
| 23 // of given MessagePipe and participates the event loop, and | 22 // of given MessagePipe and participates the event loop, and |
| 24 // read the stream and call the client when it is ready. | 23 // read the stream and call the client when it is ready. |
| 25 // | 24 // |
| 26 // Each client has to: | 25 // Each client has to: |
| 27 // | 26 // |
| 28 // * Provide a subclass implemenation of a specific use of a MessagePipe | 27 // * Provide a subclass implemenation of a specific use of a MessagePipe |
| 29 // and implement callbacks. | 28 // and implement callbacks. |
| 30 // * Create the subclass instance with a MessagePipeHandle. | 29 // * Create the subclass instance with a MessagePipeHandle. |
| 31 // The constructor automatically start listening on the pipe. | 30 // The constructor automatically start listening on the pipe. |
| 32 // | 31 // |
| 33 // MessageReader has to be used in IO thread. It isn't thread-safe. | 32 // MessageReader has to be used in IO thread. It isn't thread-safe. |
| 34 // | 33 // |
| 35 class MessagePipeReader { | 34 class MessagePipeReader : public AsyncHandleWaiter::Delegate { |
| 36 public: | 35 public: |
| 37 class Delegate { | 36 class Delegate { |
| 38 public: | 37 public: |
| 39 virtual void OnMessageReceived(Message& message) = 0; | 38 virtual void OnMessageReceived(Message& message) = 0; |
| 40 virtual void OnPipeClosed(MessagePipeReader* reader) = 0; | 39 virtual void OnPipeClosed(MessagePipeReader* reader) = 0; |
| 41 virtual void OnPipeError(MessagePipeReader* reader) = 0; | 40 virtual void OnPipeError(MessagePipeReader* reader) = 0; |
| 42 }; | 41 }; |
| 43 | 42 |
| 44 // Delay the object deletion using the current message loop. | 43 // Delay the object deletion using the current message loop. |
| 45 // This is intended to used by MessagePipeReader owners. | 44 // This is intended to used by MessagePipeReader owners. |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 bool IsValid() { return pipe_.is_valid(); } | 82 bool IsValid() { return pipe_.is_valid(); } |
| 84 | 83 |
| 85 bool Send(scoped_ptr<Message> message); | 84 bool Send(scoped_ptr<Message> message); |
| 86 void ReadMessagesThenWait(); | 85 void ReadMessagesThenWait(); |
| 87 | 86 |
| 88 private: | 87 private: |
| 89 void OnMessageReceived(); | 88 void OnMessageReceived(); |
| 90 void OnPipeClosed(); | 89 void OnPipeClosed(); |
| 91 void OnPipeError(MojoResult error); | 90 void OnPipeError(MojoResult error); |
| 92 | 91 |
| 92 // AsyncHandleWaiter::Delegate implementations |
| 93 void PipeIsReady(MojoResult wait_result) override; |
| 94 void MessageWasArrived(const void* bytes, uint32_t num_bytes) override; |
| 95 |
| 93 MojoResult ReadMessageBytes(); | 96 MojoResult ReadMessageBytes(); |
| 94 void PipeIsReady(MojoResult wait_result); | |
| 95 void ReadAvailableMessages(); | 97 void ReadAvailableMessages(); |
| 96 | 98 |
| 97 std::vector<char> data_buffer_; | 99 std::vector<char> data_buffer_; |
| 98 std::vector<MojoHandle> handle_buffer_; | 100 std::vector<MojoHandle> handle_buffer_; |
| 99 mojo::ScopedMessagePipeHandle pipe_; | 101 mojo::ScopedMessagePipeHandle pipe_; |
| 100 // |delegate_| and |async_waiter_| are null once the message pipe is closed. | 102 // |delegate_| and |async_waiter_| are null once the message pipe is closed. |
| 101 Delegate* delegate_; | 103 Delegate* delegate_; |
| 102 scoped_ptr<AsyncHandleWaiter> async_waiter_; | 104 scoped_ptr<AsyncHandleWaiter> async_waiter_; |
| 103 | 105 |
| 104 DISALLOW_COPY_AND_ASSIGN(MessagePipeReader); | 106 DISALLOW_COPY_AND_ASSIGN(MessagePipeReader); |
| 105 }; | 107 }; |
| 106 | 108 |
| 107 } // namespace internal | 109 } // namespace internal |
| 108 } // namespace IPC | 110 } // namespace IPC |
| 109 | 111 |
| 110 #endif // IPC_IPC_MESSAGE_PIPE_READER_H_ | 112 #endif // IPC_IPC_MESSAGE_PIPE_READER_H_ |
| OLD | NEW |