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 <utility> | 10 #include <utility> |
10 | 11 |
11 #include "base/bind.h" | 12 #include "base/bind.h" |
12 #include "base/callback.h" | 13 #include "base/callback.h" |
13 #include "base/logging.h" | 14 #include "base/logging.h" |
14 #include "base/macros.h" | 15 #include "base/macros.h" |
15 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
16 #include "mojo/public/cpp/bindings/binding.h" | 17 #include "mojo/public/cpp/bindings/binding.h" |
| 18 #include "mojo/public/cpp/bindings/connection_error_callback.h" |
17 #include "mojo/public/cpp/bindings/filter_chain.h" | 19 #include "mojo/public/cpp/bindings/filter_chain.h" |
18 #include "mojo/public/cpp/bindings/interface_ptr.h" | 20 #include "mojo/public/cpp/bindings/interface_ptr.h" |
19 #include "mojo/public/cpp/bindings/interface_request.h" | 21 #include "mojo/public/cpp/bindings/interface_request.h" |
20 #include "mojo/public/cpp/bindings/lib/router.h" | 22 #include "mojo/public/cpp/bindings/lib/router.h" |
21 #include "mojo/public/cpp/bindings/message_header_validator.h" | 23 #include "mojo/public/cpp/bindings/message_header_validator.h" |
22 #include "mojo/public/cpp/system/core.h" | 24 #include "mojo/public/cpp/system/core.h" |
23 | 25 |
24 namespace mojo { | 26 namespace mojo { |
25 | 27 |
26 template <typename Interface> | 28 template <typename Interface> |
(...skipping 25 matching lines...) Expand all Loading... |
52 return binding->weak_factory_.GetWeakPtr(); | 54 return binding->weak_factory_.GetWeakPtr(); |
53 } | 55 } |
54 | 56 |
55 // Note: The error handler must not delete the interface implementation. | 57 // Note: The error handler must not delete the interface implementation. |
56 // | 58 // |
57 // This method may only be called after this StrongBinding has been bound to a | 59 // This method may only be called after this StrongBinding has been bound to a |
58 // message pipe. | 60 // message pipe. |
59 void set_connection_error_handler(const base::Closure& error_handler) { | 61 void set_connection_error_handler(const base::Closure& error_handler) { |
60 DCHECK(binding_.is_bound()); | 62 DCHECK(binding_.is_bound()); |
61 connection_error_handler_ = error_handler; | 63 connection_error_handler_ = error_handler; |
| 64 connection_error_with_reason_handler_.Reset(); |
| 65 } |
| 66 |
| 67 void set_connection_error_with_reason_handler( |
| 68 const ConnectionErrorWithReasonCallback& error_handler) { |
| 69 DCHECK(binding_.is_bound()); |
| 70 connection_error_with_reason_handler_ = error_handler; |
| 71 connection_error_handler_.Reset(); |
62 } | 72 } |
63 | 73 |
64 // Forces the binding to close. This destroys the StrongBinding instance. | 74 // Forces the binding to close. This destroys the StrongBinding instance. |
65 void Close() { delete this; } | 75 void Close() { delete this; } |
66 | 76 |
67 Interface* impl() { return impl_.get(); } | 77 Interface* impl() { return impl_.get(); } |
68 | 78 |
69 // Exposed for testing, should not generally be used. | 79 // Exposed for testing, should not generally be used. |
70 internal::Router* internal_router() { return binding_.internal_router(); } | 80 internal::Router* internal_router() { return binding_.internal_router(); } |
71 | 81 |
72 // Sends a message on the underlying message pipe and runs the current | 82 // Sends a message on the underlying message pipe and runs the current |
73 // message loop until its response is received. This can be used in tests to | 83 // message loop until its response is received. This can be used in tests to |
74 // verify that no message was sent on a message pipe in response to some | 84 // verify that no message was sent on a message pipe in response to some |
75 // stimulus. | 85 // stimulus. |
76 void FlushForTesting() { binding_.FlushForTesting(); } | 86 void FlushForTesting() { binding_.FlushForTesting(); } |
77 | 87 |
78 private: | 88 private: |
79 StrongBinding(std::unique_ptr<Interface> impl, | 89 StrongBinding(std::unique_ptr<Interface> impl, |
80 InterfaceRequest<Interface> request) | 90 InterfaceRequest<Interface> request) |
81 : impl_(std::move(impl)), | 91 : impl_(std::move(impl)), |
82 binding_(impl_.get(), std::move(request)), | 92 binding_(impl_.get(), std::move(request)), |
83 weak_factory_(this) { | 93 weak_factory_(this) { |
84 binding_.set_connection_error_handler( | 94 binding_.set_connection_error_with_reason_handler( |
85 base::Bind(&StrongBinding::OnConnectionError, base::Unretained(this))); | 95 base::Bind(&StrongBinding::OnConnectionError, base::Unretained(this))); |
86 } | 96 } |
87 | 97 |
88 ~StrongBinding() {} | 98 ~StrongBinding() {} |
89 | 99 |
90 void OnConnectionError() { | 100 void OnConnectionError(uint32_t custom_reason, |
| 101 const std::string& description) { |
91 if (!connection_error_handler_.is_null()) | 102 if (!connection_error_handler_.is_null()) |
92 connection_error_handler_.Run(); | 103 connection_error_handler_.Run(); |
| 104 else if (!connection_error_with_reason_handler_.is_null()) |
| 105 connection_error_with_reason_handler_.Run(custom_reason, description); |
93 Close(); | 106 Close(); |
94 } | 107 } |
95 | 108 |
96 std::unique_ptr<Interface> impl_; | 109 std::unique_ptr<Interface> impl_; |
97 base::Closure connection_error_handler_; | 110 base::Closure connection_error_handler_; |
| 111 ConnectionErrorWithReasonCallback connection_error_with_reason_handler_; |
98 Binding<Interface> binding_; | 112 Binding<Interface> binding_; |
99 base::WeakPtrFactory<StrongBinding> weak_factory_; | 113 base::WeakPtrFactory<StrongBinding> weak_factory_; |
100 | 114 |
101 DISALLOW_COPY_AND_ASSIGN(StrongBinding); | 115 DISALLOW_COPY_AND_ASSIGN(StrongBinding); |
102 }; | 116 }; |
103 | 117 |
104 template <typename Interface, typename Impl> | 118 template <typename Interface, typename Impl> |
105 StrongBindingPtr<Interface> MakeStrongBinding( | 119 StrongBindingPtr<Interface> MakeStrongBinding( |
106 std::unique_ptr<Impl> impl, | 120 std::unique_ptr<Impl> impl, |
107 InterfaceRequest<Interface> request) { | 121 InterfaceRequest<Interface> request) { |
108 return StrongBinding<Interface>::Create(std::move(impl), std::move(request)); | 122 return StrongBinding<Interface>::Create(std::move(impl), std::move(request)); |
109 } | 123 } |
110 | 124 |
111 } // namespace mojo | 125 } // namespace mojo |
112 | 126 |
113 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRONG_BINDING_H_ | 127 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRONG_BINDING_H_ |
OLD | NEW |