1 // Copyright 2015 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.
5 #import "chrome/browser/ui/cocoa/passwords/account_avatar_fetcher_manager.h"
7 #include "base/memory/weak_ptr.h"
8 #import "chrome/browser/ui/cocoa/passwords/credential_item_view.h"
9 #include "chrome/browser/ui/passwords/account_avatar_fetcher.h"
10 #include "chrome/browser/ui/passwords/manage_passwords_view_utils.h"
11 #include "ui/gfx/image/image_skia.h"
12 #include "ui/gfx/image/image_skia_util_mac.h"
14 class AccountAvatarFetcherBridge;
16 @interface AccountAvatarFetcherManager()
17 - (void)updateAvatar:(NSImage*)image
18 fromBridge:(AccountAvatarFetcherBridge*)bridge
19 forView:(CredentialItemView*)view;
22 class AccountAvatarFetcherBridge
23 : public AccountAvatarFetcherDelegate,
24 public base::SupportsWeakPtr<AccountAvatarFetcherBridge> {
26 AccountAvatarFetcherBridge(AccountAvatarFetcherManager* manager,
27 CredentialItemView* view);
28 virtual ~AccountAvatarFetcherBridge();
30 // AccountAvatarFetcherDelegate:
31 void UpdateAvatar(const gfx::ImageSkia& image) override;
34 AccountAvatarFetcherManager* manager_;
35 CredentialItemView* view_;
38 AccountAvatarFetcherBridge::AccountAvatarFetcherBridge(
39 AccountAvatarFetcherManager* manager,
40 CredentialItemView* view)
41 : manager_(manager), view_(view) {
44 AccountAvatarFetcherBridge::~AccountAvatarFetcherBridge() = default;
46 void AccountAvatarFetcherBridge::UpdateAvatar(const gfx::ImageSkia& image) {
48 updateAvatar:gfx::NSImageFromImageSkia(ScaleImageForAccountAvatar(image))
53 @implementation AccountAvatarFetcherManager
55 - (id)initWithRequestContext:
56 (scoped_refptr<net::URLRequestContextGetter>)requestContext {
57 if ((self = [super init])) {
58 requestContext_ = requestContext;
63 - (void)startRequestWithFetcher:(AccountAvatarFetcher*)fetcher {
64 fetcher->Start(requestContext_.get());
67 - (void)fetchAvatar:(const GURL&)avatarURL forView:(CredentialItemView*)view {
68 scoped_ptr<AccountAvatarFetcherBridge> bridge(
69 new AccountAvatarFetcherBridge(self, view));
70 AccountAvatarFetcher* fetcher =
71 new AccountAvatarFetcher(avatarURL, bridge->AsWeakPtr());
72 bridges_.push_back(bridge.Pass());
73 [self startRequestWithFetcher:fetcher];
76 - (void)updateAvatar:(NSImage*)image
77 fromBridge:(AccountAvatarFetcherBridge*)bridge
78 forView:(CredentialItemView*)view {
79 [view updateAvatar:image];
80 auto it = std::find(bridges_.begin(), bridges_.end(), bridge);
81 if (it != bridges_.end())