Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / mojo / system / message_pipe_dispatcher.h
blob1c843fc9acb421f618af3948b658e7f0c56c7364
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_
8 #include <utility>
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"
16 namespace mojo {
17 namespace system {
19 class MessagePipe;
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 {
25 public:
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,
61 const void* source,
62 size_t size);
64 private:
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,
84 uint32_t num_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(
94 Waiter* waiter,
95 MojoHandleSignals signals,
96 uint32_t context,
97 HandleSignalsState* signals_state) OVERRIDE;
98 virtual void RemoveWaiterImplNoLock(
99 Waiter* waiter,
100 HandleSignalsState* signals_state) OVERRIDE;
101 virtual void StartSerializeImplNoLock(Channel* channel,
102 size_t* max_size,
103 size_t* max_platform_handles) OVERRIDE;
104 virtual bool EndSerializeAndCloseImplNoLock(
105 Channel* channel,
106 void* destination,
107 size_t* actual_size,
108 embedder::PlatformHandleVector* platform_handles) OVERRIDE;
110 // Protected by |lock()|:
111 scoped_refptr<MessagePipe> message_pipe_; // This will be null if closed.
112 unsigned port_;
114 DISALLOW_COPY_AND_ASSIGN(MessagePipeDispatcher);
117 class MessagePipeDispatcherTransport : public DispatcherTransport {
118 public:
119 explicit MessagePipeDispatcherTransport(DispatcherTransport transport);
121 MessagePipe* GetMessagePipe() {
122 return message_pipe_dispatcher()->GetMessagePipeNoLock();
124 unsigned GetPort() { return message_pipe_dispatcher()->GetPortNoLock(); }
126 private:
127 MessagePipeDispatcher* message_pipe_dispatcher() {
128 return static_cast<MessagePipeDispatcher*>(dispatcher());
131 // Copy and assign allowed.
134 } // namespace system
135 } // namespace mojo
137 #endif // MOJO_SYSTEM_MESSAGE_PIPE_DISPATCHER_H_