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_SYSTEM_MESSAGE_PIPE_H_ 6 #define MOJO_SYSTEM_MESSAGE_PIPE_H_ 7 8 #include <stdint.h> 9 10 #include <vector> 11 12 #include "base/macros.h" 13 #include "base/memory/ref_counted.h" 14 #include "base/memory/scoped_ptr.h" 15 #include "base/synchronization/lock.h" 16 #include "mojo/public/c/system/message_pipe.h" 17 #include "mojo/public/c/system/types.h" 18 #include "mojo/system/dispatcher.h" 19 #include "mojo/system/message_in_transit.h" 20 #include "mojo/system/message_pipe_endpoint.h" 21 #include "mojo/system/system_impl_export.h" 22 23 namespace mojo { 24 namespace system { 25 26 class Channel; 27 class Waiter; 28 29 // |MessagePipe| is the secondary object implementing a message pipe (see the 30 // explanatory comment in core.cc). It is typically owned by the dispatcher(s) 31 // corresponding to the local endpoints. This class is thread-safe. 32 class MOJO_SYSTEM_IMPL_EXPORT MessagePipe : 33 public base::RefCountedThreadSafe<MessagePipe> { 34 public: 35 MessagePipe(scoped_ptr<MessagePipeEndpoint> endpoint0, 36 scoped_ptr<MessagePipeEndpoint> endpoint1); 37 38 // Convenience constructor that constructs a |MessagePipe| with two new 39 // |LocalMessagePipeEndpoint|s. 40 MessagePipe(); 41 42 // Gets the other port number (i.e., 0 -> 1, 1 -> 0). 43 static unsigned GetPeerPort(unsigned port); 44 45 // Gets the type of the endpoint (used for assertions, etc.). 46 MessagePipeEndpoint::Type GetType(unsigned port); 47 48 // These are called by the dispatcher to implement its methods of 49 // corresponding names. In all cases, the port |port| must be open. 50 void CancelAllWaiters(unsigned port); 51 void Close(unsigned port); 52 // Unlike |MessagePipeDispatcher::WriteMessage()|, this does not validate its 53 // arguments. 54 MojoResult WriteMessage(unsigned port, 55 const void* bytes, 56 uint32_t num_bytes, 57 std::vector<DispatcherTransport>* transports, 58 MojoWriteMessageFlags flags); 59 // Unlike |MessagePipeDispatcher::ReadMessage()|, this does not validate its 60 // arguments. 61 MojoResult ReadMessage(unsigned port, 62 void* bytes, 63 uint32_t* num_bytes, 64 DispatcherVector* dispatchers, 65 uint32_t* num_dispatchers, 66 MojoReadMessageFlags flags); 67 MojoResult AddWaiter(unsigned port, 68 Waiter* waiter, 69 MojoHandleSignals signals, 70 uint32_t context); 71 void RemoveWaiter(unsigned port, Waiter* waiter); 72 73 // This is called by the dispatcher to convert a local endpoint to a proxy 74 // endpoint. 75 void ConvertLocalToProxy(unsigned port); 76 77 // This is used by |Channel| to enqueue messages (typically to a 78 // |LocalMessagePipeEndpoint|). Unlike |WriteMessage()|, |port| is the 79 // *destination* port. 80 MojoResult EnqueueMessage(unsigned port, 81 scoped_ptr<MessageInTransit> message); 82 83 // These are used by |Channel|. 84 bool Attach(unsigned port, 85 scoped_refptr<Channel> channel, 86 MessageInTransit::EndpointId local_id); 87 void Run(unsigned port, MessageInTransit::EndpointId remote_id); 88 void OnRemove(unsigned port); 89 90 private: 91 friend class base::RefCountedThreadSafe<MessagePipe>; 92 virtual ~MessagePipe(); 93 94 // This is used internally by |WriteMessage()| and by |EnqueueMessage()|. 95 // |transports| may be non-null only if it's nonempty and |message| has no 96 // dispatchers attached. 97 MojoResult EnqueueMessageInternal( 98 unsigned port, 99 scoped_ptr<MessageInTransit> message, 100 std::vector<DispatcherTransport>* transports); 101 102 // Helper for |EnqueueMessageInternal()|. Must be called with |lock_| held. 103 MojoResult AttachTransportsNoLock( 104 unsigned port, 105 MessageInTransit* message, 106 std::vector<DispatcherTransport>* transports); 107 108 // Used by |EnqueueMessageInternal()| to handle control messages that are 109 // actually meant for us. 110 MojoResult HandleControlMessage(unsigned port, 111 scoped_ptr<MessageInTransit> message); 112 113 base::Lock lock_; // Protects the following members. 114 scoped_ptr<MessagePipeEndpoint> endpoints_[2]; 115 116 DISALLOW_COPY_AND_ASSIGN(MessagePipe); 117 }; 118 119 } // namespace system 120 } // namespace mojo 121 122 #endif // MOJO_SYSTEM_MESSAGE_PIPE_H_ 123