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