Chromium Code Reviews| 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 _Nonnull) | |
| 32 initWithFaviconSize:(CGFloat)faviconSize | |
| 33 minFaviconSize:(CGFloat)minFaviconSize | |
| 34 largeIconService:(favicon::LargeIconService* _Nonnull)largeIconService { | |
| 35 DCHECK(largeIconService); | |
|
jif
2016/09/26 10:58:13
is the DCHECK necessary if largeIconService is _No
stkhapugin
2016/09/26 15:51:36
Yes, because nullability markers are not tightly e
| |
| 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: | |
| 48 (void (^_Nonnull)(FaviconAttributes* _Nonnull)) | |
| 49 completion { | |
| 50 GURL blockURL(URL); | |
| 51 void (^faviconBlock)(const favicon_base::LargeIconResult&) = | |
| 52 ^(const favicon_base::LargeIconResult& result) { | |
| 53 FaviconAttributes* attributes = nil; | |
| 54 if (result.bitmap.is_valid()) { | |
| 55 scoped_refptr<base::RefCountedMemory> data = | |
| 56 result.bitmap.bitmap_data.get(); | |
| 57 UIImage* favicon = | |
| 58 [UIImage imageWithData:[NSData dataWithBytes:data->front() | |
| 59 length:data->size()]]; | |
| 60 attributes = [FaviconAttributes attributesWithImage:favicon]; | |
| 61 } else if (result.fallback_icon_style) { | |
| 62 UIColor* backgroundColor = skia::UIColorFromSkColor( | |
| 63 result.fallback_icon_style->background_color); | |
| 64 UIColor* textColor = | |
| 65 skia::UIColorFromSkColor(result.fallback_icon_style->text_color); | |
| 66 NSString* monogram = | |
| 67 base::SysUTF16ToNSString(favicon::GetFallbackIconText(blockURL)); | |
| 68 attributes = | |
| 69 [FaviconAttributes attributesWithMonogram:monogram | |
| 70 textColor:textColor | |
| 71 backgroundColor:backgroundColor]; | |
| 72 } | |
| 73 DCHECK(attributes); | |
| 74 completion(attributes); | |
| 75 }; | |
| 76 | |
| 77 // Always call LargeIconService in case the favicon was updated. | |
| 78 CGFloat faviconSize = [UIScreen mainScreen].scale * self.faviconSize; | |
| 79 CGFloat minFaviconSize = [UIScreen mainScreen].scale * self.minSize; | |
| 80 self.largeIconService->GetLargeIconOrFallbackStyle( | |
| 81 URL, minFaviconSize, faviconSize, base::BindBlock(faviconBlock), | |
| 82 &_faviconTaskTracker); | |
| 83 } | |
| 84 @end | |
| OLD | NEW |