| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/favicon/favicon_loader.h" | 5 #import "ios/chrome/browser/favicon/favicon_loader.h" |
| 6 | 6 |
| 7 #import <UIKit/UIKit.h> | 7 #import <UIKit/UIKit.h> |
| 8 | 8 |
| 9 #import "base/mac/foundation_util.h" | 9 #import "base/mac/foundation_util.h" |
| 10 #import "base/mac/scoped_block.h" | 10 #import "base/mac/scoped_block.h" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 // or a valid UIImage. | 46 // or a valid UIImage. |
| 47 if (value == [NSNull null]) | 47 if (value == [NSNull null]) |
| 48 return [UIImage imageNamed:@"default_favicon"]; | 48 return [UIImage imageNamed:@"default_favicon"]; |
| 49 return base::mac::ObjCCastStrict<UIImage>(value); | 49 return base::mac::ObjCCastStrict<UIImage>(value); |
| 50 } | 50 } |
| 51 | 51 |
| 52 // Kick off an async request for the favicon. | 52 // Kick off an async request for the favicon. |
| 53 if (favicon_service_) { | 53 if (favicon_service_) { |
| 54 int size = gfx::kFaviconSize * [UIScreen mainScreen].scale; | 54 int size = gfx::kFaviconSize * [UIScreen mainScreen].scale; |
| 55 | 55 |
| 56 scoped_ptr<RequestData> request_data(new RequestData(key, block)); | 56 std::unique_ptr<RequestData> request_data(new RequestData(key, block)); |
| 57 favicon_base::FaviconResultsCallback callback = | 57 favicon_base::FaviconResultsCallback callback = |
| 58 base::Bind(&FaviconLoader::OnFaviconAvailable, base::Unretained(this), | 58 base::Bind(&FaviconLoader::OnFaviconAvailable, base::Unretained(this), |
| 59 base::Passed(&request_data)); | 59 base::Passed(&request_data)); |
| 60 favicon_service_->GetFaviconForPageURL(url, types, size, callback, | 60 favicon_service_->GetFaviconForPageURL(url, types, size, callback, |
| 61 &cancelable_task_tracker_); | 61 &cancelable_task_tracker_); |
| 62 } | 62 } |
| 63 | 63 |
| 64 return [UIImage imageNamed:@"default_favicon"]; | 64 return [UIImage imageNamed:@"default_favicon"]; |
| 65 } | 65 } |
| 66 | 66 |
| 67 void FaviconLoader::PurgeCache() { | 67 void FaviconLoader::PurgeCache() { |
| 68 DCHECK(thread_checker_.CalledOnValidThread()); | 68 DCHECK(thread_checker_.CalledOnValidThread()); |
| 69 cancelable_task_tracker_.TryCancelAll(); | 69 cancelable_task_tracker_.TryCancelAll(); |
| 70 favicon_cache_.reset( | 70 favicon_cache_.reset( |
| 71 [[NSMutableDictionary dictionaryWithCapacity:10] retain]); | 71 [[NSMutableDictionary dictionaryWithCapacity:10] retain]); |
| 72 } | 72 } |
| 73 | 73 |
| 74 void FaviconLoader::OnFaviconAvailable( | 74 void FaviconLoader::OnFaviconAvailable( |
| 75 scoped_ptr<RequestData> request_data, | 75 std::unique_ptr<RequestData> request_data, |
| 76 const std::vector<favicon_base::FaviconRawBitmapResult>& | 76 const std::vector<favicon_base::FaviconRawBitmapResult>& |
| 77 favicon_bitmap_results) { | 77 favicon_bitmap_results) { |
| 78 DCHECK(request_data); | 78 DCHECK(request_data); |
| 79 DCHECK(thread_checker_.CalledOnValidThread()); | 79 DCHECK(thread_checker_.CalledOnValidThread()); |
| 80 if (favicon_bitmap_results.size() < 1 || | 80 if (favicon_bitmap_results.size() < 1 || |
| 81 !favicon_bitmap_results[0].is_valid()) { | 81 !favicon_bitmap_results[0].is_valid()) { |
| 82 // Return early if there were no results or if it is invalid, after adding a | 82 // Return early if there were no results or if it is invalid, after adding a |
| 83 // "no favicon" entry to the cache so that we don't keep trying to fetch a | 83 // "no favicon" entry to the cache so that we don't keep trying to fetch a |
| 84 // missing favicon over and over. | 84 // missing favicon over and over. |
| 85 [favicon_cache_ setObject:[NSNull null] forKey:request_data->key]; | 85 [favicon_cache_ setObject:[NSNull null] forKey:request_data->key]; |
| 86 return; | 86 return; |
| 87 } | 87 } |
| 88 | 88 |
| 89 // The favicon code assumes favicons are PNG-encoded. | 89 // The favicon code assumes favicons are PNG-encoded. |
| 90 NSData* image_data = | 90 NSData* image_data = |
| 91 [NSData dataWithBytes:favicon_bitmap_results[0].bitmap_data->front() | 91 [NSData dataWithBytes:favicon_bitmap_results[0].bitmap_data->front() |
| 92 length:favicon_bitmap_results[0].bitmap_data->size()]; | 92 length:favicon_bitmap_results[0].bitmap_data->size()]; |
| 93 UIImage* favicon = | 93 UIImage* favicon = |
| 94 [UIImage imageWithData:image_data scale:[[UIScreen mainScreen] scale]]; | 94 [UIImage imageWithData:image_data scale:[[UIScreen mainScreen] scale]]; |
| 95 [favicon_cache_ setObject:favicon forKey:request_data->key]; | 95 [favicon_cache_ setObject:favicon forKey:request_data->key]; |
| 96 | 96 |
| 97 // Call the block to tell the caller this is complete. | 97 // Call the block to tell the caller this is complete. |
| 98 if (request_data->block) | 98 if (request_data->block) |
| 99 (request_data->block.get())(favicon); | 99 (request_data->block.get())(favicon); |
| 100 } | 100 } |
| OLD | NEW |