| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_ENDPOINT_H_ | 5 #ifndef MOJO_EDK_SYSTEM_CHANNEL_ENDPOINT_H_ |
| 6 #define MOJO_EDK_SYSTEM_CHANNEL_ENDPOINT_H_ | 6 #define MOJO_EDK_SYSTEM_CHANNEL_ENDPOINT_H_ |
| 7 | 7 |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/synchronization/lock.h" | 11 #include "base/synchronization/lock.h" |
| 12 #include "mojo/edk/embedder/platform_handle_vector.h" | 12 #include "mojo/edk/embedder/platform_handle_vector.h" |
| 13 #include "mojo/edk/system/channel_endpoint_id.h" | 13 #include "mojo/edk/system/channel_endpoint_id.h" |
| 14 #include "mojo/edk/system/message_in_transit.h" |
| 14 #include "mojo/edk/system/message_in_transit_queue.h" | 15 #include "mojo/edk/system/message_in_transit_queue.h" |
| 15 #include "mojo/edk/system/system_impl_export.h" | 16 #include "mojo/edk/system/system_impl_export.h" |
| 16 | 17 |
| 17 namespace mojo { | 18 namespace mojo { |
| 18 namespace system { | 19 namespace system { |
| 19 | 20 |
| 20 class Channel; | 21 class Channel; |
| 22 class ChannelEndpointClient; |
| 21 class MessageInTransit; | 23 class MessageInTransit; |
| 22 class MessagePipe; | |
| 23 | 24 |
| 24 // TODO(vtl): The plan: | 25 // TODO(vtl): The plan: |
| 25 // - (Done.) Move |Channel::Endpoint| to |ChannelEndpoint|. Make it | 26 // - (Done.) Move |Channel::Endpoint| to |ChannelEndpoint|. Make it |
| 26 // refcounted, and not copyable. Make |Channel| a friend. Make things work. | 27 // refcounted, and not copyable. Make |Channel| a friend. Make things work. |
| 27 // - (Done.) Give |ChannelEndpoint| a lock. The lock order (in order of | 28 // - (Done.) Give |ChannelEndpoint| a lock. The lock order (in order of |
| 28 // allowable acquisition) is: |MessagePipe|, |ChannelEndpoint|, |Channel|. | 29 // allowable acquisition) is: |MessagePipe|, |ChannelEndpoint|, |Channel|. |
| 29 // - (Done) Stop having |Channel| as a friend. | 30 // - (Done) Stop having |Channel| as a friend. |
| 30 // - (Done) Move logic from |ProxyMessagePipeEndpoint| into |ChannelEndpoint|. | 31 // - (Done) Move logic from |ProxyMessagePipeEndpoint| into |ChannelEndpoint|. |
| 31 // Right now, we have to go through lots of contortions to manipulate state | 32 // Right now, we have to go through lots of contortions to manipulate state |
| 32 // owned by |ProxyMessagePipeEndpoint| (in particular, |Channel::Endpoint| | 33 // owned by |ProxyMessagePipeEndpoint| (in particular, |Channel::Endpoint| |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 // running since under the current scheme it wouldn't have a remote ID | 106 // running since under the current scheme it wouldn't have a remote ID |
| 106 // yet.] | 107 // yet.] |
| 107 // - Note that even if the local side is closed, it may still receive a | 108 // - Note that even if the local side is closed, it may still receive a |
| 108 // "remove" message from the other side (if the other side is closed | 109 // "remove" message from the other side (if the other side is closed |
| 109 // simultaneously, and both sides send "remove" messages). In that | 110 // simultaneously, and both sides send "remove" messages). In that |
| 110 // case, it must still remain alive until it receives the "remove | 111 // case, it must still remain alive until it receives the "remove |
| 111 // ack" (and it must ack the "remove" message that it received). | 112 // ack" (and it must ack the "remove" message that it received). |
| 112 class MOJO_SYSTEM_IMPL_EXPORT ChannelEndpoint | 113 class MOJO_SYSTEM_IMPL_EXPORT ChannelEndpoint |
| 113 : public base::RefCountedThreadSafe<ChannelEndpoint> { | 114 : public base::RefCountedThreadSafe<ChannelEndpoint> { |
| 114 public: | 115 public: |
| 115 // Constructor for a |ChannelEndpoint| attached to the given message pipe | 116 // Constructor for a |ChannelEndpoint| with the given client (specified by |
| 116 // endpoint (specified by |message_pipe| and |port|). Optionally takes | 117 // |client| and |client_port|). Optionally takes messages from |
| 117 // messages from |*message_queue| if |message_queue| is non-null. | 118 // |*message_queue| if |message_queue| is non-null. |
| 118 // | 119 // |
| 119 // |message_pipe| may be null if this endpoint will never need to receive | 120 // |client| may be null if this endpoint will never need to receive messages, |
| 120 // messages, in which case |message_queue| should not be null. In that case, | 121 // in which case |message_queue| should not be null. In that case, this |
| 121 // this endpoint will simply send queued messages upon being attached to a | 122 // endpoint will simply send queued messages upon being attached to a |
| 122 // |Channel| and immediately detach itself. | 123 // |Channel| and immediately detach itself. |
| 123 ChannelEndpoint(MessagePipe* message_pipe, | 124 ChannelEndpoint(ChannelEndpointClient* client, |
| 124 unsigned port, | 125 unsigned client_port, |
| 125 MessageInTransitQueue* message_queue = nullptr); | 126 MessageInTransitQueue* message_queue = nullptr); |
| 126 | 127 |
| 127 // Methods called by |MessagePipe| (via |ProxyMessagePipeEndpoint|): | 128 // Methods called by |ChannelEndpointClient|: |
| 128 | 129 |
| 129 // TODO(vtl): This currently only works if we're "running". We'll move the | 130 // TODO(vtl): This currently only works if we're "running". We'll move the |
| 130 // "paused message queue" here (will this be needed when we have | 131 // "paused message queue" here (will this be needed when we have |
| 131 // locally-allocated remote IDs?). | 132 // locally-allocated remote IDs?). |
| 132 bool EnqueueMessage(scoped_ptr<MessageInTransit> message); | 133 bool EnqueueMessage(scoped_ptr<MessageInTransit> message); |
| 133 | 134 |
| 134 void DetachFromMessagePipe(); | 135 void DetachFromClient(); |
| 135 | 136 |
| 136 // Methods called by |Channel|: | 137 // Methods called by |Channel|: |
| 137 | 138 |
| 138 // Called by |Channel| when it takes a reference to this object. It will send | 139 // Called by |Channel| when it takes a reference to this object. It will send |
| 139 // all queue messages (in |paused_message_queue_|). | 140 // all queue messages (in |paused_message_queue_|). |
| 140 // TODO(vtl): Maybe rename this "OnAttach"? | 141 // TODO(vtl): Maybe rename this "OnAttach"? |
| 141 void AttachAndRun(Channel* channel, | 142 void AttachAndRun(Channel* channel, |
| 142 ChannelEndpointId local_id, | 143 ChannelEndpointId local_id, |
| 143 ChannelEndpointId remote_id); | 144 ChannelEndpointId remote_id); |
| 144 | 145 |
| 145 // Called by |Channel| when it receives a message for the message pipe. | 146 // Called by |Channel| when it receives a message for the |ChannelEndpoint|. |
| 146 bool OnReadMessage(const MessageInTransit::View& message_view, | 147 bool OnReadMessage(const MessageInTransit::View& message_view, |
| 147 embedder::ScopedPlatformHandleVectorPtr platform_handles); | 148 embedder::ScopedPlatformHandleVectorPtr platform_handles); |
| 148 | 149 |
| 149 // Called by |Channel| before it gives up its reference to this object. | 150 // Called by |Channel| before it gives up its reference to this object. |
| 150 void DetachFromChannel(); | 151 void DetachFromChannel(); |
| 151 | 152 |
| 152 private: | 153 private: |
| 153 friend class base::RefCountedThreadSafe<ChannelEndpoint>; | 154 friend class base::RefCountedThreadSafe<ChannelEndpoint>; |
| 154 ~ChannelEndpoint(); | 155 ~ChannelEndpoint(); |
| 155 | 156 |
| 156 // Must be called with |lock_| held. | 157 // Must be called with |lock_| held. |
| 157 bool WriteMessageNoLock(scoped_ptr<MessageInTransit> message); | 158 bool WriteMessageNoLock(scoped_ptr<MessageInTransit> message); |
| 158 | 159 |
| 159 // Protects the members below. | 160 // Protects the members below. |
| 160 base::Lock lock_; | 161 base::Lock lock_; |
| 161 | 162 |
| 162 // |message_pipe_| must be valid whenever it is non-null. Before | 163 // |client_| must be valid whenever it is non-null. Before |*client_| gives up |
| 163 // |*message_pipe_| gives up its reference to this object, it must call | 164 // its reference to this object, it must call |DetachFromClient()|. |
| 164 // |DetachFromMessagePipe()|. | |
| 165 // NOTE: This is a |scoped_refptr<>|, rather than a raw pointer, since the | 165 // NOTE: This is a |scoped_refptr<>|, rather than a raw pointer, since the |
| 166 // |Channel| needs to keep the |MessagePipe| alive for the "proxy-proxy" case. | 166 // |Channel| needs to keep the |MessagePipe| alive for the "proxy-proxy" case. |
| 167 // Possibly we'll be able to eliminate that case when we have full | 167 // Possibly we'll be able to eliminate that case when we have full |
| 168 // multiprocess support. | 168 // multiprocess support. |
| 169 // WARNING: |MessagePipe| methods must not be called under |lock_|. Thus to | 169 // WARNING: |ChannelEndpointClient| methods must not be called under |lock_|. |
| 170 // make such a call, a reference must first be taken under |lock_| and the | 170 // Thus to make such a call, a reference must first be taken under |lock_| and |
| 171 // lock released. | 171 // the lock released. |
| 172 scoped_refptr<MessagePipe> message_pipe_; | 172 scoped_refptr<ChannelEndpointClient> client_; |
| 173 unsigned port_; | 173 unsigned client_port_; |
| 174 | 174 |
| 175 // |channel_| must be valid whenever it is non-null. Before |*channel_| gives | 175 // |channel_| must be valid whenever it is non-null. Before |*channel_| gives |
| 176 // up its reference to this object, it must call |DetachFromChannel()|. | 176 // up its reference to this object, it must call |DetachFromChannel()|. |
| 177 Channel* channel_; | 177 Channel* channel_; |
| 178 ChannelEndpointId local_id_; | 178 ChannelEndpointId local_id_; |
| 179 ChannelEndpointId remote_id_; | 179 ChannelEndpointId remote_id_; |
| 180 | 180 |
| 181 // This queue is used before we're running on a channel and ready to send | 181 // This queue is used before we're running on a channel and ready to send |
| 182 // messages. | 182 // messages. |
| 183 MessageInTransitQueue paused_message_queue_; | 183 MessageInTransitQueue paused_message_queue_; |
| 184 | 184 |
| 185 DISALLOW_COPY_AND_ASSIGN(ChannelEndpoint); | 185 DISALLOW_COPY_AND_ASSIGN(ChannelEndpoint); |
| 186 }; | 186 }; |
| 187 | 187 |
| 188 } // namespace system | 188 } // namespace system |
| 189 } // namespace mojo | 189 } // namespace mojo |
| 190 | 190 |
| 191 #endif // MOJO_EDK_SYSTEM_CHANNEL_ENDPOINT_H_ | 191 #endif // MOJO_EDK_SYSTEM_CHANNEL_ENDPOINT_H_ |
| OLD | NEW |