| 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/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/synchronization/lock.h" | |
| 14 #include "base/threading/thread_checker.h" | 13 #include "base/threading/thread_checker.h" |
| 15 #include "mojo/edk/embedder/scoped_platform_handle.h" | 14 #include "mojo/edk/embedder/scoped_platform_handle.h" |
| 16 #include "mojo/edk/system/channel_endpoint.h" | 15 #include "mojo/edk/system/channel_endpoint.h" |
| 17 #include "mojo/edk/system/channel_endpoint_id.h" | 16 #include "mojo/edk/system/channel_endpoint_id.h" |
| 18 #include "mojo/edk/system/incoming_endpoint.h" | 17 #include "mojo/edk/system/incoming_endpoint.h" |
| 19 #include "mojo/edk/system/message_in_transit.h" | 18 #include "mojo/edk/system/message_in_transit.h" |
| 19 #include "mojo/edk/system/mutex.h" |
| 20 #include "mojo/edk/system/raw_channel.h" | 20 #include "mojo/edk/system/raw_channel.h" |
| 21 #include "mojo/edk/system/system_impl_export.h" | 21 #include "mojo/edk/system/system_impl_export.h" |
| 22 #include "mojo/public/c/system/types.h" | 22 #include "mojo/public/c/system/types.h" |
| 23 #include "mojo/public/cpp/system/macros.h" | 23 #include "mojo/public/cpp/system/macros.h" |
| 24 | 24 |
| 25 namespace mojo { | 25 namespace mojo { |
| 26 | 26 |
| 27 namespace embedder { | 27 namespace embedder { |
| 28 class PlatformSupport; | 28 class PlatformSupport; |
| 29 } | 29 } |
| (...skipping 22 matching lines...) Expand all Loading... |
| 52 class MOJO_SYSTEM_IMPL_EXPORT Channel final | 52 class MOJO_SYSTEM_IMPL_EXPORT Channel final |
| 53 : public base::RefCountedThreadSafe<Channel>, | 53 : public base::RefCountedThreadSafe<Channel>, |
| 54 public RawChannel::Delegate { | 54 public RawChannel::Delegate { |
| 55 public: | 55 public: |
| 56 // |platform_support| must remain alive until after |Shutdown()| is called. | 56 // |platform_support| must remain alive until after |Shutdown()| is called. |
| 57 explicit Channel(embedder::PlatformSupport* platform_support); | 57 explicit Channel(embedder::PlatformSupport* platform_support); |
| 58 | 58 |
| 59 // 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 |
| 60 // 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 |
| 61 // threads. |raw_channel| should be uninitialized. | 61 // threads. |raw_channel| should be uninitialized. |
| 62 void Init(scoped_ptr<RawChannel> raw_channel); | 62 void Init(scoped_ptr<RawChannel> raw_channel) MOJO_NOT_THREAD_SAFE; |
| 63 | 63 |
| 64 // Sets the channel manager associated with this channel. This should be set | 64 // Sets the channel manager associated with this channel. This should be set |
| 65 // at most once and only called before |WillShutdownSoon()| (and | 65 // at most once and only called before |WillShutdownSoon()| (and |
| 66 // |Shutdown()|). (This is called by the channel manager when adding a | 66 // |Shutdown()|). (This is called by the channel manager when adding a |
| 67 // channel; this should not be called before the channel is managed by the | 67 // channel; this should not be called before the channel is managed by the |
| 68 // channel manager.) | 68 // channel manager.) |
| 69 void SetChannelManager(ChannelManager* channel_manager); | 69 void SetChannelManager(ChannelManager* channel_manager); |
| 70 | 70 |
| 71 // This must be called on the creation thread before destruction (which can | 71 // This must be called on the creation thread before destruction (which can |
| 72 // happen on any thread). | 72 // happen on any thread). |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 // endpoint to this channel and runs it. This assigns the endpoint both local | 213 // endpoint to this channel and runs it. This assigns the endpoint both local |
| 214 // and remote IDs. This will also send a | 214 // and remote IDs. This will also send a |
| 215 // |Subtype::CHANNEL_ATTACH_AND_RUN_ENDPOINT| message to the remote side to | 215 // |Subtype::CHANNEL_ATTACH_AND_RUN_ENDPOINT| message to the remote side to |
| 216 // tell it to create an endpoint as well. This returns the *remote* ID (one | 216 // tell it to create an endpoint as well. This returns the *remote* ID (one |
| 217 // for which |is_remote()| returns true). | 217 // for which |is_remote()| returns true). |
| 218 // | 218 // |
| 219 // TODO(vtl): Maybe limit the number of attached message pipes. | 219 // TODO(vtl): Maybe limit the number of attached message pipes. |
| 220 ChannelEndpointId AttachAndRunEndpoint( | 220 ChannelEndpointId AttachAndRunEndpoint( |
| 221 scoped_refptr<ChannelEndpoint> endpoint); | 221 scoped_refptr<ChannelEndpoint> endpoint); |
| 222 | 222 |
| 223 // Helper to send channel control messages. Returns true on success. Should be | 223 // Helper to send channel control messages. Returns true on success. Callable |
| 224 // called *without* |lock_| held. Callable from any thread. | 224 // from any thread. |
| 225 bool SendControlMessage(MessageInTransit::Subtype subtype, | 225 bool SendControlMessage(MessageInTransit::Subtype subtype, |
| 226 ChannelEndpointId source_id, | 226 ChannelEndpointId source_id, |
| 227 ChannelEndpointId destination_id); | 227 ChannelEndpointId destination_id) |
| 228 MOJO_LOCKS_EXCLUDED(mutex_); |
| 228 | 229 |
| 229 base::ThreadChecker creation_thread_checker_; | 230 base::ThreadChecker creation_thread_checker_; |
| 230 | 231 |
| 231 embedder::PlatformSupport* const platform_support_; | 232 embedder::PlatformSupport* const platform_support_; |
| 232 | 233 |
| 233 // Note: |ChannelEndpointClient|s (in particular, |MessagePipe|s) MUST NOT be | 234 // Note: |ChannelEndpointClient|s (in particular, |MessagePipe|s) MUST NOT be |
| 234 // used under |lock_|. E.g., |lock_| can only be acquired after | 235 // used under |mutex_|. E.g., |mutex_| can only be acquired after |
| 235 // |MessagePipe::lock_|, never before. Thus to call into a | 236 // |MessagePipe::lock_|, never before. Thus to call into a |
| 236 // |ChannelEndpointClient|, a reference should be acquired from | 237 // |ChannelEndpointClient|, a reference should be acquired from |
| 237 // |local_id_to_endpoint_map_| under |lock_| and then the lock released. | 238 // |local_id_to_endpoint_map_| under |mutex_| and then the lock released. |
| 238 base::Lock lock_; // Protects the members below. | 239 // TODO(vtl): Annotate the above rule using |MOJO_ACQUIRED_{BEFORE,AFTER}()|, |
| 240 // once clang actually checks such annotations. |
| 241 // https://github.com/domokit/mojo/issues/313 |
| 242 mutable Mutex mutex_; |
| 239 | 243 |
| 240 scoped_ptr<RawChannel> raw_channel_; | 244 scoped_ptr<RawChannel> raw_channel_ MOJO_GUARDED_BY(mutex_); |
| 241 bool is_running_; | 245 bool is_running_ MOJO_GUARDED_BY(mutex_); |
| 242 // Set when |WillShutdownSoon()| is called. | 246 // Set when |WillShutdownSoon()| is called. |
| 243 bool is_shutting_down_; | 247 bool is_shutting_down_ MOJO_GUARDED_BY(mutex_); |
| 244 | 248 |
| 245 // Has a reference to us. | 249 // Has a reference to us. |
| 246 ChannelManager* channel_manager_; | 250 ChannelManager* channel_manager_ MOJO_GUARDED_BY(mutex_); |
| 247 | 251 |
| 248 using IdToEndpointMap = | 252 using IdToEndpointMap = |
| 249 base::hash_map<ChannelEndpointId, scoped_refptr<ChannelEndpoint>>; | 253 base::hash_map<ChannelEndpointId, scoped_refptr<ChannelEndpoint>>; |
| 250 // Map from local IDs to endpoints. If the endpoint is null, this means that | 254 // Map from local IDs to endpoints. If the endpoint is null, this means that |
| 251 // we're just waiting for the remove ack before removing the entry. | 255 // we're just waiting for the remove ack before removing the entry. |
| 252 IdToEndpointMap local_id_to_endpoint_map_; | 256 IdToEndpointMap local_id_to_endpoint_map_ MOJO_GUARDED_BY(mutex_); |
| 253 // Note: The IDs generated by this should be checked for existence before use. | 257 // Note: The IDs generated by this should be checked for existence before use. |
| 254 LocalChannelEndpointIdGenerator local_id_generator_; | 258 LocalChannelEndpointIdGenerator local_id_generator_ MOJO_GUARDED_BY(mutex_); |
| 255 | 259 |
| 256 using IdToIncomingEndpointMap = | 260 using IdToIncomingEndpointMap = |
| 257 base::hash_map<ChannelEndpointId, scoped_refptr<IncomingEndpoint>>; | 261 base::hash_map<ChannelEndpointId, scoped_refptr<IncomingEndpoint>>; |
| 258 // Map from local IDs to incoming endpoints (i.e., those received inside other | 262 // Map from local IDs to incoming endpoints (i.e., those received inside other |
| 259 // messages, but not yet claimed via |DeserializeEndpoint()|). | 263 // messages, but not yet claimed via |DeserializeEndpoint()|). |
| 260 IdToIncomingEndpointMap incoming_endpoints_; | 264 IdToIncomingEndpointMap incoming_endpoints_ MOJO_GUARDED_BY(mutex_); |
| 261 // TODO(vtl): We need to keep track of remote IDs (so that we don't collide | 265 // TODO(vtl): We need to keep track of remote IDs (so that we don't collide |
| 262 // if/when we wrap). | 266 // if/when we wrap). |
| 263 RemoteChannelEndpointIdGenerator remote_id_generator_; | 267 RemoteChannelEndpointIdGenerator remote_id_generator_ MOJO_GUARDED_BY(mutex_); |
| 264 | 268 |
| 265 MOJO_DISALLOW_COPY_AND_ASSIGN(Channel); | 269 MOJO_DISALLOW_COPY_AND_ASSIGN(Channel); |
| 266 }; | 270 }; |
| 267 | 271 |
| 268 } // namespace system | 272 } // namespace system |
| 269 } // namespace mojo | 273 } // namespace mojo |
| 270 | 274 |
| 271 #endif // MOJO_EDK_SYSTEM_CHANNEL_H_ | 275 #endif // MOJO_EDK_SYSTEM_CHANNEL_H_ |
| OLD | NEW |