| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "ios/chrome/browser/snapshots/snapshot_cache.h" | 5 #import "ios/chrome/browser/snapshots/snapshot_cache.h" |
| 6 | 6 |
| 7 #import <UIKit/UIKit.h> | 7 #import <UIKit/UIKit.h> |
| 8 | 8 |
| 9 #include "base/critical_closure.h" | 9 #include "base/critical_closure.h" |
| 10 #include "base/files/file_enumerator.h" | 10 #include "base/files/file_enumerator.h" |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 } | 225 } |
| 226 // Cap snapshot resolution to 2x to reduce the amount of memory they use. | 226 // Cap snapshot resolution to 2x to reduce the amount of memory they use. |
| 227 return MIN([UIScreen mainScreen].scale, 2.0); | 227 return MIN([UIScreen mainScreen].scale, 2.0); |
| 228 } | 228 } |
| 229 | 229 |
| 230 - (void)retrieveImageForSessionID:(NSString*)sessionID | 230 - (void)retrieveImageForSessionID:(NSString*)sessionID |
| 231 callback:(void (^)(UIImage*))callback { | 231 callback:(void (^)(UIImage*))callback { |
| 232 DCHECK_CURRENTLY_ON(web::WebThread::UI); | 232 DCHECK_CURRENTLY_ON(web::WebThread::UI); |
| 233 DCHECK(sessionID); | 233 DCHECK(sessionID); |
| 234 | 234 |
| 235 if (![self inMemoryCacheIsEnabled] && !callback) | |
| 236 return; | |
| 237 | |
| 238 UIImage* img = nil; | 235 UIImage* img = nil; |
| 239 if (lruCache_) | 236 if (lruCache_) |
| 240 img = [lruCache_ objectForKey:sessionID]; | 237 img = [lruCache_ objectForKey:sessionID]; |
| 241 else | 238 else |
| 242 img = [imageDictionary_ objectForKey:sessionID]; | 239 img = [imageDictionary_ objectForKey:sessionID]; |
| 243 | 240 |
| 244 if (img) { | 241 if (img) { |
| 245 if (callback) | 242 if (callback) |
| 246 callback(img); | 243 callback(img); |
| 247 return; | 244 return; |
| 248 } | 245 } |
| 249 | 246 |
| 250 base::PostTaskAndReplyWithResult( | 247 base::PostTaskAndReplyWithResult( |
| 251 web::WebThread::GetTaskRunnerForThread(web::WebThread::FILE_USER_BLOCKING) | 248 web::WebThread::GetTaskRunnerForThread(web::WebThread::FILE_USER_BLOCKING) |
| 252 .get(), | 249 .get(), |
| 253 FROM_HERE, base::BindBlock(^base::scoped_nsobject<UIImage>() { | 250 FROM_HERE, base::BindBlock(^base::scoped_nsobject<UIImage>() { |
| 254 // Retrieve the image on a high priority thread. | 251 // Retrieve the image on a high priority thread. |
| 255 return base::scoped_nsobject<UIImage>([ReadImageFromDisk( | 252 return base::scoped_nsobject<UIImage>([ReadImageFromDisk( |
| 256 [SnapshotCache imagePathForSessionID:sessionID]) retain]); | 253 [SnapshotCache imagePathForSessionID:sessionID]) retain]); |
| 257 }), | 254 }), |
| 258 base::BindBlock(^(base::scoped_nsobject<UIImage> image) { | 255 base::BindBlock(^(base::scoped_nsobject<UIImage> image) { |
| 259 if ([self inMemoryCacheIsEnabled] && image) { | 256 if (image) { |
| 260 if (lruCache_) | 257 if (lruCache_) |
| 261 [lruCache_ setObject:image forKey:sessionID]; | 258 [lruCache_ setObject:image forKey:sessionID]; |
| 262 else | 259 else |
| 263 [imageDictionary_ setObject:image forKey:sessionID]; | 260 [imageDictionary_ setObject:image forKey:sessionID]; |
| 264 } | 261 } |
| 265 if (callback) | 262 if (callback) |
| 266 callback(image); | 263 callback(image); |
| 267 })); | 264 })); |
| 268 } | 265 } |
| 269 | 266 |
| 270 - (void)setImage:(UIImage*)img withSessionID:(NSString*)sessionID { | 267 - (void)setImage:(UIImage*)img withSessionID:(NSString*)sessionID { |
| 271 DCHECK_CURRENTLY_ON(web::WebThread::UI); | 268 DCHECK_CURRENTLY_ON(web::WebThread::UI); |
| 272 if (!img || !sessionID) | 269 if (!img || !sessionID) |
| 273 return; | 270 return; |
| 274 | 271 |
| 275 if ([self inMemoryCacheIsEnabled]) { | 272 if (lruCache_) |
| 276 if (lruCache_) | 273 [lruCache_ setObject:img forKey:sessionID]; |
| 277 [lruCache_ setObject:img forKey:sessionID]; | 274 else |
| 278 else | 275 [imageDictionary_ setObject:img forKey:sessionID]; |
| 279 [imageDictionary_ setObject:img forKey:sessionID]; | 276 |
| 280 } | |
| 281 // Save the image to disk. | 277 // Save the image to disk. |
| 282 web::WebThread::PostBlockingPoolSequencedTask( | 278 web::WebThread::PostBlockingPoolSequencedTask( |
| 283 kSequenceToken, FROM_HERE, | 279 kSequenceToken, FROM_HERE, |
| 284 base::BindBlock(^{ | 280 base::BindBlock(^{ |
| 285 base::scoped_nsobject<UIImage> image([img retain]); | 281 base::scoped_nsobject<UIImage> image([img retain]); |
| 286 WriteImageToDisk(image, | 282 WriteImageToDisk(image, |
| 287 [SnapshotCache imagePathForSessionID:sessionID]); | 283 [SnapshotCache imagePathForSessionID:sessionID]); |
| 288 })); | 284 })); |
| 289 } | 285 } |
| 290 | 286 |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 396 return; | 392 return; |
| 397 backgroundingImageSessionId_.reset([sessionID copy]); | 393 backgroundingImageSessionId_.reset([sessionID copy]); |
| 398 if (lruCache_) { | 394 if (lruCache_) { |
| 399 backgroundingColorImage_.reset([[lruCache_ objectForKey:sessionID] retain]); | 395 backgroundingColorImage_.reset([[lruCache_ objectForKey:sessionID] retain]); |
| 400 } else { | 396 } else { |
| 401 backgroundingColorImage_.reset( | 397 backgroundingColorImage_.reset( |
| 402 [[imageDictionary_ objectForKey:sessionID] retain]); | 398 [[imageDictionary_ objectForKey:sessionID] retain]); |
| 403 } | 399 } |
| 404 } | 400 } |
| 405 | 401 |
| 406 - (BOOL)inMemoryCacheIsEnabled { | |
| 407 // In-memory cache on iPad is enabled only when the tab switcher is enabled. | |
| 408 return !IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled(); | |
| 409 } | |
| 410 | |
| 411 - (BOOL)usesLRUCache { | 402 - (BOOL)usesLRUCache { |
| 412 // Always use the LRUCache when the tab switcher is enabled. | 403 // TODO(crbug.com/687904): Remove the non-LRU cache code. |
| 413 return experimental_flags::IsTabSwitcherEnabled() || | 404 return true; |
| 414 experimental_flags::IsLRUSnapshotCacheEnabled(); | |
| 415 } | 405 } |
| 416 | 406 |
| 417 - (void)handleLowMemory { | 407 - (void)handleLowMemory { |
| 418 DCHECK_CURRENTLY_ON(web::WebThread::UI); | 408 DCHECK_CURRENTLY_ON(web::WebThread::UI); |
| 419 base::scoped_nsobject<NSMutableDictionary> dictionary( | 409 base::scoped_nsobject<NSMutableDictionary> dictionary( |
| 420 [[NSMutableDictionary alloc] initWithCapacity:2]); | 410 [[NSMutableDictionary alloc] initWithCapacity:2]); |
| 421 for (NSString* sessionID in pinnedIDs_) { | 411 for (NSString* sessionID in pinnedIDs_) { |
| 422 UIImage* image = nil; | 412 UIImage* image = nil; |
| 423 if (lruCache_) | 413 if (lruCache_) |
| 424 image = [lruCache_ objectForKey:sessionID]; | 414 image = [lruCache_ objectForKey:sessionID]; |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 602 | 592 |
| 603 - (BOOL)hasGreyImageInMemory:(NSString*)sessionID { | 593 - (BOOL)hasGreyImageInMemory:(NSString*)sessionID { |
| 604 return [greyImageDictionary_ objectForKey:sessionID] != nil; | 594 return [greyImageDictionary_ objectForKey:sessionID] != nil; |
| 605 } | 595 } |
| 606 | 596 |
| 607 - (NSUInteger)lruCacheMaxSize { | 597 - (NSUInteger)lruCacheMaxSize { |
| 608 return [lruCache_ maxCacheSize]; | 598 return [lruCache_ maxCacheSize]; |
| 609 } | 599 } |
| 610 | 600 |
| 611 @end | 601 @end |
| OLD | NEW |