File: supervised_user_favicon_request_handler.h

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 (92 lines) | stat: -rw-r--r-- 3,744 bytes parent folder | download | duplicates (4)
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
// 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.

#ifndef CHROME_BROWSER_SUPERVISED_USER_CHROMEOS_SUPERVISED_USER_FAVICON_REQUEST_HANDLER_H_
#define CHROME_BROWSER_SUPERVISED_USER_CHROMEOS_SUPERVISED_USER_FAVICON_REQUEST_HANDLER_H_

#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/task/cancelable_task_tracker.h"
#include "components/favicon_base/favicon_types.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "url/gurl.h"

namespace favicon {
class LargeIconService;
}

// Retrieves the favicon for given page URL. If the favicon is not in the cache,
// a network request is made to fetch the icon. If the favicon cannot be
// retrieved, then a fallback monogram icon is created.
class SupervisedUserFaviconRequestHandler {
 public:
  // These enum values represent whether the favicon was ready at the time it
  // was requested. These values are logged to UMA. Entries should not be
  // renumbered and numeric values should never be reused. Please keep in sync
  // with "SupervisedUserFaviconAvailability" in
  // src/tools/metrics/histograms/enums.xml.
  enum class FaviconAvailability {
    kAvailable = 0,
    kUnavailable = 1,
    // Used for UMA. Update kMaxValue to the last value. Add future entries
    // above this comment. Sync with enums.xml.
    kMaxValue = kUnavailable,
  };

  SupervisedUserFaviconRequestHandler(
      const GURL& url,
      favicon::LargeIconService* large_icon_service);
  SupervisedUserFaviconRequestHandler(
      const SupervisedUserFaviconRequestHandler&) = delete;
  SupervisedUserFaviconRequestHandler& operator=(
      const SupervisedUserFaviconRequestHandler&) = delete;
  ~SupervisedUserFaviconRequestHandler();

  static const char* GetFaviconAvailabilityHistogramForTesting();

  // Starts fetching the URL favicon and calls on_fetched_callback when
  // finished. The favicon can be then obtained by calling GetFaviconOrFallback.
  void StartFaviconFetch(base::OnceClosure on_fetched_callback);

  // Returns the fetched favicon if it is available. If the requestor asks for
  // the favicon before it has been fetched or the favicon has failed to be
  // fetched, then a monogram fallback icon is constructed. This method is not
  // thread-safe, therefore only the creator of this request handler should get
  // the favicon through this method.
  SkBitmap GetFaviconOrFallback();

 private:
  // Attempts to fetch the favicon from the cache. If the favicon is not
  // available in the cache, then a network request is made to fetch the icon.
  void FetchFaviconFromCache();

  // Callbacks for favicon fetches.
  void OnGetFaviconFromCacheFinished(
      const favicon_base::LargeIconResult& result);
  void OnGetFaviconFromGoogleServerFinished(
      favicon_base::GoogleFaviconServerRequestStatus status);

  // The page that the favicon is being fetched for.
  GURL page_url_;

  // Stores the fetched favicon. May be an empty bitmap if the favicon fetch
  // task has not finished or fails.
  SkBitmap favicon_;

  // True if a network request has already been made to fetch the favicon.
  bool network_request_completed_ = false;

  // Callback run when the favicon has been fetched, either from the cache or
  // via a network request.
  base::OnceClosure on_fetched_callback_;

  raw_ptr<favicon::LargeIconService> large_icon_service_ = nullptr;
  base::CancelableTaskTracker favicon_task_tracker_;

  base::WeakPtrFactory<SupervisedUserFaviconRequestHandler> weak_ptr_factory_{
      this};
};

#endif  // CHROME_BROWSER_SUPERVISED_USER_CHROMEOS_SUPERVISED_USER_FAVICON_REQUEST_HANDLER_H_