Chromium Code Reviews| Index: mojo/common/weak_binding_set.h |
| diff --git a/mojo/common/weak_binding_set.h b/mojo/common/weak_binding_set.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c47aafe686c737b7ce3a050c816286e59f97426a |
| --- /dev/null |
| +++ b/mojo/common/weak_binding_set.h |
| @@ -0,0 +1,92 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <algorithm> |
| +#include <vector> |
| + |
| +#include "base/memory/weak_ptr.h" |
| +#include "mojo/public/cpp/bindings/binding.h" |
| + |
| +namespace mojo { |
| + |
| +template <typename Interface> |
| +class WeakBinding; |
| + |
| +// Use this class to manage a set of weak pointers to bindings each of which is |
| +// owned by the pipe they are bound to. |
| +template <typename Interface> |
| +class WeakBindingSet { |
| + public: |
| + WeakBindingSet() {} |
| + ~WeakBindingSet() {} |
| + |
| + void AddBinding(Interface* impl, InterfaceRequest<Interface> request) { |
| + auto binding = new WeakBinding<Interface>(impl, request.Pass()); |
| + bindings_.push_back(binding->GetWeakPtr()); |
| + ClearNullBindings(); |
| + } |
| + |
| + |
| + void CloseAllBindings() { |
| + for (const auto& it : bindings_) { |
| + 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
|
| + it->Close(); |
| + } |
| + bindings_.clear(); |
| + } |
| + |
| + template <typename FunctionType> |
| + void ForAllBindings(FunctionType function) { |
| + for (const auto& it : bindings_) { |
| + if (it.get()) |
| + function(it.get()->client()); |
| + } |
| + ClearNullBindings(); |
| + } |
| + |
| + private: |
| + void ClearNullBindings() { |
| + bindings_.erase( |
| + std::remove_if(bindings_.begin(), bindings_.end(), |
| + [](const base::WeakPtr<WeakBinding<Interface>>& p) { |
| + 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
|
| + }), |
| + bindings_.end()); |
| + } |
| + |
| + std::vector<base::WeakPtr<WeakBinding<Interface>>> bindings_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(WeakBindingSet); |
| +}; |
| + |
| +template <typename Interface> |
| +class WeakBinding : public ErrorHandler { |
| + public: |
| + WeakBinding(Interface* impl, InterfaceRequest<Interface> request) |
| + : binding_(impl, request.Pass()), weak_ptr_factory_(this) { |
| + binding_.set_error_handler(this); |
| + } |
| + |
| + ~WeakBinding() override {} |
| + |
| + typename Interface::Client* client() { return binding_.client(); } |
| + |
| + base::WeakPtr<WeakBinding> GetWeakPtr() { |
| + return weak_ptr_factory_.GetWeakPtr(); |
| + } |
| + |
| + void Close() { binding_.Close(); } |
| + |
| + // ErrorHandler implementation. |
| + void OnConnectionError() override { delete this; } |
| + |
| + private: |
| + mojo::Binding<Interface> binding_; |
| + base::WeakPtrFactory<WeakBinding> weak_ptr_factory_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(WeakBinding); |
| +}; |
| + |
| + |
| +} // namespace mojo |