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

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 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/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 ManifestIconDownloader::ManifestIconDownloader(
14 content::WebContents* web_contents)
15 : WebContentsObserver(web_contents) {
16 }
17
18 bool ManifestIconDownloader::Download(
19 const GURL& icon_url,
20 int ideal_icon_size_in_dp,
21 const ManifestIconDownloader::IconFetchCallback& callback) {
22 if (!web_contents() || !icon_url.is_valid()) return false;
23
24 const gfx::Screen* screen =
25 gfx::Screen::GetScreenFor(web_contents()->GetNativeView());
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 web_contents()->DownloadImage(
32 icon_url,
33 false, // is_favicon
34 0, // max_bitmap_size - 0 means no maximum size.
35 false, // bypass_cache
36 base::Bind(&ManifestIconDownloader::OnIconFetched,
37 ideal_icon_size_in_px,
38 callback));
39 return true;
40 }
41
42 void ManifestIconDownloader::OnIconFetched(
43 int ideal_icon_size_in_px,
44 const ManifestIconDownloader::IconFetchCallback& callback,
45 int id,
46 int http_status_code,
47 const GURL& url,
48 const std::vector<SkBitmap>& bitmaps,
49 const std::vector<gfx::Size>& sizes) {
50 const int closest_index = FindClosestBitmapIndex(
mlamouri (slow - plz ping) 2015/08/10 15:08:08 nit: add |DCHECK_CURRENTLY_ON(content::BrowserThre
Lalit Maganti 2015/08/10 16:55:45 Done.
51 ideal_icon_size_in_px, bitmaps);
52
53 if (closest_index == -1) {
54 callback.Run(SkBitmap());
55 return;
56 }
57
58 const SkBitmap& chosen = bitmaps[closest_index];
59 if (chosen.height() != ideal_icon_size_in_px) {
60 content::BrowserThread::PostTask(
61 content::BrowserThread::IO,
62 FROM_HERE,
63 base::Bind(&ManifestIconDownloader::ScaleIcon,
64 ideal_icon_size_in_px,
65 chosen,
66 callback));
67 return;
68 }
69
70 callback.Run(chosen);
71 }
72
73 void ManifestIconDownloader::ScaleIcon(
74 const int ideal_icon_size_in_px,
75 const SkBitmap& bitmap,
76 const ManifestIconDownloader::IconFetchCallback& callback) {
77 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
78
79 const SkBitmap& scaled = skia::ImageOperations::Resize(
80 bitmap,
81 skia::ImageOperations::RESIZE_BEST,
82 ideal_icon_size_in_px,
83 ideal_icon_size_in_px);
84 content::BrowserThread::PostTask(
85 content::BrowserThread::UI,
86 FROM_HERE,
87 base::Bind(callback, scaled));
88 }
89
90 void ManifestIconDownloader::RunCallbackOnUIThread(
mlamouri (slow - plz ping) 2015/08/10 15:08:08 You don't actually use that, maybe you could remov
Lalit Maganti 2015/08/10 16:55:45 Done.
91 const ManifestIconDownloader::IconFetchCallback& callback,
92 const SkBitmap& bitmap) {
93 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
94 callback.Run(bitmap);
95 }
96
97 int ManifestIconDownloader::FindClosestBitmapIndex(
98 int ideal_icon_size_in_px,
99 const std::vector<SkBitmap>& bitmaps) {
100 // This algorithm searches for an icon equal to the ideal size. Failing that,
101 // it picks the smallest icon larger than the ideal size to downscale. Icons
102 // which are not in a square shape are ignored.
103 const SkBitmap* best_upper = nullptr;
104 int best_upper_index = -1;
105
106 for (size_t i = 0; i < bitmaps.size(); ++i) {
107 const SkBitmap& current_bitmap = bitmaps[i];
108 if (current_bitmap.height() != current_bitmap.width())
109 continue;
110
111 if (current_bitmap.height() == ideal_icon_size_in_px)
112 return i;
113
114 if (current_bitmap.height() > ideal_icon_size_in_px &&
115 (!best_upper || current_bitmap.height() < best_upper->height())) {
116 best_upper = &current_bitmap;
117 best_upper_index = i;
118 }
119 }
120
121 return best_upper_index;
122 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698