1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/keyed_service/content/browser_context_keyed_service_factory.h"
7 #include "base/logging.h"
8 #include "base/prefs/pref_service.h"
9 #include "components/keyed_service/content/browser_context_dependency_manager.h"
10 #include "components/keyed_service/core/keyed_service.h"
11 #include "components/pref_registry/pref_registry_syncable.h"
12 #include "components/user_prefs/user_prefs.h"
13 #include "content/public/browser/browser_context.h"
15 void BrowserContextKeyedServiceFactory::SetTestingFactory(
16 content::BrowserContext
* context
,
17 TestingFactoryFunction testing_factory
) {
18 KeyedServiceFactory::SetTestingFactory(
20 reinterpret_cast<KeyedServiceFactory::TestingFactoryFunction
>(
24 KeyedService
* BrowserContextKeyedServiceFactory::SetTestingFactoryAndUse(
25 content::BrowserContext
* context
,
26 TestingFactoryFunction testing_factory
) {
27 return KeyedServiceFactory::SetTestingFactoryAndUse(
29 reinterpret_cast<KeyedServiceFactory::TestingFactoryFunction
>(
33 BrowserContextKeyedServiceFactory::BrowserContextKeyedServiceFactory(
35 BrowserContextDependencyManager
* manager
)
36 : KeyedServiceFactory(name
, manager
) {
39 BrowserContextKeyedServiceFactory::~BrowserContextKeyedServiceFactory() {
42 KeyedService
* BrowserContextKeyedServiceFactory::GetServiceForBrowserContext(
43 content::BrowserContext
* context
,
45 return KeyedServiceFactory::GetServiceForContext(context
, create
);
48 content::BrowserContext
*
49 BrowserContextKeyedServiceFactory::GetBrowserContextToUse(
50 content::BrowserContext
* context
) const {
51 DCHECK(CalledOnValidThread());
54 AssertContextWasntDestroyed(context
);
57 // Safe default for Incognito mode: no service.
58 if (context
->IsOffTheRecord())
65 BrowserContextKeyedServiceFactory::RegisterUserPrefsOnBrowserContextForTest(
66 content::BrowserContext
* context
) {
67 KeyedServiceBaseFactory::RegisterUserPrefsOnContextForTest(context
);
70 bool BrowserContextKeyedServiceFactory::ServiceIsCreatedWithBrowserContext()
72 return KeyedServiceBaseFactory::ServiceIsCreatedWithContext();
75 bool BrowserContextKeyedServiceFactory::ServiceIsNULLWhileTesting() const {
76 return KeyedServiceBaseFactory::ServiceIsNULLWhileTesting();
79 void BrowserContextKeyedServiceFactory::BrowserContextShutdown(
80 content::BrowserContext
* context
) {
81 KeyedServiceFactory::ContextShutdown(context
);
84 void BrowserContextKeyedServiceFactory::BrowserContextDestroyed(
85 content::BrowserContext
* context
) {
86 KeyedServiceFactory::ContextDestroyed(context
);
89 KeyedService
* BrowserContextKeyedServiceFactory::BuildServiceInstanceFor(
90 base::SupportsUserData
* context
) const {
91 return BuildServiceInstanceFor(
92 static_cast<content::BrowserContext
*>(context
));
95 bool BrowserContextKeyedServiceFactory::IsOffTheRecord(
96 base::SupportsUserData
* context
) const {
97 return static_cast<content::BrowserContext
*>(context
)->IsOffTheRecord();
100 user_prefs::PrefRegistrySyncable
*
101 BrowserContextKeyedServiceFactory::GetAssociatedPrefRegistry(
102 base::SupportsUserData
* context
) const {
103 PrefService
* prefs
= user_prefs::UserPrefs::Get(
104 static_cast<content::BrowserContext
*>(context
));
105 user_prefs::PrefRegistrySyncable
* registry
=
106 static_cast<user_prefs::PrefRegistrySyncable
*>(
107 prefs
->DeprecatedGetPrefRegistry());
111 base::SupportsUserData
* BrowserContextKeyedServiceFactory::GetContextToUse(
112 base::SupportsUserData
* context
) const {
113 return GetBrowserContextToUse(static_cast<content::BrowserContext
*>(context
));
116 bool BrowserContextKeyedServiceFactory::ServiceIsCreatedWithContext() const {
117 return ServiceIsCreatedWithBrowserContext();
120 void BrowserContextKeyedServiceFactory::ContextShutdown(
121 base::SupportsUserData
* context
) {
122 BrowserContextShutdown(static_cast<content::BrowserContext
*>(context
));
125 void BrowserContextKeyedServiceFactory::ContextDestroyed(
126 base::SupportsUserData
* context
) {
127 BrowserContextDestroyed(static_cast<content::BrowserContext
*>(context
));
130 void BrowserContextKeyedServiceFactory::RegisterPrefs(
131 user_prefs::PrefRegistrySyncable
* registry
) {
132 RegisterProfilePrefs(registry
);