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

Side by Side Diff: mojo/public/cpp/bindings/binding_set.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 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_BINDING_SET_H_ 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_BINDING_SET_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_BINDING_SET_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_BINDING_SET_H_
7 7
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 using ContextTraits = BindingSetContextTraits<ContextType>; 66 using ContextTraits = BindingSetContextTraits<ContextType>;
67 using Context = typename ContextTraits::Type; 67 using Context = typename ContextTraits::Type;
68 using PreDispatchCallback = base::Callback<void(const Context&)>; 68 using PreDispatchCallback = base::Callback<void(const Context&)>;
69 using Traits = BindingSetTraits<BindingType>; 69 using Traits = BindingSetTraits<BindingType>;
70 using ProxyType = typename Traits::ProxyType; 70 using ProxyType = typename Traits::ProxyType;
71 using RequestType = typename Traits::RequestType; 71 using RequestType = typename Traits::RequestType;
72 using ImplPointerType = typename Traits::ImplPointerType; 72 using ImplPointerType = typename Traits::ImplPointerType;
73 73
74 BindingSetBase() {} 74 BindingSetBase() {}
75 75
76 void set_connection_error_handler(const base::Closure& error_handler) { 76 void set_connection_error_handler(base::RepeatingClosure error_handler) {
77 error_handler_ = error_handler; 77 error_handler_ = std::move(error_handler);
78 error_with_reason_handler_.Reset(); 78 error_with_reason_handler_.Reset();
79 } 79 }
80 80
81 void set_connection_error_with_reason_handler( 81 void set_connection_error_with_reason_handler(
82 const ConnectionErrorWithReasonCallback& error_handler) { 82 RepeatingConnectionErrorWithReasonCallback error_handler) {
83 error_with_reason_handler_ = error_handler; 83 error_with_reason_handler_ = std::move(error_handler);
84 error_handler_.Reset(); 84 error_handler_.Reset();
85 } 85 }
86 86
87 // Sets a callback to be invoked immediately before dispatching any message or 87 // Sets a callback to be invoked immediately before dispatching any message or
88 // error received by any of the bindings in the set. This may only be used 88 // error received by any of the bindings in the set. This may only be used
89 // with a non-void |ContextType|. 89 // with a non-void |ContextType|.
90 void set_pre_dispatch_handler(const PreDispatchCallback& handler) { 90 void set_pre_dispatch_handler(const PreDispatchCallback& handler) {
91 static_assert(ContextTraits::SupportsContext(), 91 static_assert(ContextTraits::SupportsContext(),
92 "Pre-dispatch handler usage requires non-void context type."); 92 "Pre-dispatch handler usage requires non-void context type.");
93 pre_dispatch_handler_ = handler; 93 pre_dispatch_handler_ = handler;
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 BindingSetBase* binding_set, 175 BindingSetBase* binding_set,
176 BindingId binding_id, 176 BindingId binding_id,
177 Context context) 177 Context context)
178 : binding_(std::move(impl), std::move(request)), 178 : binding_(std::move(impl), std::move(request)),
179 binding_set_(binding_set), 179 binding_set_(binding_set),
180 binding_id_(binding_id), 180 binding_id_(binding_id),
181 context_(std::move(context)) { 181 context_(std::move(context)) {
182 if (ContextTraits::SupportsContext()) 182 if (ContextTraits::SupportsContext())
183 binding_.AddFilter(base::MakeUnique<DispatchFilter>(this)); 183 binding_.AddFilter(base::MakeUnique<DispatchFilter>(this));
184 binding_.set_connection_error_with_reason_handler( 184 binding_.set_connection_error_with_reason_handler(
185 base::Bind(&Entry::OnConnectionError, base::Unretained(this))); 185 base::BindOnce(&Entry::OnConnectionError, base::Unretained(this)));
186 } 186 }
187 187
188 void FlushForTesting() { binding_.FlushForTesting(); } 188 void FlushForTesting() { binding_.FlushForTesting(); }
189 189
190 private: 190 private:
191 class DispatchFilter : public MessageReceiver { 191 class DispatchFilter : public MessageReceiver {
192 public: 192 public:
193 explicit DispatchFilter(Entry* entry) : entry_(entry) {} 193 explicit DispatchFilter(Entry* entry) : entry_(entry) {}
194 ~DispatchFilter() override {} 194 ~DispatchFilter() override {}
195 195
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 uint32_t custom_reason, 247 uint32_t custom_reason,
248 const std::string& description) { 248 const std::string& description) {
249 auto it = bindings_.find(id); 249 auto it = bindings_.find(id);
250 DCHECK(it != bindings_.end()); 250 DCHECK(it != bindings_.end());
251 251
252 // We keep the Entry alive throughout error dispatch. 252 // We keep the Entry alive throughout error dispatch.
253 std::unique_ptr<Entry> entry = std::move(it->second); 253 std::unique_ptr<Entry> entry = std::move(it->second);
254 if (!is_flushing_) 254 if (!is_flushing_)
255 bindings_.erase(it); 255 bindings_.erase(it);
256 256
257 if (!error_handler_.is_null()) 257 if (error_handler_) {
258 error_handler_.Run(); 258 error_handler_.Run();
259 else if (!error_with_reason_handler_.is_null()) 259 } else if (error_with_reason_handler_) {
260 error_with_reason_handler_.Run(custom_reason, description); 260 error_with_reason_handler_.Run(custom_reason, description);
261 }
261 } 262 }
262 263
263 base::Closure error_handler_; 264 base::RepeatingClosure error_handler_;
264 ConnectionErrorWithReasonCallback error_with_reason_handler_; 265 RepeatingConnectionErrorWithReasonCallback error_with_reason_handler_;
265 PreDispatchCallback pre_dispatch_handler_; 266 PreDispatchCallback pre_dispatch_handler_;
266 BindingId next_binding_id_ = 0; 267 BindingId next_binding_id_ = 0;
267 std::map<BindingId, std::unique_ptr<Entry>> bindings_; 268 std::map<BindingId, std::unique_ptr<Entry>> bindings_;
268 bool is_flushing_ = false; 269 bool is_flushing_ = false;
269 const Context* dispatch_context_ = nullptr; 270 const Context* dispatch_context_ = nullptr;
270 271
271 DISALLOW_COPY_AND_ASSIGN(BindingSetBase); 272 DISALLOW_COPY_AND_ASSIGN(BindingSetBase);
272 }; 273 };
273 274
274 template <typename Interface, typename ContextType = void> 275 template <typename Interface, typename ContextType = void>
275 using BindingSet = BindingSetBase<Interface, Binding<Interface>, ContextType>; 276 using BindingSet = BindingSetBase<Interface, Binding<Interface>, ContextType>;
276 277
277 } // namespace mojo 278 } // namespace mojo
278 279
279 #endif // MOJO_PUBLIC_CPP_BINDINGS_BINDING_SET_H_ 280 #endif // MOJO_PUBLIC_CPP_BINDINGS_BINDING_SET_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/binding.h ('k') | mojo/public/cpp/bindings/connection_error_callback.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698