| Index: mojo/common/weak_interface_ptr_set.h
|
| diff --git a/mojo/common/weak_interface_ptr_set.h b/mojo/common/weak_interface_ptr_set.h
|
| index e2e88a5c4f3c178525b7ca64504fcaacb9e40409..f93c0259574a7b7cadcd51023c41b902f31fc54a 100644
|
| --- a/mojo/common/weak_interface_ptr_set.h
|
| +++ b/mojo/common/weak_interface_ptr_set.h
|
| @@ -7,26 +7,68 @@
|
|
|
| #include <vector>
|
|
|
| +#include "base/logging.h"
|
| #include "base/memory/weak_ptr.h"
|
| -#include "third_party/mojo/src/mojo/public/cpp/bindings/interface_ptr.h"
|
| +#include "mojo/public/cpp/bindings/interface_ptr.h"
|
|
|
| namespace mojo {
|
|
|
| +namespace internal {
|
| +
|
| +// TODO(vtl): This name of this class is a little odd -- it's not a "weak
|
| +// pointer", but a wrapper around InterfacePtr that owns itself and can vend
|
| +// weak pointers to itself. Probably, with connection error callbacks instead of
|
| +// ErrorHandlers, this class is unneeded, and WeakInterfacePtrSet can simply
|
| +// own/remove interface pointers as connection errors occur.
|
| +// https://github.com/domokit/mojo/issues/311
|
| template <typename Interface>
|
| -class WeakInterfacePtr;
|
| +class WeakInterfacePtr {
|
| + public:
|
| + explicit WeakInterfacePtr(InterfacePtr<Interface> ptr)
|
| + : ptr_(ptr.Pass()), weak_ptr_factory_(this) {
|
| + ptr_.set_connection_error_handler([this]() { delete this; });
|
| + }
|
| + ~WeakInterfacePtr() {}
|
| +
|
| + void Close() { ptr_.reset(); }
|
| +
|
| + Interface* get() { return ptr_.get(); }
|
|
|
| + base::WeakPtr<WeakInterfacePtr> GetWeakPtr() {
|
| + return weak_ptr_factory_.GetWeakPtr();
|
| + }
|
| +
|
| + private:
|
| + InterfacePtr<Interface> ptr_;
|
| + base::WeakPtrFactory<WeakInterfacePtr> weak_ptr_factory_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(WeakInterfacePtr);
|
| +};
|
| +
|
| +} // namespace internal
|
| +
|
| +// A WeakInterfacePtrSet contains a collection of InterfacePtrs
|
| +// that are automatically removed from the collection and destroyed
|
| +// when their associated MessagePipe experiences a connection error.
|
| +// When the set is destroyed all of the MessagePipes will be closed.
|
| +// TODO(rudominer) Rename this class since the ownership of the elements
|
| +// is not "weak" from the point of view of the client.
|
| template <typename Interface>
|
| class WeakInterfacePtrSet {
|
| public:
|
| WeakInterfacePtrSet() {}
|
| ~WeakInterfacePtrSet() { CloseAll(); }
|
|
|
| + // |ptr| must be bound to a message pipe.
|
| void AddInterfacePtr(InterfacePtr<Interface> ptr) {
|
| - auto weak_interface_ptr = new WeakInterfacePtr<Interface>(ptr.Pass());
|
| + DCHECK(ptr.is_bound());
|
| + auto weak_interface_ptr =
|
| + new internal::WeakInterfacePtr<Interface>(ptr.Pass());
|
| ptrs_.push_back(weak_interface_ptr->GetWeakPtr());
|
| ClearNullInterfacePtrs();
|
| }
|
|
|
| + // Applies |function| to each of the InterfacePtrs in the set.
|
| template <typename FunctionType>
|
| void ForAllPtrs(FunctionType function) {
|
| for (const auto& it : ptrs_) {
|
| @@ -36,6 +78,8 @@ class WeakInterfacePtrSet {
|
| ClearNullInterfacePtrs();
|
| }
|
|
|
| + // Closes the MessagePipe associated with each of the InterfacePtrs in
|
| + // this set and clears the set.
|
| void CloseAll() {
|
| for (const auto& it : ptrs_) {
|
| if (it)
|
| @@ -44,8 +88,15 @@ class WeakInterfacePtrSet {
|
| ptrs_.clear();
|
| }
|
|
|
| + // TODO(rudominer) After reworking this class and eliminating the method
|
| + // ClearNullInterfacePtrs, this method should become const.
|
| + size_t size() {
|
| + ClearNullInterfacePtrs();
|
| + return ptrs_.size();
|
| + }
|
| +
|
| private:
|
| - using WPWIPI = base::WeakPtr<WeakInterfacePtr<Interface>>;
|
| + using WPWIPI = base::WeakPtr<internal::WeakInterfacePtr<Interface>>;
|
|
|
| void ClearNullInterfacePtrs() {
|
| ptrs_.erase(std::remove_if(ptrs_.begin(), ptrs_.end(), [](const WPWIPI& p) {
|
| @@ -56,33 +107,6 @@ class WeakInterfacePtrSet {
|
| std::vector<WPWIPI> ptrs_;
|
| };
|
|
|
| -template <typename Interface>
|
| -class WeakInterfacePtr : public ErrorHandler {
|
| - public:
|
| - explicit WeakInterfacePtr(InterfacePtr<Interface> 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<WeakInterfacePtr> GetWeakPtr() {
|
| - return weak_ptr_factory_.GetWeakPtr();
|
| - }
|
| -
|
| - private:
|
| - // ErrorHandler implementation
|
| - void OnConnectionError() override { delete this; }
|
| -
|
| - InterfacePtr<Interface> ptr_;
|
| - base::WeakPtrFactory<WeakInterfacePtr> weak_ptr_factory_;
|
| -
|
| - DISALLOW_COPY_AND_ASSIGN(WeakInterfacePtr);
|
| -};
|
| -
|
| } // namespace mojo
|
|
|
| #endif // MOJO_COMMON_WEAK_INTERFACE_PTR_SET_H_
|
|
|