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/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/ref_counted.h"
13 #include "mojo/system/dispatcher.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 MessagePipeDispatcher();
28 // Must be called before any other methods. (This method is not thread-safe.)
29 void Init(scoped_refptr<MessagePipe> message_pipe, unsigned port);
31 // |Dispatcher| public methods:
32 virtual Type GetType() const OVERRIDE;
34 // Creates a |MessagePipe| with a local endpoint (at port 0) and a proxy
35 // endpoint, and creates/initializes a |MessagePipeDispatcher| (attached to
36 // the message pipe, port 0).
37 static std::pair<scoped_refptr<MessagePipeDispatcher>,
38 scoped_refptr<MessagePipe> > CreateRemoteMessagePipe();
40 // The "opposite" of |SerializeAndClose()|. (Typically this is called by
41 // |Dispatcher::Deserialize()|.)
42 static scoped_refptr<MessagePipeDispatcher> Deserialize(Channel* channel,
47 friend class MessagePipeDispatcherTransport;
49 virtual ~MessagePipeDispatcher();
51 // Gets a dumb pointer to |message_pipe_|. This must be called under the
52 // |Dispatcher| lock (that it's a dumb pointer is okay since it's under lock).
53 // This is needed when sending handles across processes, where nontrivial,
54 // invasive work needs to be done.
55 MessagePipe* GetMessagePipeNoLock() const;
56 // Similarly for the port.
57 unsigned GetPortNoLock() const;
59 // |Dispatcher| protected methods:
60 virtual void CancelAllWaitersNoLock() OVERRIDE;
61 virtual void CloseImplNoLock() OVERRIDE;
62 virtual scoped_refptr<Dispatcher>
63 CreateEquivalentDispatcherAndCloseImplNoLock() OVERRIDE;
64 virtual MojoResult WriteMessageImplNoLock(
67 std::vector<DispatcherTransport>* transports,
68 MojoWriteMessageFlags flags) OVERRIDE;
69 virtual MojoResult ReadMessageImplNoLock(void* bytes,
71 DispatcherVector* dispatchers,
72 uint32_t* num_dispatchers,
73 MojoReadMessageFlags flags) OVERRIDE;
74 virtual MojoResult AddWaiterImplNoLock(Waiter* waiter,
76 MojoResult wake_result) OVERRIDE;
77 virtual void RemoveWaiterImplNoLock(Waiter* waiter) OVERRIDE;
78 virtual void StartSerializeImplNoLock(Channel* channel,
80 size_t* max_platform_handles) OVERRIDE;
81 virtual bool EndSerializeAndCloseImplNoLock(
85 std::vector<embedder::PlatformHandle>* platform_handles) OVERRIDE;
87 // Protected by |lock()|:
88 scoped_refptr<MessagePipe> message_pipe_; // This will be null if closed.
91 DISALLOW_COPY_AND_ASSIGN(MessagePipeDispatcher);
94 class MessagePipeDispatcherTransport : public DispatcherTransport {
96 explicit MessagePipeDispatcherTransport(DispatcherTransport transport);
98 MessagePipe* GetMessagePipe() {
99 return message_pipe_dispatcher()->GetMessagePipeNoLock();
101 unsigned GetPort() { return message_pipe_dispatcher()->GetPortNoLock(); }
104 MessagePipeDispatcher* message_pipe_dispatcher() {
105 return static_cast<MessagePipeDispatcher*>(dispatcher());
108 // Copy and assign allowed.
111 } // namespace system
114 #endif // MOJO_SYSTEM_MESSAGE_PIPE_DISPATCHER_H_