| 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 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
| 20 #error "This file requires ARC support." | |
| 21 #endif | |
| 22 | |
| 23 @interface FaviconAttributesProvider () { | |
| 24 // Used to cancel tasks for the LargeIconService. | |
| 25 base::CancelableTaskTracker _faviconTaskTracker; | |
| 26 } | |
| 27 | |
| 28 @end | |
| 29 | |
| 30 @implementation FaviconAttributesProvider | |
| 31 @synthesize largeIconService = _largeIconService; | |
| 32 @synthesize minSize = _minSize; | |
| 33 @synthesize faviconSize = _faviconSize; | |
| 34 | |
| 35 - (instancetype)initWithFaviconSize:(CGFloat)faviconSize | |
| 36 minFaviconSize:(CGFloat)minFaviconSize | |
| 37 largeIconService: | |
| 38 (favicon::LargeIconService*)largeIconService { | |
| 39 DCHECK(largeIconService); | |
| 40 self = [super init]; | |
| 41 if (self) { | |
| 42 _faviconSize = faviconSize; | |
| 43 _minSize = minFaviconSize; | |
| 44 _largeIconService = largeIconService; | |
| 45 } | |
| 46 | |
| 47 return self; | |
| 48 } | |
| 49 | |
| 50 - (void)fetchFaviconAttributesForURL:(const GURL&)URL | |
| 51 completion:(void (^)(FaviconAttributes*))completion { | |
| 52 GURL blockURL(URL); | |
| 53 void (^faviconBlock)(const favicon_base::LargeIconResult&) = | |
| 54 ^(const favicon_base::LargeIconResult& result) { | |
| 55 FaviconAttributes* attributes = nil; | |
| 56 if (result.bitmap.is_valid()) { | |
| 57 scoped_refptr<base::RefCountedMemory> data = | |
| 58 result.bitmap.bitmap_data.get(); | |
| 59 UIImage* favicon = | |
| 60 [UIImage imageWithData:[NSData dataWithBytes:data->front() | |
| 61 length:data->size()]]; | |
| 62 attributes = [FaviconAttributes attributesWithImage:favicon]; | |
| 63 } else if (result.fallback_icon_style) { | |
| 64 UIColor* backgroundColor = skia::UIColorFromSkColor( | |
| 65 result.fallback_icon_style->background_color); | |
| 66 UIColor* textColor = | |
| 67 skia::UIColorFromSkColor(result.fallback_icon_style->text_color); | |
| 68 NSString* monogram = | |
| 69 base::SysUTF16ToNSString(favicon::GetFallbackIconText(blockURL)); | |
| 70 attributes = | |
| 71 [FaviconAttributes attributesWithMonogram:monogram | |
| 72 textColor:textColor | |
| 73 backgroundColor:backgroundColor]; | |
| 74 } | |
| 75 DCHECK(attributes); | |
| 76 completion(attributes); | |
| 77 }; | |
| 78 | |
| 79 // Always call LargeIconService in case the favicon was updated. | |
| 80 CGFloat faviconSize = [UIScreen mainScreen].scale * self.faviconSize; | |
| 81 CGFloat minFaviconSize = [UIScreen mainScreen].scale * self.minSize; | |
| 82 self.largeIconService->GetLargeIconOrFallbackStyle( | |
| 83 URL, minFaviconSize, faviconSize, base::BindBlockArc(faviconBlock), | |
| 84 &_faviconTaskTracker); | |
| 85 } | |
| 86 @end | |
| OLD | NEW |