Commit f16444b3 authored by huangs's avatar huangs Committed by Commit bot

[Fallback Icons] Refactor FallbackIconService to be a BrowserContext-level singleton

- FallbackIconServiceFactory now get sFallbackIconService singleton from
  BrowserContext.
- FallbackIconClient & ChromeFallbackIconClient: wrap external code so the Favicon
  component won't have to include them. Specifically:
  - Choosing fonts: also removed duplicate code.
  - Extracting letter from URL to render Fallback.

BUG=455063

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

Cr-Commit-Position: refs/heads/master@{#322653}
parent 328a417b
...@@ -12,6 +12,7 @@ include_rules = [ ...@@ -12,6 +12,7 @@ include_rules = [
"+chrome/browser/profiles/incognito_helpers.h", "+chrome/browser/profiles/incognito_helpers.h",
"+chrome/browser/search/search.h", "+chrome/browser/search/search.h",
"+components/favicon/core", "+components/favicon/core",
"+net/base/registry_controlled_domains/registry_controlled_domain.h",
"+third_party/skia/include/core/SkBitmap.h", "+third_party/skia/include/core/SkBitmap.h",
# TODO(caitkp): Bring this list to zero. # TODO(caitkp): Bring this list to zero.
......
// 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/favicon/chrome_fallback_icon_client.h"
#include "base/i18n/case_conversion.h"
#include "base/strings/utf_string_conversions.h"
#include "grit/platform_locale_settings.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "ui/base/l10n/l10n_util.h"
#include "url/gurl.h"
ChromeFallbackIconClient::ChromeFallbackIconClient() {
#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
}
ChromeFallbackIconClient::~ChromeFallbackIconClient() {
}
const std::vector<std::string>& ChromeFallbackIconClient::GetFontNameList()
const {
return font_list_;
}
// Returns a single character to represent |url|. To do this we take the first
// letter in a domain's name and make it upper case.
base::string16 ChromeFallbackIconClient::GetFallbackIconText(const GURL& url)
const {
// TODO(huangs): Handle non-ASCII ("xn--") domain names.
std::string domain = net::registry_controlled_domains::GetDomainAndRegistry(
url, net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
return domain.empty() ? base::string16() :
base::i18n::ToUpper(base::ASCIIToUTF16(domain.substr(0, 1)));
}
// 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.
#ifndef CHROME_BROWSER_FAVICON_CHROME_FALLBACK_ICON_CLIENT_H_
#define CHROME_BROWSER_FAVICON_CHROME_FALLBACK_ICON_CLIENT_H_
#include <string>
#include <vector>
#include "base/macros.h"
#include "base/strings/string16.h"
#include "components/favicon/core/fallback_icon_client.h"
class GURL;
// ChromeFallbackIconClient implements the FallbackIconClient interface.
class ChromeFallbackIconClient : public FallbackIconClient {
public:
ChromeFallbackIconClient();
~ChromeFallbackIconClient() override;
// FallbackIconClient implementation:
const std::vector<std::string>& GetFontNameList() const override;
base::string16 GetFallbackIconText(const GURL& url) const override;
private:
std::vector<std::string> font_list_;
DISALLOW_COPY_AND_ASSIGN(ChromeFallbackIconClient);
};
#endif // CHROME_BROWSER_FAVICON_CHROME_FALLBACK_ICON_CLIENT_H_
// 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/favicon/chrome_fallback_icon_client_factory.h"
#include "base/memory/singleton.h"
#include "chrome/browser/favicon/chrome_fallback_icon_client.h"
#include "chrome/browser/profiles/incognito_helpers.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "content/public/browser/browser_context.h"
ChromeFallbackIconClientFactory::ChromeFallbackIconClientFactory()
: BrowserContextKeyedServiceFactory(
"ChromeFallbackIconClient",
BrowserContextDependencyManager::GetInstance()) {
}
ChromeFallbackIconClientFactory::~ChromeFallbackIconClientFactory() {
}
// static
FallbackIconClient* ChromeFallbackIconClientFactory::GetForBrowserContext(
content::BrowserContext* context) {
return static_cast<FallbackIconClient*>(
GetInstance()->GetServiceForBrowserContext(context, true));
}
// static
ChromeFallbackIconClientFactory*
ChromeFallbackIconClientFactory::GetInstance() {
return Singleton<ChromeFallbackIconClientFactory>::get();
}
KeyedService* ChromeFallbackIconClientFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const {
return new ChromeFallbackIconClient();
}
// 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.
#ifndef CHROME_BROWSER_FAVICON_CHROME_FALLBACK_ICON_CLIENT_FACTORY_H_
#define CHROME_BROWSER_FAVICON_CHROME_FALLBACK_ICON_CLIENT_FACTORY_H_
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
template <typename T> struct DefaultSingletonTraits;
class FallbackIconClient;
namespace content {
class BrowserContext;
}
// Singleton that owns all ChromeFallbackIconClients and associates them with
// Profiles.
class ChromeFallbackIconClientFactory
: public BrowserContextKeyedServiceFactory {
public:
// Returns the instance of FallbackIconClient associated with this profile
// (creating one if none exists).
static FallbackIconClient* GetForBrowserContext(
content::BrowserContext* context);
// Returns an instance of the factory singleton.
static ChromeFallbackIconClientFactory* GetInstance();
private:
friend struct DefaultSingletonTraits<ChromeFallbackIconClientFactory>;
ChromeFallbackIconClientFactory();
~ChromeFallbackIconClientFactory() override;
// BrowserContextKeyedServiceFactory:
KeyedService* BuildServiceInstanceFor(
content::BrowserContext* context) const override;
};
#endif // CHROME_BROWSER_FAVICON_CHROME_FALLBACK_ICON_CLIENT_FACTORY_H_
// 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/favicon/fallback_icon_service_factory.h"
#include "base/memory/singleton.h"
#include "chrome/browser/favicon/chrome_fallback_icon_client_factory.h"
#include "components/favicon/core/fallback_icon_service.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "content/public/browser/browser_context.h"
// static
FallbackIconService* FallbackIconServiceFactory::GetForBrowserContext(
content::BrowserContext* context) {
return static_cast<FallbackIconService*>(
GetInstance()->GetServiceForBrowserContext(context, true));
}
// static
FallbackIconServiceFactory* FallbackIconServiceFactory::GetInstance() {
return Singleton<FallbackIconServiceFactory>::get();
}
FallbackIconServiceFactory::FallbackIconServiceFactory()
: BrowserContextKeyedServiceFactory(
"FallbackIconService",
BrowserContextDependencyManager::GetInstance()) {
DependsOn(ChromeFallbackIconClientFactory::GetInstance());
}
FallbackIconServiceFactory::~FallbackIconServiceFactory() {}
KeyedService* FallbackIconServiceFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const {
FallbackIconClient* fallback_icon_client =
ChromeFallbackIconClientFactory::GetForBrowserContext(context);
return new FallbackIconService(fallback_icon_client);
}
bool FallbackIconServiceFactory::ServiceIsNULLWhileTesting() const {
return true;
}
// 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.
#ifndef CHROME_BROWSER_FAVICON_FALLBACK_ICON_SERVICE_FACTORY_H_
#define CHROME_BROWSER_FAVICON_FALLBACK_ICON_SERVICE_FACTORY_H_
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
template <typename T> struct DefaultSingletonTraits;
class FallbackIconService;
namespace content {
class BrowserContext;
}
// Singleton that owns all FallbackIconService and associates them with
// BrowserContext instances.
class FallbackIconServiceFactory : public BrowserContextKeyedServiceFactory {
public:
static FallbackIconService* GetForBrowserContext(
content::BrowserContext* context);
static FallbackIconServiceFactory* GetInstance();
private:
friend struct DefaultSingletonTraits<FallbackIconServiceFactory>;
FallbackIconServiceFactory();
~FallbackIconServiceFactory() override;
// BrowserContextKeyedServiceFactory:
KeyedService* BuildServiceInstanceFor(
content::BrowserContext* context) const override;
bool ServiceIsNULLWhileTesting() const override;
DISALLOW_COPY_AND_ASSIGN(FallbackIconServiceFactory);
};
#endif // CHROME_BROWSER_FAVICON_FALLBACK_ICON_SERVICE_FACTORY_H_
...@@ -18,7 +18,7 @@ class Profile; ...@@ -18,7 +18,7 @@ class Profile;
// Profiles. // Profiles.
class FaviconServiceFactory : public BrowserContextKeyedServiceFactory { class FaviconServiceFactory : public BrowserContextKeyedServiceFactory {
public: public:
// |access| defines what the caller plans to do with the service. See // |sat| defines what the caller plans to do with the service. See
// the ServiceAccessType definition in profile.h. // the ServiceAccessType definition in profile.h.
static FaviconService* GetForProfile(Profile* profile, ServiceAccessType sat); static FaviconService* GetForProfile(Profile* profile, ServiceAccessType sat);
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#include "chrome/browser/search/instant_service.h" #include "chrome/browser/search/instant_service.h"
#include "chrome/browser/chrome_notification_types.h" #include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/favicon/fallback_icon_service_factory.h"
#include "chrome/browser/favicon/favicon_service_factory.h"
#include "chrome/browser/history/top_sites_factory.h" #include "chrome/browser/history/top_sites_factory.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search/instant_io_context.h" #include "chrome/browser/search/instant_io_context.h"
...@@ -25,7 +27,10 @@ ...@@ -25,7 +27,10 @@
#include "chrome/browser/ui/webui/ntp/thumbnail_source.h" #include "chrome/browser/ui/webui/ntp/thumbnail_source.h"
#include "chrome/browser/ui/webui/theme_source.h" #include "chrome/browser/ui/webui/theme_source.h"
#include "chrome/common/render_messages.h" #include "chrome/common/render_messages.h"
#include "components/favicon/core/fallback_icon_service.h"
#include "components/favicon/core/favicon_service.h"
#include "components/history/core/browser/top_sites.h" #include "components/history/core/browser/top_sites.h"
#include "components/keyed_service/core/service_access_type.h"
#include "components/search_engines/template_url_service.h" #include "components/search_engines/template_url_service.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_service.h" #include "content/public/browser/notification_service.h"
...@@ -126,8 +131,15 @@ InstantService::InstantService(Profile* profile) ...@@ -126,8 +131,15 @@ InstantService::InstantService(Profile* profile)
content::URLDataSource::Add(profile_, new ThumbnailListSource(profile_)); content::URLDataSource::Add(profile_, new ThumbnailListSource(profile_));
#endif // !defined(OS_ANDROID) #endif // !defined(OS_ANDROID)
content::URLDataSource::Add(profile_, new LargeIconSource(profile)); FaviconService* favicon_service = FaviconServiceFactory::GetForProfile(
content::URLDataSource::Add(profile_, new FallbackIconSource()); profile_, ServiceAccessType::EXPLICIT_ACCESS);
FallbackIconService* fallback_icon_service =
FallbackIconServiceFactory::GetForBrowserContext(profile_);
content::URLDataSource::Add(profile_,
new LargeIconSource(favicon_service, fallback_icon_service));
content::URLDataSource::Add(
profile_, new FallbackIconSource(fallback_icon_service));
content::URLDataSource::Add( content::URLDataSource::Add(
profile_, new FaviconSource(profile_, FaviconSource::FAVICON)); profile_, new FaviconSource(profile_, FaviconSource::FAVICON));
content::URLDataSource::Add(profile_, new MostVisitedIframeSource()); content::URLDataSource::Add(profile_, new MostVisitedIframeSource());
......
...@@ -4,29 +4,21 @@ ...@@ -4,29 +4,21 @@
#include "chrome/browser/ui/webui/fallback_icon_source.h" #include "chrome/browser/ui/webui/fallback_icon_source.h"
#include <string>
#include <vector> #include <vector>
#include "base/memory/ref_counted_memory.h" #include "base/memory/ref_counted_memory.h"
#include "chrome/browser/search/instant_io_context.h" #include "chrome/browser/search/instant_io_context.h"
#include "chrome/common/favicon/fallback_icon_url_parser.h" #include "chrome/common/favicon/fallback_icon_url_parser.h"
#include "chrome/common/url_constants.h" #include "chrome/common/url_constants.h"
#include "grit/platform_locale_settings.h" #include "components/favicon/core/fallback_icon_service.h"
#include "components/keyed_service/core/service_access_type.h"
#include "net/url_request/url_request.h" #include "net/url_request/url_request.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/favicon_size.h" #include "ui/gfx/favicon_size.h"
#include "url/gurl.h" #include "url/gurl.h"
FallbackIconSource::FallbackIconSource() { FallbackIconSource::FallbackIconSource(
std::vector<std::string> font_list; FallbackIconService* fallback_icon_service)
#if defined(OS_CHROMEOS) : fallback_icon_service_(fallback_icon_service) {
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 FallbackIconService(font_list));
} }
FallbackIconSource::~FallbackIconSource() { FallbackIconSource::~FallbackIconSource() {
...@@ -47,15 +39,15 @@ void FallbackIconSource::StartDataRequest( ...@@ -47,15 +39,15 @@ void FallbackIconSource::StartDataRequest(
SendDefaultResponse(callback); SendDefaultResponse(callback);
return; return;
} }
GURL url(parsed.url_string()); GURL url(parsed.url_string());
if (url.is_empty() || !url.is_valid()) { if (url.is_empty() || !url.is_valid()) {
SendDefaultResponse(callback); SendDefaultResponse(callback);
return; return;
} }
std::vector<unsigned char> bitmap_data =
fallback_icon_service_->RenderFallbackIconBitmap( SendFallbackIconHelper(
url, parsed.size_in_pixels(), parsed.style()); url, parsed.size_in_pixels(), parsed.style(), callback);
callback.Run(base::RefCountedBytes::TakeVector(&bitmap_data));
} }
std::string FallbackIconSource::GetMimeType(const std::string&) const { std::string FallbackIconSource::GetMimeType(const std::string&) const {
...@@ -77,10 +69,23 @@ bool FallbackIconSource::ShouldServiceRequest( ...@@ -77,10 +69,23 @@ bool FallbackIconSource::ShouldServiceRequest(
return URLDataSource::ShouldServiceRequest(request); return URLDataSource::ShouldServiceRequest(request);
} }
void FallbackIconSource::SendDefaultResponse( void FallbackIconSource::SendFallbackIconHelper(
const GURL& url,
int size_in_pixels,
const favicon_base::FallbackIconStyle& style,
const content::URLDataSource::GotDataCallback& callback) { const content::URLDataSource::GotDataCallback& callback) {
if (!fallback_icon_service_) { // Can be null for tests.
callback.Run(nullptr); // Trigger "Not Found" response.
return;
}
std::vector<unsigned char> bitmap_data = std::vector<unsigned char> bitmap_data =
fallback_icon_service_->RenderFallbackIconBitmap( fallback_icon_service_->RenderFallbackIconBitmap(
GURL(), gfx::kFaviconSize, favicon_base::FallbackIconStyle()); url, size_in_pixels, style);
callback.Run(base::RefCountedBytes::TakeVector(&bitmap_data)); callback.Run(base::RefCountedBytes::TakeVector(&bitmap_data));
} }
void FallbackIconSource::SendDefaultResponse(
const content::URLDataSource::GotDataCallback& callback) {
favicon_base::FallbackIconStyle default_style;
SendFallbackIconHelper(GURL(), gfx::kFaviconSize, default_style, callback);
}
...@@ -5,10 +5,18 @@ ...@@ -5,10 +5,18 @@
#ifndef CHROME_BROWSER_UI_WEBUI_FALLBACK_ICON_SOURCE_H_ #ifndef CHROME_BROWSER_UI_WEBUI_FALLBACK_ICON_SOURCE_H_
#define CHROME_BROWSER_UI_WEBUI_FALLBACK_ICON_SOURCE_H_ #define CHROME_BROWSER_UI_WEBUI_FALLBACK_ICON_SOURCE_H_
#include <string>
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "components/favicon/core/fallback_icon_service.h"
#include "content/public/browser/url_data_source.h" #include "content/public/browser/url_data_source.h"
class FallbackIconService;
class GURL;
namespace favicon_base {
struct FallbackIconStyle;
}
// FallbackIconSource services explicit chrome:// requests for fallback icons. // FallbackIconSource services explicit chrome:// requests for fallback icons.
// //
// Format: // Format:
...@@ -41,7 +49,8 @@ ...@@ -41,7 +49,8 @@
// 32 * 0.5 = 16, and the icon's background shape is a circle. // 32 * 0.5 = 16, and the icon's background shape is a circle.
class FallbackIconSource : public content::URLDataSource { class FallbackIconSource : public content::URLDataSource {
public: public:
FallbackIconSource(); // |fallback_icon_service| is owned by caller, and may be null.
explicit FallbackIconSource(FallbackIconService* fallback_icon_service);
~FallbackIconSource() override; ~FallbackIconSource() override;
...@@ -57,11 +66,17 @@ class FallbackIconSource : public content::URLDataSource { ...@@ -57,11 +66,17 @@ class FallbackIconSource : public content::URLDataSource {
bool ShouldServiceRequest(const net::URLRequest* request) const override; bool ShouldServiceRequest(const net::URLRequest* request) const override;
private: private:
void SendFallbackIconHelper(
const GURL& url,
int size_in_pixels,
const favicon_base::FallbackIconStyle& style,
const content::URLDataSource::GotDataCallback& callback);
// Sends the default fallback icon. // Sends the default fallback icon.
void SendDefaultResponse( void SendDefaultResponse(
const content::URLDataSource::GotDataCallback& callback); const content::URLDataSource::GotDataCallback& callback);
scoped_ptr<FallbackIconService> fallback_icon_service_; FallbackIconService* fallback_icon_service_;
DISALLOW_COPY_AND_ASSIGN(FallbackIconSource); DISALLOW_COPY_AND_ASSIGN(FallbackIconSource);
}; };
......
...@@ -4,21 +4,17 @@ ...@@ -4,21 +4,17 @@
#include "chrome/browser/ui/webui/large_icon_source.h" #include "chrome/browser/ui/webui/large_icon_source.h"
#include <string>
#include <vector> #include <vector>
#include "base/memory/ref_counted_memory.h" #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/browser/search/instant_io_context.h"
#include "chrome/common/favicon/large_icon_url_parser.h" #include "chrome/common/favicon/large_icon_url_parser.h"
#include "chrome/common/url_constants.h" #include "chrome/common/url_constants.h"
#include "components/favicon/core/fallback_icon_service.h"
#include "components/favicon/core/favicon_service.h" #include "components/favicon/core/favicon_service.h"
#include "components/favicon_base/fallback_icon_style.h" #include "components/favicon_base/fallback_icon_style.h"
#include "grit/platform_locale_settings.h"
#include "net/url_request/url_request.h" #include "net/url_request/url_request.h"
#include "third_party/skia/include/core/SkColor.h" #include "third_party/skia/include/core/SkColor.h"
#include "ui/base/l10n/l10n_util.h"
namespace { namespace {
...@@ -42,16 +38,10 @@ LargeIconSource::IconRequest::IconRequest( ...@@ -42,16 +38,10 @@ LargeIconSource::IconRequest::IconRequest(
LargeIconSource::IconRequest::~IconRequest() { LargeIconSource::IconRequest::~IconRequest() {
} }
LargeIconSource::LargeIconSource(Profile* profile) : profile_(profile) { LargeIconSource::LargeIconSource(FaviconService* favicon_service,
std::vector<std::string> font_list; FallbackIconService* fallback_icon_service)
#if defined(OS_CHROMEOS) : favicon_service_(favicon_service),
font_list.push_back("Noto Sans"); fallback_icon_service_(fallback_icon_service) {
#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 FallbackIconService(font_list));
} }
LargeIconSource::~LargeIconSource() { LargeIconSource::~LargeIconSource() {
...@@ -66,17 +56,16 @@ void LargeIconSource::StartDataRequest( ...@@ -66,17 +56,16 @@ void LargeIconSource::StartDataRequest(
int render_process_id, int render_process_id,
int render_frame_id, int render_frame_id,
const content::URLDataSource::GotDataCallback& callback) { const content::URLDataSource::GotDataCallback& callback) {
LargeIconUrlParser parser; if (!favicon_service_) {
bool success = parser.Parse(path);
if (!success || parser.size_in_pixels() <= 0 ||
parser.size_in_pixels() > kMaxLargeIconSize) {
SendNotFoundResponse(callback); SendNotFoundResponse(callback);
return; return;
} }
FaviconService* favicon_service = FaviconServiceFactory::GetForProfile( LargeIconUrlParser parser;
profile_, ServiceAccessType::EXPLICIT_ACCESS); bool success = parser.Parse(path);
if (!favicon_service) { if (!success ||
parser.size_in_pixels() <= 0 ||
parser.size_in_pixels() > kMaxLargeIconSize) {
SendNotFoundResponse(callback); SendNotFoundResponse(callback);
return; return;
} }
...@@ -87,7 +76,7 @@ void LargeIconSource::StartDataRequest( ...@@ -87,7 +76,7 @@ void LargeIconSource::StartDataRequest(
return; return;
} }
favicon_service->GetRawFaviconForPageURL( favicon_service_->GetRawFaviconForPageURL(
url, url,
favicon_base::TOUCH_ICON | favicon_base::TOUCH_PRECOMPOSED_ICON, favicon_base::TOUCH_ICON | favicon_base::TOUCH_PRECOMPOSED_ICON,
parser.size_in_pixels(), parser.size_in_pixels(),
...@@ -127,6 +116,10 @@ void LargeIconSource::OnIconDataAvailable( ...@@ -127,6 +116,10 @@ void LargeIconSource::OnIconDataAvailable(
} }
void LargeIconSource::SendFallbackIcon(const IconRequest& request) { void LargeIconSource::SendFallbackIcon(const IconRequest& request) {
if (!fallback_icon_service_) {
SendNotFoundResponse(request.callback);
return;
}
favicon_base::FallbackIconStyle style; favicon_base::FallbackIconStyle style;
style.background_color = SkColorSetRGB(0xcc, 0xcc, 0xcc); style.background_color = SkColorSetRGB(0xcc, 0xcc, 0xcc);
favicon_base::MatchFallbackIconTextColorAgainstBackgroundColor(&style); favicon_base::MatchFallbackIconTextColorAgainstBackgroundColor(&style);
......
...@@ -5,13 +5,16 @@ ...@@ -5,13 +5,16 @@
#ifndef CHROME_BROWSER_UI_WEBUI_LARGE_ICON_SOURCE_H_ #ifndef CHROME_BROWSER_UI_WEBUI_LARGE_ICON_SOURCE_H_
#define CHROME_BROWSER_UI_WEBUI_LARGE_ICON_SOURCE_H_ #define CHROME_BROWSER_UI_WEBUI_LARGE_ICON_SOURCE_H_
#include <string>
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "base/task/cancelable_task_tracker.h" #include "base/task/cancelable_task_tracker.h"
#include "components/favicon/core/fallback_icon_service.h" #include "components/favicon/core/fallback_icon_service.h"
#include "components/favicon_base/favicon_types.h" #include "components/favicon_base/favicon_types.h"
#include "content/public/browser/url_data_source.h" #include "content/public/browser/url_data_source.h"
class Profile; class FallbackIconService;
class FaviconService;
// LargeIconSource services explicit chrome:// requests for large icons. // LargeIconSource services explicit chrome:// requests for large icons.
// //
...@@ -28,7 +31,10 @@ class Profile; ...@@ -28,7 +31,10 @@ class Profile;
// This requests a 48x48 large icon for http://www.google.com. // This requests a 48x48 large icon for http://www.google.com.
class LargeIconSource : public content::URLDataSource { class LargeIconSource : public content::URLDataSource {
public: public:
explicit LargeIconSource(Profile* profile); // |favicon_service| and |fallback_icon_service| are owned by caller and may
// be null.
LargeIconSource(FaviconService* favicon_service,
FallbackIconService* fallback_icon_service);
~LargeIconSource() override; ~LargeIconSource() override;
...@@ -69,11 +75,11 @@ class LargeIconSource : public content::URLDataSource { ...@@ -69,11 +75,11 @@ class LargeIconSource : public content::URLDataSource {
void SendNotFoundResponse( void SendNotFoundResponse(
const content::URLDataSource::GotDataCallback& callback); const content::URLDataSource::GotDataCallback& callback);
Profile* profile_;
base::CancelableTaskTracker cancelable_task_tracker_; base::CancelableTaskTracker cancelable_task_tracker_;
scoped_ptr<FallbackIconService> fallback_icon_service_; FaviconService* favicon_service_;
FallbackIconService* fallback_icon_service_;
DISALLOW_COPY_AND_ASSIGN(LargeIconSource); DISALLOW_COPY_AND_ASSIGN(LargeIconSource);
}; };
......
...@@ -20,6 +20,8 @@ ...@@ -20,6 +20,8 @@
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "base/threading/thread.h" #include "base/threading/thread.h"
#include "base/values.h" #include "base/values.h"
#include "chrome/browser/favicon/fallback_icon_service_factory.h"
#include "chrome/browser/favicon/favicon_service_factory.h"
#include "chrome/browser/history/top_sites_factory.h" #include "chrome/browser/history/top_sites_factory.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/thumbnails/thumbnail_list_source.h" #include "chrome/browser/thumbnails/thumbnail_list_source.h"
...@@ -35,8 +37,11 @@ ...@@ -35,8 +37,11 @@
#include "chrome/browser/ui/webui/ntp/thumbnail_source.h" #include "chrome/browser/ui/webui/ntp/thumbnail_source.h"
#include "chrome/common/pref_names.h" #include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h" #include "chrome/common/url_constants.h"
#include "components/favicon/core/fallback_icon_service.h"
#include "components/favicon/core/favicon_service.h"
#include "components/history/core/browser/page_usage_data.h" #include "components/history/core/browser/page_usage_data.h"
#include "components/history/core/browser/top_sites.h" #include "components/history/core/browser/top_sites.h"
#include "components/keyed_service/core/service_access_type.h"
#include "components/pref_registry/pref_registry_syncable.h" #include "components/pref_registry/pref_registry_syncable.h"
#include "content/public/browser/navigation_controller.h" #include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_entry.h" #include "content/public/browser/navigation_entry.h"
...@@ -81,11 +86,16 @@ void MostVisitedHandler::RegisterMessages() { ...@@ -81,11 +86,16 @@ void MostVisitedHandler::RegisterMessages() {
// Set up our sources for top-sites data. // Set up our sources for top-sites data.
content::URLDataSource::Add(profile, new ThumbnailListSource(profile)); content::URLDataSource::Add(profile, new ThumbnailListSource(profile));
// Register chrome://large-icon as a data source for large icons. FaviconService* favicon_service = FaviconServiceFactory::GetForProfile(
content::URLDataSource::Add(profile, new LargeIconSource(profile)); profile, ServiceAccessType::EXPLICIT_ACCESS);
FallbackIconService* fallback_icon_service =
FallbackIconServiceFactory::GetForBrowserContext(profile);
// Register chrome://fallback-icon as a data source for fallback icons. // Register chrome://large-icon as a data source for large icons.
content::URLDataSource::Add(profile, new FallbackIconSource()); content::URLDataSource::Add(profile,
new LargeIconSource(favicon_service, fallback_icon_service));
content::URLDataSource::Add(profile,
new FallbackIconSource(fallback_icon_service));
// Register chrome://favicon as a data source for favicons. // Register chrome://favicon as a data source for favicons.
content::URLDataSource::Add( content::URLDataSource::Add(
......
...@@ -1545,10 +1545,16 @@ ...@@ -1545,10 +1545,16 @@
'browser/sync_file_system/task_logger.h', 'browser/sync_file_system/task_logger.h',
], ],
'chrome_browser_favicon_sources': [ 'chrome_browser_favicon_sources': [
'browser/favicon/chrome_fallback_icon_client.cc',
'browser/favicon/chrome_fallback_icon_client.h',
'browser/favicon/chrome_fallback_icon_client_factory.cc',
'browser/favicon/chrome_fallback_icon_client_factory.h',
'browser/favicon/chrome_favicon_client.cc', 'browser/favicon/chrome_favicon_client.cc',
'browser/favicon/chrome_favicon_client.h', 'browser/favicon/chrome_favicon_client.h',
'browser/favicon/chrome_favicon_client_factory.cc', 'browser/favicon/chrome_favicon_client_factory.cc',
'browser/favicon/chrome_favicon_client_factory.h', 'browser/favicon/chrome_favicon_client_factory.h',
'browser/favicon/fallback_icon_service_factory.cc',
'browser/favicon/fallback_icon_service_factory.h',
'browser/favicon/favicon_service_factory.cc', 'browser/favicon/favicon_service_factory.cc',
'browser/favicon/favicon_service_factory.h', 'browser/favicon/favicon_service_factory.h',
'browser/favicon/favicon_tab_helper.cc', 'browser/favicon/favicon_tab_helper.cc',
......
...@@ -20,7 +20,9 @@ ...@@ -20,7 +20,9 @@
#include "chrome/browser/bookmarks/chrome_bookmark_client_factory.h" #include "chrome/browser/bookmarks/chrome_bookmark_client_factory.h"
#include "chrome/browser/browser_process.h" #include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_notification_types.h" #include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/favicon/chrome_fallback_icon_client_factory.h"
#include "chrome/browser/favicon/chrome_favicon_client_factory.h" #include "chrome/browser/favicon/chrome_favicon_client_factory.h"
#include "chrome/browser/favicon/fallback_icon_service_factory.h"
#include "chrome/browser/favicon/favicon_service_factory.h" #include "chrome/browser/favicon/favicon_service_factory.h"
#include "chrome/browser/history/chrome_history_client.h" #include "chrome/browser/history/chrome_history_client.h"
#include "chrome/browser/history/chrome_history_client_factory.h" #include "chrome/browser/history/chrome_history_client_factory.h"
...@@ -54,6 +56,7 @@ ...@@ -54,6 +56,7 @@
#include "components/bookmarks/browser/bookmark_model.h" #include "components/bookmarks/browser/bookmark_model.h"
#include "components/bookmarks/common/bookmark_constants.h" #include "components/bookmarks/common/bookmark_constants.h"
#include "components/content_settings/core/browser/host_content_settings_map.h" #include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/favicon/core/fallback_icon_service.h"
#include "components/favicon/core/favicon_service.h" #include "components/favicon/core/favicon_service.h"
#include "components/history/content/browser/content_visit_delegate.h" #include "components/history/content/browser/content_visit_delegate.h"
#include "components/history/content/browser/history_database_helper.h" #include "components/history/content/browser/history_database_helper.h"
...@@ -213,6 +216,12 @@ KeyedService* CreateTestDesktopNotificationService( ...@@ -213,6 +216,12 @@ KeyedService* CreateTestDesktopNotificationService(
} }
#endif #endif
KeyedService* BuildFallbackIconService(content::BrowserContext* context) {
Profile* profile = Profile::FromBrowserContext(context);
return new FallbackIconService(
ChromeFallbackIconClientFactory::GetForBrowserContext(profile));
}
KeyedService* BuildFaviconService(content::BrowserContext* context) { KeyedService* BuildFaviconService(content::BrowserContext* context) {
Profile* profile = Profile::FromBrowserContext(context); Profile* profile = Profile::FromBrowserContext(context);
return new FaviconService(ChromeFaviconClientFactory::GetForProfile(profile), return new FaviconService(ChromeFaviconClientFactory::GetForProfile(profile),
...@@ -570,6 +579,11 @@ TestingProfile::~TestingProfile() { ...@@ -570,6 +579,11 @@ TestingProfile::~TestingProfile() {
} }
} }
void TestingProfile::CreateFallbackIconService() {
FaviconServiceFactory::GetInstance()->SetTestingFactory(
this, BuildFallbackIconService);
}
void TestingProfile::CreateFaviconService() { void TestingProfile::CreateFaviconService() {
// It is up to the caller to create the history service if one is needed. // It is up to the caller to create the history service if one is needed.
FaviconServiceFactory::GetInstance()->SetTestingFactory( FaviconServiceFactory::GetInstance()->SetTestingFactory(
......
...@@ -6,6 +6,8 @@ ...@@ -6,6 +6,8 @@
#define CHROME_TEST_BASE_TESTING_PROFILE_H_ #define CHROME_TEST_BASE_TESTING_PROFILE_H_
#include <string> #include <string>
#include <utility>
#include <vector>
#include "base/files/scoped_temp_dir.h" #include "base/files/scoped_temp_dir.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
...@@ -164,6 +166,9 @@ class TestingProfile : public Profile { ...@@ -164,6 +166,9 @@ class TestingProfile : public Profile {
~TestingProfile() override; ~TestingProfile() override;
// Creates the fallback icon service.
void CreateFallbackIconService();
// Creates the favicon service. Consequent calls would recreate the service. // Creates the favicon service. Consequent calls would recreate the service.
void CreateFaviconService(); void CreateFaviconService();
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
], ],
'sources': [ 'sources': [
# Note: sources list duplicated in GN build. # Note: sources list duplicated in GN build.
'favicon/core/fallback_icon_client.h',
'favicon/core/fallback_icon_service.cc', 'favicon/core/fallback_icon_service.cc',
'favicon/core/fallback_icon_service.h', 'favicon/core/fallback_icon_service.h',
'favicon/core/favicon_client.h', 'favicon/core/favicon_client.h',
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
static_library("core") { static_library("core") {
sources = [ sources = [
"fallback_icon_client.h",
"fallback_icon_service.cc", "fallback_icon_service.cc",
"fallback_icon_service.h", "fallback_icon_service.h",
"favicon_client.h", "favicon_client.h",
......
...@@ -2,7 +2,6 @@ include_rules = [ ...@@ -2,7 +2,6 @@ include_rules = [
"+components/bookmarks/browser", "+components/bookmarks/browser",
"+components/history/core/browser", "+components/history/core/browser",
"+components/keyed_service/core", "+components/keyed_service/core",
"+net/base/registry_controlled_domains/registry_controlled_domain.h",
"+skia", "+skia",
"+third_party/skia", "+third_party/skia",
"+third_party/skia/include", "+third_party/skia/include",
......
// 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.
#ifndef COMPONENTS_FAVICON_CORE_FALLBACK_ICON_CLIENT_H_
#define COMPONENTS_FAVICON_CORE_FALLBACK_ICON_CLIENT_H_
#include <string>
#include <vector>
#include "base/strings/string16.h"
#include "components/keyed_service/core/keyed_service.h"
class GURL;
// This class abstracts operations that depend on the embedder's environment,
// e.g. Chrome.
class FallbackIconClient : public KeyedService {
public:
// Returns a list of font names for fallback icon rendering.
virtual const std::vector<std::string>& GetFontNameList() const = 0;
// Returns the text to render in fallback icon for |url|.
virtual base::string16 GetFallbackIconText(const GURL& url) const = 0;
protected:
~FallbackIconClient() override {}
};
#endif // COMPONENTS_FAVICON_CORE_FALLBACK_ICON_CLIENT_H_
...@@ -6,10 +6,8 @@ ...@@ -6,10 +6,8 @@
#include <algorithm> #include <algorithm>
#include "base/i18n/case_conversion.h" #include "components/favicon/core/fallback_icon_client.h"
#include "base/strings/utf_string_conversions.h"
#include "components/favicon_base/fallback_icon_style.h" #include "components/favicon_base/fallback_icon_style.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "third_party/skia/include/core/SkPaint.h" #include "third_party/skia/include/core/SkPaint.h"
#include "ui/gfx/canvas.h" #include "ui/gfx/canvas.h"
#include "ui/gfx/codec/png_codec.h" #include "ui/gfx/codec/png_codec.h"
...@@ -23,21 +21,11 @@ namespace { ...@@ -23,21 +21,11 @@ namespace {
// Arbitrary maximum icon size, can be reasonably increased if needed. // Arbitrary maximum icon size, can be reasonably increased if needed.
const int kMaxFallbackFaviconSize = 288; const int kMaxFallbackFaviconSize = 288;
// Returns a single character to represent a page URL. To do this we take the
// first letter in a domain's name and make it upper case.
// TODO(huangs): Handle non-ASCII ("xn--") domain names.
base::string16 GetFallbackIconText(const GURL& url) {
std::string domain = net::registry_controlled_domains::GetDomainAndRegistry(
url, net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
return domain.empty() ? base::string16() :
base::i18n::ToUpper(base::ASCIIToUTF16(domain.substr(0, 1)));
}
} // namespace } // namespace
FallbackIconService::FallbackIconService( FallbackIconService::FallbackIconService(
const std::vector<std::string>& font_list) FallbackIconClient* fallback_icon_client)
: font_list_(font_list) { : fallback_icon_client_(fallback_icon_client) {
} }
FallbackIconService::~FallbackIconService() { FallbackIconService::~FallbackIconService() {
...@@ -77,17 +65,18 @@ void FallbackIconService::DrawFallbackIcon( ...@@ -77,17 +65,18 @@ void FallbackIconService::DrawFallbackIcon(
gfx::Rect(kOffsetX, kOffsetY, size, size), corner_radius, paint); gfx::Rect(kOffsetX, kOffsetY, size, size), corner_radius, paint);
// Draw text. // Draw text.
base::string16 icon_text = GetFallbackIconText(icon_url); base::string16 icon_text =
fallback_icon_client_->GetFallbackIconText(icon_url);
if (icon_text.empty()) if (icon_text.empty())
return; return;
int font_size = static_cast<int>(size * style.font_size_ratio); int font_size = static_cast<int>(size * style.font_size_ratio);
if (font_size <= 0) if (font_size <= 0)
return; return;
// TODO(huangs): See how expensive gfx::FontList() is, and possibly cache.
canvas->DrawStringRectWithFlags( canvas->DrawStringRectWithFlags(
icon_text, icon_text,
gfx::FontList(font_list_, gfx::Font::NORMAL, font_size), gfx::FontList(fallback_icon_client_->GetFontNameList(), gfx::Font::NORMAL,
font_size),
style.text_color, style.text_color,
gfx::Rect(kOffsetX, kOffsetY, size, size), gfx::Rect(kOffsetX, kOffsetY, size, size),
gfx::Canvas::TEXT_ALIGN_CENTER); gfx::Canvas::TEXT_ALIGN_CENTER);
......
...@@ -5,11 +5,12 @@ ...@@ -5,11 +5,12 @@
#ifndef COMPONENTS_FAVICON_CORE_FALLBACK_ICON_SERVICE_H_ #ifndef COMPONENTS_FAVICON_CORE_FALLBACK_ICON_SERVICE_H_
#define COMPONENTS_FAVICON_CORE_FALLBACK_ICON_SERVICE_H_ #define COMPONENTS_FAVICON_CORE_FALLBACK_ICON_SERVICE_H_
#include <string>
#include <vector> #include <vector>
#include "base/macros.h" #include "base/macros.h"
#include "components/keyed_service/core/keyed_service.h"
class FallbackIconClient;
class GURL; class GURL;
namespace gfx { namespace gfx {
...@@ -21,10 +22,10 @@ struct FallbackIconStyle; ...@@ -21,10 +22,10 @@ struct FallbackIconStyle;
} }
// A service to provide methods to render fallback favicons. // A service to provide methods to render fallback favicons.
class FallbackIconService { class FallbackIconService : public KeyedService {
public: public:
explicit FallbackIconService(const std::vector<std::string>& font_list); explicit FallbackIconService(FallbackIconClient* fallback_icon_client);
~FallbackIconService(); ~FallbackIconService() override;
// Renders a fallback icon synchronously and returns the bitmap. Returns an // Renders a fallback icon synchronously and returns the bitmap. Returns an
// empty std::vector on failure. |size| is icon width and height in pixels. // empty std::vector on failure. |size| is icon width and height in pixels.
...@@ -41,7 +42,7 @@ class FallbackIconService { ...@@ -41,7 +42,7 @@ class FallbackIconService {
const favicon_base::FallbackIconStyle& style, const favicon_base::FallbackIconStyle& style,
gfx::Canvas* canvas); gfx::Canvas* canvas);
std::vector<std::string> font_list_; FallbackIconClient* fallback_icon_client_;
DISALLOW_COPY_AND_ASSIGN(FallbackIconService); DISALLOW_COPY_AND_ASSIGN(FallbackIconService);
}; };
......
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