• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   // MessageReceiver implementation:
67   virtual bool Accept(Message* message) MOJO_OVERRIDE;
68 
69  private:
70   static void CallOnHandleReady(void* closure, MojoResult result);
71   void OnHandleReady(MojoResult result);
72 
73   void WaitToReadMore();
74 
75   // Returns false if |this| was destroyed during message dispatch.
76   MOJO_WARN_UNUSED_RESULT bool ReadMore();
77 
78   ErrorHandler* error_handler_;
79   const MojoAsyncWaiter* waiter_;
80 
81   ScopedMessagePipeHandle message_pipe_;
82   MessageReceiver* incoming_receiver_;
83 
84   MojoAsyncWaitID async_wait_id_;
85   bool error_;
86   bool drop_writes_;
87   bool enforce_errors_from_incoming_receiver_;
88 
89   // If non-null, this will be set to true when the Connector is destroyed.  We
90   // use this flag to allow for the Connector to be destroyed as a side-effect
91   // of dispatching an incoming message.
92   bool* destroyed_flag_;
93 
94   MOJO_DISALLOW_COPY_AND_ASSIGN(Connector);
95 };
96 
97 }  // namespace internal
98 }  // namespace mojo
99 
100 #endif  // MOJO_PUBLIC_CPP_BINDINGS_LIB_CONNECTOR_H_
101