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_downloader.h" | 5 #include "chrome/browser/manifest/manifest_icon_downloader.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 | 8 |
9 #include "chrome/browser/manifest/manifest_icon_selector.h" | 9 #include "chrome/browser/manifest/manifest_icon_selector.h" |
10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 if (closest_index == -1) { | 61 if (closest_index == -1) { |
62 callback.Run(SkBitmap()); | 62 callback.Run(SkBitmap()); |
63 return; | 63 return; |
64 } | 64 } |
65 | 65 |
66 const SkBitmap& chosen = bitmaps[closest_index]; | 66 const SkBitmap& chosen = bitmaps[closest_index]; |
67 | 67 |
68 // Only scale if we need to scale down. For scaling up we will let the system | 68 // Only scale if we need to scale down. For scaling up we will let the system |
69 // handle that when it is required to display it. This saves space in the | 69 // handle that when it is required to display it. This saves space in the |
70 // webapp storage system as well. | 70 // webapp storage system as well. |
71 if (chosen.height() > ideal_icon_size_in_px) { | 71 if (chosen.height() > ideal_icon_size_in_px || |
| 72 chosen.width() > ideal_icon_size_in_px) { |
72 content::BrowserThread::PostTask( | 73 content::BrowserThread::PostTask( |
73 content::BrowserThread::IO, | 74 content::BrowserThread::IO, |
74 FROM_HERE, | 75 FROM_HERE, |
75 base::Bind(&ManifestIconDownloader::ScaleIcon, | 76 base::Bind(&ManifestIconDownloader::ScaleIcon, |
76 ideal_icon_size_in_px, | 77 ideal_icon_size_in_px, |
77 chosen, | 78 chosen, |
78 callback)); | 79 callback)); |
79 return; | 80 return; |
80 } | 81 } |
81 | 82 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 | 120 |
120 if (best_delta > 0 && delta < 0) | 121 if (best_delta > 0 && delta < 0) |
121 continue; | 122 continue; |
122 | 123 |
123 if ((best_delta > 0 && delta < best_delta) || | 124 if ((best_delta > 0 && delta < best_delta) || |
124 (best_delta < 0 && delta > best_delta && delta >= max_negative_delta)) { | 125 (best_delta < 0 && delta > best_delta && delta >= max_negative_delta)) { |
125 best_index = i; | 126 best_index = i; |
126 best_delta = delta; | 127 best_delta = delta; |
127 } | 128 } |
128 } | 129 } |
| 130 |
| 131 if (best_index != -1) |
| 132 return best_index; |
| 133 |
| 134 // There was no square icon of a correct size found. Try to find the most |
| 135 // square-like icon which has both dimensions greater than the minimum size. |
| 136 float best_ratio_difference = std::numeric_limits<float>::infinity(); |
| 137 for (size_t i = 0; i < bitmaps.size(); ++i) { |
| 138 if (bitmaps[i].height() < minimum_icon_size_in_px || |
| 139 bitmaps[i].width() < minimum_icon_size_in_px) { |
| 140 continue; |
| 141 } |
| 142 |
| 143 float height = static_cast<float>(bitmaps[i].height()); |
| 144 float width = static_cast<float>(bitmaps[i].width()); |
| 145 float ratio = height / width; |
| 146 float ratio_difference = fabs(ratio - 1); |
| 147 if (ratio_difference < best_ratio_difference) { |
| 148 best_index = i; |
| 149 best_ratio_difference = ratio_difference; |
| 150 } |
| 151 } |
| 152 |
129 return best_index; | 153 return best_index; |
130 } | 154 } |
OLD | NEW |