| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_BINDING_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_STRONG_BINDING_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_STRONG_BINDING_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_STRONG_BINDING_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 InterfaceRequest<Interface> request) { | 52 InterfaceRequest<Interface> request) { |
| 53 StrongBinding* binding = | 53 StrongBinding* binding = |
| 54 new StrongBinding(std::move(impl), std::move(request)); | 54 new StrongBinding(std::move(impl), std::move(request)); |
| 55 return binding->weak_factory_.GetWeakPtr(); | 55 return binding->weak_factory_.GetWeakPtr(); |
| 56 } | 56 } |
| 57 | 57 |
| 58 // Note: The error handler must not delete the interface implementation. | 58 // Note: The error handler must not delete the interface implementation. |
| 59 // | 59 // |
| 60 // This method may only be called after this StrongBinding has been bound to a | 60 // This method may only be called after this StrongBinding has been bound to a |
| 61 // message pipe. | 61 // message pipe. |
| 62 void set_connection_error_handler(const base::Closure& error_handler) { | 62 void set_connection_error_handler(base::OnceClosure error_handler) { |
| 63 DCHECK(binding_.is_bound()); | 63 DCHECK(binding_.is_bound()); |
| 64 connection_error_handler_ = error_handler; | 64 connection_error_handler_ = std::move(error_handler); |
| 65 connection_error_with_reason_handler_.Reset(); | 65 connection_error_with_reason_handler_.Reset(); |
| 66 } | 66 } |
| 67 | 67 |
| 68 void set_connection_error_with_reason_handler( | 68 void set_connection_error_with_reason_handler( |
| 69 const ConnectionErrorWithReasonCallback& error_handler) { | 69 ConnectionErrorWithReasonCallback error_handler) { |
| 70 DCHECK(binding_.is_bound()); | 70 DCHECK(binding_.is_bound()); |
| 71 connection_error_with_reason_handler_ = error_handler; | 71 connection_error_with_reason_handler_ = std::move(error_handler); |
| 72 connection_error_handler_.Reset(); | 72 connection_error_handler_.Reset(); |
| 73 } | 73 } |
| 74 | 74 |
| 75 // Forces the binding to close. This destroys the StrongBinding instance. | 75 // Forces the binding to close. This destroys the StrongBinding instance. |
| 76 void Close() { delete this; } | 76 void Close() { delete this; } |
| 77 | 77 |
| 78 Interface* impl() { return impl_.get(); } | 78 Interface* impl() { return impl_.get(); } |
| 79 | 79 |
| 80 // Sends a message on the underlying message pipe and runs the current | 80 // Sends a message on the underlying message pipe and runs the current |
| 81 // message loop until its response is received. This can be used in tests to | 81 // message loop until its response is received. This can be used in tests to |
| 82 // verify that no message was sent on a message pipe in response to some | 82 // verify that no message was sent on a message pipe in response to some |
| 83 // stimulus. | 83 // stimulus. |
| 84 void FlushForTesting() { binding_.FlushForTesting(); } | 84 void FlushForTesting() { binding_.FlushForTesting(); } |
| 85 | 85 |
| 86 private: | 86 private: |
| 87 StrongBinding(std::unique_ptr<Interface> impl, | 87 StrongBinding(std::unique_ptr<Interface> impl, |
| 88 InterfaceRequest<Interface> request) | 88 InterfaceRequest<Interface> request) |
| 89 : impl_(std::move(impl)), | 89 : impl_(std::move(impl)), |
| 90 binding_(impl_.get(), std::move(request)), | 90 binding_(impl_.get(), std::move(request)), |
| 91 weak_factory_(this) { | 91 weak_factory_(this) { |
| 92 binding_.set_connection_error_with_reason_handler( | 92 binding_.set_connection_error_with_reason_handler( |
| 93 base::Bind(&StrongBinding::OnConnectionError, base::Unretained(this))); | 93 base::Bind(&StrongBinding::OnConnectionError, base::Unretained(this))); |
| 94 } | 94 } |
| 95 | 95 |
| 96 ~StrongBinding() {} | 96 ~StrongBinding() {} |
| 97 | 97 |
| 98 void OnConnectionError(uint32_t custom_reason, | 98 void OnConnectionError(uint32_t custom_reason, |
| 99 const std::string& description) { | 99 const std::string& description) { |
| 100 if (!connection_error_handler_.is_null()) | 100 if (connection_error_handler_) { |
| 101 connection_error_handler_.Run(); | 101 std::move(connection_error_handler_).Run(); |
| 102 else if (!connection_error_with_reason_handler_.is_null()) | 102 } else if (connection_error_with_reason_handler_) { |
| 103 connection_error_with_reason_handler_.Run(custom_reason, description); | 103 std::move(connection_error_with_reason_handler_) |
| 104 .Run(custom_reason, description); |
| 105 } |
| 104 Close(); | 106 Close(); |
| 105 } | 107 } |
| 106 | 108 |
| 107 std::unique_ptr<Interface> impl_; | 109 std::unique_ptr<Interface> impl_; |
| 108 base::Closure connection_error_handler_; | 110 base::OnceClosure connection_error_handler_; |
| 109 ConnectionErrorWithReasonCallback connection_error_with_reason_handler_; | 111 ConnectionErrorWithReasonCallback connection_error_with_reason_handler_; |
| 110 Binding<Interface> binding_; | 112 Binding<Interface> binding_; |
| 111 base::WeakPtrFactory<StrongBinding> weak_factory_; | 113 base::WeakPtrFactory<StrongBinding> weak_factory_; |
| 112 | 114 |
| 113 DISALLOW_COPY_AND_ASSIGN(StrongBinding); | 115 DISALLOW_COPY_AND_ASSIGN(StrongBinding); |
| 114 }; | 116 }; |
| 115 | 117 |
| 116 template <typename Interface, typename Impl> | 118 template <typename Interface, typename Impl> |
| 117 StrongBindingPtr<Interface> MakeStrongBinding( | 119 StrongBindingPtr<Interface> MakeStrongBinding( |
| 118 std::unique_ptr<Impl> impl, | 120 std::unique_ptr<Impl> impl, |
| 119 InterfaceRequest<Interface> request) { | 121 InterfaceRequest<Interface> request) { |
| 120 return StrongBinding<Interface>::Create(std::move(impl), std::move(request)); | 122 return StrongBinding<Interface>::Create(std::move(impl), std::move(request)); |
| 121 } | 123 } |
| 122 | 124 |
| 123 } // namespace mojo | 125 } // namespace mojo |
| 124 | 126 |
| 125 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRONG_BINDING_H_ | 127 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRONG_BINDING_H_ |
| OLD | NEW |