Commit f0889fe1 authored by stkhapugin's avatar stkhapugin Committed by Commit bot

Creates FaviconAttributesProvider.

Creates FaviconAttributesProvider, FaviconAttributes and FaviconViewNew
classes to present favicons. These classes intend to replace FaviconView
and FaviconViewProvider downstream.

BUG=None

Review-Url: https://codereview.chromium.org/2361393004
Cr-Commit-Position: refs/heads/master@{#422769}
parent 18ea5ddb
...@@ -140,6 +140,10 @@ source_set("browser") { ...@@ -140,6 +140,10 @@ source_set("browser") {
"dom_distiller/dom_distiller_service_factory.h", "dom_distiller/dom_distiller_service_factory.h",
"experimental_flags.h", "experimental_flags.h",
"experimental_flags.mm", "experimental_flags.mm",
"favicon/favicon_attributes.h",
"favicon/favicon_attributes.mm",
"favicon/favicon_attributes_provider.h",
"favicon/favicon_attributes_provider.mm",
"favicon/favicon_client_impl.h", "favicon/favicon_client_impl.h",
"favicon/favicon_client_impl.mm", "favicon/favicon_client_impl.mm",
"favicon/favicon_loader.h", "favicon/favicon_loader.h",
...@@ -527,6 +531,8 @@ source_set("browser") { ...@@ -527,6 +531,8 @@ source_set("browser") {
"ui/elements/selector_picker_view_controller.h", "ui/elements/selector_picker_view_controller.h",
"ui/elements/selector_picker_view_controller.mm", "ui/elements/selector_picker_view_controller.mm",
"ui/elements/selector_view_controller_delegate.h", "ui/elements/selector_view_controller_delegate.h",
"ui/favicon_view.h",
"ui/favicon_view.mm",
"ui/file_locations.h", "ui/file_locations.h",
"ui/file_locations.mm", "ui/file_locations.mm",
"ui/image_util.h", "ui/image_util.h",
......
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_CHROME_BROWSER_FAVICON_FAVICON_ATTRIBUTES_H_
#define IOS_CHROME_BROWSER_FAVICON_FAVICON_ATTRIBUTES_H_
#import <UIKit/UIKit.h>
// Attributes of a favicon. A favicon is represented either with an image or
// with a fallback monogram of a given color and background color.
@interface FaviconAttributes : NSObject
// Favicon image. Can be nil. If it is nil, monogram string and color are
// guaranteed to be not nil.
@property(nonatomic, readonly, strong) UIImage* faviconImage;
// Favicon monogram. Only available when there is no image.
@property(nonatomic, readonly, copy) NSString* monogramString;
// Favicon monogram color. Only available when there is no image.
@property(nonatomic, readonly, strong) UIColor* textColor;
// Favicon monogram background color. Only available when there is no image.
@property(nonatomic, readonly, strong) UIColor* backgroundColor;
+ (instancetype)attributesWithImage:(UIImage*)image;
+ (instancetype)attributesWithMonogram:(NSString*)monogram
textColor:(UIColor*)textColor
backgroundColor:(UIColor*)backgroundColor;
// Designated initializer. Either |image| or all of |textColor|,
// |backgroundColor| and |monogram| must be not nil.
- (instancetype)initWithImage:(UIImage*)image
monogram:(NSString*)monogram
textColor:(UIColor*)textColor
backgroundColor:(UIColor*)backgroundColor
NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithImage:(UIImage*)image;
- (instancetype)initWithMonogram:(NSString*)monogram
textColor:(UIColor*)textColor
backgroundColor:(UIColor*)backgroundColor;
- (instancetype)init NS_UNAVAILABLE;
@end
#endif // IOS_CHROME_BROWSER_FAVICON_FAVICON_ATTRIBUTES_H_
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/chrome/browser/favicon/favicon_attributes.h"
#include "base/logging.h"
#include "base/mac/objc_property_releaser.h"
@implementation FaviconAttributes {
base::mac::ObjCPropertyReleaser _propertyReleaser_FaviconAttributes;
}
@synthesize faviconImage = _faviconImage;
@synthesize monogramString = _monogramString;
@synthesize textColor = _textColor;
@synthesize backgroundColor = _backgroundColor;
- (instancetype)initWithImage:(UIImage*)image
monogram:(NSString*)monogram
textColor:(UIColor*)textColor
backgroundColor:(UIColor*)backgroundColor {
DCHECK(image || (monogram && textColor && backgroundColor));
self = [super init];
if (self) {
_propertyReleaser_FaviconAttributes.Init(self, [FaviconAttributes class]);
_faviconImage = [image retain];
_monogramString = [monogram copy];
_textColor = [textColor retain];
_backgroundColor = [backgroundColor retain];
}
return self;
}
- (instancetype)initWithImage:(UIImage*)image {
DCHECK(image);
return
[self initWithImage:image monogram:nil textColor:nil backgroundColor:nil];
}
- (instancetype)initWithMonogram:(NSString*)monogram
textColor:(UIColor*)textColor
backgroundColor:(UIColor*)backgroundColor {
DCHECK(monogram && textColor && backgroundColor);
return [self initWithImage:nil
monogram:monogram
textColor:textColor
backgroundColor:backgroundColor];
}
+ (instancetype)attributesWithImage:(UIImage*)image {
return [[[self alloc] initWithImage:image] autorelease];
}
+ (instancetype)attributesWithMonogram:(NSString*)monogram
textColor:(UIColor*)textColor
backgroundColor:(UIColor*)backgroundColor {
return [[[self alloc] initWithMonogram:monogram
textColor:textColor
backgroundColor:backgroundColor] autorelease];
}
@end
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_CHROME_BROWSER_FAVICON_FAVICON_ATTRIBUTES_PROVIDER_H_
#define IOS_CHROME_BROWSER_FAVICON_FAVICON_ATTRIBUTES_PROVIDER_H_
#import <UIKit/UIKit.h>
#import "ios/chrome/browser/favicon/favicon_attributes.h"
namespace favicon {
class LargeIconService;
} // namespace favicon
@class FaviconViewProvider;
class GURL;
// Object to fetch favicon attributes by URL - an image or a fallback icon if
// there is no favicon image available with large enough resolution.
@interface FaviconAttributesProvider : NSObject
// Favicon attributes associated with |URL| will be fetched using
// |largeIconService|. The favicon will be rendered with height and width equal
// to |faviconSize|, and the image will be fetched if the source size is greater
// than or equal to |minFaviconSize|.
- (instancetype)initWithFaviconSize:(CGFloat)faviconSize
minFaviconSize:(CGFloat)minFaviconSize
largeIconService:(favicon::LargeIconService*)largeIconService
NS_DESIGNATED_INITIALIZER;
- (instancetype)init NS_UNAVAILABLE;
// Fetches favicon attributes and calls the completion block.
- (void)fetchFaviconAttributesForURL:(const GURL&)URL
completion:(void (^)(FaviconAttributes*))completion;
// LargeIconService used to fetch favicons.
@property(nonatomic, readonly) favicon::LargeIconService* largeIconService;
// Minimal acceptable favicon size. Below that, will fall back to a monogram.
@property(nonatomic, readonly) CGFloat minSize;
// Expected favicon size (in points). Will downscale favicon to this.
@property(nonatomic, readonly) CGFloat faviconSize;
@end
#endif // IOS_CHROME_BROWSER_FAVICON_FAVICON_ATTRIBUTES_PROVIDER_H_
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/chrome/browser/favicon/favicon_attributes_provider.h"
#include "base/logging.h"
#include "base/mac/bind_objc_block.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/cancelable_task_tracker.h"
#include "components/favicon/core/fallback_url_util.h"
#include "components/favicon/core/large_icon_service.h"
#include "components/favicon_base/fallback_icon_style.h"
#include "components/favicon_base/favicon_types.h"
#include "skia/ext/skia_utils_ios.h"
#include "url/gurl.h"
@interface FaviconAttributesProvider () {
// Used to cancel tasks for the LargeIconService.
base::CancelableTaskTracker _faviconTaskTracker;
}
@end
@implementation FaviconAttributesProvider
@synthesize largeIconService = _largeIconService;
@synthesize minSize = _minSize;
@synthesize faviconSize = _faviconSize;
- (instancetype)initWithFaviconSize:(CGFloat)faviconSize
minFaviconSize:(CGFloat)minFaviconSize
largeIconService:
(favicon::LargeIconService*)largeIconService {
DCHECK(largeIconService);
self = [super init];
if (self) {
_faviconSize = faviconSize;
_minSize = minFaviconSize;
_largeIconService = largeIconService;
}
return self;
}
- (void)fetchFaviconAttributesForURL:(const GURL&)URL
completion:(void (^)(FaviconAttributes*))completion {
GURL blockURL(URL);
void (^faviconBlock)(const favicon_base::LargeIconResult&) =
^(const favicon_base::LargeIconResult& result) {
FaviconAttributes* attributes = nil;
if (result.bitmap.is_valid()) {
scoped_refptr<base::RefCountedMemory> data =
result.bitmap.bitmap_data.get();
UIImage* favicon =
[UIImage imageWithData:[NSData dataWithBytes:data->front()
length:data->size()]];
attributes = [FaviconAttributes attributesWithImage:favicon];
} else if (result.fallback_icon_style) {
UIColor* backgroundColor = skia::UIColorFromSkColor(
result.fallback_icon_style->background_color);
UIColor* textColor =
skia::UIColorFromSkColor(result.fallback_icon_style->text_color);
NSString* monogram =
base::SysUTF16ToNSString(favicon::GetFallbackIconText(blockURL));
attributes =
[FaviconAttributes attributesWithMonogram:monogram
textColor:textColor
backgroundColor:backgroundColor];
}
DCHECK(attributes);
completion(attributes);
};
// Always call LargeIconService in case the favicon was updated.
CGFloat faviconSize = [UIScreen mainScreen].scale * self.faviconSize;
CGFloat minFaviconSize = [UIScreen mainScreen].scale * self.minSize;
self.largeIconService->GetLargeIconOrFallbackStyle(
URL, minFaviconSize, faviconSize, base::BindBlock(faviconBlock),
&_faviconTaskTracker);
}
@end
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_CHROME_BROWSER_UI_FAVICON_VIEW_H_
#define IOS_CHROME_BROWSER_UI_FAVICON_VIEW_H_
#import <UIKit/UIKit.h>
#import "ios/chrome/browser/favicon/favicon_attributes.h"
namespace {
// Minimum width and height of favicon.
const CGFloat kFaviconMinSize = 16.0f;
// Default width and height of favicon.
const CGFloat kFaviconPreferredSize = 24.0f;
} // namespace
@interface FaviconViewNew : UIView
// Configures this view with given attributes.
- (void)configureWithAttributes:(FaviconAttributes*)attributes;
// Sets monogram font.
- (void)setFont:(UIFont*)font;
@end
#endif // IOS_CHROME_BROWSER_UI_FAVICON_VIEW_H_
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/chrome/browser/ui/favicon_view.h"
#include "base/mac/objc_property_releaser.h"
#import "ios/chrome/browser/ui/uikit_ui_util.h"
namespace {
// Default corner radius for the favicon image view.
const CGFloat kDefaultCornerRadius = 3;
}
@interface FaviconViewNew () {
// Property releaser for FaviconViewNew.
base::mac::ObjCPropertyReleaser _propertyReleaser_FaviconViewNew;
}
// Image view for the favicon.
@property(nonatomic, retain) UIImageView* faviconImageView;
// Label for fallback favicon placeholder.
@property(nonatomic, retain) UILabel* faviconFallbackLabel;
@end
@implementation FaviconViewNew
@synthesize faviconImageView = _faviconImageView;
@synthesize faviconFallbackLabel = _faviconFallbackLabel;
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
_propertyReleaser_FaviconViewNew.Init(self, [FaviconViewNew class]);
_faviconImageView = [[UIImageView alloc] initWithFrame:self.bounds];
_faviconImageView.clipsToBounds = YES;
_faviconImageView.layer.cornerRadius = kDefaultCornerRadius;
_faviconImageView.image = nil;
_faviconFallbackLabel = [[UILabel alloc] initWithFrame:self.bounds];
_faviconFallbackLabel.backgroundColor = [UIColor clearColor];
_faviconFallbackLabel.textAlignment = NSTextAlignmentCenter;
_faviconFallbackLabel.isAccessibilityElement = NO;
_faviconFallbackLabel.clipsToBounds = YES;
_faviconFallbackLabel.layer.cornerRadius = kDefaultCornerRadius;
_faviconFallbackLabel.text = nil;
[self addSubview:_faviconFallbackLabel];
[self addSubview:_faviconImageView];
[_faviconImageView setTranslatesAutoresizingMaskIntoConstraints:NO];
[_faviconFallbackLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
// Both image and fallback label are centered and match the size of favicon.
AddSameCenterConstraints(_faviconImageView, self);
AddSameCenterConstraints(_faviconFallbackLabel, self);
AddSameSizeConstraint(_faviconFallbackLabel, self);
AddSameSizeConstraint(_faviconImageView, self);
}
return self;
}
- (void)configureWithAttributes:(FaviconAttributes*)attributes {
if (attributes.faviconImage) {
self.faviconImageView.image = attributes.faviconImage;
self.faviconImageView.hidden = NO;
self.faviconFallbackLabel.hidden = YES;
} else {
self.faviconFallbackLabel.backgroundColor = attributes.backgroundColor;
self.faviconFallbackLabel.textColor = attributes.textColor;
self.faviconFallbackLabel.text = attributes.monogramString;
self.faviconFallbackLabel.hidden = NO;
self.faviconImageView.hidden = YES;
}
}
- (void)setFont:(UIFont*)font {
self.faviconFallbackLabel.font = font;
}
#pragma mark - UIView
- (CGSize)intrinsicContentSize {
return CGSizeMake(kFaviconPreferredSize, kFaviconPreferredSize);
}
@end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment