OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef MOJO_EDK_SYSTEM_MESSAGE_PIPE_DISPATCHER_H_ | |
6 #define MOJO_EDK_SYSTEM_MESSAGE_PIPE_DISPATCHER_H_ | |
7 | |
8 #include "base/macros.h" | |
9 #include "base/memory/ref_counted.h" | |
10 #include "mojo/edk/system/dispatcher.h" | |
11 #include "mojo/edk/system/memory.h" | |
12 #include "mojo/edk/system/system_impl_export.h" | |
13 | |
14 namespace mojo { | |
15 namespace system { | |
16 | |
17 class ChannelEndpoint; | |
18 class MessagePipe; | |
19 class MessagePipeDispatcherTransport; | |
20 | |
21 // This is the |Dispatcher| implementation for message pipes (created by the | |
22 // Mojo primitive |MojoCreateMessagePipe()|). This class is thread-safe. | |
23 class MOJO_SYSTEM_IMPL_EXPORT MessagePipeDispatcher : public Dispatcher { | |
24 public: | |
25 // The default options to use for |MojoCreateMessagePipe()|. (Real uses | |
26 // should obtain this via |ValidateCreateOptions()| with a null |in_options|; | |
27 // this is exposed directly for testing convenience.) | |
28 static const MojoCreateMessagePipeOptions kDefaultCreateOptions; | |
29 | |
30 MessagePipeDispatcher( | |
31 const MojoCreateMessagePipeOptions& /*validated_options*/); | |
32 | |
33 // Validates and/or sets default options for |MojoCreateMessagePipeOptions|. | |
34 // If non-null, |in_options| must point to a struct of at least | |
35 // |in_options->struct_size| bytes. |out_options| must point to a (current) | |
36 // |MojoCreateMessagePipeOptions| and will be entirely overwritten on success | |
37 // (it may be partly overwritten on failure). | |
38 static MojoResult ValidateCreateOptions( | |
39 UserPointer<const MojoCreateMessagePipeOptions> in_options, | |
40 MojoCreateMessagePipeOptions* out_options); | |
41 | |
42 // Must be called before any other methods. (This method is not thread-safe.) | |
43 void Init(scoped_refptr<MessagePipe> message_pipe, unsigned port); | |
44 | |
45 // |Dispatcher| public methods: | |
46 Type GetType() const override; | |
47 | |
48 // Creates a |MessagePipe| with a local endpoint (at port 0) and a proxy | |
49 // endpoint, and creates/initializes a |MessagePipeDispatcher| (attached to | |
50 // the message pipe, port 0). | |
51 // TODO(vtl): This currently uses |kDefaultCreateOptions|, which is okay since | |
52 // there aren't any options, but eventually options should be plumbed through. | |
53 static scoped_refptr<MessagePipeDispatcher> CreateRemoteMessagePipe( | |
54 scoped_refptr<ChannelEndpoint>* channel_endpoint); | |
55 | |
56 // The "opposite" of |SerializeAndClose()|. (Typically this is called by | |
57 // |Dispatcher::Deserialize()|.) | |
58 static scoped_refptr<MessagePipeDispatcher> Deserialize(Channel* channel, | |
59 const void* source, | |
60 size_t size); | |
61 | |
62 private: | |
63 friend class MessagePipeDispatcherTransport; | |
64 | |
65 ~MessagePipeDispatcher() override; | |
66 | |
67 // Gets a dumb pointer to |message_pipe_|. This must be called under the | |
68 // |Dispatcher| lock (that it's a dumb pointer is okay since it's under lock). | |
69 // This is needed when sending handles across processes, where nontrivial, | |
70 // invasive work needs to be done. | |
71 MessagePipe* GetMessagePipeNoLock() const; | |
72 // Similarly for the port. | |
73 unsigned GetPortNoLock() const; | |
74 | |
75 // |Dispatcher| protected methods: | |
76 void CancelAllAwakablesNoLock() override; | |
77 void CloseImplNoLock() override; | |
78 scoped_refptr<Dispatcher> CreateEquivalentDispatcherAndCloseImplNoLock() | |
79 override; | |
80 MojoResult WriteMessageImplNoLock( | |
81 UserPointer<const void> bytes, | |
82 uint32_t num_bytes, | |
83 std::vector<DispatcherTransport>* transports, | |
84 MojoWriteMessageFlags flags) override; | |
85 MojoResult ReadMessageImplNoLock(UserPointer<void> bytes, | |
86 UserPointer<uint32_t> num_bytes, | |
87 DispatcherVector* dispatchers, | |
88 uint32_t* num_dispatchers, | |
89 MojoReadMessageFlags flags) override; | |
90 HandleSignalsState GetHandleSignalsStateImplNoLock() const override; | |
91 MojoResult AddAwakableImplNoLock(Awakable* awakable, | |
92 MojoHandleSignals signals, | |
93 uint32_t context, | |
94 HandleSignalsState* signals_state) override; | |
95 void RemoveAwakableImplNoLock(Awakable* awakable, | |
96 HandleSignalsState* signals_state) override; | |
97 void StartSerializeImplNoLock(Channel* channel, | |
98 size_t* max_size, | |
99 size_t* max_platform_handles) override; | |
100 bool EndSerializeAndCloseImplNoLock( | |
101 Channel* channel, | |
102 void* destination, | |
103 size_t* actual_size, | |
104 embedder::PlatformHandleVector* platform_handles) override; | |
105 | |
106 // Protected by |lock()|: | |
107 scoped_refptr<MessagePipe> message_pipe_; // This will be null if closed. | |
108 unsigned port_; | |
109 | |
110 DISALLOW_COPY_AND_ASSIGN(MessagePipeDispatcher); | |
111 }; | |
112 | |
113 class MessagePipeDispatcherTransport : public DispatcherTransport { | |
114 public: | |
115 explicit MessagePipeDispatcherTransport(DispatcherTransport transport); | |
116 | |
117 MessagePipe* GetMessagePipe() { | |
118 return message_pipe_dispatcher()->GetMessagePipeNoLock(); | |
119 } | |
120 unsigned GetPort() { return message_pipe_dispatcher()->GetPortNoLock(); } | |
121 | |
122 private: | |
123 MessagePipeDispatcher* message_pipe_dispatcher() { | |
124 return static_cast<MessagePipeDispatcher*>(dispatcher()); | |
125 } | |
126 | |
127 // Copy and assign allowed. | |
128 }; | |
129 | |
130 } // namespace system | |
131 } // namespace mojo | |
132 | |
133 #endif // MOJO_EDK_SYSTEM_MESSAGE_PIPE_DISPATCHER_H_ | |
OLD | NEW |