Chromium Code Reviews| 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 #include <algorithm> | |
| 6 #include <vector> | |
| 7 | |
| 8 #include "base/memory/weak_ptr.h" | |
| 9 #include "mojo/public/cpp/bindings/binding.h" | |
| 10 | |
| 11 namespace mojo { | |
| 12 | |
| 13 template <typename Interface> | |
| 14 class WeakBinding; | |
| 15 | |
| 16 // Use this class to manage a set of weak pointers to bindings each of which is | |
| 17 // owned by the pipe they are bound to. | |
| 18 template <typename Interface> | |
| 19 class WeakBindingSet { | |
| 20 public: | |
| 21 WeakBindingSet() {} | |
| 22 ~WeakBindingSet() {} | |
| 23 | |
| 24 void AddBinding(Interface* impl, InterfaceRequest<Interface> request) { | |
| 25 auto binding = new WeakBinding<Interface>(impl, request.Pass()); | |
| 26 bindings_.push_back(binding->GetWeakPtr()); | |
| 27 ClearNullBindings(); | |
| 28 } | |
| 29 | |
| 30 | |
| 31 void CloseAllBindings() { | |
| 32 for (const auto& it : bindings_) { | |
| 33 if (it.get()) | |
|
eseidel
2014/11/18 23:47:49
Does it not coerce to bool?
jamesr
2014/11/19 00:17:17
WeakPtr is testable, so if (it) should work. will
| |
| 34 it->Close(); | |
| 35 } | |
| 36 bindings_.clear(); | |
| 37 } | |
| 38 | |
| 39 template <typename FunctionType> | |
| 40 void ForAllBindings(FunctionType function) { | |
| 41 for (const auto& it : bindings_) { | |
| 42 if (it.get()) | |
| 43 function(it.get()->client()); | |
| 44 } | |
| 45 ClearNullBindings(); | |
| 46 } | |
| 47 | |
| 48 private: | |
| 49 void ClearNullBindings() { | |
| 50 bindings_.erase( | |
| 51 std::remove_if(bindings_.begin(), bindings_.end(), | |
| 52 [](const base::WeakPtr<WeakBinding<Interface>>& p) { | |
| 53 return p.get() == nullptr; | |
|
eseidel
2014/11/18 23:47:50
Seems like a helper functor would be nice here. &
jamesr
2014/11/19 00:17:17
Yeah - that'd be nice but would require upstream c
| |
| 54 }), | |
| 55 bindings_.end()); | |
| 56 } | |
| 57 | |
| 58 std::vector<base::WeakPtr<WeakBinding<Interface>>> bindings_; | |
| 59 | |
| 60 DISALLOW_COPY_AND_ASSIGN(WeakBindingSet); | |
| 61 }; | |
| 62 | |
| 63 template <typename Interface> | |
| 64 class WeakBinding : public ErrorHandler { | |
| 65 public: | |
| 66 WeakBinding(Interface* impl, InterfaceRequest<Interface> request) | |
| 67 : binding_(impl, request.Pass()), weak_ptr_factory_(this) { | |
| 68 binding_.set_error_handler(this); | |
| 69 } | |
| 70 | |
| 71 ~WeakBinding() override {} | |
| 72 | |
| 73 typename Interface::Client* client() { return binding_.client(); } | |
| 74 | |
| 75 base::WeakPtr<WeakBinding> GetWeakPtr() { | |
| 76 return weak_ptr_factory_.GetWeakPtr(); | |
| 77 } | |
| 78 | |
| 79 void Close() { binding_.Close(); } | |
| 80 | |
| 81 // ErrorHandler implementation. | |
| 82 void OnConnectionError() override { delete this; } | |
| 83 | |
| 84 private: | |
| 85 mojo::Binding<Interface> binding_; | |
| 86 base::WeakPtrFactory<WeakBinding> weak_ptr_factory_; | |
| 87 | |
| 88 DISALLOW_COPY_AND_ASSIGN(WeakBinding); | |
| 89 }; | |
| 90 | |
| 91 | |
| 92 } // namespace mojo | |
| OLD | NEW |