Commit 9b48f66b authored by sdefresne's avatar sdefresne Committed by Commit bot

[iOS] Fixed tab snapshot cache refresh issues on iPad.

BUG=463851

Review URL: https://codereview.chromium.org/1141423002

Cr-Commit-Position: refs/heads/master@{#330731}
parent 4369139d
......@@ -140,42 +140,46 @@ void ConvertAndSaveGreyImage(
DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI);
propertyReleaser_SnapshotCache_.Init(self, [SnapshotCache class]);
// TODO(andybons): In the case where the cache grows, it is expensive.
// Make sure this doesn't suck when there are more than ten tabs.
imageDictionary_.reset(
[[NSMutableDictionary alloc] initWithCapacity:kCacheInitialCapacity]);
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(handleLowMemory)
name:UIApplicationDidReceiveMemoryWarningNotification
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(handleEnterBackground)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(handleBecomeActive)
name:UIApplicationDidBecomeActiveNotification
object:nil];
if (!IsIPadIdiom()) {
// TODO(jbbegue): In the case where the cache grows, it is expensive.
// Make sure this doesn't suck when there are more than ten tabs.
imageDictionary_.reset(
[[NSMutableDictionary alloc] initWithCapacity:kCacheInitialCapacity]);
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(handleLowMemory)
name:UIApplicationDidReceiveMemoryWarningNotification
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(handleEnterBackground)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(handleBecomeActive)
name:UIApplicationDidBecomeActiveNotification
object:nil];
}
}
return self;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:UIApplicationDidReceiveMemoryWarningNotification
object:nil];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:UIApplicationDidEnterBackgroundNotification
object:nil];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:UIApplicationDidBecomeActiveNotification
object:nil];
if (!IsIPadIdiom()) {
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:UIApplicationDidReceiveMemoryWarningNotification
object:nil];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:UIApplicationDidEnterBackgroundNotification
object:nil];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:UIApplicationDidBecomeActiveNotification
object:nil];
}
[super dealloc];
}
......@@ -195,6 +199,11 @@ void ConvertAndSaveGreyImage(
callback:(void (^)(UIImage*))callback {
DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI);
DCHECK(sessionID);
// iPad does not cache images, so if there is no callback we can avoid an
// expensive read from storage.
if (IsIPadIdiom() && !callback)
return;
UIImage* img = [imageDictionary_ objectForKey:sessionID];
if (img) {
if (callback)
......@@ -205,14 +214,16 @@ void ConvertAndSaveGreyImage(
base::PostTaskAndReplyWithResult(
web::WebThread::GetMessageLoopProxyForThread(
web::WebThread::FILE_USER_BLOCKING).get(),
FROM_HERE,
base::BindBlock(^base::scoped_nsobject<UIImage>() {
FROM_HERE, base::BindBlock(^base::scoped_nsobject<UIImage>() {
// Retrieve the image on a high priority thread.
return base::scoped_nsobject<UIImage>([ReadImageFromDisk(
[SnapshotCache imagePathForSessionID:sessionID]) retain]);
}),
base::BindBlock(^(base::scoped_nsobject<UIImage> image) {
if (image)
// The iPad tab switcher is currently using its own memory cache so the
// image is not stored in memory here if running on iPad.
// The same logic is used on image writes (code below).
if (!IsIPadIdiom() && image)
[imageDictionary_ setObject:image forKey:sessionID];
if (callback)
callback(image);
......@@ -224,7 +235,9 @@ void ConvertAndSaveGreyImage(
if (!img || !sessionID)
return;
// Color snapshots are not used on tablets, so don't keep them in memory.
// The iPad tab switcher is currently using its own memory cache so the image
// is not stored in memory here if running on iPad.
// The same logic is used on image reads (code above).
if (!IsIPadIdiom()) {
[imageDictionary_ setObject:img forKey:sessionID];
}
......@@ -346,6 +359,7 @@ void ConvertAndSaveGreyImage(
}
- (void)handleLowMemory {
DCHECK(!IsIPadIdiom());
DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI);
NSMutableDictionary* dictionary =
[[NSMutableDictionary alloc] initWithCapacity:2];
......@@ -358,11 +372,13 @@ void ConvertAndSaveGreyImage(
}
- (void)handleEnterBackground {
DCHECK(!IsIPadIdiom());
DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI);
[imageDictionary_ removeAllObjects];
}
- (void)handleBecomeActive {
DCHECK(!IsIPadIdiom());
DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI);
for (NSString* sessionID in pinnedIDs_)
[self retrieveImageForSessionID:sessionID callback:nil];
......
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