OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #ifndef MOJO_COMMON_WEAK_BINDING_SET_H_ |
| 6 #define MOJO_COMMON_WEAK_BINDING_SET_H_ |
| 7 |
| 8 #include <algorithm> |
| 9 #include <vector> |
| 10 |
| 11 #include "base/memory/weak_ptr.h" |
| 12 #include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h" |
| 13 |
| 14 namespace mojo { |
| 15 |
| 16 template <typename Interface> |
| 17 class WeakBinding; |
| 18 |
| 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 : public ErrorHandler { |
| 23 public: |
| 24 WeakBindingSet() : error_handler_(nullptr) {} |
| 25 ~WeakBindingSet() { CloseAllBindings(); } |
| 26 |
| 27 void set_error_handler(ErrorHandler* error_handler) { |
| 28 error_handler_ = error_handler; |
| 29 } |
| 30 |
| 31 void AddBinding(Interface* impl, InterfaceRequest<Interface> request) { |
| 32 auto binding = new WeakBinding<Interface>(impl, request.Pass()); |
| 33 binding->set_error_handler(this); |
| 34 bindings_.push_back(binding->GetWeakPtr()); |
| 35 } |
| 36 |
| 37 void CloseAllBindings() { |
| 38 for (const auto& it : bindings_) { |
| 39 if (it) |
| 40 it->Close(); |
| 41 } |
| 42 bindings_.clear(); |
| 43 } |
| 44 |
| 45 private: |
| 46 // ErrorHandler implementation. |
| 47 void OnConnectionError() override { |
| 48 // Clear any deleted bindings. |
| 49 bindings_.erase( |
| 50 std::remove_if(bindings_.begin(), bindings_.end(), |
| 51 [](const base::WeakPtr<WeakBinding<Interface>>& p) { |
| 52 return p.get() == nullptr; |
| 53 }), |
| 54 bindings_.end()); |
| 55 |
| 56 if (error_handler_) |
| 57 error_handler_->OnConnectionError(); |
| 58 } |
| 59 |
| 60 ErrorHandler* error_handler_; |
| 61 std::vector<base::WeakPtr<WeakBinding<Interface>>> bindings_; |
| 62 |
| 63 DISALLOW_COPY_AND_ASSIGN(WeakBindingSet); |
| 64 }; |
| 65 |
| 66 template <typename Interface> |
| 67 class WeakBinding : public ErrorHandler { |
| 68 public: |
| 69 WeakBinding(Interface* impl, InterfaceRequest<Interface> request) |
| 70 : binding_(impl, request.Pass()), |
| 71 error_handler_(nullptr), |
| 72 weak_ptr_factory_(this) { |
| 73 binding_.set_error_handler(this); |
| 74 } |
| 75 |
| 76 ~WeakBinding() override {} |
| 77 |
| 78 void set_error_handler(ErrorHandler* error_handler) { |
| 79 error_handler_ = error_handler; |
| 80 } |
| 81 |
| 82 base::WeakPtr<WeakBinding> GetWeakPtr() { |
| 83 return weak_ptr_factory_.GetWeakPtr(); |
| 84 } |
| 85 |
| 86 void Close() { binding_.Close(); } |
| 87 |
| 88 // ErrorHandler implementation. |
| 89 void OnConnectionError() override { |
| 90 ErrorHandler* error_handler = error_handler_; |
| 91 delete this; |
| 92 if (error_handler) |
| 93 error_handler->OnConnectionError(); |
| 94 } |
| 95 |
| 96 private: |
| 97 mojo::Binding<Interface> binding_; |
| 98 ErrorHandler* error_handler_; |
| 99 base::WeakPtrFactory<WeakBinding> weak_ptr_factory_; |
| 100 |
| 101 DISALLOW_COPY_AND_ASSIGN(WeakBinding); |
| 102 }; |
| 103 |
| 104 } // namespace mojo |
| 105 |
| 106 #endif // MOJO_COMMON_WEAK_BINDING_SET_H_ |
OLD | NEW |