OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef MOJO_SYSTEM_CHANNEL_H_ | |
6 #define MOJO_SYSTEM_CHANNEL_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include "base/basictypes.h" | |
11 #include "base/compiler_specific.h" | |
12 #include "base/containers/hash_tables.h" | |
13 #include "base/memory/ref_counted.h" | |
14 #include "base/memory/scoped_ptr.h" | |
15 #include "base/strings/string_piece.h" | |
16 #include "base/synchronization/lock.h" | |
17 #include "mojo/public/system/core.h" | |
18 #include "mojo/public/system/system_export.h" | |
19 #include "mojo/system/message_in_transit.h" | |
20 #include "mojo/system/message_pipe.h" | |
21 #include "mojo/system/raw_channel.h" | |
22 | |
23 namespace base { | |
24 class MessageLoop; | |
25 } | |
26 | |
27 namespace mojo { | |
28 namespace system { | |
29 | |
30 // This class is mostly thread-safe. It must be created on an "I/O thread" (see | |
31 // raw_channel.h) and |Shutdown()| must be called on that same thread. Its | |
32 // public methods are otherwise thread-safe. It may be destroyed on any thread, | |
33 // in the sense that the last reference to it may be released on any thread, | |
34 // with the proviso that |Shutdown()| must have been called first (so the | |
35 // pattern is that a "main" reference is kept on its creation thread and is | |
36 // released after |Shutdown()| is called, but other threads may have temporarily | |
37 // "dangling" references). | |
38 // | |
39 // Note that |MessagePipe| calls into |Channel| and the former's |lock_| must be | |
40 // acquired before the latter's. When |Channel| wants to call into a | |
41 // |MessagePipe|, it must obtain a reference to the |MessagePipe| (from | |
42 // |local_id_to_endpoint_info_map_|) under |Channel::lock_| and then release the | |
43 // lock. | |
44 // | |
45 // Also, care must be taken with respect to references: While a |Channel| has | |
46 // references to |MessagePipe|s, |MessagePipe|s (via |ProxyMessagePipeEndpoint|) | |
47 // may also have references to |Channel|s. These references are set up by | |
48 // calling |AttachMessagePipeEndpoint()|. The reference to |MessagePipe| owned | |
49 // by |Channel| must be removed by calling |DetachMessagePipeEndpoint()| (which | |
50 // is done by |MessagePipe|/|ProxyMessagePipeEndpoint|, which simultaneously | |
51 // removes its reference to |Channel|). | |
52 class Channel : public base::RefCountedThreadSafe<Channel>, | |
53 public RawChannel::Delegate { | |
54 public: | |
55 // The first message pipe endpoint attached will have this as its local ID. | |
56 static const MessageInTransit::EndpointId kBootstrapEndpointId = 1; | |
57 | |
58 // Takes ownership of |handle|. | |
59 explicit Channel(const PlatformChannelHandle& handle); | |
60 | |
61 // This must be called on the creation thread before destruction (which can | |
62 // happen on any thread). | |
63 void Shutdown(); | |
64 | |
65 // Attaches the given message pipe/port's endpoint (which must be a | |
66 // |ProxyMessagePipeEndpoint|) to this channel. This assigns it a local ID, | |
67 // which it returns. The first message pipe endpoint attached will always have | |
68 // |kBootstrapEndpointId| as its local ID. (For bootstrapping, this occurs on | |
69 // both sides, so one should use |kBootstrapEndpointId| for the remote ID for | |
70 // the first message pipe across a channel.) | |
71 MessageInTransit::EndpointId AttachMessagePipeEndpoint( | |
72 scoped_refptr<MessagePipe> message_pipe, unsigned port); | |
73 void RunMessagePipeEndpoint(MessageInTransit::EndpointId local_id, | |
74 MessageInTransit::EndpointId remote_id); | |
75 | |
76 // This forwards |message| verbatim to |raw_channel_|. | |
77 bool WriteMessage(MessageInTransit* message); | |
78 | |
79 // This removes the message pipe/port's endpoint (with the given local ID, | |
80 // returned by |AttachMessagePipeEndpoint()| from this channel. After this is | |
81 // called, |local_id| may be reused for another message pipe. | |
82 void DetachMessagePipeEndpoint(MessageInTransit::EndpointId local_id); | |
83 | |
84 private: | |
85 friend class base::RefCountedThreadSafe<Channel>; | |
86 virtual ~Channel(); | |
87 | |
88 // |RawChannel::Delegate| implementation: | |
89 virtual void OnReadMessage(const MessageInTransit& message) OVERRIDE; | |
90 virtual void OnFatalError(FatalError fatal_error) OVERRIDE; | |
91 | |
92 // Helpers for |OnReadMessage|: | |
93 void OnReadMessageForDownstream(const MessageInTransit& message); | |
94 void OnReadMessageForChannel(const MessageInTransit& message); | |
95 | |
96 // Handles errors (e.g., invalid messages) from the remote side. | |
97 void HandleRemoteError(const base::StringPiece& error_message); | |
98 // Handles internal errors/failures from the local side. | |
99 void HandleLocalError(const base::StringPiece& error_message); | |
100 | |
101 #ifdef NDEBUG | |
102 void AssertOnCreationThread() {} | |
103 #else | |
104 void AssertOnCreationThread(); | |
105 #endif | |
106 | |
107 struct EndpointInfo { | |
108 EndpointInfo(); | |
109 EndpointInfo(scoped_refptr<MessagePipe> message_pipe, unsigned port); | |
110 ~EndpointInfo(); | |
111 | |
112 scoped_refptr<MessagePipe> message_pipe; | |
113 unsigned port; | |
114 }; | |
115 | |
116 #ifndef NDEBUG | |
117 // This is the |MessageLoop| for the thread that we were created and must be | |
118 // shutdown on. (This is only used by |AssertOnCreationThread()|.) | |
119 base::MessageLoop* creation_thread_message_loop_; | |
darin (slow to review)
2013/11/06 18:26:00
Maybe you want to use base::ThreadChecker here ins
viettrungluu
2013/11/06 21:13:39
Done.
| |
120 #endif | |
121 | |
122 // Note: |MessagePipe|s MUST NOT be used under |lock_|. I.e., |lock_| can only | |
123 // be acquired after |MessagePipe::lock_|, never before. Thus to call into a | |
124 // |MessagePipe|, a reference should be acquired from | |
125 // |local_id_to_endpoint_info_map_| under |lock_| (e.g., by copying the | |
126 // |EndpointInfo|) and then the lock released. | |
127 base::Lock lock_; // Protects the members below. | |
128 | |
129 scoped_ptr<RawChannel> raw_channel_; | |
130 | |
131 typedef base::hash_map<MessageInTransit::EndpointId, EndpointInfo> | |
132 IdToEndpointInfoMap; | |
133 IdToEndpointInfoMap local_id_to_endpoint_info_map_; | |
134 // The next local ID to try (when allocating new local IDs). Note: It should | |
135 // be checked for existence before use. | |
136 MessageInTransit::EndpointId next_local_id_; | |
137 | |
138 DISALLOW_COPY_AND_ASSIGN(Channel); | |
139 }; | |
140 | |
141 } // namespace system | |
142 } // namespace mojo | |
143 | |
144 #endif // MOJO_SYSTEM_CHANNEL_H_ | |
OLD | NEW |