Commit 420909e8 authored by Scott Violet's avatar Scott Violet Committed by Commit Bot

favicons: makes FaviconHandler/Driver deal with a null service

In WebLayer incognito profiles are not backed by a real profile.
This means there is no FaviconService to use in this case. This
makes FaviconHandler/Driver work with a null service so that
WebLayer can still offer favicons in incognito mode.

BUG=1076463
TEST=none

Change-Id: Idc049f37c469576720847ed817453b17ec519aea
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2339550Reviewed-by: default avatarPeter Kotwicz <pkotwicz@chromium.org>
Commit-Queue: Scott Violet <sky@chromium.org>
Cr-Commit-Position: refs/heads/master@{#796443}
parent b8502343
...@@ -27,9 +27,6 @@ const bool kEnableTouchIcon = false; ...@@ -27,9 +27,6 @@ const bool kEnableTouchIcon = false;
FaviconDriverImpl::FaviconDriverImpl(CoreFaviconService* favicon_service) FaviconDriverImpl::FaviconDriverImpl(CoreFaviconService* favicon_service)
: favicon_service_(favicon_service) { : favicon_service_(favicon_service) {
if (!favicon_service_)
return;
if (kEnableTouchIcon) { if (kEnableTouchIcon) {
handlers_.push_back(std::make_unique<FaviconHandler>( handlers_.push_back(std::make_unique<FaviconHandler>(
favicon_service_, this, FaviconDriverObserver::NON_TOUCH_LARGEST)); favicon_service_, this, FaviconDriverObserver::NON_TOUCH_LARGEST));
......
...@@ -37,6 +37,7 @@ class FaviconDriverImpl : public FaviconDriver, ...@@ -37,6 +37,7 @@ class FaviconDriverImpl : public FaviconDriver,
bool HasPendingTasksForTest(); bool HasPendingTasksForTest();
protected: protected:
// |favicon_service| may be null, which means favicons are not saved.
explicit FaviconDriverImpl(CoreFaviconService* favicon_service); explicit FaviconDriverImpl(CoreFaviconService* favicon_service);
~FaviconDriverImpl() override; ~FaviconDriverImpl() override;
...@@ -54,8 +55,8 @@ class FaviconDriverImpl : public FaviconDriver, ...@@ -54,8 +55,8 @@ class FaviconDriverImpl : public FaviconDriver,
CoreFaviconService* favicon_service() { return favicon_service_; } CoreFaviconService* favicon_service() { return favicon_service_; }
private: private:
// KeyedService used by FaviconDriverImpl. It may be null during testing, // KeyedService used by FaviconDriverImpl. It may be null, if non-null, it
// but if it is defined, it must outlive the FaviconDriverImpl. // must outlive FaviconDriverImpl.
CoreFaviconService* favicon_service_; CoreFaviconService* favicon_service_;
// FaviconHandlers used to download the different kind of favicons. // FaviconHandlers used to download the different kind of favicons.
......
...@@ -230,12 +230,16 @@ void FaviconHandler::FetchFavicon(const GURL& page_url, bool is_same_document) { ...@@ -230,12 +230,16 @@ void FaviconHandler::FetchFavicon(const GURL& page_url, bool is_same_document) {
// possible values in |page_urls_|) because we want to use the most // possible values in |page_urls_|) because we want to use the most
// up-to-date / latest URL for DB lookups, which is the page URL for which // up-to-date / latest URL for DB lookups, which is the page URL for which
// we get <link rel="icon"> candidates (FaviconHandler::OnUpdateCandidates()). // we get <link rel="icon"> candidates (FaviconHandler::OnUpdateCandidates()).
service_->GetFaviconForPageURL( if (service_) {
last_page_url_, icon_types_, preferred_icon_size(), service_->GetFaviconForPageURL(
base::BindOnce( last_page_url_, icon_types_, preferred_icon_size(),
&FaviconHandler::OnFaviconDataForInitialURLFromFaviconService, base::BindOnce(
base::Unretained(this)), &FaviconHandler::OnFaviconDataForInitialURLFromFaviconService,
&cancelable_task_tracker_for_page_url_); base::Unretained(this)),
&cancelable_task_tracker_for_page_url_);
} else {
OnFaviconDataForInitialURLFromFaviconService({});
}
} }
bool FaviconHandler::ShouldDownloadNextCandidate() const { bool FaviconHandler::ShouldDownloadNextCandidate() const {
...@@ -263,7 +267,7 @@ void FaviconHandler::SetFavicon(const GURL& icon_url, ...@@ -263,7 +267,7 @@ void FaviconHandler::SetFavicon(const GURL& icon_url,
// Associate the icon to all URLs in |page_urls_|, which contains page URLs // Associate the icon to all URLs in |page_urls_|, which contains page URLs
// within the same site/document that have been considered to reliably share // within the same site/document that have been considered to reliably share
// the same icon candidates. // the same icon candidates.
if (!delegate_->IsOffTheRecord()) if (service_ && !delegate_->IsOffTheRecord())
service_->SetFavicons(page_urls_, icon_url, icon_type, image); service_->SetFavicons(page_urls_, icon_url, icon_type, image);
NotifyFaviconUpdated(icon_url, icon_type, image); NotifyFaviconUpdated(icon_url, icon_type, image);
...@@ -277,7 +281,7 @@ void FaviconHandler::MaybeDeleteFaviconMappings() { ...@@ -277,7 +281,7 @@ void FaviconHandler::MaybeDeleteFaviconMappings() {
// state to be checked at the very end. // state to be checked at the very end.
if (!error_other_than_404_found_ && if (!error_other_than_404_found_ &&
notification_icon_type_ != favicon_base::IconType::kInvalid) { notification_icon_type_ != favicon_base::IconType::kInvalid) {
if (!delegate_->IsOffTheRecord()) if (service_ && !delegate_->IsOffTheRecord())
service_->DeleteFaviconMappings(page_urls_, notification_icon_type_); service_->DeleteFaviconMappings(page_urls_, notification_icon_type_);
delegate_->OnFaviconDeleted(last_page_url_, handler_type_); delegate_->OnFaviconDeleted(last_page_url_, handler_type_);
...@@ -349,9 +353,8 @@ void FaviconHandler::OnUpdateCandidates( ...@@ -349,9 +353,8 @@ void FaviconHandler::OnUpdateCandidates(
best_favicon_ = DownloadedFavicon(); best_favicon_ = DownloadedFavicon();
manifest_url_ = manifest_url; manifest_url_ = manifest_url;
// Check if the manifest was previously blacklisted (e.g. returned a 404) and // If the manifest couldn't be downloaded (or there is no service), ignore it.
// ignore the manifest URL if that's the case. if (!manifest_url_.is_empty() && service_ &&
if (!manifest_url_.is_empty() &&
service_->WasUnableToDownloadFavicon(manifest_url_)) { service_->WasUnableToDownloadFavicon(manifest_url_)) {
DVLOG(1) << "Skip failed Manifest: " << manifest_url; DVLOG(1) << "Skip failed Manifest: " << manifest_url;
manifest_url_ = GURL(); manifest_url_ = GURL();
...@@ -363,6 +366,11 @@ void FaviconHandler::OnUpdateCandidates( ...@@ -363,6 +366,11 @@ void FaviconHandler::OnUpdateCandidates(
return; return;
} }
if (!service_) {
OnFaviconDataForManifestFromFaviconService({});
return;
}
// See if there is a cached favicon for the manifest. This will update the DB // See if there is a cached favicon for the manifest. This will update the DB
// mappings only if the manifest URL is cached. // mappings only if the manifest URL is cached.
GetFaviconAndUpdateMappingsUnlessIncognito( GetFaviconAndUpdateMappingsUnlessIncognito(
...@@ -419,7 +427,9 @@ void FaviconHandler::OnDidDownloadManifest( ...@@ -419,7 +427,9 @@ void FaviconHandler::OnDidDownloadManifest(
<< ", falling back to inlined ones, which are " << ", falling back to inlined ones, which are "
<< non_manifest_original_candidates_.size(); << non_manifest_original_candidates_.size();
service_->UnableToDownloadFavicon(manifest_url_); if (service_)
service_->UnableToDownloadFavicon(manifest_url_);
manifest_url_ = GURL(); manifest_url_ = GURL();
OnGotFinalIconURLCandidates(non_manifest_original_candidates_); OnGotFinalIconURLCandidates(non_manifest_original_candidates_);
...@@ -503,7 +513,8 @@ void FaviconHandler::OnDidDownloadFavicon( ...@@ -503,7 +513,8 @@ void FaviconHandler::OnDidDownloadFavicon(
if (bitmaps.empty()) { if (bitmaps.empty()) {
if (http_status_code == 404) { if (http_status_code == 404) {
DVLOG(1) << "Failed to Download Favicon:" << image_url; DVLOG(1) << "Failed to Download Favicon:" << image_url;
service_->UnableToDownloadFavicon(image_url); if (service_)
service_->UnableToDownloadFavicon(image_url);
} else if (http_status_code != 0) { } else if (http_status_code != 0) {
error_other_than_404_found_ = true; error_other_than_404_found_ = true;
} }
...@@ -596,7 +607,7 @@ void FaviconHandler::OnFaviconDataForInitialURLFromFaviconService( ...@@ -596,7 +607,7 @@ void FaviconHandler::OnFaviconDataForInitialURLFromFaviconService(
// - The favicon in the database is expired. // - The favicon in the database is expired.
// AND // AND
// - Redownloading the favicon fails with a non-404 error code. // - Redownloading the favicon fails with a non-404 error code.
if (!delegate_->IsOffTheRecord()) { if (service_ && !delegate_->IsOffTheRecord()) {
service_->CloneFaviconMappingsForPages(last_page_url_, icon_types_, service_->CloneFaviconMappingsForPages(last_page_url_, icon_types_,
page_urls_); page_urls_);
} }
...@@ -622,7 +633,7 @@ void FaviconHandler::DownloadCurrentCandidateOrAskFaviconService() { ...@@ -622,7 +633,7 @@ void FaviconHandler::DownloadCurrentCandidateOrAskFaviconService() {
// If the icons listed in a manifest are being processed, skip the cache // If the icons listed in a manifest are being processed, skip the cache
// lookup for |icon_url| since the manifest's URL is used for caching, not the // lookup for |icon_url| since the manifest's URL is used for caching, not the
// icon URL, and this lookup has happened earlier. // icon URL, and this lookup has happened earlier.
if (redownload_icons_ || !manifest_url_.is_empty()) { if (!service_ || redownload_icons_ || !manifest_url_.is_empty()) {
// We have the mapping, but the favicon is out of date. Download it now. // We have the mapping, but the favicon is out of date. Download it now.
ScheduleImageDownload(icon_url, icon_type); ScheduleImageDownload(icon_url, icon_type);
} else { } else {
...@@ -636,6 +647,10 @@ void FaviconHandler::GetFaviconAndUpdateMappingsUnlessIncognito( ...@@ -636,6 +647,10 @@ void FaviconHandler::GetFaviconAndUpdateMappingsUnlessIncognito(
const GURL& icon_url, const GURL& icon_url,
favicon_base::IconType icon_type, favicon_base::IconType icon_type,
favicon_base::FaviconResultsCallback callback) { favicon_base::FaviconResultsCallback callback) {
// This should only be called if |service_| is available. If this is called
// with no |service_|, then |callback| is never run.
DCHECK(service_);
// We don't know the favicon, but we may have previously downloaded the // We don't know the favicon, but we may have previously downloaded the
// favicon for another page that shares the same favicon. Ask for the // favicon for another page that shares the same favicon. Ask for the
// favicon given the favicon URL. // favicon given the favicon URL.
...@@ -682,7 +697,7 @@ void FaviconHandler::ScheduleImageDownload(const GURL& image_url, ...@@ -682,7 +697,7 @@ void FaviconHandler::ScheduleImageDownload(const GURL& image_url,
// Note that CancelableCallback starts cancelled. // Note that CancelableCallback starts cancelled.
DCHECK(image_download_request_.IsCancelled()) DCHECK(image_download_request_.IsCancelled())
<< "More than one ongoing download"; << "More than one ongoing download";
if (service_->WasUnableToDownloadFavicon(image_url)) { if (service_ && service_->WasUnableToDownloadFavicon(image_url)) {
DVLOG(1) << "Skip Failed FavIcon: " << image_url; DVLOG(1) << "Skip Failed FavIcon: " << image_url;
OnDidDownloadFavicon(icon_type, 0, 0, image_url, std::vector<SkBitmap>(), OnDidDownloadFavicon(icon_type, 0, 0, image_url, std::vector<SkBitmap>(),
std::vector<gfx::Size>()); std::vector<gfx::Size>());
......
...@@ -126,7 +126,9 @@ class FaviconHandler { ...@@ -126,7 +126,9 @@ class FaviconHandler {
FaviconDriverObserver::NotificationIconType notification_icon_type) = 0; FaviconDriverObserver::NotificationIconType notification_icon_type) = 0;
}; };
// |service| and |delegate| must not be nullptr and must outlive this class. // |service| may be null (which means favicons are not saved). If |service|
// is non-null it must outlive this class. |delegate| must not be nullptr and
// must outlive this class.
FaviconHandler(CoreFaviconService* service, FaviconHandler(CoreFaviconService* service,
Delegate* delegate, Delegate* delegate,
FaviconDriverObserver::NotificationIconType handler_type); FaviconDriverObserver::NotificationIconType handler_type);
...@@ -358,8 +360,7 @@ class FaviconHandler { ...@@ -358,8 +360,7 @@ class FaviconHandler {
GURL notification_icon_url_; GURL notification_icon_url_;
favicon_base::IconType notification_icon_type_; favicon_base::IconType notification_icon_type_;
// The CoreFaviconService which implements favicon operations. May be null // The CoreFaviconService which implements favicon operations. May be null.
// during testing.
CoreFaviconService* service_; CoreFaviconService* service_;
// This handler's delegate. // This handler's delegate.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment