OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/ios/weak_nsobject.h" |
| 6 |
| 7 #include "base/mac/scoped_nsautorelease_pool.h" |
| 8 #include "base/mac/scoped_nsobject.h" |
| 9 |
| 10 namespace { |
| 11 // The key needed by objc_setAssociatedObject. |
| 12 char sentinelObserverKey_; |
| 13 } |
| 14 |
| 15 @interface CRBWeakNSProtocolSentinel () |
| 16 // Container to notify on dealloc. |
| 17 @property(readonly, assign) scoped_refptr<base::WeakContainer> container; |
| 18 // Designed initializer. |
| 19 - (id)initWithContainer:(scoped_refptr<base::WeakContainer>)container; |
| 20 @end |
| 21 |
| 22 @implementation CRBWeakNSProtocolSentinel |
| 23 |
| 24 @synthesize container = container_; |
| 25 |
| 26 + (scoped_refptr<base::WeakContainer>)containerForObject:(id)object { |
| 27 if (object == nil) |
| 28 return nullptr; |
| 29 // The autoreleasePool is needed here as the call to objc_getAssociatedObject |
| 30 // returns an autoreleased object which is better released sooner than later. |
| 31 base::mac::ScopedNSAutoreleasePool pool; |
| 32 CRBWeakNSProtocolSentinel* sentinel = |
| 33 objc_getAssociatedObject(object, &sentinelObserverKey_); |
| 34 if (!sentinel) { |
| 35 base::scoped_nsobject<CRBWeakNSProtocolSentinel> newSentinel( |
| 36 [[CRBWeakNSProtocolSentinel alloc] |
| 37 initWithContainer:new base::WeakContainer(object)]); |
| 38 sentinel = newSentinel; |
| 39 objc_setAssociatedObject(object, &sentinelObserverKey_, sentinel, |
| 40 OBJC_ASSOCIATION_RETAIN); |
| 41 // The retain count is 2. One retain is due to the alloc, the other to the |
| 42 // association with the weak object. |
| 43 DCHECK_EQ(2u, [sentinel retainCount]); |
| 44 } |
| 45 return [sentinel container]; |
| 46 } |
| 47 |
| 48 - (id)initWithContainer:(scoped_refptr<base::WeakContainer>)container { |
| 49 DCHECK(container.get()); |
| 50 self = [super init]; |
| 51 if (self) |
| 52 container_ = container; |
| 53 return self; |
| 54 } |
| 55 |
| 56 - (void)dealloc { |
| 57 self.container->nullify(); |
| 58 [super dealloc]; |
| 59 } |
| 60 |
| 61 @end |
OLD | NEW |