Index: base/memory/weak_ptr.h |
diff --git a/base/memory/weak_ptr.h b/base/memory/weak_ptr.h |
index 3544439dd3cafc9d8cc849a2cb4181cf70d2e947..aa5e5a17b1a07505d40ba44d2c07c56693e134fe 100644 |
--- a/base/memory/weak_ptr.h |
+++ b/base/memory/weak_ptr.h |
@@ -99,6 +99,8 @@ class BASE_EXPORT WeakReference { |
void Invalidate(); |
bool IsValid() const; |
+ void DetachFromSequence(); |
+ |
private: |
friend class base::RefCountedThreadSafe<Flag>; |
@@ -109,7 +111,7 @@ class BASE_EXPORT WeakReference { |
}; |
WeakReference(); |
- explicit WeakReference(const Flag* flag); |
+ explicit WeakReference(const scoped_refptr<Flag>& flag); |
~WeakReference(); |
WeakReference(WeakReference&& other); |
@@ -130,12 +132,12 @@ class BASE_EXPORT WeakReferenceOwner { |
WeakReference GetRef() const; |
- bool HasRefs() const { |
- return flag_.get() && !flag_->HasOneRef(); |
- } |
+ bool HasRefs() const { return flag_ && !flag_->HasOneRef(); } |
void Invalidate(); |
+ void DetachFromSequence(); |
+ |
private: |
mutable scoped_refptr<WeakReference::Flag> flag_; |
}; |
@@ -288,18 +290,24 @@ class WeakPtrFactory { |
~WeakPtrFactory() { ptr_ = nullptr; } |
+ // Returns a new WeakPtr. Although GetWeakPtr() can be called from any thread |
+ // (assuming calling code ensures the WeakPtrFactory is still live at the |
+ // time), it is not safe to call concurrently from separate threads. Code |
+ // which hands-off WeakPtrs to separate threads for them to Bind() into |
+ // posted tasks, for example, should usually take a WeakPtr during setup |
+ // and store it for later use, rather than call GetWeakPtr() at Bind() time. |
WeakPtr<T> GetWeakPtr() { |
DCHECK(ptr_); |
return WeakPtr<T>(weak_reference_owner_.GetRef(), ptr_); |
} |
- // Call this method to invalidate all existing weak pointers. |
+ // Invalidates all existing weak pointers issued by this factory. |
void InvalidateWeakPtrs() { |
DCHECK(ptr_); |
weak_reference_owner_.Invalidate(); |
} |
- // Call this method to determine if any weak pointers exist. |
+ // Returns true if one or more WeakPtrs issued by this factory still exist. |
bool HasWeakPtrs() const { |
DCHECK(ptr_); |
return weak_reference_owner_.HasRefs(); |
@@ -328,6 +336,11 @@ class SupportsWeakPtr : public internal::SupportsWeakPtrBase { |
protected: |
~SupportsWeakPtr() {} |
+ // Asserts that no issued WeakPtrs exist, and detaches the sequence binding. |
+ void DetachFromSequence() { weak_reference_owner_.DetachFromSequence(); } |
+ |
+ bool HasWeakPtrs() const { return weak_reference_owner_.HasRefs(); } |
+ |
private: |
internal::WeakReferenceOwner weak_reference_owner_; |
DISALLOW_COPY_AND_ASSIGN(SupportsWeakPtr); |