Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(225)

Side by Side Diff: chrome/browser/manifest/manifest_icon_selector.cc

Issue 1901363003: Remove support for the web manifest icon density member. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/manifest/manifest_icon_selector.h" 5 #include "chrome/browser/manifest/manifest_icon_selector.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <cmath> 10 #include <cmath>
11 #include <limits> 11 #include <limits>
12 12
13 #include "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
14 #include "components/mime_util/mime_util.h" 14 #include "components/mime_util/mime_util.h"
15 #include "content/public/browser/web_contents.h" 15 #include "content/public/browser/web_contents.h"
16 #include "ui/gfx/screen.h" 16 #include "ui/gfx/screen.h"
17 17
18 using content::Manifest; 18 using content::Manifest;
19 19
20 ManifestIconSelector::ManifestIconSelector(int ideal_icon_size_in_px, 20 ManifestIconSelector::ManifestIconSelector(int ideal_icon_size_in_px,
21 int minimum_icon_size_in_px) 21 int minimum_icon_size_in_px)
22 : ideal_icon_size_in_px_(ideal_icon_size_in_px), 22 : ideal_icon_size_in_px_(ideal_icon_size_in_px),
23 minimum_icon_size_in_px_(minimum_icon_size_in_px), 23 minimum_icon_size_in_px_(minimum_icon_size_in_px) {
24 density_(
25 gfx::Screen::GetScreen()->GetPrimaryDisplay().device_scale_factor()) {
26 } 24 }
27 25
28 bool ManifestIconSelector::IconSizesContainsPreferredSize( 26 bool ManifestIconSelector::IconSizesContainsPreferredSize(
29 const std::vector<gfx::Size>& sizes) { 27 const std::vector<gfx::Size>& sizes) const {
30 for (size_t i = 0; i < sizes.size(); ++i) { 28 for (size_t i = 0; i < sizes.size(); ++i) {
31 if (sizes[i].height() != sizes[i].width()) 29 if (sizes[i].height() != sizes[i].width())
32 continue; 30 continue;
33 if (sizes[i].width() == ideal_icon_size_in_px_) 31 if (sizes[i].width() == ideal_icon_size_in_px_)
34 return true; 32 return true;
35 } 33 }
36 34
37 return false; 35 return false;
38 } 36 }
39 37
40 bool ManifestIconSelector::IconSizesContainsBiggerThanMinimumSize( 38 bool ManifestIconSelector::IconSizesContainsBiggerThanMinimumSize(
41 const std::vector<gfx::Size>& sizes) { 39 const std::vector<gfx::Size>& sizes) const {
42 for (size_t i = 0; i < sizes.size(); ++i) { 40 for (size_t i = 0; i < sizes.size(); ++i) {
43 if (sizes[i].height() != sizes[i].width()) 41 if (sizes[i].height() != sizes[i].width())
44 continue; 42 continue;
45 if (sizes[i].width() >= minimum_icon_size_in_px_) 43 if (sizes[i].width() >= minimum_icon_size_in_px_)
46 return true; 44 return true;
47 } 45 }
48 return false; 46 return false;
49 } 47 }
50 48
51 int ManifestIconSelector::FindBestMatchingIconForDensity( 49 int ManifestIconSelector::FindClosestIconToIdealSize(
52 const std::vector<content::Manifest::Icon>& icons, 50 const std::vector<content::Manifest::Icon>& icons) const {
53 float density) {
54 int best_index = -1; 51 int best_index = -1;
55 int best_delta = std::numeric_limits<int>::min(); 52 int best_delta = std::numeric_limits<int>::min();
56 53
57 for (size_t i = 0; i < icons.size(); ++i) { 54 for (size_t i = 0; i < icons.size(); ++i) {
58 if (icons[i].density != density)
59 continue;
60
61 const std::vector<gfx::Size>& sizes = icons[i].sizes; 55 const std::vector<gfx::Size>& sizes = icons[i].sizes;
62 for (size_t j = 0; j < sizes.size(); ++j) { 56 for (size_t j = 0; j < sizes.size(); ++j) {
63 if (sizes[j].height() != sizes[j].width()) 57 if (sizes[j].height() != sizes[j].width())
64 continue; 58 continue;
65 int delta = sizes[j].width() - ideal_icon_size_in_px_; 59 int delta = sizes[j].width() - ideal_icon_size_in_px_;
66 if (delta == 0) 60 if (delta == 0)
67 return i; 61 return i;
68 if (best_delta > 0 && delta < 0) 62 if (best_delta > 0 && delta < 0)
69 continue; 63 continue;
70 if ((best_delta > 0 && delta < best_delta) || 64 if ((best_delta > 0 && delta < best_delta) ||
71 (best_delta < 0 && delta > best_delta)) { 65 (best_delta < 0 && delta > best_delta)) {
72 best_index = i; 66 best_index = i;
73 best_delta = delta; 67 best_delta = delta;
74 } 68 }
75 } 69 }
76 } 70 }
77 71
78 return best_index; 72 return best_index;
79 } 73 }
80 74
81 int ManifestIconSelector::FindBestMatchingIcon( 75 int ManifestIconSelector::FindBestMatchingIcon(
82 const std::vector<content::Manifest::Icon>& icons) { 76 const std::vector<content::Manifest::Icon>& icons) const {
83 int best_index = -1; 77 int best_index = -1;
84 78
85 // The first pass is to find the ideal icon. That icon is of the right size 79 // The first pass is to find the ideal icon - one with the exact right size.
86 // with the default density or the device's density.
87 for (size_t i = 0; i < icons.size(); ++i) { 80 for (size_t i = 0; i < icons.size(); ++i) {
88 if (icons[i].density == density_ && 81 if (IconSizesContainsPreferredSize(icons[i].sizes))
89 IconSizesContainsPreferredSize(icons[i].sizes)) {
90 return i; 82 return i;
91 }
92 83
93 // If there is an icon with the right size but not the right density, keep 84 // If there is an icon size 'any', keep it on the side and only use it if
94 // it on the side and only use it if nothing better is found. 85 // nothing better is found.
95 if (icons[i].density == Manifest::Icon::kDefaultDensity && 86 if (IconSizesContainsAny(icons[i].sizes))
96 IconSizesContainsPreferredSize(icons[i].sizes)) {
97 best_index = i; 87 best_index = i;
98 }
99 } 88 }
100
101 if (best_index != -1) 89 if (best_index != -1)
102 return best_index; 90 return best_index;
103 91
104 // The second pass is to find an icon with 'any'. The current device scale 92 // The last pass will try to find the smallest icon larger than the ideal
105 // factor is preferred. Otherwise, the default scale factor is used. 93 // size, or the largest icon smaller than the ideal size.
106 for (size_t i = 0; i < icons.size(); ++i) { 94 best_index = FindClosestIconToIdealSize(icons);
107 if (icons[i].density == density_ &&
108 IconSizesContainsAny(icons[i].sizes)) {
109 return i;
110 }
111 95
112 // If there is an icon with 'any' but not the right density, keep it on the
113 // side and only use it if nothing better is found.
114 if (icons[i].density == Manifest::Icon::kDefaultDensity &&
115 IconSizesContainsAny(icons[i].sizes)) {
116 best_index = i;
117 }
118 }
119
120 if (best_index != -1)
121 return best_index;
122
123 // The last pass will try to find the best suitable icon for the device's
124 // scale factor. If none, another pass will be run using kDefaultDensity.
125 best_index = FindBestMatchingIconForDensity(icons, density_);
126 if (best_index != -1 && 96 if (best_index != -1 &&
127 IconSizesContainsBiggerThanMinimumSize(icons[best_index].sizes)) 97 IconSizesContainsBiggerThanMinimumSize(icons[best_index].sizes))
128 return best_index; 98 return best_index;
129
130 best_index = FindBestMatchingIconForDensity(icons,
131 Manifest::Icon::kDefaultDensity);
132 if (best_index != -1 &&
133 IconSizesContainsBiggerThanMinimumSize(icons[best_index].sizes))
134 return best_index;
135 99
136 return -1; 100 return -1;
137 } 101 }
138 102
139 103
140 // static 104 // static
141 bool ManifestIconSelector::IconSizesContainsAny( 105 bool ManifestIconSelector::IconSizesContainsAny(
142 const std::vector<gfx::Size>& sizes) { 106 const std::vector<gfx::Size>& sizes) {
143 for (size_t i = 0; i < sizes.size(); ++i) { 107 for (size_t i = 0; i < sizes.size(); ++i) {
144 if (sizes[i].IsEmpty()) 108 if (sizes[i].IsEmpty())
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 return GURL(); 149 return GURL();
186 return icons[index].src; 150 return icons[index].src;
187 } 151 }
188 152
189 // static 153 // static
190 int ManifestIconSelector::ConvertIconSizeFromDpToPx(int icon_size_in_dp) { 154 int ManifestIconSelector::ConvertIconSizeFromDpToPx(int icon_size_in_dp) {
191 return static_cast<int>(round( 155 return static_cast<int>(round(
192 icon_size_in_dp * 156 icon_size_in_dp *
193 gfx::Screen::GetScreen()->GetPrimaryDisplay().device_scale_factor())); 157 gfx::Screen::GetScreen()->GetPrimaryDisplay().device_scale_factor()));
194 } 158 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698