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.
5 #ifndef MOJO_SYSTEM_MESSAGE_PIPE_H_
6 #define MOJO_SYSTEM_MESSAGE_PIPE_H_
10 #include "base/basictypes.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/synchronization/lock.h"
14 #include "mojo/public/c/system/message_pipe.h"
15 #include "mojo/public/c/system/types.h"
16 #include "mojo/system/dispatcher.h"
17 #include "mojo/system/message_in_transit.h"
18 #include "mojo/system/message_pipe_endpoint.h"
19 #include "mojo/system/system_impl_export.h"
27 // |MessagePipe| is the secondary object implementing a message pipe (see the
28 // explanatory comment in core.cc). It is typically owned by the dispatcher(s)
29 // corresponding to the local endpoints. This class is thread-safe.
30 class MOJO_SYSTEM_IMPL_EXPORT MessagePipe
:
31 public base::RefCountedThreadSafe
<MessagePipe
> {
33 MessagePipe(scoped_ptr
<MessagePipeEndpoint
> endpoint0
,
34 scoped_ptr
<MessagePipeEndpoint
> endpoint1
);
36 // Convenience constructor that constructs a |MessagePipe| with two new
37 // |LocalMessagePipeEndpoint|s.
40 // Gets the other port number (i.e., 0 -> 1, 1 -> 0).
41 static unsigned GetPeerPort(unsigned port
);
43 // Gets the type of the endpoint (used for assertions, etc.).
44 MessagePipeEndpoint::Type
GetType(unsigned port
);
46 // These are called by the dispatcher to implement its methods of
47 // corresponding names. In all cases, the port |port| must be open.
48 void CancelAllWaiters(unsigned port
);
49 void Close(unsigned port
);
50 // Unlike |MessagePipeDispatcher::WriteMessage()|, this does not validate its
52 MojoResult
WriteMessage(unsigned port
,
55 std::vector
<DispatcherTransport
>* transports
,
56 MojoWriteMessageFlags flags
);
57 // Unlike |MessagePipeDispatcher::ReadMessage()|, this does not validate its
59 MojoResult
ReadMessage(unsigned port
,
62 DispatcherVector
* dispatchers
,
63 uint32_t* num_dispatchers
,
64 MojoReadMessageFlags flags
);
65 MojoResult
AddWaiter(unsigned port
,
68 MojoResult wake_result
);
69 void RemoveWaiter(unsigned port
, Waiter
* waiter
);
71 // This is called by the dispatcher to convert a local endpoint to a proxy
73 void ConvertLocalToProxy(unsigned port
);
75 // This is used by |Channel| to enqueue messages (typically to a
76 // |LocalMessagePipeEndpoint|). Unlike |WriteMessage()|, |port| is the
77 // *destination* port.
78 MojoResult
EnqueueMessage(unsigned port
,
79 scoped_ptr
<MessageInTransit
> message
);
81 // These are used by |Channel|.
82 bool Attach(unsigned port
,
83 scoped_refptr
<Channel
> channel
,
84 MessageInTransit::EndpointId local_id
);
85 void Run(unsigned port
, MessageInTransit::EndpointId remote_id
);
86 void OnRemove(unsigned port
);
89 friend class base::RefCountedThreadSafe
<MessagePipe
>;
90 virtual ~MessagePipe();
92 // This is used internally by |WriteMessage()| and by |EnqueueMessage()|.
93 // |transports| may be non-null only if it's nonempty and |message| has no
94 // dispatchers attached.
95 MojoResult
EnqueueMessageInternal(
97 scoped_ptr
<MessageInTransit
> message
,
98 std::vector
<DispatcherTransport
>* transports
);
100 // Helper for |EnqueueMessageInternal()|. Must be called with |lock_| held.
101 MojoResult
AttachTransportsNoLock(
103 MessageInTransit
* message
,
104 std::vector
<DispatcherTransport
>* transports
);
106 // Used by |EnqueueMessageInternal()| to handle control messages that are
107 // actually meant for us.
108 MojoResult
HandleControlMessage(unsigned port
,
109 scoped_ptr
<MessageInTransit
> message
);
111 base::Lock lock_
; // Protects the following members.
112 scoped_ptr
<MessagePipeEndpoint
> endpoints_
[2];
114 DISALLOW_COPY_AND_ASSIGN(MessagePipe
);
117 } // namespace system
120 #endif // MOJO_SYSTEM_MESSAGE_PIPE_H_