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

Unified Diff: chrome/browser/ui/webui/large_icon_source.cc

Issue 1016833003: [Icons NTP] Add chrome://large-icon host, which resizes icon and renders fallback. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 9 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/ui/webui/large_icon_source.cc
diff --git a/chrome/browser/ui/webui/large_icon_source.cc b/chrome/browser/ui/webui/large_icon_source.cc
new file mode 100644
index 0000000000000000000000000000000000000000..97b7625d6f1069172bc8f4ce5e2ff4e08c6ec0cf
--- /dev/null
+++ b/chrome/browser/ui/webui/large_icon_source.cc
@@ -0,0 +1,182 @@
+// 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/ui/webui/large_icon_source.h"
+
+#include <string>
+#include <vector>
+
+#include "base/memory/ref_counted_memory.h"
+#include "chrome/browser/favicon/favicon_service_factory.h"
+#include "chrome/browser/profiles/profile.h"
+#include "chrome/browser/search/instant_io_context.h"
+#include "chrome/common/favicon/large_icon_url_parser.h"
+#include "chrome/common/url_constants.h"
+#include "components/favicon/core/browser/favicon_service.h"
+#include "components/favicon_base/fallback_icon_style.h"
+#include "grit/platform_locale_settings.h"
+#include "net/url_request/url_request.h"
+#include "skia/ext/image_operations.h"
+#include "third_party/skia/include/core/SkColor.h"
+#include "ui/base/l10n/l10n_util.h"
+#include "ui/gfx/codec/png_codec.h"
+#include "ui/gfx/geometry/size.h"
+#include "ui/gfx/image/image_png_rep.h"
+#include "ui/gfx/image/image_skia.h"
+
+namespace {
+
+int kDefaultLargeIconSize = 96;
+int kMaxLargeIconSize = 512; // Arbitrary bound to safegard endpoint.
beaudoin 2015/03/17 23:08:36 Looks really big. I'd be fine limiting it more. Ac
huangs 2015/03/18 22:54:11 Done.
+
+} // namespace
+
+LargeIconSource::IconRequest::IconRequest() : size(kDefaultLargeIconSize) {
+}
+
+LargeIconSource::IconRequest::IconRequest(
+ const content::URLDataSource::GotDataCallback& callback_in,
+ const GURL& url_in,
+ int size_in)
+ : callback(callback_in),
+ url(url_in),
+ size(size_in) {
+}
+
+LargeIconSource::IconRequest::~IconRequest() {
+}
+
+LargeIconSource::LargeIconSource(Profile* profile)
+ : profile_(profile),
+ render_fallback_on_failure_(true) {
beaudoin 2015/03/17 23:08:36 Overdesign? All constructors set it to true: take
huangs 2015/03/18 22:54:11 Done.
+ std::vector<std::string> font_list;
+#if defined(OS_CHROMEOS)
+ font_list.push_back("Noto Sans");
+#elif defined(OS_IOS)
+ font_list.push_back("Helvetica Neue");
beaudoin 2015/03/17 23:08:36 Isn't there a better way to access the platform's
huangs 2015/03/18 22:54:11 That's the purpose of my refactoring CL https://ch
beaudoin 2015/03/19 01:50:18 Acknowledged.
+#else
+ font_list.push_back(l10n_util::GetStringUTF8(IDS_SANS_SERIF_FONT_FAMILY));
+#endif
+ fallback_icon_service_.reset(
+ new favicon_base::FallbackIconService(font_list));
+}
+
+LargeIconSource::~LargeIconSource() {
+}
+
+std::string LargeIconSource::GetSource() const {
+ return chrome::kChromeUILargeIconHost;
+}
+
+void LargeIconSource::StartDataRequest(
+ const std::string& path,
+ int render_process_id,
+ int render_frame_id,
+ const content::URLDataSource::GotDataCallback& callback) {
+ chrome::LargeIconUrlParser parser;
+ bool success = parser.Parse(path);
+ if (!success || parser.size_in_pixels() <= 0
+ || parser.size_in_pixels() > kMaxLargeIconSize) {
beaudoin 2015/03/17 23:08:36 More conventional in Chrome to have the || on the
huangs 2015/03/18 22:54:11 Done.
+ SendNotFoundResponse(callback);
+ return;
+ }
+
+ FaviconService* favicon_service = FaviconServiceFactory::GetForProfile(
+ profile_, ServiceAccessType::EXPLICIT_ACCESS);
+ if (!favicon_service) {
+ SendNotFoundResponse(callback);
beaudoin 2015/03/17 23:08:36 Do we want to send a fallback icon in this case? (
huangs 2015/03/18 22:54:11 |favicon_service| this is an exceptional case. Be
beaudoin 2015/03/19 01:50:18 Acknowledged.
+ return;
+ }
+
+ GURL url(parser.url_string());
+ if (!url.is_valid()) {
+ SendNotFoundResponse(callback);
+ return;
+ }
+
+ favicon_service->GetGenericLargeIconForPageURL(
+ url,
+ parser.size_in_pixels(),
+ base::Bind(
+ &LargeIconSource::OnIconDataAvailable,
+ base::Unretained(this),
+ IconRequest(callback, url, parser.size_in_pixels())),
+ &cancelable_task_tracker_);
+}
+
+std::string LargeIconSource::GetMimeType(const std::string&) const {
+ // We need to explicitly return a mime type, otherwise if the user tries to
+ // drag the image they get no extension.
+ return "image/png";
+}
+
+bool LargeIconSource::ShouldReplaceExistingSource() const {
+ // Leave the existing DataSource in place, otherwise we'll drop any pending
+ // requests on the floor.
+ return false;
+}
+
+bool LargeIconSource::ShouldServiceRequest(
+ const net::URLRequest* request) const {
+ if (request->url().SchemeIs(chrome::kChromeSearchScheme))
+ return InstantIOContext::ShouldServiceRequest(request);
+ return URLDataSource::ShouldServiceRequest(request);
+}
+
+void LargeIconSource::OnIconDataAvailable(
+ const IconRequest& request,
+ const favicon_base::FaviconRawBitmapResult& bitmap_result) {
+ if (!bitmap_result.is_valid()) {
+ OnIconDataError(request);
+ return;
+ }
+ // Resize the retrieved large icon.
beaudoin 2015/03/17 23:08:36 Indentation.
huangs 2015/03/18 22:54:11 Can remove resizing code thanks to your insight in
beaudoin 2015/03/19 01:50:18 Acknowledged.
+ std::vector<gfx::ImagePNGRep> png_reps;
+ png_reps.push_back(gfx::ImagePNGRep(bitmap_result.bitmap_data, 1.0f));
+ gfx::Image image(png_reps);
+ if (image.IsEmpty()) {
+ OnIconDataError(request);
+ return;
+ }
+
+ SkBitmap resized_bitmap =
+ skia::ImageOperations::Resize(*image.ToSkBitmap(),
+ skia::ImageOperations::RESIZE_LANCZOS3,
+ request.size,
+ request.size);
+
+ // Send resized bitmap to the networking system.
+ std::vector<unsigned char> bitmap_data;
+ if (!gfx::PNGCodec::EncodeBGRASkBitmap(
+ resized_bitmap, false, &bitmap_data)) {
+ OnIconDataError(request);
+ return;
+ }
+
+ request.callback.Run(base::RefCountedBytes::TakeVector(&bitmap_data));
+}
+
+void LargeIconSource::OnIconDataError(const IconRequest& request) {
+ if (render_fallback_on_failure_)
+ SendFallbackIcon(request);
+ else
+ SendNotFoundResponse(request.callback);
+}
+
+
+void LargeIconSource::SendFallbackIcon(const IconRequest& request) {
+ favicon_base::FallbackIconStyle style;
+ style.background_color = SkColorSetRGB(0xcc, 0xcc, 0xcc);
+ favicon_base::MatchFallbackIconTextColorAgainstBackgroundColor(&style);
+ style.roundness = 1.0;
+ std::vector<unsigned char> bitmap_data =
+ fallback_icon_service_->RenderFallbackIconBitmap(
+ request.url, request.size, style);
+ request.callback.Run(base::RefCountedBytes::TakeVector(&bitmap_data));
+}
+
+void LargeIconSource::SendNotFoundResponse(
+ const content::URLDataSource::GotDataCallback& callback) {
+ callback.Run(nullptr);
+}

Powered by Google App Engine
This is Rietveld 408576698