Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(78)

Side by Side Diff: mojo/edk/system/channel.h

Issue 1423713009: EDK: Move ref counting classes to mojo/edk/util. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/edk/system/BUILD.gn ('k') | mojo/edk/system/channel.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <memory> 10 #include <memory>
11 #include <unordered_map> 11 #include <unordered_map>
12 12
13 #include "base/threading/thread_checker.h" 13 #include "base/threading/thread_checker.h"
14 #include "mojo/edk/embedder/scoped_platform_handle.h" 14 #include "mojo/edk/embedder/scoped_platform_handle.h"
15 #include "mojo/edk/system/channel_endpoint.h" 15 #include "mojo/edk/system/channel_endpoint.h"
16 #include "mojo/edk/system/channel_endpoint_id.h" 16 #include "mojo/edk/system/channel_endpoint_id.h"
17 #include "mojo/edk/system/incoming_endpoint.h" 17 #include "mojo/edk/system/incoming_endpoint.h"
18 #include "mojo/edk/system/message_in_transit.h" 18 #include "mojo/edk/system/message_in_transit.h"
19 #include "mojo/edk/system/mutex.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/ref_counted.h" 21 #include "mojo/edk/util/ref_counted.h"
22 #include "mojo/edk/system/ref_ptr.h" 22 #include "mojo/edk/util/ref_ptr.h"
23 #include "mojo/public/c/system/types.h" 23 #include "mojo/public/c/system/types.h"
24 #include "mojo/public/cpp/system/macros.h" 24 #include "mojo/public/cpp/system/macros.h"
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 {
(...skipping 10 matching lines...) Expand all
43 // thread.) It may be destroyed on any thread, in the sense that the last 43 // thread.) It may be destroyed on any thread, in the sense that the last
44 // reference to it may be released on any thread, with the proviso that 44 // reference to it may be released on any thread, with the proviso that
45 // |Shutdown()| must have been called first (so the pattern is that a "main" 45 // |Shutdown()| must have been called first (so the pattern is that a "main"
46 // reference is kept on its creation thread and is released after |Shutdown()| 46 // reference is kept on its creation thread and is released after |Shutdown()|
47 // is called, but other threads may have temporarily "dangling" references). 47 // is called, but other threads may have temporarily "dangling" references).
48 // 48 //
49 // Note the lock order (in order of allowable acquisition): 49 // Note the lock order (in order of allowable acquisition):
50 // |ChannelEndpointClient| (e.g., |MessagePipe|), |ChannelEndpoint|, |Channel|. 50 // |ChannelEndpointClient| (e.g., |MessagePipe|), |ChannelEndpoint|, |Channel|.
51 // Thus |Channel| may not call into |ChannelEndpoint| with |Channel|'s lock 51 // Thus |Channel| may not call into |ChannelEndpoint| with |Channel|'s lock
52 // held. 52 // held.
53 class Channel final : public RefCountedThreadSafe<Channel>, 53 class Channel final : public util::RefCountedThreadSafe<Channel>,
54 public RawChannel::Delegate { 54 public RawChannel::Delegate {
55 public: 55 public:
56 // Note: Use |MakeRefCounted<Channel>()|. 56 // Note: Use |util::MakeRefCounted<Channel>()|.
57 57
58 // This must be called on the creation thread before any other methods are 58 // 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 59 // called, and before references to this object are given to any other
60 // threads. |raw_channel| should be uninitialized. 60 // threads. |raw_channel| should be uninitialized.
61 void Init(std::unique_ptr<RawChannel> raw_channel) MOJO_NOT_THREAD_SAFE; 61 void Init(std::unique_ptr<RawChannel> raw_channel) MOJO_NOT_THREAD_SAFE;
62 62
63 // Sets the channel manager associated with this channel. This should be set 63 // Sets the channel manager associated with this channel. This should be set
64 // at most once and only called before |WillShutdownSoon()| (and 64 // at most once and only called before |WillShutdownSoon()| (and
65 // |Shutdown()|). (This is called by the channel manager when adding a 65 // |Shutdown()|). (This is called by the channel manager when adding a
66 // channel; this should not be called before the channel is managed by the 66 // channel; this should not be called before the channel is managed by the
(...skipping 11 matching lines...) Expand all
78 // 78 //
79 // If set, the channel manager associated with this channel will be reset. 79 // If set, the channel manager associated with this channel will be reset.
80 void WillShutdownSoon(); 80 void WillShutdownSoon();
81 81
82 // Called to set (i.e., attach and run) the bootstrap (first) endpoint on the 82 // Called to set (i.e., attach and run) the bootstrap (first) endpoint on the
83 // channel. Both the local and remote IDs are the bootstrap ID (given by 83 // channel. Both the local and remote IDs are the bootstrap ID (given by
84 // |ChannelEndpointId::GetBootstrap()|). 84 // |ChannelEndpointId::GetBootstrap()|).
85 // 85 //
86 // (Bootstrapping is symmetric: Both sides call this, which will establish the 86 // (Bootstrapping is symmetric: Both sides call this, which will establish the
87 // first connection across a channel.) 87 // first connection across a channel.)
88 void SetBootstrapEndpoint(RefPtr<ChannelEndpoint>&& endpoint); 88 void SetBootstrapEndpoint(util::RefPtr<ChannelEndpoint>&& endpoint);
89 89
90 // Like |SetBootstrapEndpoint()|, but with explicitly-specified local and 90 // Like |SetBootstrapEndpoint()|, but with explicitly-specified local and
91 // remote IDs. 91 // remote IDs.
92 // 92 //
93 // (Bootstrapping is still symmetric, though the sides should obviously 93 // (Bootstrapping is still symmetric, though the sides should obviously
94 // interchange local and remote IDs. This can be used to allow multiple 94 // interchange local and remote IDs. This can be used to allow multiple
95 // "bootstrap" endpoints, though this is really most useful for testing.) 95 // "bootstrap" endpoints, though this is really most useful for testing.)
96 void SetBootstrapEndpointWithIds(RefPtr<ChannelEndpoint>&& endpoint, 96 void SetBootstrapEndpointWithIds(util::RefPtr<ChannelEndpoint>&& endpoint,
97 ChannelEndpointId local_id, 97 ChannelEndpointId local_id,
98 ChannelEndpointId remote_id); 98 ChannelEndpointId remote_id);
99 99
100 // This forwards |message| verbatim to |raw_channel_|. 100 // This forwards |message| verbatim to |raw_channel_|.
101 bool WriteMessage(std::unique_ptr<MessageInTransit> message); 101 bool WriteMessage(std::unique_ptr<MessageInTransit> message);
102 102
103 // See |RawChannel::IsWriteBufferEmpty()|. 103 // See |RawChannel::IsWriteBufferEmpty()|.
104 // 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
105 // |FlushWriteBufferAndShutdown()| or something like that. 105 // |FlushWriteBufferAndShutdown()| or something like that.
106 bool IsWriteBufferEmpty(); 106 bool IsWriteBufferEmpty();
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 // Case 2: The endpoint's peer is local (i.e., it has a 142 // Case 2: The endpoint's peer is local (i.e., it has a
143 // |ChannelEndpointClient| but no peer |ChannelEndpoint|). 143 // |ChannelEndpointClient| but no peer |ChannelEndpoint|).
144 // 144 //
145 // Case 3: The endpoint's peer is remote (i.e., it has a peer 145 // Case 3: The endpoint's peer is remote (i.e., it has a peer
146 // |ChannelEndpoint|). (This has two subcases: the peer endpoint may be on 146 // |ChannelEndpoint|). (This has two subcases: the peer endpoint may be on
147 // this |Channel| or another |Channel|.) 147 // this |Channel| or another |Channel|.)
148 void SerializeEndpointWithClosedPeer(void* destination, 148 void SerializeEndpointWithClosedPeer(void* destination,
149 MessageInTransitQueue* message_queue); 149 MessageInTransitQueue* message_queue);
150 // This one returns the |ChannelEndpoint| for the serialized endpoint (which 150 // This one returns the |ChannelEndpoint| for the serialized endpoint (which
151 // can be used by, e.g., a |ProxyMessagePipeEndpoint|. 151 // can be used by, e.g., a |ProxyMessagePipeEndpoint|.
152 RefPtr<ChannelEndpoint> SerializeEndpointWithLocalPeer( 152 util::RefPtr<ChannelEndpoint> SerializeEndpointWithLocalPeer(
153 void* destination, 153 void* destination,
154 MessageInTransitQueue* message_queue, 154 MessageInTransitQueue* message_queue,
155 RefPtr<ChannelEndpointClient>&& endpoint_client, 155 util::RefPtr<ChannelEndpointClient>&& endpoint_client,
156 unsigned endpoint_client_port); 156 unsigned endpoint_client_port);
157 void SerializeEndpointWithRemotePeer(void* destination, 157 void SerializeEndpointWithRemotePeer(
158 MessageInTransitQueue* message_queue, 158 void* destination,
159 RefPtr<ChannelEndpoint>&& peer_endpoint); 159 MessageInTransitQueue* message_queue,
160 util::RefPtr<ChannelEndpoint>&& peer_endpoint);
160 161
161 // Deserializes an endpoint that was sent from the peer |Channel| (using 162 // Deserializes an endpoint that was sent from the peer |Channel| (using
162 // |SerializeEndpoint...()|. |source| should be (a copy of) the data that 163 // |SerializeEndpoint...()|. |source| should be (a copy of) the data that
163 // |SerializeEndpoint...()| wrote, and must be (at least) 164 // |SerializeEndpoint...()| wrote, and must be (at least)
164 // |GetSerializedEndpointSize()| bytes. This returns the deserialized 165 // |GetSerializedEndpointSize()| bytes. This returns the deserialized
165 // |IncomingEndpoint| (which can be converted into a |MessagePipe|) or null on 166 // |IncomingEndpoint| (which can be converted into a |MessagePipe|) or null on
166 // error. 167 // error.
167 RefPtr<IncomingEndpoint> DeserializeEndpoint(const void* source); 168 util::RefPtr<IncomingEndpoint> DeserializeEndpoint(const void* source);
168 169
169 // See |RawChannel::GetSerializedPlatformHandleSize()|. 170 // See |RawChannel::GetSerializedPlatformHandleSize()|.
170 size_t GetSerializedPlatformHandleSize() const; 171 size_t GetSerializedPlatformHandleSize() const;
171 172
172 embedder::PlatformSupport* platform_support() const { 173 embedder::PlatformSupport* platform_support() const {
173 return platform_support_; 174 return platform_support_;
174 } 175 }
175 176
176 private: 177 private:
177 FRIEND_REF_COUNTED_THREAD_SAFE(Channel); 178 FRIEND_REF_COUNTED_THREAD_SAFE(Channel);
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 void HandleLocalError(const char* error_message); 218 void HandleLocalError(const char* error_message);
218 219
219 // Helper for |SerializeEndpoint...()|: Attaches the given (non-bootstrap) 220 // Helper for |SerializeEndpoint...()|: Attaches the given (non-bootstrap)
220 // endpoint to this channel and runs it. This assigns the endpoint both local 221 // endpoint to this channel and runs it. This assigns the endpoint both local
221 // and remote IDs. This will also send a 222 // and remote IDs. This will also send a
222 // |Subtype::CHANNEL_ATTACH_AND_RUN_ENDPOINT| message to the remote side to 223 // |Subtype::CHANNEL_ATTACH_AND_RUN_ENDPOINT| message to the remote side to
223 // tell it to create an endpoint as well. This returns the *remote* ID (one 224 // tell it to create an endpoint as well. This returns the *remote* ID (one
224 // for which |is_remote()| returns true). 225 // for which |is_remote()| returns true).
225 // 226 //
226 // TODO(vtl): Maybe limit the number of attached message pipes. 227 // TODO(vtl): Maybe limit the number of attached message pipes.
227 ChannelEndpointId AttachAndRunEndpoint(RefPtr<ChannelEndpoint>&& endpoint); 228 ChannelEndpointId AttachAndRunEndpoint(
229 util::RefPtr<ChannelEndpoint>&& endpoint);
228 230
229 // Helper to send channel control messages. Returns true on success. Callable 231 // Helper to send channel control messages. Returns true on success. Callable
230 // from any thread. 232 // from any thread.
231 bool SendControlMessage(MessageInTransit::Subtype subtype, 233 bool SendControlMessage(MessageInTransit::Subtype subtype,
232 ChannelEndpointId source_id, 234 ChannelEndpointId source_id,
233 ChannelEndpointId destination_id, 235 ChannelEndpointId destination_id,
234 uint32_t num_bytes, 236 uint32_t num_bytes,
235 const void* bytes) MOJO_LOCKS_EXCLUDED(mutex_); 237 const void* bytes) MOJO_LOCKS_EXCLUDED(mutex_);
236 238
237 base::ThreadChecker creation_thread_checker_; 239 base::ThreadChecker creation_thread_checker_;
(...skipping 12 matching lines...) Expand all
250 252
251 std::unique_ptr<RawChannel> raw_channel_ MOJO_GUARDED_BY(mutex_); 253 std::unique_ptr<RawChannel> raw_channel_ MOJO_GUARDED_BY(mutex_);
252 bool is_running_ MOJO_GUARDED_BY(mutex_); 254 bool is_running_ MOJO_GUARDED_BY(mutex_);
253 // Set when |WillShutdownSoon()| is called. 255 // Set when |WillShutdownSoon()| is called.
254 bool is_shutting_down_ MOJO_GUARDED_BY(mutex_); 256 bool is_shutting_down_ MOJO_GUARDED_BY(mutex_);
255 257
256 // Has a reference to us. 258 // Has a reference to us.
257 ChannelManager* channel_manager_ MOJO_GUARDED_BY(mutex_); 259 ChannelManager* channel_manager_ MOJO_GUARDED_BY(mutex_);
258 260
259 using IdToEndpointMap = 261 using IdToEndpointMap =
260 std::unordered_map<ChannelEndpointId, RefPtr<ChannelEndpoint>>; 262 std::unordered_map<ChannelEndpointId, util::RefPtr<ChannelEndpoint>>;
261 // Map from local IDs to endpoints. If the endpoint is null, this means that 263 // Map from local IDs to endpoints. If the endpoint is null, this means that
262 // we're just waiting for the remove ack before removing the entry. 264 // we're just waiting for the remove ack before removing the entry.
263 IdToEndpointMap local_id_to_endpoint_map_ MOJO_GUARDED_BY(mutex_); 265 IdToEndpointMap local_id_to_endpoint_map_ MOJO_GUARDED_BY(mutex_);
264 // Note: The IDs generated by this should be checked for existence before use. 266 // Note: The IDs generated by this should be checked for existence before use.
265 LocalChannelEndpointIdGenerator local_id_generator_ MOJO_GUARDED_BY(mutex_); 267 LocalChannelEndpointIdGenerator local_id_generator_ MOJO_GUARDED_BY(mutex_);
266 268
267 using IdToIncomingEndpointMap = 269 using IdToIncomingEndpointMap =
268 std::unordered_map<ChannelEndpointId, RefPtr<IncomingEndpoint>>; 270 std::unordered_map<ChannelEndpointId, util::RefPtr<IncomingEndpoint>>;
269 // Map from local IDs to incoming endpoints (i.e., those received inside other 271 // Map from local IDs to incoming endpoints (i.e., those received inside other
270 // messages, but not yet claimed via |DeserializeEndpoint()|). 272 // messages, but not yet claimed via |DeserializeEndpoint()|).
271 IdToIncomingEndpointMap incoming_endpoints_ MOJO_GUARDED_BY(mutex_); 273 IdToIncomingEndpointMap incoming_endpoints_ MOJO_GUARDED_BY(mutex_);
272 // TODO(vtl): We need to keep track of remote IDs (so that we don't collide 274 // TODO(vtl): We need to keep track of remote IDs (so that we don't collide
273 // if/when we wrap). 275 // if/when we wrap).
274 RemoteChannelEndpointIdGenerator remote_id_generator_ MOJO_GUARDED_BY(mutex_); 276 RemoteChannelEndpointIdGenerator remote_id_generator_ MOJO_GUARDED_BY(mutex_);
275 277
276 MOJO_DISALLOW_COPY_AND_ASSIGN(Channel); 278 MOJO_DISALLOW_COPY_AND_ASSIGN(Channel);
277 }; 279 };
278 280
279 } // namespace system 281 } // namespace system
280 } // namespace mojo 282 } // namespace mojo
281 283
282 #endif // MOJO_EDK_SYSTEM_CHANNEL_H_ 284 #endif // MOJO_EDK_SYSTEM_CHANNEL_H_
OLDNEW
« no previous file with comments | « mojo/edk/system/BUILD.gn ('k') | mojo/edk/system/channel.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698