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; | |
19 | |
20 const gfx::Screen* screen = | |
21 gfx::Screen::GetScreenFor(web_contents->GetNativeView()); | |
22 const float device_scale_factor = | |
23 screen->GetPrimaryDisplay().device_scale_factor(); | |
24 const float ideal_icon_size_in_px = | |
25 ideal_icon_size_in_dp * device_scale_factor; | |
26 | |
27 web_contents->DownloadImage( | |
28 icon_url, | |
29 false, // is_favicon | |
30 0, // max_bitmap_size - 0 means no maximum size. | |
31 false, // bypass_cache | |
32 base::Bind(&ManifestIconDownloader::OnIconFetched, | |
33 ideal_icon_size_in_px, | |
34 callback)); | |
35 return true; | |
36 } | |
37 | |
38 void ManifestIconDownloader::OnIconFetched( | |
39 int ideal_icon_size_in_px, | |
40 const ManifestIconDownloader::IconFetchCallback& callback, | |
41 int id, | |
42 int http_status_code, | |
43 const GURL& url, | |
44 const std::vector<SkBitmap>& bitmaps, | |
45 const std::vector<gfx::Size>& sizes) { | |
46 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
47 const int closest_index = FindClosestBitmapIndex( | |
48 ideal_icon_size_in_px, bitmaps); | |
49 | |
50 if (closest_index == -1) { | |
51 callback.Run(SkBitmap()); | |
52 return; | |
53 } | |
54 | |
55 const SkBitmap& chosen = bitmaps[closest_index]; | |
56 | |
57 // Only scale if we need to scale down. For scaling up we will let the system | |
gone
2015/08/19 22:21:07
Given that FindClosestBitmapIndex() only returns b
Lalit Maganti
2015/08/20 16:48:00
Ah the algortihm doesn't take into account minimum
| |
58 // handle that when it is required to display it. This saves space in the | |
59 // webapp storage system as well. | |
60 if (chosen.height() > ideal_icon_size_in_px) { | |
61 content::BrowserThread::PostTask( | |
62 content::BrowserThread::IO, | |
63 FROM_HERE, | |
64 base::Bind(&ManifestIconDownloader::ScaleIcon, | |
65 ideal_icon_size_in_px, | |
66 chosen, | |
67 callback)); | |
68 return; | |
69 } | |
70 | |
71 callback.Run(chosen); | |
72 } | |
73 | |
74 void ManifestIconDownloader::ScaleIcon( | |
75 const int ideal_icon_size_in_px, | |
76 const SkBitmap& bitmap, | |
77 const ManifestIconDownloader::IconFetchCallback& callback) { | |
78 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
79 | |
80 const SkBitmap& scaled = skia::ImageOperations::Resize( | |
81 bitmap, | |
82 skia::ImageOperations::RESIZE_BEST, | |
83 ideal_icon_size_in_px, | |
84 ideal_icon_size_in_px); | |
85 content::BrowserThread::PostTask( | |
86 content::BrowserThread::UI, | |
87 FROM_HERE, | |
88 base::Bind(callback, scaled)); | |
89 } | |
90 | |
91 int ManifestIconDownloader::FindClosestBitmapIndex( | |
92 int ideal_icon_size_in_px, | |
93 const std::vector<SkBitmap>& bitmaps) { | |
94 // There might be multiple bitmaps returned. The one to pick is bigger or | |
95 // equal to the preferred size. |bitmaps| is ordered from bigger to smaller. | |
gone
2015/08/19 22:21:07
the explicit restriction on |bitmaps| should be pu
Lalit Maganti
2015/08/20 16:48:00
Done.
| |
96 int best_index = -1; | |
97 for (size_t i = 0; i < bitmaps.size(); ++i) { | |
98 if (bitmaps[i].height() != bitmaps[i].width()) | |
99 continue; | |
100 if (bitmaps[i].height() == ideal_icon_size_in_px) | |
101 return i; | |
102 if (bitmaps[i].height() < ideal_icon_size_in_px) | |
103 break; | |
104 best_index = i; | |
105 } | |
106 return best_index; | |
107 } | |
OLD | NEW |