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( ...@@ -140,42 +140,46 @@ void ConvertAndSaveGreyImage(
DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI); DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI);
propertyReleaser_SnapshotCache_.Init(self, [SnapshotCache class]); propertyReleaser_SnapshotCache_.Init(self, [SnapshotCache class]);
// TODO(andybons): In the case where the cache grows, it is expensive. if (!IsIPadIdiom()) {
// Make sure this doesn't suck when there are more than ten tabs. // TODO(jbbegue): In the case where the cache grows, it is expensive.
imageDictionary_.reset( // Make sure this doesn't suck when there are more than ten tabs.
[[NSMutableDictionary alloc] initWithCapacity:kCacheInitialCapacity]); imageDictionary_.reset(
[[NSNotificationCenter defaultCenter] [[NSMutableDictionary alloc] initWithCapacity:kCacheInitialCapacity]);
addObserver:self [[NSNotificationCenter defaultCenter]
selector:@selector(handleLowMemory) addObserver:self
name:UIApplicationDidReceiveMemoryWarningNotification selector:@selector(handleLowMemory)
object:nil]; name:UIApplicationDidReceiveMemoryWarningNotification
[[NSNotificationCenter defaultCenter] object:nil];
addObserver:self [[NSNotificationCenter defaultCenter]
selector:@selector(handleEnterBackground) addObserver:self
name:UIApplicationDidEnterBackgroundNotification selector:@selector(handleEnterBackground)
object:nil]; name:UIApplicationDidEnterBackgroundNotification
[[NSNotificationCenter defaultCenter] object:nil];
addObserver:self [[NSNotificationCenter defaultCenter]
selector:@selector(handleBecomeActive) addObserver:self
name:UIApplicationDidBecomeActiveNotification selector:@selector(handleBecomeActive)
object:nil]; name:UIApplicationDidBecomeActiveNotification
object:nil];
}
} }
return self; return self;
} }
- (void)dealloc { - (void)dealloc {
[[NSNotificationCenter defaultCenter] if (!IsIPadIdiom()) {
removeObserver:self [[NSNotificationCenter defaultCenter]
name:UIApplicationDidReceiveMemoryWarningNotification removeObserver:self
object:nil]; name:UIApplicationDidReceiveMemoryWarningNotification
[[NSNotificationCenter defaultCenter] object:nil];
removeObserver:self [[NSNotificationCenter defaultCenter]
name:UIApplicationDidEnterBackgroundNotification removeObserver:self
object:nil]; name:UIApplicationDidEnterBackgroundNotification
[[NSNotificationCenter defaultCenter] object:nil];
removeObserver:self [[NSNotificationCenter defaultCenter]
name:UIApplicationDidBecomeActiveNotification removeObserver:self
object:nil]; name:UIApplicationDidBecomeActiveNotification
object:nil];
}
[super dealloc]; [super dealloc];
} }
...@@ -195,6 +199,11 @@ void ConvertAndSaveGreyImage( ...@@ -195,6 +199,11 @@ void ConvertAndSaveGreyImage(
callback:(void (^)(UIImage*))callback { callback:(void (^)(UIImage*))callback {
DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI); DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI);
DCHECK(sessionID); 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]; UIImage* img = [imageDictionary_ objectForKey:sessionID];
if (img) { if (img) {
if (callback) if (callback)
...@@ -205,14 +214,16 @@ void ConvertAndSaveGreyImage( ...@@ -205,14 +214,16 @@ void ConvertAndSaveGreyImage(
base::PostTaskAndReplyWithResult( base::PostTaskAndReplyWithResult(
web::WebThread::GetMessageLoopProxyForThread( web::WebThread::GetMessageLoopProxyForThread(
web::WebThread::FILE_USER_BLOCKING).get(), web::WebThread::FILE_USER_BLOCKING).get(),
FROM_HERE, FROM_HERE, base::BindBlock(^base::scoped_nsobject<UIImage>() {
base::BindBlock(^base::scoped_nsobject<UIImage>() {
// Retrieve the image on a high priority thread. // Retrieve the image on a high priority thread.
return base::scoped_nsobject<UIImage>([ReadImageFromDisk( return base::scoped_nsobject<UIImage>([ReadImageFromDisk(
[SnapshotCache imagePathForSessionID:sessionID]) retain]); [SnapshotCache imagePathForSessionID:sessionID]) retain]);
}), }),
base::BindBlock(^(base::scoped_nsobject<UIImage> image) { 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]; [imageDictionary_ setObject:image forKey:sessionID];
if (callback) if (callback)
callback(image); callback(image);
...@@ -224,7 +235,9 @@ void ConvertAndSaveGreyImage( ...@@ -224,7 +235,9 @@ void ConvertAndSaveGreyImage(
if (!img || !sessionID) if (!img || !sessionID)
return; 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()) { if (!IsIPadIdiom()) {
[imageDictionary_ setObject:img forKey:sessionID]; [imageDictionary_ setObject:img forKey:sessionID];
} }
...@@ -346,6 +359,7 @@ void ConvertAndSaveGreyImage( ...@@ -346,6 +359,7 @@ void ConvertAndSaveGreyImage(
} }
- (void)handleLowMemory { - (void)handleLowMemory {
DCHECK(!IsIPadIdiom());
DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI); DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI);
NSMutableDictionary* dictionary = NSMutableDictionary* dictionary =
[[NSMutableDictionary alloc] initWithCapacity:2]; [[NSMutableDictionary alloc] initWithCapacity:2];
...@@ -358,11 +372,13 @@ void ConvertAndSaveGreyImage( ...@@ -358,11 +372,13 @@ void ConvertAndSaveGreyImage(
} }
- (void)handleEnterBackground { - (void)handleEnterBackground {
DCHECK(!IsIPadIdiom());
DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI); DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI);
[imageDictionary_ removeAllObjects]; [imageDictionary_ removeAllObjects];
} }
- (void)handleBecomeActive { - (void)handleBecomeActive {
DCHECK(!IsIPadIdiom());
DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI); DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI);
for (NSString* sessionID in pinnedIDs_) for (NSString* sessionID in pinnedIDs_)
[self retrieveImageForSessionID:sessionID callback:nil]; [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