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_MESSAGE_PIPE_H_ | |
6 #define MOJO_SYSTEM_MESSAGE_PIPE_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include <vector> | |
11 | |
12 #include "base/macros.h" | |
13 #include "base/memory/ref_counted.h" | |
14 #include "base/memory/scoped_ptr.h" | |
15 #include "base/synchronization/lock.h" | |
16 #include "mojo/public/c/system/message_pipe.h" | |
17 #include "mojo/public/c/system/types.h" | |
18 #include "mojo/system/dispatcher.h" | |
19 #include "mojo/system/handle_signals_state.h" | |
20 #include "mojo/system/memory.h" | |
21 #include "mojo/system/message_in_transit.h" | |
22 #include "mojo/system/message_pipe_endpoint.h" | |
23 #include "mojo/system/system_impl_export.h" | |
24 | |
25 namespace mojo { | |
26 namespace system { | |
27 | |
28 class ChannelEndpoint; | |
29 class Waiter; | |
30 | |
31 // |MessagePipe| is the secondary object implementing a message pipe (see the | |
32 // explanatory comment in core.cc). It is typically owned by the dispatcher(s) | |
33 // corresponding to the local endpoints. This class is thread-safe. | |
34 class MOJO_SYSTEM_IMPL_EXPORT MessagePipe | |
35 : public base::RefCountedThreadSafe<MessagePipe> { | |
36 public: | |
37 // Creates a |MessagePipe| with two new |LocalMessagePipeEndpoint|s. | |
38 static MessagePipe* CreateLocalLocal(); | |
39 | |
40 // Creates a |MessagePipe| with a |LocalMessagePipeEndpoint| on port 0 and a | |
41 // |ProxyMessagePipeEndpoint| on port 1. |*channel_endpoint| is set to the | |
42 // (newly-created) |ChannelEndpoint| for the latter. | |
43 static MessagePipe* CreateLocalProxy( | |
44 scoped_refptr<ChannelEndpoint>* channel_endpoint); | |
45 | |
46 // Creates a |MessagePipe| with a |ProxyMessagePipeEndpoint| on port 0 and a | |
47 // |LocalMessagePipeEndpoint| on port 1. |*channel_endpoint| is set to the | |
48 // (newly-created) |ChannelEndpoint| for the former. | |
49 // Note: This is really only needed in tests (outside of tests, this | |
50 // configuration arises from a local message pipe having its port 0 | |
51 // "converted" using |ConvertLocalToProxy()|). | |
52 static MessagePipe* CreateProxyLocal( | |
53 scoped_refptr<ChannelEndpoint>* channel_endpoint); | |
54 | |
55 // Gets the other port number (i.e., 0 -> 1, 1 -> 0). | |
56 static unsigned GetPeerPort(unsigned port); | |
57 | |
58 // Gets the type of the endpoint (used for assertions, etc.). | |
59 MessagePipeEndpoint::Type GetType(unsigned port); | |
60 | |
61 // These are called by the dispatcher to implement its methods of | |
62 // corresponding names. In all cases, the port |port| must be open. | |
63 void CancelAllWaiters(unsigned port); | |
64 void Close(unsigned port); | |
65 // Unlike |MessagePipeDispatcher::WriteMessage()|, this does not validate its | |
66 // arguments. | |
67 MojoResult WriteMessage(unsigned port, | |
68 UserPointer<const void> bytes, | |
69 uint32_t num_bytes, | |
70 std::vector<DispatcherTransport>* transports, | |
71 MojoWriteMessageFlags flags); | |
72 MojoResult ReadMessage(unsigned port, | |
73 UserPointer<void> bytes, | |
74 UserPointer<uint32_t> num_bytes, | |
75 DispatcherVector* dispatchers, | |
76 uint32_t* num_dispatchers, | |
77 MojoReadMessageFlags flags); | |
78 HandleSignalsState GetHandleSignalsState(unsigned port) const; | |
79 MojoResult AddWaiter(unsigned port, | |
80 Waiter* waiter, | |
81 MojoHandleSignals signals, | |
82 uint32_t context, | |
83 HandleSignalsState* signals_state); | |
84 void RemoveWaiter(unsigned port, | |
85 Waiter* waiter, | |
86 HandleSignalsState* signals_state); | |
87 | |
88 // This is called by the dispatcher to convert a local endpoint to a proxy | |
89 // endpoint. | |
90 scoped_refptr<ChannelEndpoint> ConvertLocalToProxy(unsigned port); | |
91 | |
92 // This is used by |Channel| to enqueue messages (typically to a | |
93 // |LocalMessagePipeEndpoint|). Unlike |WriteMessage()|, |port| is the | |
94 // *destination* port. | |
95 MojoResult EnqueueMessage(unsigned port, | |
96 scoped_ptr<MessageInTransit> message); | |
97 | |
98 private: | |
99 MessagePipe(); | |
100 | |
101 friend class base::RefCountedThreadSafe<MessagePipe>; | |
102 virtual ~MessagePipe(); | |
103 | |
104 // This is used internally by |WriteMessage()| and by |EnqueueMessage()|. | |
105 // |transports| may be non-null only if it's nonempty and |message| has no | |
106 // dispatchers attached. | |
107 MojoResult EnqueueMessageInternal( | |
108 unsigned port, | |
109 scoped_ptr<MessageInTransit> message, | |
110 std::vector<DispatcherTransport>* transports); | |
111 | |
112 // Helper for |EnqueueMessageInternal()|. Must be called with |lock_| held. | |
113 MojoResult AttachTransportsNoLock( | |
114 unsigned port, | |
115 MessageInTransit* message, | |
116 std::vector<DispatcherTransport>* transports); | |
117 | |
118 // Used by |EnqueueMessageInternal()| to handle control messages that are | |
119 // actually meant for us. | |
120 MojoResult HandleControlMessage(unsigned port, | |
121 scoped_ptr<MessageInTransit> message); | |
122 | |
123 base::Lock lock_; // Protects the following members. | |
124 scoped_ptr<MessagePipeEndpoint> endpoints_[2]; | |
125 | |
126 DISALLOW_COPY_AND_ASSIGN(MessagePipe); | |
127 }; | |
128 | |
129 } // namespace system | |
130 } // namespace mojo | |
131 | |
132 #endif // MOJO_SYSTEM_MESSAGE_PIPE_H_ | |
OLD | NEW |