File: supervised_user_favicon_request_handler.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (129 lines) | stat: -rw-r--r-- 5,202 bytes parent folder | download | duplicates (5)
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
// 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/chromeos/supervised_user_favicon_request_handler.h"

#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "chrome/browser/favicon/favicon_utils.h"
#include "components/favicon/core/favicon_service.h"
#include "components/favicon/core/large_icon_service.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "ui/gfx/codec/png_codec.h"

namespace {
constexpr int kMinIconSize = 16;
constexpr int kDesiredIconSize = 28;
constexpr int kMonogramSize = 20;

const char kFaviconAvailabilityHistogramName[] =
    "ChromeOS.FamilyLinkUser.FaviconAvailability";
}  // namespace

// static
const char* SupervisedUserFaviconRequestHandler::
    GetFaviconAvailabilityHistogramForTesting() {
  return kFaviconAvailabilityHistogramName;
}

SupervisedUserFaviconRequestHandler::SupervisedUserFaviconRequestHandler(
    const GURL& url,
    favicon::LargeIconService* large_icon_service)
    : page_url_(url), large_icon_service_(large_icon_service) {}

SupervisedUserFaviconRequestHandler::~SupervisedUserFaviconRequestHandler() =
    default;

void SupervisedUserFaviconRequestHandler::StartFaviconFetch(
    base::OnceClosure on_fetched_callback) {
  DCHECK(!network_request_completed_);
  on_fetched_callback_ = std::move(on_fetched_callback);
  FetchFaviconFromCache();
}

void SupervisedUserFaviconRequestHandler::FetchFaviconFromCache() {
  // Resize the favicon in SupervisedUserFaviconRequestHandler instead of in the
  // renderer so that the favicon resizing and the sizing of the monogram
  // favicon occur in the same place.
  large_icon_service_->GetLargeIconRawBitmapForPageUrl(
      page_url_, kMinIconSize, kDesiredIconSize,
      favicon::LargeIconService::NoBigEnoughIconBehavior::kReturnBitmap,
      base::BindOnce(
          &SupervisedUserFaviconRequestHandler::OnGetFaviconFromCacheFinished,
          weak_ptr_factory_.GetWeakPtr()),
      &favicon_task_tracker_);
}

SkBitmap SupervisedUserFaviconRequestHandler::GetFaviconOrFallback() {
  if (favicon_.isNull()) {
    base::UmaHistogramEnumeration(kFaviconAvailabilityHistogramName,
                                  FaviconAvailability::kUnavailable);
    return favicon::GenerateMonogramFavicon(page_url_, kDesiredIconSize,
                                            kMonogramSize);
  }
  base::UmaHistogramEnumeration(kFaviconAvailabilityHistogramName,
                                FaviconAvailability::kAvailable);
  return favicon_;
}

void SupervisedUserFaviconRequestHandler::OnGetFaviconFromCacheFinished(
    const favicon_base::LargeIconResult& result) {
  // Check if fetching the favicon from the cache was successful.
  const favicon_base::FaviconRawBitmapResult& bitmap_result = result.bitmap;
  if (bitmap_result.is_valid()) {
    favicon_ = gfx::PNGCodec::Decode(*bitmap_result.bitmap_data);
    if (!favicon_.isNull()) {
      large_icon_service_->TouchIconFromGoogleServer(bitmap_result.icon_url);
      std::move(on_fetched_callback_).Run();
      return;
    }
  }

  // Do not make another network request if one has already been made.
  if (network_request_completed_) {
    std::move(on_fetched_callback_).Run();
    return;
  }

  // Try to fetch the favicon from a Google favicon server.
  net::NetworkTrafficAnnotationTag traffic_annotation =
      net::DefineNetworkTrafficAnnotation("supervised_user_favicon_request", R"(
        semantics {
          sender: "SupervisedUserFaviconRequest"
          description:
            "Sends a request to a Google server to retrieve the favicon bitmap "
            "for a website that is blocked for a supervised user."
          trigger:
            "The user visits a website that has been blocked by their parent "
            "or guardian."
          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: "This request cannot be disabled by "
          "policy because it is required to provide a favicon cue about a "
          "blocked website to a parent that is considering approving the site "
          "for a supervised user."
        })");
  large_icon_service_
      ->GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache(
          page_url_,
          /*should_trim_page_url_path=*/false, traffic_annotation,
          base::BindOnce(&SupervisedUserFaviconRequestHandler::
                             OnGetFaviconFromGoogleServerFinished,
                         weak_ptr_factory_.GetWeakPtr()));
}

void SupervisedUserFaviconRequestHandler::OnGetFaviconFromGoogleServerFinished(
    favicon_base::GoogleFaviconServerRequestStatus status) {
  network_request_completed_ = true;
  if (status != favicon_base::GoogleFaviconServerRequestStatus::SUCCESS) {
    LOG(WARNING) << "Favicon fetch failed, using fallback favicon.";
    std::move(on_fetched_callback_).Run();
    return;
  }
  FetchFaviconFromCache();
}