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::GetMessageLoopProxyForThread( | 215 web::WebThread::GetTaskRunnerForThread(web::WebThread::FILE_USER_BLOCKING) |
216 web::WebThread::FILE_USER_BLOCKING).get(), | 216 .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::GetMessageLoopProxyForThread( | 404 web::WebThread::GetTaskRunnerForThread(web::WebThread::FILE_USER_BLOCKING) |
405 web::WebThread::FILE_USER_BLOCKING).get(), | 405 .get(), |
406 FROM_HERE, | 406 FROM_HERE, base::BindBlock(^base::scoped_nsobject<UIImage>() { |
407 base::BindBlock(^base::scoped_nsobject<UIImage>() { | |
408 base::scoped_nsobject<UIImage> result([img retain]); | 407 base::scoped_nsobject<UIImage> result([img retain]); |
409 // If the image is not in the cache, load it from disk. | 408 // If the image is not in the cache, load it from disk. |
410 if (!result) | 409 if (!result) |
411 result.reset([ReadImageFromDisk( | 410 result.reset([ReadImageFromDisk( |
412 [SnapshotCache imagePathForSessionID:sessionID]) retain]); | 411 [SnapshotCache imagePathForSessionID:sessionID]) retain]); |
413 if (result) | 412 if (result) |
414 result.reset([GreyImage(result) retain]); | 413 result.reset([GreyImage(result) retain]); |
415 return result; | 414 return result; |
416 }), | 415 }), |
417 base::BindBlock(^(base::scoped_nsobject<UIImage> greyImage) { | 416 base::BindBlock(^(base::scoped_nsobject<UIImage> greyImage) { |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
458 DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI); | 457 DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI); |
459 if (greyImageDictionary_) { | 458 if (greyImageDictionary_) { |
460 UIImage* image = [greyImageDictionary_ objectForKey:sessionID]; | 459 UIImage* image = [greyImageDictionary_ objectForKey:sessionID]; |
461 if (image) { | 460 if (image) { |
462 callback(image); | 461 callback(image); |
463 return; | 462 return; |
464 } | 463 } |
465 } | 464 } |
466 | 465 |
467 base::PostTaskAndReplyWithResult( | 466 base::PostTaskAndReplyWithResult( |
468 web::WebThread::GetMessageLoopProxyForThread( | 467 web::WebThread::GetTaskRunnerForThread(web::WebThread::FILE_USER_BLOCKING) |
469 web::WebThread::FILE_USER_BLOCKING).get(), | 468 .get(), |
470 FROM_HERE, | 469 FROM_HERE, base::BindBlock(^base::scoped_nsobject<UIImage>() { |
471 base::BindBlock(^base::scoped_nsobject<UIImage>() { | |
472 // Retrieve the image on a high priority thread. | 470 // Retrieve the image on a high priority thread. |
473 // Loading the file into NSData is more reliable. | 471 // Loading the file into NSData is more reliable. |
474 // -imageWithContentsOfFile would ocassionally claim the image was not a | 472 // -imageWithContentsOfFile would ocassionally claim the image was not a |
475 // valid jpg. | 473 // valid jpg. |
476 // "ImageIO: <ERROR> JPEGNot a JPEG file: starts with 0xff 0xd9" | 474 // "ImageIO: <ERROR> JPEGNot a JPEG file: starts with 0xff 0xd9" |
477 // See | 475 // See |
478 // http://stackoverflow.com/questions/5081297/ios-uiimagejpegrepresentat
ion-error-not-a-jpeg-file-starts-with-0xff-0xd9 | 476 // http://stackoverflow.com/questions/5081297/ios-uiimagejpegrepresentat
ion-error-not-a-jpeg-file-starts-with-0xff-0xd9 |
479 NSData* imageData = [NSData | 477 NSData* imageData = [NSData |
480 dataWithContentsOfFile:base::SysUTF8ToNSString( | 478 dataWithContentsOfFile:base::SysUTF8ToNSString( |
481 [SnapshotCache greyImagePathForSessionID:sessionID].value())]; | 479 [SnapshotCache greyImagePathForSessionID:sessionID].value())]; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
515 backgroundingImageSessionId_.reset(); | 513 backgroundingImageSessionId_.reset(); |
516 } | 514 } |
517 } | 515 } |
518 | 516 |
519 web::WebThread::PostBlockingPoolTask( | 517 web::WebThread::PostBlockingPoolTask( |
520 FROM_HERE, base::Bind(&ConvertAndSaveGreyImage, colorImagePath, | 518 FROM_HERE, base::Bind(&ConvertAndSaveGreyImage, colorImagePath, |
521 greyImagePath, backgroundingColorImage_)); | 519 greyImagePath, backgroundingColorImage_)); |
522 } | 520 } |
523 | 521 |
524 @end | 522 @end |
OLD | NEW |