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

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: Fix compile 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/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 // This scale factor represents down to one density bucket below the device's
28 // bucket.
29 const float lowest_scale_factor = device_scale_factor - 1;
mlamouri (slow - plz ping) 2015/08/18 14:48:17 If the device_scale_factor is something like 3.5,
Lalit Maganti 2015/08/18 14:53:50 Oh yeah I forgot to comment on this. I want to fig
30 const float lowest_icon_size_in_px =
31 ideal_icon_size_in_dp * lowest_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 lowest_icon_size_in_px,
41 callback));
42 return true;
43 }
44
45 void ManifestIconDownloader::OnIconFetched(
46 int ideal_icon_size_in_px,
47 int lowest_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(
56 ideal_icon_size_in_px, lowest_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 const 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(
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 lowest_icon_size_in_px,
102 const std::vector<SkBitmap>& bitmaps) {
103 // This algorithm searches for an icon equal to the ideal size. Failing that,
104 // it picks the smallest icon larger than the ideal size. Failing that too, it
105 // finds an icon down to one density bucket below the device density. Icons
106 // which are not in a square shape are ignored.
107 const SkBitmap* best_upper = nullptr;
108 int best_upper_index = -1;
109
110 const SkBitmap* best_lower = nullptr;
111 int best_lower_index = -1;
112
113 for (size_t i = 0; i < bitmaps.size(); ++i) {
114 const SkBitmap& current_bitmap = bitmaps[i];
115 if (current_bitmap.height() != current_bitmap.width())
116 continue;
117
118 if (current_bitmap.height() == ideal_icon_size_in_px)
119 return i;
120
121 if (current_bitmap.height() > ideal_icon_size_in_px &&
mlamouri (slow - plz ping) 2015/08/18 14:48:18 what about something like: if (bigger than ideal s
Lalit Maganti 2015/08/18 14:53:50 The complexity in this one would be in the "better
122 (!best_upper || current_bitmap.height() < best_upper->height())) {
123 best_upper = &current_bitmap;
124 best_upper_index = i;
125 } else if (current_bitmap.height() < ideal_icon_size_in_px &&
126 current_bitmap.height() >= lowest_icon_size_in_px &&
127 (!best_lower || current_bitmap.height() > best_lower->height())) {
128 best_lower = &current_bitmap;
129 best_lower_index = i;
130 }
131 }
132
133 return best_upper_index == -1 ? best_lower_index : best_upper_index;
mlamouri (slow - plz ping) 2015/08/18 14:48:18 Why do you need two indexes?
Lalit Maganti 2015/08/18 14:53:50 Because I always want to pick an icon bigger than
134 }
OLDNEW
« no previous file with comments | « chrome/browser/manifest/manifest_icon_downloader.h ('k') | chrome/browser/manifest/manifest_icon_downloader_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698