| 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" |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 13 #include "base/lazy_instance.h" |
| 13 #include "base/location.h" | 14 #include "base/location.h" |
| 14 #include "base/logging.h" | 15 #include "base/logging.h" |
| 15 #include "base/mac/bind_objc_block.h" | 16 #include "base/mac/bind_objc_block.h" |
| 16 #include "base/mac/objc_property_releaser.h" | 17 #include "base/mac/objc_property_releaser.h" |
| 17 #include "base/mac/scoped_cftyperef.h" | 18 #include "base/mac/scoped_cftyperef.h" |
| 18 #include "base/mac/scoped_nsobject.h" | 19 #include "base/mac/scoped_nsobject.h" |
| 19 #include "base/strings/sys_string_conversions.h" | 20 #include "base/strings/sys_string_conversions.h" |
| 20 #include "base/task_runner_util.h" | 21 #include "base/task_runner_util.h" |
| 22 #include "base/task_scheduler/post_task.h" |
| 21 #include "base/threading/thread_restrictions.h" | 23 #include "base/threading/thread_restrictions.h" |
| 22 #include "ios/chrome/browser/experimental_flags.h" | 24 #include "ios/chrome/browser/experimental_flags.h" |
| 23 #import "ios/chrome/browser/snapshots/lru_cache.h" | 25 #import "ios/chrome/browser/snapshots/lru_cache.h" |
| 24 #import "ios/chrome/browser/snapshots/snapshot_cache_internal.h" | 26 #import "ios/chrome/browser/snapshots/snapshot_cache_internal.h" |
| 25 #include "ios/chrome/browser/ui/ui_util.h" | 27 #include "ios/chrome/browser/ui/ui_util.h" |
| 26 #import "ios/chrome/browser/ui/uikit_ui_util.h" | 28 #import "ios/chrome/browser/ui/uikit_ui_util.h" |
| 27 #include "ios/web/public/web_thread.h" | 29 #include "ios/web/public/web_thread.h" |
| 28 | 30 |
| 29 @interface SnapshotCache () | 31 @interface SnapshotCache () |
| 30 // Returns the directory where the thumbnails are saved. | 32 // Returns the directory where the thumbnails are saved. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 45 // |mostRecentGreyBlock_| if |mostRecentGreySessionId_| matches |sessionID|. | 47 // |mostRecentGreyBlock_| if |mostRecentGreySessionId_| matches |sessionID|. |
| 46 - (void)saveGreyImage:(UIImage*)greyImage forKey:(NSString*)sessionID; | 48 - (void)saveGreyImage:(UIImage*)greyImage forKey:(NSString*)sessionID; |
| 47 @end | 49 @end |
| 48 | 50 |
| 49 namespace { | 51 namespace { |
| 50 static NSArray* const kSnapshotCacheDirectory = @[ @"Chromium", @"Snapshots" ]; | 52 static NSArray* const kSnapshotCacheDirectory = @[ @"Chromium", @"Snapshots" ]; |
| 51 | 53 |
| 52 const NSUInteger kCacheInitialCapacity = 100; | 54 const NSUInteger kCacheInitialCapacity = 100; |
| 53 const NSUInteger kGreyInitialCapacity = 8; | 55 const NSUInteger kGreyInitialCapacity = 8; |
| 54 const CGFloat kJPEGImageQuality = 1.0; // Highest quality. No compression. | 56 const CGFloat kJPEGImageQuality = 1.0; // Highest quality. No compression. |
| 55 // Sequence token to make sure creation/deletion of snapshots don't overlap. | |
| 56 const char kSequenceToken[] = "SnapshotCacheSequenceToken"; | |
| 57 // Maximum size in number of elements that the LRU cache can hold before | 57 // Maximum size in number of elements that the LRU cache can hold before |
| 58 // starting to evict elements. | 58 // starting to evict elements. |
| 59 const NSUInteger kLRUCacheMaxCapacity = 6; | 59 const NSUInteger kLRUCacheMaxCapacity = 6; |
| 60 | 60 |
| 61 struct SnapshotTaskRunner { |
| 62 const scoped_refptr<base::SequencedTaskRunner> task_runner = |
| 63 base::CreateSequencedTaskRunnerWithTraits( |
| 64 {base::MayBlock(), base::TaskPriority::USER_VISIBLE}); |
| 65 }; |
| 66 |
| 67 // Sequence token to make sure creation/deletion of snapshots don't overlap. |
| 68 base::LazyInstance<SnapshotTaskRunner>::Leaky g_snapshot_task_runner = |
| 69 LAZY_INSTANCE_INITIALIZER; |
| 70 |
| 61 // The paths of the images saved to disk, given a cache directory. | 71 // The paths of the images saved to disk, given a cache directory. |
| 62 base::FilePath FilePathForSessionID(NSString* sessionID, | 72 base::FilePath FilePathForSessionID(NSString* sessionID, |
| 63 const base::FilePath& directory) { | 73 const base::FilePath& directory) { |
| 64 base::FilePath path = directory.Append(base::SysNSStringToUTF8(sessionID)) | 74 base::FilePath path = directory.Append(base::SysNSStringToUTF8(sessionID)) |
| 65 .ReplaceExtension(".jpg"); | 75 .ReplaceExtension(".jpg"); |
| 66 if ([SnapshotCache snapshotScaleForDevice] == 2.0) { | 76 if ([SnapshotCache snapshotScaleForDevice] == 2.0) { |
| 67 path = path.InsertBeforeExtension("@2x"); | 77 path = path.InsertBeforeExtension("@2x"); |
| 68 } else if ([SnapshotCache snapshotScaleForDevice] == 3.0) { | 78 } else if ([SnapshotCache snapshotScaleForDevice] == 3.0) { |
| 69 path = path.InsertBeforeExtension("@3x"); | 79 path = path.InsertBeforeExtension("@3x"); |
| 70 } | 80 } |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 DCHECK_CURRENTLY_ON(web::WebThread::UI); | 278 DCHECK_CURRENTLY_ON(web::WebThread::UI); |
| 269 if (!img || !sessionID) | 279 if (!img || !sessionID) |
| 270 return; | 280 return; |
| 271 | 281 |
| 272 if (lruCache_) | 282 if (lruCache_) |
| 273 [lruCache_ setObject:img forKey:sessionID]; | 283 [lruCache_ setObject:img forKey:sessionID]; |
| 274 else | 284 else |
| 275 [imageDictionary_ setObject:img forKey:sessionID]; | 285 [imageDictionary_ setObject:img forKey:sessionID]; |
| 276 | 286 |
| 277 // Save the image to disk. | 287 // Save the image to disk. |
| 278 web::WebThread::PostBlockingPoolSequencedTask( | 288 g_snapshot_task_runner.Get().task_runner->PostTask( |
| 279 kSequenceToken, FROM_HERE, | 289 FROM_HERE, base::BindBlock(^{ |
| 280 base::BindBlock(^{ | |
| 281 base::scoped_nsobject<UIImage> image([img retain]); | 290 base::scoped_nsobject<UIImage> image([img retain]); |
| 282 WriteImageToDisk(image, | 291 WriteImageToDisk(image, |
| 283 [SnapshotCache imagePathForSessionID:sessionID]); | 292 [SnapshotCache imagePathForSessionID:sessionID]); |
| 284 })); | 293 })); |
| 285 } | 294 } |
| 286 | 295 |
| 287 - (void)removeImageWithSessionID:(NSString*)sessionID { | 296 - (void)removeImageWithSessionID:(NSString*)sessionID { |
| 288 DCHECK_CURRENTLY_ON(web::WebThread::UI); | 297 DCHECK_CURRENTLY_ON(web::WebThread::UI); |
| 289 if (lruCache_) | 298 if (lruCache_) |
| 290 [lruCache_ removeObjectForKey:sessionID]; | 299 [lruCache_ removeObjectForKey:sessionID]; |
| 291 else | 300 else |
| 292 [imageDictionary_ removeObjectForKey:sessionID]; | 301 [imageDictionary_ removeObjectForKey:sessionID]; |
| 293 | 302 |
| 294 web::WebThread::PostBlockingPoolSequencedTask( | 303 g_snapshot_task_runner.Get().task_runner->PostTask( |
| 295 kSequenceToken, FROM_HERE, | 304 FROM_HERE, base::BindBlock(^{ |
| 296 base::BindBlock(^{ | |
| 297 base::FilePath imagePath = | 305 base::FilePath imagePath = |
| 298 [SnapshotCache imagePathForSessionID:sessionID]; | 306 [SnapshotCache imagePathForSessionID:sessionID]; |
| 299 base::DeleteFile(imagePath, false); | 307 base::DeleteFile(imagePath, false); |
| 300 base::DeleteFile([SnapshotCache greyImagePathForSessionID:sessionID], | 308 base::DeleteFile([SnapshotCache greyImagePathForSessionID:sessionID], |
| 301 false); | 309 false); |
| 302 })); | 310 })); |
| 303 } | 311 } |
| 304 | 312 |
| 305 - (base::FilePath)oldCacheDirectory { | 313 - (base::FilePath)oldCacheDirectory { |
| 306 DCHECK_CURRENTLY_ON(web::WebThread::UI); | 314 DCHECK_CURRENTLY_ON(web::WebThread::UI); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 350 DCHECK(result); | 358 DCHECK(result); |
| 351 } | 359 } |
| 352 return GreyFilePathForSessionID(sessionID, path); | 360 return GreyFilePathForSessionID(sessionID, path); |
| 353 } | 361 } |
| 354 | 362 |
| 355 - (void)purgeCacheOlderThan:(const base::Time&)date | 363 - (void)purgeCacheOlderThan:(const base::Time&)date |
| 356 keeping:(NSSet*)liveSessionIds { | 364 keeping:(NSSet*)liveSessionIds { |
| 357 DCHECK_CURRENTLY_ON(web::WebThread::UI); | 365 DCHECK_CURRENTLY_ON(web::WebThread::UI); |
| 358 // Copying the date, as the block must copy the value, not the reference. | 366 // Copying the date, as the block must copy the value, not the reference. |
| 359 const base::Time dateCopy = date; | 367 const base::Time dateCopy = date; |
| 360 web::WebThread::PostBlockingPoolSequencedTask( | 368 g_snapshot_task_runner.Get().task_runner->PostTask( |
| 361 kSequenceToken, FROM_HERE, | 369 FROM_HERE, base::BindBlock(^{ |
| 362 base::BindBlock(^{ | |
| 363 std::set<base::FilePath> filesToKeep; | 370 std::set<base::FilePath> filesToKeep; |
| 364 for (NSString* sessionID : liveSessionIds) { | 371 for (NSString* sessionID : liveSessionIds) { |
| 365 base::FilePath curImagePath = | 372 base::FilePath curImagePath = |
| 366 [SnapshotCache imagePathForSessionID:sessionID]; | 373 [SnapshotCache imagePathForSessionID:sessionID]; |
| 367 filesToKeep.insert(curImagePath); | 374 filesToKeep.insert(curImagePath); |
| 368 filesToKeep.insert( | 375 filesToKeep.insert( |
| 369 [SnapshotCache greyImagePathForSessionID:sessionID]); | 376 [SnapshotCache greyImagePathForSessionID:sessionID]); |
| 370 } | 377 } |
| 371 base::FileEnumerator enumerator([SnapshotCache cacheDirectory], false, | 378 base::FileEnumerator enumerator([SnapshotCache cacheDirectory], false, |
| 372 base::FileEnumerator::FILES); | 379 base::FileEnumerator::FILES); |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 567 FilePathForSessionID(sessionID, [SnapshotCache cacheDirectory]); | 574 FilePathForSessionID(sessionID, [SnapshotCache cacheDirectory]); |
| 568 | 575 |
| 569 // The color image may still be in memory. Verify the sessionID matches. | 576 // The color image may still be in memory. Verify the sessionID matches. |
| 570 if (backgroundingColorImage_) { | 577 if (backgroundingColorImage_) { |
| 571 if (![backgroundingImageSessionId_ isEqualToString:sessionID]) { | 578 if (![backgroundingImageSessionId_ isEqualToString:sessionID]) { |
| 572 backgroundingColorImage_.reset(); | 579 backgroundingColorImage_.reset(); |
| 573 backgroundingImageSessionId_.reset(); | 580 backgroundingImageSessionId_.reset(); |
| 574 } | 581 } |
| 575 } | 582 } |
| 576 | 583 |
| 577 web::WebThread::PostBlockingPoolTask( | 584 base::PostTaskWithTraits( |
| 578 FROM_HERE, base::Bind(&ConvertAndSaveGreyImage, colorImagePath, | 585 FROM_HERE, {base::MayBlock(), base::TaskPriority::USER_VISIBLE}, |
| 579 greyImagePath, backgroundingColorImage_)); | 586 base::BindOnce(&ConvertAndSaveGreyImage, colorImagePath, greyImagePath, |
| 587 backgroundingColorImage_)); |
| 580 } | 588 } |
| 581 | 589 |
| 582 @end | 590 @end |
| 583 | 591 |
| 584 @implementation SnapshotCache (TestingAdditions) | 592 @implementation SnapshotCache (TestingAdditions) |
| 585 | 593 |
| 586 - (BOOL)hasImageInMemory:(NSString*)sessionID { | 594 - (BOOL)hasImageInMemory:(NSString*)sessionID { |
| 587 if ([self usesLRUCache]) | 595 if ([self usesLRUCache]) |
| 588 return [lruCache_ objectForKey:sessionID] != nil; | 596 return [lruCache_ objectForKey:sessionID] != nil; |
| 589 else | 597 else |
| 590 return [imageDictionary_ objectForKey:sessionID] != nil; | 598 return [imageDictionary_ objectForKey:sessionID] != nil; |
| 591 } | 599 } |
| 592 | 600 |
| 593 - (BOOL)hasGreyImageInMemory:(NSString*)sessionID { | 601 - (BOOL)hasGreyImageInMemory:(NSString*)sessionID { |
| 594 return [greyImageDictionary_ objectForKey:sessionID] != nil; | 602 return [greyImageDictionary_ objectForKey:sessionID] != nil; |
| 595 } | 603 } |
| 596 | 604 |
| 597 - (NSUInteger)lruCacheMaxSize { | 605 - (NSUInteger)lruCacheMaxSize { |
| 598 return [lruCache_ maxCacheSize]; | 606 return [lruCache_ maxCacheSize]; |
| 599 } | 607 } |
| 600 | 608 |
| 601 @end | 609 @end |
| OLD | NEW |