OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_GROUP_H_ | |
6 #define MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_GROUP_H_ | |
7 | |
8 #include "base/memory/ref_counted.h" | |
9 #include "mojo/public/cpp/bindings/associated_interface_ptr_info.h" | |
10 #include "mojo/public/cpp/bindings/associated_interface_request.h" | |
11 #include "mojo/public/cpp/bindings/lib/scoped_interface_endpoint_handle.h" | |
12 | |
13 namespace mojo { | |
14 | |
15 namespace internal { | |
16 class MultiplexRouter; | |
17 } | |
18 | |
19 // AssociatedGroup refers to all the interface endpoints running at one end of a | |
20 // message pipe. It is used to create associated interfaces for that message | |
21 // pipe. | |
22 // It is thread safe and cheap to make copies. | |
23 class AssociatedGroup { | |
24 public: | |
25 AssociatedGroup(); | |
26 AssociatedGroup(const AssociatedGroup& other); | |
27 | |
28 ~AssociatedGroup(); | |
29 | |
30 AssociatedGroup& operator=(const AssociatedGroup& other); | |
31 | |
32 enum AssociatedInterfaceConfig { WILL_PASS_PTR, WILL_PASS_REQUEST }; | |
sky
2015/11/23 21:05:41
nit: enum's should be before constructor/destructo
yzshen1
2015/11/23 21:50:04
Done.
| |
33 | |
34 // |config| indicates whether |ptr_info| or |request| will be sent to the | |
35 // remote side of the message pipe. | |
36 // | |
37 // NOTE: If |config| is |WILL_PASS_REQUEST|, you will want to bind |ptr_info| | |
38 // to a local AssociatedInterfacePtr to make calls. However, there is one | |
39 // restriction: the pointer should NOT be used to make calls before |request| | |
40 // is sent. Violating that will cause the message pipe to be closed. On the | |
41 // other hand, as soon as |request| is sent, the pointer is usable. There is | |
42 // no need to wait until |request| is bound to an implementation at the remote | |
43 // side. | |
44 template <typename T> | |
45 void CreateAssociatedInterface(AssociatedInterfaceConfig config, | |
46 AssociatedInterfacePtrInfo<T>* ptr_info, | |
47 AssociatedInterfaceRequest<T>* request) { | |
48 internal::ScopedInterfaceEndpointHandle local; | |
49 internal::ScopedInterfaceEndpointHandle remote; | |
50 CreateEndpointHandlePair(&local, &remote); | |
51 | |
52 if (!local.is_valid() || !remote.is_valid()) { | |
53 *ptr_info = AssociatedInterfacePtrInfo<T>(); | |
54 *request = AssociatedInterfaceRequest<T>(); | |
55 return; | |
56 } | |
57 | |
58 if (config == WILL_PASS_PTR) { | |
59 internal::AssociatedInterfacePtrInfoHelper::SetHandle(ptr_info, | |
60 remote.Pass()); | |
61 // The implementation is local, therefore set the version according to | |
62 // the interface definition that this code is built against. | |
63 ptr_info->set_version(T::Version_); | |
64 internal::AssociatedInterfaceRequestHelper::SetHandle(request, | |
65 local.Pass()); | |
66 } else { | |
67 internal::AssociatedInterfacePtrInfoHelper::SetHandle(ptr_info, | |
68 local.Pass()); | |
69 // The implementation is remote, we don't know about its actual version | |
70 // yet. | |
71 ptr_info->set_version(0u); | |
72 internal::AssociatedInterfaceRequestHelper::SetHandle(request, | |
73 remote.Pass()); | |
74 } | |
75 } | |
76 | |
77 private: | |
78 friend class internal::MultiplexRouter; | |
79 | |
80 void CreateEndpointHandlePair( | |
81 internal::ScopedInterfaceEndpointHandle* local_endpoint, | |
82 internal::ScopedInterfaceEndpointHandle* remote_endpoint); | |
83 | |
84 scoped_refptr<internal::MultiplexRouter> router_; | |
85 }; | |
86 | |
87 } // namespace mojo | |
88 | |
89 #endif // MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_GROUP_H_ | |
OLD | NEW |