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/ui/settings/utils/resized_avatar_cache.h" |
| 6 |
| 7 #include "base/mac/scoped_nsobject.h" |
| 8 #import "ios/chrome/browser/ui/uikit_ui_util.h" |
| 9 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h" |
| 10 #import "ios/public/provider/chrome/browser/signin/chrome_identity.h" |
| 11 #import "ios/public/provider/chrome/browser/signin/chrome_identity_service.h" |
| 12 #include "ios/public/provider/chrome/browser/signin/signin_resources_provider.h" |
| 13 |
| 14 namespace { |
| 15 const CGFloat kAccountProfilePhotoDimension = 40.0f; |
| 16 } // namespace |
| 17 |
| 18 @implementation ResizedAvatarCache { |
| 19 // Retains resized images. Key is Chrome Identity. |
| 20 base::scoped_nsobject<NSCache<ChromeIdentity*, UIImage*>> _resizedImages; |
| 21 |
| 22 // Holds weak references to the cached avatar image from the |
| 23 // ChromeIdentityService. Key is Chrome Identity. |
| 24 base::scoped_nsobject<NSMapTable<ChromeIdentity*, UIImage*>> _originalImages; |
| 25 } |
| 26 |
| 27 - (instancetype)init { |
| 28 self = [super init]; |
| 29 if (self) { |
| 30 _resizedImages.reset([[NSCache alloc] init]); |
| 31 _originalImages.reset([[NSMapTable strongToWeakObjectsMapTable] retain]); |
| 32 } |
| 33 return self; |
| 34 } |
| 35 |
| 36 - (UIImage*)resizedAvatarForIdentity:(ChromeIdentity*)identity { |
| 37 UIImage* image = ios::GetChromeBrowserProvider() |
| 38 ->GetChromeIdentityService() |
| 39 ->GetCachedAvatarForIdentity(identity); |
| 40 if (!image) { |
| 41 image = ios::GetChromeBrowserProvider() |
| 42 ->GetSigninResourcesProvider() |
| 43 ->GetDefaultAvatar(); |
| 44 // No cached image, trigger a fetch, which will notify all observers. |
| 45 ios::GetChromeBrowserProvider() |
| 46 ->GetChromeIdentityService() |
| 47 ->GetAvatarForIdentity(identity, ^(UIImage*){ |
| 48 }); |
| 49 } |
| 50 |
| 51 // If the currently used image has already been resized, use it. |
| 52 if ([_resizedImages objectForKey:identity] && |
| 53 [_originalImages objectForKey:identity] == image) |
| 54 return [_resizedImages objectForKey:identity]; |
| 55 |
| 56 [_originalImages setObject:image forKey:identity]; |
| 57 |
| 58 // Resize the profile image. |
| 59 CGFloat dimension = kAccountProfilePhotoDimension; |
| 60 if (image.size.width != dimension || image.size.height != dimension) { |
| 61 image = ResizeImage(image, CGSizeMake(dimension, dimension), |
| 62 ProjectionMode::kAspectFit); |
| 63 } |
| 64 [_resizedImages setObject:image forKey:identity]; |
| 65 return image; |
| 66 } |
| 67 |
| 68 @end |
OLD | NEW |