| OLD | NEW |
| 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_COMMON_WEAK_BINDING_SET_H_ | 5 #ifndef MOJO_COMMON_WEAK_BINDING_SET_H_ |
| 6 #define MOJO_COMMON_WEAK_BINDING_SET_H_ | 6 #define MOJO_COMMON_WEAK_BINDING_SET_H_ |
| 7 | 7 |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <utility> |
| 9 #include <vector> | 10 #include <vector> |
| 10 | 11 |
| 11 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 12 #include "mojo/public/cpp/bindings/binding.h" | 13 #include "mojo/public/cpp/bindings/binding.h" |
| 13 | 14 |
| 14 namespace mojo { | 15 namespace mojo { |
| 15 | 16 |
| 16 template <typename Interface> | 17 template <typename Interface> |
| 17 class WeakBinding; | 18 class WeakBinding; |
| 18 | 19 |
| 19 // Use this class to manage a set of weak pointers to bindings each of which is | 20 // 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 // owned by the pipe they are bound to. |
| 21 template <typename Interface> | 22 template <typename Interface> |
| 22 class WeakBindingSet { | 23 class WeakBindingSet { |
| 23 public: | 24 public: |
| 24 using GenericInterface = typename Interface::GenericInterface; | 25 using GenericInterface = typename Interface::GenericInterface; |
| 25 | 26 |
| 26 WeakBindingSet() {} | 27 WeakBindingSet() {} |
| 27 ~WeakBindingSet() { CloseAllBindings(); } | 28 ~WeakBindingSet() { CloseAllBindings(); } |
| 28 | 29 |
| 29 void set_connection_error_handler(const Closure& error_handler) { | 30 void set_connection_error_handler(const Closure& error_handler) { |
| 30 error_handler_ = error_handler; | 31 error_handler_ = error_handler; |
| 31 } | 32 } |
| 32 | 33 |
| 33 void AddBinding(Interface* impl, | 34 void AddBinding(Interface* impl, |
| 34 InterfaceRequest<GenericInterface> request) { | 35 InterfaceRequest<GenericInterface> request) { |
| 35 auto binding = new WeakBinding<Interface>(impl, request.Pass()); | 36 auto binding = new WeakBinding<Interface>(impl, std::move(request)); |
| 36 binding->set_connection_error_handler([this]() { OnConnectionError(); }); | 37 binding->set_connection_error_handler([this]() { OnConnectionError(); }); |
| 37 bindings_.push_back(binding->GetWeakPtr()); | 38 bindings_.push_back(binding->GetWeakPtr()); |
| 38 } | 39 } |
| 39 | 40 |
| 40 void CloseAllBindings() { | 41 void CloseAllBindings() { |
| 41 for (const auto& it : bindings_) { | 42 for (const auto& it : bindings_) { |
| 42 if (it) { | 43 if (it) { |
| 43 it->Close(); | 44 it->Close(); |
| 44 delete it.get(); | 45 delete it.get(); |
| 45 } | 46 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 67 | 68 |
| 68 DISALLOW_COPY_AND_ASSIGN(WeakBindingSet); | 69 DISALLOW_COPY_AND_ASSIGN(WeakBindingSet); |
| 69 }; | 70 }; |
| 70 | 71 |
| 71 template <typename Interface> | 72 template <typename Interface> |
| 72 class WeakBinding { | 73 class WeakBinding { |
| 73 public: | 74 public: |
| 74 using GenericInterface = typename Interface::GenericInterface; | 75 using GenericInterface = typename Interface::GenericInterface; |
| 75 | 76 |
| 76 WeakBinding(Interface* impl, InterfaceRequest<GenericInterface> request) | 77 WeakBinding(Interface* impl, InterfaceRequest<GenericInterface> request) |
| 77 : binding_(impl, request.Pass()), | 78 : binding_(impl, std::move(request)), weak_ptr_factory_(this) { |
| 78 weak_ptr_factory_(this) { | |
| 79 binding_.set_connection_error_handler([this]() { OnConnectionError(); }); | 79 binding_.set_connection_error_handler([this]() { OnConnectionError(); }); |
| 80 } | 80 } |
| 81 | 81 |
| 82 ~WeakBinding() {} | 82 ~WeakBinding() {} |
| 83 | 83 |
| 84 void set_connection_error_handler(const Closure& error_handler) { | 84 void set_connection_error_handler(const Closure& error_handler) { |
| 85 error_handler_ = error_handler; | 85 error_handler_ = error_handler; |
| 86 } | 86 } |
| 87 | 87 |
| 88 base::WeakPtr<WeakBinding> GetWeakPtr() { | 88 base::WeakPtr<WeakBinding> GetWeakPtr() { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 101 Binding<Interface> binding_; | 101 Binding<Interface> binding_; |
| 102 Closure error_handler_; | 102 Closure error_handler_; |
| 103 base::WeakPtrFactory<WeakBinding> weak_ptr_factory_; | 103 base::WeakPtrFactory<WeakBinding> weak_ptr_factory_; |
| 104 | 104 |
| 105 DISALLOW_COPY_AND_ASSIGN(WeakBinding); | 105 DISALLOW_COPY_AND_ASSIGN(WeakBinding); |
| 106 }; | 106 }; |
| 107 | 107 |
| 108 } // namespace mojo | 108 } // namespace mojo |
| 109 | 109 |
| 110 #endif // MOJO_COMMON_WEAK_BINDING_SET_H_ | 110 #endif // MOJO_COMMON_WEAK_BINDING_SET_H_ |
| OLD | NEW |