blob: bff757f550a78f7353ebc3475a1f3a461435d3e3 [file] [log] [blame]
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/data_sharing/internal/avatar_fetcher.h"
#include "base/strings/stringprintf.h"
#include "components/data_sharing/public/data_sharing_network_utils.h"
#include "components/image_fetcher/core/image_fetcher.h"
#include "components/signin/public/base/avatar_icon_util.h"
#include "components/signin/public/identity_manager/access_token_info.h"
#include "components/signin/public/identity_manager/identity_manager.h"
namespace {
constexpr char kUmaClientName[] = "DataSharingAvatarImageFetcher";
} // namespace
namespace data_sharing {
AvatarFetcher::AvatarFetcher() = default;
AvatarFetcher::~AvatarFetcher() = default;
void AvatarFetcher::Fetch(const GURL& avatar_url,
int size,
ImageCallback callback,
image_fetcher::ImageFetcher* image_fetcher) {
#if BUILDFLAG(IS_IOS)
if (!image_fetcher) {
return;
}
#endif
CHECK(image_fetcher);
image_fetcher::ImageFetcherParams params(kAvatarFetcherTrafficAnnotation,
kUmaClientName);
// If server cannot return a user customized profile picture or monogram it
// returns a default silhouette. This is not the style we want. We want a
// filled person icon as the default. So no_silhouette is set to true to
// enforce an empty image which will be handled by
// AvatarFetcher::OnImageFetched.
// TODO(crbug.com/381288090): Add crop option. `avatar_url` could be legacy or
// content. See GetAvatarImageURLWithOptions. It handles legacy and content
// urls nicely but missing crop option.
GURL image_url_with_size = signin::GetAvatarImageURLWithOptions(
avatar_url, size, /*no_silhouette=*/true);
image_fetcher->FetchImage(
image_url_with_size,
base::BindOnce(&AvatarFetcher::OnImageFetched,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)),
std::move(params));
}
void AvatarFetcher::OnImageFetched(
ImageCallback callback,
const gfx::Image& image,
const image_fetcher::RequestMetadata& metadata) {
if (image.IsEmpty()) {
// TODO(crbug.com/381288296): Return a filled person icon as fallback. If
// Android needs a different style of silhouette, add a signal no_silhoutte
// to DataSharingService::GetAvatarImageForURL so this simply returns an
// empty image and Android can draw its own default image.
}
std::move(callback).Run(image);
}
} // namespace data_sharing