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

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: Rebase 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 <limits>
8
9 #include "chrome/browser/manifest/manifest_icon_selector.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "content/public/browser/web_contents.h"
12 #include "skia/ext/image_operations.h"
13 #include "ui/gfx/screen.h"
14
15 bool ManifestIconDownloader::Download(
16 content::WebContents* web_contents,
17 const GURL& icon_url,
18 int ideal_icon_size_in_dp,
19 const ManifestIconDownloader::IconFetchCallback& callback) {
20 if (!web_contents || !icon_url.is_valid())
21 return false;
22
23 const gfx::Screen* screen =
24 gfx::Screen::GetScreenFor(web_contents->GetNativeView());
25
26 const float device_scale_factor =
27 screen->GetPrimaryDisplay().device_scale_factor();
28 const float ideal_icon_size_in_px =
29 ideal_icon_size_in_dp * device_scale_factor;
30
31 const int minimum_scale_factor = std::max(
32 static_cast<int>(floor(device_scale_factor - 1)), 1);
33 const float minimum_icon_size_in_px =
34 ideal_icon_size_in_dp * minimum_scale_factor;
35
36 web_contents->DownloadImage(
37 icon_url,
38 false, // is_favicon
39 0, // max_bitmap_size - 0 means no maximum size.
40 false, // bypass_cache
41 base::Bind(&ManifestIconDownloader::OnIconFetched,
42 ideal_icon_size_in_px,
43 minimum_icon_size_in_px,
44 callback));
45 return true;
46 }
47
48 void ManifestIconDownloader::OnIconFetched(
49 int ideal_icon_size_in_px,
50 int minimum_icon_size_in_px,
51 const ManifestIconDownloader::IconFetchCallback& callback,
52 int id,
53 int http_status_code,
54 const GURL& url,
55 const std::vector<SkBitmap>& bitmaps,
56 const std::vector<gfx::Size>& sizes) {
57 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
58
59 const int closest_index = FindClosestBitmapIndex(
60 ideal_icon_size_in_px, minimum_icon_size_in_px, bitmaps);
61
62 if (closest_index == -1) {
63 callback.Run(SkBitmap());
64 return;
65 }
66
67 const SkBitmap& chosen = bitmaps[closest_index];
68
69 // Only scale if we need to scale down. For scaling up we will let the system
70 // handle that when it is required to display it. This saves space in the
71 // webapp storage system as well.
72 if (chosen.height() > ideal_icon_size_in_px) {
73 content::BrowserThread::PostTask(
74 content::BrowserThread::IO,
75 FROM_HERE,
76 base::Bind(&ManifestIconDownloader::ScaleIcon,
77 ideal_icon_size_in_px,
78 chosen,
79 callback));
80 return;
81 }
82
83 callback.Run(chosen);
84 }
85
86 void ManifestIconDownloader::ScaleIcon(
87 int ideal_icon_size_in_px,
88 const SkBitmap& bitmap,
89 const ManifestIconDownloader::IconFetchCallback& callback) {
90 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
91
92 const SkBitmap& scaled = skia::ImageOperations::Resize(
93 bitmap,
94 skia::ImageOperations::RESIZE_BEST,
95 ideal_icon_size_in_px,
96 ideal_icon_size_in_px);
97
98 content::BrowserThread::PostTask(
99 content::BrowserThread::UI,
100 FROM_HERE,
101 base::Bind(callback, scaled));
102 }
103
104 int ManifestIconDownloader::FindClosestBitmapIndex(
105 int ideal_icon_size_in_px,
106 int minimum_icon_size_in_px,
107 const std::vector<SkBitmap>& bitmaps) {
108 int best_index = -1;
109 int best_delta = std::numeric_limits<int>::min();
110 const int max_negative_delta =
111 minimum_icon_size_in_px - ideal_icon_size_in_px;
112
113 for (size_t i = 0; i < bitmaps.size(); ++i) {
114 if (bitmaps[i].height() != bitmaps[i].width())
115 continue;
116
117 int delta = bitmaps[i].width() - ideal_icon_size_in_px;
118 if (delta == 0)
119 return i;
120
121 if (best_delta > 0 && delta < 0)
122 continue;
123
124 if ((best_delta > 0 && delta < best_delta) ||
125 (best_delta < 0 && delta > best_delta && delta >= max_negative_delta)) {
126 best_index = i;
127 best_delta = delta;
128 }
129 }
130 return best_index;
131 }
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