diff options
author | huangs <huangs@chromium.org> | 2015-03-27 14:59:14 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-03-27 22:00:03 +0000 |
commit | f16444b34b44d6e9de555756e24017ff7670d73a (patch) | |
tree | 6acbfba91d7c2689c6c8f4a50f98e8c746a7639a | |
parent | 328a417b64722158a976288a65733925a65a519d (diff) | |
download | chromium_src-f16444b34b44d6e9de555756e24017ff7670d73a.zip chromium_src-f16444b34b44d6e9de555756e24017ff7670d73a.tar.gz chromium_src-f16444b34b44d6e9de555756e24017ff7670d73a.tar.bz2 |
[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}
23 files changed, 408 insertions, 81 deletions
diff --git a/chrome/browser/favicon/DEPS b/chrome/browser/favicon/DEPS index 4dcd7ec..7bc0c6f 100644 --- a/chrome/browser/favicon/DEPS +++ b/chrome/browser/favicon/DEPS @@ -12,6 +12,7 @@ include_rules = [ "+chrome/browser/profiles/incognito_helpers.h", "+chrome/browser/search/search.h", "+components/favicon/core", + "+net/base/registry_controlled_domains/registry_controlled_domain.h", "+third_party/skia/include/core/SkBitmap.h", # TODO(caitkp): Bring this list to zero. diff --git a/chrome/browser/favicon/chrome_fallback_icon_client.cc b/chrome/browser/favicon/chrome_fallback_icon_client.cc new file mode 100644 index 0000000..29651d2 --- /dev/null +++ b/chrome/browser/favicon/chrome_fallback_icon_client.cc @@ -0,0 +1,41 @@ +// 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))); +} diff --git a/chrome/browser/favicon/chrome_fallback_icon_client.h b/chrome/browser/favicon/chrome_fallback_icon_client.h new file mode 100644 index 0000000..1af050d --- /dev/null +++ b/chrome/browser/favicon/chrome_fallback_icon_client.h @@ -0,0 +1,34 @@ +// 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_ diff --git a/chrome/browser/favicon/chrome_fallback_icon_client_factory.cc b/chrome/browser/favicon/chrome_fallback_icon_client_factory.cc new file mode 100644 index 0000000..8176a0e --- /dev/null +++ b/chrome/browser/favicon/chrome_fallback_icon_client_factory.cc @@ -0,0 +1,38 @@ +// 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(); +} diff --git a/chrome/browser/favicon/chrome_fallback_icon_client_factory.h b/chrome/browser/favicon/chrome_fallback_icon_client_factory.h new file mode 100644 index 0000000..1cbe4cd --- /dev/null +++ b/chrome/browser/favicon/chrome_fallback_icon_client_factory.h @@ -0,0 +1,42 @@ +// 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_ diff --git a/chrome/browser/favicon/fallback_icon_service_factory.cc b/chrome/browser/favicon/fallback_icon_service_factory.cc new file mode 100644 index 0000000..9c8f465 --- /dev/null +++ b/chrome/browser/favicon/fallback_icon_service_factory.cc @@ -0,0 +1,43 @@ +// 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; +} diff --git a/chrome/browser/favicon/fallback_icon_service_factory.h b/chrome/browser/favicon/fallback_icon_service_factory.h new file mode 100644 index 0000000..30a3a72 --- /dev/null +++ b/chrome/browser/favicon/fallback_icon_service_factory.h @@ -0,0 +1,41 @@ +// 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_ diff --git a/chrome/browser/favicon/favicon_service_factory.h b/chrome/browser/favicon/favicon_service_factory.h index 36157bb..f62e408 100644 --- a/chrome/browser/favicon/favicon_service_factory.h +++ b/chrome/browser/favicon/favicon_service_factory.h @@ -18,7 +18,7 @@ class Profile; // Profiles. class FaviconServiceFactory : public BrowserContextKeyedServiceFactory { 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. static FaviconService* GetForProfile(Profile* profile, ServiceAccessType sat); diff --git a/chrome/browser/search/instant_service.cc b/chrome/browser/search/instant_service.cc index 818cec9..77b13bc 100644 --- a/chrome/browser/search/instant_service.cc +++ b/chrome/browser/search/instant_service.cc @@ -5,6 +5,8 @@ #include "chrome/browser/search/instant_service.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/profiles/profile.h" #include "chrome/browser/search/instant_io_context.h" @@ -25,7 +27,10 @@ #include "chrome/browser/ui/webui/ntp/thumbnail_source.h" #include "chrome/browser/ui/webui/theme_source.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/keyed_service/core/service_access_type.h" #include "components/search_engines/template_url_service.h" #include "content/public/browser/browser_thread.h" #include "content/public/browser/notification_service.h" @@ -126,8 +131,15 @@ InstantService::InstantService(Profile* profile) content::URLDataSource::Add(profile_, new ThumbnailListSource(profile_)); #endif // !defined(OS_ANDROID) - content::URLDataSource::Add(profile_, new LargeIconSource(profile)); - content::URLDataSource::Add(profile_, new FallbackIconSource()); + FaviconService* favicon_service = FaviconServiceFactory::GetForProfile( + 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( profile_, new FaviconSource(profile_, FaviconSource::FAVICON)); content::URLDataSource::Add(profile_, new MostVisitedIframeSource()); diff --git a/chrome/browser/ui/webui/fallback_icon_source.cc b/chrome/browser/ui/webui/fallback_icon_source.cc index e568b9d..deb5337 100644 --- a/chrome/browser/ui/webui/fallback_icon_source.cc +++ b/chrome/browser/ui/webui/fallback_icon_source.cc @@ -4,29 +4,21 @@ #include "chrome/browser/ui/webui/fallback_icon_source.h" -#include <string> #include <vector> #include "base/memory/ref_counted_memory.h" #include "chrome/browser/search/instant_io_context.h" #include "chrome/common/favicon/fallback_icon_url_parser.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 "ui/base/l10n/l10n_util.h" #include "ui/gfx/favicon_size.h" #include "url/gurl.h" -FallbackIconSource::FallbackIconSource() { - 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 FallbackIconService(font_list)); +FallbackIconSource::FallbackIconSource( + FallbackIconService* fallback_icon_service) + : fallback_icon_service_(fallback_icon_service) { } FallbackIconSource::~FallbackIconSource() { @@ -47,15 +39,15 @@ void FallbackIconSource::StartDataRequest( SendDefaultResponse(callback); return; } + GURL url(parsed.url_string()); if (url.is_empty() || !url.is_valid()) { SendDefaultResponse(callback); return; } - std::vector<unsigned char> bitmap_data = - fallback_icon_service_->RenderFallbackIconBitmap( - url, parsed.size_in_pixels(), parsed.style()); - callback.Run(base::RefCountedBytes::TakeVector(&bitmap_data)); + + SendFallbackIconHelper( + url, parsed.size_in_pixels(), parsed.style(), callback); } std::string FallbackIconSource::GetMimeType(const std::string&) const { @@ -77,10 +69,23 @@ bool FallbackIconSource::ShouldServiceRequest( 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) { + if (!fallback_icon_service_) { // Can be null for tests. + callback.Run(nullptr); // Trigger "Not Found" response. + return; + } std::vector<unsigned char> bitmap_data = fallback_icon_service_->RenderFallbackIconBitmap( - GURL(), gfx::kFaviconSize, favicon_base::FallbackIconStyle()); + url, size_in_pixels, style); 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); +} diff --git a/chrome/browser/ui/webui/fallback_icon_source.h b/chrome/browser/ui/webui/fallback_icon_source.h index acc1d6b..c21fd9b 100644 --- a/chrome/browser/ui/webui/fallback_icon_source.h +++ b/chrome/browser/ui/webui/fallback_icon_source.h @@ -5,10 +5,18 @@ #ifndef 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 "components/favicon/core/fallback_icon_service.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. // // Format: @@ -41,7 +49,8 @@ // 32 * 0.5 = 16, and the icon's background shape is a circle. class FallbackIconSource : public content::URLDataSource { public: - FallbackIconSource(); + // |fallback_icon_service| is owned by caller, and may be null. + explicit FallbackIconSource(FallbackIconService* fallback_icon_service); ~FallbackIconSource() override; @@ -57,11 +66,17 @@ class FallbackIconSource : public content::URLDataSource { bool ShouldServiceRequest(const net::URLRequest* request) const override; 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. void SendDefaultResponse( const content::URLDataSource::GotDataCallback& callback); - scoped_ptr<FallbackIconService> fallback_icon_service_; + FallbackIconService* fallback_icon_service_; DISALLOW_COPY_AND_ASSIGN(FallbackIconSource); }; diff --git a/chrome/browser/ui/webui/large_icon_source.cc b/chrome/browser/ui/webui/large_icon_source.cc index e28d342..1c6d0d2 100644 --- a/chrome/browser/ui/webui/large_icon_source.cc +++ b/chrome/browser/ui/webui/large_icon_source.cc @@ -4,21 +4,17 @@ #include "chrome/browser/ui/webui/large_icon_source.h" -#include <string> #include <vector> #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/common/favicon/large_icon_url_parser.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_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" namespace { @@ -42,16 +38,10 @@ LargeIconSource::IconRequest::IconRequest( LargeIconSource::IconRequest::~IconRequest() { } -LargeIconSource::LargeIconSource(Profile* profile) : profile_(profile) { - 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 FallbackIconService(font_list)); +LargeIconSource::LargeIconSource(FaviconService* favicon_service, + FallbackIconService* fallback_icon_service) + : favicon_service_(favicon_service), + fallback_icon_service_(fallback_icon_service) { } LargeIconSource::~LargeIconSource() { @@ -66,17 +56,16 @@ void LargeIconSource::StartDataRequest( int render_process_id, int render_frame_id, const content::URLDataSource::GotDataCallback& callback) { - LargeIconUrlParser parser; - bool success = parser.Parse(path); - if (!success || parser.size_in_pixels() <= 0 || - parser.size_in_pixels() > kMaxLargeIconSize) { + if (!favicon_service_) { SendNotFoundResponse(callback); return; } - FaviconService* favicon_service = FaviconServiceFactory::GetForProfile( - profile_, ServiceAccessType::EXPLICIT_ACCESS); - if (!favicon_service) { + LargeIconUrlParser parser; + bool success = parser.Parse(path); + if (!success || + parser.size_in_pixels() <= 0 || + parser.size_in_pixels() > kMaxLargeIconSize) { SendNotFoundResponse(callback); return; } @@ -87,7 +76,7 @@ void LargeIconSource::StartDataRequest( return; } - favicon_service->GetRawFaviconForPageURL( + favicon_service_->GetRawFaviconForPageURL( url, favicon_base::TOUCH_ICON | favicon_base::TOUCH_PRECOMPOSED_ICON, parser.size_in_pixels(), @@ -127,6 +116,10 @@ void LargeIconSource::OnIconDataAvailable( } void LargeIconSource::SendFallbackIcon(const IconRequest& request) { + if (!fallback_icon_service_) { + SendNotFoundResponse(request.callback); + return; + } favicon_base::FallbackIconStyle style; style.background_color = SkColorSetRGB(0xcc, 0xcc, 0xcc); favicon_base::MatchFallbackIconTextColorAgainstBackgroundColor(&style); diff --git a/chrome/browser/ui/webui/large_icon_source.h b/chrome/browser/ui/webui/large_icon_source.h index df3fad6..7fcb825 100644 --- a/chrome/browser/ui/webui/large_icon_source.h +++ b/chrome/browser/ui/webui/large_icon_source.h @@ -5,13 +5,16 @@ #ifndef 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/task/cancelable_task_tracker.h" #include "components/favicon/core/fallback_icon_service.h" #include "components/favicon_base/favicon_types.h" #include "content/public/browser/url_data_source.h" -class Profile; +class FallbackIconService; +class FaviconService; // LargeIconSource services explicit chrome:// requests for large icons. // @@ -28,7 +31,10 @@ class Profile; // This requests a 48x48 large icon for http://www.google.com. class LargeIconSource : public content::URLDataSource { 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; @@ -69,11 +75,11 @@ class LargeIconSource : public content::URLDataSource { void SendNotFoundResponse( const content::URLDataSource::GotDataCallback& callback); - Profile* profile_; - base::CancelableTaskTracker cancelable_task_tracker_; - scoped_ptr<FallbackIconService> fallback_icon_service_; + FaviconService* favicon_service_; + + FallbackIconService* fallback_icon_service_; DISALLOW_COPY_AND_ASSIGN(LargeIconSource); }; diff --git a/chrome/browser/ui/webui/ntp/most_visited_handler.cc b/chrome/browser/ui/webui/ntp/most_visited_handler.cc index 6b746ebf..e6a90c1 100644 --- a/chrome/browser/ui/webui/ntp/most_visited_handler.cc +++ b/chrome/browser/ui/webui/ntp/most_visited_handler.cc @@ -20,6 +20,8 @@ #include "base/strings/utf_string_conversions.h" #include "base/threading/thread.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/profiles/profile.h" #include "chrome/browser/thumbnails/thumbnail_list_source.h" @@ -35,8 +37,11 @@ #include "chrome/browser/ui/webui/ntp/thumbnail_source.h" #include "chrome/common/pref_names.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/top_sites.h" +#include "components/keyed_service/core/service_access_type.h" #include "components/pref_registry/pref_registry_syncable.h" #include "content/public/browser/navigation_controller.h" #include "content/public/browser/navigation_entry.h" @@ -81,11 +86,16 @@ void MostVisitedHandler::RegisterMessages() { // Set up our sources for top-sites data. content::URLDataSource::Add(profile, new ThumbnailListSource(profile)); - // Register chrome://large-icon as a data source for large icons. - content::URLDataSource::Add(profile, new LargeIconSource(profile)); + FaviconService* favicon_service = FaviconServiceFactory::GetForProfile( + profile, ServiceAccessType::EXPLICIT_ACCESS); + FallbackIconService* fallback_icon_service = + FallbackIconServiceFactory::GetForBrowserContext(profile); - // Register chrome://fallback-icon as a data source for fallback icons. - content::URLDataSource::Add(profile, new FallbackIconSource()); + // Register chrome://large-icon as a data source for large icons. + 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. content::URLDataSource::Add( diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index 5bfb010..35f13c2 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -1545,10 +1545,16 @@ 'browser/sync_file_system/task_logger.h', ], '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.h', 'browser/favicon/chrome_favicon_client_factory.cc', '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.h', 'browser/favicon/favicon_tab_helper.cc', diff --git a/chrome/test/base/testing_profile.cc b/chrome/test/base/testing_profile.cc index 004a73c..d33aeef 100644 --- a/chrome/test/base/testing_profile.cc +++ b/chrome/test/base/testing_profile.cc @@ -20,7 +20,9 @@ #include "chrome/browser/bookmarks/chrome_bookmark_client_factory.h" #include "chrome/browser/browser_process.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/fallback_icon_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_factory.h" @@ -54,6 +56,7 @@ #include "components/bookmarks/browser/bookmark_model.h" #include "components/bookmarks/common/bookmark_constants.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/history/content/browser/content_visit_delegate.h" #include "components/history/content/browser/history_database_helper.h" @@ -213,6 +216,12 @@ KeyedService* CreateTestDesktopNotificationService( } #endif +KeyedService* BuildFallbackIconService(content::BrowserContext* context) { + Profile* profile = Profile::FromBrowserContext(context); + return new FallbackIconService( + ChromeFallbackIconClientFactory::GetForBrowserContext(profile)); +} + KeyedService* BuildFaviconService(content::BrowserContext* context) { Profile* profile = Profile::FromBrowserContext(context); return new FaviconService(ChromeFaviconClientFactory::GetForProfile(profile), @@ -570,6 +579,11 @@ TestingProfile::~TestingProfile() { } } +void TestingProfile::CreateFallbackIconService() { + FaviconServiceFactory::GetInstance()->SetTestingFactory( + this, BuildFallbackIconService); +} + void TestingProfile::CreateFaviconService() { // It is up to the caller to create the history service if one is needed. FaviconServiceFactory::GetInstance()->SetTestingFactory( diff --git a/chrome/test/base/testing_profile.h b/chrome/test/base/testing_profile.h index 8db54ee..1cf11c4 100644 --- a/chrome/test/base/testing_profile.h +++ b/chrome/test/base/testing_profile.h @@ -6,6 +6,8 @@ #define CHROME_TEST_BASE_TESTING_PROFILE_H_ #include <string> +#include <utility> +#include <vector> #include "base/files/scoped_temp_dir.h" #include "base/memory/ref_counted.h" @@ -164,6 +166,9 @@ class TestingProfile : public Profile { ~TestingProfile() override; + // Creates the fallback icon service. + void CreateFallbackIconService(); + // Creates the favicon service. Consequent calls would recreate the service. void CreateFaviconService(); diff --git a/components/favicon.gypi b/components/favicon.gypi index cddc746..8a9378b 100644 --- a/components/favicon.gypi +++ b/components/favicon.gypi @@ -19,6 +19,7 @@ ], 'sources': [ # Note: sources list duplicated in GN build. + 'favicon/core/fallback_icon_client.h', 'favicon/core/fallback_icon_service.cc', 'favicon/core/fallback_icon_service.h', 'favicon/core/favicon_client.h', diff --git a/components/favicon/core/BUILD.gn b/components/favicon/core/BUILD.gn index bbda145..8a4a0f7 100644 --- a/components/favicon/core/BUILD.gn +++ b/components/favicon/core/BUILD.gn @@ -4,6 +4,7 @@ static_library("core") { sources = [ + "fallback_icon_client.h", "fallback_icon_service.cc", "fallback_icon_service.h", "favicon_client.h", diff --git a/components/favicon/core/DEPS b/components/favicon/core/DEPS index ccb5231..f4dd1e6 100644 --- a/components/favicon/core/DEPS +++ b/components/favicon/core/DEPS @@ -2,7 +2,6 @@ include_rules = [ "+components/bookmarks/browser", "+components/history/core/browser", "+components/keyed_service/core", - "+net/base/registry_controlled_domains/registry_controlled_domain.h", "+skia", "+third_party/skia", "+third_party/skia/include", diff --git a/components/favicon/core/fallback_icon_client.h b/components/favicon/core/fallback_icon_client.h new file mode 100644 index 0000000..11bec7a --- /dev/null +++ b/components/favicon/core/fallback_icon_client.h @@ -0,0 +1,30 @@ +// 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_ diff --git a/components/favicon/core/fallback_icon_service.cc b/components/favicon/core/fallback_icon_service.cc index e174818..cf7a19b 100644 --- a/components/favicon/core/fallback_icon_service.cc +++ b/components/favicon/core/fallback_icon_service.cc @@ -6,10 +6,8 @@ #include <algorithm> -#include "base/i18n/case_conversion.h" -#include "base/strings/utf_string_conversions.h" +#include "components/favicon/core/fallback_icon_client.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 "ui/gfx/canvas.h" #include "ui/gfx/codec/png_codec.h" @@ -23,21 +21,11 @@ namespace { // Arbitrary maximum icon size, can be reasonably increased if needed. 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 FallbackIconService::FallbackIconService( - const std::vector<std::string>& font_list) - : font_list_(font_list) { + FallbackIconClient* fallback_icon_client) + : fallback_icon_client_(fallback_icon_client) { } FallbackIconService::~FallbackIconService() { @@ -77,17 +65,18 @@ void FallbackIconService::DrawFallbackIcon( gfx::Rect(kOffsetX, kOffsetY, size, size), corner_radius, paint); // Draw text. - base::string16 icon_text = GetFallbackIconText(icon_url); + base::string16 icon_text = + fallback_icon_client_->GetFallbackIconText(icon_url); if (icon_text.empty()) return; int font_size = static_cast<int>(size * style.font_size_ratio); if (font_size <= 0) return; - // TODO(huangs): See how expensive gfx::FontList() is, and possibly cache. canvas->DrawStringRectWithFlags( 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, gfx::Rect(kOffsetX, kOffsetY, size, size), gfx::Canvas::TEXT_ALIGN_CENTER); diff --git a/components/favicon/core/fallback_icon_service.h b/components/favicon/core/fallback_icon_service.h index 9b3f6ba..a1b3e6f 100644 --- a/components/favicon/core/fallback_icon_service.h +++ b/components/favicon/core/fallback_icon_service.h @@ -5,11 +5,12 @@ #ifndef COMPONENTS_FAVICON_CORE_FALLBACK_ICON_SERVICE_H_ #define COMPONENTS_FAVICON_CORE_FALLBACK_ICON_SERVICE_H_ -#include <string> #include <vector> #include "base/macros.h" +#include "components/keyed_service/core/keyed_service.h" +class FallbackIconClient; class GURL; namespace gfx { @@ -21,10 +22,10 @@ struct FallbackIconStyle; } // A service to provide methods to render fallback favicons. -class FallbackIconService { +class FallbackIconService : public KeyedService { public: - explicit FallbackIconService(const std::vector<std::string>& font_list); - ~FallbackIconService(); + explicit FallbackIconService(FallbackIconClient* fallback_icon_client); + ~FallbackIconService() override; // Renders a fallback icon synchronously and returns the bitmap. Returns an // empty std::vector on failure. |size| is icon width and height in pixels. @@ -41,7 +42,7 @@ class FallbackIconService { const favicon_base::FallbackIconStyle& style, gfx::Canvas* canvas); - std::vector<std::string> font_list_; + FallbackIconClient* fallback_icon_client_; DISALLOW_COPY_AND_ASSIGN(FallbackIconService); }; |