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_DISPATCHER_H_
6 #define MOJO_SYSTEM_MESSAGE_PIPE_DISPATCHER_H_
10 #include "base/macros.h"
11 #include "base/memory/ref_counted.h"
12 #include "mojo/system/dispatcher.h"
13 #include "mojo/system/memory.h"
14 #include "mojo/system/system_impl_export.h"
20 class MessagePipeDispatcherTransport
;
22 // This is the |Dispatcher| implementation for message pipes (created by the
23 // Mojo primitive |MojoCreateMessagePipe()|). This class is thread-safe.
24 class MOJO_SYSTEM_IMPL_EXPORT MessagePipeDispatcher
: public Dispatcher
{
26 // The default options to use for |MojoCreateMessagePipe()|. (Real uses
27 // should obtain this via |ValidateCreateOptions()| with a null |in_options|;
28 // this is exposed directly for testing convenience.)
29 static const MojoCreateMessagePipeOptions kDefaultCreateOptions
;
31 MessagePipeDispatcher(
32 const MojoCreateMessagePipeOptions
& /*validated_options*/);
34 // Validates and/or sets default options for |MojoCreateMessagePipeOptions|.
35 // If non-null, |in_options| must point to a struct of at least
36 // |in_options->struct_size| bytes. |out_options| must point to a (current)
37 // |MojoCreateMessagePipeOptions| and will be entirely overwritten on success
38 // (it may be partly overwritten on failure).
39 static MojoResult
ValidateCreateOptions(
40 UserPointer
<const MojoCreateMessagePipeOptions
> in_options
,
41 MojoCreateMessagePipeOptions
* out_options
);
43 // Must be called before any other methods. (This method is not thread-safe.)
44 void Init(scoped_refptr
<MessagePipe
> message_pipe
, unsigned port
);
46 // |Dispatcher| public methods:
47 virtual Type
GetType() const OVERRIDE
;
49 // Creates a |MessagePipe| with a local endpoint (at port 0) and a proxy
50 // endpoint, and creates/initializes a |MessagePipeDispatcher| (attached to
51 // the message pipe, port 0).
52 // TODO(vtl): This currently uses |kDefaultCreateOptions|, which is okay since
53 // there aren't any options, but eventually options should be plumbed through.
54 static std::pair
<scoped_refptr
<MessagePipeDispatcher
>,
55 scoped_refptr
<MessagePipe
> >
56 CreateRemoteMessagePipe();
58 // The "opposite" of |SerializeAndClose()|. (Typically this is called by
59 // |Dispatcher::Deserialize()|.)
60 static scoped_refptr
<MessagePipeDispatcher
> Deserialize(Channel
* channel
,
65 friend class MessagePipeDispatcherTransport
;
67 virtual ~MessagePipeDispatcher();
69 // Gets a dumb pointer to |message_pipe_|. This must be called under the
70 // |Dispatcher| lock (that it's a dumb pointer is okay since it's under lock).
71 // This is needed when sending handles across processes, where nontrivial,
72 // invasive work needs to be done.
73 MessagePipe
* GetMessagePipeNoLock() const;
74 // Similarly for the port.
75 unsigned GetPortNoLock() const;
77 // |Dispatcher| protected methods:
78 virtual void CancelAllWaitersNoLock() OVERRIDE
;
79 virtual void CloseImplNoLock() OVERRIDE
;
80 virtual scoped_refptr
<Dispatcher
>
81 CreateEquivalentDispatcherAndCloseImplNoLock() OVERRIDE
;
82 virtual MojoResult
WriteMessageImplNoLock(
83 UserPointer
<const void> bytes
,
85 std::vector
<DispatcherTransport
>* transports
,
86 MojoWriteMessageFlags flags
) OVERRIDE
;
87 virtual MojoResult
ReadMessageImplNoLock(UserPointer
<void> bytes
,
88 UserPointer
<uint32_t> num_bytes
,
89 DispatcherVector
* dispatchers
,
90 uint32_t* num_dispatchers
,
91 MojoReadMessageFlags flags
) OVERRIDE
;
92 virtual HandleSignalsState
GetHandleSignalsStateImplNoLock() const OVERRIDE
;
93 virtual MojoResult
AddWaiterImplNoLock(
95 MojoHandleSignals signals
,
97 HandleSignalsState
* signals_state
) OVERRIDE
;
98 virtual void RemoveWaiterImplNoLock(
100 HandleSignalsState
* signals_state
) OVERRIDE
;
101 virtual void StartSerializeImplNoLock(Channel
* channel
,
103 size_t* max_platform_handles
) OVERRIDE
;
104 virtual bool EndSerializeAndCloseImplNoLock(
108 embedder::PlatformHandleVector
* platform_handles
) OVERRIDE
;
110 // Protected by |lock()|:
111 scoped_refptr
<MessagePipe
> message_pipe_
; // This will be null if closed.
114 DISALLOW_COPY_AND_ASSIGN(MessagePipeDispatcher
);
117 class MessagePipeDispatcherTransport
: public DispatcherTransport
{
119 explicit MessagePipeDispatcherTransport(DispatcherTransport transport
);
121 MessagePipe
* GetMessagePipe() {
122 return message_pipe_dispatcher()->GetMessagePipeNoLock();
124 unsigned GetPort() { return message_pipe_dispatcher()->GetPortNoLock(); }
127 MessagePipeDispatcher
* message_pipe_dispatcher() {
128 return static_cast<MessagePipeDispatcher
*>(dispatcher());
131 // Copy and assign allowed.
134 } // namespace system
137 #endif // MOJO_SYSTEM_MESSAGE_PIPE_DISPATCHER_H_