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_CPP_BINDINGS_LIB_CONNECTOR_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_CONNECTOR_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_CONNECTOR_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_CONNECTOR_H_ |
7 | 7 |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/threading/thread_checker.h" |
8 #include "mojo/public/c/environment/async_waiter.h" | 10 #include "mojo/public/c/environment/async_waiter.h" |
9 #include "mojo/public/cpp/bindings/callback.h" | 11 #include "mojo/public/cpp/bindings/callback.h" |
10 #include "mojo/public/cpp/bindings/message.h" | 12 #include "mojo/public/cpp/bindings/message.h" |
11 #include "mojo/public/cpp/environment/environment.h" | 13 #include "mojo/public/cpp/environment/environment.h" |
12 #include "mojo/public/cpp/system/core.h" | 14 #include "mojo/public/cpp/system/core.h" |
13 | 15 |
| 16 namespace base { |
| 17 class Lock; |
| 18 } |
| 19 |
14 namespace mojo { | 20 namespace mojo { |
15 class ErrorHandler; | |
16 | |
17 namespace internal { | 21 namespace internal { |
18 | 22 |
19 // The Connector class is responsible for performing read/write operations on a | 23 // The Connector class is responsible for performing read/write operations on a |
20 // MessagePipe. It writes messages it receives through the MessageReceiver | 24 // MessagePipe. It writes messages it receives through the MessageReceiver |
21 // interface that it subclasses, and it forwards messages it reads through the | 25 // interface that it subclasses, and it forwards messages it reads through the |
22 // MessageReceiver interface assigned as its incoming receiver. | 26 // MessageReceiver interface assigned as its incoming receiver. |
23 // | 27 // |
24 // NOTE: MessagePipe I/O is non-blocking. | 28 // NOTE: |
25 // | 29 // - MessagePipe I/O is non-blocking. |
| 30 // - Sending messages can be configured to be thread safe (please see comments |
| 31 // of the constructor). Other than that, the object should only be accessed |
| 32 // on the creating thread. |
26 class Connector : public MessageReceiver { | 33 class Connector : public MessageReceiver { |
27 public: | 34 public: |
| 35 enum SendingThreadSafetyType { SINGLE_THREADED_SEND, MULTI_THREADED_SEND }; |
| 36 |
28 // The Connector takes ownership of |message_pipe|. | 37 // The Connector takes ownership of |message_pipe|. |
29 explicit Connector( | 38 // If |sending_thread_safety_type| is set to MULTI_THREADED_SEND, Accept() is |
| 39 // safe to call from multiple threads. |
| 40 Connector( |
30 ScopedMessagePipeHandle message_pipe, | 41 ScopedMessagePipeHandle message_pipe, |
| 42 SendingThreadSafetyType sending_thread_safety_type, |
31 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()); | 43 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()); |
32 ~Connector() override; | 44 ~Connector() override; |
33 | 45 |
34 // Sets the receiver to handle messages read from the message pipe. The | 46 // 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 | 47 // Connector will read messages from the pipe regardless of whether or not an |
36 // incoming receiver has been set. | 48 // incoming receiver has been set. |
37 void set_incoming_receiver(MessageReceiver* receiver) { | 49 void set_incoming_receiver(MessageReceiver* receiver) { |
| 50 DCHECK(thread_checker_.CalledOnValidThread()); |
38 incoming_receiver_ = receiver; | 51 incoming_receiver_ = receiver; |
39 } | 52 } |
40 | 53 |
41 // Errors from incoming receivers will force the connector into an error | 54 // Errors from incoming receivers will force the connector into an error |
42 // state, where no more messages will be processed. This method is used | 55 // state, where no more messages will be processed. This method is used |
43 // during testing to prevent that from happening. | 56 // during testing to prevent that from happening. |
44 void set_enforce_errors_from_incoming_receiver(bool enforce) { | 57 void set_enforce_errors_from_incoming_receiver(bool enforce) { |
| 58 DCHECK(thread_checker_.CalledOnValidThread()); |
45 enforce_errors_from_incoming_receiver_ = enforce; | 59 enforce_errors_from_incoming_receiver_ = enforce; |
46 } | 60 } |
47 | 61 |
48 // Sets the error handler to receive notifications when an error is | 62 // 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. | 63 // encountered while reading from the pipe or waiting to read from the pipe. |
50 void set_connection_error_handler(const Closure& error_handler) { | 64 void set_connection_error_handler(const Closure& error_handler) { |
| 65 DCHECK(thread_checker_.CalledOnValidThread()); |
51 connection_error_handler_ = error_handler; | 66 connection_error_handler_ = error_handler; |
52 } | 67 } |
53 | 68 |
54 // Returns true if an error was encountered while reading from the pipe or | 69 // Returns true if an error was encountered while reading from the pipe or |
55 // waiting to read from the pipe. | 70 // waiting to read from the pipe. |
56 bool encountered_error() const { return error_; } | 71 bool encountered_error() const { |
| 72 DCHECK(thread_checker_.CalledOnValidThread()); |
| 73 return error_; |
| 74 } |
57 | 75 |
58 // Closes the pipe. The connector is put into a quiescent state. | 76 // Closes the pipe. The connector is put into a quiescent state. |
59 // | 77 // |
60 // Please note that this method shouldn't be called unless it results from an | 78 // Please note that this method shouldn't be called unless it results from an |
61 // explicit request of the user of bindings (e.g., the user sets an | 79 // explicit request of the user of bindings (e.g., the user sets an |
62 // InterfacePtr to null or closes a Binding). | 80 // InterfacePtr to null or closes a Binding). |
63 void CloseMessagePipe(); | 81 void CloseMessagePipe(); |
64 | 82 |
65 // Releases the pipe. Connector is put into a quiescent state. | 83 // Releases the pipe. Connector is put into a quiescent state. |
66 ScopedMessagePipeHandle PassMessagePipe(); | 84 ScopedMessagePipeHandle PassMessagePipe(); |
67 | 85 |
68 // Enters the error state. The upper layer may do this for unrecoverable | 86 // Enters the error state. The upper layer may do this for unrecoverable |
69 // issues such as invalid messages are received. If a connection error handler | 87 // issues such as invalid messages are received. If a connection error handler |
70 // has been set, it will be called asynchronously. | 88 // has been set, it will be called asynchronously. |
71 // | 89 // |
72 // It is a no-op if the connector is already in the error state or there isn't | 90 // It is a no-op if the connector is already in the error state or there isn't |
73 // a bound message pipe. Otherwise, it closes the message pipe, which notifies | 91 // a bound message pipe. Otherwise, it closes the message pipe, which notifies |
74 // the other end and also prevents potential danger (say, the caller raises | 92 // the other end and also prevents potential danger (say, the caller raises |
75 // an error because it believes the other end is malicious). In order to | 93 // an error because it believes the other end is malicious). In order to |
76 // appear to the user that the connector still binds to a message pipe, it | 94 // appear to the user that the connector still binds to a message pipe, it |
77 // creates a new message pipe, closes one end and binds to the other. | 95 // creates a new message pipe, closes one end and binds to the other. |
78 void RaiseError(); | 96 void RaiseError(); |
79 | 97 |
80 // Is the connector bound to a MessagePipe handle? | 98 // Is the connector bound to a MessagePipe handle? |
81 bool is_valid() const { return message_pipe_.is_valid(); } | 99 bool is_valid() const { |
| 100 DCHECK(thread_checker_.CalledOnValidThread()); |
| 101 return message_pipe_.is_valid(); |
| 102 } |
82 | 103 |
83 // Waits for the next message on the pipe, blocking until one arrives, | 104 // Waits for the next message on the pipe, blocking until one arrives, |
84 // |deadline| elapses, or an error happens. Returns |true| if a message has | 105 // |deadline| elapses, or an error happens. Returns |true| if a message has |
85 // been delivered, |false| otherwise. | 106 // been delivered, |false| otherwise. |
86 bool WaitForIncomingMessage(MojoDeadline deadline); | 107 bool WaitForIncomingMessage(MojoDeadline deadline); |
87 | 108 |
88 // See Binding for details of pause/resume. | 109 // See Binding for details of pause/resume. |
89 void PauseIncomingMethodCallProcessing(); | 110 void PauseIncomingMethodCallProcessing(); |
90 void ResumeIncomingMethodCallProcessing(); | 111 void ResumeIncomingMethodCallProcessing(); |
91 | 112 |
92 // MessageReceiver implementation: | 113 // MessageReceiver implementation: |
93 bool Accept(Message* message) override; | 114 bool Accept(Message* message) override; |
94 | 115 |
95 MessagePipeHandle handle() const { return message_pipe_.get(); } | 116 MessagePipeHandle handle() const { |
| 117 DCHECK(thread_checker_.CalledOnValidThread()); |
| 118 return message_pipe_.get(); |
| 119 } |
96 | 120 |
97 private: | 121 private: |
98 static void CallOnHandleReady(void* closure, MojoResult result); | 122 static void CallOnHandleReady(void* closure, MojoResult result); |
99 void OnHandleReady(MojoResult result); | 123 void OnHandleReady(MojoResult result); |
100 | 124 |
101 void WaitToReadMore(); | 125 void WaitToReadMore(); |
102 | 126 |
103 // Returns false if |this| was destroyed during message dispatch. | 127 // Returns false if |this| was destroyed during message dispatch. |
104 MOJO_WARN_UNUSED_RESULT bool ReadSingleMessage(MojoResult* read_result); | 128 MOJO_WARN_UNUSED_RESULT bool ReadSingleMessage(MojoResult* read_result); |
105 | 129 |
(...skipping 20 matching lines...) Expand all Loading... |
126 bool drop_writes_; | 150 bool drop_writes_; |
127 bool enforce_errors_from_incoming_receiver_; | 151 bool enforce_errors_from_incoming_receiver_; |
128 | 152 |
129 bool paused_; | 153 bool paused_; |
130 | 154 |
131 // If non-null, this will be set to true when the Connector is destroyed. We | 155 // If non-null, this will be set to true when the Connector is destroyed. We |
132 // use this flag to allow for the Connector to be destroyed as a side-effect | 156 // use this flag to allow for the Connector to be destroyed as a side-effect |
133 // of dispatching an incoming message. | 157 // of dispatching an incoming message. |
134 bool* destroyed_flag_; | 158 bool* destroyed_flag_; |
135 | 159 |
| 160 // If sending messages is allowed from multiple threads, |lock_| is used to |
| 161 // protect modifications to |message_pipe_| and |drop_writes_|. |
| 162 scoped_ptr<base::Lock> lock_; |
| 163 |
| 164 base::ThreadChecker thread_checker_; |
| 165 |
136 MOJO_DISALLOW_COPY_AND_ASSIGN(Connector); | 166 MOJO_DISALLOW_COPY_AND_ASSIGN(Connector); |
137 }; | 167 }; |
138 | 168 |
139 } // namespace internal | 169 } // namespace internal |
140 } // namespace mojo | 170 } // namespace mojo |
141 | 171 |
142 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_CONNECTOR_H_ | 172 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_CONNECTOR_H_ |
OLD | NEW |