OLD | NEW |
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 <algorithm> |
| 8 #include <cmath> |
7 #include <limits> | 9 #include <limits> |
8 | 10 |
9 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
10 #include "components/mime_util/mime_util.h" | 12 #include "components/mime_util/mime_util.h" |
11 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
12 #include "ui/gfx/screen.h" | 14 #include "ui/gfx/screen.h" |
13 | 15 |
14 using content::Manifest; | 16 using content::Manifest; |
15 | 17 |
16 ManifestIconSelector::ManifestIconSelector(float preferred_icon_size_in_pixels) | 18 ManifestIconSelector::ManifestIconSelector(float preferred_icon_size_in_pixels, |
17 : preferred_icon_size_in_pixels_(preferred_icon_size_in_pixels) { | 19 float minimum_icon_size_in_pixels) |
| 20 : preferred_icon_size_in_pixels_(preferred_icon_size_in_pixels), |
| 21 minimum_icon_size_in_pixels_(minimum_icon_size_in_pixels) { |
18 } | 22 } |
19 | 23 |
20 bool ManifestIconSelector::IconSizesContainsPreferredSize( | 24 bool ManifestIconSelector::IconSizesContainsPreferredSize( |
21 const std::vector<gfx::Size>& sizes) { | 25 const std::vector<gfx::Size>& sizes) { |
22 for (size_t i = 0; i < sizes.size(); ++i) { | 26 for (size_t i = 0; i < sizes.size(); ++i) { |
23 if (sizes[i].height() != sizes[i].width()) | 27 if (sizes[i].height() != sizes[i].width()) |
24 continue; | 28 continue; |
25 if (sizes[i].width() == preferred_icon_size_in_pixels_) | 29 if (sizes[i].width() == preferred_icon_size_in_pixels_) |
26 return true; | 30 return true; |
27 } | 31 } |
28 | 32 |
29 return false; | 33 return false; |
30 } | 34 } |
31 | 35 |
32 GURL ManifestIconSelector::FindBestMatchingIconForDensity( | 36 bool ManifestIconSelector::IconSizesContainsBiggerThanMinimumSize( |
| 37 const std::vector<gfx::Size>& sizes) { |
| 38 for (size_t i = 0; i < sizes.size(); ++i) { |
| 39 if (sizes[i].height() != sizes[i].width()) |
| 40 continue; |
| 41 if (sizes[i].width() >= minimum_icon_size_in_pixels_) |
| 42 return true; |
| 43 } |
| 44 return false; |
| 45 } |
| 46 |
| 47 int ManifestIconSelector::FindBestMatchingIconForDensity( |
33 const std::vector<content::Manifest::Icon>& icons, | 48 const std::vector<content::Manifest::Icon>& icons, |
34 float density) { | 49 float density) { |
35 GURL url; | 50 int best_index = -1; |
36 int best_delta = std::numeric_limits<int>::min(); | 51 int best_delta = std::numeric_limits<int>::min(); |
37 | 52 |
38 for (size_t i = 0; i < icons.size(); ++i) { | 53 for (size_t i = 0; i < icons.size(); ++i) { |
39 if (icons[i].density != density) | 54 if (icons[i].density != density) |
40 continue; | 55 continue; |
41 | 56 |
42 const std::vector<gfx::Size>& sizes = icons[i].sizes; | 57 const std::vector<gfx::Size>& sizes = icons[i].sizes; |
43 for (size_t j = 0; j < sizes.size(); ++j) { | 58 for (size_t j = 0; j < sizes.size(); ++j) { |
44 if (sizes[j].height() != sizes[j].width()) | 59 if (sizes[j].height() != sizes[j].width()) |
45 continue; | 60 continue; |
46 int delta = sizes[j].width() - preferred_icon_size_in_pixels_; | 61 int delta = sizes[j].width() - preferred_icon_size_in_pixels_; |
47 if (delta == 0) | 62 if (delta == 0) |
48 return icons[i].src; | 63 return i; |
49 if (best_delta > 0 && delta < 0) | 64 if (best_delta > 0 && delta < 0) |
50 continue; | 65 continue; |
51 if ((best_delta > 0 && delta < best_delta) || | 66 if ((best_delta > 0 && delta < best_delta) || |
52 (best_delta < 0 && delta > best_delta)) { | 67 (best_delta < 0 && delta > best_delta)) { |
53 url = icons[i].src; | 68 best_index = i; |
54 best_delta = delta; | 69 best_delta = delta; |
55 } | 70 } |
56 } | 71 } |
57 } | 72 } |
58 | 73 |
59 return url; | 74 return best_index; |
60 } | 75 } |
61 | 76 |
62 GURL ManifestIconSelector::FindBestMatchingIcon( | 77 int ManifestIconSelector::FindBestMatchingIcon( |
63 const std::vector<content::Manifest::Icon>& unfiltered_icons, | 78 const std::vector<content::Manifest::Icon>& icons, |
64 float density) { | 79 float density) { |
65 GURL url; | 80 int best_index = -1; |
66 std::vector<Manifest::Icon> icons = FilterIconsByType(unfiltered_icons); | |
67 | 81 |
68 // The first pass is to find the ideal icon. That icon is of the right size | 82 // The first pass is to find the ideal icon. That icon is of the right size |
69 // with the default density or the device's density. | 83 // with the default density or the device's density. |
70 for (size_t i = 0; i < icons.size(); ++i) { | 84 for (size_t i = 0; i < icons.size(); ++i) { |
71 if (icons[i].density == density && | 85 if (icons[i].density == density && |
72 IconSizesContainsPreferredSize(icons[i].sizes)) { | 86 IconSizesContainsPreferredSize(icons[i].sizes)) { |
73 return icons[i].src; | 87 return i; |
74 } | 88 } |
75 | 89 |
76 // If there is an icon with the right size but not the right density, keep | 90 // If there is an icon with the right size but not the right density, keep |
77 // it on the side and only use it if nothing better is found. | 91 // it on the side and only use it if nothing better is found. |
78 if (icons[i].density == Manifest::Icon::kDefaultDensity && | 92 if (icons[i].density == Manifest::Icon::kDefaultDensity && |
79 IconSizesContainsPreferredSize(icons[i].sizes)) { | 93 IconSizesContainsPreferredSize(icons[i].sizes)) { |
80 url = icons[i].src; | 94 best_index = i; |
81 } | 95 } |
82 } | 96 } |
83 | 97 |
| 98 if (best_index != -1) |
| 99 return best_index; |
| 100 |
84 // The second pass is to find an icon with 'any'. The current device scale | 101 // The second pass is to find an icon with 'any'. The current device scale |
85 // factor is preferred. Otherwise, the default scale factor is used. | 102 // factor is preferred. Otherwise, the default scale factor is used. |
86 for (size_t i = 0; i < icons.size(); ++i) { | 103 for (size_t i = 0; i < icons.size(); ++i) { |
87 if (icons[i].density == density && | 104 if (icons[i].density == density && |
88 IconSizesContainsAny(icons[i].sizes)) { | 105 IconSizesContainsAny(icons[i].sizes)) { |
89 return icons[i].src; | 106 return i; |
90 } | 107 } |
91 | 108 |
92 // If there is an icon with 'any' but not the right density, keep it on the | 109 // If there is an icon with 'any' but not the right density, keep it on the |
93 // side and only use it if nothing better is found. | 110 // side and only use it if nothing better is found. |
94 if (icons[i].density == Manifest::Icon::kDefaultDensity && | 111 if (icons[i].density == Manifest::Icon::kDefaultDensity && |
95 IconSizesContainsAny(icons[i].sizes)) { | 112 IconSizesContainsAny(icons[i].sizes)) { |
96 url = icons[i].src; | 113 best_index = i; |
97 } | 114 } |
98 } | 115 } |
99 | 116 |
| 117 if (best_index != -1) |
| 118 return best_index; |
| 119 |
100 // The last pass will try to find the best suitable icon for the device's | 120 // The last pass will try to find the best suitable icon for the device's |
101 // scale factor. If none, another pass will be run using kDefaultDensity. | 121 // scale factor. If none, another pass will be run using kDefaultDensity. |
102 if (!url.is_valid()) | 122 best_index = FindBestMatchingIconForDensity(icons, density); |
103 url = FindBestMatchingIconForDensity(icons, density); | 123 if (best_index != -1 && |
104 if (!url.is_valid()) | 124 IconSizesContainsBiggerThanMinimumSize(icons[best_index].sizes)) |
105 url = FindBestMatchingIconForDensity(icons, | 125 return best_index; |
106 Manifest::Icon::kDefaultDensity); | |
107 | 126 |
108 return url; | 127 best_index = FindBestMatchingIconForDensity(icons, |
| 128 Manifest::Icon::kDefaultDensity); |
| 129 if (best_index != -1 && |
| 130 IconSizesContainsBiggerThanMinimumSize(icons[best_index].sizes)) |
| 131 return best_index; |
| 132 |
| 133 return -1; |
109 } | 134 } |
110 | 135 |
111 | 136 |
112 // static | 137 // static |
113 bool ManifestIconSelector::IconSizesContainsAny( | 138 bool ManifestIconSelector::IconSizesContainsAny( |
114 const std::vector<gfx::Size>& sizes) { | 139 const std::vector<gfx::Size>& sizes) { |
115 for (size_t i = 0; i < sizes.size(); ++i) { | 140 for (size_t i = 0; i < sizes.size(); ++i) { |
116 if (sizes[i].IsEmpty()) | 141 if (sizes[i].IsEmpty()) |
117 return true; | 142 return true; |
118 } | 143 } |
119 | |
120 return false; | 144 return false; |
121 } | 145 } |
122 | 146 |
123 // static | 147 // static |
124 std::vector<Manifest::Icon> ManifestIconSelector::FilterIconsByType( | 148 std::vector<Manifest::Icon> ManifestIconSelector::FilterIconsByType( |
125 const std::vector<content::Manifest::Icon>& icons) { | 149 const std::vector<content::Manifest::Icon>& icons) { |
126 std::vector<Manifest::Icon> result; | 150 std::vector<Manifest::Icon> result; |
127 | 151 |
128 for (size_t i = 0; i < icons.size(); ++i) { | 152 for (size_t i = 0; i < icons.size(); ++i) { |
129 if (icons[i].type.is_null() || | 153 if (icons[i].type.is_null() || |
130 mime_util::IsSupportedImageMimeType( | 154 mime_util::IsSupportedImageMimeType( |
131 base::UTF16ToUTF8(icons[i].type.string()))) { | 155 base::UTF16ToUTF8(icons[i].type.string()))) { |
132 result.push_back(icons[i]); | 156 result.push_back(icons[i]); |
133 } | 157 } |
134 } | 158 } |
135 | 159 |
136 return result; | 160 return result; |
137 } | 161 } |
138 | 162 |
139 // static | 163 // static |
140 GURL ManifestIconSelector::FindBestMatchingIcon( | 164 GURL ManifestIconSelector::FindBestMatchingIcon( |
141 const std::vector<Manifest::Icon>& unfiltered_icons, | 165 const std::vector<Manifest::Icon>& unfiltered_icons, |
142 const float preferred_icon_size_in_dp, | 166 const float preferred_icon_size_in_dp, |
143 const gfx::Screen* screen) { | 167 const gfx::Screen* screen) { |
144 const float device_scale_factor = | 168 const float device_scale_factor = |
145 screen->GetPrimaryDisplay().device_scale_factor(); | 169 screen->GetPrimaryDisplay().device_scale_factor(); |
146 const float preferred_icon_size_in_pixels = | 170 const float preferred_icon_size_in_pixels = |
147 preferred_icon_size_in_dp * device_scale_factor; | 171 preferred_icon_size_in_dp * device_scale_factor; |
148 | 172 |
149 ManifestIconSelector selector(preferred_icon_size_in_pixels); | 173 const int minimum_scale_factor = std::max( |
150 return selector.FindBestMatchingIcon(unfiltered_icons, device_scale_factor); | 174 static_cast<int>(floor(device_scale_factor - 1)), 1); |
| 175 const float minimum_icon_size_in_pixels = |
| 176 preferred_icon_size_in_dp * minimum_scale_factor; |
| 177 |
| 178 std::vector<Manifest::Icon> icons = |
| 179 ManifestIconSelector::FilterIconsByType(unfiltered_icons); |
| 180 |
| 181 ManifestIconSelector selector(preferred_icon_size_in_pixels, |
| 182 minimum_icon_size_in_pixels); |
| 183 int index = selector.FindBestMatchingIcon(icons, device_scale_factor); |
| 184 if (index == -1) |
| 185 return GURL(); |
| 186 return icons[index].src; |
151 } | 187 } |
OLD | NEW |