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_EDK_SYSTEM_CHANNEL_H_ | 5 #ifndef MOJO_EDK_SYSTEM_CHANNEL_H_ |
6 #define MOJO_EDK_SYSTEM_CHANNEL_H_ | 6 #define MOJO_EDK_SYSTEM_CHANNEL_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include "base/containers/hash_tables.h" | 10 #include "base/containers/hash_tables.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 #include "mojo/public/c/system/types.h" | 24 #include "mojo/public/c/system/types.h" |
25 | 25 |
26 namespace mojo { | 26 namespace mojo { |
27 | 27 |
28 namespace embedder { | 28 namespace embedder { |
29 class PlatformSupport; | 29 class PlatformSupport; |
30 } | 30 } |
31 | 31 |
32 namespace system { | 32 namespace system { |
33 | 33 |
34 class ChannelEndpoint; | 34 class ChannelEndpointClient; |
35 class ChannelManager; | 35 class ChannelManager; |
| 36 class MessageInTransitQueue; |
36 | 37 |
37 // This class is mostly thread-safe. It must be created on an I/O thread. | 38 // This class is mostly thread-safe. It must be created on an I/O thread. |
38 // |Init()| must be called on that same thread before it becomes thread-safe (in | 39 // |Init()| must be called on that same thread before it becomes thread-safe (in |
39 // particular, before references are given to any other thread) and |Shutdown()| | 40 // particular, before references are given to any other thread) and |Shutdown()| |
40 // must be called on that same thread before destruction. Its public methods are | 41 // must be called on that same thread before destruction. Its public methods are |
41 // otherwise thread-safe. (Many private methods are restricted to the creation | 42 // otherwise thread-safe. (Many private methods are restricted to the creation |
42 // thread.) It may be destroyed on any thread, in the sense that the last | 43 // thread.) It may be destroyed on any thread, in the sense that the last |
43 // reference to it may be released on any thread, with the proviso that | 44 // reference to it may be released on any thread, with the proviso that |
44 // |Shutdown()| must have been called first (so the pattern is that a "main" | 45 // |Shutdown()| must have been called first (so the pattern is that a "main" |
45 // reference is kept on its creation thread and is released after |Shutdown()| | 46 // reference is kept on its creation thread and is released after |Shutdown()| |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 | 99 |
99 // Removes the given endpoint from this channel (|local_id| and |remote_id| | 100 // Removes the given endpoint from this channel (|local_id| and |remote_id| |
100 // are specified as an optimization; the latter should be an invalid | 101 // are specified as an optimization; the latter should be an invalid |
101 // |ChannelEndpointId| if the endpoint is not yet running). Note: If this is | 102 // |ChannelEndpointId| if the endpoint is not yet running). Note: If this is |
102 // called, the |Channel| will *not* call | 103 // called, the |Channel| will *not* call |
103 // |ChannelEndpoint::DetachFromChannel()|. | 104 // |ChannelEndpoint::DetachFromChannel()|. |
104 void DetachEndpoint(ChannelEndpoint* endpoint, | 105 void DetachEndpoint(ChannelEndpoint* endpoint, |
105 ChannelEndpointId local_id, | 106 ChannelEndpointId local_id, |
106 ChannelEndpointId remote_id); | 107 ChannelEndpointId remote_id); |
107 | 108 |
108 // Returns the size of a serialized endpoint (see |SerializeEndpoint()| and | 109 // Returns the size of a serialized endpoint (see |SerializeEndpoint...()| and |
109 // |DeserializeEndpoint()| below). This value will remain constant for a given | 110 // |DeserializeEndpoint()| below). This value will remain constant for a given |
110 // instance of |Channel|. | 111 // instance of |Channel|. |
111 size_t GetSerializedEndpointSize() const; | 112 size_t GetSerializedEndpointSize() const; |
112 | 113 |
113 // Serializes the given endpoint, writing to |destination| auxiliary | 114 // Endpoint serialization methods: From the |Channel|'s point of view, there |
114 // information to be transmitted to the peer |Channel| via some other means. | 115 // are three cases (discussed further below) and thus three methods. |
115 // |destination| should point to a buffer of (at least) the size returned by | 116 // |
| 117 // All three methods have a |destination| argument, which should be a buffer |
| 118 // to which auxiliary information will be written and which should be |
| 119 // transmitted to the peer |Channel| by some other means, but using this |
| 120 // |Channel|. It should be a buffer of (at least) the size returned by |
116 // |GetSerializedEndpointSize()| (exactly that much data will be written). | 121 // |GetSerializedEndpointSize()| (exactly that much data will be written). |
117 void SerializeEndpoint(scoped_refptr<ChannelEndpoint> endpoint, | 122 // |
118 void* destination); | 123 // All three also have a |message_queue| argument, which if non-null is the |
| 124 // queue of messages already received by the endpoint to be serialized. |
| 125 // |
| 126 // Note that "serialize" really means "send" -- the |endpoint| will be sent |
| 127 // "immediately". The contents of the |destination| buffer can then be used to |
| 128 // claim the rematerialized endpoint from the peer |Channel|. (|destination| |
| 129 // must be sent using this |Channel|, since otherwise it may be received |
| 130 // before it is valid to the peer |Channel|.) |
| 131 // |
| 132 // Case 1: The endpoint's peer is already closed. |
| 133 // |
| 134 // Case 2: The endpoint's peer is local (i.e., it has a |
| 135 // |ChannelEndpointClient| but no peer |ChannelEndpoint|). |
| 136 // |
| 137 // Case 3: The endpoint's peer is remote (i.e., it has a peer |
| 138 // |ChannelEndpoint|). (This has two subcases: the peer endpoint may be on |
| 139 // this |Channel| or another |Channel|.) |
| 140 void SerializeEndpointWithClosedPeer(void* destination, |
| 141 MessageInTransitQueue* message_queue); |
| 142 // This one returns the |ChannelEndpoint| for the serialized endpoint (which |
| 143 // can be used by, e.g., a |ProxyMessagePipeEndpoint|. |
| 144 scoped_refptr<ChannelEndpoint> SerializeEndpointWithLocalPeer( |
| 145 void* destination, |
| 146 MessageInTransitQueue* message_queue, |
| 147 ChannelEndpointClient* endpoint_client, |
| 148 unsigned endpoint_client_port); |
| 149 void SerializeEndpointWithRemotePeer( |
| 150 void* destination, |
| 151 MessageInTransitQueue* message_queue, |
| 152 scoped_refptr<ChannelEndpoint> peer_endpoint); |
119 | 153 |
120 // Deserializes an endpoint that was sent from the peer |Channel| (using | 154 // Deserializes an endpoint that was sent from the peer |Channel| (using |
121 // |SerializeEndpoint()|. |source| should be (a copy of) the data that | 155 // |SerializeEndpoint...()|. |source| should be (a copy of) the data that |
122 // |SerializeEndpoint()| wrote, and must be (at least) | 156 // |SerializeEndpoint...()| wrote, and must be (at least) |
123 // |GetSerializedEndpointSize()| bytes. This returns the deserialized | 157 // |GetSerializedEndpointSize()| bytes. This returns the deserialized |
124 // |IncomingEndpoint| (which can be converted into a |MessagePipe|) or null on | 158 // |IncomingEndpoint| (which can be converted into a |MessagePipe|) or null on |
125 // error. | 159 // error. |
126 scoped_refptr<IncomingEndpoint> DeserializeEndpoint(const void* source); | 160 scoped_refptr<IncomingEndpoint> DeserializeEndpoint(const void* source); |
127 | 161 |
128 // See |RawChannel::GetSerializedPlatformHandleSize()|. | 162 // See |RawChannel::GetSerializedPlatformHandleSize()|. |
129 size_t GetSerializedPlatformHandleSize() const; | 163 size_t GetSerializedPlatformHandleSize() const; |
130 | 164 |
131 embedder::PlatformSupport* platform_support() const { | 165 embedder::PlatformSupport* platform_support() const { |
132 return platform_support_; | 166 return platform_support_; |
(...skipping 26 matching lines...) Expand all Loading... |
159 // Handles "remove endpoint ack" messages. | 193 // Handles "remove endpoint ack" messages. |
160 bool OnRemoveEndpointAck(ChannelEndpointId local_id); | 194 bool OnRemoveEndpointAck(ChannelEndpointId local_id); |
161 | 195 |
162 // Handles errors (e.g., invalid messages) from the remote side. Callable from | 196 // Handles errors (e.g., invalid messages) from the remote side. Callable from |
163 // any thread. | 197 // any thread. |
164 void HandleRemoteError(const base::StringPiece& error_message); | 198 void HandleRemoteError(const base::StringPiece& error_message); |
165 // Handles internal errors/failures from the local side. Callable from any | 199 // Handles internal errors/failures from the local side. Callable from any |
166 // thread. | 200 // thread. |
167 void HandleLocalError(const base::StringPiece& error_message); | 201 void HandleLocalError(const base::StringPiece& error_message); |
168 | 202 |
169 // Helper for |SerializeEndpoint()|: Attaches the given (non-bootstrap) | 203 // Helper for |SerializeEndpoint...()|: Attaches the given (non-bootstrap) |
170 // endpoint to this channel and runs it. This assigns the endpoint both local | 204 // endpoint to this channel and runs it. This assigns the endpoint both local |
171 // and remote IDs. This will also send a |kSubtypeChannelAttachAndRunEndpoint| | 205 // and remote IDs. This will also send a |kSubtypeChannelAttachAndRunEndpoint| |
172 // message to the remote side to tell it to create an endpoint as well. This | 206 // message to the remote side to tell it to create an endpoint as well. This |
173 // returns the *remote* ID (one for which |is_remote()| returns true). | 207 // returns the *remote* ID (one for which |is_remote()| returns true). |
174 // | 208 // |
175 // TODO(vtl): Maybe limit the number of attached message pipes. | 209 // TODO(vtl): Maybe limit the number of attached message pipes. |
176 ChannelEndpointId AttachAndRunEndpoint( | 210 ChannelEndpointId AttachAndRunEndpoint( |
177 scoped_refptr<ChannelEndpoint> endpoint); | 211 scoped_refptr<ChannelEndpoint> endpoint); |
178 | 212 |
179 // Helper to send channel control messages. Returns true on success. Should be | 213 // Helper to send channel control messages. Returns true on success. Should be |
180 // called *without* |lock_| held. Callable from any thread. | 214 // called *without* |lock_| held. Callable from any thread. |
181 bool SendControlMessage(MessageInTransit::Subtype subtype, | 215 bool SendControlMessage(MessageInTransit::Subtype subtype, |
182 ChannelEndpointId source_id, | 216 ChannelEndpointId source_id, |
183 ChannelEndpointId destination_id); | 217 ChannelEndpointId destination_id); |
184 | 218 |
185 base::ThreadChecker creation_thread_checker_; | 219 base::ThreadChecker creation_thread_checker_; |
186 | 220 |
187 embedder::PlatformSupport* const platform_support_; | 221 embedder::PlatformSupport* const platform_support_; |
188 | 222 |
189 // Note: |ChannelEndpointClient|s (in particular, |MessagePipe|s) MUST NOT be | 223 // Note: |ChannelEndpointClient|s (in particular, |MessagePipe|s) MUST NOT be |
190 // used under |lock_|. E.g., |lock_| can only be acquired after | 224 // used under |lock_|. E.g., |lock_| can only be acquired after |
191 // |MessagePipe::lock_|, never before. Thus to call into a | 225 // |MessagePipe::lock_|, never before. Thus to call into a |
192 // |ChannelEndpointClinet|, a reference should be acquired from | 226 // |ChannelEndpointClient|, a reference should be acquired from |
193 // |local_id_to_endpoint_map_| under |lock_| and then the lock released. | 227 // |local_id_to_endpoint_map_| under |lock_| and then the lock released. |
194 base::Lock lock_; // Protects the members below. | 228 base::Lock lock_; // Protects the members below. |
195 | 229 |
196 scoped_ptr<RawChannel> raw_channel_; | 230 scoped_ptr<RawChannel> raw_channel_; |
197 bool is_running_; | 231 bool is_running_; |
198 // Set when |WillShutdownSoon()| is called. | 232 // Set when |WillShutdownSoon()| is called. |
199 bool is_shutting_down_; | 233 bool is_shutting_down_; |
200 | 234 |
201 // Has a reference to us. | 235 // Has a reference to us. |
202 ChannelManager* channel_manager_; | 236 ChannelManager* channel_manager_; |
(...skipping 15 matching lines...) Expand all Loading... |
218 // if/when we wrap). | 252 // if/when we wrap). |
219 RemoteChannelEndpointIdGenerator remote_id_generator_; | 253 RemoteChannelEndpointIdGenerator remote_id_generator_; |
220 | 254 |
221 DISALLOW_COPY_AND_ASSIGN(Channel); | 255 DISALLOW_COPY_AND_ASSIGN(Channel); |
222 }; | 256 }; |
223 | 257 |
224 } // namespace system | 258 } // namespace system |
225 } // namespace mojo | 259 } // namespace mojo |
226 | 260 |
227 #endif // MOJO_EDK_SYSTEM_CHANNEL_H_ | 261 #endif // MOJO_EDK_SYSTEM_CHANNEL_H_ |
OLD | NEW |