Chromium Code Reviews| Index: base/ios/weak_nsobject.h |
| diff --git a/base/ios/weak_nsobject.h b/base/ios/weak_nsobject.h |
| index 46aecb5e0b641fb3d004e23b71a5111f02295bfe..baed4e44fbf296c5c0e5ceb244261d5423e49e97 100644 |
| --- a/base/ios/weak_nsobject.h |
| +++ b/base/ios/weak_nsobject.h |
| @@ -45,8 +45,13 @@ namespace base { |
| // receives nullify() from the object's sentinel. |
| class WeakContainer : public base::RefCountedThreadSafe<WeakContainer> { |
| public: |
| - WeakContainer(id object) : object_(object) {} |
| - id object() { return object_; } |
| + explicit WeakContainer(id object) : object_(object) {} |
| + |
| + id object() { |
| + DCHECK(checker_.CalledOnValidThread()); |
| + return object_; |
| + } |
| + |
| void nullify() { |
| DCHECK(checker_.CalledOnValidThread()); |
| object_ = nil; |
| @@ -74,53 +79,66 @@ namespace base { |
| // Base class for all WeakNSObject derivatives. |
| template <typename NST> |
| -class WeakNSProtocol : public base::NonThreadSafe { |
| +class WeakNSProtocol { |
| public: |
| explicit WeakNSProtocol(NST object = nil) { |
| + // A WeakNSProtocol object can be allocated on one thread and used on |
| + // another. This is not the case for the contained object. |
| + checker_.DetachFromThread(); |
|
lliabraa
2015/01/14 15:25:00
won't calling DetachFromThread() here cause the We
|
| container_ = [CRBWeakNSProtocolSentinel containerForObject:object]; |
| } |
| WeakNSProtocol(const WeakNSProtocol<NST>& that) { |
| + // A WeakNSProtocol object can be allocated on one thread and used on |
| + // another. This is not the case for the contained object. |
| + checker_.DetachFromThread(); |
| container_ = that.container_; |
| } |
| ~WeakNSProtocol() { |
| - // A WeakNSProtocol object can be allocated on one thread and released on |
| + // A WeakNSProtocol object can be used on one thread and released on |
| // another. This is not the case for the contained object. |
| - DetachFromThread(); |
| + checker_.DetachFromThread(); |
| } |
| void reset(NST object = nil) { |
| - DCHECK(CalledOnValidThread()); |
| + DCHECK(checker_.CalledOnValidThread()); |
| container_ = [CRBWeakNSProtocolSentinel containerForObject:object]; |
| } |
| NST get() const { |
| - DCHECK(CalledOnValidThread()); |
| + DCHECK(checker_.CalledOnValidThread()); |
| if (!container_.get()) |
| return nil; |
| return container_->object(); |
| } |
| WeakNSProtocol& operator=(const WeakNSProtocol<NST>& that) { |
| - DCHECK(CalledOnValidThread()); |
| + DCHECK(checker_.CalledOnValidThread()); |
| container_ = that.container_; |
| return *this; |
| } |
| bool operator==(NST that) const { |
| - DCHECK(CalledOnValidThread()); |
| + DCHECK(checker_.CalledOnValidThread()); |
| return get() == that; |
| } |
| - bool operator!=(NST that) const { return get() != that; } |
| + bool operator!=(NST that) const { |
| + DCHECK(checker_.CalledOnValidThread()); |
| + return get() != that; |
| + } |
| - operator NST() const { return get(); } |
| + operator NST() const { |
| + DCHECK(checker_.CalledOnValidThread()); |
| + return get(); |
| + } |
| private: |
| // Refecounted reference to the container tracking the ObjectiveC object this |
| // class encapsulates. |
| scoped_refptr<base::WeakContainer> container_; |
| + base::ThreadChecker checker_; |
| }; |
| // Free functions |