Index: base/memory/weak_ptr.cc |
diff --git a/base/memory/weak_ptr.cc b/base/memory/weak_ptr.cc |
index c179b80097cc674e1ad2e484b99909166dda860a..6ec4fd3cf359ae0ad3c20a806b5496d389bb4642 100644 |
--- a/base/memory/weak_ptr.cc |
+++ b/base/memory/weak_ptr.cc |
@@ -15,9 +15,7 @@ WeakReference::Flag::Flag() : is_valid_(true) { |
} |
void WeakReference::Flag::Invalidate() { |
- // The flag being invalidated with a single ref implies that there are no |
- // weak pointers in existence. Allow deletion on other thread in this case. |
- DCHECK(sequence_checker_.CalledOnValidSequence() || HasOneRef()) |
+ DCHECK(sequence_checker_.CalledOnValidSequence()) |
<< "WeakPtrs must be invalidated on the same sequenced thread."; |
is_valid_ = false; |
} |
@@ -28,14 +26,18 @@ bool WeakReference::Flag::IsValid() const { |
return is_valid_; |
} |
+void WeakReference::Flag::DetachFromSequence() { |
+ DCHECK(HasOneRef()) << "Cannot detach from Sequence while WeakPtrs exist."; |
+ sequence_checker_.DetachFromSequence(); |
+} |
+ |
WeakReference::Flag::~Flag() { |
} |
WeakReference::WeakReference() { |
} |
-WeakReference::WeakReference(const Flag* flag) : flag_(flag) { |
-} |
+WeakReference::WeakReference(const scoped_refptr<Flag>& flag) : flag_(flag) {} |
WeakReference::~WeakReference() { |
} |
@@ -44,7 +46,9 @@ WeakReference::WeakReference(WeakReference&& other) = default; |
WeakReference::WeakReference(const WeakReference& other) = default; |
-bool WeakReference::is_valid() const { return flag_.get() && flag_->IsValid(); } |
+bool WeakReference::is_valid() const { |
+ return flag_ && flag_->IsValid(); |
+} |
WeakReferenceOwner::WeakReferenceOwner() { |
} |
@@ -54,20 +58,23 @@ WeakReferenceOwner::~WeakReferenceOwner() { |
} |
WeakReference WeakReferenceOwner::GetRef() const { |
- // If we hold the last reference to the Flag then create a new one. |
- if (!HasRefs()) |
+ if (!flag_) |
flag_ = new WeakReference::Flag(); |
- return WeakReference(flag_.get()); |
+ return WeakReference(flag_); |
} |
void WeakReferenceOwner::Invalidate() { |
- if (flag_.get()) { |
+ if (flag_) { |
flag_->Invalidate(); |
- flag_ = NULL; |
+ flag_ = nullptr; |
} |
} |
+void WeakReferenceOwner::DetachFromSequence() { |
+ flag_->DetachFromSequence(); |
+} |
+ |
WeakPtrBase::WeakPtrBase() { |
} |