Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(873)

Side by Side Diff: mojo/public/cpp/bindings/lib/connector.h

Issue 1455063004: Mojo C++ bindings: introduce MultiplexRouter and related classes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/public/cpp/bindings/BUILD.gn ('k') | mojo/public/cpp/bindings/lib/connector.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 ConnectorConfig {
36 // Connector::Accept() is only called from a single thread.
37 SINGLE_THREADED_SEND,
38 // Connector::Accept() is allowed to be called from multiple threads.
39 MULTI_THREADED_SEND
40 };
41
28 // The Connector takes ownership of |message_pipe|. 42 // The Connector takes ownership of |message_pipe|.
29 explicit Connector( 43 Connector(
30 ScopedMessagePipeHandle message_pipe, 44 ScopedMessagePipeHandle message_pipe,
45 ConnectorConfig config,
31 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()); 46 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter());
32 ~Connector() override; 47 ~Connector() override;
33 48
34 // Sets the receiver to handle messages read from the message pipe. The 49 // 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 50 // Connector will read messages from the pipe regardless of whether or not an
36 // incoming receiver has been set. 51 // incoming receiver has been set.
37 void set_incoming_receiver(MessageReceiver* receiver) { 52 void set_incoming_receiver(MessageReceiver* receiver) {
53 DCHECK(thread_checker_.CalledOnValidThread());
38 incoming_receiver_ = receiver; 54 incoming_receiver_ = receiver;
39 } 55 }
40 56
41 // Errors from incoming receivers will force the connector into an error 57 // Errors from incoming receivers will force the connector into an error
42 // state, where no more messages will be processed. This method is used 58 // state, where no more messages will be processed. This method is used
43 // during testing to prevent that from happening. 59 // during testing to prevent that from happening.
44 void set_enforce_errors_from_incoming_receiver(bool enforce) { 60 void set_enforce_errors_from_incoming_receiver(bool enforce) {
61 DCHECK(thread_checker_.CalledOnValidThread());
45 enforce_errors_from_incoming_receiver_ = enforce; 62 enforce_errors_from_incoming_receiver_ = enforce;
46 } 63 }
47 64
48 // Sets the error handler to receive notifications when an error is 65 // 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. 66 // encountered while reading from the pipe or waiting to read from the pipe.
50 void set_connection_error_handler(const Closure& error_handler) { 67 void set_connection_error_handler(const Closure& error_handler) {
68 DCHECK(thread_checker_.CalledOnValidThread());
51 connection_error_handler_ = error_handler; 69 connection_error_handler_ = error_handler;
52 } 70 }
53 71
54 // Returns true if an error was encountered while reading from the pipe or 72 // Returns true if an error was encountered while reading from the pipe or
55 // waiting to read from the pipe. 73 // waiting to read from the pipe.
56 bool encountered_error() const { return error_; } 74 bool encountered_error() const {
75 DCHECK(thread_checker_.CalledOnValidThread());
76 return error_;
77 }
57 78
58 // Closes the pipe. The connector is put into a quiescent state. 79 // Closes the pipe. The connector is put into a quiescent state.
59 // 80 //
60 // Please note that this method shouldn't be called unless it results from an 81 // 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 82 // explicit request of the user of bindings (e.g., the user sets an
62 // InterfacePtr to null or closes a Binding). 83 // InterfacePtr to null or closes a Binding).
63 void CloseMessagePipe(); 84 void CloseMessagePipe();
64 85
65 // Releases the pipe. Connector is put into a quiescent state. 86 // Releases the pipe. Connector is put into a quiescent state.
66 ScopedMessagePipeHandle PassMessagePipe(); 87 ScopedMessagePipeHandle PassMessagePipe();
67 88
68 // Enters the error state. The upper layer may do this for unrecoverable 89 // 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 90 // issues such as invalid messages are received. If a connection error handler
70 // has been set, it will be called asynchronously. 91 // has been set, it will be called asynchronously.
71 // 92 //
72 // It is a no-op if the connector is already in the error state or there isn't 93 // 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 94 // 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 95 // 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 96 // 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 97 // 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. 98 // creates a new message pipe, closes one end and binds to the other.
78 void RaiseError(); 99 void RaiseError();
79 100
80 // Is the connector bound to a MessagePipe handle? 101 // Is the connector bound to a MessagePipe handle?
81 bool is_valid() const { return message_pipe_.is_valid(); } 102 bool is_valid() const {
103 DCHECK(thread_checker_.CalledOnValidThread());
104 return message_pipe_.is_valid();
105 }
82 106
83 // Waits for the next message on the pipe, blocking until one arrives, 107 // 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 108 // |deadline| elapses, or an error happens. Returns |true| if a message has
85 // been delivered, |false| otherwise. 109 // been delivered, |false| otherwise.
86 bool WaitForIncomingMessage(MojoDeadline deadline); 110 bool WaitForIncomingMessage(MojoDeadline deadline);
87 111
88 // See Binding for details of pause/resume. 112 // See Binding for details of pause/resume.
89 void PauseIncomingMethodCallProcessing(); 113 void PauseIncomingMethodCallProcessing();
90 void ResumeIncomingMethodCallProcessing(); 114 void ResumeIncomingMethodCallProcessing();
91 115
92 // MessageReceiver implementation: 116 // MessageReceiver implementation:
93 bool Accept(Message* message) override; 117 bool Accept(Message* message) override;
94 118
95 MessagePipeHandle handle() const { return message_pipe_.get(); } 119 MessagePipeHandle handle() const {
120 DCHECK(thread_checker_.CalledOnValidThread());
121 return message_pipe_.get();
122 }
96 123
97 private: 124 private:
98 static void CallOnHandleReady(void* closure, MojoResult result); 125 static void CallOnHandleReady(void* closure, MojoResult result);
99 void OnHandleReady(MojoResult result); 126 void OnHandleReady(MojoResult result);
100 127
101 void WaitToReadMore(); 128 void WaitToReadMore();
102 129
103 // Returns false if |this| was destroyed during message dispatch. 130 // Returns false if |this| was destroyed during message dispatch.
104 MOJO_WARN_UNUSED_RESULT bool ReadSingleMessage(MojoResult* read_result); 131 MOJO_WARN_UNUSED_RESULT bool ReadSingleMessage(MojoResult* read_result);
105 132
(...skipping 20 matching lines...) Expand all
126 bool drop_writes_; 153 bool drop_writes_;
127 bool enforce_errors_from_incoming_receiver_; 154 bool enforce_errors_from_incoming_receiver_;
128 155
129 bool paused_; 156 bool paused_;
130 157
131 // If non-null, this will be set to true when the Connector is destroyed. We 158 // 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 159 // use this flag to allow for the Connector to be destroyed as a side-effect
133 // of dispatching an incoming message. 160 // of dispatching an incoming message.
134 bool* destroyed_flag_; 161 bool* destroyed_flag_;
135 162
163 // If sending messages is allowed from multiple threads, |lock_| is used to
164 // protect modifications to |message_pipe_| and |drop_writes_|.
165 scoped_ptr<base::Lock> lock_;
166
167 base::ThreadChecker thread_checker_;
168
136 MOJO_DISALLOW_COPY_AND_ASSIGN(Connector); 169 MOJO_DISALLOW_COPY_AND_ASSIGN(Connector);
137 }; 170 };
138 171
139 } // namespace internal 172 } // namespace internal
140 } // namespace mojo 173 } // namespace mojo
141 174
142 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_CONNECTOR_H_ 175 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_CONNECTOR_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/BUILD.gn ('k') | mojo/public/cpp/bindings/lib/connector.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698