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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 | 73 |
74 // TODO(vtl): Remove these once |AttachAndRunEndpoint()| is fully implemented | 74 // TODO(vtl): Remove these once |AttachAndRunEndpoint()| is fully implemented |
75 // and everything is converted to use it. | 75 // and everything is converted to use it. |
76 ChannelEndpointId AttachEndpoint(scoped_refptr<ChannelEndpoint> endpoint); | 76 ChannelEndpointId AttachEndpoint(scoped_refptr<ChannelEndpoint> endpoint); |
77 void RunEndpoint(scoped_refptr<ChannelEndpoint> endpoint, | 77 void RunEndpoint(scoped_refptr<ChannelEndpoint> endpoint, |
78 ChannelEndpointId remote_id); | 78 ChannelEndpointId remote_id); |
79 | 79 |
80 // Attaches the given endpoint to this channel and runs it. |is_bootstrap| | 80 // Attaches the given endpoint to this channel and runs it. |is_bootstrap| |
81 // should be set if and only if it is the first endpoint on the channel. This | 81 // should be set if and only if it is the first endpoint on the channel. This |
82 // assigns the endpoint both local and remote IDs. If |is_bootstrap| is set, | 82 // assigns the endpoint both local and remote IDs. If |is_bootstrap| is set, |
83 // both are the bootstrap ID (given by |ChannelEndpointId::GetBootstrap()|). | 83 // both are the bootstrap ID (given by |ChannelEndpointId::GetBootstrap()|); |
| 84 // if not, it will also send a |kSubtypeChannelAttachAndRunEndpoint| message |
| 85 // to the remote side to tell it to create an endpoint as well. |
84 // | 86 // |
85 // (Bootstrapping is symmetric: Both sides attach and run endpoints with | 87 // (Bootstrapping is symmetric: Both sides attach and run endpoints with |
86 // |is_bootstrap| set, which establishes the first message pipe across a | 88 // |is_bootstrap| set, which establishes the first message pipe across a |
87 // channel.) | 89 // channel.) |
88 // | 90 // |
| 91 // This returns the *remote* ID (which will be the bootstrap ID in the |
| 92 // bootstrap case, and a "remote ID", i.e., one for which |is_remote()| |
| 93 // returns true, otherwise). |
| 94 // |
89 // TODO(vtl): Maybe limit the number of attached message pipes. | 95 // TODO(vtl): Maybe limit the number of attached message pipes. |
90 void AttachAndRunEndpoint(scoped_refptr<ChannelEndpoint> endpoint, | 96 ChannelEndpointId AttachAndRunEndpoint( |
91 bool is_bootstrap); | 97 scoped_refptr<ChannelEndpoint> endpoint, |
92 | 98 bool is_bootstrap); |
93 // Tells the other side of the channel to run a message pipe endpoint (which | |
94 // must already be attached); |local_id| and |remote_id| are relative to this | |
95 // channel (i.e., |local_id| is the other side's remote ID and |remote_id| is | |
96 // its local ID). | |
97 // TODO(vtl): Maybe we should just have a flag argument to | |
98 // |RunMessagePipeEndpoint()| that tells it to do this. | |
99 void RunRemoteMessagePipeEndpoint(ChannelEndpointId local_id, | |
100 ChannelEndpointId remote_id); | |
101 | 99 |
102 // This forwards |message| verbatim to |raw_channel_|. | 100 // This forwards |message| verbatim to |raw_channel_|. |
103 bool WriteMessage(scoped_ptr<MessageInTransit> message); | 101 bool WriteMessage(scoped_ptr<MessageInTransit> message); |
104 | 102 |
105 // See |RawChannel::IsWriteBufferEmpty()|. | 103 // See |RawChannel::IsWriteBufferEmpty()|. |
106 // TODO(vtl): Maybe we shouldn't expose this, and instead have a | 104 // TODO(vtl): Maybe we shouldn't expose this, and instead have a |
107 // |FlushWriteBufferAndShutdown()| or something like that. | 105 // |FlushWriteBufferAndShutdown()| or something like that. |
108 bool IsWriteBufferEmpty(); | 106 bool IsWriteBufferEmpty(); |
109 | 107 |
110 // This removes the given endpoint from this channel (|local_id| and | 108 // Removes the given endpoint from this channel (|local_id| and |remote_id| |
111 // |remote_id| are specified as an optimization; the latter should be an | 109 // are specified as an optimization; the latter should be an invalid |
112 // invalid |ChannelEndpointId| if the endpoint is not yet running). Note: If | 110 // |ChannelEndpointId| if the endpoint is not yet running). Note: If this is |
113 // this is called, the |Channel| will *not* call | 111 // called, the |Channel| will *not* call |
114 // |ChannelEndpoint::DetachFromChannel()|. | 112 // |ChannelEndpoint::DetachFromChannel()|. |
115 void DetachEndpoint(ChannelEndpoint* endpoint, | 113 void DetachEndpoint(ChannelEndpoint* endpoint, |
116 ChannelEndpointId local_id, | 114 ChannelEndpointId local_id, |
117 ChannelEndpointId remote_id); | 115 ChannelEndpointId remote_id); |
118 | 116 |
| 117 // Takes ownership of an incoming message pipe (i.e., one that was created via |
| 118 // a |kSubtypeChannelAttachAndRunEndpoint| message). |
| 119 scoped_refptr<MessagePipe> PassIncomingMessagePipe( |
| 120 ChannelEndpointId local_id); |
| 121 |
119 // See |RawChannel::GetSerializedPlatformHandleSize()|. | 122 // See |RawChannel::GetSerializedPlatformHandleSize()|. |
120 size_t GetSerializedPlatformHandleSize() const; | 123 size_t GetSerializedPlatformHandleSize() const; |
121 | 124 |
122 embedder::PlatformSupport* platform_support() const { | 125 embedder::PlatformSupport* platform_support() const { |
123 return platform_support_; | 126 return platform_support_; |
124 } | 127 } |
125 | 128 |
126 private: | 129 private: |
127 friend class base::RefCountedThreadSafe<Channel>; | 130 friend class base::RefCountedThreadSafe<Channel>; |
128 virtual ~Channel(); | 131 virtual ~Channel(); |
129 | 132 |
130 // |RawChannel::Delegate| implementation (only called on the creation thread): | 133 // |RawChannel::Delegate| implementation (only called on the creation thread): |
131 virtual void OnReadMessage( | 134 virtual void OnReadMessage( |
132 const MessageInTransit::View& message_view, | 135 const MessageInTransit::View& message_view, |
133 embedder::ScopedPlatformHandleVectorPtr platform_handles) override; | 136 embedder::ScopedPlatformHandleVectorPtr platform_handles) override; |
134 virtual void OnError(Error error) override; | 137 virtual void OnError(Error error) override; |
135 | 138 |
136 // Helpers for |OnReadMessage| (only called on the creation thread): | 139 // Helpers for |OnReadMessage| (only called on the creation thread): |
137 void OnReadMessageForDownstream( | 140 void OnReadMessageForDownstream( |
138 const MessageInTransit::View& message_view, | 141 const MessageInTransit::View& message_view, |
139 embedder::ScopedPlatformHandleVectorPtr platform_handles); | 142 embedder::ScopedPlatformHandleVectorPtr platform_handles); |
140 void OnReadMessageForChannel( | 143 void OnReadMessageForChannel( |
141 const MessageInTransit::View& message_view, | 144 const MessageInTransit::View& message_view, |
142 embedder::ScopedPlatformHandleVectorPtr platform_handles); | 145 embedder::ScopedPlatformHandleVectorPtr platform_handles); |
143 | 146 |
144 // Handles "run message pipe endpoint" messages. | 147 // Handles "attach and run endpoint" messages. |
145 bool OnRunMessagePipeEndpoint(ChannelEndpointId local_id, | 148 bool OnAttachAndRunEndpoint(ChannelEndpointId local_id, |
146 ChannelEndpointId remote_id); | 149 ChannelEndpointId remote_id); |
147 // Handles "remove message pipe endpoint" messages. | 150 // Handles "remove message pipe endpoint" messages. |
148 bool OnRemoveMessagePipeEndpoint(ChannelEndpointId local_id, | 151 bool OnRemoveMessagePipeEndpoint(ChannelEndpointId local_id, |
149 ChannelEndpointId remote_id); | 152 ChannelEndpointId remote_id); |
150 // Handles "remove message pipe endpoint ack" messages. | 153 // Handles "remove message pipe endpoint ack" messages. |
151 bool OnRemoveMessagePipeEndpointAck(ChannelEndpointId local_id); | 154 bool OnRemoveMessagePipeEndpointAck(ChannelEndpointId local_id); |
152 | 155 |
153 // Handles errors (e.g., invalid messages) from the remote side. Callable from | 156 // Handles errors (e.g., invalid messages) from the remote side. Callable from |
154 // any thread. | 157 // any thread. |
155 void HandleRemoteError(const base::StringPiece& error_message); | 158 void HandleRemoteError(const base::StringPiece& error_message); |
156 // Handles internal errors/failures from the local side. Callable from any | 159 // Handles internal errors/failures from the local side. Callable from any |
(...skipping 22 matching lines...) Expand all Loading... |
179 bool is_shutting_down_; | 182 bool is_shutting_down_; |
180 | 183 |
181 typedef base::hash_map<ChannelEndpointId, scoped_refptr<ChannelEndpoint>> | 184 typedef base::hash_map<ChannelEndpointId, scoped_refptr<ChannelEndpoint>> |
182 IdToEndpointMap; | 185 IdToEndpointMap; |
183 // Map from local IDs to endpoints. If the endpoint is null, this means that | 186 // Map from local IDs to endpoints. If the endpoint is null, this means that |
184 // we're just waiting for the remove ack before removing the entry. | 187 // we're just waiting for the remove ack before removing the entry. |
185 IdToEndpointMap local_id_to_endpoint_map_; | 188 IdToEndpointMap local_id_to_endpoint_map_; |
186 // Note: The IDs generated by this should be checked for existence before use. | 189 // Note: The IDs generated by this should be checked for existence before use. |
187 LocalChannelEndpointIdGenerator local_id_generator_; | 190 LocalChannelEndpointIdGenerator local_id_generator_; |
188 | 191 |
| 192 typedef base::hash_map<ChannelEndpointId, scoped_refptr<MessagePipe>> |
| 193 IdToMessagePipeMap; |
| 194 // Map from local IDs to pending/incoming endpoints (i.e., those which do not |
| 195 // yet have a dispatcher attached). |
| 196 IdToMessagePipeMap incoming_message_pipes_; |
189 // TODO(vtl): We need to keep track of remote IDs (so that we don't collide | 197 // TODO(vtl): We need to keep track of remote IDs (so that we don't collide |
190 // if/when we wrap). | 198 // if/when we wrap). |
191 RemoteChannelEndpointIdGenerator remote_id_generator_; | 199 RemoteChannelEndpointIdGenerator remote_id_generator_; |
192 | 200 |
193 DISALLOW_COPY_AND_ASSIGN(Channel); | 201 DISALLOW_COPY_AND_ASSIGN(Channel); |
194 }; | 202 }; |
195 | 203 |
196 } // namespace system | 204 } // namespace system |
197 } // namespace mojo | 205 } // namespace mojo |
198 | 206 |
199 #endif // MOJO_EDK_SYSTEM_CHANNEL_H_ | 207 #endif // MOJO_EDK_SYSTEM_CHANNEL_H_ |
OLD | NEW |