Commit c458c18b authored by jbbegue's avatar jbbegue Committed by Commit bot

Enable the snapshot cache on iPad when the tab switcher is enabled.

BUG=576269

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

Cr-Commit-Position: refs/heads/master@{#369418}
parent 87882ce3
......@@ -144,15 +144,17 @@ void ConvertAndSaveGreyImage(
DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI);
propertyReleaser_SnapshotCache_.Init(self, [SnapshotCache class]);
if (!IsIPadIdiom()) {
if (experimental_flags::IsLRUSnapshotCacheEnabled()) {
lruCache_.reset(
[[LRUCache alloc] initWithCacheSize:kLRUCacheMaxCapacity]);
} else {
imageDictionary_.reset([[NSMutableDictionary alloc]
initWithCapacity:kCacheInitialCapacity]);
}
// Always use the LRUCache when the tab switcher is enabled.
if (experimental_flags::IsTabSwitcherEnabled() ||
experimental_flags::IsLRUSnapshotCacheEnabled()) {
lruCache_.reset(
[[LRUCache alloc] initWithCacheSize:kLRUCacheMaxCapacity]);
} else {
imageDictionary_.reset(
[[NSMutableDictionary alloc] initWithCapacity:kCacheInitialCapacity]);
}
if (!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled()) {
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(handleLowMemory)
......@@ -174,7 +176,7 @@ void ConvertAndSaveGreyImage(
}
- (void)dealloc {
if (!IsIPadIdiom()) {
if (!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled()) {
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:UIApplicationDidReceiveMemoryWarningNotification
......@@ -207,9 +209,10 @@ 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)
// Cache on iPad is enabled only when the tab switcher is enabled.
if ((IsIPadIdiom() && !experimental_flags::IsTabSwitcherEnabled()) &&
!callback)
return;
UIImage* img = nil;
......@@ -233,10 +236,9 @@ void ConvertAndSaveGreyImage(
[SnapshotCache imagePathForSessionID:sessionID]) retain]);
}),
base::BindBlock(^(base::scoped_nsobject<UIImage> 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) {
// Cache on iPad is enabled only when the tab switcher is enabled.
if ((!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled()) &&
image) {
if (lruCache_)
[lruCache_ setObject:image forKey:sessionID];
else
......@@ -252,10 +254,8 @@ void ConvertAndSaveGreyImage(
if (!img || !sessionID)
return;
// 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()) {
// Cache on iPad is enabled only when the tab switcher is enabled.
if (!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled()) {
if (lruCache_)
[lruCache_ setObject:img forKey:sessionID];
else
......@@ -387,7 +387,7 @@ void ConvertAndSaveGreyImage(
}
- (void)handleLowMemory {
DCHECK(!IsIPadIdiom());
DCHECK(!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled());
DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI);
NSMutableDictionary* dictionary =
[[NSMutableDictionary alloc] initWithCapacity:2];
......@@ -410,14 +410,14 @@ void ConvertAndSaveGreyImage(
}
- (void)handleEnterBackground {
DCHECK(!IsIPadIdiom());
DCHECK(!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled());
DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI);
[imageDictionary_ removeAllObjects];
[lruCache_ removeAllObjects];
}
- (void)handleBecomeActive {
DCHECK(!IsIPadIdiom());
DCHECK(!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled());
DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI);
for (NSString* sessionID in pinnedIDs_)
[self retrieveImageForSessionID:sessionID callback:nil];
......
......@@ -216,7 +216,7 @@ class SnapshotCacheTest : public PlatformTest {
TEST_F(SnapshotCacheTest, Cache) {
// Don't run on tablets because color snapshots are not cached so this test
// can't compare the UIImage pointers directly.
if (IsIPadIdiom()) {
if (IsIPadIdiom() && !experimental_flags::IsTabSwitcherEnabled()) {
return;
}
......@@ -368,13 +368,13 @@ TEST_F(SnapshotCacheTest, HandleLowMemory) {
[set addObject:secondPinnedID];
cache.pinnedIDs = set;
if (!IsIPadIdiom())
if (!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled())
[cache handleLowMemory];
BOOL expectedValue = YES;
if (IsIPadIdiom()) {
if (IsIPadIdiom() && !experimental_flags::IsTabSwitcherEnabled())
expectedValue = NO;
}
EXPECT_EQ(expectedValue, [cache hasImageInMemory:firstPinnedID]);
EXPECT_EQ(expectedValue, [cache hasImageInMemory:secondPinnedID]);
......@@ -423,7 +423,7 @@ TEST_F(SnapshotCacheTest, CreateGreyCacheFromDisk) {
// Remove color images from in-memory cache.
SnapshotCache* cache = GetSnapshotCache();
if (!IsIPadIdiom())
if (!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled())
[cache handleLowMemory];
// Request the creation of a grey image cache for all images.
......@@ -465,7 +465,7 @@ TEST_F(SnapshotCacheTest, MostRecentGreyBlock) {
LoadColorImagesIntoCache(kNumImages, true);
// Make sure the color images are only on disk, to ensure the background
// thread is slow enough to queue up the requests.
if (!IsIPadIdiom())
if (!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled())
[cache handleLowMemory];
// Enable the grey image cache.
......@@ -537,7 +537,7 @@ TEST_F(SnapshotCacheTest, SizeAndScalePreservation) {
NSString* const kSession = @"foo";
[cache setImage:image withSessionID:kSession];
FlushRunLoops(); // ensure the file is written to disk.
if (!IsIPadIdiom())
if (!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled())
[cache handleLowMemory];
// Retrive the image and have the callback verify the size and scale.
......@@ -574,7 +574,7 @@ TEST_F(SnapshotCacheTest, DeleteRetinaImages) {
NSString* const kSession = @"foo";
[cache setImage:image withSessionID:kSession];
FlushRunLoops(); // ensure the file is written to disk.
if (!IsIPadIdiom())
if (!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled())
[cache handleLowMemory];
// Verify the file was writted with @2x in the file name.
......
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