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/web_contents.h" | |
9 #include "skia/ext/image_operations.h" | |
10 #include "ui/gfx/screen.h" | |
11 | |
12 ManifestIconDownloader::ManifestIconDownloader( | |
13 content::WebContents* web_contents) | |
14 : WebContentsObserver(web_contents), | |
15 weak_ptr_factory_(this) { | |
16 } | |
17 | |
18 bool ManifestIconDownloader::Download( | |
19 const GURL& icon_url, | |
20 int ideal_icon_size, | |
21 const ManifestIconDownloader::Callback& callback) { | |
22 content::WebContents* contents = web_contents(); | |
23 if (!contents || !icon_url.is_valid()) return false; | |
24 | |
25 contents->DownloadImage( | |
26 icon_url, | |
27 false, | |
28 0, | |
29 false, | |
30 base::Bind(&ManifestIconDownloader::OnIconFetched, | |
31 weak_ptr_factory_.GetWeakPtr(), | |
32 ideal_icon_size, | |
33 callback)); | |
34 return true; | |
35 } | |
36 | |
37 void ManifestIconDownloader::OnIconFetched( | |
38 int ideal_icon_size, | |
gone
2015/07/31 18:09:20
rename this ideal_icon_size_in_units (dp?)
Lalit Maganti
2015/08/03 08:58:29
Will do.
| |
39 const ManifestIconDownloader::Callback& callback, | |
40 int id, | |
41 int http_status_code, | |
42 const GURL& url, | |
43 const std::vector<SkBitmap>& bitmaps, | |
44 const std::vector<gfx::Size>& sizes) { | |
45 content::WebContents* contents = web_contents(); | |
46 if (!contents || bitmaps.empty()) { | |
47 callback.Run(SkBitmap()); | |
48 return; | |
49 } | |
50 | |
51 const gfx::Screen* screen = gfx::Screen::GetScreenFor(contents->GetNativeView( )); | |
52 const float device_scale_factor = | |
53 screen->GetPrimaryDisplay().device_scale_factor(); | |
54 const float scaled_ideal_icon_size = ideal_icon_size * device_scale_factor; | |
gone
2015/07/31 18:09:20
rename this so that the unit is obvious
Lalit Maganti
2015/08/03 08:58:29
Will do.
| |
55 | |
56 const SkBitmap* best_upper = nullptr; | |
gone
2015/07/31 18:09:20
best upper what? Document everything about the se
Lalit Maganti
2015/08/03 08:58:29
Will do.
| |
57 const SkBitmap* best_lower = nullptr; | |
58 | |
59 // This entire algorithm assumes square or very close to square icons. | |
60 for (size_t i = 0; i < bitmaps.size(); ++i) { | |
61 const SkBitmap& current_bitmap = bitmaps[i]; | |
62 int height = current_bitmap.height(); | |
63 | |
64 if (height == scaled_ideal_icon_size) { | |
65 callback.Run(current_bitmap); | |
66 return; | |
67 } | |
68 | |
69 if (height > scaled_ideal_icon_size && | |
70 (!best_upper || height < best_upper->height())) { | |
71 best_upper = ¤t_bitmap; | |
72 } else if (height < scaled_ideal_icon_size && | |
73 (!best_lower || height > best_lower->height())) { | |
74 best_lower = ¤t_bitmap; | |
75 } | |
76 } | |
77 | |
78 float soft_upper_bound = 1.5 * scaled_ideal_icon_size; | |
gone
2015/07/31 18:09:20
How'd you come up with these bounds? 2/1 and 1/2
Lalit Maganti
2015/08/03 08:58:29
They are completely arbitary - I just put them in
gone
2015/08/03 23:27:58
I'd argue that having anything larger that you can
Lalit Maganti
2015/08/04 08:59:53
Right OK.
| |
79 float soft_lower_bound = 0.9 * scaled_ideal_icon_size; | |
80 | |
81 float hard_upper_bound = 2.0 * scaled_ideal_icon_size; | |
82 float hard_lower_bound = 0.5 * scaled_ideal_icon_size; | |
83 | |
84 if (best_upper && best_upper->height() < soft_upper_bound) { | |
gone
2015/07/31 18:09:20
I don't think it ever makes sense to scale up if y
Lalit Maganti
2015/08/03 08:58:29
Hmmm this is an interesting one. I was thinking al
gone
2015/08/03 23:27:58
It is actually always preferable to scale down a 2
Lalit Maganti
2015/08/04 08:59:53
OK that sounds good to me - I can vastly simplify
| |
85 ScaleAndCallback(*best_upper, scaled_ideal_icon_size, callback); | |
86 } else if (best_lower && best_lower->height() > soft_lower_bound) { | |
87 ScaleAndCallback(*best_lower, scaled_ideal_icon_size, callback); | |
88 } else if (best_upper && best_upper->height() < hard_upper_bound) { | |
89 ScaleAndCallback(*best_upper, scaled_ideal_icon_size, callback); | |
90 } else if (best_lower && best_lower->height() > hard_lower_bound) { | |
91 ScaleAndCallback(*best_lower, scaled_ideal_icon_size, callback); | |
92 } else { | |
93 callback.Run(SkBitmap()); | |
94 } | |
95 } | |
96 | |
97 void ManifestIconDownloader::ScaleAndCallback( | |
98 const SkBitmap& image, | |
99 const float scaled_ideal_icon_size, | |
100 const Callback& callback) { | |
101 callback.Run(skia::ImageOperations::Resize( | |
102 image, skia::ImageOperations::RESIZE_BEST, | |
103 scaled_ideal_icon_size, | |
104 scaled_ideal_icon_size)); | |
105 } | |
OLD | NEW |