OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/chrome/browser/snapshots/lru_cache.h" | 5 #import "ios/chrome/browser/snapshots/lru_cache.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/containers/hash_tables.h" | 9 #include "base/containers/hash_tables.h" |
10 #include "base/containers/mru_cache.h" | 10 #include "base/containers/mru_cache.h" |
11 #include "base/mac/scoped_nsobject.h" | 11 #include "base/mac/scoped_nsobject.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 class MRUCacheNSObjectDelegate { | |
18 public: | |
19 MRUCacheNSObjectDelegate(id<LRUCacheDelegate> delegate) | |
20 : delegate_(delegate) {} | |
21 | |
22 MRUCacheNSObjectDelegate(const MRUCacheNSObjectDelegate& other) = default; | |
23 | |
24 void operator()(const base::scoped_nsprotocol<id<NSObject>>& payload) const { | |
25 [delegate_ lruCacheWillEvictObject:payload.get()]; | |
26 } | |
27 | |
28 private: | |
29 id<LRUCacheDelegate> delegate_; // Weak. | |
30 }; | |
31 | |
32 struct NSObjectEqualTo { | 17 struct NSObjectEqualTo { |
33 bool operator()(const base::scoped_nsprotocol<id<NSObject>>& obj1, | 18 bool operator()(const base::scoped_nsprotocol<id<NSObject>>& obj1, |
34 const base::scoped_nsprotocol<id<NSObject>>& obj2) const { | 19 const base::scoped_nsprotocol<id<NSObject>>& obj2) const { |
35 return [obj1 isEqual:obj2]; | 20 return [obj1 isEqual:obj2]; |
36 } | 21 } |
37 }; | 22 }; |
38 | 23 |
39 struct NSObjectHash { | 24 struct NSObjectHash { |
40 std::size_t operator()( | 25 std::size_t operator()( |
41 const base::scoped_nsprotocol<id<NSObject>>& obj) const { | 26 const base::scoped_nsprotocol<id<NSObject>>& obj) const { |
42 return [obj hash]; | 27 return [obj hash]; |
43 } | 28 } |
44 }; | 29 }; |
45 | 30 |
46 template <class KeyType, class ValueType, class HashType> | 31 template <class KeyType, class ValueType, class HashType> |
47 struct MRUCacheNSObjectHashMap { | 32 struct MRUCacheNSObjectHashMap { |
48 typedef base::hash_map<KeyType, ValueType, HashType, NSObjectEqualTo> Type; | 33 typedef base::hash_map<KeyType, ValueType, HashType, NSObjectEqualTo> Type; |
49 }; | 34 }; |
50 | 35 |
51 class NSObjectMRUCache | 36 class NSObjectMRUCache |
52 : public base::MRUCacheBase<base::scoped_nsprotocol<id<NSObject>>, | 37 : public base::MRUCacheBase<base::scoped_nsprotocol<id<NSObject>>, |
53 base::scoped_nsprotocol<id<NSObject>>, | 38 base::scoped_nsprotocol<id<NSObject>>, |
54 NSObjectHash, | 39 NSObjectHash, |
55 MRUCacheNSObjectDelegate, | |
56 MRUCacheNSObjectHashMap> { | 40 MRUCacheNSObjectHashMap> { |
57 private: | 41 private: |
58 typedef base::MRUCacheBase<base::scoped_nsprotocol<id<NSObject>>, | 42 typedef base::MRUCacheBase<base::scoped_nsprotocol<id<NSObject>>, |
59 base::scoped_nsprotocol<id<NSObject>>, | 43 base::scoped_nsprotocol<id<NSObject>>, |
60 NSObjectHash, | 44 NSObjectHash, |
61 MRUCacheNSObjectDelegate, | |
62 MRUCacheNSObjectHashMap> | 45 MRUCacheNSObjectHashMap> |
63 ParentType; | 46 ParentType; |
64 | 47 |
65 public: | 48 public: |
66 NSObjectMRUCache(typename ParentType::size_type max_size, | 49 explicit NSObjectMRUCache(typename ParentType::size_type max_size) |
67 const MRUCacheNSObjectDelegate& deletor) | 50 : ParentType(max_size) {} |
68 : ParentType(max_size, deletor) {} | |
69 | 51 |
70 private: | 52 private: |
71 DISALLOW_COPY_AND_ASSIGN(NSObjectMRUCache); | 53 DISALLOW_COPY_AND_ASSIGN(NSObjectMRUCache); |
72 }; | 54 }; |
73 | 55 |
74 } // namespace | 56 } // namespace |
75 | 57 |
76 @interface LRUCache ()<LRUCacheDelegate> | |
77 @end | |
78 | |
79 @implementation LRUCache { | 58 @implementation LRUCache { |
80 scoped_ptr<NSObjectMRUCache> _cache; | 59 scoped_ptr<NSObjectMRUCache> _cache; |
81 } | 60 } |
82 | 61 |
83 @synthesize delegate = _delegate; | |
84 @synthesize maxCacheSize = _maxCacheSize; | 62 @synthesize maxCacheSize = _maxCacheSize; |
85 | 63 |
86 - (instancetype)init { | 64 - (instancetype)init { |
87 NOTREACHED(); // Use initWithCacheSize: instead. | 65 NOTREACHED(); // Use initWithCacheSize: instead. |
88 return nil; | 66 return nil; |
89 } | 67 } |
90 | 68 |
91 - (instancetype)initWithCacheSize:(NSUInteger)maxCacheSize { | 69 - (instancetype)initWithCacheSize:(NSUInteger)maxCacheSize { |
92 self = [super init]; | 70 self = [super init]; |
93 if (self) { | 71 if (self) { |
94 _maxCacheSize = maxCacheSize; | 72 _maxCacheSize = maxCacheSize; |
95 MRUCacheNSObjectDelegate cacheDelegateDeletor(self); | 73 _cache.reset(new NSObjectMRUCache(self.maxCacheSize)); |
96 _cache.reset(new NSObjectMRUCache(self.maxCacheSize, cacheDelegateDeletor)); | |
97 } | 74 } |
98 return self; | 75 return self; |
99 } | 76 } |
100 | 77 |
101 - (id)objectForKey:(id<NSObject>)key { | 78 - (id)objectForKey:(id<NSObject>)key { |
102 base::scoped_nsprotocol<id<NSObject>> keyObj([key retain]); | 79 base::scoped_nsprotocol<id<NSObject>> keyObj([key retain]); |
103 auto it = _cache->Get(keyObj); | 80 auto it = _cache->Get(keyObj); |
104 if (it == _cache->end()) | 81 if (it == _cache->end()) |
105 return nil; | 82 return nil; |
106 return it->second.get(); | 83 return it->second.get(); |
(...skipping 17 matching lines...) Expand all Loading... |
124 } | 101 } |
125 | 102 |
126 - (NSUInteger)count { | 103 - (NSUInteger)count { |
127 return _cache->size(); | 104 return _cache->size(); |
128 } | 105 } |
129 | 106 |
130 - (BOOL)isEmpty { | 107 - (BOOL)isEmpty { |
131 return _cache->empty(); | 108 return _cache->empty(); |
132 } | 109 } |
133 | 110 |
134 #pragma mark - Private | |
135 | |
136 - (void)lruCacheWillEvictObject:(id<NSObject>)obj { | |
137 [self.delegate lruCacheWillEvictObject:obj]; | |
138 } | |
139 | |
140 @end | 111 @end |
OLD | NEW |