Chromium Code Reviews| Index: ios/chrome/browser/snapshots/snapshot_cache.mm |
| diff --git a/ios/chrome/browser/snapshots/snapshot_cache.mm b/ios/chrome/browser/snapshots/snapshot_cache.mm |
| index d6537ccab69c7978fa77567e978f2fe7d6a3923b..8e85668ae48271691c70ad82cb1c9771ead3f9f6 100644 |
| --- a/ios/chrome/browser/snapshots/snapshot_cache.mm |
| +++ b/ios/chrome/browser/snapshots/snapshot_cache.mm |
| @@ -18,13 +18,12 @@ |
| #include "base/task_runner_util.h" |
| #include "base/threading/thread_restrictions.h" |
| #include "ios/chrome/browser/experimental_flags.h" |
| +#import "ios/chrome/browser/snapshots/snapshot_cache_internal.h" |
| #include "ios/chrome/browser/ui/ui_util.h" |
| #import "ios/chrome/browser/ui/uikit_ui_util.h" |
| #include "ios/web/public/web_thread.h" |
| @interface SnapshotCache () |
| -+ (base::FilePath)imagePathForSessionID:(NSString*)sessionID; |
| -+ (base::FilePath)greyImagePathForSessionID:(NSString*)sessionID; |
| // Returns the directory where the thumbnails are saved. |
| + (base::FilePath)cacheDirectory; |
| // Returns the directory where the thumbnails were stored in M28 and earlier. |
| @@ -144,52 +143,45 @@ void ConvertAndSaveGreyImage( |
| DCHECK_CURRENTLY_ON(web::WebThread::UI); |
| propertyReleaser_SnapshotCache_.Init(self, [SnapshotCache class]); |
| - // Always use the LRUCache when the tab switcher is enabled. |
| - if (experimental_flags::IsTabSwitcherEnabled() || |
| - experimental_flags::IsLRUSnapshotCacheEnabled()) { |
| + if ([self usesLRUCache]) { |
| 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) |
| - 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]; |
| - } |
| + [[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 { |
| - if (!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled()) { |
| - [[NSNotificationCenter defaultCenter] |
| - removeObserver:self |
| - name:UIApplicationDidReceiveMemoryWarningNotification |
| - object:nil]; |
| - [[NSNotificationCenter defaultCenter] |
| - removeObserver:self |
| - name:UIApplicationDidEnterBackgroundNotification |
| - object:nil]; |
| - [[NSNotificationCenter defaultCenter] |
| - removeObserver:self |
| - name:UIApplicationDidBecomeActiveNotification |
| - object:nil]; |
| - } |
| + [[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]; |
| } |
| @@ -210,9 +202,7 @@ void ConvertAndSaveGreyImage( |
| DCHECK_CURRENTLY_ON(web::WebThread::UI); |
| DCHECK(sessionID); |
| - // Cache on iPad is enabled only when the tab switcher is enabled. |
| - if ((IsIPadIdiom() && !experimental_flags::IsTabSwitcherEnabled()) && |
| - !callback) |
| + if (![self inMemoryCacheIsEnabled] && !callback) |
| return; |
| UIImage* img = nil; |
| @@ -236,9 +226,7 @@ void ConvertAndSaveGreyImage( |
| [SnapshotCache imagePathForSessionID:sessionID]) retain]); |
| }), |
| base::BindBlock(^(base::scoped_nsobject<UIImage> image) { |
| - // Cache on iPad is enabled only when the tab switcher is enabled. |
| - if ((!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled()) && |
| - image) { |
| + if ([self inMemoryCacheIsEnabled] && image) { |
| if (lruCache_) |
| [lruCache_ setObject:image forKey:sessionID]; |
| else |
| @@ -254,8 +242,7 @@ void ConvertAndSaveGreyImage( |
| if (!img || !sessionID) |
| return; |
| - // Cache on iPad is enabled only when the tab switcher is enabled. |
| - if (!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled()) { |
| + if ([self inMemoryCacheIsEnabled]) { |
| if (lruCache_) |
| [lruCache_ setObject:img forKey:sessionID]; |
| else |
| @@ -386,8 +373,18 @@ void ConvertAndSaveGreyImage( |
| } |
| } |
| +- (BOOL)inMemoryCacheIsEnabled { |
| + // In-memory cache on iPad is enabled only when the tab switcher is enabled. |
| + return !IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled(); |
| +} |
| + |
| +- (BOOL)usesLRUCache { |
| + // Always use the LRUCache when the tab switcher is enabled. |
| + return experimental_flags::IsTabSwitcherEnabled() || |
| + experimental_flags::IsLRUSnapshotCacheEnabled(); |
| +} |
| + |
| - (void)handleLowMemory { |
| - DCHECK(!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled()); |
| DCHECK_CURRENTLY_ON(web::WebThread::UI); |
| base::scoped_nsobject<NSMutableDictionary> dictionary( |
| [[NSMutableDictionary alloc] initWithCapacity:2]); |
| @@ -411,14 +408,12 @@ void ConvertAndSaveGreyImage( |
| } |
| - (void)handleEnterBackground { |
| - DCHECK(!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled()); |
| DCHECK_CURRENTLY_ON(web::WebThread::UI); |
| [imageDictionary_ removeAllObjects]; |
| [lruCache_ removeAllObjects]; |
| } |
| - (void)handleBecomeActive { |
| - DCHECK(!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled()); |
| DCHECK_CURRENTLY_ON(web::WebThread::UI); |
| for (NSString* sessionID in pinnedIDs_) |
| [self retrieveImageForSessionID:sessionID callback:nil]; |
| @@ -569,11 +564,12 @@ void ConvertAndSaveGreyImage( |
| @implementation SnapshotCache (TestingAdditions) |
| - (BOOL)hasImageInMemory:(NSString*)sessionID { |
| - if (experimental_flags::IsLRUSnapshotCacheEnabled()) |
| + if ([self usesLRUCache]) |
|
sdefresne
2016/10/07 14:56:19
The test changed, is this expected?
jif
2016/10/24 15:10:03
Yes. It is now correct:
The LRU cache is used when
|
| return [lruCache_ objectForKey:sessionID] != nil; |
| else |
| return [imageDictionary_ objectForKey:sessionID] != nil; |
| } |
| + |
| - (BOOL)hasGreyImageInMemory:(NSString*)sessionID { |
| return [greyImageDictionary_ objectForKey:sessionID] != nil; |
| } |