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 <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <algorithm> // For |std::swap()|. | 10 #include <algorithm> // For |std::swap()|. |
11 #include <memory> | 11 #include <memory> |
12 #include <utility> | 12 #include <utility> |
13 | 13 |
14 #include "base/bind.h" | 14 #include "base/bind.h" |
15 #include "base/callback_forward.h" | 15 #include "base/callback_forward.h" |
16 #include "base/macros.h" | 16 #include "base/macros.h" |
17 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
18 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
19 #include "base/single_thread_task_runner.h" | 19 #include "base/single_thread_task_runner.h" |
20 #include "mojo/public/cpp/bindings/associated_group.h" | 20 #include "mojo/public/cpp/bindings/associated_group.h" |
21 #include "mojo/public/cpp/bindings/associated_group_controller.h" | 21 #include "mojo/public/cpp/bindings/associated_group_controller.h" |
22 #include "mojo/public/cpp/bindings/associated_interface_ptr_info.h" | 22 #include "mojo/public/cpp/bindings/associated_interface_ptr_info.h" |
23 #include "mojo/public/cpp/bindings/interface_endpoint_client.h" | 23 #include "mojo/public/cpp/bindings/interface_endpoint_client.h" |
24 #include "mojo/public/cpp/bindings/interface_id.h" | 24 #include "mojo/public/cpp/bindings/interface_id.h" |
25 #include "mojo/public/cpp/bindings/lib/control_message_handler.h" | |
25 #include "mojo/public/cpp/bindings/lib/control_message_proxy.h" | 26 #include "mojo/public/cpp/bindings/lib/control_message_proxy.h" |
26 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" | 27 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" |
27 #include "mojo/public/cpp/system/message_pipe.h" | 28 #include "mojo/public/cpp/system/message_pipe.h" |
28 | 29 |
29 namespace mojo { | 30 namespace mojo { |
30 namespace internal { | 31 namespace internal { |
31 | 32 |
32 template <typename Interface> | 33 template <typename Interface> |
33 class AssociatedInterfacePtrState { | 34 class AssociatedInterfacePtrState { |
34 public: | 35 public: |
(...skipping 10 matching lines...) Expand all Loading... | |
45 } | 46 } |
46 | 47 |
47 uint32_t version() const { return version_; } | 48 uint32_t version() const { return version_; } |
48 | 49 |
49 uint32_t interface_id() const { | 50 uint32_t interface_id() const { |
50 DCHECK(is_bound()); | 51 DCHECK(is_bound()); |
51 return endpoint_client_->interface_id(); | 52 return endpoint_client_->interface_id(); |
52 } | 53 } |
53 | 54 |
54 void QueryVersion(const base::Callback<void(uint32_t)>& callback) { | 55 void QueryVersion(const base::Callback<void(uint32_t)>& callback) { |
55 // Do a static cast in case the interface contains methods with the same | 56 // It is safe to capture |this| because the callback won't be run after this |
56 // name. It is safe to capture |this| because the callback won't be run | 57 // object goes away. |
57 // after this object goes away. | 58 endpoint_client_->control_message_proxy()->QueryVersion( |
58 static_cast<ControlMessageProxy*>(proxy_.get()) | 59 base::Bind(&AssociatedInterfacePtrState::OnQueryVersion, |
59 ->QueryVersion(base::Bind(&AssociatedInterfacePtrState::OnQueryVersion, | 60 base::Unretained(this), callback)); |
60 base::Unretained(this), callback)); | |
61 } | 61 } |
62 | 62 |
63 void RequireVersion(uint32_t version) { | 63 void RequireVersion(uint32_t version) { |
64 if (version <= version_) | 64 if (version <= version_) |
65 return; | 65 return; |
66 | 66 |
67 version_ = version; | 67 version_ = version; |
68 // Do a static cast in case the interface contains methods with the same | 68 endpoint_client_->control_message_proxy()->RequireVersion(version); |
69 // name. | 69 } |
70 static_cast<ControlMessageProxy*>(proxy_.get())->RequireVersion(version); | 70 |
71 void FlushForTesting() { | |
72 if (encountered_error() || !proxy_) | |
yzshen1
2016/08/29 23:24:58
Does it make sense to remove this check? (Currentl
Sam McNally
2016/08/30 03:05:51
Done.
| |
73 return; | |
74 | |
75 endpoint_client_->control_message_proxy()->FlushForTesting(); | |
71 } | 76 } |
72 | 77 |
73 void Swap(AssociatedInterfacePtrState* other) { | 78 void Swap(AssociatedInterfacePtrState* other) { |
74 using std::swap; | 79 using std::swap; |
75 swap(other->endpoint_client_, endpoint_client_); | 80 swap(other->endpoint_client_, endpoint_client_); |
76 swap(other->proxy_, proxy_); | 81 swap(other->proxy_, proxy_); |
77 swap(other->version_, version_); | 82 swap(other->version_, version_); |
78 } | 83 } |
79 | 84 |
80 void Bind(AssociatedInterfacePtrInfo<Interface> info, | 85 void Bind(AssociatedInterfacePtrInfo<Interface> info, |
81 scoped_refptr<base::SingleThreadTaskRunner> runner) { | 86 scoped_refptr<base::SingleThreadTaskRunner> runner) { |
82 DCHECK(!endpoint_client_); | 87 DCHECK(!endpoint_client_); |
83 DCHECK(!proxy_); | 88 DCHECK(!proxy_); |
84 DCHECK_EQ(0u, version_); | 89 DCHECK_EQ(0u, version_); |
85 DCHECK(info.is_valid()); | 90 DCHECK(info.is_valid()); |
86 | 91 |
87 version_ = info.version(); | 92 version_ = info.version(); |
88 endpoint_client_.reset(new InterfaceEndpointClient( | 93 endpoint_client_.reset(new InterfaceEndpointClient( |
89 info.PassHandle(), nullptr, | 94 info.PassHandle(), nullptr, |
90 base::WrapUnique(new typename Interface::ResponseValidator_()), false, | 95 base::WrapUnique(new typename Interface::ResponseValidator_()), false, |
91 std::move(runner))); | 96 std::move(runner), 0u)); |
yzshen1
2016/08/29 23:24:58
Please comment on why we use 0 here. It might not
Sam McNally
2016/08/30 03:05:51
Done.
| |
92 proxy_.reset(new Proxy(endpoint_client_.get())); | 97 proxy_.reset(new Proxy(endpoint_client_.get())); |
93 proxy_->serialization_context()->group_controller = | 98 proxy_->serialization_context()->group_controller = |
94 endpoint_client_->group_controller(); | 99 endpoint_client_->group_controller(); |
95 } | 100 } |
96 | 101 |
97 // After this method is called, the object is in an invalid state and | 102 // After this method is called, the object is in an invalid state and |
98 // shouldn't be reused. | 103 // shouldn't be reused. |
99 AssociatedInterfacePtrInfo<Interface> PassInterface() { | 104 AssociatedInterfacePtrInfo<Interface> PassInterface() { |
100 ScopedInterfaceEndpointHandle handle = endpoint_client_->PassHandle(); | 105 ScopedInterfaceEndpointHandle handle = endpoint_client_->PassHandle(); |
101 endpoint_client_.reset(); | 106 endpoint_client_.reset(); |
(...skipping 23 matching lines...) Expand all Loading... | |
125 | 130 |
126 private: | 131 private: |
127 using Proxy = typename Interface::Proxy_; | 132 using Proxy = typename Interface::Proxy_; |
128 | 133 |
129 void OnQueryVersion(const base::Callback<void(uint32_t)>& callback, | 134 void OnQueryVersion(const base::Callback<void(uint32_t)>& callback, |
130 uint32_t version) { | 135 uint32_t version) { |
131 version_ = version; | 136 version_ = version; |
132 callback.Run(version); | 137 callback.Run(version); |
133 } | 138 } |
134 | 139 |
140 void OnConnectionError() { | |
yzshen1
2016/08/29 23:24:58
I think this is not used.
Sam McNally
2016/08/30 03:05:51
Done.
| |
141 endpoint_client_->control_message_proxy()->OnConnectionError(); | |
142 } | |
143 | |
135 std::unique_ptr<InterfaceEndpointClient> endpoint_client_; | 144 std::unique_ptr<InterfaceEndpointClient> endpoint_client_; |
136 std::unique_ptr<Proxy> proxy_; | 145 std::unique_ptr<Proxy> proxy_; |
137 | 146 |
138 uint32_t version_; | 147 uint32_t version_; |
139 | 148 |
140 DISALLOW_COPY_AND_ASSIGN(AssociatedInterfacePtrState); | 149 DISALLOW_COPY_AND_ASSIGN(AssociatedInterfacePtrState); |
141 }; | 150 }; |
142 | 151 |
143 } // namespace internal | 152 } // namespace internal |
144 } // namespace mojo | 153 } // namespace mojo |
145 | 154 |
146 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ASSOCIATED_INTERFACE_PTR_STATE_H_ | 155 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ASSOCIATED_INTERFACE_PTR_STATE_H_ |
OLD | NEW |