blob: 3c23a397a8f64454e0c4df429706a4b33c7f92bb (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/web/weak_nsobject_counter.h"
#import <objc/runtime.h>
#include "base/logging.h"
#import "base/mac/scoped_nsobject.h"
// Used for observing the objects tracked in the WeakNSObjectCounter. This
// object will be dealloced when the tracked object is dealloced and will
// notify the shared counter.
@interface CRBWeakNSObjectDeallocationObserver : NSObject
// Designated initializer. |object| cannot be nil. It registers self as an
// associated object to |object|.
- (instancetype)initWithSharedCounter:(const linked_ptr<NSUInteger>&)counter
objectToBeObserved:(id)object;
@end
@implementation CRBWeakNSObjectDeallocationObserver {
linked_ptr<NSUInteger> _counter;
}
- (instancetype)initWithSharedCounter:(const linked_ptr<NSUInteger>&)counter
objectToBeObserved:(id)object {
self = [super init];
if (self) {
DCHECK(counter.get());
DCHECK(object);
_counter = counter;
objc_setAssociatedObject(
object,
reinterpret_cast<const void*>(_counter.get()), // The key.
self, OBJC_ASSOCIATION_RETAIN);
(*_counter)++;
}
return self;
}
- (instancetype)init {
NOTREACHED();
return nil;
}
- (void)dealloc {
DCHECK(_counter.get());
(*_counter)--;
_counter.reset();
[super dealloc];
}
@end
namespace web {
WeakNSObjectCounter::WeakNSObjectCounter() : counter_(new NSUInteger(0)) {
}
WeakNSObjectCounter::~WeakNSObjectCounter() {
DCHECK(CalledOnValidThread());
}
void WeakNSObjectCounter::Insert(id object) {
DCHECK(CalledOnValidThread());
DCHECK(object);
// Create an associated object and register it with |object|.
base::scoped_nsobject<CRBWeakNSObjectDeallocationObserver> observingObject(
[[CRBWeakNSObjectDeallocationObserver alloc]
initWithSharedCounter:counter_ objectToBeObserved:object]);
}
NSUInteger WeakNSObjectCounter::Size() const {
DCHECK(CalledOnValidThread());
return *counter_;
}
} // namespace web
|