| 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_SYSTEM_CHANNEL_H_ | 5 #ifndef MOJO_SYSTEM_CHANNEL_H_ |
| 6 #define MOJO_SYSTEM_CHANNEL_H_ | 6 #define MOJO_SYSTEM_CHANNEL_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 11 #include "base/containers/hash_tables.h" | 11 #include "base/containers/hash_tables.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/strings/string_piece.h" | 15 #include "base/strings/string_piece.h" |
| 16 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
| 17 #include "base/threading/thread_checker.h" | 17 #include "base/threading/thread_checker.h" |
| 18 #include "mojo/embedder/scoped_platform_handle.h" | 18 #include "mojo/embedder/scoped_platform_handle.h" |
| 19 #include "mojo/public/c/system/types.h" | 19 #include "mojo/public/c/system/types.h" |
| 20 #include "mojo/system/message_in_transit.h" | 20 #include "mojo/system/message_in_transit.h" |
| 21 #include "mojo/system/message_pipe.h" | 21 #include "mojo/system/message_pipe.h" |
| 22 #include "mojo/system/raw_channel.h" | 22 #include "mojo/system/raw_channel.h" |
| 23 #include "mojo/system/system_impl_export.h" | 23 #include "mojo/system/system_impl_export.h" |
| 24 | 24 |
| 25 namespace mojo { | 25 namespace mojo { |
| 26 |
| 27 namespace embedder { |
| 28 class PlatformSupport; |
| 29 } |
| 30 |
| 26 namespace system { | 31 namespace system { |
| 27 | 32 |
| 28 // This class is mostly thread-safe. It must be created on an I/O thread. | 33 // This class is mostly thread-safe. It must be created on an I/O thread. |
| 29 // |Init()| must be called on that same thread before it becomes thread-safe (in | 34 // |Init()| must be called on that same thread before it becomes thread-safe (in |
| 30 // particular, before references are given to any other thread) and |Shutdown()| | 35 // particular, before references are given to any other thread) and |Shutdown()| |
| 31 // must be called on that same thread before destruction. Its public methods are | 36 // must be called on that same thread before destruction. Its public methods are |
| 32 // otherwise thread-safe. It may be destroyed on any thread, in the sense that | 37 // otherwise thread-safe. It may be destroyed on any thread, in the sense that |
| 33 // the last reference to it may be released on any thread, with the proviso that | 38 // the last reference to it may be released on any thread, with the proviso that |
| 34 // |Shutdown()| must have been called first (so the pattern is that a "main" | 39 // |Shutdown()| must have been called first (so the pattern is that a "main" |
| 35 // reference is kept on its creation thread and is released after |Shutdown()| | 40 // reference is kept on its creation thread and is released after |Shutdown()| |
| (...skipping 12 matching lines...) Expand all Loading... |
| 48 // by |Channel| must be removed by calling |DetachMessagePipeEndpoint()| (which | 53 // by |Channel| must be removed by calling |DetachMessagePipeEndpoint()| (which |
| 49 // is done by |MessagePipe|/|ProxyMessagePipeEndpoint|, which simultaneously | 54 // is done by |MessagePipe|/|ProxyMessagePipeEndpoint|, which simultaneously |
| 50 // removes its reference to |Channel|). | 55 // removes its reference to |Channel|). |
| 51 class MOJO_SYSTEM_IMPL_EXPORT Channel | 56 class MOJO_SYSTEM_IMPL_EXPORT Channel |
| 52 : public base::RefCountedThreadSafe<Channel>, | 57 : public base::RefCountedThreadSafe<Channel>, |
| 53 public RawChannel::Delegate { | 58 public RawChannel::Delegate { |
| 54 public: | 59 public: |
| 55 // The first message pipe endpoint attached will have this as its local ID. | 60 // The first message pipe endpoint attached will have this as its local ID. |
| 56 static const MessageInTransit::EndpointId kBootstrapEndpointId = 1; | 61 static const MessageInTransit::EndpointId kBootstrapEndpointId = 1; |
| 57 | 62 |
| 58 Channel(); | 63 // |platform_support| (typically owned by |Core|) must remain alive until |
| 64 // after |Shutdown()| is called. |
| 65 explicit Channel(embedder::PlatformSupport* platform_support); |
| 59 | 66 |
| 60 // This must be called on the creation thread before any other methods are | 67 // This must be called on the creation thread before any other methods are |
| 61 // called, and before references to this object are given to any other | 68 // called, and before references to this object are given to any other |
| 62 // threads. |raw_channel| should be uninitialized. Returns true on success. On | 69 // threads. |raw_channel| should be uninitialized. Returns true on success. On |
| 63 // failure, no other methods should be called (including |Shutdown()|). | 70 // failure, no other methods should be called (including |Shutdown()|). |
| 64 bool Init(scoped_ptr<RawChannel> raw_channel); | 71 bool Init(scoped_ptr<RawChannel> raw_channel); |
| 65 | 72 |
| 66 // This must be called on the creation thread before destruction (which can | 73 // This must be called on the creation thread before destruction (which can |
| 67 // happen on any thread). | 74 // happen on any thread). |
| 68 void Shutdown(); | 75 void Shutdown(); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 // This removes the message pipe/port's endpoint (with the given local ID and | 120 // This removes the message pipe/port's endpoint (with the given local ID and |
| 114 // given remote ID, which should be |kInvalidEndpointId| if not yet running), | 121 // given remote ID, which should be |kInvalidEndpointId| if not yet running), |
| 115 // returned by |AttachMessagePipeEndpoint()| from this channel. After this is | 122 // returned by |AttachMessagePipeEndpoint()| from this channel. After this is |
| 116 // called, |local_id| may be reused for another message pipe. | 123 // called, |local_id| may be reused for another message pipe. |
| 117 void DetachMessagePipeEndpoint(MessageInTransit::EndpointId local_id, | 124 void DetachMessagePipeEndpoint(MessageInTransit::EndpointId local_id, |
| 118 MessageInTransit::EndpointId remote_id); | 125 MessageInTransit::EndpointId remote_id); |
| 119 | 126 |
| 120 // See |RawChannel::GetSerializedPlatformHandleSize()|. | 127 // See |RawChannel::GetSerializedPlatformHandleSize()|. |
| 121 size_t GetSerializedPlatformHandleSize() const; | 128 size_t GetSerializedPlatformHandleSize() const; |
| 122 | 129 |
| 130 embedder::PlatformSupport* platform_support() const { |
| 131 return platform_support_; |
| 132 } |
| 133 |
| 123 private: | 134 private: |
| 124 struct EndpointInfo { | 135 struct EndpointInfo { |
| 125 enum State { | 136 enum State { |
| 126 // Attached, possibly running or not. | 137 // Attached, possibly running or not. |
| 127 STATE_NORMAL, | 138 STATE_NORMAL, |
| 128 // "Zombie" states: | 139 // "Zombie" states: |
| 129 // Waiting for |DetachMessagePipeEndpoint()| before removing. | 140 // Waiting for |DetachMessagePipeEndpoint()| before removing. |
| 130 STATE_WAIT_LOCAL_DETACH, | 141 STATE_WAIT_LOCAL_DETACH, |
| 131 // Waiting for a |kSubtypeChannelRemoveMessagePipeEndpointAck| before | 142 // Waiting for a |kSubtypeChannelRemoveMessagePipeEndpointAck| before |
| 132 // removing. | 143 // removing. |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 void HandleLocalError(const base::StringPiece& error_message); | 184 void HandleLocalError(const base::StringPiece& error_message); |
| 174 | 185 |
| 175 // Helper to send channel control messages. Returns true on success. Should be | 186 // Helper to send channel control messages. Returns true on success. Should be |
| 176 // called *without* |lock_| held. | 187 // called *without* |lock_| held. |
| 177 bool SendControlMessage(MessageInTransit::Subtype subtype, | 188 bool SendControlMessage(MessageInTransit::Subtype subtype, |
| 178 MessageInTransit::EndpointId source_id, | 189 MessageInTransit::EndpointId source_id, |
| 179 MessageInTransit::EndpointId destination_id); | 190 MessageInTransit::EndpointId destination_id); |
| 180 | 191 |
| 181 base::ThreadChecker creation_thread_checker_; | 192 base::ThreadChecker creation_thread_checker_; |
| 182 | 193 |
| 194 embedder::PlatformSupport* const platform_support_; |
| 195 |
| 183 // Note: |MessagePipe|s MUST NOT be used under |lock_|. I.e., |lock_| can only | 196 // Note: |MessagePipe|s MUST NOT be used under |lock_|. I.e., |lock_| can only |
| 184 // be acquired after |MessagePipe::lock_|, never before. Thus to call into a | 197 // be acquired after |MessagePipe::lock_|, never before. Thus to call into a |
| 185 // |MessagePipe|, a reference should be acquired from | 198 // |MessagePipe|, a reference should be acquired from |
| 186 // |local_id_to_endpoint_info_map_| under |lock_| (e.g., by copying the | 199 // |local_id_to_endpoint_info_map_| under |lock_| (e.g., by copying the |
| 187 // |EndpointInfo|) and then the lock released. | 200 // |EndpointInfo|) and then the lock released. |
| 188 base::Lock lock_; // Protects the members below. | 201 base::Lock lock_; // Protects the members below. |
| 189 | 202 |
| 190 scoped_ptr<RawChannel> raw_channel_; | 203 scoped_ptr<RawChannel> raw_channel_; |
| 191 bool is_running_; | 204 bool is_running_; |
| 192 // Set when |WillShutdownSoon()| is called. | 205 // Set when |WillShutdownSoon()| is called. |
| 193 bool is_shutting_down_; | 206 bool is_shutting_down_; |
| 194 | 207 |
| 195 typedef base::hash_map<MessageInTransit::EndpointId, EndpointInfo> | 208 typedef base::hash_map<MessageInTransit::EndpointId, EndpointInfo> |
| 196 IdToEndpointInfoMap; | 209 IdToEndpointInfoMap; |
| 197 IdToEndpointInfoMap local_id_to_endpoint_info_map_; | 210 IdToEndpointInfoMap local_id_to_endpoint_info_map_; |
| 198 // The next local ID to try (when allocating new local IDs). Note: It should | 211 // The next local ID to try (when allocating new local IDs). Note: It should |
| 199 // be checked for existence before use. | 212 // be checked for existence before use. |
| 200 MessageInTransit::EndpointId next_local_id_; | 213 MessageInTransit::EndpointId next_local_id_; |
| 201 | 214 |
| 202 DISALLOW_COPY_AND_ASSIGN(Channel); | 215 DISALLOW_COPY_AND_ASSIGN(Channel); |
| 203 }; | 216 }; |
| 204 | 217 |
| 205 } // namespace system | 218 } // namespace system |
| 206 } // namespace mojo | 219 } // namespace mojo |
| 207 | 220 |
| 208 #endif // MOJO_SYSTEM_CHANNEL_H_ | 221 #endif // MOJO_SYSTEM_CHANNEL_H_ |
| OLD | NEW |