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/core.h"
15 #include "mojo/system/dispatcher.h"
16 #include "mojo/system/message_in_transit.h"
17 #include "mojo/system/message_pipe_endpoint.h"
18 #include "mojo/system/system_impl_export.h"
26 // |MessagePipe| is the secondary object implementing a message pipe (see the
27 // explanatory comment in core.cc). It is typically owned by the dispatcher(s)
28 // corresponding to the local endpoints. This class is thread-safe.
29 class MOJO_SYSTEM_IMPL_EXPORT MessagePipe :
30 public base::RefCountedThreadSafe<MessagePipe> {
32 MessagePipe(scoped_ptr<MessagePipeEndpoint> endpoint0,
33 scoped_ptr<MessagePipeEndpoint> endpoint1);
35 // Convenience constructor that constructs a |MessagePipe| with two new
36 // |LocalMessagePipeEndpoint|s.
39 // Gets the other port number (i.e., 0 -> 1, 1 -> 0).
40 static unsigned GetPeerPort(unsigned port);
42 // Gets the type of the endpoint (used for assertions, etc.).
43 MessagePipeEndpoint::Type GetType(unsigned port);
45 // These are called by the dispatcher to implement its methods of
46 // corresponding names. In all cases, the port |port| must be open.
47 void CancelAllWaiters(unsigned port);
48 void Close(unsigned port);
49 // Unlike |MessagePipeDispatcher::WriteMessage()|, this does not validate its
51 MojoResult WriteMessage(unsigned port,
54 std::vector<DispatcherTransport>* transports,
55 MojoWriteMessageFlags flags);
56 // Unlike |MessagePipeDispatcher::ReadMessage()|, this does not validate its
58 MojoResult ReadMessage(unsigned port,
61 DispatcherVector* dispatchers,
62 uint32_t* num_dispatchers,
63 MojoReadMessageFlags flags);
64 MojoResult AddWaiter(unsigned port,
67 MojoResult wake_result);
68 void RemoveWaiter(unsigned port, Waiter* waiter);
70 // This is called by the dispatcher to convert a local endpoint to a proxy
72 void ConvertLocalToProxy(unsigned port);
74 // This is used by |Channel| to enqueue messages (typically to a
75 // |LocalMessagePipeEndpoint|). Unlike |WriteMessage()|, |port| is the
76 // *destination* port.
77 MojoResult EnqueueMessage(unsigned port,
78 scoped_ptr<MessageInTransit> message);
80 // These are used by |Channel|.
81 bool Attach(unsigned port,
82 scoped_refptr<Channel> channel,
83 MessageInTransit::EndpointId local_id);
84 void Run(unsigned port, MessageInTransit::EndpointId remote_id);
85 void OnRemove(unsigned port);
88 friend class base::RefCountedThreadSafe<MessagePipe>;
89 virtual ~MessagePipe();
91 // This is used internally by |WriteMessage()| and by |EnqueueMessage()|.
92 // |transports| may be non-null only if it's nonempty and |message| has no
93 // dispatchers attached.
94 MojoResult EnqueueMessageInternal(
96 scoped_ptr<MessageInTransit> message,
97 std::vector<DispatcherTransport>* transports);
99 // Helper for |EnqueueMessageInternal()|. Must be called with |lock_| held.
100 MojoResult AttachTransportsNoLock(
102 MessageInTransit* message,
103 std::vector<DispatcherTransport>* transports);
105 // Used by |EnqueueMessageInternal()| to handle control messages that are
106 // actually meant for us.
107 MojoResult HandleControlMessage(unsigned port,
108 scoped_ptr<MessageInTransit> message);
110 base::Lock lock_; // Protects the following members.
111 scoped_ptr<MessagePipeEndpoint> endpoints_[2];
113 DISALLOW_COPY_AND_ASSIGN(MessagePipe);
116 } // namespace system
119 #endif // MOJO_SYSTEM_MESSAGE_PIPE_H_