OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/chrome/browser/favicon/favicon_attributes_provider.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/mac/bind_objc_block.h" |
| 9 #include "base/strings/sys_string_conversions.h" |
| 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "base/task/cancelable_task_tracker.h" |
| 12 #include "components/favicon/core/fallback_url_util.h" |
| 13 #include "components/favicon/core/large_icon_service.h" |
| 14 #include "components/favicon_base/fallback_icon_style.h" |
| 15 #include "components/favicon_base/favicon_types.h" |
| 16 #include "skia/ext/skia_utils_ios.h" |
| 17 #include "url/gurl.h" |
| 18 |
| 19 @interface FaviconAttributesProvider () { |
| 20 // Used to cancel tasks for the LargeIconService. |
| 21 base::CancelableTaskTracker _faviconTaskTracker; |
| 22 } |
| 23 |
| 24 @end |
| 25 |
| 26 @implementation FaviconAttributesProvider |
| 27 @synthesize largeIconService = _largeIconService; |
| 28 @synthesize minSize = _minSize; |
| 29 @synthesize faviconSize = _faviconSize; |
| 30 |
| 31 - (instancetype)initWithFaviconSize:(CGFloat)faviconSize |
| 32 minFaviconSize:(CGFloat)minFaviconSize |
| 33 largeIconService: |
| 34 (favicon::LargeIconService*)largeIconService { |
| 35 DCHECK(largeIconService); |
| 36 self = [super init]; |
| 37 if (self) { |
| 38 _faviconSize = faviconSize; |
| 39 _minSize = minFaviconSize; |
| 40 _largeIconService = largeIconService; |
| 41 } |
| 42 |
| 43 return self; |
| 44 } |
| 45 |
| 46 - (void)fetchFaviconAttributesForURL:(const GURL&)URL |
| 47 completion:(void (^)(FaviconAttributes*))completion { |
| 48 GURL blockURL(URL); |
| 49 void (^faviconBlock)(const favicon_base::LargeIconResult&) = |
| 50 ^(const favicon_base::LargeIconResult& result) { |
| 51 FaviconAttributes* attributes = nil; |
| 52 if (result.bitmap.is_valid()) { |
| 53 scoped_refptr<base::RefCountedMemory> data = |
| 54 result.bitmap.bitmap_data.get(); |
| 55 UIImage* favicon = |
| 56 [UIImage imageWithData:[NSData dataWithBytes:data->front() |
| 57 length:data->size()]]; |
| 58 attributes = [FaviconAttributes attributesWithImage:favicon]; |
| 59 } else if (result.fallback_icon_style) { |
| 60 UIColor* backgroundColor = skia::UIColorFromSkColor( |
| 61 result.fallback_icon_style->background_color); |
| 62 UIColor* textColor = |
| 63 skia::UIColorFromSkColor(result.fallback_icon_style->text_color); |
| 64 NSString* monogram = |
| 65 base::SysUTF16ToNSString(favicon::GetFallbackIconText(blockURL)); |
| 66 attributes = |
| 67 [FaviconAttributes attributesWithMonogram:monogram |
| 68 textColor:textColor |
| 69 backgroundColor:backgroundColor]; |
| 70 } |
| 71 DCHECK(attributes); |
| 72 completion(attributes); |
| 73 }; |
| 74 |
| 75 // Always call LargeIconService in case the favicon was updated. |
| 76 CGFloat faviconSize = [UIScreen mainScreen].scale * self.faviconSize; |
| 77 CGFloat minFaviconSize = [UIScreen mainScreen].scale * self.minSize; |
| 78 self.largeIconService->GetLargeIconOrFallbackStyle( |
| 79 URL, minFaviconSize, faviconSize, base::BindBlock(faviconBlock), |
| 80 &_faviconTaskTracker); |
| 81 } |
| 82 @end |
OLD | NEW |