Commit 1fd547a9 authored by droger's avatar droger Committed by Commit bot

[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}
parent 18fd3fcf
...@@ -117,7 +117,9 @@ class WeakNSProtocol { ...@@ -117,7 +117,9 @@ class WeakNSProtocol {
} }
WeakNSProtocol& operator=(const WeakNSProtocol<NST>& that) { 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_; container_ = that.container_;
return *this; return *this;
} }
......
...@@ -109,8 +109,12 @@ void TouchWeakData(const WeakNSObject<NSMutableData>& weak_data) { ...@@ -109,8 +109,12 @@ void TouchWeakData(const WeakNSObject<NSMutableData>& weak_data) {
// the weak object on its original thread. // the weak object on its original thread.
void CopyWeakNSObjectAndPost(const WeakNSObject<NSMutableData>& weak_object, void CopyWeakNSObjectAndPost(const WeakNSObject<NSMutableData>& weak_object,
scoped_refptr<SingleThreadTaskRunner> runner) { scoped_refptr<SingleThreadTaskRunner> runner) {
WeakNSObject<NSMutableData> weak_copy(weak_object); // Copy using constructor.
runner->PostTask(FROM_HERE, Bind(&TouchWeakData, weak_copy)); 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. // Tests that the weak object can be copied on a different thread.
...@@ -128,8 +132,8 @@ TEST(WeakNSObjectTest, WeakNSObjectCopyOnOtherThread) { ...@@ -128,8 +132,8 @@ TEST(WeakNSObjectTest, WeakNSObjectCopyOnOtherThread) {
other_thread.Stop(); other_thread.Stop();
loop.RunUntilIdle(); loop.RunUntilIdle();
// Check that TouchWeakData was called. // Check that TouchWeakData was called and the object touched twice.
EXPECT_EQ(1u, [data length]); EXPECT_EQ(2u, [data length]);
} }
} // namespace } // namespace
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment