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_LIB_ASSOCIATED_INTERFACE_PTR_STATE_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_ASSOCIATED_INTERFACE_PTR_STATE_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_ASSOCIATED_INTERFACE_PTR_STATE_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_ASSOCIATED_INTERFACE_PTR_STATE_H_ |
7 | 7 |
8 #include <algorithm> // For |std::swap()|. | 8 #include <algorithm> // For |std::swap()|. |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "mojo/public/cpp/bindings/associated_group.h" | 12 #include "mojo/public/cpp/bindings/associated_group.h" |
13 #include "mojo/public/cpp/bindings/associated_interface_ptr_info.h" | 13 #include "mojo/public/cpp/bindings/associated_interface_ptr_info.h" |
14 #include "mojo/public/cpp/bindings/callback.h" | 14 #include "mojo/public/cpp/bindings/callback.h" |
15 #include "mojo/public/cpp/bindings/lib/control_message_proxy.h" | 15 #include "mojo/public/cpp/bindings/lib/control_message_proxy.h" |
16 #include "mojo/public/cpp/bindings/lib/interface_endpoint_client.h" | 16 #include "mojo/public/cpp/bindings/lib/interface_endpoint_client.h" |
17 #include "mojo/public/cpp/bindings/lib/interface_id.h" | 17 #include "mojo/public/cpp/bindings/lib/interface_id.h" |
18 #include "mojo/public/cpp/bindings/lib/multiplex_router.h" | 18 #include "mojo/public/cpp/bindings/lib/multiplex_router.h" |
19 #include "mojo/public/cpp/bindings/lib/scoped_interface_endpoint_handle.h" | 19 #include "mojo/public/cpp/bindings/lib/scoped_interface_endpoint_handle.h" |
20 | 20 |
21 namespace mojo { | 21 namespace mojo { |
22 namespace internal { | 22 namespace internal { |
23 | 23 |
24 template <typename Interface> | 24 template <typename Interface> |
25 class AssociatedInterfacePtrState { | 25 class AssociatedInterfacePtrState { |
26 public: | 26 public: |
| 27 using GenericInterface = typename Interface::GenericInterface; |
| 28 |
27 AssociatedInterfacePtrState() : version_(0u) {} | 29 AssociatedInterfacePtrState() : version_(0u) {} |
28 | 30 |
29 ~AssociatedInterfacePtrState() { | 31 ~AssociatedInterfacePtrState() { |
30 endpoint_client_.reset(); | 32 endpoint_client_.reset(); |
31 proxy_.reset(); | 33 proxy_.reset(); |
32 } | 34 } |
33 | 35 |
34 Interface* instance() { | 36 Interface* instance() { |
35 // This will be null if the object is not bound. | 37 // This will be null if the object is not bound. |
36 return proxy_.get(); | 38 return proxy_.get(); |
(...skipping 25 matching lines...) Expand all Loading... |
62 static_cast<ControlMessageProxy*>(proxy_.get())->RequireVersion(version); | 64 static_cast<ControlMessageProxy*>(proxy_.get())->RequireVersion(version); |
63 } | 65 } |
64 | 66 |
65 void Swap(AssociatedInterfacePtrState* other) { | 67 void Swap(AssociatedInterfacePtrState* other) { |
66 using std::swap; | 68 using std::swap; |
67 swap(other->endpoint_client_, endpoint_client_); | 69 swap(other->endpoint_client_, endpoint_client_); |
68 swap(other->proxy_, proxy_); | 70 swap(other->proxy_, proxy_); |
69 swap(other->version_, version_); | 71 swap(other->version_, version_); |
70 } | 72 } |
71 | 73 |
72 void Bind(AssociatedInterfacePtrInfo<Interface> info) { | 74 void Bind(AssociatedInterfacePtrInfo<GenericInterface> info) { |
73 DCHECK(!endpoint_client_); | 75 DCHECK(!endpoint_client_); |
74 DCHECK(!proxy_); | 76 DCHECK(!proxy_); |
75 DCHECK_EQ(0u, version_); | 77 DCHECK_EQ(0u, version_); |
76 DCHECK(info.is_valid()); | 78 DCHECK(info.is_valid()); |
77 | 79 |
78 version_ = info.version(); | 80 version_ = info.version(); |
79 endpoint_client_.reset(new InterfaceEndpointClient( | 81 endpoint_client_.reset(new InterfaceEndpointClient( |
80 AssociatedInterfacePtrInfoHelper::PassHandle(&info), nullptr, | 82 AssociatedInterfacePtrInfoHelper::PassHandle(&info), nullptr, |
81 make_scoped_ptr(new typename Interface::ResponseValidator_()))); | 83 make_scoped_ptr(new typename Interface::ResponseValidator_()))); |
82 proxy_.reset(new Proxy(endpoint_client_.get())); | 84 proxy_.reset(new Proxy(endpoint_client_.get())); |
83 proxy_->serialization_context()->router = endpoint_client_->router(); | 85 proxy_->serialization_context()->router = endpoint_client_->router(); |
84 } | 86 } |
85 | 87 |
86 // After this method is called, the object is in an invalid state and | 88 // After this method is called, the object is in an invalid state and |
87 // shouldn't be reused. | 89 // shouldn't be reused. |
88 AssociatedInterfacePtrInfo<Interface> PassInterface() { | 90 AssociatedInterfacePtrInfo<GenericInterface> PassInterface() { |
89 ScopedInterfaceEndpointHandle handle = endpoint_client_->PassHandle(); | 91 ScopedInterfaceEndpointHandle handle = endpoint_client_->PassHandle(); |
90 endpoint_client_.reset(); | 92 endpoint_client_.reset(); |
91 proxy_.reset(); | 93 proxy_.reset(); |
92 | 94 |
93 AssociatedInterfacePtrInfo<Interface> result; | 95 AssociatedInterfacePtrInfo<GenericInterface> result; |
94 result.set_version(version_); | 96 result.set_version(version_); |
95 AssociatedInterfacePtrInfoHelper::SetHandle(&result, handle.Pass()); | 97 AssociatedInterfacePtrInfoHelper::SetHandle(&result, handle.Pass()); |
96 return result.Pass(); | 98 return result.Pass(); |
97 } | 99 } |
98 | 100 |
99 bool is_bound() const { return !!endpoint_client_; } | 101 bool is_bound() const { return !!endpoint_client_; } |
100 | 102 |
101 bool encountered_error() const { | 103 bool encountered_error() const { |
102 return endpoint_client_ ? endpoint_client_->encountered_error() : false; | 104 return endpoint_client_ ? endpoint_client_->encountered_error() : false; |
103 } | 105 } |
(...skipping 20 matching lines...) Expand all Loading... |
124 | 126 |
125 uint32_t version_; | 127 uint32_t version_; |
126 | 128 |
127 DISALLOW_COPY_AND_ASSIGN(AssociatedInterfacePtrState); | 129 DISALLOW_COPY_AND_ASSIGN(AssociatedInterfacePtrState); |
128 }; | 130 }; |
129 | 131 |
130 } // namespace internal | 132 } // namespace internal |
131 } // namespace mojo | 133 } // namespace mojo |
132 | 134 |
133 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ASSOCIATED_INTERFACE_PTR_STATE_H_ | 135 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ASSOCIATED_INTERFACE_PTR_STATE_H_ |
OLD | NEW |