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