OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_GROUP_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_GROUP_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_GROUP_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_GROUP_H_ |
7 | 7 |
8 #include <utility> | 8 #include "base/callback.h" |
9 | |
10 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
11 #include "mojo/public/cpp/bindings/associated_interface_ptr_info.h" | 10 #include "mojo/public/cpp/bindings/associated_interface_ptr_info.h" |
12 #include "mojo/public/cpp/bindings/associated_interface_request.h" | 11 #include "mojo/public/cpp/bindings/associated_interface_request.h" |
13 #include "mojo/public/cpp/bindings/bindings_export.h" | 12 #include "mojo/public/cpp/bindings/bindings_export.h" |
14 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" | 13 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" |
15 | 14 |
16 namespace mojo { | 15 namespace mojo { |
17 | 16 |
18 class AssociatedGroupController; | 17 class AssociatedGroupController; |
19 | 18 |
20 // AssociatedGroup refers to all the interface endpoints running at one end of a | 19 // AssociatedGroup refers to all the interface endpoints running at one end of a |
21 // message pipe. It is used to create associated interfaces for that message | 20 // message pipe. |
22 // pipe. | |
23 // It is thread safe and cheap to make copies. | 21 // It is thread safe and cheap to make copies. |
24 class MOJO_CPP_BINDINGS_EXPORT AssociatedGroup { | 22 class MOJO_CPP_BINDINGS_EXPORT AssociatedGroup { |
25 public: | 23 public: |
26 // Configuration used by CreateAssociatedInterface(). Please see the comments | 24 AssociatedGroup(); |
27 // of that method for more details. | |
28 enum AssociatedInterfaceConfig { WILL_PASS_PTR, WILL_PASS_REQUEST }; | |
29 | 25 |
30 AssociatedGroup(); | 26 explicit AssociatedGroup(scoped_refptr<AssociatedGroupController> controller); |
| 27 |
| 28 explicit AssociatedGroup(const ScopedInterfaceEndpointHandle& handle); |
| 29 |
31 AssociatedGroup(const AssociatedGroup& other); | 30 AssociatedGroup(const AssociatedGroup& other); |
32 | 31 |
33 ~AssociatedGroup(); | 32 ~AssociatedGroup(); |
34 | 33 |
35 AssociatedGroup& operator=(const AssociatedGroup& other); | 34 AssociatedGroup& operator=(const AssociatedGroup& other); |
36 | 35 |
| 36 // The return value of this getter if this object is initialized with a |
| 37 // ScopedInterfaceEndpointHandle: |
| 38 // - If the handle is invalid, the return value will always be null. |
| 39 // - If the handle is valid and non-pending, the return value will be |
| 40 // non-null and remain unchanged even if the handle is later reset. |
| 41 // - If the handle is pending asssociation, the return value will initially |
| 42 // be null, change to non-null when/if the handle is associated, and |
| 43 // remain unchanged ever since. |
| 44 AssociatedGroupController* GetController(); |
| 45 |
| 46 // TODO(yzshen): Remove the following public method. It is not needed anymore. |
| 47 // Configuration used by CreateAssociatedInterface(). Please see the comments |
| 48 // of that method for more details. |
| 49 enum AssociatedInterfaceConfig { WILL_PASS_PTR, WILL_PASS_REQUEST }; |
| 50 |
37 // |config| indicates whether |ptr_info| or |request| will be sent to the | 51 // |config| indicates whether |ptr_info| or |request| will be sent to the |
38 // remote side of the message pipe. | 52 // remote side of the message pipe. |
39 // | 53 // |
40 // NOTE: If |config| is |WILL_PASS_REQUEST|, you will want to bind |ptr_info| | 54 // NOTE: If |config| is |WILL_PASS_REQUEST|, you will want to bind |ptr_info| |
41 // to a local AssociatedInterfacePtr to make calls. However, there is one | 55 // to a local AssociatedInterfacePtr to make calls. However, there is one |
42 // restriction: the pointer should NOT be used to make calls before |request| | 56 // restriction: the pointer should NOT be used to make calls before |request| |
43 // is sent. Violating that will cause the message pipe to be closed. On the | 57 // is sent. Violating that will cause the message pipe to be closed. On the |
44 // other hand, as soon as |request| is sent, the pointer is usable. There is | 58 // other hand, as soon as |request| is sent, the pointer is usable. There is |
45 // no need to wait until |request| is bound to an implementation at the remote | 59 // no need to wait until |request| is bound to an implementation at the remote |
46 // side. | 60 // side. |
47 template <typename T> | 61 template <typename T> |
48 void CreateAssociatedInterface( | 62 void CreateAssociatedInterface(AssociatedInterfaceConfig config, |
49 AssociatedInterfaceConfig config, | 63 AssociatedInterfacePtrInfo<T>* ptr_info, |
50 AssociatedInterfacePtrInfo<T>* ptr_info, | 64 AssociatedInterfaceRequest<T>* request) { |
51 AssociatedInterfaceRequest<T>* request) { | 65 ScopedInterfaceEndpointHandle handle0; |
52 ScopedInterfaceEndpointHandle local; | 66 ScopedInterfaceEndpointHandle handle1; |
53 ScopedInterfaceEndpointHandle remote; | 67 ScopedInterfaceEndpointHandle::CreatePairPendingAssociation(&handle0, |
54 CreateEndpointHandlePair(&local, &remote); | 68 &handle1); |
55 | 69 |
56 if (!local.is_valid() || !remote.is_valid()) { | 70 ptr_info->set_handle(std::move(handle0)); |
57 *ptr_info = AssociatedInterfacePtrInfo<T>(); | 71 request->Bind(std::move(handle1)); |
58 *request = AssociatedInterfaceRequest<T>(); | |
59 return; | |
60 } | |
61 | 72 |
62 if (config == WILL_PASS_PTR) { | 73 if (config == WILL_PASS_PTR) { |
63 ptr_info->set_handle(std::move(remote)); | |
64 | |
65 // The implementation is local, therefore set the version according to | 74 // The implementation is local, therefore set the version according to |
66 // the interface definition that this code is built against. | 75 // the interface definition that this code is built against. |
67 ptr_info->set_version(T::Version_); | 76 ptr_info->set_version(T::Version_); |
68 request->Bind(std::move(local)); | |
69 } else { | 77 } else { |
70 ptr_info->set_handle(std::move(local)); | |
71 | |
72 // The implementation is remote, we don't know about its actual version | 78 // The implementation is remote, we don't know about its actual version |
73 // yet. | 79 // yet. |
74 ptr_info->set_version(0u); | 80 ptr_info->set_version(0u); |
75 request->Bind(std::move(remote)); | |
76 } | 81 } |
77 } | 82 } |
78 | 83 |
79 private: | 84 private: |
80 friend class AssociatedGroupController; | 85 base::Callback<AssociatedGroupController*()> controller_getter_; |
81 | |
82 void CreateEndpointHandlePair( | |
83 ScopedInterfaceEndpointHandle* local_endpoint, | |
84 ScopedInterfaceEndpointHandle* remote_endpoint); | |
85 | |
86 scoped_refptr<AssociatedGroupController> controller_; | 86 scoped_refptr<AssociatedGroupController> controller_; |
87 }; | 87 }; |
88 | 88 |
89 } // namespace mojo | 89 } // namespace mojo |
90 | 90 |
91 #endif // MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_GROUP_H_ | 91 #endif // MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_GROUP_H_ |
OLD | NEW |