Commit b73e6271 authored by edchin's avatar edchin Committed by Commit Bot

[ios] Rename sessionID to snapshotID in //ios/c/b/snapshots

It is time to remove the use of the term "session" in
//ios/chrome/browser/snapshots.

The identifier used to store and retrieve images from SnapshotCache
is called |snapshotID|. The SnapshotCache has no concept of a Tab
or WebState. It is merely a collection of images with identifiers.

However, SnapshotTabHelper and SnapshotGenerator are tied to a
WebState. Within those classes, the unique identifier for a WebState
is called |tabID|. When those classes call SnapshotCache, they use
the WebState's |tabID| as the identifier to be used in SnapshotCache.

Change-Id: Ic08cd0d70b66e4d699bf06c8b5d8afc093e90255
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2354807
Commit-Queue: edchin <edchin@chromium.org>
Reviewed-by: default avatarMohammad Refaat <mrefaat@chromium.org>
Reviewed-by: default avatarMark Cogan <marq@chromium.org>
Cr-Commit-Position: refs/heads/master@{#799782}
parent 2f017bdc
...@@ -23,36 +23,36 @@ ...@@ -23,36 +23,36 @@
// only a singleton instance. // only a singleton instance.
@property(nonatomic, copy) NSString* uniqueIdentifier; @property(nonatomic, copy) NSString* uniqueIdentifier;
// Track session IDs to not release on low memory and to reload on // Track snapshot IDs to not release on low memory and to reload on
// |UIApplicationDidBecomeActiveNotification|. // |UIApplicationDidBecomeActiveNotification|.
@property(nonatomic, strong) NSSet* pinnedIDs; @property(nonatomic, strong) NSSet* pinnedIDs;
// The scale that should be used for snapshots. // The scale that should be used for snapshots.
- (CGFloat)snapshotScaleForDevice; - (CGFloat)snapshotScaleForDevice;
// Retrieve a cached snapshot for the |sessionID| and return it via the callback // Retrieve a cached snapshot for the |snapshotID| and return it via the
// if it exists. The callback is guaranteed to be called synchronously if the // callback if it exists. The callback is guaranteed to be called synchronously
// image is in memory. It will be called asynchronously if the image is on disk // if the image is in memory. It will be called asynchronously if the image is
// or with nil if the image is not present at all. // on disk or with nil if the image is not present at all.
- (void)retrieveImageForSessionID:(NSString*)sessionID - (void)retrieveImageForSnapshotID:(NSString*)snapshotID
callback:(void (^)(UIImage*))callback; callback:(void (^)(UIImage*))callback;
// Request the session's grey snapshot. If the image is already loaded in // Request the grey snapshot for |snapshotID|. If the image is already loaded in
// memory, this will immediately call back on |callback|. // memory, this will immediately call back on |callback|.
- (void)retrieveGreyImageForSessionID:(NSString*)sessionID - (void)retrieveGreyImageForSnapshotID:(NSString*)snapshotID
callback:(void (^)(UIImage*))callback; callback:(void (^)(UIImage*))callback;
- (void)setImage:(UIImage*)img withSessionID:(NSString*)sessionID; - (void)setImage:(UIImage*)image withSnapshotID:(NSString*)snapshotID;
// Removes the image from both the LRU and disk cache, unless it is marked for // Removes the image from both the LRU and disk cache, unless it is marked for
// deferred deletion. Images marked for deferred deletion can only be removed by // deferred deletion. Images marked for deferred deletion can only be removed by
// calling |-removeMarkedImages|. // calling |-removeMarkedImages|.
- (void)removeImageWithSessionID:(NSString*)sessionID; - (void)removeImageWithSnapshotID:(NSString*)snapshotID;
// Marks an image for deferred deletion. The image will not be immediately // Marks an image for deferred deletion. The image will not be immediately
// deleted when |-removeImageWithSessionID:| is called. Images marked for // deleted when |-removeImageWithSnapshotID:| is called. Images marked for
// deferred deletion can only be removed by calling |-removeMarkedImages|. // deferred deletion can only be removed by calling |-removeMarkedImages|.
- (void)markImageWithSessionID:(NSString*)sessionID; - (void)markImageWithSnapshotID:(NSString*)snapshotID;
// Removes all marked images from both the LRU and disk cache. // Removes all marked images from both the LRU and disk cache.
- (void)removeMarkedImages; - (void)removeMarkedImages;
...@@ -62,28 +62,28 @@ ...@@ -62,28 +62,28 @@
- (void)unmarkAllImages; - (void)unmarkAllImages;
// Purge the cache of snapshots that are older than |date|. The snapshots for // Purge the cache of snapshots that are older than |date|. The snapshots for
// the sessions given in |liveSessionIds| will be kept. This will be done // |liveSnapshotIDs| will be kept. This will be done asynchronously on a
// asynchronously on a background thread. // background thread.
- (void)purgeCacheOlderThan:(const base::Time&)date - (void)purgeCacheOlderThan:(const base::Time&)date
keeping:(NSSet*)liveSessionIds; keeping:(NSSet*)liveSnapshotIDs;
// Hint that the snapshot for |sessionID| will likely be saved to disk when the // Hint that the snapshot for |snapshotID| will likely be saved to disk when the
// application is backgrounded. The snapshot is then saved in memory, so it // application is backgrounded. The snapshot is then saved in memory, so it
// does not need to be read off disk. // does not need to be read off disk.
- (void)willBeSavedGreyWhenBackgrounding:(NSString*)sessionID; - (void)willBeSavedGreyWhenBackgrounding:(NSString*)snapshotID;
// Create temporary cache of grey images for tablet side swipe. // Create temporary cache of grey images for tablet side swipe.
- (void)createGreyCache:(NSArray*)sessionIDs; - (void)createGreyCache:(NSArray*)snapshotIDs;
// Release all images in grey cache. // Release all images in grey cache.
- (void)removeGreyCache; - (void)removeGreyCache;
// Request the session's grey snapshot. If the image is already loaded this will // Request the grey snapshot for |snapshotID|. If the image is already loaded
// immediately call back on |callback|. Otherwise, only use |callback| for the // this will immediately call back on |callback|. Otherwise, only use |callback|
// most recent caller. The callback is not guaranteed to be called. // for the most recent caller. The callback is not guaranteed to be called.
- (void)greyImageForSessionID:(NSString*)sessionID - (void)greyImageForSnapshotID:(NSString*)snapshotID
callback:(void (^)(UIImage*))callback; callback:(void (^)(UIImage*))callback;
// Write a grey copy of the snapshot for |sessionID| to disk, but if and only if // Write a grey copy of the snapshot for |snapshotID| to disk, but if and only
// a color version of the snapshot already exists in memory or on disk. // if a color version of the snapshot already exists in memory or on disk.
- (void)saveGreyInBackgroundForSessionID:(NSString*)sessionID; - (void)saveGreyInBackgroundForSnapshotID:(NSString*)snapshotID;
// Adds an observer to this snapshot cache. // Adds an observer to this snapshot cache.
- (void)addObserver:(id<SnapshotCacheObserver>)observer; - (void)addObserver:(id<SnapshotCacheObserver>)observer;
...@@ -99,8 +99,8 @@ ...@@ -99,8 +99,8 @@
// Additionnal methods that should only be used for tests. // Additionnal methods that should only be used for tests.
@interface SnapshotCache (TestingAdditions) @interface SnapshotCache (TestingAdditions)
- (BOOL)hasImageInMemory:(NSString*)sessionID; - (BOOL)hasImageInMemory:(NSString*)snapshotID;
- (BOOL)hasGreyImageInMemory:(NSString*)sessionID; - (BOOL)hasGreyImageInMemory:(NSString*)snapshotID;
- (NSUInteger)lruCacheMaxSize; - (NSUInteger)lruCacheMaxSize;
@end @end
......
...@@ -48,21 +48,7 @@ ...@@ -48,21 +48,7 @@
// List of observers to be notified of changes to the snapshot cache. // List of observers to be notified of changes to the snapshot cache.
@property(nonatomic, strong) SnapshotCacheObservers* observers; @property(nonatomic, strong) SnapshotCacheObservers* observers;
// Marked set of identifiers for which images should not be immediately deleted. // Marked set of identifiers for which images should not be immediately deleted.
@property(nonatomic, strong) NSMutableSet* markedIDs; @property(nonatomic, strong) NSMutableSet<NSString*>* markedIDs;
// Remove all UIImages from |lruCache_|.
- (void)handleEnterBackground;
// Remove all but adjacent UIImages from |lruCache_|.
- (void)handleLowMemory;
// Restore adjacent UIImages to |lruCache_|.
- (void)handleBecomeActive;
// Clear most recent caller information.
- (void)clearGreySessionInfo;
// Load uncached snapshot image and convert image to grey.
- (void)loadGreyImageAsync:(NSString*)sessionID;
// Save grey image to |greyImageDictionary_| and call into most recent
// |mostRecentGreyBlock_| if |mostRecentGreySessionId_| matches |sessionID|.
- (void)saveGreyImage:(UIImage*)greyImage forKey:(NSString*)sessionID;
@end @end
namespace { namespace {
...@@ -100,13 +86,13 @@ bool GetSnapshotsCacheDirectory(base::FilePath* snapshots_cache_directory) { ...@@ -100,13 +86,13 @@ bool GetSnapshotsCacheDirectory(base::FilePath* snapshots_cache_directory) {
return true; return true;
} }
// Returns the path of the image for |session_id|, in |cache_directory|, // Returns the path of the image for |snapshot_id|, in |cache_directory|,
// of type |image_type| and scale |image_scale|. // of type |image_type| and scale |image_scale|.
base::FilePath ImagePath(NSString* session_id, base::FilePath ImagePath(NSString* snapshot_id,
ImageType image_type, ImageType image_type,
ImageScale image_scale, ImageScale image_scale,
const base::FilePath& cache_directory) { const base::FilePath& cache_directory) {
NSString* filename = session_id; NSString* filename = snapshot_id;
switch (image_type) { switch (image_type) {
case IMAGE_TYPE_COLOR: case IMAGE_TYPE_COLOR:
// no-op // no-op
...@@ -153,7 +139,7 @@ CGFloat ScaleFromImageScale(ImageScale image_scale) { ...@@ -153,7 +139,7 @@ CGFloat ScaleFromImageScale(ImageScale image_scale) {
} }
} }
UIImage* ReadImageForSessionFromDisk(NSString* session_id, UIImage* ReadImageForSnapshotIDFromDisk(NSString* snapshot_id,
ImageType image_type, ImageType image_type,
ImageScale image_scale, ImageScale image_scale,
const base::FilePath& cache_directory) { const base::FilePath& cache_directory) {
...@@ -162,7 +148,7 @@ UIImage* ReadImageForSessionFromDisk(NSString* session_id, ...@@ -162,7 +148,7 @@ UIImage* ReadImageForSessionFromDisk(NSString* session_id,
// reporting the image as damaged https://stackoverflow.com/q/5081297/5353 // reporting the image as damaged https://stackoverflow.com/q/5081297/5353
// are fixed. // are fixed.
base::FilePath file_path = base::FilePath file_path =
ImagePath(session_id, image_type, image_scale, cache_directory); ImagePath(snapshot_id, image_type, image_scale, cache_directory);
NSString* path = base::SysUTF8ToNSString(file_path.AsUTF8Unsafe()); NSString* path = base::SysUTF8ToNSString(file_path.AsUTF8Unsafe());
base::ScopedBlockingCall scoped_blocking_call(FROM_HERE, base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
base::BlockingType::WILL_BLOCK); base::BlockingType::WILL_BLOCK);
...@@ -207,20 +193,20 @@ void WriteImageToDisk(UIImage* image, const base::FilePath& file_path) { ...@@ -207,20 +193,20 @@ void WriteImageToDisk(UIImage* image, const base::FilePath& file_path) {
} }
} }
void ConvertAndSaveGreyImage(NSString* session_id, void ConvertAndSaveGreyImage(NSString* snapshot_id,
ImageScale image_scale, ImageScale image_scale,
UIImage* color_image, UIImage* color_image,
const base::FilePath& cache_directory) { const base::FilePath& cache_directory) {
base::ScopedBlockingCall scoped_blocking_call(FROM_HERE, base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
base::BlockingType::WILL_BLOCK); base::BlockingType::WILL_BLOCK);
if (!color_image) { if (!color_image) {
color_image = ReadImageForSessionFromDisk(session_id, IMAGE_TYPE_COLOR, color_image = ReadImageForSnapshotIDFromDisk(snapshot_id, IMAGE_TYPE_COLOR,
image_scale, cache_directory); image_scale, cache_directory);
if (!color_image) if (!color_image)
return; return;
} }
UIImage* grey_image = GreyImage(color_image); UIImage* grey_image = GreyImage(color_image);
WriteImageToDisk(grey_image, ImagePath(session_id, IMAGE_TYPE_GREYSCALE, WriteImageToDisk(grey_image, ImagePath(snapshot_id, IMAGE_TYPE_GREYSCALE,
image_scale, cache_directory)); image_scale, cache_directory));
} }
...@@ -236,14 +222,14 @@ void ConvertAndSaveGreyImage(NSString* session_id, ...@@ -236,14 +222,14 @@ void ConvertAndSaveGreyImage(NSString* session_id,
// is called. // is called.
NSMutableDictionary<NSString*, UIImage*>* _greyImageDictionary; NSMutableDictionary<NSString*, UIImage*>* _greyImageDictionary;
// Session ID of most recent pending grey snapshot request. // Snapshot ID of most recent pending grey snapshot request.
NSString* _mostRecentGreySessionId; NSString* _mostRecentGreySnapshotID;
// Block used by pending request for a grey snapshot. // Block used by pending request for a grey snapshot.
void (^_mostRecentGreyBlock)(UIImage*); void (^_mostRecentGreyBlock)(UIImage*);
// Session ID and corresponding UIImage for the snapshot that will likely // Snapshot ID and corresponding UIImage for the snapshot that will likely
// be requested to be saved to disk when the application is backgrounded. // be requested to be saved to disk when the application is backgrounded.
NSString* _backgroundingImageSessionId; NSString* _backgroundingSnapshotID;
UIImage* _backgroundingColorImage; UIImage* _backgroundingColorImage;
// Scale for snapshot images. May be smaller than the screen scale in order // Scale for snapshot images. May be smaller than the screen scale in order
...@@ -341,13 +327,13 @@ void ConvertAndSaveGreyImage(NSString* session_id, ...@@ -341,13 +327,13 @@ void ConvertAndSaveGreyImage(NSString* session_id,
return ScaleFromImageScale(_snapshotsScale); return ScaleFromImageScale(_snapshotsScale);
} }
- (void)retrieveImageForSessionID:(NSString*)sessionID - (void)retrieveImageForSnapshotID:(NSString*)snapshotID
callback:(void (^)(UIImage*))callback { callback:(void (^)(UIImage*))callback {
DCHECK_CALLED_ON_VALID_SEQUENCE(_sequenceChecker); DCHECK_CALLED_ON_VALID_SEQUENCE(_sequenceChecker);
DCHECK(sessionID); DCHECK(snapshotID);
DCHECK(callback); DCHECK(callback);
if (UIImage* image = [_lruCache objectForKey:sessionID]) { if (UIImage* image = [_lruCache objectForKey:snapshotID]) {
callback(image); callback(image);
return; return;
} }
...@@ -365,46 +351,46 @@ void ConvertAndSaveGreyImage(NSString* session_id, ...@@ -365,46 +351,46 @@ void ConvertAndSaveGreyImage(NSString* session_id,
base::PostTaskAndReplyWithResult( base::PostTaskAndReplyWithResult(
_taskRunner.get(), FROM_HERE, base::BindOnce(^UIImage*() { _taskRunner.get(), FROM_HERE, base::BindOnce(^UIImage*() {
// Retrieve the image on a high priority thread. // Retrieve the image on a high priority thread.
return ReadImageForSessionFromDisk(sessionID, IMAGE_TYPE_COLOR, return ReadImageForSnapshotIDFromDisk(snapshotID, IMAGE_TYPE_COLOR,
snapshotsScale, cacheDirectory); snapshotsScale, cacheDirectory);
}), }),
base::BindOnce(^(UIImage* image) { base::BindOnce(^(UIImage* image) {
if (image) if (image)
[weakLRUCache setObject:image forKey:sessionID]; [weakLRUCache setObject:image forKey:snapshotID];
callback(image); callback(image);
})); }));
} }
- (void)setImage:(UIImage*)image withSessionID:(NSString*)sessionID { - (void)setImage:(UIImage*)image withSnapshotID:(NSString*)snapshotID {
DCHECK_CALLED_ON_VALID_SEQUENCE(_sequenceChecker); DCHECK_CALLED_ON_VALID_SEQUENCE(_sequenceChecker);
if (!image || !sessionID || !_taskRunner) if (!image || !snapshotID || !_taskRunner)
return; return;
[_lruCache setObject:image forKey:sessionID]; [_lruCache setObject:image forKey:snapshotID];
[self.observers snapshotCache:self didUpdateSnapshotForIdentifier:sessionID]; [self.observers snapshotCache:self didUpdateSnapshotForIdentifier:snapshotID];
// Copy ivars used by the block so that it does not reference |self|. // Copy ivars used by the block so that it does not reference |self|.
const base::FilePath cacheDirectory = _cacheDirectory; const base::FilePath cacheDirectory = _cacheDirectory;
const ImageScale snapshotsScale = _snapshotsScale; const ImageScale snapshotsScale = _snapshotsScale;
// Save the image to disk. // Save the image to disk.
_taskRunner->PostTask( _taskRunner->PostTask(FROM_HERE, base::BindOnce(^{
FROM_HERE, base::BindOnce(^{ WriteImageToDisk(
WriteImageToDisk(image, ImagePath(sessionID, IMAGE_TYPE_COLOR, image, ImagePath(snapshotID, IMAGE_TYPE_COLOR,
snapshotsScale, cacheDirectory)); snapshotsScale, cacheDirectory));
})); }));
} }
- (void)removeImageWithSessionID:(NSString*)sessionID { - (void)removeImageWithSnapshotID:(NSString*)snapshotID {
DCHECK_CALLED_ON_VALID_SEQUENCE(_sequenceChecker); DCHECK_CALLED_ON_VALID_SEQUENCE(_sequenceChecker);
// Do not immediately delete if the ID is marked. // Do not immediately delete if the ID is marked.
if ([self.markedIDs containsObject:sessionID]) if ([self.markedIDs containsObject:snapshotID])
return; return;
[_lruCache removeObjectForKey:sessionID]; [_lruCache removeObjectForKey:snapshotID];
[self.observers snapshotCache:self didUpdateSnapshotForIdentifier:sessionID]; [self.observers snapshotCache:self didUpdateSnapshotForIdentifier:snapshotID];
if (!_taskRunner) if (!_taskRunner)
return; return;
...@@ -416,21 +402,21 @@ void ConvertAndSaveGreyImage(NSString* session_id, ...@@ -416,21 +402,21 @@ void ConvertAndSaveGreyImage(NSString* session_id,
_taskRunner->PostTask( _taskRunner->PostTask(
FROM_HERE, base::BindOnce(^{ FROM_HERE, base::BindOnce(^{
for (size_t index = 0; index < base::size(kImageTypes); ++index) { for (size_t index = 0; index < base::size(kImageTypes); ++index) {
base::DeleteFile(ImagePath(sessionID, kImageTypes[index], base::DeleteFile(ImagePath(snapshotID, kImageTypes[index],
snapshotsScale, cacheDirectory)); snapshotsScale, cacheDirectory));
} }
})); }));
} }
- (void)markImageWithSessionID:(NSString*)sessionID { - (void)markImageWithSnapshotID:(NSString*)snapshotID {
[self.markedIDs addObject:sessionID]; [self.markedIDs addObject:snapshotID];
} }
- (void)removeMarkedImages { - (void)removeMarkedImages {
while (self.markedIDs.count > 0) { while (self.markedIDs.count > 0) {
NSString* sessionID = [self.markedIDs anyObject]; NSString* snapshotID = [self.markedIDs anyObject];
[self.markedIDs removeObject:sessionID]; [self.markedIDs removeObject:snapshotID];
[self removeImageWithSessionID:sessionID]; [self removeImageWithSnapshotID:snapshotID];
} }
} }
...@@ -438,18 +424,18 @@ void ConvertAndSaveGreyImage(NSString* session_id, ...@@ -438,18 +424,18 @@ void ConvertAndSaveGreyImage(NSString* session_id,
[self.markedIDs removeAllObjects]; [self.markedIDs removeAllObjects];
} }
- (base::FilePath)imagePathForSessionID:(NSString*)sessionID { - (base::FilePath)imagePathForSnapshotID:(NSString*)snapshotID {
return ImagePath(sessionID, IMAGE_TYPE_COLOR, _snapshotsScale, return ImagePath(snapshotID, IMAGE_TYPE_COLOR, _snapshotsScale,
_cacheDirectory); _cacheDirectory);
} }
- (base::FilePath)greyImagePathForSessionID:(NSString*)sessionID { - (base::FilePath)greyImagePathForSnapshotID:(NSString*)snapshotID {
return ImagePath(sessionID, IMAGE_TYPE_GREYSCALE, _snapshotsScale, return ImagePath(snapshotID, IMAGE_TYPE_GREYSCALE, _snapshotsScale,
_cacheDirectory); _cacheDirectory);
} }
- (void)purgeCacheOlderThan:(const base::Time&)date - (void)purgeCacheOlderThan:(const base::Time&)date
keeping:(NSSet*)liveSessionIds { keeping:(NSSet*)liveSnapshotIDs {
DCHECK_CALLED_ON_VALID_SEQUENCE(_sequenceChecker); DCHECK_CALLED_ON_VALID_SEQUENCE(_sequenceChecker);
if (!_taskRunner) if (!_taskRunner)
...@@ -468,9 +454,9 @@ void ConvertAndSaveGreyImage(NSString* session_id, ...@@ -468,9 +454,9 @@ void ConvertAndSaveGreyImage(NSString* session_id,
return; return;
std::set<base::FilePath> filesToKeep; std::set<base::FilePath> filesToKeep;
for (NSString* sessionID : liveSessionIds) { for (NSString* snapshotID : liveSnapshotIDs) {
for (size_t index = 0; index < base::size(kImageTypes); ++index) { for (size_t index = 0; index < base::size(kImageTypes); ++index) {
filesToKeep.insert(ImagePath(sessionID, kImageTypes[index], filesToKeep.insert(ImagePath(snapshotID, kImageTypes[index],
snapshotsScale, cacheDirectory)); snapshotsScale, cacheDirectory));
} }
} }
...@@ -490,57 +476,64 @@ void ConvertAndSaveGreyImage(NSString* session_id, ...@@ -490,57 +476,64 @@ void ConvertAndSaveGreyImage(NSString* session_id,
})); }));
} }
- (void)willBeSavedGreyWhenBackgrounding:(NSString*)sessionID { - (void)willBeSavedGreyWhenBackgrounding:(NSString*)snapshotID {
DCHECK_CALLED_ON_VALID_SEQUENCE(_sequenceChecker); DCHECK_CALLED_ON_VALID_SEQUENCE(_sequenceChecker);
if (!sessionID) if (!snapshotID)
return; return;
_backgroundingImageSessionId = [sessionID copy]; _backgroundingSnapshotID = [snapshotID copy];
_backgroundingColorImage = [_lruCache objectForKey:sessionID]; _backgroundingColorImage = [_lruCache objectForKey:snapshotID];
} }
// Remove all but adjacent UIImages from |lruCache_|.
- (void)handleLowMemory { - (void)handleLowMemory {
DCHECK_CALLED_ON_VALID_SEQUENCE(_sequenceChecker); DCHECK_CALLED_ON_VALID_SEQUENCE(_sequenceChecker);
NSMutableDictionary<NSString*, UIImage*>* dictionary = NSMutableDictionary<NSString*, UIImage*>* dictionary =
[NSMutableDictionary dictionaryWithCapacity:2]; [NSMutableDictionary dictionaryWithCapacity:2];
for (NSString* sessionID in self.pinnedIDs) { for (NSString* snapshotID in self.pinnedIDs) {
UIImage* image = [_lruCache objectForKey:sessionID]; UIImage* image = [_lruCache objectForKey:snapshotID];
if (image) if (image)
[dictionary setObject:image forKey:sessionID]; [dictionary setObject:image forKey:snapshotID];
} }
[_lruCache removeAllObjects]; [_lruCache removeAllObjects];
for (NSString* sessionID in self.pinnedIDs) for (NSString* snapshotID in self.pinnedIDs)
[_lruCache setObject:[dictionary objectForKey:sessionID] forKey:sessionID]; [_lruCache setObject:[dictionary objectForKey:snapshotID]
forKey:snapshotID];
} }
// Remove all UIImages from |lruCache_|.
- (void)handleEnterBackground { - (void)handleEnterBackground {
DCHECK_CALLED_ON_VALID_SEQUENCE(_sequenceChecker); DCHECK_CALLED_ON_VALID_SEQUENCE(_sequenceChecker);
[_lruCache removeAllObjects]; [_lruCache removeAllObjects];
} }
// Restore adjacent UIImages to |lruCache_|.
- (void)handleBecomeActive { - (void)handleBecomeActive {
DCHECK_CALLED_ON_VALID_SEQUENCE(_sequenceChecker); DCHECK_CALLED_ON_VALID_SEQUENCE(_sequenceChecker);
for (NSString* sessionID in self.pinnedIDs) for (NSString* snapshotID in self.pinnedIDs)
[self retrieveImageForSessionID:sessionID [self retrieveImageForSnapshotID:snapshotID
callback:^(UIImage*){ callback:^(UIImage*){
}]; }];
} }
- (void)saveGreyImage:(UIImage*)greyImage forKey:(NSString*)sessionID { // Save grey image to |greyImageDictionary_| and call into most recent
// |_mostRecentGreyBlock| if |_mostRecentGreySnapshotID| matches |snapshotID|.
- (void)saveGreyImage:(UIImage*)greyImage forSnapshotID:(NSString*)snapshotID {
DCHECK_CALLED_ON_VALID_SEQUENCE(_sequenceChecker); DCHECK_CALLED_ON_VALID_SEQUENCE(_sequenceChecker);
if (greyImage) if (greyImage)
[_greyImageDictionary setObject:greyImage forKey:sessionID]; [_greyImageDictionary setObject:greyImage forKey:snapshotID];
if ([sessionID isEqualToString:_mostRecentGreySessionId]) { if ([snapshotID isEqualToString:_mostRecentGreySnapshotID]) {
_mostRecentGreyBlock(greyImage); _mostRecentGreyBlock(greyImage);
[self clearGreySessionInfo]; [self clearGreySnapshotInfo];
} }
} }
- (void)loadGreyImageAsync:(NSString*)sessionID { // Load uncached snapshot image and convert image to grey.
- (void)loadGreyImageAsync:(NSString*)snapshotID {
DCHECK_CALLED_ON_VALID_SEQUENCE(_sequenceChecker); DCHECK_CALLED_ON_VALID_SEQUENCE(_sequenceChecker);
// Don't call -retrieveImageForSessionID here because it caches the colored // Don't call -retrieveImageForSnapshotID here because it caches the colored
// image, which we don't need for the grey image cache. But if the image is // image, which we don't need for the grey image cache. But if the image is
// already in the cache, use it. // already in the cache, use it.
UIImage* image = [_lruCache objectForKey:sessionID]; UIImage* image = [_lruCache objectForKey:snapshotID];
if (!_taskRunner) if (!_taskRunner)
return; return;
...@@ -555,62 +548,63 @@ void ConvertAndSaveGreyImage(NSString* session_id, ...@@ -555,62 +548,63 @@ void ConvertAndSaveGreyImage(NSString* session_id,
// If the image is not in the cache, load it from disk. // If the image is not in the cache, load it from disk.
UIImage* localImage = image; UIImage* localImage = image;
if (!localImage) { if (!localImage) {
localImage = ReadImageForSessionFromDisk( localImage = ReadImageForSnapshotIDFromDisk(
sessionID, IMAGE_TYPE_COLOR, snapshotsScale, cacheDirectory); snapshotID, IMAGE_TYPE_COLOR, snapshotsScale, cacheDirectory);
} }
if (localImage) if (localImage)
localImage = GreyImage(localImage); localImage = GreyImage(localImage);
return localImage; return localImage;
}), }),
base::BindOnce(^(UIImage* greyImage) { base::BindOnce(^(UIImage* greyImage) {
[weakSelf saveGreyImage:greyImage forKey:sessionID]; [weakSelf saveGreyImage:greyImage forSnapshotID:snapshotID];
})); }));
} }
- (void)createGreyCache:(NSArray*)sessionIDs { - (void)createGreyCache:(NSArray*)snapshotIDs {
DCHECK_CALLED_ON_VALID_SEQUENCE(_sequenceChecker); DCHECK_CALLED_ON_VALID_SEQUENCE(_sequenceChecker);
_greyImageDictionary = _greyImageDictionary =
[NSMutableDictionary dictionaryWithCapacity:kGreyInitialCapacity]; [NSMutableDictionary dictionaryWithCapacity:kGreyInitialCapacity];
for (NSString* sessionID in sessionIDs) for (NSString* snapshotID in snapshotIDs)
[self loadGreyImageAsync:sessionID]; [self loadGreyImageAsync:snapshotID];
} }
- (void)removeGreyCache { - (void)removeGreyCache {
DCHECK_CALLED_ON_VALID_SEQUENCE(_sequenceChecker); DCHECK_CALLED_ON_VALID_SEQUENCE(_sequenceChecker);
_greyImageDictionary = nil; _greyImageDictionary = nil;
[self clearGreySessionInfo]; [self clearGreySnapshotInfo];
} }
- (void)clearGreySessionInfo { // Clear most recent caller information.
- (void)clearGreySnapshotInfo {
DCHECK_CALLED_ON_VALID_SEQUENCE(_sequenceChecker); DCHECK_CALLED_ON_VALID_SEQUENCE(_sequenceChecker);
_mostRecentGreySessionId = nil; _mostRecentGreySnapshotID = nil;
_mostRecentGreyBlock = nil; _mostRecentGreyBlock = nil;
} }
- (void)greyImageForSessionID:(NSString*)sessionID - (void)greyImageForSnapshotID:(NSString*)snapshotID
callback:(void (^)(UIImage*))callback { callback:(void (^)(UIImage*))callback {
DCHECK_CALLED_ON_VALID_SEQUENCE(_sequenceChecker); DCHECK_CALLED_ON_VALID_SEQUENCE(_sequenceChecker);
DCHECK(_greyImageDictionary); DCHECK(_greyImageDictionary);
DCHECK(sessionID); DCHECK(snapshotID);
DCHECK(callback); DCHECK(callback);
if (UIImage* image = [_greyImageDictionary objectForKey:sessionID]) { if (UIImage* image = [_greyImageDictionary objectForKey:snapshotID]) {
callback(image); callback(image);
[self clearGreySessionInfo]; [self clearGreySnapshotInfo];
} else { } else {
_mostRecentGreySessionId = [sessionID copy]; _mostRecentGreySnapshotID = [snapshotID copy];
_mostRecentGreyBlock = [callback copy]; _mostRecentGreyBlock = [callback copy];
} }
} }
- (void)retrieveGreyImageForSessionID:(NSString*)sessionID - (void)retrieveGreyImageForSnapshotID:(NSString*)snapshotID
callback:(void (^)(UIImage*))callback { callback:(void (^)(UIImage*))callback {
DCHECK_CALLED_ON_VALID_SEQUENCE(_sequenceChecker); DCHECK_CALLED_ON_VALID_SEQUENCE(_sequenceChecker);
DCHECK(sessionID); DCHECK(snapshotID);
DCHECK(callback); DCHECK(callback);
if (_greyImageDictionary) { if (_greyImageDictionary) {
if (UIImage* image = [_greyImageDictionary objectForKey:sessionID]) { if (UIImage* image = [_greyImageDictionary objectForKey:snapshotID]) {
callback(image); callback(image);
return; return;
} }
...@@ -629,7 +623,7 @@ void ConvertAndSaveGreyImage(NSString* session_id, ...@@ -629,7 +623,7 @@ void ConvertAndSaveGreyImage(NSString* session_id,
base::PostTaskAndReplyWithResult( base::PostTaskAndReplyWithResult(
_taskRunner.get(), FROM_HERE, base::BindOnce(^UIImage*() { _taskRunner.get(), FROM_HERE, base::BindOnce(^UIImage*() {
// Retrieve the image on a high priority thread. // Retrieve the image on a high priority thread.
return ReadImageForSessionFromDisk(sessionID, IMAGE_TYPE_GREYSCALE, return ReadImageForSnapshotIDFromDisk(snapshotID, IMAGE_TYPE_GREYSCALE,
snapshotsScale, cacheDirectory); snapshotsScale, cacheDirectory);
}), }),
base::BindOnce(^(UIImage* image) { base::BindOnce(^(UIImage* image) {
...@@ -637,7 +631,7 @@ void ConvertAndSaveGreyImage(NSString* session_id, ...@@ -637,7 +631,7 @@ void ConvertAndSaveGreyImage(NSString* session_id,
callback(image); callback(image);
return; return;
} }
[weakSelf retrieveImageForSessionID:sessionID [weakSelf retrieveImageForSnapshotID:snapshotID
callback:^(UIImage* localImage) { callback:^(UIImage* localImage) {
if (localImage) if (localImage)
localImage = GreyImage(localImage); localImage = GreyImage(localImage);
...@@ -646,15 +640,15 @@ void ConvertAndSaveGreyImage(NSString* session_id, ...@@ -646,15 +640,15 @@ void ConvertAndSaveGreyImage(NSString* session_id,
})); }));
} }
- (void)saveGreyInBackgroundForSessionID:(NSString*)sessionID { - (void)saveGreyInBackgroundForSnapshotID:(NSString*)snapshotID {
DCHECK_CALLED_ON_VALID_SEQUENCE(_sequenceChecker); DCHECK_CALLED_ON_VALID_SEQUENCE(_sequenceChecker);
if (!sessionID) if (!snapshotID)
return; return;
// The color image may still be in memory. Verify the sessionID matches. // The color image may still be in memory. Verify the snapshotID matches.
if (_backgroundingColorImage) { if (_backgroundingColorImage) {
if (![_backgroundingImageSessionId isEqualToString:sessionID]) { if (![_backgroundingSnapshotID isEqualToString:snapshotID]) {
_backgroundingImageSessionId = nil; _backgroundingSnapshotID = nil;
_backgroundingColorImage = nil; _backgroundingColorImage = nil;
} }
} }
...@@ -667,10 +661,10 @@ void ConvertAndSaveGreyImage(NSString* session_id, ...@@ -667,10 +661,10 @@ void ConvertAndSaveGreyImage(NSString* session_id,
const base::FilePath cacheDirectory = _cacheDirectory; const base::FilePath cacheDirectory = _cacheDirectory;
const ImageScale snapshotsScale = _snapshotsScale; const ImageScale snapshotsScale = _snapshotsScale;
_taskRunner->PostTask( _taskRunner->PostTask(FROM_HERE, base::BindOnce(^{
FROM_HERE, base::BindOnce(^{ ConvertAndSaveGreyImage(snapshotID, snapshotsScale,
ConvertAndSaveGreyImage(sessionID, snapshotsScale, backgroundingColorImage,
backgroundingColorImage, cacheDirectory); cacheDirectory);
})); }));
} }
...@@ -690,12 +684,12 @@ void ConvertAndSaveGreyImage(NSString* session_id, ...@@ -690,12 +684,12 @@ void ConvertAndSaveGreyImage(NSString* session_id,
@implementation SnapshotCache (TestingAdditions) @implementation SnapshotCache (TestingAdditions)
- (BOOL)hasImageInMemory:(NSString*)sessionID { - (BOOL)hasImageInMemory:(NSString*)snapshotID {
return [_lruCache objectForKey:sessionID] != nil; return [_lruCache objectForKey:snapshotID] != nil;
} }
- (BOOL)hasGreyImageInMemory:(NSString*)sessionID { - (BOOL)hasGreyImageInMemory:(NSString*)snapshotID {
return [_greyImageDictionary objectForKey:sessionID] != nil; return [_greyImageDictionary objectForKey:snapshotID] != nil;
} }
- (NSUInteger)lruCacheMaxSize { - (NSUInteger)lruCacheMaxSize {
......
...@@ -14,10 +14,10 @@ class FilePath; ...@@ -14,10 +14,10 @@ class FilePath;
@class NSString; @class NSString;
@interface SnapshotCache (Internal) @interface SnapshotCache (Internal)
// Returns filepath to the color snapshot of |sessionID|. // Returns filepath to the color snapshot of |snapshotID|.
- (base::FilePath)imagePathForSessionID:(NSString*)sessionID; - (base::FilePath)imagePathForSnapshotID:(NSString*)snapshotID;
// Returns filepath to the greyscale snapshot of |sessionID|. // Returns filepath to the greyscale snapshot of |snapshotID|.
- (base::FilePath)greyImagePathForSessionID:(NSString*)sessionID; - (base::FilePath)greyImagePathForSnapshotID:(NSString*)snapshotID;
@end @end
#endif // IOS_CHROME_BROWSER_SNAPSHOTS_SNAPSHOT_CACHE_INTERNAL_H_ #endif // IOS_CHROME_BROWSER_SNAPSHOTS_SNAPSHOT_CACHE_INTERNAL_H_
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
#error "This file requires ARC support." #error "This file requires ARC support."
#endif #endif
static const NSUInteger kSessionCount = 10; static const NSUInteger kSnapshotCount = 10;
static const NSUInteger kSnapshotPixelSize = 8; static const NSUInteger kSnapshotPixelSize = 8;
@interface FakeSnapshotCacheObserver : NSObject<SnapshotCacheObserver> @interface FakeSnapshotCacheObserver : NSObject<SnapshotCacheObserver>
...@@ -46,13 +46,13 @@ namespace { ...@@ -46,13 +46,13 @@ namespace {
class SnapshotCacheTest : public PlatformTest { class SnapshotCacheTest : public PlatformTest {
protected: protected:
// Build an array of session names and an array of UIImages filled with // Build an array of snapshot IDs and an array of UIImages filled with
// random colors. // random colors.
void SetUp() override { void SetUp() override {
PlatformTest::SetUp(); PlatformTest::SetUp();
snapshotCache_ = [[SnapshotCache alloc] init]; snapshotCache_ = [[SnapshotCache alloc] init];
testImages_ = [[NSMutableArray alloc] initWithCapacity:kSessionCount]; testImages_ = [[NSMutableArray alloc] initWithCapacity:kSnapshotCount];
testSessions_ = [[NSMutableArray alloc] initWithCapacity:kSessionCount]; snapshotIDs_ = [[NSMutableArray alloc] initWithCapacity:kSnapshotCount];
CGFloat scale = [snapshotCache_ snapshotScaleForDevice]; CGFloat scale = [snapshotCache_ snapshotScaleForDevice];
UIGraphicsBeginImageContextWithOptions( UIGraphicsBeginImageContextWithOptions(
...@@ -60,11 +60,11 @@ class SnapshotCacheTest : public PlatformTest { ...@@ -60,11 +60,11 @@ class SnapshotCacheTest : public PlatformTest {
CGContextRef context = UIGraphicsGetCurrentContext(); CGContextRef context = UIGraphicsGetCurrentContext();
srand(1); srand(1);
for (NSUInteger i = 0; i < kSessionCount; ++i) { for (NSUInteger i = 0; i < kSnapshotCount; ++i) {
UIImage* image = GenerateRandomImage(context); UIImage* image = GenerateRandomImage(context);
[testImages_ addObject:image]; [testImages_ addObject:image];
[testSessions_ [snapshotIDs_
addObject:[NSString stringWithFormat:@"SessionId-%" PRIuNS, i]]; addObject:[NSString stringWithFormat:@"SnapshotID-%" PRIuNS, i]];
} }
UIGraphicsEndImageContext(); UIGraphicsEndImageContext();
...@@ -113,26 +113,26 @@ class SnapshotCacheTest : public PlatformTest { ...@@ -113,26 +113,26 @@ class SnapshotCacheTest : public PlatformTest {
void ClearDumpedImages() { void ClearDumpedImages() {
SnapshotCache* cache = GetSnapshotCache(); SnapshotCache* cache = GetSnapshotCache();
NSString* sessionID; NSString* snapshotID;
for (sessionID in testSessions_) for (snapshotID in snapshotIDs_)
[cache removeImageWithSessionID:sessionID]; [cache removeImageWithSnapshotID:snapshotID];
FlushRunLoops(); FlushRunLoops();
// The above calls to -removeImageWithSessionID remove both the color // The above calls to -removeImageWithSnapshotID remove both the color
// and grey snapshots for each sessionID, if they are on disk. However, // and grey snapshots for each snapshotID, if they are on disk. However,
// ensure we also get rid of the grey snapshots in memory. // ensure we also get rid of the grey snapshots in memory.
[cache removeGreyCache]; [cache removeGreyCache];
__block BOOL foundImage = NO; __block BOOL foundImage = NO;
__block NSUInteger numCallbacks = 0; __block NSUInteger numCallbacks = 0;
for (sessionID in testSessions_) { for (snapshotID in snapshotIDs_) {
base::FilePath path([cache imagePathForSessionID:sessionID]); base::FilePath path([cache imagePathForSnapshotID:snapshotID]);
// Checks that the snapshot is not on disk. // Checks that the snapshot is not on disk.
EXPECT_FALSE(base::PathExists(path)); EXPECT_FALSE(base::PathExists(path));
// Check that the snapshot is not in the dictionary. // Check that the snapshot is not in the dictionary.
[cache retrieveImageForSessionID:sessionID [cache retrieveImageForSnapshotID:snapshotID
callback:^(UIImage* image) { callback:^(UIImage* image) {
++numCallbacks; ++numCallbacks;
if (image) if (image)
...@@ -142,14 +142,14 @@ class SnapshotCacheTest : public PlatformTest { ...@@ -142,14 +142,14 @@ class SnapshotCacheTest : public PlatformTest {
// Expect that all the callbacks ran and that none retrieved an image. // Expect that all the callbacks ran and that none retrieved an image.
FlushRunLoops(); FlushRunLoops();
EXPECT_EQ([testSessions_ count], numCallbacks); EXPECT_EQ([snapshotIDs_ count], numCallbacks);
EXPECT_FALSE(foundImage); EXPECT_FALSE(foundImage);
} }
// Loads kSessionCount color images into the cache. If |waitForFilesOnDisk| // Loads kSnapshotCount color images into the cache. If |waitForFilesOnDisk|
// is YES, will not return until the images have been written to disk. // is YES, will not return until the images have been written to disk.
void LoadAllColorImagesIntoCache(bool waitForFilesOnDisk) { void LoadAllColorImagesIntoCache(bool waitForFilesOnDisk) {
LoadColorImagesIntoCache(kSessionCount, waitForFilesOnDisk); LoadColorImagesIntoCache(kSnapshotCount, waitForFilesOnDisk);
} }
// Loads |count| color images into the cache. If |waitForFilesOnDisk| // Loads |count| color images into the cache. If |waitForFilesOnDisk|
...@@ -160,28 +160,28 @@ class SnapshotCacheTest : public PlatformTest { ...@@ -160,28 +160,28 @@ class SnapshotCacheTest : public PlatformTest {
for (NSUInteger i = 0; i < count; ++i) { for (NSUInteger i = 0; i < count; ++i) {
@autoreleasepool { @autoreleasepool {
UIImage* image = [testImages_ objectAtIndex:i]; UIImage* image = [testImages_ objectAtIndex:i];
NSString* sessionID = [testSessions_ objectAtIndex:i]; NSString* snapshotID = [snapshotIDs_ objectAtIndex:i];
[cache setImage:image withSessionID:sessionID]; [cache setImage:image withSnapshotID:snapshotID];
} }
} }
if (waitForFilesOnDisk) { if (waitForFilesOnDisk) {
FlushRunLoops(); FlushRunLoops();
for (NSUInteger i = 0; i < count; ++i) { for (NSUInteger i = 0; i < count; ++i) {
// Check that images are on the disk. // Check that images are on the disk.
NSString* sessionID = [testSessions_ objectAtIndex:i]; NSString* snapshotID = [snapshotIDs_ objectAtIndex:i];
base::FilePath path([cache imagePathForSessionID:sessionID]); base::FilePath path([cache imagePathForSnapshotID:snapshotID]);
EXPECT_TRUE(base::PathExists(path)); EXPECT_TRUE(base::PathExists(path));
} }
} }
} }
// Waits for the first |count| grey images for sessions in |testSessions_| // Waits for the first |count| grey images for |snapshotIDs_| to be placed in
// to be placed in the cache. // the cache.
void WaitForGreyImagesInCache(NSUInteger count) { void WaitForGreyImagesInCache(NSUInteger count) {
SnapshotCache* cache = GetSnapshotCache(); SnapshotCache* cache = GetSnapshotCache();
FlushRunLoops(); FlushRunLoops();
for (NSUInteger i = 0; i < count; i++) for (NSUInteger i = 0; i < count; i++)
EXPECT_TRUE([cache hasGreyImageInMemory:testSessions_[i]]); EXPECT_TRUE([cache hasGreyImageInMemory:snapshotIDs_[i]]);
} }
// Guesses the order of the color channels in the image. // Guesses the order of the color channels in the image.
...@@ -227,7 +227,7 @@ class SnapshotCacheTest : public PlatformTest { ...@@ -227,7 +227,7 @@ class SnapshotCacheTest : public PlatformTest {
web::WebTaskEnvironment task_environment_; web::WebTaskEnvironment task_environment_;
SnapshotCache* snapshotCache_; SnapshotCache* snapshotCache_;
NSMutableArray* testSessions_; NSMutableArray* snapshotIDs_;
NSMutableArray* testImages_; NSMutableArray* testImages_;
}; };
...@@ -237,22 +237,22 @@ class SnapshotCacheTest : public PlatformTest { ...@@ -237,22 +237,22 @@ class SnapshotCacheTest : public PlatformTest {
TEST_F(SnapshotCacheTest, Cache) { TEST_F(SnapshotCacheTest, Cache) {
SnapshotCache* cache = GetSnapshotCache(); SnapshotCache* cache = GetSnapshotCache();
NSUInteger expectedCacheSize = MIN(kSessionCount, [cache lruCacheMaxSize]); NSUInteger expectedCacheSize = MIN(kSnapshotCount, [cache lruCacheMaxSize]);
// Put all images in the cache. // Put all images in the cache.
for (NSUInteger i = 0; i < expectedCacheSize; ++i) { for (NSUInteger i = 0; i < expectedCacheSize; ++i) {
UIImage* image = [testImages_ objectAtIndex:i]; UIImage* image = [testImages_ objectAtIndex:i];
NSString* sessionID = [testSessions_ objectAtIndex:i]; NSString* snapshotID = [snapshotIDs_ objectAtIndex:i];
[cache setImage:image withSessionID:sessionID]; [cache setImage:image withSnapshotID:snapshotID];
} }
// Get images back. // Get images back.
__block NSUInteger numberOfCallbacks = 0; __block NSUInteger numberOfCallbacks = 0;
for (NSUInteger i = 0; i < expectedCacheSize; ++i) { for (NSUInteger i = 0; i < expectedCacheSize; ++i) {
NSString* sessionID = [testSessions_ objectAtIndex:i]; NSString* snapshotID = [snapshotIDs_ objectAtIndex:i];
UIImage* expectedImage = [testImages_ objectAtIndex:i]; UIImage* expectedImage = [testImages_ objectAtIndex:i];
EXPECT_TRUE(expectedImage != nil); EXPECT_TRUE(expectedImage != nil);
[cache retrieveImageForSessionID:sessionID [cache retrieveImageForSnapshotID:snapshotID
callback:^(UIImage* image) { callback:^(UIImage* image) {
// Images have not been removed from the // Images have not been removed from the
// dictionnary. We expect the same pointer. // dictionnary. We expect the same pointer.
...@@ -269,18 +269,18 @@ TEST_F(SnapshotCacheTest, SaveToDisk) { ...@@ -269,18 +269,18 @@ TEST_F(SnapshotCacheTest, SaveToDisk) {
SnapshotCache* cache = GetSnapshotCache(); SnapshotCache* cache = GetSnapshotCache();
// Put all images in the cache. // Put all images in the cache.
for (NSUInteger i = 0; i < kSessionCount; ++i) { for (NSUInteger i = 0; i < kSnapshotCount; ++i) {
UIImage* image = [testImages_ objectAtIndex:i]; UIImage* image = [testImages_ objectAtIndex:i];
NSString* sessionID = [testSessions_ objectAtIndex:i]; NSString* snapshotID = [snapshotIDs_ objectAtIndex:i];
[cache setImage:image withSessionID:sessionID]; [cache setImage:image withSnapshotID:snapshotID];
} }
FlushRunLoops(); FlushRunLoops();
for (NSUInteger i = 0; i < kSessionCount; ++i) { for (NSUInteger i = 0; i < kSnapshotCount; ++i) {
// Check that images are on the disk. // Check that images are on the disk.
NSString* sessionID = [testSessions_ objectAtIndex:i]; NSString* snapshotID = [snapshotIDs_ objectAtIndex:i];
base::FilePath path([cache imagePathForSessionID:sessionID]); base::FilePath path([cache imagePathForSnapshotID:snapshotID]);
EXPECT_TRUE(base::PathExists(path)); EXPECT_TRUE(base::PathExists(path));
// Check image colors by comparing the first pixel against the reference // Check image colors by comparing the first pixel against the reference
...@@ -327,39 +327,39 @@ TEST_F(SnapshotCacheTest, Purge) { ...@@ -327,39 +327,39 @@ TEST_F(SnapshotCacheTest, Purge) {
SnapshotCache* cache = GetSnapshotCache(); SnapshotCache* cache = GetSnapshotCache();
// Put all images in the cache. // Put all images in the cache.
for (NSUInteger i = 0; i < kSessionCount; ++i) { for (NSUInteger i = 0; i < kSnapshotCount; ++i) {
UIImage* image = [testImages_ objectAtIndex:i]; UIImage* image = [testImages_ objectAtIndex:i];
NSString* sessionID = [testSessions_ objectAtIndex:i]; NSString* snapshotID = [snapshotIDs_ objectAtIndex:i];
[cache setImage:image withSessionID:sessionID]; [cache setImage:image withSnapshotID:snapshotID];
} }
NSMutableSet* liveSessions = [NSMutableSet setWithCapacity:1]; NSMutableSet* liveSnapshotIDs = [NSMutableSet setWithCapacity:1];
[liveSessions addObject:[testSessions_ objectAtIndex:0]]; [liveSnapshotIDs addObject:[snapshotIDs_ objectAtIndex:0]];
// Purge the cache. // Purge the cache.
[cache purgeCacheOlderThan:(base::Time::Now() - base::TimeDelta::FromHours(1)) [cache purgeCacheOlderThan:(base::Time::Now() - base::TimeDelta::FromHours(1))
keeping:liveSessions]; keeping:liveSnapshotIDs];
FlushRunLoops(); FlushRunLoops();
// Check that nothing has been deleted. // Check that nothing has been deleted.
for (NSUInteger i = 0; i < kSessionCount; ++i) { for (NSUInteger i = 0; i < kSnapshotCount; ++i) {
// Check that images are on the disk. // Check that images are on the disk.
NSString* sessionID = [testSessions_ objectAtIndex:i]; NSString* snapshotID = [snapshotIDs_ objectAtIndex:i];
base::FilePath path([cache imagePathForSessionID:sessionID]); base::FilePath path([cache imagePathForSnapshotID:snapshotID]);
EXPECT_TRUE(base::PathExists(path)); EXPECT_TRUE(base::PathExists(path));
} }
// Purge the cache. // Purge the cache.
[cache purgeCacheOlderThan:base::Time::Now() keeping:liveSessions]; [cache purgeCacheOlderThan:base::Time::Now() keeping:liveSnapshotIDs];
FlushRunLoops(); FlushRunLoops();
// Check that the file have been deleted. // Check that the file have been deleted.
for (NSUInteger i = 0; i < kSessionCount; ++i) { for (NSUInteger i = 0; i < kSnapshotCount; ++i) {
// Check that images are on the disk. // Check that images are on the disk.
NSString* sessionID = [testSessions_ objectAtIndex:i]; NSString* snapshotID = [snapshotIDs_ objectAtIndex:i];
base::FilePath path([cache imagePathForSessionID:sessionID]); base::FilePath path([cache imagePathForSnapshotID:snapshotID]);
if (i == 0) if (i == 0)
EXPECT_TRUE(base::PathExists(path)); EXPECT_TRUE(base::PathExists(path));
else else
...@@ -374,8 +374,8 @@ TEST_F(SnapshotCacheTest, HandleMemoryWarning) { ...@@ -374,8 +374,8 @@ TEST_F(SnapshotCacheTest, HandleMemoryWarning) {
SnapshotCache* cache = GetSnapshotCache(); SnapshotCache* cache = GetSnapshotCache();
NSString* firstPinnedID = [testSessions_ objectAtIndex:4]; NSString* firstPinnedID = [snapshotIDs_ objectAtIndex:4];
NSString* secondPinnedID = [testSessions_ objectAtIndex:6]; NSString* secondPinnedID = [snapshotIDs_ objectAtIndex:6];
NSMutableSet* set = [NSMutableSet set]; NSMutableSet* set = [NSMutableSet set];
[set addObject:firstPinnedID]; [set addObject:firstPinnedID];
[set addObject:secondPinnedID]; [set addObject:secondPinnedID];
...@@ -386,7 +386,7 @@ TEST_F(SnapshotCacheTest, HandleMemoryWarning) { ...@@ -386,7 +386,7 @@ TEST_F(SnapshotCacheTest, HandleMemoryWarning) {
EXPECT_EQ(YES, [cache hasImageInMemory:firstPinnedID]); EXPECT_EQ(YES, [cache hasImageInMemory:firstPinnedID]);
EXPECT_EQ(YES, [cache hasImageInMemory:secondPinnedID]); EXPECT_EQ(YES, [cache hasImageInMemory:secondPinnedID]);
NSString* notPinnedID = [testSessions_ objectAtIndex:2]; NSString* notPinnedID = [snapshotIDs_ objectAtIndex:2];
EXPECT_FALSE([cache hasImageInMemory:notPinnedID]); EXPECT_FALSE([cache hasImageInMemory:notPinnedID]);
// Wait for the final image to be pulled off disk. // Wait for the final image to be pulled off disk.
...@@ -404,22 +404,22 @@ TEST_F(SnapshotCacheTest, CreateGreyCache) { ...@@ -404,22 +404,22 @@ TEST_F(SnapshotCacheTest, CreateGreyCache) {
// Request the creation of a grey image cache for all images. // Request the creation of a grey image cache for all images.
SnapshotCache* cache = GetSnapshotCache(); SnapshotCache* cache = GetSnapshotCache();
[cache createGreyCache:testSessions_]; [cache createGreyCache:snapshotIDs_];
// Wait for them to be put into the grey image cache. // Wait for them to be put into the grey image cache.
WaitForGreyImagesInCache(kSessionCount); WaitForGreyImagesInCache(kSnapshotCount);
__block NSUInteger numberOfCallbacks = 0; __block NSUInteger numberOfCallbacks = 0;
for (NSUInteger i = 0; i < kSessionCount; ++i) { for (NSUInteger i = 0; i < kSnapshotCount; ++i) {
NSString* sessionID = [testSessions_ objectAtIndex:i]; NSString* snapshotID = [snapshotIDs_ objectAtIndex:i];
[cache retrieveGreyImageForSessionID:sessionID [cache retrieveGreyImageForSnapshotID:snapshotID
callback:^(UIImage* image) { callback:^(UIImage* image) {
EXPECT_TRUE(image); EXPECT_TRUE(image);
++numberOfCallbacks; ++numberOfCallbacks;
}]; }];
} }
EXPECT_EQ(numberOfCallbacks, kSessionCount); EXPECT_EQ(numberOfCallbacks, kSnapshotCount);
} }
// Same as previous test, except that all the color images are on disk, // Same as previous test, except that all the color images are on disk,
...@@ -434,22 +434,22 @@ TEST_F(SnapshotCacheTest, CreateGreyCacheFromDisk) { ...@@ -434,22 +434,22 @@ TEST_F(SnapshotCacheTest, CreateGreyCacheFromDisk) {
TriggerMemoryWarning(); TriggerMemoryWarning();
// Request the creation of a grey image cache for all images. // Request the creation of a grey image cache for all images.
[cache createGreyCache:testSessions_]; [cache createGreyCache:snapshotIDs_];
// Wait for them to be put into the grey image cache. // Wait for them to be put into the grey image cache.
WaitForGreyImagesInCache(kSessionCount); WaitForGreyImagesInCache(kSnapshotCount);
__block NSUInteger numberOfCallbacks = 0; __block NSUInteger numberOfCallbacks = 0;
for (NSUInteger i = 0; i < kSessionCount; ++i) { for (NSUInteger i = 0; i < kSnapshotCount; ++i) {
NSString* sessionID = [testSessions_ objectAtIndex:i]; NSString* snapshotID = [snapshotIDs_ objectAtIndex:i];
[cache retrieveGreyImageForSessionID:sessionID [cache retrieveGreyImageForSnapshotID:snapshotID
callback:^(UIImage* image) { callback:^(UIImage* image) {
EXPECT_TRUE(image); EXPECT_TRUE(image);
++numberOfCallbacks; ++numberOfCallbacks;
}]; }];
} }
EXPECT_EQ(numberOfCallbacks, kSessionCount); EXPECT_EQ(numberOfCallbacks, kSnapshotCount);
} }
#endif // !TARGET_IPHONE_SIMULATOR #endif // !TARGET_IPHONE_SIMULATOR
...@@ -460,11 +460,11 @@ TEST_F(SnapshotCacheTest, CreateGreyCacheFromDisk) { ...@@ -460,11 +460,11 @@ TEST_F(SnapshotCacheTest, CreateGreyCacheFromDisk) {
// Disabled due to the greyImage crash. b/8048597 // Disabled due to the greyImage crash. b/8048597
TEST_F(SnapshotCacheTest, MostRecentGreyBlock) { TEST_F(SnapshotCacheTest, MostRecentGreyBlock) {
const NSUInteger kNumImages = 3; const NSUInteger kNumImages = 3;
NSMutableArray* sessionIDs = NSMutableArray* snapshotIDs =
[[NSMutableArray alloc] initWithCapacity:kNumImages]; [[NSMutableArray alloc] initWithCapacity:kNumImages];
[sessionIDs addObject:[testSessions_ objectAtIndex:0]]; [snapshotIDs addObject:[snapshotIDs_ objectAtIndex:0]];
[sessionIDs addObject:[testSessions_ objectAtIndex:1]]; [snapshotIDs addObject:[snapshotIDs_ objectAtIndex:1]];
[sessionIDs addObject:[testSessions_ objectAtIndex:2]]; [snapshotIDs addObject:[snapshotIDs_ objectAtIndex:2]];
SnapshotCache* cache = GetSnapshotCache(); SnapshotCache* cache = GetSnapshotCache();
...@@ -475,21 +475,21 @@ TEST_F(SnapshotCacheTest, MostRecentGreyBlock) { ...@@ -475,21 +475,21 @@ TEST_F(SnapshotCacheTest, MostRecentGreyBlock) {
TriggerMemoryWarning(); TriggerMemoryWarning();
// Enable the grey image cache. // Enable the grey image cache.
[cache createGreyCache:sessionIDs]; [cache createGreyCache:snapshotIDs];
// Request the grey versions // Request the grey versions
__block BOOL firstCallbackCalled = NO; __block BOOL firstCallbackCalled = NO;
__block BOOL secondCallbackCalled = NO; __block BOOL secondCallbackCalled = NO;
__block BOOL thirdCallbackCalled = NO; __block BOOL thirdCallbackCalled = NO;
[cache greyImageForSessionID:[testSessions_ objectAtIndex:0] [cache greyImageForSnapshotID:[snapshotIDs_ objectAtIndex:0]
callback:^(UIImage*) { callback:^(UIImage*) {
firstCallbackCalled = YES; firstCallbackCalled = YES;
}]; }];
[cache greyImageForSessionID:[testSessions_ objectAtIndex:1] [cache greyImageForSnapshotID:[snapshotIDs_ objectAtIndex:1]
callback:^(UIImage*) { callback:^(UIImage*) {
secondCallbackCalled = YES; secondCallbackCalled = YES;
}]; }];
[cache greyImageForSessionID:[testSessions_ objectAtIndex:2] [cache greyImageForSnapshotID:[snapshotIDs_ objectAtIndex:2]
callback:^(UIImage*) { callback:^(UIImage*) {
thirdCallbackCalled = YES; thirdCallbackCalled = YES;
}]; }];
...@@ -510,16 +510,16 @@ TEST_F(SnapshotCacheTest, GreyImageAllInBackground) { ...@@ -510,16 +510,16 @@ TEST_F(SnapshotCacheTest, GreyImageAllInBackground) {
SnapshotCache* cache = GetSnapshotCache(); SnapshotCache* cache = GetSnapshotCache();
// Now convert every image into a grey image, on disk, in the background. // Now convert every image into a grey image, on disk, in the background.
for (NSUInteger i = 0; i < kSessionCount; ++i) { for (NSUInteger i = 0; i < kSnapshotCount; ++i) {
[cache saveGreyInBackgroundForSessionID:[testSessions_ objectAtIndex:i]]; [cache saveGreyInBackgroundForSnapshotID:[snapshotIDs_ objectAtIndex:i]];
} }
// Waits for the grey images for the sessions in |testSessions_| to be written // Waits for the grey images for |snapshotIDs_| to be written to disk, which
// to disk, which happens in a background thread. // happens in a background thread.
FlushRunLoops(); FlushRunLoops();
for (NSString* sessionID in testSessions_) { for (NSString* snapshotID in snapshotIDs_) {
base::FilePath path([cache greyImagePathForSessionID:sessionID]); base::FilePath path([cache greyImagePathForSnapshotID:snapshotID]);
EXPECT_TRUE(base::PathExists(path)); EXPECT_TRUE(base::PathExists(path));
base::DeleteFile(path); base::DeleteFile(path);
} }
...@@ -540,17 +540,17 @@ TEST_F(SnapshotCacheTest, SizeAndScalePreservation) { ...@@ -540,17 +540,17 @@ TEST_F(SnapshotCacheTest, SizeAndScalePreservation) {
// Add the image to the cache then call handle low memory to ensure the image // Add the image to the cache then call handle low memory to ensure the image
// is read from disk instead of the in-memory cache. // is read from disk instead of the in-memory cache.
NSString* const kSession = @"foo"; NSString* const kSnapshotID = @"foo";
[cache setImage:image withSessionID:kSession]; [cache setImage:image withSnapshotID:kSnapshotID];
FlushRunLoops(); // ensure the file is written to disk. FlushRunLoops(); // ensure the file is written to disk.
TriggerMemoryWarning(); TriggerMemoryWarning();
// Retrive the image and have the callback verify the size and scale. // Retrive the image and have the callback verify the size and scale.
__block BOOL callbackComplete = NO; __block BOOL callbackComplete = NO;
[cache retrieveImageForSessionID:kSession [cache
retrieveImageForSnapshotID:kSnapshotID
callback:^(UIImage* imageFromDisk) { callback:^(UIImage* imageFromDisk) {
EXPECT_EQ(image.size.width, EXPECT_EQ(image.size.width, imageFromDisk.size.width);
imageFromDisk.size.width);
EXPECT_EQ(image.size.height, EXPECT_EQ(image.size.height,
imageFromDisk.size.height); imageFromDisk.size.height);
EXPECT_EQ(image.scale, imageFromDisk.scale); EXPECT_EQ(image.scale, imageFromDisk.scale);
...@@ -576,33 +576,33 @@ TEST_F(SnapshotCacheTest, DeleteRetinaImages) { ...@@ -576,33 +576,33 @@ TEST_F(SnapshotCacheTest, DeleteRetinaImages) {
// Add the image to the cache then call handle low memory to ensure the image // Add the image to the cache then call handle low memory to ensure the image
// is read from disk instead of the in-memory cache. // is read from disk instead of the in-memory cache.
NSString* const kSession = @"foo"; NSString* const kSnapshotID = @"foo";
[cache setImage:image withSessionID:kSession]; [cache setImage:image withSnapshotID:kSnapshotID];
FlushRunLoops(); // ensure the file is written to disk. FlushRunLoops(); // ensure the file is written to disk.
TriggerMemoryWarning(); TriggerMemoryWarning();
// Verify the file was writted with @2x in the file name. // Verify the file was writted with @2x in the file name.
base::FilePath retinaFile = [cache imagePathForSessionID:kSession]; base::FilePath retinaFile = [cache imagePathForSnapshotID:kSnapshotID];
EXPECT_TRUE(base::PathExists(retinaFile)); EXPECT_TRUE(base::PathExists(retinaFile));
// Delete the image. // Delete the image.
[cache removeImageWithSessionID:kSession]; [cache removeImageWithSnapshotID:kSnapshotID];
FlushRunLoops(); // ensure the file is removed. FlushRunLoops(); // ensure the file is removed.
EXPECT_FALSE(base::PathExists(retinaFile)); EXPECT_FALSE(base::PathExists(retinaFile));
} }
// Tests that a marked image does not immediately delete when calling // Tests that a marked image does not immediately delete when calling
// |-removeImageWithSessionID:|. Calling |-removeMarkedImages| immediately // |-removeImageWithSnapshotID:|. Calling |-removeMarkedImages| immediately
// deletes the marked image. // deletes the marked image.
TEST_F(SnapshotCacheTest, MarkedImageNotImmediatelyDeleted) { TEST_F(SnapshotCacheTest, MarkedImageNotImmediatelyDeleted) {
SnapshotCache* cache = GetSnapshotCache(); SnapshotCache* cache = GetSnapshotCache();
UIImage* image = UIImage* image =
GenerateRandomImage(CGSizeMake(kSnapshotPixelSize, kSnapshotPixelSize)); GenerateRandomImage(CGSizeMake(kSnapshotPixelSize, kSnapshotPixelSize));
[cache setImage:image withSessionID:@"sessionID"]; [cache setImage:image withSnapshotID:@"snapshotID"];
base::FilePath image_path = [cache imagePathForSessionID:@"sessionID"]; base::FilePath image_path = [cache imagePathForSnapshotID:@"snapshotID"];
[cache markImageWithSessionID:@"sessionID"]; [cache markImageWithSnapshotID:@"snapshotID"];
[cache removeImageWithSessionID:@"sessionID"]; [cache removeImageWithSnapshotID:@"snapshotID"];
// Give enough time for deletion. // Give enough time for deletion.
FlushRunLoops(); FlushRunLoops();
EXPECT_TRUE(base::PathExists(image_path)); EXPECT_TRUE(base::PathExists(image_path));
...@@ -617,9 +617,9 @@ TEST_F(SnapshotCacheTest, UnmarkedImageNotDeleted) { ...@@ -617,9 +617,9 @@ TEST_F(SnapshotCacheTest, UnmarkedImageNotDeleted) {
SnapshotCache* cache = GetSnapshotCache(); SnapshotCache* cache = GetSnapshotCache();
UIImage* image = UIImage* image =
GenerateRandomImage(CGSizeMake(kSnapshotPixelSize, kSnapshotPixelSize)); GenerateRandomImage(CGSizeMake(kSnapshotPixelSize, kSnapshotPixelSize));
[cache setImage:image withSessionID:@"sessionID"]; [cache setImage:image withSnapshotID:@"snapshotID"];
base::FilePath image_path = [cache imagePathForSessionID:@"sessionID"]; base::FilePath image_path = [cache imagePathForSnapshotID:@"snapshotID"];
[cache markImageWithSessionID:@"sessionID"]; [cache markImageWithSnapshotID:@"snapshotID"];
[cache unmarkAllImages]; [cache unmarkAllImages];
[cache removeMarkedImages]; [cache removeMarkedImages];
// Give enough time for deletion. // Give enough time for deletion.
...@@ -635,12 +635,12 @@ TEST_F(SnapshotCacheTest, ObserversNotifiedOnSetAndRemoveImage) { ...@@ -635,12 +635,12 @@ TEST_F(SnapshotCacheTest, ObserversNotifiedOnSetAndRemoveImage) {
[cache addObserver:observer]; [cache addObserver:observer];
EXPECT_NSEQ(nil, observer.lastUpdatedIdentifier); EXPECT_NSEQ(nil, observer.lastUpdatedIdentifier);
UIImage* image = [testImages_ objectAtIndex:0]; UIImage* image = [testImages_ objectAtIndex:0];
NSString* sessionID = [testSessions_ objectAtIndex:0]; NSString* snapshotID = [snapshotIDs_ objectAtIndex:0];
[cache setImage:image withSessionID:sessionID]; [cache setImage:image withSnapshotID:snapshotID];
EXPECT_NSEQ(sessionID, observer.lastUpdatedIdentifier); EXPECT_NSEQ(snapshotID, observer.lastUpdatedIdentifier);
observer.lastUpdatedIdentifier = nil; observer.lastUpdatedIdentifier = nil;
[cache removeImageWithSessionID:sessionID]; [cache removeImageWithSnapshotID:snapshotID];
EXPECT_NSEQ(sessionID, observer.lastUpdatedIdentifier); EXPECT_NSEQ(snapshotID, observer.lastUpdatedIdentifier);
[cache removeObserver:observer]; [cache removeObserver:observer];
} }
} // namespace } // namespace
...@@ -24,8 +24,7 @@ class WebState; ...@@ -24,8 +24,7 @@ class WebState;
// Designated initializer. // Designated initializer.
- (instancetype)initWithWebState:(web::WebState*)webState - (instancetype)initWithWebState:(web::WebState*)webState
snapshotSessionId:(NSString*)snapshotSessionId tabID:(NSString*)tabID NS_DESIGNATED_INITIALIZER;
NS_DESIGNATED_INITIALIZER;
- (instancetype)init NS_UNAVAILABLE; - (instancetype)init NS_UNAVAILABLE;
......
...@@ -56,7 +56,7 @@ BOOL ViewHierarchyContainsWKWebView(UIView* view) { ...@@ -56,7 +56,7 @@ BOOL ViewHierarchyContainsWKWebView(UIView* view) {
@interface SnapshotGenerator ()<CRWWebStateObserver> @interface SnapshotGenerator ()<CRWWebStateObserver>
// The unique ID for the web state. // The unique ID for the web state.
@property(nonatomic, copy) NSString* sessionID; @property(nonatomic, copy) NSString* tabID;
// The associated web state. // The associated web state.
@property(nonatomic, assign) web::WebState* webState; @property(nonatomic, assign) web::WebState* webState;
...@@ -68,12 +68,12 @@ BOOL ViewHierarchyContainsWKWebView(UIView* view) { ...@@ -68,12 +68,12 @@ BOOL ViewHierarchyContainsWKWebView(UIView* view) {
} }
- (instancetype)initWithWebState:(web::WebState*)webState - (instancetype)initWithWebState:(web::WebState*)webState
snapshotSessionId:(NSString*)snapshotSessionId { tabID:(NSString*)tabID {
if ((self = [super init])) { if ((self = [super init])) {
DCHECK(webState); DCHECK(webState);
DCHECK(snapshotSessionId); DCHECK(tabID);
_webState = webState; _webState = webState;
_sessionID = snapshotSessionId; _tabID = tabID;
_webStateObserver = std::make_unique<web::WebStateObserverBridge>(self); _webStateObserver = std::make_unique<web::WebStateObserverBridge>(self);
_webState->AddObserver(_webStateObserver.get()); _webState->AddObserver(_webStateObserver.get());
...@@ -92,7 +92,7 @@ BOOL ViewHierarchyContainsWKWebView(UIView* view) { ...@@ -92,7 +92,7 @@ BOOL ViewHierarchyContainsWKWebView(UIView* view) {
- (void)retrieveSnapshot:(void (^)(UIImage*))callback { - (void)retrieveSnapshot:(void (^)(UIImage*))callback {
DCHECK(callback); DCHECK(callback);
if (self.snapshotCache) { if (self.snapshotCache) {
[self.snapshotCache retrieveImageForSessionID:self.sessionID [self.snapshotCache retrieveImageForSnapshotID:self.tabID
callback:callback]; callback:callback];
} else { } else {
callback(nil); callback(nil);
...@@ -114,7 +114,7 @@ BOOL ViewHierarchyContainsWKWebView(UIView* view) { ...@@ -114,7 +114,7 @@ BOOL ViewHierarchyContainsWKWebView(UIView* view) {
SnapshotCache* snapshotCache = self.snapshotCache; SnapshotCache* snapshotCache = self.snapshotCache;
if (snapshotCache) { if (snapshotCache) {
[snapshotCache retrieveGreyImageForSessionID:self.sessionID [snapshotCache retrieveGreyImageForSnapshotID:self.tabID
callback:wrappedCallback]; callback:wrappedCallback];
} else { } else {
wrappedCallback(nil); wrappedCallback(nil);
...@@ -178,7 +178,7 @@ BOOL ViewHierarchyContainsWKWebView(UIView* view) { ...@@ -178,7 +178,7 @@ BOOL ViewHierarchyContainsWKWebView(UIView* view) {
} }
- (void)removeSnapshot { - (void)removeSnapshot {
[self.snapshotCache removeImageWithSessionID:self.sessionID]; [self.snapshotCache removeImageWithSnapshotID:self.tabID];
} }
#pragma mark - Private methods #pragma mark - Private methods
...@@ -277,10 +277,10 @@ BOOL ViewHierarchyContainsWKWebView(UIView* view) { ...@@ -277,10 +277,10 @@ BOOL ViewHierarchyContainsWKWebView(UIView* view) {
// Updates the snapshot cache with |snapshot|. // Updates the snapshot cache with |snapshot|.
- (void)updateSnapshotCacheWithImage:(UIImage*)snapshot { - (void)updateSnapshotCacheWithImage:(UIImage*)snapshot {
if (snapshot) { if (snapshot) {
[self.snapshotCache setImage:snapshot withSessionID:self.sessionID]; [self.snapshotCache setImage:snapshot withSnapshotID:self.tabID];
} else { } else {
// Remove any stale snapshot since the snapshot failed. // Remove any stale snapshot since the snapshot failed.
[self.snapshotCache removeImageWithSessionID:self.sessionID]; [self.snapshotCache removeImageWithSnapshotID:self.tabID];
} }
} }
......
...@@ -30,9 +30,9 @@ class SnapshotTabHelper : public infobars::InfoBarManager::Observer, ...@@ -30,9 +30,9 @@ class SnapshotTabHelper : public infobars::InfoBarManager::Observer,
~SnapshotTabHelper() override; ~SnapshotTabHelper() override;
// Creates the tab helper for |web_state| if it does not exists. The // Creates the tab helper for |web_state| if it does not exists. The
// unique identifier |session_id| is used when interacting with the // unique identifier |tab_id| is used when interacting with the
// cache to save or fetch snapshots. // cache to save or fetch snapshots.
static void CreateForWebState(web::WebState* web_state, NSString* session_id); static void CreateForWebState(web::WebState* web_state, NSString* tab_id);
// Sets the delegate. Capturing snapshot before setting a delegate will // Sets the delegate. Capturing snapshot before setting a delegate will
// results in failures. The delegate is not owned by the tab helper. // results in failures. The delegate is not owned by the tab helper.
...@@ -81,7 +81,7 @@ class SnapshotTabHelper : public infobars::InfoBarManager::Observer, ...@@ -81,7 +81,7 @@ class SnapshotTabHelper : public infobars::InfoBarManager::Observer,
private: private:
friend class web::WebStateUserData<SnapshotTabHelper>; friend class web::WebStateUserData<SnapshotTabHelper>;
SnapshotTabHelper(web::WebState* web_state, NSString* session_id); SnapshotTabHelper(web::WebState* web_state, NSString* tab_id);
// web::WebStateObserver implementation. // web::WebStateObserver implementation.
void PageLoaded( void PageLoaded(
......
...@@ -47,12 +47,12 @@ SnapshotTabHelper::~SnapshotTabHelper() { ...@@ -47,12 +47,12 @@ SnapshotTabHelper::~SnapshotTabHelper() {
// static // static
void SnapshotTabHelper::CreateForWebState(web::WebState* web_state, void SnapshotTabHelper::CreateForWebState(web::WebState* web_state,
NSString* session_id) { NSString* tab_id) {
DCHECK(web_state); DCHECK(web_state);
if (!FromWebState(web_state)) { if (!FromWebState(web_state)) {
web_state->SetUserData( web_state->SetUserData(
UserDataKey(), UserDataKey(),
base::WrapUnique(new SnapshotTabHelper(web_state, session_id))); base::WrapUnique(new SnapshotTabHelper(web_state, tab_id)));
} }
} }
...@@ -109,14 +109,13 @@ void SnapshotTabHelper::IgnoreNextLoad() { ...@@ -109,14 +109,13 @@ void SnapshotTabHelper::IgnoreNextLoad() {
ignore_next_load_ = true; ignore_next_load_ = true;
} }
SnapshotTabHelper::SnapshotTabHelper(web::WebState* web_state, SnapshotTabHelper::SnapshotTabHelper(web::WebState* web_state, NSString* tab_id)
NSString* session_id)
: web_state_(web_state), : web_state_(web_state),
web_state_observer_(this), web_state_observer_(this),
infobar_observer_(this), infobar_observer_(this),
weak_ptr_factory_(this) { weak_ptr_factory_(this) {
snapshot_generator_ = [[SnapshotGenerator alloc] initWithWebState:web_state_ snapshot_generator_ = [[SnapshotGenerator alloc] initWithWebState:web_state_
snapshotSessionId:session_id]; tabID:tab_id];
web_state_observer_.Add(web_state_); web_state_observer_.Add(web_state_);
// Supports missing InfoBarManager to make testing easier. // Supports missing InfoBarManager to make testing easier.
......
...@@ -86,9 +86,9 @@ class SnapshotTabHelperTest : public PlatformTest { ...@@ -86,9 +86,9 @@ class SnapshotTabHelperTest : public PlatformTest {
public: public:
SnapshotTabHelperTest() { SnapshotTabHelperTest() {
// Create the SnapshotTabHelper with a fake delegate. // Create the SnapshotTabHelper with a fake delegate.
snapshot_session_id_ = [[NSUUID UUID] UUIDString]; snapshot_id_ = [[NSUUID UUID] UUIDString];
delegate_ = [[TabHelperSnapshotGeneratorDelegate alloc] init]; delegate_ = [[TabHelperSnapshotGeneratorDelegate alloc] init];
SnapshotTabHelper::CreateForWebState(&web_state_, snapshot_session_id_); SnapshotTabHelper::CreateForWebState(&web_state_, snapshot_id_);
SnapshotTabHelper::FromWebState(&web_state_)->SetDelegate(delegate_); SnapshotTabHelper::FromWebState(&web_state_)->SetDelegate(delegate_);
// Set custom snapshot cache. // Set custom snapshot cache.
...@@ -106,7 +106,7 @@ class SnapshotTabHelperTest : public PlatformTest { ...@@ -106,7 +106,7 @@ class SnapshotTabHelperTest : public PlatformTest {
~SnapshotTabHelperTest() override { [snapshot_cache_ shutdown]; } ~SnapshotTabHelperTest() override { [snapshot_cache_ shutdown]; }
void SetCachedSnapshot(UIImage* image) { void SetCachedSnapshot(UIImage* image) {
[snapshot_cache_ setImage:image withSessionID:snapshot_session_id_]; [snapshot_cache_ setImage:image withSnapshotID:snapshot_id_];
} }
UIImage* GetCachedSnapshot() { UIImage* GetCachedSnapshot() {
...@@ -114,7 +114,7 @@ class SnapshotTabHelperTest : public PlatformTest { ...@@ -114,7 +114,7 @@ class SnapshotTabHelperTest : public PlatformTest {
base::RunLoop* run_loop_ptr = &run_loop; base::RunLoop* run_loop_ptr = &run_loop;
__block UIImage* snapshot = nil; __block UIImage* snapshot = nil;
[snapshot_cache_ retrieveImageForSessionID:snapshot_session_id_ [snapshot_cache_ retrieveImageForSnapshotID:snapshot_id_
callback:^(UIImage* cached_snapshot) { callback:^(UIImage* cached_snapshot) {
snapshot = cached_snapshot; snapshot = cached_snapshot;
run_loop_ptr->Quit(); run_loop_ptr->Quit();
...@@ -128,7 +128,7 @@ class SnapshotTabHelperTest : public PlatformTest { ...@@ -128,7 +128,7 @@ class SnapshotTabHelperTest : public PlatformTest {
web::WebTaskEnvironment task_environment_; web::WebTaskEnvironment task_environment_;
TabHelperSnapshotGeneratorDelegate* delegate_ = nil; TabHelperSnapshotGeneratorDelegate* delegate_ = nil;
SnapshotCache* snapshot_cache_ = nil; SnapshotCache* snapshot_cache_ = nil;
NSString* snapshot_session_id_ = nil; NSString* snapshot_id_ = nil;
web::TestWebState web_state_; web::TestWebState web_state_;
private: private:
...@@ -340,14 +340,14 @@ TEST_F(SnapshotTabHelperTest, ClosingWebStateDoesNotRemoveSnapshot) { ...@@ -340,14 +340,14 @@ TEST_F(SnapshotTabHelperTest, ClosingWebStateDoesNotRemoveSnapshot) {
NSString* tab_id = TabIdTabHelper::FromWebState(web_state.get())->tab_id(); NSString* tab_id = TabIdTabHelper::FromWebState(web_state.get())->tab_id();
SnapshotTabHelper::CreateForWebState(web_state.get(), tab_id); SnapshotTabHelper::CreateForWebState(web_state.get(), tab_id);
[[partialMock reject] removeImageWithSessionID:tab_id]; [[partialMock reject] removeImageWithSnapshotID:tab_id];
// Use @try/@catch as -reject raises an exception. // Use @try/@catch as -reject raises an exception.
@try { @try {
web_state.reset(); web_state.reset();
EXPECT_OCMOCK_VERIFY(partialMock); EXPECT_OCMOCK_VERIFY(partialMock);
} @catch (NSException* exception) { } @catch (NSException* exception) {
// The exception is raised when -removeImageWithSessionID: is invoked. As // The exception is raised when -removeImageWithSnapshotID: is invoked. As
// this should not happen, mark the test as failed. // this should not happen, mark the test as failed.
GTEST_FAIL(); GTEST_FAIL();
} }
......
...@@ -295,7 +295,7 @@ void RecordInterfaceOrientationMetric() { ...@@ -295,7 +295,7 @@ void RecordInterfaceOrientationMetric() {
->tab_id(); ->tab_id();
[_snapshotBrowserAgent->GetSnapshotCache() [_snapshotBrowserAgent->GetSnapshotCache()
saveGreyInBackgroundForSessionID:tabId]; saveGreyInBackgroundForSnapshotID:tabId];
} }
} }
......
...@@ -308,7 +308,7 @@ web::WebState* GetWebStateWithId(WebStateList* web_state_list, ...@@ -308,7 +308,7 @@ web::WebState* GetWebStateWithId(WebStateList* web_state_list,
for (int i = 0; i < self.webStateList->count(); i++) { for (int i = 0; i < self.webStateList->count(); i++) {
web::WebState* webState = self.webStateList->GetWebStateAt(i); web::WebState* webState = self.webStateList->GetWebStateAt(i);
TabIdTabHelper* tabHelper = TabIdTabHelper::FromWebState(webState); TabIdTabHelper* tabHelper = TabIdTabHelper::FromWebState(webState);
[self.snapshotCache markImageWithSessionID:tabHelper->tab_id()]; [self.snapshotCache markImageWithSnapshotID:tabHelper->tab_id()];
} }
self.closedSessionWindow = SerializeWebStateList(self.webStateList); self.closedSessionWindow = SerializeWebStateList(self.webStateList);
int old_size = int old_size =
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment