// 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. #ifndef MOJO_COMMON_WEAK_INTERFACE_PTR_SET_H_ #define MOJO_COMMON_WEAK_INTERFACE_PTR_SET_H_ #include #include "base/memory/weak_ptr.h" #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_ptr.h" namespace mojo { template class WeakInterfacePtr; template class WeakInterfacePtrSet { public: WeakInterfacePtrSet() {} ~WeakInterfacePtrSet() { CloseAll(); } void AddInterfacePtr(InterfacePtr ptr) { auto weak_interface_ptr = new WeakInterfacePtr(ptr.Pass()); ptrs_.push_back(weak_interface_ptr->GetWeakPtr()); ClearNullInterfacePtrs(); } template void ForAllPtrs(FunctionType function) { for (const auto& it : ptrs_) { if (it) function(it->get()); } ClearNullInterfacePtrs(); } void CloseAll() { for (const auto& it : ptrs_) { if (it) it->Close(); } ptrs_.clear(); } private: using WPWIPI = base::WeakPtr>; void ClearNullInterfacePtrs() { ptrs_.erase(std::remove_if(ptrs_.begin(), ptrs_.end(), [](const WPWIPI& p) { return p.get() == nullptr; }), ptrs_.end()); } std::vector ptrs_; }; template class WeakInterfacePtr : public ErrorHandler { public: explicit WeakInterfacePtr(InterfacePtr ptr) : ptr_(ptr.Pass()), weak_ptr_factory_(this) { ptr_.set_error_handler(this); } ~WeakInterfacePtr() override {} void Close() { ptr_.reset(); } Interface* get() { return ptr_.get(); } base::WeakPtr GetWeakPtr() { return weak_ptr_factory_.GetWeakPtr(); } private: // ErrorHandler implementation void OnConnectionError() override { delete this; } InterfacePtr ptr_; base::WeakPtrFactory weak_ptr_factory_; DISALLOW_COPY_AND_ASSIGN(WeakInterfacePtr); }; } // namespace mojo #endif // MOJO_COMMON_WEAK_INTERFACE_PTR_SET_H_