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