Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(186)

Side by Side Diff: chrome/browser/manifest/manifest_icon_downloader.cc

Issue 1261143004: Implement manifest icon downloader (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments on previous review Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 }
16
17 bool ManifestIconDownloader::Download(
18 const GURL& icon_url,
19 int ideal_icon_size_in_dp,
20 const ManifestIconDownloader::IconFetchCallback& callback) {
21 if (!web_contents() || !icon_url.is_valid()) return false;
22
23 const gfx::Screen* screen =
24 gfx::Screen::GetScreenFor(web_contents()->GetNativeView());
25 const float device_scale_factor =
26 screen->GetPrimaryDisplay().device_scale_factor();
27 const float ideal_icon_size_in_px =
28 ideal_icon_size_in_dp * device_scale_factor;
29
30 web_contents()->DownloadImage(
31 icon_url,
32 false,
33 0,
34 false,
mlamouri (slow - plz ping) 2015/08/07 10:11:11 Could you add comments after the three arguments a
Lalit Maganti 2015/08/07 13:04:30 Done.
35 base::Bind(&ManifestIconDownloader::OnIconFetched,
36 ideal_icon_size_in_px,
37 callback));
38 return true;
39 }
40
41 void ManifestIconDownloader::OnIconFetched(
42 int ideal_icon_size_in_px,
43 const ManifestIconDownloader::IconFetchCallback& callback,
44 int id,
45 int http_status_code,
46 const GURL& url,
47 const std::vector<SkBitmap>& bitmaps,
48 const std::vector<gfx::Size>& sizes) {
49 const int closest_index = FindClosestBitmapIndex(
50 ideal_icon_size_in_px, bitmaps);
51
52 if (closest_index == -1) {
53 callback.Run(SkBitmap());
54 return;
55 }
56
57 const SkBitmap& scaled = skia::ImageOperations::Resize(
mlamouri (slow - plz ping) 2015/08/07 10:11:11 We need to make sure this happens in the IO thread
Lalit Maganti 2015/08/07 13:04:30 Refactored to make resizing happen in background.
58 bitmaps[closest_index],
59 skia::ImageOperations::RESIZE_BEST,
60 ideal_icon_size_in_px,
61 ideal_icon_size_in_px);
62 callback.Run(scaled);
63 }
64
65 int ManifestIconDownloader::FindClosestBitmapIndex(
66 int ideal_icon_size_in_px,
67 const std::vector<SkBitmap>& bitmaps) {
68 if (bitmaps.empty())
69 return -1;
70
71 // This algorithm searches for an icon equal to the ideal size. Failing that,
72 // it picks the smallest icon larger than the ideal size to downscale. Icons
73 // which are not in a square shape are ignored.
74 const SkBitmap* best_upper = nullptr;
75 int best_upper_index = -1;
76
77 for (size_t i = 0; i < bitmaps.size(); ++i) {
mlamouri (slow - plz ping) 2015/08/07 10:11:11 Let's use some C++11 syntax sugar: for (const auto
Lalit Maganti 2015/08/07 13:04:30 I would if I could but I need the index to return
78 const SkBitmap& current_bitmap = bitmaps[i];
79 int height = current_bitmap.height();
mlamouri (slow - plz ping) 2015/08/07 10:11:11 no need to have a temporary variable here.
Lalit Maganti 2015/08/07 13:04:30 Done.
80 if (height != current_bitmap.width())
81 continue;
82
83 if (height == ideal_icon_size_in_px)
84 return i;
85
86 if (height > ideal_icon_size_in_px &&
87 (!best_upper || height < best_upper->height())) {
88 best_upper = &current_bitmap;
89 best_upper_index = i;
90 }
91 }
92
93 return best_upper_index;
94 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698