| OLD | NEW |
| (Empty) |
| 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/environment/async_waiter.h> | |
| 9 #include <mojo/result.h> | |
| 10 #include <mojo/system/time.h> | |
| 11 | |
| 12 #include "mojo/public/cpp/bindings/callback.h" | |
| 13 #include "mojo/public/cpp/bindings/message.h" | |
| 14 #include "mojo/public/cpp/environment/environment.h" | |
| 15 #include "mojo/public/cpp/system/macros.h" | |
| 16 #include "mojo/public/cpp/system/message_pipe.h" | |
| 17 | |
| 18 namespace mojo { | |
| 19 namespace internal { | |
| 20 | |
| 21 // The Connector class is responsible for performing read/write operations on a | |
| 22 // MessagePipe. It writes messages it receives through the MessageReceiver | |
| 23 // interface that it subclasses, and it forwards messages it reads through the | |
| 24 // MessageReceiver interface assigned as its incoming receiver. | |
| 25 // | |
| 26 // NOTE: MessagePipe I/O is non-blocking. | |
| 27 // | |
| 28 class Connector : public MessageReceiver { | |
| 29 public: | |
| 30 // The Connector takes ownership of |message_pipe|. | |
| 31 explicit Connector( | |
| 32 ScopedMessagePipeHandle message_pipe, | |
| 33 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()); | |
| 34 ~Connector() override; | |
| 35 | |
| 36 // Sets the receiver to handle messages read from the message pipe. The | |
| 37 // Connector will read messages from the pipe regardless of whether or not an | |
| 38 // incoming receiver has been set. | |
| 39 void set_incoming_receiver(MessageReceiver* receiver) { | |
| 40 incoming_receiver_ = receiver; | |
| 41 } | |
| 42 | |
| 43 // Errors from incoming receivers will force the connector into an error | |
| 44 // state, where no more messages will be processed. This method is used | |
| 45 // during testing to prevent that from happening. | |
| 46 void set_enforce_errors_from_incoming_receiver(bool enforce) { | |
| 47 enforce_errors_from_incoming_receiver_ = enforce; | |
| 48 } | |
| 49 | |
| 50 // Sets the error handler to receive notifications when an error is | |
| 51 // encountered while reading from the pipe or waiting to read from the pipe. | |
| 52 void set_connection_error_handler(const Closure& error_handler) { | |
| 53 connection_error_handler_ = error_handler; | |
| 54 } | |
| 55 | |
| 56 // Returns true if an error was encountered while reading from the pipe or | |
| 57 // waiting to read from the pipe. | |
| 58 bool encountered_error() const { return error_; } | |
| 59 | |
| 60 // Closes the pipe, triggering the error state. Connector is put into a | |
| 61 // quiescent state. | |
| 62 void CloseMessagePipe(); | |
| 63 | |
| 64 // Releases the pipe, not triggering the error state. Connector is put into | |
| 65 // a quiescent state. | |
| 66 ScopedMessagePipeHandle PassMessagePipe(); | |
| 67 | |
| 68 // Is the connector bound to a MessagePipe handle? | |
| 69 bool is_valid() const { return message_pipe_.is_valid(); } | |
| 70 | |
| 71 // Waits for the next message on the pipe, blocking until one arrives, | |
| 72 // |deadline| elapses, or an error happens. Returns |true| if a message has | |
| 73 // been delivered, |false| otherwise. | |
| 74 // When returning |false| closes the message pipe, unless the reason for | |
| 75 // for returning |false| was |MOJO_RESULT_SHOULD_WAIT| or | |
| 76 // |MOJO_RESULT_DEADLINE_EXCEEDED|. | |
| 77 // Use |encountered_error| to see if an error occurred. | |
| 78 bool WaitForIncomingMessage(MojoDeadline deadline); | |
| 79 | |
| 80 // MessageReceiver implementation: | |
| 81 bool Accept(Message* message) override; | |
| 82 | |
| 83 MessagePipeHandle handle() const { return message_pipe_.get(); } | |
| 84 | |
| 85 private: | |
| 86 static void CallOnHandleReady(void* closure, MojoResult result); | |
| 87 void OnHandleReady(MojoResult result); | |
| 88 | |
| 89 void WaitToReadMore(); | |
| 90 | |
| 91 // Returns false if |this| was destroyed during message dispatch. | |
| 92 MOJO_WARN_UNUSED_RESULT bool ReadSingleMessage(MojoResult* read_result); | |
| 93 | |
| 94 // |this| can be destroyed during message dispatch. | |
| 95 void ReadAllAvailableMessages(); | |
| 96 | |
| 97 void NotifyError(); | |
| 98 | |
| 99 // Cancels any calls made to |waiter_|. | |
| 100 void CancelWait(); | |
| 101 | |
| 102 Closure connection_error_handler_; | |
| 103 const MojoAsyncWaiter* waiter_; | |
| 104 | |
| 105 ScopedMessagePipeHandle message_pipe_; | |
| 106 MessageReceiver* incoming_receiver_; | |
| 107 | |
| 108 MojoAsyncWaitID async_wait_id_; | |
| 109 bool error_; | |
| 110 bool drop_writes_; | |
| 111 bool enforce_errors_from_incoming_receiver_; | |
| 112 | |
| 113 // If non-null, this will be set to true when the Connector is destroyed. We | |
| 114 // use this flag to allow for the Connector to be destroyed as a side-effect | |
| 115 // of dispatching an incoming message. | |
| 116 bool* destroyed_flag_; | |
| 117 | |
| 118 MOJO_DISALLOW_COPY_AND_ASSIGN(Connector); | |
| 119 }; | |
| 120 | |
| 121 } // namespace internal | |
| 122 } // namespace mojo | |
| 123 | |
| 124 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_CONNECTOR_H_ | |
| OLD | NEW |