1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/supervised_user/android/favicon_fetcher.h"
#include <cstddef>
#include <memory>
#include "base/android/callback_android.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/logging.h"
#include "base/task/cancelable_task_tracker.h"
#include "components/favicon/core/large_icon_service.h"
#include "components/favicon_base/favicon_callback.h"
#include "components/favicon_base/favicon_types.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "ui/gfx/android/java_bitmap.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_rep.h"
#include "url/gurl.h"
FaviconFetcher::FaviconFetcher(
raw_ptr<favicon::LargeIconService> large_icon_service)
: large_icon_service_(large_icon_service) {}
FaviconFetcher::~FaviconFetcher() = default;
void FaviconFetcher::Destroy() {
delete this;
}
void FaviconFetcher::OnFaviconDownloaded(
const GURL& url,
const base::android::ScopedJavaGlobalRef<jobject>& callback,
FaviconDimensions faviconDimensions,
favicon_base::GoogleFaviconServerRequestStatus status) {
if (status == favicon_base::GoogleFaviconServerRequestStatus::SUCCESS) {
FetchFavicon(url, false, faviconDimensions.min_source_size_in_pixel,
faviconDimensions.desired_size_in_pixel, std::move(callback));
} else {
LOG(WARNING)
<< "Unable to obtain a favicon image with the required specs for "
<< url.host();
Destroy();
}
}
void FaviconFetcher::ExecuteFaviconCallback(
const base::android::ScopedJavaGlobalRef<jobject>& callback,
SkBitmap bitmap) {
base::android::RunObjectCallbackAndroid(callback,
gfx::ConvertToJavaBitmap(bitmap));
}
void FaviconFetcher::OnGetFaviconFromCacheFinished(
const GURL& url,
bool continue_to_server,
const base::android::ScopedJavaGlobalRef<jobject>& callback,
FaviconDimensions faviconDimensions,
const favicon_base::LargeIconImageResult& image_result) {
if (!image_result.image.IsEmpty()) {
SkBitmap faviconBitmap =
image_result.image.AsImageSkia().GetRepresentation(1.0f).GetBitmap();
// Return the image to the caller by executing the callback and destroy this
// instance.
ExecuteFaviconCallback(callback, faviconBitmap);
Destroy();
return;
}
// Try to fetch the favicon from a Google favicon server.
if (continue_to_server) {
net::NetworkTrafficAnnotationTag traffic_annotation =
net::DefineNetworkTrafficAnnotation("favicon_fetcher_get_favicon", R"(
semantics {
sender: "Favicon"
description:
"Sends a request to a Google server to retrieve a favicon bitmap "
"for a URL that a supervised user has requested approval to "
"access from their parents."
trigger:
"A request can be sent if Chrome does not have a favicon for a "
"particular page that supervised users are requesting approval to "
"access from their parents."
data: "Page URL and desired icon size."
destination: GOOGLE_OWNED_SERVICE
}
policy {
cookies_allowed: NO
setting: "This feature cannot be disabled by settings."
policy_exception_justification: "Not implemented."
}
comments: "No policy is necessary as the request cannot be turned off via settings."
"A request for a favicon will always be sent for supervised users."
)");
large_icon_service_
->GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache(
url,
/*should_trim_page_url_path=*/false, traffic_annotation,
base::BindOnce(&FaviconFetcher::OnFaviconDownloaded,
base::Unretained(this), url, std::move(callback),
faviconDimensions));
} else {
Destroy();
}
}
base::WeakPtr<FaviconFetcher> FaviconFetcher::GetWeakPtr() {
return weak_factory_.GetWeakPtr();
}
void FaviconFetcher::FetchFavicon(
const GURL& url,
bool continue_to_server,
int min_source_side_size_in_pixel,
int desired_side_size_in_pixel,
const base::android::ScopedJavaGlobalRef<jobject>& callback) {
FaviconDimensions faviconDimensions;
faviconDimensions.min_source_size_in_pixel = min_source_side_size_in_pixel;
faviconDimensions.desired_size_in_pixel = desired_side_size_in_pixel;
large_icon_service_->GetLargeIconImageOrFallbackStyleForPageUrl(
url, faviconDimensions.min_source_size_in_pixel,
faviconDimensions.desired_size_in_pixel,
base::BindOnce(&FaviconFetcher::OnGetFaviconFromCacheFinished,
base::Unretained(this), url, continue_to_server,
std::move(callback), faviconDimensions),
&task_tracker_);
}
|