| 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 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 return; | 205 return; |
| 206 | 206 |
| 207 UIImage* img = [imageDictionary_ objectForKey:sessionID]; | 207 UIImage* img = [imageDictionary_ objectForKey:sessionID]; |
| 208 if (img) { | 208 if (img) { |
| 209 if (callback) | 209 if (callback) |
| 210 callback(img); | 210 callback(img); |
| 211 return; | 211 return; |
| 212 } | 212 } |
| 213 | 213 |
| 214 base::PostTaskAndReplyWithResult( | 214 base::PostTaskAndReplyWithResult( |
| 215 web::WebThread::GetTaskRunnerForThread(web::WebThread::FILE_USER_BLOCKING) | 215 web::WebThread::GetMessageLoopProxyForThread( |
| 216 .get(), | 216 web::WebThread::FILE_USER_BLOCKING).get(), |
| 217 FROM_HERE, base::BindBlock(^base::scoped_nsobject<UIImage>() { | 217 FROM_HERE, base::BindBlock(^base::scoped_nsobject<UIImage>() { |
| 218 // Retrieve the image on a high priority thread. | 218 // Retrieve the image on a high priority thread. |
| 219 return base::scoped_nsobject<UIImage>([ReadImageFromDisk( | 219 return base::scoped_nsobject<UIImage>([ReadImageFromDisk( |
| 220 [SnapshotCache imagePathForSessionID:sessionID]) retain]); | 220 [SnapshotCache imagePathForSessionID:sessionID]) retain]); |
| 221 }), | 221 }), |
| 222 base::BindBlock(^(base::scoped_nsobject<UIImage> image) { | 222 base::BindBlock(^(base::scoped_nsobject<UIImage> image) { |
| 223 // The iPad tab switcher is currently using its own memory cache so the | 223 // The iPad tab switcher is currently using its own memory cache so the |
| 224 // image is not stored in memory here if running on iPad. | 224 // image is not stored in memory here if running on iPad. |
| 225 // The same logic is used on image writes (code below). | 225 // The same logic is used on image writes (code below). |
| 226 if (!IsIPadIdiom() && image) | 226 if (!IsIPadIdiom() && image) |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 } | 394 } |
| 395 } | 395 } |
| 396 | 396 |
| 397 - (void)loadGreyImageAsync:(NSString*)sessionID { | 397 - (void)loadGreyImageAsync:(NSString*)sessionID { |
| 398 DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI); | 398 DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI); |
| 399 // Don't call -retrieveImageForSessionID here because it caches the colored | 399 // Don't call -retrieveImageForSessionID here because it caches the colored |
| 400 // image, which we don't need for the grey image cache. But if the image is | 400 // image, which we don't need for the grey image cache. But if the image is |
| 401 // already in the cache, use it. | 401 // already in the cache, use it. |
| 402 UIImage* img = [imageDictionary_ objectForKey:sessionID]; | 402 UIImage* img = [imageDictionary_ objectForKey:sessionID]; |
| 403 base::PostTaskAndReplyWithResult( | 403 base::PostTaskAndReplyWithResult( |
| 404 web::WebThread::GetTaskRunnerForThread(web::WebThread::FILE_USER_BLOCKING) | 404 web::WebThread::GetMessageLoopProxyForThread( |
| 405 .get(), | 405 web::WebThread::FILE_USER_BLOCKING).get(), |
| 406 FROM_HERE, base::BindBlock(^base::scoped_nsobject<UIImage>() { | 406 FROM_HERE, |
| 407 base::BindBlock(^base::scoped_nsobject<UIImage>() { |
| 407 base::scoped_nsobject<UIImage> result([img retain]); | 408 base::scoped_nsobject<UIImage> result([img retain]); |
| 408 // If the image is not in the cache, load it from disk. | 409 // If the image is not in the cache, load it from disk. |
| 409 if (!result) | 410 if (!result) |
| 410 result.reset([ReadImageFromDisk( | 411 result.reset([ReadImageFromDisk( |
| 411 [SnapshotCache imagePathForSessionID:sessionID]) retain]); | 412 [SnapshotCache imagePathForSessionID:sessionID]) retain]); |
| 412 if (result) | 413 if (result) |
| 413 result.reset([GreyImage(result) retain]); | 414 result.reset([GreyImage(result) retain]); |
| 414 return result; | 415 return result; |
| 415 }), | 416 }), |
| 416 base::BindBlock(^(base::scoped_nsobject<UIImage> greyImage) { | 417 base::BindBlock(^(base::scoped_nsobject<UIImage> greyImage) { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 457 DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI); | 458 DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI); |
| 458 if (greyImageDictionary_) { | 459 if (greyImageDictionary_) { |
| 459 UIImage* image = [greyImageDictionary_ objectForKey:sessionID]; | 460 UIImage* image = [greyImageDictionary_ objectForKey:sessionID]; |
| 460 if (image) { | 461 if (image) { |
| 461 callback(image); | 462 callback(image); |
| 462 return; | 463 return; |
| 463 } | 464 } |
| 464 } | 465 } |
| 465 | 466 |
| 466 base::PostTaskAndReplyWithResult( | 467 base::PostTaskAndReplyWithResult( |
| 467 web::WebThread::GetTaskRunnerForThread(web::WebThread::FILE_USER_BLOCKING) | 468 web::WebThread::GetMessageLoopProxyForThread( |
| 468 .get(), | 469 web::WebThread::FILE_USER_BLOCKING).get(), |
| 469 FROM_HERE, base::BindBlock(^base::scoped_nsobject<UIImage>() { | 470 FROM_HERE, |
| 471 base::BindBlock(^base::scoped_nsobject<UIImage>() { |
| 470 // Retrieve the image on a high priority thread. | 472 // Retrieve the image on a high priority thread. |
| 471 // Loading the file into NSData is more reliable. | 473 // Loading the file into NSData is more reliable. |
| 472 // -imageWithContentsOfFile would ocassionally claim the image was not a | 474 // -imageWithContentsOfFile would ocassionally claim the image was not a |
| 473 // valid jpg. | 475 // valid jpg. |
| 474 // "ImageIO: <ERROR> JPEGNot a JPEG file: starts with 0xff 0xd9" | 476 // "ImageIO: <ERROR> JPEGNot a JPEG file: starts with 0xff 0xd9" |
| 475 // See | 477 // See |
| 476 // http://stackoverflow.com/questions/5081297/ios-uiimagejpegrepresentat
ion-error-not-a-jpeg-file-starts-with-0xff-0xd9 | 478 // http://stackoverflow.com/questions/5081297/ios-uiimagejpegrepresentat
ion-error-not-a-jpeg-file-starts-with-0xff-0xd9 |
| 477 NSData* imageData = [NSData | 479 NSData* imageData = [NSData |
| 478 dataWithContentsOfFile:base::SysUTF8ToNSString( | 480 dataWithContentsOfFile:base::SysUTF8ToNSString( |
| 479 [SnapshotCache greyImagePathForSessionID:sessionID].value())]; | 481 [SnapshotCache greyImagePathForSessionID:sessionID].value())]; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 513 backgroundingImageSessionId_.reset(); | 515 backgroundingImageSessionId_.reset(); |
| 514 } | 516 } |
| 515 } | 517 } |
| 516 | 518 |
| 517 web::WebThread::PostBlockingPoolTask( | 519 web::WebThread::PostBlockingPoolTask( |
| 518 FROM_HERE, base::Bind(&ConvertAndSaveGreyImage, colorImagePath, | 520 FROM_HERE, base::Bind(&ConvertAndSaveGreyImage, colorImagePath, |
| 519 greyImagePath, backgroundingColorImage_)); | 521 greyImagePath, backgroundingColorImage_)); |
| 520 } | 522 } |
| 521 | 523 |
| 522 @end | 524 @end |
| OLD | NEW |