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/component_export.h"
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/optional.h"
14 #include "base/sequenced_task_runner.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.
COMPONENT_EXPORT(MOJO_CPP_BINDINGS_BASE)26 class COMPONENT_EXPORT(MOJO_CPP_BINDINGS_BASE) 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 sequence 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::SequencedTaskRunner> runner) = 0;
62
63 // Detaches the client attached to the specified endpoint. It must be called
64 // on the same sequence 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 // Indicates whether or this endpoint prefers to accept outgoing messages in
73 // serializaed form only.
74 virtual bool PrefersSerializedMessages() = 0;
75
76 protected:
77 friend class base::RefCountedThreadSafe<AssociatedGroupController>;
78
79 // Creates a new ScopedInterfaceEndpointHandle within this associated group.
80 ScopedInterfaceEndpointHandle CreateScopedInterfaceEndpointHandle(
81 InterfaceId id);
82
83 // Notifies that the interface represented by |handle_to_send| and its peer
84 // has been associated with this AssociatedGroupController's message pipe, and
85 // |handle_to_send|'s peer has joined this associated group. (Note: it is the
86 // peer who has joined the associated group; |handle_to_send| will be sent to
87 // the remote side.)
88 // Returns false if |handle_to_send|'s peer has closed.
89 bool NotifyAssociation(ScopedInterfaceEndpointHandle* handle_to_send,
90 InterfaceId id);
91
92 virtual ~AssociatedGroupController();
93 };
94
95 } // namespace mojo
96
97 #endif // MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_GROUP_CONTROLLER_H_
98