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

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

Issue 60103005: Mojo: First stab at making MessagePipes work across OS pipes. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebased Created 7 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 | Annotate | Revision Log
OLDNEW
(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 "base/threading/thread_checker.h"
18 #include "mojo/public/system/core.h"
19 #include "mojo/public/system/system_export.h"
20 #include "mojo/system/message_in_transit.h"
21 #include "mojo/system/message_pipe.h"
22 #include "mojo/system/raw_channel.h"
23
24 namespace base {
25 class MessageLoop;
26 }
27
28 namespace mojo {
29 namespace system {
30
31 // This class is mostly thread-safe. It must be created on an "I/O thread" (see
32 // raw_channel.h). |Init()| must be called on that same thread before it becomes
33 // thread-safe (in particular, before references are given to any other thread)
34 // and |Shutdown()| must be called on that same thread before destruction. Its
35 // public methods are otherwise thread-safe. It may be destroyed on any thread,
36 // in the sense that the last reference to it may be released on any thread,
37 // with the proviso that |Shutdown()| must have been called first (so the
38 // pattern is that a "main" reference is kept on its creation thread and is
39 // released after |Shutdown()| is called, but other threads may have temporarily
40 // "dangling" references).
41 //
42 // Note that |MessagePipe| calls into |Channel| and the former's |lock_| must be
43 // acquired before the latter's. When |Channel| wants to call into a
44 // |MessagePipe|, it must obtain a reference to the |MessagePipe| (from
45 // |local_id_to_endpoint_info_map_|) under |Channel::lock_| and then release the
46 // lock.
47 //
48 // Also, care must be taken with respect to references: While a |Channel| has
49 // references to |MessagePipe|s, |MessagePipe|s (via |ProxyMessagePipeEndpoint|)
50 // may also have references to |Channel|s. These references are set up by
51 // calling |AttachMessagePipeEndpoint()|. The reference to |MessagePipe| owned
52 // by |Channel| must be removed by calling |DetachMessagePipeEndpoint()| (which
53 // is done by |MessagePipe|/|ProxyMessagePipeEndpoint|, which simultaneously
54 // removes its reference to |Channel|).
55 class MOJO_SYSTEM_EXPORT Channel : public base::RefCountedThreadSafe<Channel>,
56 public RawChannel::Delegate {
57 public:
58 // The first message pipe endpoint attached will have this as its local ID.
59 static const MessageInTransit::EndpointId kBootstrapEndpointId = 1;
60
61 Channel();
62
63 // This must be called on the creation thread before any other methods are
64 // called, and before references to this object are given to any other
65 // threads. Takes ownership of |handle|. Returns true on success. On failure,
66 // no other methods should be called (including |Shutdown()|).
67 bool Init(const PlatformChannelHandle& handle);
68
69 // This must be called on the creation thread before destruction (which can
70 // happen on any thread).
71 void Shutdown();
72
73 // Attaches the given message pipe/port's endpoint (which must be a
74 // |ProxyMessagePipeEndpoint|) to this channel. This assigns it a local ID,
75 // which it returns. The first message pipe endpoint attached will always have
76 // |kBootstrapEndpointId| as its local ID. (For bootstrapping, this occurs on
77 // both sides, so one should use |kBootstrapEndpointId| for the remote ID for
78 // the first message pipe across a channel.)
79 MessageInTransit::EndpointId AttachMessagePipeEndpoint(
80 scoped_refptr<MessagePipe> message_pipe, unsigned port);
81 void RunMessagePipeEndpoint(MessageInTransit::EndpointId local_id,
82 MessageInTransit::EndpointId remote_id);
83
84 // This forwards |message| verbatim to |raw_channel_|.
85 bool WriteMessage(MessageInTransit* message);
86
87 // This removes the message pipe/port's endpoint (with the given local ID,
88 // returned by |AttachMessagePipeEndpoint()| from this channel. After this is
89 // called, |local_id| may be reused for another message pipe.
90 void DetachMessagePipeEndpoint(MessageInTransit::EndpointId local_id);
91
92 private:
93 friend class base::RefCountedThreadSafe<Channel>;
94 virtual ~Channel();
95
96 // |RawChannel::Delegate| implementation:
97 virtual void OnReadMessage(const MessageInTransit& message) OVERRIDE;
98 virtual void OnFatalError(FatalError fatal_error) OVERRIDE;
99
100 // Helpers for |OnReadMessage|:
101 void OnReadMessageForDownstream(const MessageInTransit& message);
102 void OnReadMessageForChannel(const MessageInTransit& message);
103
104 // Handles errors (e.g., invalid messages) from the remote side.
105 void HandleRemoteError(const base::StringPiece& error_message);
106 // Handles internal errors/failures from the local side.
107 void HandleLocalError(const base::StringPiece& error_message);
108
109 struct EndpointInfo {
110 EndpointInfo();
111 EndpointInfo(scoped_refptr<MessagePipe> message_pipe, unsigned port);
112 ~EndpointInfo();
113
114 scoped_refptr<MessagePipe> message_pipe;
115 unsigned port;
116 };
117
118 base::ThreadChecker creation_thread_checker_;
119
120 // Note: |MessagePipe|s MUST NOT be used under |lock_|. I.e., |lock_| can only
121 // be acquired after |MessagePipe::lock_|, never before. Thus to call into a
122 // |MessagePipe|, a reference should be acquired from
123 // |local_id_to_endpoint_info_map_| under |lock_| (e.g., by copying the
124 // |EndpointInfo|) and then the lock released.
125 base::Lock lock_; // Protects the members below.
126
127 scoped_ptr<RawChannel> raw_channel_;
128
129 typedef base::hash_map<MessageInTransit::EndpointId, EndpointInfo>
130 IdToEndpointInfoMap;
131 IdToEndpointInfoMap local_id_to_endpoint_info_map_;
132 // The next local ID to try (when allocating new local IDs). Note: It should
133 // be checked for existence before use.
134 MessageInTransit::EndpointId next_local_id_;
135
136 DISALLOW_COPY_AND_ASSIGN(Channel);
137 };
138
139 } // namespace system
140 } // namespace mojo
141
142 #endif // MOJO_SYSTEM_CHANNEL_H_
OLDNEW
« base/compiler_specific.h ('K') | « mojo/mojo.gyp ('k') | mojo/system/channel.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698