File: manifest_icon_selector.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; 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,806; 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 (118 lines) | stat: -rw-r--r-- 4,089 bytes parent folder | download | duplicates (6)
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
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/public/common/manifest/manifest_icon_selector.h"

#include <limits>

#include "base/containers/contains.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "third_party/blink/public/common/mime_util/mime_util.h"

namespace blink {

// static
BLINK_COMMON_EXPORT GURL ManifestIconSelector::FindBestMatchingSquareIcon(
    const std::vector<blink::Manifest::ImageResource>& icons,
    int ideal_icon_size_in_px,
    int minimum_icon_size_in_px,
    blink::mojom::ManifestImageResource_Purpose purpose) {
  return FindBestMatchingIcon(icons, ideal_icon_size_in_px,
                              minimum_icon_size_in_px,
                              1 /*max_width_to_height_ratio */, purpose);
}

// static
BLINK_COMMON_EXPORT GURL ManifestIconSelector::FindBestMatchingIcon(
    const std::vector<blink::Manifest::ImageResource>& icons,
    int ideal_icon_height_in_px,
    int minimum_icon_height_in_px,
    float max_width_to_height_ratio,
    blink::mojom::ManifestImageResource_Purpose purpose) {
  DCHECK_LE(minimum_icon_height_in_px, ideal_icon_height_in_px);
  DCHECK_GE(max_width_to_height_ratio, 1.0);

  // Icon with exact matching size has priority over icon with size "any", which
  // has priority over icon with closest matching size.
  int latest_size_any_index = -1;
  int closest_size_match_index = -1;
  int best_delta_in_size = std::numeric_limits<int>::min();

  for (size_t i = 0; i < icons.size(); ++i) {
    const auto& icon = icons[i];

    // Check for supported image MIME types.
    if (!icon.type.empty()) {
      std::string type = base::UTF16ToUTF8(icon.type);
      if (!(blink::IsSupportedImageMimeType(base::UTF16ToUTF8(icon.type)) ||
            // The following condition is intended to support image/svg+xml:
            (base::UTF16ToUTF8(icon.type).starts_with("image/") &&
             blink::IsSupportedNonImageMimeType(
                 base::UTF16ToUTF8(icon.type))))) {
        continue;
      }
    }

    // Check for icon purpose.
    if (!base::Contains(icon.purpose, purpose))
      continue;

    // Check for size constraints.
    for (const gfx::Size& size : icon.sizes) {
      // Check for size "any". Return this icon if no better one is found.
      if (size.IsEmpty()) {
        latest_size_any_index = i;
        continue;
      }

      // Check for minimum size.
      if (size.height() < minimum_icon_height_in_px)
        continue;

      // Check for width to height ratio.
      float width = static_cast<float>(size.width());
      float height = static_cast<float>(size.height());
      DCHECK_GT(height, 0);
      float ratio = width / height;
      if (ratio < 1 || ratio > max_width_to_height_ratio)
        continue;

      // According to the spec when there are multiple equally appropriate icons
      // we should choose the last one declared in the list:
      // https://w3c.github.io/manifest/#icons-member
      if (size.height() == ideal_icon_height_in_px) {
        closest_size_match_index = i;
        best_delta_in_size = 0;
        continue;
      }

      // Check for closest match.
      int delta = size.height() - ideal_icon_height_in_px;

      // Smallest icon larger than ideal size has priority over largest icon
      // smaller than ideal size.
      if (best_delta_in_size > 0 && delta < 0)
        continue;

      if ((best_delta_in_size > 0 && delta < best_delta_in_size) ||
          (best_delta_in_size < 0 && delta > best_delta_in_size)) {
        closest_size_match_index = i;
        best_delta_in_size = delta;
      }
    }
  }

  if (best_delta_in_size == 0) {
    DCHECK_NE(closest_size_match_index, -1);
    return icons[closest_size_match_index].src;
  }
  if (latest_size_any_index != -1)
    return icons[latest_size_any_index].src;
  if (closest_size_match_index != -1)
    return icons[closest_size_match_index].src;
  return GURL();
}

}  // namespace blink