1 // Copyright 2012 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 #ifndef IOS_CHROME_BROWSER_SNAPSHOTS_SNAPSHOT_CACHE_H_ 6 #define IOS_CHROME_BROWSER_SNAPSHOTS_SNAPSHOT_CACHE_H_ 7 8 #import <UIKit/UIKit.h> 9 10 namespace base { 11 class FilePath; 12 class Time; 13 } 14 15 @protocol SnapshotCacheObserver; 16 17 // A class providing an in-memory and on-disk cache of tab snapshots. 18 // A snapshot is a full-screen image of the contents of the page at the current 19 // scroll offset and zoom level, used to stand in for the WKWebView if it has 20 // been purged from memory or when quickly switching tabs. 21 // Persists to disk on a background thread each time a snapshot changes. 22 @interface SnapshotCache : NSObject 23 24 // Track snapshot IDs to not release on low memory and to reload on 25 // |UIApplicationDidBecomeActiveNotification|. 26 @property(nonatomic, strong) NSSet* pinnedIDs; 27 28 // Designated initializer. |storagePath| is the file path where all images 29 // managed by this SnapshotCache is stored. |storagePath| is not guaranteed to 30 // exist. The contents of |storagePath| are entirely managed by this 31 // SnapshotCache. 32 - (instancetype)initWithStoragePath:(const base::FilePath&)storagePath 33 NS_DESIGNATED_INITIALIZER; 34 - (instancetype)init NS_UNAVAILABLE; 35 36 // The scale that should be used for snapshots. 37 - (CGFloat)snapshotScaleForDevice; 38 39 // Retrieve a cached snapshot for the |snapshotID| and return it via the 40 // callback if it exists. The callback is guaranteed to be called synchronously 41 // if the image is in memory. It will be called asynchronously if the image is 42 // on disk or with nil if the image is not present at all. 43 - (void)retrieveImageForSnapshotID:(NSString*)snapshotID 44 callback:(void (^)(UIImage*))callback; 45 46 // Request the grey snapshot for |snapshotID|. If the image is already loaded in 47 // memory, this will immediately call back on |callback|. 48 - (void)retrieveGreyImageForSnapshotID:(NSString*)snapshotID 49 callback:(void (^)(UIImage*))callback; 50 51 - (void)setImage:(UIImage*)image withSnapshotID:(NSString*)snapshotID; 52 53 // Removes the image from both the LRU and disk. 54 - (void)removeImageWithSnapshotID:(NSString*)snapshotID; 55 56 // Removes all images from the LRU and disk. 57 - (void)removeAllImages; 58 59 // Moves all images for |snapshotIDs| from |sourcePath| to the current storage 60 // path of this snapshot cache. Deletes the folder |sourcePath| after migration, 61 // regardless of remaining files (which may be obsolete snapshots). 62 - (void)migrateSnapshotsWithIDs:(NSSet<NSString*>*)snapshotIDs 63 fromSourcePath:(const base::FilePath&)sourcePath; 64 65 // Purge the cache of snapshots that are older than |date|. The snapshots for 66 // |liveSnapshotIDs| will be kept. This will be done asynchronously on a 67 // background thread. 68 - (void)purgeCacheOlderThan:(const base::Time&)date 69 keeping:(NSSet*)liveSnapshotIDs; 70 71 // Hint that the snapshot for |snapshotID| will likely be saved to disk when the 72 // application is backgrounded. The snapshot is then saved in memory, so it 73 // does not need to be read off disk. 74 - (void)willBeSavedGreyWhenBackgrounding:(NSString*)snapshotID; 75 76 // Create temporary cache of grey images for tablet side swipe. 77 - (void)createGreyCache:(NSArray*)snapshotIDs; 78 // Release all images in grey cache. 79 - (void)removeGreyCache; 80 // Request the grey snapshot for |snapshotID|. If the image is already loaded 81 // this will immediately call back on |callback|. Otherwise, only use |callback| 82 // for the most recent caller. The callback is not guaranteed to be called. 83 - (void)greyImageForSnapshotID:(NSString*)snapshotID 84 callback:(void (^)(UIImage*))callback; 85 86 // Write a grey copy of the snapshot for |snapshotID| to disk, but if and only 87 // if a color version of the snapshot already exists in memory or on disk. 88 - (void)saveGreyInBackgroundForSnapshotID:(NSString*)snapshotID; 89 90 // Adds an observer to this snapshot cache. 91 - (void)addObserver:(id<SnapshotCacheObserver>)observer; 92 93 // Removes an observer from this snapshot cache. 94 - (void)removeObserver:(id<SnapshotCacheObserver>)observer; 95 96 // Invoked before the instance is deallocated. Needs to release all reference 97 // to C++ objects. Object will soon be deallocated. 98 - (void)shutdown; 99 100 @end 101 102 // Additionnal methods that should only be used for tests. 103 @interface SnapshotCache (TestingAdditions) 104 - (BOOL)hasImageInMemory:(NSString*)snapshotID; 105 - (BOOL)hasGreyImageInMemory:(NSString*)snapshotID; 106 - (NSUInteger)lruCacheMaxSize; 107 @end 108 109 #endif // IOS_CHROME_BROWSER_SNAPSHOTS_SNAPSHOT_CACHE_H_ 110