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

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

Issue 2515873003: Mojo C++ Bindings: Introduce mojo::SupportsStrongBinding
Patch Set: . Created 4 years, 1 month 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
« no previous file with comments | « no previous file | mojo/public/cpp/bindings/tests/associated_interface_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 13 matching lines...) Expand all
24 #include "mojo/public/cpp/system/core.h" 24 #include "mojo/public/cpp/system/core.h"
25 25
26 namespace mojo { 26 namespace mojo {
27 27
28 template <typename Interface> 28 template <typename Interface>
29 class StrongBinding; 29 class StrongBinding;
30 30
31 template <typename Interface> 31 template <typename Interface>
32 using StrongBindingPtr = base::WeakPtr<StrongBinding<Interface>>; 32 using StrongBindingPtr = base::WeakPtr<StrongBinding<Interface>>;
33 33
34 // Interface implementations must inherit this in order to support being
35 // strongly bound to a message pipe.
36 template <typename Interface>
37 class SupportsStrongBinding : public Interface {
38 public:
39 virtual ~SupportsStrongBinding() {
40 if (binding_) {
41 // Dissociate |this| from the binding before closing it.
42 ignore_result(binding_->Release().release());
43
44 // Deletes |*binding_|.
45 binding_->Close();
46 }
47 }
48
49 private:
50 friend class StrongBinding<Interface>;
51
52 void set_binding(const StrongBindingPtr<Interface>& binding) {
53 binding_ = binding;
54 }
55
56 StrongBindingPtr<Interface> binding_;
57
58 DISALLOW_COPY_AND_ASSIGN(SupportsStrongBinding);
59 };
60
34 // This connects an interface implementation strongly to a pipe. When a 61 // This connects an interface implementation strongly to a pipe. When a
35 // connection error is detected the implementation is deleted. 62 // connection error is detected the implementation is deleted.
36 // 63 //
37 // To use, call StrongBinding<T>::Create() (see below) or the helper 64 // To use, call StrongBinding<T>::Create() (see below) or the helper
38 // MakeStrongBinding function: 65 // MakeStrongBinding function:
39 // 66 //
40 // mojo::MakeStrongBinding(base::MakeUnique<FooImpl>(), 67 // mojo::MakeStrongBinding(base::MakeUnique<FooImpl>(),
41 // std::move(foo_request)); 68 // std::move(foo_request));
42 // 69 //
43 template <typename Interface> 70 template <typename Interface>
44 class StrongBinding { 71 class StrongBinding {
45 public: 72 public:
46 // Create a new StrongBinding instance. The instance owns itself, cleaning up 73 // Create a new StrongBinding instance. The instance owns itself, cleaning up
47 // only in the event of a pipe connection error. Returns a WeakPtr to the new 74 // only in the event of a pipe connection error. Returns a WeakPtr to the new
48 // StrongBinding instance. 75 // StrongBinding instance.
76 //
77 // Note that it's also safe for |impl| to delete itself at any time.
78 template <typename Impl>
49 static StrongBindingPtr<Interface> Create( 79 static StrongBindingPtr<Interface> Create(
dcheng 2016/11/21 10:42:12 Would this still need to return a weak pointer to
50 std::unique_ptr<Interface> impl, 80 std::unique_ptr<Impl> impl,
51 InterfaceRequest<Interface> request) { 81 InterfaceRequest<Interface> request) {
82 SupportsStrongBinding<Interface>* raw_impl = impl.get();
52 StrongBinding* binding = 83 StrongBinding* binding =
53 new StrongBinding(std::move(impl), std::move(request)); 84 new StrongBinding(std::move(impl), std::move(request));
54 return binding->weak_factory_.GetWeakPtr(); 85 StrongBindingPtr<Interface> weak_binding =
86 binding->weak_factory_.GetWeakPtr();
87 raw_impl->set_binding(weak_binding);
88 return weak_binding;
55 } 89 }
56 90
57 // Note: The error handler must not delete the interface implementation. 91 // Note: The error handler must not delete the interface implementation.
58 // 92 //
59 // This method may only be called after this StrongBinding has been bound to a 93 // This method may only be called after this StrongBinding has been bound to a
60 // message pipe. 94 // message pipe.
61 void set_connection_error_handler(const base::Closure& error_handler) { 95 void set_connection_error_handler(const base::Closure& error_handler) {
62 DCHECK(binding_.is_bound()); 96 DCHECK(binding_.is_bound());
63 connection_error_handler_ = error_handler; 97 connection_error_handler_ = error_handler;
64 connection_error_with_reason_handler_.Reset(); 98 connection_error_with_reason_handler_.Reset();
65 } 99 }
66 100
67 void set_connection_error_with_reason_handler( 101 void set_connection_error_with_reason_handler(
68 const ConnectionErrorWithReasonCallback& error_handler) { 102 const ConnectionErrorWithReasonCallback& error_handler) {
69 DCHECK(binding_.is_bound()); 103 DCHECK(binding_.is_bound());
70 connection_error_with_reason_handler_ = error_handler; 104 connection_error_with_reason_handler_ = error_handler;
71 connection_error_handler_.Reset(); 105 connection_error_handler_.Reset();
72 } 106 }
73 107
74 // Forces the binding to close. This destroys the StrongBinding instance. 108 // Forces the binding to close. This destroys the StrongBinding instance.
75 void Close() { delete this; } 109 void Close() { delete this; }
76 110
111 std::unique_ptr<Interface> Release() WARN_UNUSED_RESULT {
112 return std::move(impl_);
113 }
114
77 Interface* impl() { return impl_.get(); } 115 Interface* impl() { return impl_.get(); }
78 116
79 // Exposed for testing, should not generally be used. 117 // Exposed for testing, should not generally be used.
80 internal::Router* internal_router() { return binding_.internal_router(); } 118 internal::Router* internal_router() { return binding_.internal_router(); }
81 119
82 // Sends a message on the underlying message pipe and runs the current 120 // 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 121 // 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 122 // verify that no message was sent on a message pipe in response to some
85 // stimulus. 123 // stimulus.
86 void FlushForTesting() { binding_.FlushForTesting(); } 124 void FlushForTesting() { binding_.FlushForTesting(); }
87 125
88 private: 126 private:
89 StrongBinding(std::unique_ptr<Interface> impl, 127 StrongBinding(std::unique_ptr<Interface> impl,
90 InterfaceRequest<Interface> request) 128 InterfaceRequest<Interface> request)
91 : impl_(std::move(impl)), 129 : impl_(std::move(impl)),
92 binding_(impl_.get(), std::move(request)), 130 binding_(impl_.get(), std::move(request)),
93 weak_factory_(this) { 131 weak_factory_(this) {
94 binding_.set_connection_error_with_reason_handler( 132 binding_.set_connection_error_with_reason_handler(
95 base::Bind(&StrongBinding::OnConnectionError, base::Unretained(this))); 133 base::Bind(&StrongBinding::OnConnectionError, base::Unretained(this)));
96 } 134 }
97 135
98 ~StrongBinding() {} 136 ~StrongBinding() {
137 // Ensure any WeakPtrs are invalidated *before* |impl_| is destroyed,
138 // otherwise the |impl_|'s destructor will attempt to reenter this object to
139 // Close it.
140 weak_factory_.InvalidateWeakPtrs();
141 }
99 142
100 void OnConnectionError(uint32_t custom_reason, 143 void OnConnectionError(uint32_t custom_reason,
101 const std::string& description) { 144 const std::string& description) {
102 if (!connection_error_handler_.is_null()) 145 if (!connection_error_handler_.is_null())
103 connection_error_handler_.Run(); 146 connection_error_handler_.Run();
104 else if (!connection_error_with_reason_handler_.is_null()) 147 else if (!connection_error_with_reason_handler_.is_null())
105 connection_error_with_reason_handler_.Run(custom_reason, description); 148 connection_error_with_reason_handler_.Run(custom_reason, description);
106 Close(); 149 Close();
107 } 150 }
108 151
109 std::unique_ptr<Interface> impl_; 152 std::unique_ptr<Interface> impl_;
110 base::Closure connection_error_handler_; 153 base::Closure connection_error_handler_;
111 ConnectionErrorWithReasonCallback connection_error_with_reason_handler_; 154 ConnectionErrorWithReasonCallback connection_error_with_reason_handler_;
112 Binding<Interface> binding_; 155 Binding<Interface> binding_;
113 base::WeakPtrFactory<StrongBinding> weak_factory_; 156 base::WeakPtrFactory<StrongBinding> weak_factory_;
114 157
115 DISALLOW_COPY_AND_ASSIGN(StrongBinding); 158 DISALLOW_COPY_AND_ASSIGN(StrongBinding);
116 }; 159 };
117 160
161 // Convenient helper wrapping StrongBinding::Create. See above.
118 template <typename Interface, typename Impl> 162 template <typename Interface, typename Impl>
119 StrongBindingPtr<Interface> MakeStrongBinding( 163 StrongBindingPtr<Interface> MakeStrongBinding(
120 std::unique_ptr<Impl> impl, 164 std::unique_ptr<Impl> impl,
121 InterfaceRequest<Interface> request) { 165 InterfaceRequest<Interface> request) {
122 return StrongBinding<Interface>::Create(std::move(impl), std::move(request)); 166 return StrongBinding<Interface>::Create(std::move(impl), std::move(request));
123 } 167 }
124 168
125 } // namespace mojo 169 } // namespace mojo
126 170
127 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRONG_BINDING_H_ 171 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRONG_BINDING_H_
OLDNEW
« no previous file with comments | « no previous file | mojo/public/cpp/bindings/tests/associated_interface_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698