OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 MOJO_PUBLIC_BINDINGS_LIB_CONNECTOR_H_ | 5 #ifndef MOJO_PUBLIC_BINDINGS_LIB_CONNECTOR_H_ |
6 #define MOJO_PUBLIC_BINDINGS_LIB_CONNECTOR_H_ | 6 #define MOJO_PUBLIC_BINDINGS_LIB_CONNECTOR_H_ |
7 | 7 |
8 #include "mojo/public/bindings/lib/message.h" | 8 #include "mojo/public/bindings/lib/message.h" |
9 #include "mojo/public/bindings/lib/message_queue.h" | 9 #include "mojo/public/bindings/lib/message_queue.h" |
10 #include "mojo/public/environment/default_async_waiter.h" | 10 #include "mojo/public/environment/default_async_waiter.h" |
11 #include "mojo/public/system/core_cpp.h" | 11 #include "mojo/public/system/core_cpp.h" |
12 | 12 |
13 namespace mojo { | 13 namespace mojo { |
| 14 class ErrorHandler; |
| 15 |
14 namespace internal { | 16 namespace internal { |
15 | 17 |
16 // The Connector class is responsible for performing read/write operations on a | 18 // The Connector class is responsible for performing read/write operations on a |
17 // MessagePipe. It writes messages it receives through the MessageReceiver | 19 // MessagePipe. It writes messages it receives through the MessageReceiver |
18 // interface that it subclasses, and it forwards messages it reads through the | 20 // interface that it subclasses, and it forwards messages it reads through the |
19 // MessageReceiver interface assigned as its incoming receiver. | 21 // MessageReceiver interface assigned as its incoming receiver. |
20 // | 22 // |
21 // NOTE: MessagePipe I/O is non-blocking. | 23 // NOTE: MessagePipe I/O is non-blocking. |
22 // | 24 // |
23 class Connector : public MessageReceiver { | 25 class Connector : public MessageReceiver { |
24 public: | 26 public: |
25 // The Connector takes ownership of |message_pipe|. | 27 // The Connector takes ownership of |message_pipe|. |
26 explicit Connector(ScopedMessagePipeHandle message_pipe, | 28 explicit Connector(ScopedMessagePipeHandle message_pipe, |
27 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter()); | 29 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter()); |
28 virtual ~Connector(); | 30 virtual ~Connector(); |
29 | 31 |
30 // Sets the receiver to handle messages read from the message pipe. The | 32 // Sets the receiver to handle messages read from the message pipe. The |
31 // Connector will only read messages from the pipe if an incoming receiver | 33 // Connector will only read messages from the pipe if an incoming receiver |
32 // has been set. | 34 // has been set. |
33 void SetIncomingReceiver(MessageReceiver* receiver); | 35 void set_incoming_receiver(MessageReceiver* receiver) { |
| 36 incoming_receiver_ = receiver; |
| 37 } |
| 38 |
| 39 // Sets the error handler to receives notifications when an error is |
| 40 // encountered while reading from the pipe or waiting to read from the pipe. |
| 41 void set_error_handler(ErrorHandler* error_handler) { |
| 42 error_handler_ = error_handler; |
| 43 } |
34 | 44 |
35 // Returns true if an error was encountered while reading from or writing to | 45 // Returns true if an error was encountered while reading from or writing to |
36 // the message pipe. | 46 // the message pipe. |
37 bool encountered_error() const { return error_; } | 47 bool encountered_error() const { return error_; } |
38 | 48 |
39 // MessageReceiver implementation: | 49 // MessageReceiver implementation: |
40 virtual bool Accept(Message* message) MOJO_OVERRIDE; | 50 virtual bool Accept(Message* message) MOJO_OVERRIDE; |
41 | 51 |
42 private: | 52 private: |
43 static void OnHandleReady(void* closure, MojoResult result); | 53 static void CallOnHandleReady(void* closure, MojoResult result); |
| 54 void OnHandleReady(MojoResult result); |
44 | 55 |
45 void WaitToReadMore(); | 56 void WaitToReadMore(); |
46 void ReadMore(); | 57 void ReadMore(); |
47 void WriteOne(Message* message); | 58 void WriteOne(Message* message); |
48 | 59 |
| 60 ErrorHandler* error_handler_; |
49 MojoAsyncWaiter* waiter_; | 61 MojoAsyncWaiter* waiter_; |
50 | 62 |
51 ScopedMessagePipeHandle message_pipe_; | 63 ScopedMessagePipeHandle message_pipe_; |
52 MessageReceiver* incoming_receiver_; | 64 MessageReceiver* incoming_receiver_; |
53 | 65 |
54 MojoAsyncWaitID async_wait_id_; | 66 MojoAsyncWaitID async_wait_id_; |
55 bool error_; | 67 bool error_; |
56 | 68 |
57 MOJO_DISALLOW_COPY_AND_ASSIGN(Connector); | 69 MOJO_DISALLOW_COPY_AND_ASSIGN(Connector); |
58 }; | 70 }; |
59 | 71 |
60 } // namespace internal | 72 } // namespace internal |
61 } // namespace mojo | 73 } // namespace mojo |
62 | 74 |
63 #endif // MOJO_PUBLIC_BINDINGS_LIB_CONNECTOR_H_ | 75 #endif // MOJO_PUBLIC_BINDINGS_LIB_CONNECTOR_H_ |
OLD | NEW |