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