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