1 // Copyright 2016 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_PUBLIC_CPP_BINDINGS_ASSOCIATED_GROUP_CONTROLLER_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_GROUP_CONTROLLER_H_ 7 8 #include <memory> 9 10 #include "base/macros.h" 11 #include "base/memory/ref_counted.h" 12 #include "base/optional.h" 13 #include "base/single_thread_task_runner.h" 14 #include "mojo/public/cpp/bindings/bindings_export.h" 15 #include "mojo/public/cpp/bindings/disconnect_reason.h" 16 #include "mojo/public/cpp/bindings/interface_id.h" 17 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" 18 19 namespace mojo { 20 21 class InterfaceEndpointClient; 22 class InterfaceEndpointController; 23 24 // An internal interface used to manage endpoints within an associated group, 25 // which corresponds to one end of a message pipe. 26 class MOJO_CPP_BINDINGS_EXPORT AssociatedGroupController 27 : public base::RefCountedThreadSafe<AssociatedGroupController> { 28 public: 29 // Associates an interface with this AssociatedGroupController's message pipe. 30 // It takes ownership of |handle_to_send| and returns an interface ID that 31 // could be sent by any endpoints within the same associated group. 32 // If |handle_to_send| is not in pending association state, it returns 33 // kInvalidInterfaceId. Otherwise, the peer handle of |handle_to_send| joins 34 // the associated group and is no longer pending. 35 virtual InterfaceId AssociateInterface( 36 ScopedInterfaceEndpointHandle handle_to_send) = 0; 37 38 // Creates an interface endpoint handle from a given interface ID. The handle 39 // joins this associated group. 40 // Typically, this method is used to (1) create an endpoint handle for the 41 // master interface; or (2) create an endpoint handle on receiving an 42 // interface ID from the message pipe. 43 // 44 // On failure, the method returns an invalid handle. Usually that is because 45 // the ID has already been used to create a handle. 46 virtual ScopedInterfaceEndpointHandle CreateLocalEndpointHandle( 47 InterfaceId id) = 0; 48 49 // Closes an interface endpoint handle. 50 virtual void CloseEndpointHandle( 51 InterfaceId id, 52 const base::Optional<DisconnectReason>& reason) = 0; 53 54 // Attaches a client to the specified endpoint to send and receive messages. 55 // The returned object is still owned by the controller. It must only be used 56 // on the same thread as this call, and only before the client is detached 57 // using DetachEndpointClient(). 58 virtual InterfaceEndpointController* AttachEndpointClient( 59 const ScopedInterfaceEndpointHandle& handle, 60 InterfaceEndpointClient* endpoint_client, 61 scoped_refptr<base::SingleThreadTaskRunner> runner) = 0; 62 63 // Detaches the client attached to the specified endpoint. It must be called 64 // on the same thread as the corresponding AttachEndpointClient() call. 65 virtual void DetachEndpointClient( 66 const ScopedInterfaceEndpointHandle& handle) = 0; 67 68 // Raises an error on the underlying message pipe. It disconnects the pipe 69 // and notifies all interfaces running on this pipe. 70 virtual void RaiseError() = 0; 71 72 protected: 73 friend class base::RefCountedThreadSafe<AssociatedGroupController>; 74 75 // Creates a new ScopedInterfaceEndpointHandle within this associated group. 76 ScopedInterfaceEndpointHandle CreateScopedInterfaceEndpointHandle( 77 InterfaceId id); 78 79 // Notifies that the interface represented by |handle_to_send| and its peer 80 // has been associated with this AssociatedGroupController's message pipe, and 81 // |handle_to_send|'s peer has joined this associated group. (Note: it is the 82 // peer who has joined the associated group; |handle_to_send| will be sent to 83 // the remote side.) 84 // Returns false if |handle_to_send|'s peer has closed. 85 bool NotifyAssociation(ScopedInterfaceEndpointHandle* handle_to_send, 86 InterfaceId id); 87 88 virtual ~AssociatedGroupController(); 89 }; 90 91 } // namespace mojo 92 93 #endif // MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_GROUP_CONTROLLER_H_ 94