[iOS] WeakNSObject assignment is consistent with copy constructor
The copy constructor was changed (in CL
https://codereview.chromium.org/853503002/) to allow a WeakNSObject to
be copied on a different thread.
This CL is making the assignment operator behave consistently with the
copy constructor: it is now allowed to assign a WeakNSObject on a
different thread.
In any case, dereferencing the WeakNSObject on another thread remains
disallowed.
BUG=394008
Review URL: https://codereview.chromium.org/1014813002
Cr-Commit-Position: refs/heads/master@{#320915}
diff --git a/base/ios/weak_nsobject.h b/base/ios/weak_nsobject.h
index a1984bb..fc3a7c3 100644
--- a/base/ios/weak_nsobject.h
+++ b/base/ios/weak_nsobject.h
@@ -117,7 +117,9 @@
}
WeakNSProtocol& operator=(const WeakNSProtocol<NST>& that) {
- DCHECK(checker_.CalledOnValidThread());
+ // A WeakNSProtocol object can be copied on one thread and used on
+ // another.
+ checker_.DetachFromThread();
container_ = that.container_;
return *this;
}
diff --git a/base/ios/weak_nsobject_unittest.mm b/base/ios/weak_nsobject_unittest.mm
index 325dcd21..81de993 100644
--- a/base/ios/weak_nsobject_unittest.mm
+++ b/base/ios/weak_nsobject_unittest.mm
@@ -109,8 +109,12 @@
// the weak object on its original thread.
void CopyWeakNSObjectAndPost(const WeakNSObject<NSMutableData>& weak_object,
scoped_refptr<SingleThreadTaskRunner> runner) {
- WeakNSObject<NSMutableData> weak_copy(weak_object);
- runner->PostTask(FROM_HERE, Bind(&TouchWeakData, weak_copy));
+ // Copy using constructor.
+ WeakNSObject<NSMutableData> weak_copy1(weak_object);
+ runner->PostTask(FROM_HERE, Bind(&TouchWeakData, weak_copy1));
+ // Copy using assignment operator.
+ WeakNSObject<NSMutableData> weak_copy2 = weak_object;
+ runner->PostTask(FROM_HERE, Bind(&TouchWeakData, weak_copy2));
}
// Tests that the weak object can be copied on a different thread.
@@ -128,8 +132,8 @@
other_thread.Stop();
loop.RunUntilIdle();
- // Check that TouchWeakData was called.
- EXPECT_EQ(1u, [data length]);
+ // Check that TouchWeakData was called and the object touched twice.
+ EXPECT_EQ(2u, [data length]);
}
} // namespace