| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "ios/web/weak_nsobject_counter.h" | 5 #import "ios/web/weak_nsobject_counter.h" |
| 6 | 6 |
| 7 #import <objc/runtime.h> | 7 #import <objc/runtime.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #import "base/mac/scoped_nsobject.h" | 10 #import "base/mac/scoped_nsobject.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 // Designated initializer. |object| cannot be nil. It registers self as an | 22 // Designated initializer. |object| cannot be nil. It registers self as an |
| 23 // associated object to |object|. | 23 // associated object to |object|. |
| 24 - (instancetype)initWithSharedCounter:(const linked_ptr<NSUInteger>&)counter | 24 - (instancetype)initWithSharedCounter:(const linked_ptr<NSUInteger>&)counter |
| 25 objectToBeObserved:(id)object; | 25 objectToBeObserved:(id)object; |
| 26 @end | 26 @end |
| 27 | 27 |
| 28 @implementation CRBWeakNSObjectDeallocationObserver { | 28 @implementation CRBWeakNSObjectDeallocationObserver { |
| 29 linked_ptr<NSUInteger> _counter; | 29 linked_ptr<NSUInteger> _counter; |
| 30 } | 30 } |
| 31 | 31 |
| 32 - (instancetype)init { | |
| 33 NOTREACHED(); | |
| 34 return nil; | |
| 35 } | |
| 36 | |
| 37 - (instancetype)initWithSharedCounter:(const linked_ptr<NSUInteger>&)counter | 32 - (instancetype)initWithSharedCounter:(const linked_ptr<NSUInteger>&)counter |
| 38 objectToBeObserved:(id)object { | 33 objectToBeObserved:(id)object { |
| 39 self = [super init]; | 34 self = [super init]; |
| 40 if (self) { | 35 if (self) { |
| 41 DCHECK(counter.get()); | 36 DCHECK(counter.get()); |
| 42 DCHECK(object); | 37 DCHECK(object); |
| 43 _counter = counter; | 38 _counter = counter; |
| 44 objc_setAssociatedObject(object, &kObserverAssociatedObjectKey, self, | 39 objc_setAssociatedObject(object, &kObserverAssociatedObjectKey, self, |
| 45 OBJC_ASSOCIATION_RETAIN); | 40 OBJC_ASSOCIATION_RETAIN); |
| 46 (*_counter)++; | 41 (*_counter)++; |
| 47 } | 42 } |
| 48 return self; | 43 return self; |
| 49 } | 44 } |
| 50 | 45 |
| 46 - (instancetype)init { |
| 47 NOTREACHED(); |
| 48 return nil; |
| 49 } |
| 50 |
| 51 - (void)dealloc { | 51 - (void)dealloc { |
| 52 DCHECK(_counter.get()); | 52 DCHECK(_counter.get()); |
| 53 (*_counter)--; | 53 (*_counter)--; |
| 54 _counter.reset(); | 54 _counter.reset(); |
| 55 [super dealloc]; | 55 [super dealloc]; |
| 56 } | 56 } |
| 57 | 57 |
| 58 @end | 58 @end |
| 59 | 59 |
| 60 namespace web { | 60 namespace web { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 74 [[CRBWeakNSObjectDeallocationObserver alloc] | 74 [[CRBWeakNSObjectDeallocationObserver alloc] |
| 75 initWithSharedCounter:counter_ objectToBeObserved:object]); | 75 initWithSharedCounter:counter_ objectToBeObserved:object]); |
| 76 } | 76 } |
| 77 | 77 |
| 78 NSUInteger WeakNSObjectCounter::Size() const { | 78 NSUInteger WeakNSObjectCounter::Size() const { |
| 79 DCHECK(CalledOnValidThread()); | 79 DCHECK(CalledOnValidThread()); |
| 80 return *counter_; | 80 return *counter_; |
| 81 } | 81 } |
| 82 | 82 |
| 83 } // namespace web | 83 } // namespace web |
| OLD | NEW |