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

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

Issue 1010783002: [Icons NTP] Working prototype to fetch, store, and display big icons. (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/big_icon_source.cc
diff --git a/chrome/browser/ui/webui/big_icon_source.cc b/chrome/browser/ui/webui/big_icon_source.cc
new file mode 100644
index 0000000000000000000000000000000000000000..dc263a7344ffa9e888975f282800d7e4ae81cfb5
--- /dev/null
+++ b/chrome/browser/ui/webui/big_icon_source.cc
@@ -0,0 +1,153 @@
+// 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/big_icon_source.h"
+
+#include <string>
+#include <vector>
+
+#include "base/memory/ref_counted_memory.h"
+#include "chrome/browser/favicon/favicon_service.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/big_icon_url_parser.h"
+#include "chrome/common/url_constants.h"
+#include "components/favicon_base/fallback_icon_style.h"
+#include "grit/platform_locale_settings.h"
+#include "net/url_request/url_request.h"
+#include "third_party/skia/include/core/SkColor.h"
+#include "ui/base/l10n/l10n_util.h"
+#include "ui/gfx/favicon_size.h"
+
+namespace {
+
+int kFallbackIconSize = 96;
+
+} // namespace
+
+BigIconSource::IconRequest::IconRequest() : size(48) {
+}
+
+BigIconSource::IconRequest::IconRequest(
+ const content::URLDataSource::GotDataCallback& callback_in,
+ const GURL& path_in,
+ int size_in)
+ : callback(callback_in),
+ path(path_in),
+ size(size_in) {
+}
+
+BigIconSource::IconRequest::~IconRequest() {
+}
+
+BigIconSource::BigIconSource(Profile* profile)
+ : profile_(profile),
+ render_fallback_on_failure_(true) {
+ 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");
+#else
+ font_list.push_back(l10n_util::GetStringUTF8(IDS_SANS_SERIF_FONT_FAMILY));
+#endif
+ fallback_icon_service_.reset(
+ new favicon_base::FallbackIconService(font_list));
+}
+
+BigIconSource::~BigIconSource() {
+}
+
+std::string BigIconSource::GetSource() const {
+ return chrome::kChromeUIBigIconHost;
+}
+
+void BigIconSource::StartDataRequest(
+ const std::string& path,
+ int render_process_id,
+ int render_frame_id,
+ const content::URLDataSource::GotDataCallback& callback) {
+ chrome::BigIconUrlParser parser;
+ bool success = parser.Parse(path);
+ if (!success) {
+ SendDefaultResponse(callback);
+ return;
+ }
+
+ FaviconService* favicon_service = FaviconServiceFactory::GetForProfile(
+ profile_, ServiceAccessType::EXPLICIT_ACCESS);
+ if (!favicon_service) {
+ SendDefaultResponse(callback);
+ return;
+ }
+
+ GURL url(parser.url_string());
+ if (!url.is_valid()) {
+ SendDefaultResponse(callback);
+ return;
+ }
+ favicon_service->HackGetBigIcon(
+ url,
+ parser.size_in_pixels(),
+ base::Bind(
+ &BigIconSource::OnBigIconDataAvailable,
+ base::Unretained(this),
+ IconRequest(callback, url, parser.size_in_pixels())),
+ &cancelable_task_tracker_);
+}
+
+std::string BigIconSource::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 BigIconSource::ShouldReplaceExistingSource() const {
+ // Leave the existing DataSource in place, otherwise we'll drop any pending
+ // requests on the floor.
+ return false;
+}
+
+bool BigIconSource::ShouldServiceRequest(
+ const net::URLRequest* request) const {
+ if (request->url().SchemeIs(chrome::kChromeSearchScheme))
+ return InstantIOContext::ShouldServiceRequest(request);
+ return URLDataSource::ShouldServiceRequest(request);
+}
+
+void BigIconSource::OnBigIconDataAvailable(
+ IconRequest request,
+ const favicon_base::FaviconRawBitmapResult& bitmap_result) {
+ if (bitmap_result.is_valid()) {
+ // TODO: Resize the big icon to request.size.
+ // Need to do so here because otherwise NTP can deduce by size whether user
+ // has visited a site!
Roger McFarlane (Chromium) 2015/03/16 19:00:59 assuming: (1) non-privileged access is allowed fr
huangs 2015/03/17 01:43:52 Yup! Raw bitmap sizes are pretty much arbitrary.
+
+ // Forward the data along to the networking system.
+ request.callback.Run(bitmap_result.bitmap_data.get());
+ } else if (render_fallback_on_failure_) {
+ SendFallbackIcon(request.path, request.callback);
+ } else {
+ SendDefaultResponse(request.callback);
+ }
+}
+
+void BigIconSource::SendFallbackIcon(
+ const GURL& url,
+ const content::URLDataSource::GotDataCallback& callback) {
+ 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(
+ url, kFallbackIconSize, style);
+ callback.Run(base::RefCountedBytes::TakeVector(&bitmap_data));
+}
+
+void BigIconSource::SendDefaultResponse(
Roger McFarlane (Chromium) 2015/03/16 19:00:59 is this really a default? or is it an error respon
huangs 2015/03/17 01:43:52 Renamed.
+ const content::URLDataSource::GotDataCallback& callback) {
+ callback.Run(nullptr);
+}

Powered by Google App Engine
This is Rietveld 408576698