OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/manifest/manifest_icon_downloader.h" | |
6 | |
7 #include "chrome/browser/manifest/manifest_icon_selector.h" | |
8 #include "content/public/browser/browser_thread.h" | |
9 #include "content/public/browser/web_contents.h" | |
10 #include "skia/ext/image_operations.h" | |
11 #include "ui/gfx/screen.h" | |
12 | |
13 bool ManifestIconDownloader::Download( | |
14 content::WebContents* web_contents, | |
15 const GURL& icon_url, | |
16 int ideal_icon_size_in_dp, | |
17 const ManifestIconDownloader::IconFetchCallback& callback) { | |
18 if (!web_contents || !icon_url.is_valid()) return false; | |
mlamouri (slow - plz ping)
2015/08/21 09:27:11
style: contrary to java, you must have |return fal
Lalit Maganti
2015/08/21 12:14:19
Done.
| |
19 | |
20 const gfx::Screen* screen = | |
21 gfx::Screen::GetScreenFor(web_contents->GetNativeView()); | |
22 | |
23 const float device_scale_factor = | |
24 screen->GetPrimaryDisplay().device_scale_factor(); | |
25 const float ideal_icon_size_in_px = | |
26 ideal_icon_size_in_dp * device_scale_factor; | |
27 | |
28 const int minimum_scale_factor = std::max( | |
29 static_cast<int>(floor(device_scale_factor - 1)), 1); | |
30 const float minimum_icon_size_in_px = | |
31 ideal_icon_size_in_dp * minimum_scale_factor; | |
32 | |
33 web_contents->DownloadImage( | |
34 icon_url, | |
35 false, // is_favicon | |
36 0, // max_bitmap_size - 0 means no maximum size. | |
37 false, // bypass_cache | |
38 base::Bind(&ManifestIconDownloader::OnIconFetched, | |
39 ideal_icon_size_in_px, | |
40 minimum_icon_size_in_px, | |
41 callback)); | |
42 return true; | |
43 } | |
44 | |
45 void ManifestIconDownloader::OnIconFetched( | |
46 int ideal_icon_size_in_px, | |
47 int minimum_icon_size_in_px, | |
48 const ManifestIconDownloader::IconFetchCallback& callback, | |
49 int id, | |
50 int http_status_code, | |
51 const GURL& url, | |
52 const std::vector<SkBitmap>& bitmaps, | |
53 const std::vector<gfx::Size>& sizes) { | |
54 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
55 const int closest_index = FindClosestBitmapIndex( | |
mlamouri (slow - plz ping)
2015/08/21 09:27:11
nit: leave a blank line between the DCHECK() and t
Lalit Maganti
2015/08/21 12:14:19
Done.
| |
56 ideal_icon_size_in_px, minimum_icon_size_in_px, bitmaps); | |
57 | |
58 if (closest_index == -1) { | |
59 callback.Run(SkBitmap()); | |
60 return; | |
61 } | |
62 | |
63 const SkBitmap& chosen = bitmaps[closest_index]; | |
64 | |
65 // Only scale if we need to scale down. For scaling up we will let the system | |
66 // handle that when it is required to display it. This saves space in the | |
67 // webapp storage system as well. | |
68 if (chosen.height() > ideal_icon_size_in_px) { | |
69 content::BrowserThread::PostTask( | |
70 content::BrowserThread::IO, | |
71 FROM_HERE, | |
72 base::Bind(&ManifestIconDownloader::ScaleIcon, | |
73 ideal_icon_size_in_px, | |
74 chosen, | |
75 callback)); | |
76 return; | |
77 } | |
78 | |
79 callback.Run(chosen); | |
80 } | |
81 | |
82 void ManifestIconDownloader::ScaleIcon( | |
83 int ideal_icon_size_in_px, | |
84 const SkBitmap& bitmap, | |
85 const ManifestIconDownloader::IconFetchCallback& callback) { | |
86 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
87 | |
88 const SkBitmap& scaled = skia::ImageOperations::Resize( | |
89 bitmap, | |
90 skia::ImageOperations::RESIZE_BEST, | |
91 ideal_icon_size_in_px, | |
92 ideal_icon_size_in_px); | |
93 content::BrowserThread::PostTask( | |
mlamouri (slow - plz ping)
2015/08/21 09:27:11
nit: I would add a blank line, up to you.
Lalit Maganti
2015/08/21 12:14:19
Done.
| |
94 content::BrowserThread::UI, | |
95 FROM_HERE, | |
96 base::Bind(callback, scaled)); | |
97 } | |
98 | |
99 int ManifestIconDownloader::FindClosestBitmapIndex( | |
100 int ideal_icon_size_in_px, | |
101 int minimum_icon_size_in_px, | |
102 const std::vector<SkBitmap>& bitmaps) { | |
103 // There might be multiple bitmaps returned. The one to pick is bigger or | |
104 // equal to the preferred size. |bitmaps| is ordered from bigger to smaller. | |
105 int best_index = -1; | |
106 for (size_t i = 0; i < bitmaps.size(); ++i) { | |
107 if (bitmaps[i].height() != bitmaps[i].width()) | |
108 continue; | |
109 if (bitmaps[i].height() == ideal_icon_size_in_px) | |
110 return i; | |
111 if (bitmaps[i].height() < ideal_icon_size_in_px) { | |
112 // best_index == -1 means the first good icon which has been found | |
gone
2015/08/20 18:48:42
if you have to explicitly explain what -1 means, d
Lalit Maganti
2015/08/21 12:14:19
Code has changed enough such that use of -1 is muc
| |
113 // is smaller than the ideal size. It can still be used though if it is | |
114 // bigger than the minimum. | |
mlamouri (slow - plz ping)
2015/08/21 09:27:11
Hmm, if you found an icon bigger than minimum and
Lalit Maganti
2015/08/21 12:14:19
Changed as discussed offline.
| |
115 if (best_index == -1) | |
116 return bitmaps[i].height() >= minimum_icon_size_in_px ? i : -1; | |
117 break; | |
118 } | |
119 best_index = i; | |
120 } | |
121 return best_index; | |
122 } | |
OLD | NEW |