OLD | NEW |
---|---|
1 // Copyright 2014 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_BINDING_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_STRONG_ASSOCIATED_BINDING_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_STRONG_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> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/callback.h" | 13 #include "base/callback.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/macros.h" | 15 #include "base/macros.h" |
16 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
17 #include "mojo/public/cpp/bindings/binding.h" | 17 #include "mojo/public/cpp/bindings/associated_binding.h" |
18 #include "mojo/public/cpp/bindings/associated_interface_request.h" | |
18 #include "mojo/public/cpp/bindings/connection_error_callback.h" | 19 #include "mojo/public/cpp/bindings/connection_error_callback.h" |
19 #include "mojo/public/cpp/bindings/filter_chain.h" | |
20 #include "mojo/public/cpp/bindings/interface_ptr.h" | |
21 #include "mojo/public/cpp/bindings/interface_request.h" | |
22 #include "mojo/public/cpp/bindings/lib/router.h" | |
23 #include "mojo/public/cpp/bindings/message_header_validator.h" | |
24 #include "mojo/public/cpp/system/core.h" | 20 #include "mojo/public/cpp/system/core.h" |
25 | 21 |
26 namespace mojo { | 22 namespace mojo { |
27 | 23 |
28 template <typename Interface> | 24 template <typename Interface> |
29 class StrongBinding; | 25 class StrongAssociatedBinding; |
30 | 26 |
31 template <typename Interface> | 27 template <typename Interface> |
32 using StrongBindingPtr = base::WeakPtr<StrongBinding<Interface>>; | 28 using StrongAssociatedBindingPtr = |
29 base::WeakPtr<StrongAssociatedBinding<Interface>>; | |
33 | 30 |
34 // This connects an interface implementation strongly to a pipe. When a | 31 // This connects an interface implementation strongly to an associated pipe. |
35 // connection error is detected the implementation is deleted. | 32 // When a connection error is detected the implementation is deleted. |
36 // | 33 // |
37 // To use, call StrongBinding<T>::Create() (see below) or the helper | 34 // To use, call StrongAssociatedBinding<T>::Create() (see below) or the helper |
38 // MakeStrongBinding function: | 35 // MakeStrongAssociatedBinding function: |
39 // | 36 // |
40 // mojo::MakeStrongBinding(base::MakeUnique<FooImpl>(), | 37 // mojo::MakeStrongAssociatedBinding(base::MakeUnique<FooImpl>(), |
41 // std::move(foo_request)); | 38 // std::move(foo_request)); |
42 // | 39 // |
43 template <typename Interface> | 40 template <typename Interface> |
44 class StrongBinding { | 41 class StrongAssociatedBinding { |
45 public: | 42 public: |
46 // Create a new StrongBinding instance. The instance owns itself, cleaning up | 43 // Create a new StrongAssociatedBinding instance. The instance owns itself, |
47 // only in the event of a pipe connection error. Returns a WeakPtr to the new | 44 // cleaning up only in the event of a pipe connection error. Returns a WeakPtr |
48 // StrongBinding instance. | 45 // to the new StrongAssociatedBinding instance. |
49 static StrongBindingPtr<Interface> Create( | 46 static StrongAssociatedBindingPtr<Interface> Create( |
50 std::unique_ptr<Interface> impl, | 47 std::unique_ptr<Interface> impl, |
51 InterfaceRequest<Interface> request) { | 48 AssociatedInterfaceRequest<Interface> request) { |
52 StrongBinding* binding = | 49 StrongAssociatedBinding* binding = |
53 new StrongBinding(std::move(impl), std::move(request)); | 50 new StrongAssociatedBinding(std::move(impl), std::move(request)); |
54 return binding->weak_factory_.GetWeakPtr(); | 51 return binding->weak_factory_.GetWeakPtr(); |
55 } | 52 } |
56 | 53 |
57 // Note: The error handler must not delete the interface implementation. | 54 // Note: The error handler must not delete the interface implementation. |
58 // | 55 // |
59 // This method may only be called after this StrongBinding has been bound to a | 56 // This method may only be called after this StrongAssociatedBinding has been |
60 // message pipe. | 57 // bound to a message pipe. |
61 void set_connection_error_handler(const base::Closure& error_handler) { | 58 void set_connection_error_handler(const base::Closure& error_handler) { |
62 DCHECK(binding_.is_bound()); | 59 DCHECK(binding_.is_bound()); |
63 connection_error_handler_ = error_handler; | 60 connection_error_handler_ = error_handler; |
64 connection_error_with_reason_handler_.Reset(); | 61 connection_error_with_reason_handler_.Reset(); |
65 } | 62 } |
66 | 63 |
67 void set_connection_error_with_reason_handler( | 64 void set_connection_error_with_reason_handler( |
68 const ConnectionErrorWithReasonCallback& error_handler) { | 65 const ConnectionErrorWithReasonCallback& error_handler) { |
69 DCHECK(binding_.is_bound()); | 66 DCHECK(binding_.is_bound()); |
70 connection_error_with_reason_handler_ = error_handler; | 67 connection_error_with_reason_handler_ = error_handler; |
71 connection_error_handler_.Reset(); | 68 connection_error_handler_.Reset(); |
72 } | 69 } |
73 | 70 |
74 // Forces the binding to close. This destroys the StrongBinding instance. | 71 // Forces the binding to close. This destroys the StrongBinding instance. |
75 void Close() { delete this; } | 72 void Close() { delete this; } |
76 | 73 |
77 Interface* impl() { return impl_.get(); } | 74 Interface* impl() { return impl_.get(); } |
78 | 75 |
79 // Exposed for testing, should not generally be used. | |
80 internal::Router* internal_router() { return binding_.internal_router(); } | |
81 | |
82 // Sends a message on the underlying message pipe and runs the current | 76 // Sends a message on the underlying message pipe and runs the current |
83 // message loop until its response is received. This can be used in tests to | 77 // message loop until its response is received. This can be used in tests to |
84 // verify that no message was sent on a message pipe in response to some | 78 // verify that no message was sent on a message pipe in response to some |
85 // stimulus. | 79 // stimulus. |
86 void FlushForTesting() { binding_.FlushForTesting(); } | 80 void FlushForTesting() { binding_.FlushForTesting(); } |
87 | 81 |
88 private: | 82 private: |
89 StrongBinding(std::unique_ptr<Interface> impl, | 83 StrongAssociatedBinding(std::unique_ptr<Interface> impl, |
90 InterfaceRequest<Interface> request) | 84 AssociatedInterfaceRequest<Interface> request) |
91 : impl_(std::move(impl)), | 85 : impl_(std::move(impl)), |
92 binding_(impl_.get(), std::move(request)), | 86 binding_(impl_.get(), std::move(request)), |
93 weak_factory_(this) { | 87 weak_factory_(this) { |
94 binding_.set_connection_error_with_reason_handler( | 88 binding_.set_connection_error_with_reason_handler(base::Bind( |
95 base::Bind(&StrongBinding::OnConnectionError, base::Unretained(this))); | 89 &StrongAssociatedBinding::OnConnectionError, base::Unretained(this))); |
96 } | 90 } |
97 | 91 |
98 ~StrongBinding() {} | 92 ~StrongAssociatedBinding() {} |
99 | 93 |
100 void OnConnectionError(uint32_t custom_reason, | 94 void OnConnectionError(uint32_t custom_reason, |
101 const std::string& description) { | 95 const std::string& description) { |
102 if (!connection_error_handler_.is_null()) | 96 if (!connection_error_handler_.is_null()) |
103 connection_error_handler_.Run(); | 97 connection_error_handler_.Run(); |
104 else if (!connection_error_with_reason_handler_.is_null()) | 98 else if (!connection_error_with_reason_handler_.is_null()) |
cmumford
2016/10/11 18:30:54
I'm assuming that only one type of error handler w
Reilly Grant (use Gerrit)
2016/10/11 23:46:13
Yes.
| |
105 connection_error_with_reason_handler_.Run(custom_reason, description); | 99 connection_error_with_reason_handler_.Run(custom_reason, description); |
106 Close(); | 100 Close(); |
107 } | 101 } |
108 | 102 |
109 std::unique_ptr<Interface> impl_; | 103 std::unique_ptr<Interface> impl_; |
110 base::Closure connection_error_handler_; | 104 base::Closure connection_error_handler_; |
111 ConnectionErrorWithReasonCallback connection_error_with_reason_handler_; | 105 ConnectionErrorWithReasonCallback connection_error_with_reason_handler_; |
112 Binding<Interface> binding_; | 106 AssociatedBinding<Interface> binding_; |
113 base::WeakPtrFactory<StrongBinding> weak_factory_; | 107 base::WeakPtrFactory<StrongAssociatedBinding> weak_factory_; |
114 | 108 |
115 DISALLOW_COPY_AND_ASSIGN(StrongBinding); | 109 DISALLOW_COPY_AND_ASSIGN(StrongAssociatedBinding); |
116 }; | 110 }; |
117 | 111 |
118 template <typename Interface, typename Impl> | 112 template <typename Interface, typename Impl> |
119 StrongBindingPtr<Interface> MakeStrongBinding( | 113 StrongAssociatedBindingPtr<Interface> MakeStrongAssociatedBinding( |
120 std::unique_ptr<Impl> impl, | 114 std::unique_ptr<Impl> impl, |
121 InterfaceRequest<Interface> request) { | 115 AssociatedInterfaceRequest<Interface> request) { |
122 return StrongBinding<Interface>::Create(std::move(impl), std::move(request)); | 116 return StrongAssociatedBinding<Interface>::Create(std::move(impl), |
117 std::move(request)); | |
123 } | 118 } |
124 | 119 |
125 } // namespace mojo | 120 } // namespace mojo |
126 | 121 |
127 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRONG_BINDING_H_ | 122 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRONG_ASSOCIATED_BINDING_H_ |
OLD | NEW |