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" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/strings/string_piece.h" | 14 #include "base/strings/string_piece.h" |
15 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
16 #include "base/threading/thread_checker.h" | 16 #include "base/threading/thread_checker.h" |
17 #include "mojo/edk/embedder/scoped_platform_handle.h" | 17 #include "mojo/edk/embedder/scoped_platform_handle.h" |
18 #include "mojo/edk/system/channel_endpoint.h" | 18 #include "mojo/edk/system/channel_endpoint.h" |
19 #include "mojo/edk/system/channel_endpoint_id.h" | 19 #include "mojo/edk/system/channel_endpoint_id.h" |
| 20 #include "mojo/edk/system/incoming_endpoint.h" |
20 #include "mojo/edk/system/message_in_transit.h" | 21 #include "mojo/edk/system/message_in_transit.h" |
21 #include "mojo/edk/system/message_pipe.h" | |
22 #include "mojo/edk/system/raw_channel.h" | 22 #include "mojo/edk/system/raw_channel.h" |
23 #include "mojo/edk/system/system_impl_export.h" | 23 #include "mojo/edk/system/system_impl_export.h" |
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 ChannelEndpoint; |
35 class ChannelManager; | 35 class ChannelManager; |
36 | 36 |
37 // This class is mostly thread-safe. It must be created on an I/O thread. | 37 // 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 | 38 // |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()| | 39 // 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 | 40 // 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 | 41 // 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 | 42 // 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 | 43 // 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" | 44 // |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()| | 45 // reference is kept on its creation thread and is released after |Shutdown()| |
46 // is called, but other threads may have temporarily "dangling" references). | 46 // is called, but other threads may have temporarily "dangling" references). |
47 // | 47 // |
48 // Note the lock order (in order of allowable acquisition): |MessagePipe|, | 48 // Note the lock order (in order of allowable acquisition): |
49 // |ChannelEndpoint|, |Channel|. Thus |Channel| may not call into | 49 // |ChannelEndpointClient| (e.g., |MessagePipe|), |ChannelEndpoint|, |Channel|. |
50 // |ChannelEndpoint| with |Channel|'s lock held. | 50 // Thus |Channel| may not call into |ChannelEndpoint| with |Channel|'s lock |
| 51 // held. |
51 class MOJO_SYSTEM_IMPL_EXPORT Channel | 52 class MOJO_SYSTEM_IMPL_EXPORT Channel |
52 : public base::RefCountedThreadSafe<Channel>, | 53 : public base::RefCountedThreadSafe<Channel>, |
53 public RawChannel::Delegate { | 54 public RawChannel::Delegate { |
54 public: | 55 public: |
55 // |platform_support| (typically owned by |Core|) must remain alive until | 56 // |platform_support| (typically owned by |Core|) must remain alive until |
56 // after |Shutdown()| is called. | 57 // after |Shutdown()| is called. |
57 explicit Channel(embedder::PlatformSupport* platform_support); | 58 explicit Channel(embedder::PlatformSupport* platform_support); |
58 | 59 |
59 // This must be called on the creation thread before any other methods are | 60 // This must be called on the creation thread before any other methods are |
60 // called, and before references to this object are given to any other | 61 // called, and before references to this object are given to any other |
(...skipping 11 matching lines...) Expand all Loading... |
72 void Shutdown(); | 73 void Shutdown(); |
73 | 74 |
74 // Signals that |Shutdown()| will be called soon (this may be called from any | 75 // Signals that |Shutdown()| will be called soon (this may be called from any |
75 // thread, unlike |Shutdown()|). Warnings will be issued if, e.g., messages | 76 // thread, unlike |Shutdown()|). Warnings will be issued if, e.g., messages |
76 // are written after this is called; other warnings may be suppressed. (This | 77 // are written after this is called; other warnings may be suppressed. (This |
77 // may be called multiple times, or not at all.) | 78 // may be called multiple times, or not at all.) |
78 // | 79 // |
79 // If set, the channel manager associated with this channel will be reset. | 80 // If set, the channel manager associated with this channel will be reset. |
80 void WillShutdownSoon(); | 81 void WillShutdownSoon(); |
81 | 82 |
82 // Attaches the given endpoint to this channel and runs it. |is_bootstrap| | 83 // Called to set (i.e., attach and run) the bootstrap (first) endpoint on the |
83 // should be set if and only if it is the first endpoint on the channel. This | 84 // channel. Both the local and remote IDs are the bootstrap ID (given by |
84 // assigns the endpoint both local and remote IDs. If |is_bootstrap| is set, | 85 // |ChannelEndpointId::GetBootstrap()|). |
85 // both are the bootstrap ID (given by |ChannelEndpointId::GetBootstrap()|); | |
86 // if not, it will also send a |kSubtypeChannelAttachAndRunEndpoint| message | |
87 // to the remote side to tell it to create an endpoint as well. | |
88 // | 86 // |
89 // (Bootstrapping is symmetric: Both sides attach and run endpoints with | 87 // (Bootstrapping is symmetric: Both sides call this, which will establish the |
90 // |is_bootstrap| set, which establishes the first message pipe across a | 88 // first connection across a channel.) |
91 // channel.) | 89 void SetBootstrapEndpoint(scoped_refptr<ChannelEndpoint> endpoint); |
92 // | |
93 // This returns the *remote* ID (which will be the bootstrap ID in the | |
94 // bootstrap case, and a "remote ID", i.e., one for which |is_remote()| | |
95 // returns true, otherwise). | |
96 // | |
97 // TODO(vtl): Maybe limit the number of attached message pipes. | |
98 ChannelEndpointId AttachAndRunEndpoint( | |
99 scoped_refptr<ChannelEndpoint> endpoint, | |
100 bool is_bootstrap); | |
101 | 90 |
102 // This forwards |message| verbatim to |raw_channel_|. | 91 // This forwards |message| verbatim to |raw_channel_|. |
103 bool WriteMessage(scoped_ptr<MessageInTransit> message); | 92 bool WriteMessage(scoped_ptr<MessageInTransit> message); |
104 | 93 |
105 // See |RawChannel::IsWriteBufferEmpty()|. | 94 // See |RawChannel::IsWriteBufferEmpty()|. |
106 // TODO(vtl): Maybe we shouldn't expose this, and instead have a | 95 // TODO(vtl): Maybe we shouldn't expose this, and instead have a |
107 // |FlushWriteBufferAndShutdown()| or something like that. | 96 // |FlushWriteBufferAndShutdown()| or something like that. |
108 bool IsWriteBufferEmpty(); | 97 bool IsWriteBufferEmpty(); |
109 | 98 |
110 // Removes the given endpoint from this channel (|local_id| and |remote_id| | 99 // Removes the given endpoint from this channel (|local_id| and |remote_id| |
111 // are specified as an optimization; the latter should be an invalid | 100 // are specified as an optimization; the latter should be an invalid |
112 // |ChannelEndpointId| if the endpoint is not yet running). Note: If this is | 101 // |ChannelEndpointId| if the endpoint is not yet running). Note: If this is |
113 // called, the |Channel| will *not* call | 102 // called, the |Channel| will *not* call |
114 // |ChannelEndpoint::DetachFromChannel()|. | 103 // |ChannelEndpoint::DetachFromChannel()|. |
115 void DetachEndpoint(ChannelEndpoint* endpoint, | 104 void DetachEndpoint(ChannelEndpoint* endpoint, |
116 ChannelEndpointId local_id, | 105 ChannelEndpointId local_id, |
117 ChannelEndpointId remote_id); | 106 ChannelEndpointId remote_id); |
118 | 107 |
119 // Takes ownership of an incoming message pipe (i.e., one that was created via | 108 // Returns the size of a serialized endpoint (see |SerializeEndpoint()| and |
120 // a |kSubtypeChannelAttachAndRunEndpoint| message). | 109 // |DeserializeEndpoint()| below). This value will remain constant for a given |
121 scoped_refptr<MessagePipe> PassIncomingMessagePipe( | 110 // instance of |Channel|. |
122 ChannelEndpointId local_id); | 111 size_t GetSerializedEndpointSize() const; |
| 112 |
| 113 // Serializes the given endpoint, writing to |destination| auxiliary |
| 114 // information to be transmitted to the peer |Channel| via some other means. |
| 115 // |destination| should point to a buffer of (at least) the size returned by |
| 116 // |GetSerializedEndpointSize()| (exactly that much data will be written). |
| 117 void SerializeEndpoint(scoped_refptr<ChannelEndpoint> endpoint, |
| 118 void* destination); |
| 119 |
| 120 // Deserializes an endpoint that was sent from the peer |Channel| (using |
| 121 // |SerializeEndpoint()|. |source| should be (a copy of) the data that |
| 122 // |SerializeEndpoint()| wrote, and must be (at least) |
| 123 // |GetSerializedEndpointSize()| bytes. This returns the deserialized |
| 124 // |IncomingEndpoint| (which can be converted into a |MessagePipe|) or null on |
| 125 // error. |
| 126 scoped_refptr<IncomingEndpoint> DeserializeEndpoint(const void* source); |
123 | 127 |
124 // See |RawChannel::GetSerializedPlatformHandleSize()|. | 128 // See |RawChannel::GetSerializedPlatformHandleSize()|. |
125 size_t GetSerializedPlatformHandleSize() const; | 129 size_t GetSerializedPlatformHandleSize() const; |
126 | 130 |
127 embedder::PlatformSupport* platform_support() const { | 131 embedder::PlatformSupport* platform_support() const { |
128 return platform_support_; | 132 return platform_support_; |
129 } | 133 } |
130 | 134 |
131 private: | 135 private: |
132 friend class base::RefCountedThreadSafe<Channel>; | 136 friend class base::RefCountedThreadSafe<Channel>; |
133 ~Channel() override; | 137 ~Channel() override; |
134 | 138 |
135 // |RawChannel::Delegate| implementation (only called on the creation thread): | 139 // |RawChannel::Delegate| implementation (only called on the creation thread): |
136 void OnReadMessage( | 140 void OnReadMessage( |
137 const MessageInTransit::View& message_view, | 141 const MessageInTransit::View& message_view, |
138 embedder::ScopedPlatformHandleVectorPtr platform_handles) override; | 142 embedder::ScopedPlatformHandleVectorPtr platform_handles) override; |
139 void OnError(Error error) override; | 143 void OnError(Error error) override; |
140 | 144 |
141 // Helpers for |OnReadMessage| (only called on the creation thread): | 145 // Helpers for |OnReadMessage| (only called on the creation thread): |
142 void OnReadMessageForEndpoint( | 146 void OnReadMessageForEndpoint( |
143 const MessageInTransit::View& message_view, | 147 const MessageInTransit::View& message_view, |
144 embedder::ScopedPlatformHandleVectorPtr platform_handles); | 148 embedder::ScopedPlatformHandleVectorPtr platform_handles); |
145 void OnReadMessageForChannel( | 149 void OnReadMessageForChannel( |
146 const MessageInTransit::View& message_view, | 150 const MessageInTransit::View& message_view, |
147 embedder::ScopedPlatformHandleVectorPtr platform_handles); | 151 embedder::ScopedPlatformHandleVectorPtr platform_handles); |
148 | 152 |
149 // Handles "attach and run endpoint" messages. | 153 // Handles "attach and run endpoint" messages. |
150 bool OnAttachAndRunEndpoint(ChannelEndpointId local_id, | 154 bool OnAttachAndRunEndpoint(ChannelEndpointId local_id, |
151 ChannelEndpointId remote_id); | 155 ChannelEndpointId remote_id); |
152 // Handles "remove message pipe endpoint" messages. | 156 // Handles "remove endpoint" messages. |
153 bool OnRemoveMessagePipeEndpoint(ChannelEndpointId local_id, | 157 bool OnRemoveEndpoint(ChannelEndpointId local_id, |
154 ChannelEndpointId remote_id); | 158 ChannelEndpointId remote_id); |
155 // Handles "remove message pipe endpoint ack" messages. | 159 // Handles "remove endpoint ack" messages. |
156 bool OnRemoveMessagePipeEndpointAck(ChannelEndpointId local_id); | 160 bool OnRemoveEndpointAck(ChannelEndpointId local_id); |
157 | 161 |
158 // Handles errors (e.g., invalid messages) from the remote side. Callable from | 162 // Handles errors (e.g., invalid messages) from the remote side. Callable from |
159 // any thread. | 163 // any thread. |
160 void HandleRemoteError(const base::StringPiece& error_message); | 164 void HandleRemoteError(const base::StringPiece& error_message); |
161 // Handles internal errors/failures from the local side. Callable from any | 165 // Handles internal errors/failures from the local side. Callable from any |
162 // thread. | 166 // thread. |
163 void HandleLocalError(const base::StringPiece& error_message); | 167 void HandleLocalError(const base::StringPiece& error_message); |
164 | 168 |
| 169 // Helper for |SerializeEndpoint()|: Attaches the given (non-bootstrap) |
| 170 // endpoint to this channel and runs it. This assigns the endpoint both local |
| 171 // 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 |
| 173 // returns the *remote* ID (one for which |is_remote()| returns true). |
| 174 // |
| 175 // TODO(vtl): Maybe limit the number of attached message pipes. |
| 176 ChannelEndpointId AttachAndRunEndpoint( |
| 177 scoped_refptr<ChannelEndpoint> endpoint); |
| 178 |
165 // Helper to send channel control messages. Returns true on success. Should be | 179 // Helper to send channel control messages. Returns true on success. Should be |
166 // called *without* |lock_| held. Callable from any thread. | 180 // called *without* |lock_| held. Callable from any thread. |
167 bool SendControlMessage(MessageInTransit::Subtype subtype, | 181 bool SendControlMessage(MessageInTransit::Subtype subtype, |
168 ChannelEndpointId source_id, | 182 ChannelEndpointId source_id, |
169 ChannelEndpointId destination_id); | 183 ChannelEndpointId destination_id); |
170 | 184 |
171 base::ThreadChecker creation_thread_checker_; | 185 base::ThreadChecker creation_thread_checker_; |
172 | 186 |
173 embedder::PlatformSupport* const platform_support_; | 187 embedder::PlatformSupport* const platform_support_; |
174 | 188 |
175 // Note: |MessagePipe|s MUST NOT be used under |lock_|. I.e., |lock_| can only | 189 // Note: |ChannelEndpointClient|s (in particular, |MessagePipe|s) MUST NOT be |
176 // be acquired after |MessagePipe::lock_|, never before. Thus to call into a | 190 // used under |lock_|. E.g., |lock_| can only be acquired after |
177 // |MessagePipe|, a reference to the |MessagePipe| should be acquired from | 191 // |MessagePipe::lock_|, never before. Thus to call into a |
| 192 // |ChannelEndpointClinet|, a reference should be acquired from |
178 // |local_id_to_endpoint_map_| under |lock_| and then the lock released. | 193 // |local_id_to_endpoint_map_| under |lock_| and then the lock released. |
179 base::Lock lock_; // Protects the members below. | 194 base::Lock lock_; // Protects the members below. |
180 | 195 |
181 scoped_ptr<RawChannel> raw_channel_; | 196 scoped_ptr<RawChannel> raw_channel_; |
182 bool is_running_; | 197 bool is_running_; |
183 // Set when |WillShutdownSoon()| is called. | 198 // Set when |WillShutdownSoon()| is called. |
184 bool is_shutting_down_; | 199 bool is_shutting_down_; |
185 | 200 |
186 // Has a reference to us. | 201 // Has a reference to us. |
187 ChannelManager* channel_manager_; | 202 ChannelManager* channel_manager_; |
188 | 203 |
189 typedef base::hash_map<ChannelEndpointId, scoped_refptr<ChannelEndpoint>> | 204 typedef base::hash_map<ChannelEndpointId, scoped_refptr<ChannelEndpoint>> |
190 IdToEndpointMap; | 205 IdToEndpointMap; |
191 // Map from local IDs to endpoints. If the endpoint is null, this means that | 206 // Map from local IDs to endpoints. If the endpoint is null, this means that |
192 // we're just waiting for the remove ack before removing the entry. | 207 // we're just waiting for the remove ack before removing the entry. |
193 IdToEndpointMap local_id_to_endpoint_map_; | 208 IdToEndpointMap local_id_to_endpoint_map_; |
194 // Note: The IDs generated by this should be checked for existence before use. | 209 // Note: The IDs generated by this should be checked for existence before use. |
195 LocalChannelEndpointIdGenerator local_id_generator_; | 210 LocalChannelEndpointIdGenerator local_id_generator_; |
196 | 211 |
197 typedef base::hash_map<ChannelEndpointId, scoped_refptr<MessagePipe>> | 212 typedef base::hash_map<ChannelEndpointId, scoped_refptr<IncomingEndpoint>> |
198 IdToMessagePipeMap; | 213 IdToIncomingEndpointMap; |
199 // Map from local IDs to pending/incoming message pipes (i.e., those which do | 214 // Map from local IDs to incoming endpoints (i.e., those received inside other |
200 // not yet have a dispatcher attached). | 215 // messages, but not yet claimed via |DeserializeEndpoint()|). |
201 // TODO(vtl): This is a layering violation, since |Channel| shouldn't know | 216 IdToIncomingEndpointMap incoming_endpoints_; |
202 // about |MessagePipe|. However, we can't just hang on to |ChannelEndpoint|s | |
203 // (even if they have a reference to the |MessagePipe|) since their lifetimes | |
204 // are tied to the "remote" side. When |ChannelEndpoint::DetachFromChannel()| | |
205 // (eventually) results in |ChannelEndpoint::DetachFromClient()| being called. | |
206 // We really need to hang on to the "local" side of the message pipe, to which | |
207 // dispatchers will be "attached". | |
208 IdToMessagePipeMap incoming_message_pipes_; | |
209 // TODO(vtl): We need to keep track of remote IDs (so that we don't collide | 217 // TODO(vtl): We need to keep track of remote IDs (so that we don't collide |
210 // if/when we wrap). | 218 // if/when we wrap). |
211 RemoteChannelEndpointIdGenerator remote_id_generator_; | 219 RemoteChannelEndpointIdGenerator remote_id_generator_; |
212 | 220 |
213 DISALLOW_COPY_AND_ASSIGN(Channel); | 221 DISALLOW_COPY_AND_ASSIGN(Channel); |
214 }; | 222 }; |
215 | 223 |
216 } // namespace system | 224 } // namespace system |
217 } // namespace mojo | 225 } // namespace mojo |
218 | 226 |
219 #endif // MOJO_EDK_SYSTEM_CHANNEL_H_ | 227 #endif // MOJO_EDK_SYSTEM_CHANNEL_H_ |
OLD | NEW |