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/ios/browser_state_keyed_service_factory.h"
7 #include "base/logging.h"
8 #include "components/keyed_service/core/keyed_service.h"
9 #include "components/keyed_service/ios/browser_state_context_converter.h"
10 #include "components/keyed_service/ios/browser_state_dependency_manager.h"
11 #include "ios/web/public/browser_state.h"
13 void BrowserStateKeyedServiceFactory::SetTestingFactory(
14 web::BrowserState
* context
,
15 TestingFactoryFunction testing_factory
) {
16 KeyedServiceFactory::SetTestingFactory(
17 context
, reinterpret_cast<KeyedServiceFactory::TestingFactoryFunction
>(
21 KeyedService
* BrowserStateKeyedServiceFactory::SetTestingFactoryAndUse(
22 web::BrowserState
* context
,
23 TestingFactoryFunction testing_factory
) {
24 return KeyedServiceFactory::SetTestingFactoryAndUse(
25 context
, reinterpret_cast<KeyedServiceFactory::TestingFactoryFunction
>(
29 BrowserStateKeyedServiceFactory::BrowserStateKeyedServiceFactory(
31 /*BrowserState*/DependencyManager
* manager
)
32 : KeyedServiceFactory(name
, manager
) {
35 BrowserStateKeyedServiceFactory::~BrowserStateKeyedServiceFactory() {
38 KeyedService
* BrowserStateKeyedServiceFactory::GetServiceForBrowserState(
39 web::BrowserState
* context
,
41 return KeyedServiceFactory::GetServiceForContext(context
, create
);
44 web::BrowserState
* BrowserStateKeyedServiceFactory::GetBrowserStateToUse(
45 web::BrowserState
* context
) const {
46 DCHECK(CalledOnValidThread());
49 AssertContextWasntDestroyed(context
);
52 // Safe default for Incognito mode: no service.
53 if (context
->IsOffTheRecord())
59 bool BrowserStateKeyedServiceFactory::ServiceIsCreatedWithBrowserState() const {
60 return KeyedServiceBaseFactory::ServiceIsCreatedWithContext();
63 bool BrowserStateKeyedServiceFactory::ServiceIsNULLWhileTesting() const {
64 return KeyedServiceBaseFactory::ServiceIsNULLWhileTesting();
67 void BrowserStateKeyedServiceFactory::BrowserStateShutdown(
68 web::BrowserState
* context
) {
69 KeyedServiceFactory::ContextShutdown(context
);
72 void BrowserStateKeyedServiceFactory::BrowserStateDestroyed(
73 web::BrowserState
* context
) {
74 KeyedServiceFactory::ContextDestroyed(context
);
77 scoped_ptr
<KeyedService
>
78 BrowserStateKeyedServiceFactory::BuildServiceInstanceFor(
79 base::SupportsUserData
* context
) const {
80 return BuildServiceInstanceFor(static_cast<web::BrowserState
*>(context
));
83 bool BrowserStateKeyedServiceFactory::IsOffTheRecord(
84 base::SupportsUserData
* context
) const {
85 return static_cast<web::BrowserState
*>(context
)->IsOffTheRecord();
89 base::SupportsUserData
* BrowserStateKeyedServiceFactory::GetTypedContext(
90 base::SupportsUserData
* context
) const {
92 BrowserStateContextConverter
* context_converter
=
93 BrowserStateContextConverter::GetInstance();
94 if (context_converter
) {
95 context
= context_converter
->GetBrowserStateForContext(context
);
102 base::SupportsUserData
*
103 BrowserStateKeyedServiceFactory::GetContextForDependencyManager(
104 base::SupportsUserData
* context
) const {
106 BrowserStateContextConverter
* context_converter
=
107 BrowserStateContextConverter::GetInstance();
108 if (context_converter
) {
109 context
= context_converter
->GetBrowserContextForContext(context
);
115 #endif // defined(OS_IOS)
117 base::SupportsUserData
* BrowserStateKeyedServiceFactory::GetContextToUse(
118 base::SupportsUserData
* context
) const {
119 return GetBrowserStateToUse(static_cast<web::BrowserState
*>(context
));
122 bool BrowserStateKeyedServiceFactory::ServiceIsCreatedWithContext() const {
123 return ServiceIsCreatedWithBrowserState();
126 void BrowserStateKeyedServiceFactory::ContextShutdown(
127 base::SupportsUserData
* context
) {
128 BrowserStateShutdown(static_cast<web::BrowserState
*>(context
));
131 void BrowserStateKeyedServiceFactory::ContextDestroyed(
132 base::SupportsUserData
* context
) {
133 BrowserStateDestroyed(static_cast<web::BrowserState
*>(context
));
136 void BrowserStateKeyedServiceFactory::RegisterPrefs(
137 user_prefs::PrefRegistrySyncable
* registry
) {
138 RegisterBrowserStatePrefs(registry
);