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