Commit 453d0b5b authored by Trent Apted's avatar Trent Apted Committed by Commit Bot

base::CheckedObserver: Follow-up review comments

Follow-ups to review comments in https://crrev.com/c/1053338

Bug: 842987
Change-Id: I1cf78667887bb86555d3f9043e2afe5d8eacf2bb
Reviewed-on: https://chromium-review.googlesource.com/1184590Reviewed-by: default avatarWez <wez@chromium.org>
Reviewed-by: default avatarGabriel Charette <gab@chromium.org>
Commit-Queue: Trent Apted <tapted@chromium.org>
Cr-Commit-Position: refs/heads/master@{#585327}
parent 8e6f1aaf
...@@ -270,7 +270,7 @@ class WeakPtr : public internal::WeakPtrBase { ...@@ -270,7 +270,7 @@ class WeakPtr : public internal::WeakPtrBase {
// Returns whether the object |this| points to has been invalidated. This can // Returns whether the object |this| points to has been invalidated. This can
// be used to distinguish a WeakPtr to a destroyed object from one that has // be used to distinguish a WeakPtr to a destroyed object from one that has
// been explicitly set to null. A null WeakPtr is always valid. // been explicitly set to null.
bool WasInvalidated() const { return ptr_ && !ref_.IsValid(); } bool WasInvalidated() const { return ptr_ && !ref_.IsValid(); }
private: private:
......
...@@ -393,27 +393,31 @@ TEST(WeakPtrTest, InvalidateWeakPtrs) { ...@@ -393,27 +393,31 @@ TEST(WeakPtrTest, InvalidateWeakPtrs) {
} }
// Tests that WasInvalidated() is true only for invalidated WeakPtrs (not // Tests that WasInvalidated() is true only for invalidated WeakPtrs (not
// nullptr) and doesn't DCHECK. // nullptr) and doesn't DCHECK (e.g. because of a dereference attempt).
TEST(WeakPtrTest, WasInvalidatedByFactoryDestruction) { TEST(WeakPtrTest, WasInvalidatedByFactoryDestruction) {
WeakPtr<int> ptr; WeakPtr<int> ptr;
EXPECT_FALSE(ptr.WasInvalidated()); EXPECT_FALSE(ptr.WasInvalidated());
// Test |data| destroyed. // Test |data| destroyed. That is, the typical pattern when |data| (and its
// associated factory) go out of scope.
{ {
int data; int data = 0;
WeakPtrFactory<int> factory(&data); WeakPtrFactory<int> factory(&data);
ptr = factory.GetWeakPtr(); ptr = factory.GetWeakPtr();
// Verify that a live WeakPtr is not reported as Invalidated.
EXPECT_FALSE(ptr.WasInvalidated()); EXPECT_FALSE(ptr.WasInvalidated());
} }
EXPECT_TRUE(ptr.WasInvalidated()); // Shouldn't tickle asan.
// Checking validity shouldn't read beyond the stack frame.
EXPECT_TRUE(ptr.WasInvalidated());
ptr = nullptr; ptr = nullptr;
EXPECT_FALSE(ptr.WasInvalidated()); EXPECT_FALSE(ptr.WasInvalidated());
} }
// As above, but testing InvalidateWeakPtrs(). // As above, but testing InvalidateWeakPtrs().
TEST(WeakPtrTest, WasInvalidatedByInvalidateWeakPtrs) { TEST(WeakPtrTest, WasInvalidatedByInvalidateWeakPtrs) {
int data; int data = 0;
WeakPtrFactory<int> factory(&data); WeakPtrFactory<int> factory(&data);
WeakPtr<int> ptr = factory.GetWeakPtr(); WeakPtr<int> ptr = factory.GetWeakPtr();
EXPECT_FALSE(ptr.WasInvalidated()); EXPECT_FALSE(ptr.WasInvalidated());
...@@ -423,9 +427,10 @@ TEST(WeakPtrTest, WasInvalidatedByInvalidateWeakPtrs) { ...@@ -423,9 +427,10 @@ TEST(WeakPtrTest, WasInvalidatedByInvalidateWeakPtrs) {
EXPECT_FALSE(ptr.WasInvalidated()); EXPECT_FALSE(ptr.WasInvalidated());
} }
// Test WasInvalidated() when assigning null before invalidating. // A WeakPtr should not be reported as 'invalidated' if nullptr was assigned to
// it.
TEST(WeakPtrTest, WasInvalidatedWhilstNull) { TEST(WeakPtrTest, WasInvalidatedWhilstNull) {
int data; int data = 0;
WeakPtrFactory<int> factory(&data); WeakPtrFactory<int> factory(&data);
WeakPtr<int> ptr = factory.GetWeakPtr(); WeakPtr<int> ptr = factory.GetWeakPtr();
EXPECT_FALSE(ptr.WasInvalidated()); EXPECT_FALSE(ptr.WasInvalidated());
......
...@@ -104,8 +104,8 @@ class ObserverList : public SupportsWeakPtr<ObserverList<ObserverType, ...@@ -104,8 +104,8 @@ class ObserverList : public SupportsWeakPtr<ObserverList<ObserverType,
public: public:
// Allow declaring an ObserverList<...>::Unchecked that replaces the default // Allow declaring an ObserverList<...>::Unchecked that replaces the default
// ObserverStorageType to use raw pointers. This is required to support legacy // ObserverStorageType to use raw pointers. This is required to support legacy
// observers that do not inherit from CheckedObserver. The majority of NEW // observers that do not inherit from CheckedObserver. The majority of new
// CODE SHOULD NOT USE THIS, but it may be suited for performance-critical // code should not use this, but it may be suited for performance-critical
// situations to avoid overheads of a CHECK(). Note the type can't be chosen // situations to avoid overheads of a CHECK(). Note the type can't be chosen
// based on ObserverType's definition because ObserverLists are often declared // based on ObserverType's definition because ObserverLists are often declared
// in headers using a forward-declare of ObserverType. // in headers using a forward-declare of ObserverType.
...@@ -285,8 +285,8 @@ class ObserverList : public SupportsWeakPtr<ObserverList<ObserverType, ...@@ -285,8 +285,8 @@ class ObserverList : public SupportsWeakPtr<ObserverList<ObserverType,
// Determine whether a particular observer is in the list. // Determine whether a particular observer is in the list.
bool HasObserver(const ObserverType* obs) const { bool HasObserver(const ObserverType* obs) const {
// Client code passing null could be confused by the treatment of observers // Client code passing null could be confused by the treatment of observers
// removed mid-iteration. TODO(tapted): This should probably DCHECK, but // removed mid-iteration. TODO(https://crbug.com/876588): This should
// some client code currently does pass null to HasObserver(). // probably DCHECK, but some client code currently does pass null.
if (obs == nullptr) if (obs == nullptr)
return false; return false;
return std::find_if(observers_.begin(), observers_.end(), return std::find_if(observers_.begin(), observers_.end(),
......
...@@ -39,8 +39,6 @@ class BASE_EXPORT UncheckedObserverAdapter { ...@@ -39,8 +39,6 @@ class BASE_EXPORT UncheckedObserverAdapter {
private: private:
void* ptr_; void* ptr_;
// Although copying works, disallow it to be consistent with
// CheckedObserverAdapter.
DISALLOW_COPY_AND_ASSIGN(UncheckedObserverAdapter); DISALLOW_COPY_AND_ASSIGN(UncheckedObserverAdapter);
}; };
...@@ -61,7 +59,7 @@ class BASE_EXPORT CheckedObserverAdapter { ...@@ -61,7 +59,7 @@ class BASE_EXPORT CheckedObserverAdapter {
void MarkForRemoval() { void MarkForRemoval() {
DCHECK(weak_ptr_); DCHECK(weak_ptr_);
weak_ptr_.reset(); weak_ptr_ = nullptr;
} }
bool IsMarkedForRemoval() const { bool IsMarkedForRemoval() const {
......
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