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
3 // found in the LICENSE file.
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_CONNECTOR_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_CONNECTOR_H_
8 #include "mojo/public/c/environment/async_waiter.h"
9 #include "mojo/public/cpp/bindings/lib/message_queue.h"
10 #include "mojo/public/cpp/bindings/message.h"
11 #include "mojo/public/cpp/environment/environment.h"
12 #include "mojo/public/cpp/system/core.h"
19 // The Connector class is responsible for performing read/write operations on a
20 // MessagePipe. It writes messages it receives through the MessageReceiver
21 // interface that it subclasses, and it forwards messages it reads through the
22 // MessageReceiver interface assigned as its incoming receiver.
24 // NOTE: MessagePipe I/O is non-blocking.
26 class Connector
: public MessageReceiver
{
28 // The Connector takes ownership of |message_pipe|.
30 ScopedMessagePipeHandle message_pipe
,
31 const MojoAsyncWaiter
* waiter
= Environment::GetDefaultAsyncWaiter());
34 // Sets the receiver to handle messages read from the message pipe. The
35 // Connector will read messages from the pipe regardless of whether or not an
36 // incoming receiver has been set.
37 void set_incoming_receiver(MessageReceiver
* receiver
) {
38 incoming_receiver_
= receiver
;
41 // Errors from incoming receivers will force the connector into an error
42 // state, where no more messages will be processed. This method is used
43 // during testing to prevent that from happening.
44 void set_enforce_errors_from_incoming_receiver(bool enforce
) {
45 enforce_errors_from_incoming_receiver_
= enforce
;
48 // Sets the error handler to receive notifications when an error is
49 // encountered while reading from the pipe or waiting to read from the pipe.
50 void set_error_handler(ErrorHandler
* error_handler
) {
51 error_handler_
= error_handler
;
54 // Returns true if an error was encountered while reading from the pipe or
55 // waiting to read from the pipe.
56 bool encountered_error() const { return error_
; }
58 // Closes the pipe, triggering the error state. Connector is put into a
60 void CloseMessagePipe();
62 // Releases the pipe, not triggering the error state. Connector is put into
64 ScopedMessagePipeHandle
PassMessagePipe();
66 // Waits for the next message on the pipe, blocking until one arrives or an
67 // error happens. Returns |true| if a message has been delivered, |false|
69 bool WaitForIncomingMessage();
71 // MessageReceiver implementation:
72 virtual bool Accept(Message
* message
) MOJO_OVERRIDE
;
75 static void CallOnHandleReady(void* closure
, MojoResult result
);
76 void OnHandleReady(MojoResult result
);
78 void WaitToReadMore();
80 // Returns false if |this| was destroyed during message dispatch.
81 MOJO_WARN_UNUSED_RESULT
bool ReadSingleMessage(MojoResult
* read_result
);
83 // |this| can be destroyed during message dispatch.
84 void ReadAllAvailableMessages();
88 // Cancels any calls made to |waiter_|.
91 ErrorHandler
* error_handler_
;
92 const MojoAsyncWaiter
* waiter_
;
94 ScopedMessagePipeHandle message_pipe_
;
95 MessageReceiver
* incoming_receiver_
;
97 MojoAsyncWaitID async_wait_id_
;
100 bool enforce_errors_from_incoming_receiver_
;
102 // If non-null, this will be set to true when the Connector is destroyed. We
103 // use this flag to allow for the Connector to be destroyed as a side-effect
104 // of dispatching an incoming message.
105 bool* destroyed_flag_
;
107 MOJO_DISALLOW_COPY_AND_ASSIGN(Connector
);
110 } // namespace internal
113 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_CONNECTOR_H_