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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/manifest/manifest_icon_downloader.cc
diff --git a/chrome/browser/manifest/manifest_icon_downloader.cc b/chrome/browser/manifest/manifest_icon_downloader.cc
new file mode 100644
index 0000000000000000000000000000000000000000..4b59958423675c52d99dfc217fd9f29fd3ee78f1
--- /dev/null
+++ b/chrome/browser/manifest/manifest_icon_downloader.cc
@@ -0,0 +1,134 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/manifest/manifest_icon_downloader.h"
+
+#include "chrome/browser/manifest/manifest_icon_selector.h"
+#include "content/public/browser/browser_thread.h"
+#include "content/public/browser/web_contents.h"
+#include "skia/ext/image_operations.h"
+#include "ui/gfx/screen.h"
+
+bool ManifestIconDownloader::Download(
+ content::WebContents* web_contents,
+ const GURL& icon_url,
+ int ideal_icon_size_in_dp,
+ const ManifestIconDownloader::IconFetchCallback& callback) {
+ if (!web_contents || !icon_url.is_valid()) return false;
+
+ const gfx::Screen* screen =
+ gfx::Screen::GetScreenFor(web_contents->GetNativeView());
+ const float device_scale_factor =
+ screen->GetPrimaryDisplay().device_scale_factor();
+ const float ideal_icon_size_in_px =
+ ideal_icon_size_in_dp * device_scale_factor;
+
+ // This scale factor represents down to one density bucket below the device's
+ // bucket.
+ 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
+ const float lowest_icon_size_in_px =
+ ideal_icon_size_in_dp * lowest_scale_factor;
+
+ web_contents->DownloadImage(
+ icon_url,
+ false, // is_favicon
+ 0, // max_bitmap_size - 0 means no maximum size.
+ false, // bypass_cache
+ base::Bind(&ManifestIconDownloader::OnIconFetched,
+ ideal_icon_size_in_px,
+ lowest_icon_size_in_px,
+ callback));
+ return true;
+}
+
+void ManifestIconDownloader::OnIconFetched(
+ int ideal_icon_size_in_px,
+ int lowest_icon_size_in_px,
+ const ManifestIconDownloader::IconFetchCallback& callback,
+ int id,
+ int http_status_code,
+ const GURL& url,
+ const std::vector<SkBitmap>& bitmaps,
+ const std::vector<gfx::Size>& sizes) {
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
+ const int closest_index = FindClosestBitmapIndex(
+ ideal_icon_size_in_px, lowest_icon_size_in_px, bitmaps);
+
+ if (closest_index == -1) {
+ callback.Run(SkBitmap());
+ return;
+ }
+
+ const SkBitmap& chosen = bitmaps[closest_index];
+
+ // Only scale if we need to scale down. For scaling up we will let the system
+ // handle that when it is required to display it. This saves space in the
+ // webapp storage system as well.
+ if (chosen.height() > ideal_icon_size_in_px) {
+ content::BrowserThread::PostTask(
+ content::BrowserThread::IO,
+ FROM_HERE,
+ base::Bind(&ManifestIconDownloader::ScaleIcon,
+ ideal_icon_size_in_px,
+ chosen,
+ callback));
+ return;
+ }
+
+ callback.Run(chosen);
+}
+
+void ManifestIconDownloader::ScaleIcon(
+ const int ideal_icon_size_in_px,
+ const SkBitmap& bitmap,
+ const ManifestIconDownloader::IconFetchCallback& callback) {
+ DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
+
+ const SkBitmap& scaled = skia::ImageOperations::Resize(
+ bitmap,
+ skia::ImageOperations::RESIZE_BEST,
+ ideal_icon_size_in_px,
+ ideal_icon_size_in_px);
+ content::BrowserThread::PostTask(
+ content::BrowserThread::UI,
+ FROM_HERE,
+ base::Bind(callback, scaled));
+}
+
+int ManifestIconDownloader::FindClosestBitmapIndex(
+ int ideal_icon_size_in_px,
+ int lowest_icon_size_in_px,
+ const std::vector<SkBitmap>& bitmaps) {
+ // This algorithm searches for an icon equal to the ideal size. Failing that,
+ // it picks the smallest icon larger than the ideal size. Failing that too, it
+ // finds an icon down to one density bucket below the device density. Icons
+ // which are not in a square shape are ignored.
+ const SkBitmap* best_upper = nullptr;
+ int best_upper_index = -1;
+
+ const SkBitmap* best_lower = nullptr;
+ int best_lower_index = -1;
+
+ for (size_t i = 0; i < bitmaps.size(); ++i) {
+ const SkBitmap& current_bitmap = bitmaps[i];
+ if (current_bitmap.height() != current_bitmap.width())
+ continue;
+
+ if (current_bitmap.height() == ideal_icon_size_in_px)
+ return i;
+
+ 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
+ (!best_upper || current_bitmap.height() < best_upper->height())) {
+ best_upper = &current_bitmap;
+ best_upper_index = i;
+ } else if (current_bitmap.height() < ideal_icon_size_in_px &&
+ current_bitmap.height() >= lowest_icon_size_in_px &&
+ (!best_lower || current_bitmap.height() > best_lower->height())) {
+ best_lower = &current_bitmap;
+ best_lower_index = i;
+ }
+ }
+
+ 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
+}
« 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