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..95d91b0fb39af295e1168124886d2f9e8349b316 |
--- /dev/null |
+++ b/chrome/browser/manifest/manifest_icon_downloader.cc |
@@ -0,0 +1,105 @@ |
+// 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/web_contents.h" |
+#include "skia/ext/image_operations.h" |
+#include "ui/gfx/screen.h" |
+ |
+ManifestIconDownloader::ManifestIconDownloader( |
+ content::WebContents* web_contents) |
+ : WebContentsObserver(web_contents), |
+ weak_ptr_factory_(this) { |
+} |
+ |
+bool ManifestIconDownloader::Download( |
+ const GURL& icon_url, |
+ int ideal_icon_size, |
+ const ManifestIconDownloader::Callback& callback) { |
+ content::WebContents* contents = web_contents(); |
+ if (!contents || !icon_url.is_valid()) return false; |
+ |
+ contents->DownloadImage( |
+ icon_url, |
+ false, |
+ 0, |
+ false, |
+ base::Bind(&ManifestIconDownloader::OnIconFetched, |
+ weak_ptr_factory_.GetWeakPtr(), |
+ ideal_icon_size, |
+ callback)); |
+ return true; |
+} |
+ |
+void ManifestIconDownloader::OnIconFetched( |
+ int ideal_icon_size, |
gone
2015/07/31 18:09:20
rename this ideal_icon_size_in_units (dp?)
Lalit Maganti
2015/08/03 08:58:29
Will do.
|
+ const ManifestIconDownloader::Callback& callback, |
+ int id, |
+ int http_status_code, |
+ const GURL& url, |
+ const std::vector<SkBitmap>& bitmaps, |
+ const std::vector<gfx::Size>& sizes) { |
+ content::WebContents* contents = web_contents(); |
+ if (!contents || bitmaps.empty()) { |
+ callback.Run(SkBitmap()); |
+ return; |
+ } |
+ |
+ const gfx::Screen* screen = gfx::Screen::GetScreenFor(contents->GetNativeView()); |
+ const float device_scale_factor = |
+ screen->GetPrimaryDisplay().device_scale_factor(); |
+ const float scaled_ideal_icon_size = ideal_icon_size * device_scale_factor; |
gone
2015/07/31 18:09:20
rename this so that the unit is obvious
Lalit Maganti
2015/08/03 08:58:29
Will do.
|
+ |
+ const SkBitmap* best_upper = nullptr; |
gone
2015/07/31 18:09:20
best upper what? Document everything about the se
Lalit Maganti
2015/08/03 08:58:29
Will do.
|
+ const SkBitmap* best_lower = nullptr; |
+ |
+ // This entire algorithm assumes square or very close to square icons. |
+ for (size_t i = 0; i < bitmaps.size(); ++i) { |
+ const SkBitmap& current_bitmap = bitmaps[i]; |
+ int height = current_bitmap.height(); |
+ |
+ if (height == scaled_ideal_icon_size) { |
+ callback.Run(current_bitmap); |
+ return; |
+ } |
+ |
+ if (height > scaled_ideal_icon_size && |
+ (!best_upper || height < best_upper->height())) { |
+ best_upper = ¤t_bitmap; |
+ } else if (height < scaled_ideal_icon_size && |
+ (!best_lower || height > best_lower->height())) { |
+ best_lower = ¤t_bitmap; |
+ } |
+ } |
+ |
+ float soft_upper_bound = 1.5 * scaled_ideal_icon_size; |
gone
2015/07/31 18:09:20
How'd you come up with these bounds? 2/1 and 1/2
Lalit Maganti
2015/08/03 08:58:29
They are completely arbitary - I just put them in
gone
2015/08/03 23:27:58
I'd argue that having anything larger that you can
Lalit Maganti
2015/08/04 08:59:53
Right OK.
|
+ float soft_lower_bound = 0.9 * scaled_ideal_icon_size; |
+ |
+ float hard_upper_bound = 2.0 * scaled_ideal_icon_size; |
+ float hard_lower_bound = 0.5 * scaled_ideal_icon_size; |
+ |
+ if (best_upper && best_upper->height() < soft_upper_bound) { |
gone
2015/07/31 18:09:20
I don't think it ever makes sense to scale up if y
Lalit Maganti
2015/08/03 08:58:29
Hmmm this is an interesting one. I was thinking al
gone
2015/08/03 23:27:58
It is actually always preferable to scale down a 2
Lalit Maganti
2015/08/04 08:59:53
OK that sounds good to me - I can vastly simplify
|
+ ScaleAndCallback(*best_upper, scaled_ideal_icon_size, callback); |
+ } else if (best_lower && best_lower->height() > soft_lower_bound) { |
+ ScaleAndCallback(*best_lower, scaled_ideal_icon_size, callback); |
+ } else if (best_upper && best_upper->height() < hard_upper_bound) { |
+ ScaleAndCallback(*best_upper, scaled_ideal_icon_size, callback); |
+ } else if (best_lower && best_lower->height() > hard_lower_bound) { |
+ ScaleAndCallback(*best_lower, scaled_ideal_icon_size, callback); |
+ } else { |
+ callback.Run(SkBitmap()); |
+ } |
+} |
+ |
+void ManifestIconDownloader::ScaleAndCallback( |
+ const SkBitmap& image, |
+ const float scaled_ideal_icon_size, |
+ const Callback& callback) { |
+ callback.Run(skia::ImageOperations::Resize( |
+ image, skia::ImageOperations::RESIZE_BEST, |
+ scaled_ideal_icon_size, |
+ scaled_ideal_icon_size)); |
+} |