Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(170)

Side by Side Diff: base/ios/weak_nsobject.mm

Issue 792443004: Upstream WeakNSObject in //base/ios (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: cleanup Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/ios/weak_nsobject.h ('k') | base/ios/weak_nsobject_unittest.mm » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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
OLDNEW
« no previous file with comments | « base/ios/weak_nsobject.h ('k') | base/ios/weak_nsobject_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698