forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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}
- Loading branch information
1 parent
328a417
commit f16444b
Showing
23 changed files
with
408 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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_ |
38 changes: 38 additions & 0 deletions
38
chrome/browser/favicon/chrome_fallback_icon_client_factory.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} |
42 changes: 42 additions & 0 deletions
42
chrome/browser/favicon/chrome_fallback_icon_client_factory.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.