Chromium Code Reviews| 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_in_dp, | |
| 21 const ManifestIconDownloader::Callback& callback) { | |
| 22 content::WebContents* contents = web_contents(); | |
|
mlamouri (slow - plz ping)
2015/08/05 08:20:16
nit: no need for that, web_contents() is an access
Lalit Maganti
2015/08/05 14:47:13
Done.
| |
| 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( | |
|
mlamouri (slow - plz ping)
2015/08/05 08:20:16
It would be great to have unit tests for this meth
Lalit Maganti
2015/08/05 14:47:13
Added.
| |
| 38 int ideal_icon_size_in_dp, | |
| 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(); | |
|
mlamouri (slow - plz ping)
2015/08/05 08:20:16
ditto
Lalit Maganti
2015/08/05 14:47:13
Done.
| |
| 46 if (!contents || bitmaps.empty()) { | |
| 47 callback.Run(SkBitmap()); | |
| 48 return; | |
| 49 } | |
| 50 | |
| 51 const gfx::Screen* screen = | |
| 52 gfx::Screen::GetScreenFor(contents->GetNativeView()); | |
| 53 const float device_scale_factor = | |
| 54 screen->GetPrimaryDisplay().device_scale_factor(); | |
| 55 const float ideal_icon_size_in_px = | |
| 56 ideal_icon_size_in_dp * device_scale_factor; | |
| 57 | |
| 58 // This algorithm attempts to find an icon equal to the size we want or, | |
| 59 // failing that, an icon which is as close to our ideal size which we can | |
|
gone
2015/08/04 21:41:45
Other reviewers are very pretty nit-picky about co
Lalit Maganti
2015/08/05 14:47:13
Done.
| |
| 60 // scale down to our required size. We should hopefully have a URL provided | |
| 61 // to us which should be an icon close in size anyway, but we scale regardless | |
| 62 // of how big the icon is. | |
| 63 // This entire algorithm assumes square or very close to square icons. | |
| 64 const SkBitmap* best_upper = nullptr; | |
| 65 for (size_t i = 0; i < bitmaps.size(); ++i) { | |
| 66 const SkBitmap& current_bitmap = bitmaps[i]; | |
| 67 int height = current_bitmap.height(); | |
| 68 | |
| 69 if (height == ideal_icon_size_in_px) { | |
|
mlamouri (slow - plz ping)
2015/08/05 08:20:16
This is assuming height === width, right? Shouldn'
Lalit Maganti
2015/08/05 14:47:13
Done.
| |
| 70 callback.Run(current_bitmap); | |
| 71 return; | |
| 72 } | |
| 73 | |
| 74 if (height > ideal_icon_size_in_px && | |
| 75 (!best_upper || height < best_upper->height())) { | |
| 76 best_upper = ¤t_bitmap; | |
| 77 } | |
| 78 } | |
| 79 | |
| 80 if (best_upper) { | |
|
mlamouri (slow - plz ping)
2015/08/05 08:20:16
That's me or you don't actually close that { ?
Th
Lalit Maganti
2015/08/05 14:47:13
Done.
| |
| 81 callback.Run(skia::ImageOperations::Resize( | |
| 82 image, skia::ImageOperations::RESIZE_BEST, | |
| 83 ideal_icon_size_in_px, | |
| 84 ideal_icon_size_in_px)); | |
| 85 else | |
| 86 callback.Run(SkBitmap()); | |
| 87 } | |
| OLD | NEW |