Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(90)

Side by Side Diff: ios/chrome/browser/ui/history/favicon_view_provider.mm

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

Powered by Google App Engine
This is Rietveld 408576698