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" |
(...skipping 26 matching lines...) Expand all Loading... |
37 // |Init()| must be called on that same thread before it becomes thread-safe (in | 37 // |Init()| must be called on that same thread before it becomes thread-safe (in |
38 // particular, before references are given to any other thread) and |Shutdown()| | 38 // particular, before references are given to any other thread) and |Shutdown()| |
39 // must be called on that same thread before destruction. Its public methods are | 39 // must be called on that same thread before destruction. Its public methods are |
40 // otherwise thread-safe. (Many private methods are restricted to the creation | 40 // otherwise thread-safe. (Many private methods are restricted to the creation |
41 // thread.) It may be destroyed on any thread, in the sense that the last | 41 // thread.) It may be destroyed on any thread, in the sense that the last |
42 // reference to it may be released on any thread, with the proviso that | 42 // reference to it may be released on any thread, with the proviso that |
43 // |Shutdown()| must have been called first (so the pattern is that a "main" | 43 // |Shutdown()| must have been called first (so the pattern is that a "main" |
44 // reference is kept on its creation thread and is released after |Shutdown()| | 44 // reference is kept on its creation thread and is released after |Shutdown()| |
45 // is called, but other threads may have temporarily "dangling" references). | 45 // is called, but other threads may have temporarily "dangling" references). |
46 // | 46 // |
47 // Note that |MessagePipe| calls into |Channel| and the former's |lock_| must be | 47 // Note the lock order (in order of allowable acquisition): |MessagePipe|, |
48 // acquired before the latter's. When |Channel| wants to call into a | 48 // |ChannelEndpoint|, |Channel|. Thus |Channel| may not call into |
49 // |MessagePipe|, it must obtain a reference to the |MessagePipe| (from | 49 // |ChannelEndpoint| with |Channel|'s lock held. |
50 // |local_id_to_endpoint_info_map_|) under |Channel::lock_| and then release the | |
51 // lock. | |
52 // | |
53 // Also, care must be taken with respect to references: While a |Channel| has | |
54 // references to |MessagePipe|s, |MessagePipe|s (via |ProxyMessagePipeEndpoint|) | |
55 // may also have references to |Channel|s. These references are set up by | |
56 // calling |AttachMessagePipeEndpoint()|. The reference to |MessagePipe| owned | |
57 // by |Channel| must be removed by calling |DetachMessagePipeEndpoint()| (which | |
58 // is done by |MessagePipe|/|ProxyMessagePipeEndpoint|, which simultaneously | |
59 // removes its reference to |Channel|). | |
60 class MOJO_SYSTEM_IMPL_EXPORT Channel | 50 class MOJO_SYSTEM_IMPL_EXPORT Channel |
61 : public base::RefCountedThreadSafe<Channel>, | 51 : public base::RefCountedThreadSafe<Channel>, |
62 public RawChannel::Delegate { | 52 public RawChannel::Delegate { |
63 public: | 53 public: |
64 // The first message pipe endpoint attached will have this as its local ID. | 54 // The first message pipe endpoint attached will have this as its local ID. |
65 static const MessageInTransit::EndpointId kBootstrapEndpointId = 1; | 55 static const MessageInTransit::EndpointId kBootstrapEndpointId = 1; |
66 | 56 |
67 // |platform_support| (typically owned by |Core|) must remain alive until | 57 // |platform_support| (typically owned by |Core|) must remain alive until |
68 // after |Shutdown()| is called. | 58 // after |Shutdown()| is called. |
69 explicit Channel(embedder::PlatformSupport* platform_support); | 59 explicit Channel(embedder::PlatformSupport* platform_support); |
70 | 60 |
71 // This must be called on the creation thread before any other methods are | 61 // This must be called on the creation thread before any other methods are |
72 // called, and before references to this object are given to any other | 62 // called, and before references to this object are given to any other |
73 // threads. |raw_channel| should be uninitialized. Returns true on success. On | 63 // threads. |raw_channel| should be uninitialized. Returns true on success. On |
74 // failure, no other methods should be called (including |Shutdown()|). | 64 // failure, no other methods should be called (including |Shutdown()|). |
75 bool Init(scoped_ptr<RawChannel> raw_channel); | 65 bool Init(scoped_ptr<RawChannel> raw_channel); |
76 | 66 |
77 // This must be called on the creation thread before destruction (which can | 67 // This must be called on the creation thread before destruction (which can |
78 // happen on any thread). | 68 // happen on any thread). |
79 void Shutdown(); | 69 void Shutdown(); |
80 | 70 |
81 // Signals that |Shutdown()| will be called soon (this may be called from any | 71 // Signals that |Shutdown()| will be called soon (this may be called from any |
82 // thread, unlike |Shutdown()|). Warnings will be issued if, e.g., messages | 72 // thread, unlike |Shutdown()|). Warnings will be issued if, e.g., messages |
83 // are written after this is called; other warnings may be suppressed. (This | 73 // are written after this is called; other warnings may be suppressed. (This |
84 // may be called multiple times, or not at all.) | 74 // may be called multiple times, or not at all.) |
85 void WillShutdownSoon(); | 75 void WillShutdownSoon(); |
86 | 76 |
87 // TODO(vtl): Write comment here. | 77 // Attaches the given endpoint to this channel. This assigns it a local ID, |
88 MessageInTransit::EndpointId AttachEndpoint( | 78 // which it returns. The first endpoint attached will always have |
89 scoped_refptr<ChannelEndpoint> endpoint); | |
90 | |
91 // TODO(vtl): Remove this version. | |
92 // Attaches the given message pipe/port's endpoint (which must be a | |
93 // |ProxyMessagePipeEndpoint|) to this channel. This assigns it a local ID, | |
94 // which it returns. The first message pipe endpoint attached will always have | |
95 // |kBootstrapEndpointId| as its local ID. (For bootstrapping, this occurs on | 79 // |kBootstrapEndpointId| as its local ID. (For bootstrapping, this occurs on |
96 // both sides, so one should use |kBootstrapEndpointId| for the remote ID for | 80 // both sides, so one should use |kBootstrapEndpointId| for the remote ID for |
97 // the first message pipe across a channel.) Returns |kInvalidEndpointId| on | 81 // the first message pipe across a channel.) Returns |kInvalidEndpointId| on |
98 // failure. | 82 // failure. |
99 // TODO(vtl): This should be combined with "run", and it should take a | 83 // TODO(vtl): This should be combined with "run", and it should take a |
100 // |ChannelEndpoint| instead. | 84 // |ChannelEndpoint| instead. |
101 // TODO(vtl): Maybe limit the number of attached message pipes. | 85 // TODO(vtl): Maybe limit the number of attached message pipes. |
102 MessageInTransit::EndpointId AttachMessagePipeEndpoint( | 86 MessageInTransit::EndpointId AttachEndpoint( |
103 scoped_refptr<MessagePipe> message_pipe, | 87 scoped_refptr<ChannelEndpoint> endpoint); |
104 unsigned port); | |
105 | 88 |
106 // Runs the message pipe with the given |local_id| (previously attached), with | 89 // Runs the message pipe with the given |local_id| (previously attached), with |
107 // the given |remote_id| (negotiated using some other means, e.g., over an | 90 // the given |remote_id| (negotiated using some other means, e.g., over an |
108 // existing message pipe; see comments above for the bootstrap case). Returns | 91 // existing message pipe; see comments above for the bootstrap case). Returns |
109 // false on failure, in particular if no message pipe with |local_id| is | 92 // false on failure, in particular if no message pipe with |local_id| is |
110 // attached. | 93 // attached. |
111 bool RunMessagePipeEndpoint(MessageInTransit::EndpointId local_id, | 94 bool RunMessagePipeEndpoint(MessageInTransit::EndpointId local_id, |
112 MessageInTransit::EndpointId remote_id); | 95 MessageInTransit::EndpointId remote_id); |
113 | 96 |
114 // Tells the other side of the channel to run a message pipe endpoint (which | 97 // Tells the other side of the channel to run a message pipe endpoint (which |
115 // must already be attached); |local_id| and |remote_id| are relative to this | 98 // must already be attached); |local_id| and |remote_id| are relative to this |
116 // channel (i.e., |local_id| is the other side's remote ID and |remote_id| is | 99 // channel (i.e., |local_id| is the other side's remote ID and |remote_id| is |
117 // its local ID). | 100 // its local ID). |
118 // TODO(vtl): Maybe we should just have a flag argument to | 101 // TODO(vtl): Maybe we should just have a flag argument to |
119 // |RunMessagePipeEndpoint()| that tells it to do this. | 102 // |RunMessagePipeEndpoint()| that tells it to do this. |
120 void RunRemoteMessagePipeEndpoint(MessageInTransit::EndpointId local_id, | 103 void RunRemoteMessagePipeEndpoint(MessageInTransit::EndpointId local_id, |
121 MessageInTransit::EndpointId remote_id); | 104 MessageInTransit::EndpointId remote_id); |
122 | 105 |
123 // This forwards |message| verbatim to |raw_channel_|. | 106 // This forwards |message| verbatim to |raw_channel_|. |
124 bool WriteMessage(scoped_ptr<MessageInTransit> message); | 107 bool WriteMessage(scoped_ptr<MessageInTransit> message); |
125 | 108 |
126 // See |RawChannel::IsWriteBufferEmpty()|. | 109 // See |RawChannel::IsWriteBufferEmpty()|. |
127 // TODO(vtl): Maybe we shouldn't expose this, and instead have a | 110 // TODO(vtl): Maybe we shouldn't expose this, and instead have a |
128 // |FlushWriteBufferAndShutdown()| or something like that. | 111 // |FlushWriteBufferAndShutdown()| or something like that. |
129 bool IsWriteBufferEmpty(); | 112 bool IsWriteBufferEmpty(); |
130 | 113 |
131 // This removes the message pipe/port's endpoint (with the given local ID and | 114 // This removes the message pipe/port's endpoint (with the given local ID and |
132 // given remote ID, which should be |kInvalidEndpointId| if not yet running), | 115 // given remote ID, which should be |kInvalidEndpointId| if not yet running), |
133 // returned by |AttachMessagePipeEndpoint()| from this channel. After this is | 116 // returned by |AttachEndpoint()| from this channel. After this is called, |
134 // called, |local_id| may be reused for another message pipe. | 117 // |local_id| may be reused for another message pipe. |
135 void DetachMessagePipeEndpoint(MessageInTransit::EndpointId local_id, | 118 void DetachMessagePipeEndpoint(MessageInTransit::EndpointId local_id, |
136 MessageInTransit::EndpointId remote_id); | 119 MessageInTransit::EndpointId remote_id); |
137 | 120 |
138 // See |RawChannel::GetSerializedPlatformHandleSize()|. | 121 // See |RawChannel::GetSerializedPlatformHandleSize()|. |
139 size_t GetSerializedPlatformHandleSize() const; | 122 size_t GetSerializedPlatformHandleSize() const; |
140 | 123 |
141 embedder::PlatformSupport* platform_support() const { | 124 embedder::PlatformSupport* platform_support() const { |
142 return platform_support_; | 125 return platform_support_; |
143 } | 126 } |
144 | 127 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 // be checked for existence before use. | 185 // be checked for existence before use. |
203 MessageInTransit::EndpointId next_local_id_; | 186 MessageInTransit::EndpointId next_local_id_; |
204 | 187 |
205 DISALLOW_COPY_AND_ASSIGN(Channel); | 188 DISALLOW_COPY_AND_ASSIGN(Channel); |
206 }; | 189 }; |
207 | 190 |
208 } // namespace system | 191 } // namespace system |
209 } // namespace mojo | 192 } // namespace mojo |
210 | 193 |
211 #endif // MOJO_SYSTEM_CHANNEL_H_ | 194 #endif // MOJO_SYSTEM_CHANNEL_H_ |
OLD | NEW |