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. 4 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_CONNECTOR_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_CONNECTOR_H_ 7 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" 13 14 namespace mojo { 15 class ErrorHandler; 16 17 namespace internal { 18 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. 23 // 24 // NOTE: MessagePipe I/O is non-blocking. 25 // 26 class Connector : public MessageReceiver { 27 public: 28 // The Connector takes ownership of |message_pipe|. 29 explicit Connector( 30 ScopedMessagePipeHandle message_pipe, 31 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()); 32 virtual ~Connector(); 33 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. set_incoming_receiver(MessageReceiver * receiver)37 void set_incoming_receiver(MessageReceiver* receiver) { 38 incoming_receiver_ = receiver; 39 } 40 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. set_enforce_errors_from_incoming_receiver(bool enforce)44 void set_enforce_errors_from_incoming_receiver(bool enforce) { 45 enforce_errors_from_incoming_receiver_ = enforce; 46 } 47 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. set_error_handler(ErrorHandler * error_handler)50 void set_error_handler(ErrorHandler* error_handler) { 51 error_handler_ = error_handler; 52 } 53 54 // Returns true if an error was encountered while reading from the pipe or 55 // waiting to read from the pipe. encountered_error()56 bool encountered_error() const { return error_; } 57 58 // Closes the pipe, triggering the error state. Connector is put into a 59 // quiescent state. 60 void CloseMessagePipe(); 61 62 // Releases the pipe, not triggering the error state. Connector is put into 63 // a quiescent state. 64 ScopedMessagePipeHandle PassMessagePipe(); 65 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| 68 // otherwise. 69 bool WaitForIncomingMessage(); 70 71 // MessageReceiver implementation: 72 virtual bool Accept(Message* message) MOJO_OVERRIDE; 73 74 private: 75 static void CallOnHandleReady(void* closure, MojoResult result); 76 void OnHandleReady(MojoResult result); 77 78 void WaitToReadMore(); 79 80 // Returns false if |this| was destroyed during message dispatch. 81 MOJO_WARN_UNUSED_RESULT bool ReadSingleMessage(MojoResult* read_result); 82 83 // |this| can be destroyed during message dispatch. 84 void ReadAllAvailableMessages(); 85 86 void NotifyError(); 87 88 // Cancels any calls made to |waiter_|. 89 void CancelWait(); 90 91 ErrorHandler* error_handler_; 92 const MojoAsyncWaiter* waiter_; 93 94 ScopedMessagePipeHandle message_pipe_; 95 MessageReceiver* incoming_receiver_; 96 97 MojoAsyncWaitID async_wait_id_; 98 bool error_; 99 bool drop_writes_; 100 bool enforce_errors_from_incoming_receiver_; 101 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_; 106 107 MOJO_DISALLOW_COPY_AND_ASSIGN(Connector); 108 }; 109 110 } // namespace internal 111 } // namespace mojo 112 113 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_CONNECTOR_H_ 114