Commit 498547a3 authored by michaelbai's avatar michaelbai Committed by Commit bot

This patch added Observer in FaviconTabHelper, so the favicon retrieved by...

This patch added Observer in FaviconTabHelper, so the favicon retrieved by FaviconHandler could be used right away.

BUG=428218

Review URL: https://codereview.chromium.org/684983003

Cr-Commit-Position: refs/heads/master@{#302700}
parent 76cab852
......@@ -288,14 +288,19 @@ void FaviconHandler::SetFavicon(const GURL& url,
if (client_->GetFaviconService() && ShouldSaveFavicon(url))
SetHistoryFavicons(url, icon_url, icon_type, image);
if (UrlMatches(url, url_) && icon_type == favicon_base::FAVICON) {
if (!PageChangedSinceFaviconWasRequested())
SetFaviconOnActivePage(icon_url, image);
}
if (!UrlMatches(url, url_) || PageChangedSinceFaviconWasRequested())
return;
NotifyFaviconAvailable(
icon_url,
image,
icon_type == favicon_base::FAVICON && !download_largest_icon_);
}
void FaviconHandler::SetFaviconOnActivePage(const std::vector<
favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results) {
void FaviconHandler::NotifyFaviconAvailable(
const std::vector<favicon_base::FaviconRawBitmapResult>&
favicon_bitmap_results,
bool is_active_favicon) {
gfx::Image resized_image = favicon_base::SelectFaviconFramesFromPNGs(
favicon_bitmap_results,
favicon_base::GetFaviconScales(),
......@@ -304,25 +309,17 @@ void FaviconHandler::SetFaviconOnActivePage(const std::vector<
// not matter which result we get the |icon_url| from.
const GURL icon_url = favicon_bitmap_results.empty() ?
GURL() : favicon_bitmap_results[0].icon_url;
SetFaviconOnActivePage(icon_url, resized_image);
NotifyFaviconAvailable(icon_url, resized_image, is_active_favicon);
}
void FaviconHandler::SetFaviconOnActivePage(const GURL& icon_url,
const gfx::Image& image) {
// No matter what happens, we need to mark the favicon as being set.
driver_->SetActiveFaviconValidity(true);
bool icon_url_changed = driver_->GetActiveFaviconURL() != icon_url;
driver_->SetActiveFaviconURL(icon_url);
if (image.IsEmpty())
return;
void FaviconHandler::NotifyFaviconAvailable(const GURL& icon_url,
const gfx::Image& image,
bool is_active_favicon) {
gfx::Image image_with_adjusted_colorspace = image;
favicon_base::SetFaviconColorSpace(&image_with_adjusted_colorspace);
driver_->SetActiveFaviconImage(image_with_adjusted_colorspace);
NotifyFaviconUpdated(icon_url_changed);
driver_->OnFaviconAvailable(
image_with_adjusted_colorspace, icon_url, is_active_favicon);
}
void FaviconHandler::OnUpdateFaviconURL(
......@@ -510,10 +507,6 @@ bool FaviconHandler::ShouldSaveFavicon(const GURL& url) {
return client_->IsBookmarked(url);
}
void FaviconHandler::NotifyFaviconUpdated(bool icon_url_changed) {
driver_->NotifyFaviconUpdated(icon_url_changed);
}
int FaviconHandler::GetMaximalIconSize(favicon_base::IconType icon_type) {
switch (icon_type) {
case favicon_base::FAVICON:
......@@ -542,16 +535,18 @@ void FaviconHandler::OnFaviconDataForInitialURLFromFaviconService(
bool has_results = !favicon_bitmap_results.empty();
favicon_expired_or_incomplete_ = has_results && HasExpiredOrIncompleteResult(
preferred_icon_size(), favicon_bitmap_results);
bool has_valid_result = HasValidResult(favicon_bitmap_results);
if (has_results && icon_types_ == favicon_base::FAVICON &&
!driver_->GetActiveFaviconValidity() &&
(!current_candidate() ||
DoUrlsAndIconsMatch(*current_candidate(), favicon_bitmap_results))) {
if (HasValidResult(favicon_bitmap_results)) {
if (has_valid_result) {
// The db knows the favicon (although it may be out of date) and the entry
// doesn't have an icon. Set the favicon now, and if the favicon turns out
// to be expired (or the wrong url) we'll fetch later on. This way the
// user doesn't see a flash of the default favicon.
SetFaviconOnActivePage(favicon_bitmap_results);
NotifyFaviconAvailable(favicon_bitmap_results, !download_largest_icon_);
} else {
// If |favicon_bitmap_results| does not have any valid results, treat the
// favicon as if it's expired.
......@@ -579,6 +574,9 @@ void FaviconHandler::OnFaviconDataForInitialURLFromFaviconService(
}
// else we haven't got the icon url. When we get it we'll ask the
// renderer to download the icon.
if (has_valid_result && icon_types_ != favicon_base::FAVICON)
NotifyFaviconAvailable(favicon_bitmap_results, false);
}
void FaviconHandler::DownloadFaviconOrAskFaviconService(
......@@ -619,13 +617,14 @@ void FaviconHandler::OnFaviconData(const std::vector<
bool has_results = !favicon_bitmap_results.empty();
bool has_expired_or_incomplete_result = HasExpiredOrIncompleteResult(
preferred_icon_size(), favicon_bitmap_results);
bool has_valid_result = HasValidResult(favicon_bitmap_results);
if (has_results && icon_types_ == favicon_base::FAVICON) {
if (HasValidResult(favicon_bitmap_results)) {
if (has_valid_result) {
// There is a favicon, set it now. If expired we'll download the current
// one again, but at least the user will get some icon instead of the
// default and most likely the current one is fine anyway.
SetFaviconOnActivePage(favicon_bitmap_results);
NotifyFaviconAvailable(favicon_bitmap_results, !download_largest_icon_);
}
if (has_expired_or_incomplete_result) {
// The favicon is out of date. Request the current one.
......@@ -643,6 +642,9 @@ void FaviconHandler::OnFaviconData(const std::vector<
current_candidate()->icon_type);
}
history_results_ = favicon_bitmap_results;
if (has_valid_result && icon_types_ != favicon_base::FAVICON)
NotifyFaviconAvailable(favicon_bitmap_results, false);
}
int FaviconHandler::ScheduleDownload(const GURL& url,
......
......@@ -148,11 +148,6 @@ class FaviconHandler {
// Returns true if the favicon should be saved.
virtual bool ShouldSaveFavicon(const GURL& url);
// Notifies the driver that the favicon for the active entry was updated.
// |icon_url_changed| is true if a favicon with a different icon URL has been
// selected since the previous call to NotifyFaviconUpdated().
virtual void NotifyFaviconUpdated(bool icon_url_changed);
private:
friend class TestFaviconHandler; // For testing
......@@ -226,10 +221,15 @@ class FaviconHandler {
const gfx::Image& image,
favicon_base::IconType icon_type);
// Sets the favicon's data.
void SetFaviconOnActivePage(const std::vector<
favicon_base::FaviconRawBitmapResult>& favicon_bitmap_results);
void SetFaviconOnActivePage(const GURL& icon_url, const gfx::Image& image);
// Notifies |driver_| favicon available. See
// FaviconDriver::NotifyFaviconAvailable() for |is_active_favicon| in detail.
void NotifyFaviconAvailable(
const std::vector<favicon_base::FaviconRawBitmapResult>&
favicon_bitmap_results,
bool is_active_favicon);
void NotifyFaviconAvailable(const GURL& icon_url,
const gfx::Image& image,
bool is_active_favicon);
// Return the current candidate if any.
favicon::FaviconURL* current_candidate() {
......
......@@ -10,6 +10,7 @@
#include "chrome/browser/favicon/favicon_handler.h"
#include "chrome/browser/favicon/favicon_service.h"
#include "chrome/browser/favicon/favicon_service_factory.h"
#include "chrome/browser/favicon/favicon_tab_helper_observer.h"
#include "chrome/browser/history/history_service.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/profiles/profile.h"
......@@ -136,6 +137,14 @@ void FaviconTabHelper::SaveFavicon() {
entry->GetURL(), favicon.url, favicon_base::FAVICON, favicon.image);
}
void FaviconTabHelper::AddObserver(FaviconTabHelperObserver* observer) {
observer_list_.AddObserver(observer);
}
void FaviconTabHelper::RemoveObserver(FaviconTabHelperObserver* observer) {
observer_list_.RemoveObserver(observer);
}
int FaviconTabHelper::StartDownload(const GURL& url, int max_image_size) {
FaviconService* favicon_service = FaviconServiceFactory::GetForProfile(
profile_->GetOriginalProfile(), Profile::IMPLICIT_ACCESS);
......@@ -151,14 +160,6 @@ int FaviconTabHelper::StartDownload(const GURL& url, int max_image_size) {
base::Bind(&FaviconTabHelper::DidDownloadFavicon,base::Unretained(this)));
}
void FaviconTabHelper::NotifyFaviconUpdated(bool icon_url_changed) {
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_FAVICON_UPDATED,
content::Source<WebContents>(web_contents()),
content::Details<bool>(&icon_url_changed));
web_contents()->NotifyNavigationStateChanged(content::INVALIDATE_TYPE_TAB);
}
bool FaviconTabHelper::IsOffTheRecord() {
DCHECK(web_contents());
return web_contents()->GetBrowserContext()->IsOffTheRecord();
......@@ -195,6 +196,30 @@ void FaviconTabHelper::SetActiveFaviconValidity(bool validity) {
GetFaviconStatus().valid = validity;
}
void FaviconTabHelper::OnFaviconAvailable(const gfx::Image& image,
const GURL& icon_url,
bool is_active_favicon) {
if (is_active_favicon) {
// No matter what happens, we need to mark the favicon as being set.
SetActiveFaviconValidity(true);
bool icon_url_changed = GetActiveFaviconURL() != icon_url;
SetActiveFaviconURL(icon_url);
if (image.IsEmpty())
return;
SetActiveFaviconImage(image);
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_FAVICON_UPDATED,
content::Source<WebContents>(web_contents()),
content::Details<bool>(&icon_url_changed));
web_contents()->NotifyNavigationStateChanged(content::INVALIDATE_TYPE_TAB);
}
if (!image.IsEmpty())
FOR_EACH_OBSERVER(FaviconTabHelperObserver, observer_list_,
OnFaviconAvailable(image));
}
content::FaviconStatus& FaviconTabHelper::GetFaviconStatus() {
DCHECK(web_contents()->GetController().GetActiveEntry());
return web_contents()->GetController().GetActiveEntry()->GetFavicon();
......
......@@ -9,6 +9,7 @@
#include "base/basictypes.h"
#include "base/callback.h"
#include "base/observer_list.h"
#include "components/favicon/core/browser/favicon_client.h"
#include "components/favicon/core/favicon_driver.h"
#include "content/public/browser/web_contents_observer.h"
......@@ -25,6 +26,7 @@ struct FaviconStatus;
class GURL;
class FaviconHandler;
class FaviconTabHelperObserver;
class Profile;
class SkBitmap;
......@@ -70,17 +72,19 @@ class FaviconTabHelper : public content::WebContentsObserver,
// Saves the favicon for the current page.
void SaveFavicon();
void AddObserver(FaviconTabHelperObserver* observer);
void RemoveObserver(FaviconTabHelperObserver* observer);
// FaviconDriver methods.
int StartDownload(const GURL& url, int max_bitmap_size) override;
void NotifyFaviconUpdated(bool icon_url_changed) override;
bool IsOffTheRecord() override;
const gfx::Image GetActiveFaviconImage() override;
const GURL GetActiveFaviconURL() override;
bool GetActiveFaviconValidity() override;
const GURL GetActiveURL() override;
void SetActiveFaviconImage(gfx::Image image) override;
void SetActiveFaviconURL(GURL url) override;
void SetActiveFaviconValidity(bool validity) override;
void OnFaviconAvailable(const gfx::Image& image,
const GURL& url,
bool is_active_favicon) override;
// Favicon download callback.
void DidDownloadFavicon(
......@@ -102,6 +106,16 @@ class FaviconTabHelper : public content::WebContentsObserver,
const content::LoadCommittedDetails& details,
const content::FrameNavigateParams& params) override;
// Sets whether the page's favicon is valid (if false, the default favicon is
// being used). Requires GetActiveURL() to be valid.
void SetActiveFaviconValidity(bool validity);
// Sets the URL of the favicon's bitmap.
void SetActiveFaviconURL(GURL url);
// Sets the bitmap of the current page's favicon.
void SetActiveFaviconImage(gfx::Image image);
// Helper method that returns the active navigation entry's favicon.
content::FaviconStatus& GetFaviconStatus();
......@@ -117,6 +131,8 @@ class FaviconTabHelper : public content::WebContentsObserver,
// browser_defaults::kEnableTouchIcon is false.
scoped_ptr<FaviconHandler> touch_icon_handler_;
ObserverList<FaviconTabHelperObserver> observer_list_;
DISALLOW_COPY_AND_ASSIGN(FaviconTabHelper);
};
......
// Copyright 2014 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.
#ifndef CHROME_BROWSER_FAVICON_FAVICON_TAB_HELPER_OBSERVER_H_
#define CHROME_BROWSER_FAVICON_FAVICON_TAB_HELPER_OBSERVER_H_
namespace gfx {
class Image;
}
// An observer implemented by classes which are interested in envent in
// FaviconTabHelper.
class FaviconTabHelperObserver {
public:
// Called when favicon |image| is retrieved from either web site or history
// backend.
virtual void OnFaviconAvailable(const gfx::Image& image) = 0;
protected:
virtual ~FaviconTabHelperObserver() {}
};
#endif // CHROME_BROWSER_FAVICON_FAVICON_TAB_HELPER_OBSERVER_H_
......@@ -398,6 +398,7 @@
'browser/favicon/favicon_service_factory.h',
'browser/favicon/favicon_tab_helper.cc',
'browser/favicon/favicon_tab_helper.h',
'browser/favicon/favicon_tab_helper_observer.h',
'browser/file_select_helper.cc',
'browser/file_select_helper.h',
'browser/file_select_helper_mac.mm',
......
......@@ -27,11 +27,6 @@ class FaviconDriver {
// is the only result. A |max_bitmap_size| of 0 means unlimited.
virtual int StartDownload(const GURL& url, int max_bitmap_size) = 0;
// Notifies the driver that the favicon for the active entry was updated.
// |icon_url_changed| is true if a favicon with a different icon URL has
// been selected since the previous call to NotifyFaviconUpdated().
virtual void NotifyFaviconUpdated(bool icon_url_changed) = 0;
// Returns whether the user is operating in an off-the-record context.
virtual bool IsOffTheRecord() = 0;
......@@ -51,16 +46,13 @@ class FaviconDriver {
// URL otherwise.
virtual const GURL GetActiveURL() = 0;
// Sets the bitmap of the current page's favicon. Requires GetActiveURL() to
// be valid.
virtual void SetActiveFaviconImage(gfx::Image image) = 0;
// Sets the URL of the favicon's bitmap. Requires GetActiveURL() to be valid.
virtual void SetActiveFaviconURL(GURL url) = 0;
// Sets whether the page's favicon is valid (if false, the default favicon is
// being used). Requires GetActiveURL() to be valid.
virtual void SetActiveFaviconValidity(bool validity) = 0;
// Notifies the driver a favicon image is available. |image| is not
// necessarily 16x16. |icon_url| is the url the image is from. If
// |is_active_favicon| is true the image corresponds to the favicon
// (possibly empty) of the page.
virtual void OnFaviconAvailable(const gfx::Image& image,
const GURL& icon_url,
bool is_active_favicon) = 0;
};
#endif // COMPONENTS_FAVICON_CORE_FAVICON_DRIVER_H_
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