Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(121)

Side by Side Diff: mojo/public/cpp/bindings/strong_associated_binding.h

Issue 2932193002: Use OnceCallback for Mojo binding connection error handlers. (Closed)
Patch Set: Call Run() on rvalue. Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 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_STRONG_ASSOCIATED_BINDING_H_ 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_STRONG_ASSOCIATED_BINDING_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_STRONG_ASSOCIATED_BINDING_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_STRONG_ASSOCIATED_BINDING_H_
7 7
8 #include <memory> 8 #include <memory>
9 #include <string> 9 #include <string>
10 #include <utility> 10 #include <utility>
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 AssociatedInterfaceRequest<Interface> request) { 51 AssociatedInterfaceRequest<Interface> request) {
52 StrongAssociatedBinding* binding = 52 StrongAssociatedBinding* binding =
53 new StrongAssociatedBinding(std::move(impl), std::move(request)); 53 new StrongAssociatedBinding(std::move(impl), std::move(request));
54 return binding->weak_factory_.GetWeakPtr(); 54 return binding->weak_factory_.GetWeakPtr();
55 } 55 }
56 56
57 // Note: The error handler must not delete the interface implementation. 57 // Note: The error handler must not delete the interface implementation.
58 // 58 //
59 // This method may only be called after this StrongAssociatedBinding has been 59 // This method may only be called after this StrongAssociatedBinding has been
60 // bound to a message pipe. 60 // bound to a message pipe.
61 void set_connection_error_handler(const base::Closure& error_handler) { 61 void set_connection_error_handler(base::OnceClosure error_handler) {
62 DCHECK(binding_.is_bound()); 62 DCHECK(binding_.is_bound());
63 connection_error_handler_ = error_handler; 63 connection_error_handler_ = std::move(error_handler);
64 connection_error_with_reason_handler_.Reset(); 64 connection_error_with_reason_handler_.Reset();
65 } 65 }
66 66
67 void set_connection_error_with_reason_handler( 67 void set_connection_error_with_reason_handler(
68 const ConnectionErrorWithReasonCallback& error_handler) { 68 ConnectionErrorWithReasonCallback error_handler) {
69 DCHECK(binding_.is_bound()); 69 DCHECK(binding_.is_bound());
70 connection_error_with_reason_handler_ = error_handler; 70 connection_error_with_reason_handler_ = std::move(error_handler);
71 connection_error_handler_.Reset(); 71 connection_error_handler_.Reset();
72 } 72 }
73 73
74 // Forces the binding to close. This destroys the StrongBinding instance. 74 // Forces the binding to close. This destroys the StrongBinding instance.
75 void Close() { delete this; } 75 void Close() { delete this; }
76 76
77 Interface* impl() { return impl_.get(); } 77 Interface* impl() { return impl_.get(); }
78 78
79 // Sends a message on the underlying message pipe and runs the current 79 // Sends a message on the underlying message pipe and runs the current
80 // message loop until its response is received. This can be used in tests to 80 // message loop until its response is received. This can be used in tests to
81 // verify that no message was sent on a message pipe in response to some 81 // verify that no message was sent on a message pipe in response to some
82 // stimulus. 82 // stimulus.
83 void FlushForTesting() { binding_.FlushForTesting(); } 83 void FlushForTesting() { binding_.FlushForTesting(); }
84 84
85 private: 85 private:
86 StrongAssociatedBinding(std::unique_ptr<Interface> impl, 86 StrongAssociatedBinding(std::unique_ptr<Interface> impl,
87 AssociatedInterfaceRequest<Interface> request) 87 AssociatedInterfaceRequest<Interface> request)
88 : impl_(std::move(impl)), 88 : impl_(std::move(impl)),
89 binding_(impl_.get(), std::move(request)), 89 binding_(impl_.get(), std::move(request)),
90 weak_factory_(this) { 90 weak_factory_(this) {
91 binding_.set_connection_error_with_reason_handler(base::Bind( 91 binding_.set_connection_error_with_reason_handler(base::Bind(
92 &StrongAssociatedBinding::OnConnectionError, base::Unretained(this))); 92 &StrongAssociatedBinding::OnConnectionError, base::Unretained(this)));
93 } 93 }
94 94
95 ~StrongAssociatedBinding() {} 95 ~StrongAssociatedBinding() {}
96 96
97 void OnConnectionError(uint32_t custom_reason, 97 void OnConnectionError(uint32_t custom_reason,
98 const std::string& description) { 98 const std::string& description) {
99 if (!connection_error_handler_.is_null()) 99 if (connection_error_handler_) {
100 connection_error_handler_.Run(); 100 std::move(connection_error_handler_).Run();
101 else if (!connection_error_with_reason_handler_.is_null()) 101 } else if (connection_error_with_reason_handler_) {
102 connection_error_with_reason_handler_.Run(custom_reason, description); 102 std::move(connection_error_with_reason_handler_)
103 .Run(custom_reason, description);
104 }
103 Close(); 105 Close();
104 } 106 }
105 107
106 std::unique_ptr<Interface> impl_; 108 std::unique_ptr<Interface> impl_;
107 base::Closure connection_error_handler_; 109 base::OnceClosure connection_error_handler_;
108 ConnectionErrorWithReasonCallback connection_error_with_reason_handler_; 110 ConnectionErrorWithReasonCallback connection_error_with_reason_handler_;
109 AssociatedBinding<Interface> binding_; 111 AssociatedBinding<Interface> binding_;
110 base::WeakPtrFactory<StrongAssociatedBinding> weak_factory_; 112 base::WeakPtrFactory<StrongAssociatedBinding> weak_factory_;
111 113
112 DISALLOW_COPY_AND_ASSIGN(StrongAssociatedBinding); 114 DISALLOW_COPY_AND_ASSIGN(StrongAssociatedBinding);
113 }; 115 };
114 116
115 template <typename Interface, typename Impl> 117 template <typename Interface, typename Impl>
116 StrongAssociatedBindingPtr<Interface> MakeStrongAssociatedBinding( 118 StrongAssociatedBindingPtr<Interface> MakeStrongAssociatedBinding(
117 std::unique_ptr<Impl> impl, 119 std::unique_ptr<Impl> impl,
118 AssociatedInterfaceRequest<Interface> request) { 120 AssociatedInterfaceRequest<Interface> request) {
119 return StrongAssociatedBinding<Interface>::Create(std::move(impl), 121 return StrongAssociatedBinding<Interface>::Create(std::move(impl),
120 std::move(request)); 122 std::move(request));
121 } 123 }
122 124
123 } // namespace mojo 125 } // namespace mojo
124 126
125 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRONG_ASSOCIATED_BINDING_H_ 127 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRONG_ASSOCIATED_BINDING_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/lib/interface_ptr_state.h ('k') | mojo/public/cpp/bindings/strong_binding.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698