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

Unified Diff: components/favicon/core/favicon_service.cc

Issue 2347173002: Extend FaviconService to support fetching favicons from a Google server (Closed)
Patch Set: Minor polish Created 4 years, 3 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: components/favicon/core/favicon_service.cc
diff --git a/components/favicon/core/favicon_service.cc b/components/favicon/core/favicon_service.cc
index 57a29deaf5fe69c7250a85ef799d064bab7ffe7a..82ae94d7cc49f54b567f2105d479d1f0b2f72fa9 100644
--- a/components/favicon/core/favicon_service.cc
+++ b/components/favicon/core/favicon_service.cc
@@ -10,12 +10,14 @@
#include "base/hash.h"
#include "base/single_thread_task_runner.h"
+#include "base/strings/stringprintf.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/trace_event/trace_event.h"
#include "components/favicon/core/favicon_client.h"
#include "components/favicon_base/favicon_util.h"
#include "components/favicon_base/select_favicon_frames.h"
#include "components/history/core/browser/history_service.h"
+#include "components/image_fetcher/image_fetcher.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/favicon_size.h"
@@ -25,6 +27,29 @@
namespace favicon {
namespace {
+const char kGoogleFaviconServiceRequestFormat[] =
+ "https://s2.googleusercontent.com/s2/"
+ "favicons?domain=%s&src=chrome_newtab_mobile&sz=%d&alt=404";
+
+const int kGoogleFaviconServiceSupportedSizes[] = {16, 24, 32, 48, 64};
+
+int GetGoogleFaviconServiceSupportedSize(int arbitrary_size) {
+ // Take the smallest size larger than arbitrary_size.
+ for (int size : kGoogleFaviconServiceSupportedSizes) {
+ if (size > arbitrary_size)
Marc Treib 2016/09/19 08:26:39 I think this should be >=
noyau (Ping after 24h) 2016/09/19 09:00:50 Definitely should be >=. I'm surprised that there
jkrcal 2016/09/20 15:52:06 Done.
+ return size;
+ }
+ // Or at least the largest available size.
+ return kGoogleFaviconServiceSupportedSizes
+ [arraysize(kGoogleFaviconServiceSupportedSizes) - 1];
+}
+
+GURL GetGoogleFaviconServiceURL(GURL domain_url, int supported_size) {
+ return GURL(base::StringPrintf(kGoogleFaviconServiceRequestFormat,
+ domain_url.spec().c_str(),
+ supported_size));
+}
+
// Helper to run callback with empty results if we cannot get the history
// service.
base::CancelableTaskTracker::TaskId RunWithEmptyResultAsync(
@@ -51,10 +76,13 @@ std::vector<int> GetPixelSizesForFaviconScales(int size_in_dip) {
} // namespace
-FaviconService::FaviconService(std::unique_ptr<FaviconClient> favicon_client,
- history::HistoryService* history_service)
+FaviconService::FaviconService(
+ std::unique_ptr<FaviconClient> favicon_client,
+ history::HistoryService* history_service,
+ std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher)
: favicon_client_(std::move(favicon_client)),
- history_service_(history_service) {}
+ history_service_(history_service),
+ image_fetcher_(std::move(image_fetcher)) {}
FaviconService::~FaviconService() {
}
@@ -198,6 +226,32 @@ base::CancelableTaskTracker::TaskId FaviconService::GetFaviconForPageURL(
tracker);
}
+void FaviconService::DownloadFromGoogleServerFaviconImageForPageURL(
+ const GURL& domain_url,
+ int desired_size_in_pixel,
+ std::string client_id,
pkotwicz 2016/09/19 20:07:51 |client_id| seems unused. Do you need to add a TOD
jkrcal 2016/09/20 15:52:06 Oh, my mistake, thanks! Used now.
+ data_use_measurement::DataUseUserData::ServiceName data_use_service_name,
+ const favicon_base::FaviconImageCallback& callback) {
+ if (!image_fetcher_.get()) {
+ base::ThreadTaskRunnerHandle::Get()->PostTask(
+ FROM_HERE, base::Bind(callback, favicon_base::FaviconImageResult()));
+ return;
+ }
+
+ int supported_size =
+ GetGoogleFaviconServiceSupportedSize(desired_size_in_pixel);
+ GURL domain_url_sanitized = domain_url.GetWithEmptyPath();
+ GURL image_url =
+ GetGoogleFaviconServiceURL(domain_url_sanitized, supported_size);
+
+ image_fetcher_->SetDataUseServiceName(data_use_service_name);
+ image_fetcher_->StartOrQueueNetworkRequest(
+ image_url.spec(), image_url,
+ base::Bind(&FaviconService::
+ StoreFaviconFromGoogleServiceAndRunFaviconImageCallback,
+ base::Unretained(this), callback, domain_url_sanitized));
+}
+
base::CancelableTaskTracker::TaskId
FaviconService::UpdateFaviconMappingsAndFetch(
const GURL& page_url,
@@ -348,4 +402,29 @@ void FaviconService::RunFaviconRawBitmapCallbackWithBitmapResults(
ResizeFaviconBitmapResult(favicon_bitmap_results, desired_size_in_pixel));
}
+void FaviconService::StoreFaviconFromGoogleServiceAndRunFaviconImageCallback(
+ const favicon_base::FaviconImageCallback& callback,
+ const GURL& domain_url,
+ const std::string& icon_url,
+ const gfx::Image& image) {
+ favicon_base::FaviconImageResult image_result;
+ image_result.image = image;
+ if (image_result.image.IsEmpty()) {
+ image_result.icon_url = GURL();
pkotwicz 2016/09/19 20:07:51 The "icon URL" is used as a key in the database. U
jkrcal 2016/09/20 15:52:06 I do not understand what you mean. Maybe I puzzled
pkotwicz 2016/09/21 20:55:53 Sorry my bad. I did not realize that |icon_url| is
jkrcal 2016/12/09 13:57:44 No problem :)
+ callback.Run(image_result);
+ }
+
+ image_result.icon_url = GURL(icon_url);
+ favicon_base::SetFaviconColorSpace(&image_result.image);
+
+ // Store the favicon in the cache.
+ SetFavicons(domain_url, image_result.icon_url,
+ favicon_base::IconType::FAVICON, image_result.image);
noyau (Ping after 24h) 2016/09/19 09:00:50 The documentation for SetFavicons is very explicit
jkrcal 2016/09/20 15:52:06 I am not sure how to address your comment. - Do
noyau (Ping after 24h) 2016/09/21 09:49:20 I'm not sure how to solve the issue, pkotwicz@ is
pkotwicz 2016/09/21 20:55:53 noyau: Not having image reps for all of ui::GetSup
jkrcal 2016/12/09 13:57:44 Acknowledged.
+ // Mark them as out-of-date so that they are refetched when we visit the
+ // original page any time in the future.
+ SetFaviconOutOfDateForPage(domain_url);
pkotwicz 2016/09/19 20:07:51 - favicon_base::FAVICON is only meant to store 16x
jkrcal 2016/09/20 15:52:06 My observations: - If I correctly understand what
pkotwicz 2016/09/21 20:55:53 We only sync favicons for bookmarks. It is possibl
jkrcal 2016/12/09 13:57:44 As per offline discussion with pkotwicz@, only one
+
+ callback.Run(image_result);
+}
+
} // namespace favicon

Powered by Google App Engine
This is Rietveld 408576698