Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / mojo / common / weak_binding_set.h
blob66a90b1da135d4cd1f408f6755fc519c374a4810
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
3 // found in the LICENSE file.
5 #ifndef MOJO_COMMON_WEAK_BINDING_SET_H_
6 #define MOJO_COMMON_WEAK_BINDING_SET_H_
8 #include <algorithm>
9 #include <vector>
11 #include "base/memory/weak_ptr.h"
12 #include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h"
14 namespace mojo {
16 template <typename Interface>
17 class WeakBinding;
19 // Use this class to manage a set of weak pointers to bindings each of which is
20 // owned by the pipe they are bound to.
21 template <typename Interface>
22 class WeakBindingSet {
23 public:
24 WeakBindingSet() {}
25 ~WeakBindingSet() { CloseAllBindings(); }
27 void set_connection_error_handler(const Closure& error_handler) {
28 error_handler_ = error_handler;
31 // NOTE: Deprecated. Please use the method above.
32 // TODO(yzshen): Remove this method once all callsites are converted.
33 void set_error_handler(ErrorHandler* error_handler) {
34 if (error_handler) {
35 set_connection_error_handler(
36 [error_handler]() { error_handler->OnConnectionError(); });
37 } else {
38 set_connection_error_handler(Closure());
42 void AddBinding(Interface* impl, InterfaceRequest<Interface> request) {
43 auto binding = new WeakBinding<Interface>(impl, request.Pass());
44 binding->set_connection_error_handler([this]() { OnConnectionError(); });
45 bindings_.push_back(binding->GetWeakPtr());
48 void CloseAllBindings() {
49 for (const auto& it : bindings_) {
50 if (it)
51 it->Close();
53 bindings_.clear();
56 bool empty() const { return bindings_.empty(); }
58 private:
59 void OnConnectionError() {
60 // Clear any deleted bindings.
61 bindings_.erase(
62 std::remove_if(bindings_.begin(), bindings_.end(),
63 [](const base::WeakPtr<WeakBinding<Interface>>& p) {
64 return p.get() == nullptr;
65 }),
66 bindings_.end());
68 error_handler_.Run();
71 Closure error_handler_;
72 std::vector<base::WeakPtr<WeakBinding<Interface>>> bindings_;
74 DISALLOW_COPY_AND_ASSIGN(WeakBindingSet);
77 template <typename Interface>
78 class WeakBinding {
79 public:
80 WeakBinding(Interface* impl, InterfaceRequest<Interface> request)
81 : binding_(impl, request.Pass()),
82 weak_ptr_factory_(this) {
83 binding_.set_connection_error_handler([this]() { OnConnectionError(); });
86 ~WeakBinding() {}
88 void set_connection_error_handler(const Closure& error_handler) {
89 error_handler_ = error_handler;
92 base::WeakPtr<WeakBinding> GetWeakPtr() {
93 return weak_ptr_factory_.GetWeakPtr();
96 void Close() { binding_.Close(); }
98 void OnConnectionError() {
99 Closure error_handler = error_handler_;
100 delete this;
101 error_handler.Run();
104 private:
105 Binding<Interface> binding_;
106 Closure error_handler_;
107 base::WeakPtrFactory<WeakBinding> weak_ptr_factory_;
109 DISALLOW_COPY_AND_ASSIGN(WeakBinding);
112 } // namespace mojo
114 #endif // MOJO_COMMON_WEAK_BINDING_SET_H_