| 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_H_ | |
| 6 #define MOJO_EDK_SYSTEM_MESSAGE_PIPE_H_ | |
| 7 | |
| 8 #include <stddef.h> | |
| 9 #include <stdint.h> | |
| 10 | |
| 11 #include <vector> | |
| 12 | |
| 13 #include "base/macros.h" | |
| 14 #include "base/memory/ref_counted.h" | |
| 15 #include "base/memory/scoped_ptr.h" | |
| 16 #include "base/synchronization/lock.h" | |
| 17 #include "mojo/edk/embedder/platform_handle_vector.h" | |
| 18 #include "mojo/edk/system/channel_endpoint_client.h" | |
| 19 #include "mojo/edk/system/dispatcher.h" | |
| 20 #include "mojo/edk/system/handle_signals_state.h" | |
| 21 #include "mojo/edk/system/memory.h" | |
| 22 #include "mojo/edk/system/message_in_transit.h" | |
| 23 #include "mojo/edk/system/message_pipe_endpoint.h" | |
| 24 #include "mojo/edk/system/system_impl_export.h" | |
| 25 #include "mojo/public/c/system/message_pipe.h" | |
| 26 #include "mojo/public/c/system/types.h" | |
| 27 | |
| 28 namespace mojo { | |
| 29 namespace system { | |
| 30 | |
| 31 class Awakable; | |
| 32 class Channel; | |
| 33 class ChannelEndpoint; | |
| 34 class MessageInTransitQueue; | |
| 35 | |
| 36 // |MessagePipe| is the secondary object implementing a message pipe (see the | |
| 37 // explanatory comment in core.cc). It is typically owned by the dispatcher(s) | |
| 38 // corresponding to the local endpoints. This class is thread-safe. | |
| 39 class MOJO_SYSTEM_IMPL_EXPORT MessagePipe : public ChannelEndpointClient { | |
| 40 public: | |
| 41 // Creates a |MessagePipe| with two new |LocalMessagePipeEndpoint|s. | |
| 42 static MessagePipe* CreateLocalLocal(); | |
| 43 | |
| 44 // Creates a |MessagePipe| with a |LocalMessagePipeEndpoint| on port 0 and a | |
| 45 // |ProxyMessagePipeEndpoint| on port 1. |*channel_endpoint| is set to the | |
| 46 // (newly-created) |ChannelEndpoint| for the latter. | |
| 47 static MessagePipe* CreateLocalProxy( | |
| 48 scoped_refptr<ChannelEndpoint>* channel_endpoint); | |
| 49 | |
| 50 // Similar to |CreateLocalProxy()|, except that it'll do so from an existing | |
| 51 // |ChannelEndpoint| (whose |ReplaceClient()| it'll call) and take | |
| 52 // |message_queue|'s contents as already-received incoming messages. If | |
| 53 // |channel_endpoint| is null, this will create a "half-open" message pipe. | |
| 54 static MessagePipe* CreateLocalProxyFromExisting( | |
| 55 MessageInTransitQueue* message_queue, | |
| 56 ChannelEndpoint* channel_endpoint); | |
| 57 | |
| 58 // Creates a |MessagePipe| with a |ProxyMessagePipeEndpoint| on port 0 and a | |
| 59 // |LocalMessagePipeEndpoint| on port 1. |*channel_endpoint| is set to the | |
| 60 // (newly-created) |ChannelEndpoint| for the former. | |
| 61 // Note: This is really only needed in tests (outside of tests, this | |
| 62 // configuration arises from a local message pipe having its port 0 | |
| 63 // "converted" using |ConvertLocalToProxy()|). | |
| 64 static MessagePipe* CreateProxyLocal( | |
| 65 scoped_refptr<ChannelEndpoint>* channel_endpoint); | |
| 66 | |
| 67 // Gets the other port number (i.e., 0 -> 1, 1 -> 0). | |
| 68 static unsigned GetPeerPort(unsigned port); | |
| 69 | |
| 70 // Used by |MessagePipeDispatcher::Deserialize()|. Returns true on success (in | |
| 71 // which case, |*message_pipe|/|*port| are set appropriately) and false on | |
| 72 // failure (in which case |*message_pipe| may or may not be set to null). | |
| 73 static bool Deserialize(Channel* channel, | |
| 74 const void* source, | |
| 75 size_t size, | |
| 76 scoped_refptr<MessagePipe>* message_pipe, | |
| 77 unsigned* port); | |
| 78 | |
| 79 // Gets the type of the endpoint (used for assertions, etc.). | |
| 80 MessagePipeEndpoint::Type GetType(unsigned port); | |
| 81 | |
| 82 // These are called by the dispatcher to implement its methods of | |
| 83 // corresponding names. In all cases, the port |port| must be open. | |
| 84 void CancelAllAwakables(unsigned port); | |
| 85 void Close(unsigned port); | |
| 86 // Unlike |MessagePipeDispatcher::WriteMessage()|, this does not validate its | |
| 87 // arguments. | |
| 88 MojoResult WriteMessage(unsigned port, | |
| 89 UserPointer<const void> bytes, | |
| 90 uint32_t num_bytes, | |
| 91 std::vector<DispatcherTransport>* transports, | |
| 92 MojoWriteMessageFlags flags); | |
| 93 MojoResult ReadMessage(unsigned port, | |
| 94 UserPointer<void> bytes, | |
| 95 UserPointer<uint32_t> num_bytes, | |
| 96 DispatcherVector* dispatchers, | |
| 97 uint32_t* num_dispatchers, | |
| 98 MojoReadMessageFlags flags); | |
| 99 HandleSignalsState GetHandleSignalsState(unsigned port) const; | |
| 100 MojoResult AddAwakable(unsigned port, | |
| 101 Awakable* awakable, | |
| 102 MojoHandleSignals signals, | |
| 103 uint32_t context, | |
| 104 HandleSignalsState* signals_state); | |
| 105 void RemoveAwakable(unsigned port, | |
| 106 Awakable* awakable, | |
| 107 HandleSignalsState* signals_state); | |
| 108 void StartSerialize(unsigned port, | |
| 109 Channel* channel, | |
| 110 size_t* max_size, | |
| 111 size_t* max_platform_handles); | |
| 112 bool EndSerialize(unsigned port, | |
| 113 Channel* channel, | |
| 114 void* destination, | |
| 115 size_t* actual_size, | |
| 116 embedder::PlatformHandleVector* platform_handles); | |
| 117 | |
| 118 // |ChannelEndpointClient| methods: | |
| 119 bool OnReadMessage(unsigned port, MessageInTransit* message) override; | |
| 120 void OnDetachFromChannel(unsigned port) override; | |
| 121 | |
| 122 private: | |
| 123 MessagePipe(); | |
| 124 ~MessagePipe() override; | |
| 125 | |
| 126 // This is used internally by |WriteMessage()| and by |OnReadMessage()|. | |
| 127 // |transports| may be non-null only if it's nonempty and |message| has no | |
| 128 // dispatchers attached. Must be called with |lock_| held. | |
| 129 MojoResult EnqueueMessageNoLock(unsigned port, | |
| 130 scoped_ptr<MessageInTransit> message, | |
| 131 std::vector<DispatcherTransport>* transports); | |
| 132 | |
| 133 // Helper for |EnqueueMessageNoLock()|. Must be called with |lock_| held. | |
| 134 MojoResult AttachTransportsNoLock( | |
| 135 unsigned port, | |
| 136 MessageInTransit* message, | |
| 137 std::vector<DispatcherTransport>* transports); | |
| 138 | |
| 139 base::Lock lock_; // Protects the following members. | |
| 140 scoped_ptr<MessagePipeEndpoint> endpoints_[2]; | |
| 141 | |
| 142 DISALLOW_COPY_AND_ASSIGN(MessagePipe); | |
| 143 }; | |
| 144 | |
| 145 } // namespace system | |
| 146 } // namespace mojo | |
| 147 | |
| 148 #endif // MOJO_EDK_SYSTEM_MESSAGE_PIPE_H_ | |
| OLD | NEW |