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 14 matching lines...) Expand all Loading... |
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; | |
36 | 35 |
37 // This class is mostly thread-safe. It must be created on an I/O thread. | 36 // 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 | 37 // |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()| | 38 // 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 | 39 // 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 | 40 // 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 | 41 // 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 | 42 // 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" | 43 // |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()| | 44 // reference is kept on its creation thread and is released after |Shutdown()| |
46 // is called, but other threads may have temporarily "dangling" references). | 45 // is called, but other threads may have temporarily "dangling" references). |
47 // | 46 // |
48 // Note the lock order (in order of allowable acquisition): |MessagePipe|, | 47 // Note the lock order (in order of allowable acquisition): |MessagePipe|, |
49 // |ChannelEndpoint|, |Channel|. Thus |Channel| may not call into | 48 // |ChannelEndpoint|, |Channel|. Thus |Channel| may not call into |
50 // |ChannelEndpoint| with |Channel|'s lock held. | 49 // |ChannelEndpoint| with |Channel|'s lock held. |
51 class MOJO_SYSTEM_IMPL_EXPORT Channel | 50 class MOJO_SYSTEM_IMPL_EXPORT Channel |
52 : public base::RefCountedThreadSafe<Channel>, | 51 : public base::RefCountedThreadSafe<Channel>, |
53 public RawChannel::Delegate { | 52 public RawChannel::Delegate { |
54 public: | 53 public: |
55 // |platform_support| (typically owned by |Core|) must remain alive until | 54 // |platform_support| (typically owned by |Core|) must remain alive until |
56 // after |Shutdown()| is called. | 55 // after |Shutdown()| is called. |
57 explicit Channel(embedder::PlatformSupport* platform_support); | 56 explicit Channel(embedder::PlatformSupport* platform_support); |
58 | 57 |
59 // This must be called on the creation thread before any other methods are | 58 // 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 | 59 // called, and before references to this object are given to any other |
61 // threads. |raw_channel| should be uninitialized. Returns true on success. On | 60 // threads. |raw_channel| should be uninitialized. Returns true on success. On |
62 // failure, no other methods should be called (including |Shutdown()|). | 61 // failure, no other methods should be called (including |Shutdown()|). |
63 bool Init(scoped_ptr<RawChannel> raw_channel); | 62 bool Init(scoped_ptr<RawChannel> raw_channel); |
64 | 63 |
65 // Sets the channel manager associated with this channel. This should be set | |
66 // at most once and only called before |WillShutdownSoon()| (and | |
67 // |Shutdown()|). | |
68 void SetChannelManager(ChannelManager* channel_manager); | |
69 | |
70 // This must be called on the creation thread before destruction (which can | 64 // This must be called on the creation thread before destruction (which can |
71 // happen on any thread). | 65 // happen on any thread). |
72 void Shutdown(); | 66 void Shutdown(); |
73 | 67 |
74 // Signals that |Shutdown()| will be called soon (this may be called from any | 68 // 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 | 69 // thread, unlike |Shutdown()|). Warnings will be issued if, e.g., messages |
76 // are written after this is called; other warnings may be suppressed. (This | 70 // are written after this is called; other warnings may be suppressed. (This |
77 // may be called multiple times, or not at all.) | 71 // may be called multiple times, or not at all.) |
78 // | |
79 // If set, the channel manager associated with this channel will be reset. | |
80 void WillShutdownSoon(); | 72 void WillShutdownSoon(); |
81 | 73 |
82 // Attaches the given endpoint to this channel and runs it. |is_bootstrap| | 74 // Attaches the given endpoint to this channel and runs it. |is_bootstrap| |
83 // should be set if and only if it is the first endpoint on the channel. This | 75 // should be set if and only if it is the first endpoint on the channel. This |
84 // assigns the endpoint both local and remote IDs. If |is_bootstrap| is set, | 76 // assigns the endpoint both local and remote IDs. If |is_bootstrap| is set, |
85 // both are the bootstrap ID (given by |ChannelEndpointId::GetBootstrap()|); | 77 // both are the bootstrap ID (given by |ChannelEndpointId::GetBootstrap()|); |
86 // if not, it will also send a |kSubtypeChannelAttachAndRunEndpoint| message | 78 // if not, it will also send a |kSubtypeChannelAttachAndRunEndpoint| message |
87 // to the remote side to tell it to create an endpoint as well. | 79 // to the remote side to tell it to create an endpoint as well. |
88 // | 80 // |
89 // (Bootstrapping is symmetric: Both sides attach and run endpoints with | 81 // (Bootstrapping is symmetric: Both sides attach and run endpoints with |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 // be acquired after |MessagePipe::lock_|, never before. Thus to call into a | 168 // be acquired after |MessagePipe::lock_|, never before. Thus to call into a |
177 // |MessagePipe|, a reference to the |MessagePipe| should be acquired from | 169 // |MessagePipe|, a reference to the |MessagePipe| should be acquired from |
178 // |local_id_to_endpoint_map_| under |lock_| and then the lock released. | 170 // |local_id_to_endpoint_map_| under |lock_| and then the lock released. |
179 base::Lock lock_; // Protects the members below. | 171 base::Lock lock_; // Protects the members below. |
180 | 172 |
181 scoped_ptr<RawChannel> raw_channel_; | 173 scoped_ptr<RawChannel> raw_channel_; |
182 bool is_running_; | 174 bool is_running_; |
183 // Set when |WillShutdownSoon()| is called. | 175 // Set when |WillShutdownSoon()| is called. |
184 bool is_shutting_down_; | 176 bool is_shutting_down_; |
185 | 177 |
186 // Has a reference to us. | |
187 ChannelManager* channel_manager_; | |
188 | |
189 typedef base::hash_map<ChannelEndpointId, scoped_refptr<ChannelEndpoint>> | 178 typedef base::hash_map<ChannelEndpointId, scoped_refptr<ChannelEndpoint>> |
190 IdToEndpointMap; | 179 IdToEndpointMap; |
191 // Map from local IDs to endpoints. If the endpoint is null, this means that | 180 // 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. | 181 // we're just waiting for the remove ack before removing the entry. |
193 IdToEndpointMap local_id_to_endpoint_map_; | 182 IdToEndpointMap local_id_to_endpoint_map_; |
194 // Note: The IDs generated by this should be checked for existence before use. | 183 // Note: The IDs generated by this should be checked for existence before use. |
195 LocalChannelEndpointIdGenerator local_id_generator_; | 184 LocalChannelEndpointIdGenerator local_id_generator_; |
196 | 185 |
197 typedef base::hash_map<ChannelEndpointId, scoped_refptr<MessagePipe>> | 186 typedef base::hash_map<ChannelEndpointId, scoped_refptr<MessagePipe>> |
198 IdToMessagePipeMap; | 187 IdToMessagePipeMap; |
199 // Map from local IDs to pending/incoming endpoints (i.e., those which do not | 188 // Map from local IDs to pending/incoming endpoints (i.e., those which do not |
200 // yet have a dispatcher attached). | 189 // yet have a dispatcher attached). |
201 IdToMessagePipeMap incoming_message_pipes_; | 190 IdToMessagePipeMap incoming_message_pipes_; |
202 // TODO(vtl): We need to keep track of remote IDs (so that we don't collide | 191 // TODO(vtl): We need to keep track of remote IDs (so that we don't collide |
203 // if/when we wrap). | 192 // if/when we wrap). |
204 RemoteChannelEndpointIdGenerator remote_id_generator_; | 193 RemoteChannelEndpointIdGenerator remote_id_generator_; |
205 | 194 |
206 DISALLOW_COPY_AND_ASSIGN(Channel); | 195 DISALLOW_COPY_AND_ASSIGN(Channel); |
207 }; | 196 }; |
208 | 197 |
209 } // namespace system | 198 } // namespace system |
210 } // namespace mojo | 199 } // namespace mojo |
211 | 200 |
212 #endif // MOJO_EDK_SYSTEM_CHANNEL_H_ | 201 #endif // MOJO_EDK_SYSTEM_CHANNEL_H_ |
OLD | NEW |