| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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/ui/history/favicon_view_provider.h" | 5 #import "ios/chrome/browser/ui/history/favicon_view_provider.h" |
| 6 | 6 |
| 7 #include "base/i18n/case_conversion.h" | 7 #include "base/i18n/case_conversion.h" |
| 8 #include "base/ios/weak_nsobject.h" | 8 #include "base/ios/weak_nsobject.h" |
| 9 #include "base/mac/bind_objc_block.h" | 9 #include "base/mac/bind_objc_block.h" |
| 10 #import "base/mac/foundation_util.h" | 10 #import "base/mac/foundation_util.h" |
| 11 #include "base/mac/objc_property_releaser.h" | |
| 12 #include "base/mac/scoped_nsobject.h" | 11 #include "base/mac/scoped_nsobject.h" |
| 13 #include "base/memory/ref_counted_memory.h" | 12 #include "base/memory/ref_counted_memory.h" |
| 14 #include "base/strings/sys_string_conversions.h" | 13 #include "base/strings/sys_string_conversions.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 16 #include "base/task/cancelable_task_tracker.h" | 15 #include "base/task/cancelable_task_tracker.h" |
| 17 #include "components/favicon/core/fallback_url_util.h" | 16 #include "components/favicon/core/fallback_url_util.h" |
| 18 #include "components/favicon/core/large_icon_service.h" | 17 #include "components/favicon/core/large_icon_service.h" |
| 19 #include "components/favicon_base/fallback_icon_style.h" | 18 #include "components/favicon_base/fallback_icon_style.h" |
| 20 #include "components/favicon_base/favicon_types.h" | 19 #include "components/favicon_base/favicon_types.h" |
| 21 #import "ios/chrome/browser/ui/history/favicon_view.h" | 20 #import "ios/chrome/browser/ui/history/favicon_view.h" |
| 22 #import "ios/third_party/material_roboto_font_loader_ios/src/src/MaterialRobotoF
ontLoader.h" | 21 #import "ios/third_party/material_roboto_font_loader_ios/src/src/MaterialRobotoF
ontLoader.h" |
| 23 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 22 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| 24 #include "skia/ext/skia_utils_ios.h" | 23 #include "skia/ext/skia_utils_ios.h" |
| 25 #include "url/gurl.h" | 24 #include "url/gurl.h" |
| 26 | 25 |
| 26 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 27 #error "This file requires ARC support." |
| 28 #endif |
| 29 |
| 27 @interface FaviconViewProvider () { | 30 @interface FaviconViewProvider () { |
| 28 // Property releaser for FaviconViewProvider. | |
| 29 base::mac::ObjCPropertyReleaser _propertyReleaser_FaviconViewProvider; | |
| 30 // Delegate for handling completion of favicon load. | 31 // Delegate for handling completion of favicon load. |
| 31 base::WeakNSProtocol<id<FaviconViewProviderDelegate>> _delegate; | 32 base::WeakNSProtocol<id<FaviconViewProviderDelegate>> _delegate; |
| 32 // Used to cancel tasks for the LargeIconService. | 33 // Used to cancel tasks for the LargeIconService. |
| 33 base::CancelableTaskTracker _faviconTaskTracker; | 34 base::CancelableTaskTracker _faviconTaskTracker; |
| 34 // View that renders a favicon or a fallback image. | 35 // View that renders a favicon or a fallback image. |
| 35 base::scoped_nsobject<FaviconView> _faviconView; | 36 base::scoped_nsobject<FaviconView> _faviconView; |
| 36 } | 37 } |
| 37 | 38 |
| 38 // Size to render the favicon. | 39 // Size to render the favicon. |
| 39 @property(nonatomic, assign) CGFloat faviconSize; | 40 @property(nonatomic, assign) CGFloat faviconSize; |
| 40 // Favicon image for the favicon view. | 41 // Favicon image for the favicon view. |
| 41 @property(nonatomic, retain) UIImage* favicon; | 42 @property(nonatomic, strong) UIImage* favicon; |
| 42 // Fallback text for the favicon view if there is no appropriately sized | 43 // Fallback text for the favicon view if there is no appropriately sized |
| 43 // favicon availabile. | 44 // favicon availabile. |
| 44 @property(nonatomic, copy) NSString* fallbackText; | 45 @property(nonatomic, copy) NSString* fallbackText; |
| 45 // Fallback background color for the favicon view if there is no appropriately | 46 // Fallback background color for the favicon view if there is no appropriately |
| 46 // sized favicon available. | 47 // sized favicon available. |
| 47 @property(nonatomic, retain) UIColor* fallbackBackgroundColor; | 48 @property(nonatomic, strong) UIColor* fallbackBackgroundColor; |
| 48 // Fallback text color for the favicon view if there is no appropriately | 49 // Fallback text color for the favicon view if there is no appropriately |
| 49 // sized favicon available. | 50 // sized favicon available. |
| 50 @property(nonatomic, retain) UIColor* fallbackTextColor; | 51 @property(nonatomic, strong) UIColor* fallbackTextColor; |
| 51 | 52 |
| 52 // Fetches favicon for |URL| from |faviconService|. Notifies delegate when | 53 // Fetches favicon for |URL| from |faviconService|. Notifies delegate when |
| 53 // favicon is retrieved. | 54 // favicon is retrieved. |
| 54 - (void)fetchFaviconForURL:(const GURL&)URL | 55 - (void)fetchFaviconForURL:(const GURL&)URL |
| 55 size:(CGFloat)size | 56 size:(CGFloat)size |
| 56 minSize:(CGFloat)minSize | 57 minSize:(CGFloat)minSize |
| 57 service:(favicon::LargeIconService*)faviconService; | 58 service:(favicon::LargeIconService*)faviconService; |
| 58 | 59 |
| 59 @end | 60 @end |
| 60 | 61 |
| 61 @implementation FaviconViewProvider | 62 @implementation FaviconViewProvider |
| 62 | 63 |
| 63 @synthesize faviconSize = _faviconSize; | 64 @synthesize faviconSize = _faviconSize; |
| 64 @synthesize favicon = _favicon; | 65 @synthesize favicon = _favicon; |
| 65 @synthesize fallbackText = _fallbackText; | 66 @synthesize fallbackText = _fallbackText; |
| 66 @synthesize fallbackBackgroundColor = _fallbackBackgroundColor; | 67 @synthesize fallbackBackgroundColor = _fallbackBackgroundColor; |
| 67 @synthesize fallbackTextColor = _fallbackTextColor; | 68 @synthesize fallbackTextColor = _fallbackTextColor; |
| 68 | 69 |
| 69 - (instancetype)initWithURL:(const GURL&)URL | 70 - (instancetype)initWithURL:(const GURL&)URL |
| 70 faviconSize:(CGFloat)faviconSize | 71 faviconSize:(CGFloat)faviconSize |
| 71 minFaviconSize:(CGFloat)minFaviconSize | 72 minFaviconSize:(CGFloat)minFaviconSize |
| 72 largeIconService:(favicon::LargeIconService*)largeIconService | 73 largeIconService:(favicon::LargeIconService*)largeIconService |
| 73 delegate:(id<FaviconViewProviderDelegate>)delegate { | 74 delegate:(id<FaviconViewProviderDelegate>)delegate { |
| 74 self = [super init]; | 75 self = [super init]; |
| 75 if (self) { | 76 if (self) { |
| 76 _propertyReleaser_FaviconViewProvider.Init(self, | |
| 77 [FaviconViewProvider class]); | |
| 78 _faviconSize = faviconSize; | 77 _faviconSize = faviconSize; |
| 79 _delegate.reset(delegate); | 78 _delegate.reset(delegate); |
| 80 _fallbackBackgroundColor = [[UIColor grayColor] retain]; | 79 _fallbackBackgroundColor = [UIColor grayColor]; |
| 81 _fallbackTextColor = [[UIColor whiteColor] retain]; | 80 _fallbackTextColor = [UIColor whiteColor]; |
| 82 [self fetchFaviconForURL:URL | 81 [self fetchFaviconForURL:URL |
| 83 size:faviconSize | 82 size:faviconSize |
| 84 minSize:minFaviconSize | 83 minSize:minFaviconSize |
| 85 service:largeIconService]; | 84 service:largeIconService]; |
| 86 } | 85 } |
| 87 return self; | 86 return self; |
| 88 } | 87 } |
| 89 | 88 |
| 90 - (instancetype)init { | 89 - (instancetype)init { |
| 91 NOTREACHED(); | 90 NOTREACHED(); |
| 92 return nil; | 91 return nil; |
| 93 } | 92 } |
| 94 | 93 |
| 95 - (void)fetchFaviconForURL:(const GURL&)URL | 94 - (void)fetchFaviconForURL:(const GURL&)URL |
| 96 size:(CGFloat)size | 95 size:(CGFloat)size |
| 97 minSize:(CGFloat)minSize | 96 minSize:(CGFloat)minSize |
| 98 service:(favicon::LargeIconService*)largeIconService { | 97 service:(favicon::LargeIconService*)largeIconService { |
| 99 if (!largeIconService) | 98 if (!largeIconService) |
| 100 return; | 99 return; |
| 101 base::WeakNSObject<FaviconViewProvider> weakSelf(self); | 100 base::WeakNSObject<FaviconViewProvider> weakSelf(self); |
| 102 GURL blockURL(URL); | 101 GURL blockURL(URL); |
| 103 void (^faviconBlock)(const favicon_base::LargeIconResult&) = ^( | 102 void (^faviconBlock)(const favicon_base::LargeIconResult&) = ^( |
| 104 const favicon_base::LargeIconResult& result) { | 103 const favicon_base::LargeIconResult& result) { |
| 105 base::scoped_nsobject<FaviconViewProvider> strongSelf([weakSelf retain]); | 104 base::scoped_nsobject<FaviconViewProvider> strongSelf(weakSelf); |
| 106 if (!strongSelf) | 105 if (!strongSelf) |
| 107 return; | 106 return; |
| 108 if (result.bitmap.is_valid()) { | 107 if (result.bitmap.is_valid()) { |
| 109 scoped_refptr<base::RefCountedMemory> data = | 108 scoped_refptr<base::RefCountedMemory> data = |
| 110 result.bitmap.bitmap_data.get(); | 109 result.bitmap.bitmap_data.get(); |
| 111 [strongSelf | 110 [strongSelf |
| 112 setFavicon:[UIImage | 111 setFavicon:[UIImage |
| 113 imageWithData:[NSData dataWithBytes:data->front() | 112 imageWithData:[NSData dataWithBytes:data->front() |
| 114 length:data->size()]]]; | 113 length:data->size()]]]; |
| 115 } else if (result.fallback_icon_style) { | 114 } else if (result.fallback_icon_style) { |
| 116 [strongSelf setFallbackBackgroundColor:skia::UIColorFromSkColor( | 115 [strongSelf setFallbackBackgroundColor:skia::UIColorFromSkColor( |
| 117 result.fallback_icon_style | 116 result.fallback_icon_style |
| 118 ->background_color)]; | 117 ->background_color)]; |
| 119 [strongSelf | 118 [strongSelf |
| 120 setFallbackTextColor:skia::UIColorFromSkColor( | 119 setFallbackTextColor:skia::UIColorFromSkColor( |
| 121 result.fallback_icon_style->text_color)]; | 120 result.fallback_icon_style->text_color)]; |
| 122 [strongSelf setFallbackText:base::SysUTF16ToNSString( | 121 [strongSelf setFallbackText:base::SysUTF16ToNSString( |
| 123 favicon::GetFallbackIconText(blockURL))]; | 122 favicon::GetFallbackIconText(blockURL))]; |
| 124 } | 123 } |
| 125 [strongSelf.get()->_delegate faviconViewProviderFaviconDidLoad:strongSelf]; | 124 [strongSelf.get()->_delegate faviconViewProviderFaviconDidLoad:strongSelf]; |
| 126 }; | 125 }; |
| 127 | 126 |
| 128 // Always call LargeIconService in case the favicon was updated. | 127 // Always call LargeIconService in case the favicon was updated. |
| 129 CGFloat faviconSize = [UIScreen mainScreen].scale * size; | 128 CGFloat faviconSize = [UIScreen mainScreen].scale * size; |
| 130 CGFloat minFaviconSize = [UIScreen mainScreen].scale * minSize; | 129 CGFloat minFaviconSize = [UIScreen mainScreen].scale * minSize; |
| 131 largeIconService->GetLargeIconOrFallbackStyle( | 130 largeIconService->GetLargeIconOrFallbackStyle( |
| 132 URL, minFaviconSize, faviconSize, base::BindBlock(faviconBlock), | 131 URL, minFaviconSize, faviconSize, base::BindBlockArc(faviconBlock), |
| 133 &_faviconTaskTracker); | 132 &_faviconTaskTracker); |
| 134 } | 133 } |
| 135 | 134 |
| 136 - (FaviconView*)faviconView { | 135 - (FaviconView*)faviconView { |
| 137 if (!_faviconView) { | 136 if (!_faviconView) { |
| 138 _faviconView.reset([[FaviconView alloc] initWithFrame:CGRectZero]); | 137 _faviconView.reset([[FaviconView alloc] initWithFrame:CGRectZero]); |
| 139 } | 138 } |
| 140 _faviconView.get().size = _faviconSize; | 139 _faviconView.get().size = _faviconSize; |
| 141 // Update favicon view with current properties. | 140 // Update favicon view with current properties. |
| 142 if (self.favicon) { | 141 if (self.favicon) { |
| 143 _faviconView.get().faviconImage.image = self.favicon; | 142 _faviconView.get().faviconImage.image = self.favicon; |
| 144 _faviconView.get().faviconImage.backgroundColor = [UIColor whiteColor]; | 143 _faviconView.get().faviconImage.backgroundColor = [UIColor whiteColor]; |
| 145 _faviconView.get().faviconFallbackLabel.text = nil; | 144 _faviconView.get().faviconFallbackLabel.text = nil; |
| 146 } else { | 145 } else { |
| 147 _faviconView.get().faviconImage.image = nil; | 146 _faviconView.get().faviconImage.image = nil; |
| 148 _faviconView.get().faviconImage.backgroundColor = | 147 _faviconView.get().faviconImage.backgroundColor = |
| 149 self.fallbackBackgroundColor; | 148 self.fallbackBackgroundColor; |
| 150 _faviconView.get().faviconFallbackLabel.text = self.fallbackText; | 149 _faviconView.get().faviconFallbackLabel.text = self.fallbackText; |
| 151 _faviconView.get().faviconFallbackLabel.textColor = self.fallbackTextColor; | 150 _faviconView.get().faviconFallbackLabel.textColor = self.fallbackTextColor; |
| 152 | 151 |
| 153 CGFloat fontSize = floorf(_faviconSize / 2); | 152 CGFloat fontSize = floorf(_faviconSize / 2); |
| 154 _faviconView.get().faviconFallbackLabel.font = | 153 _faviconView.get().faviconFallbackLabel.font = |
| 155 [[MDFRobotoFontLoader sharedInstance] regularFontOfSize:fontSize]; | 154 [[MDFRobotoFontLoader sharedInstance] regularFontOfSize:fontSize]; |
| 156 } | 155 } |
| 157 return _faviconView; | 156 return _faviconView; |
| 158 } | 157 } |
| 159 | 158 |
| 160 @end | 159 @end |
| OLD | NEW |