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/refcounted_browser_state_keyed_service_factory.h"
7 #include "base/logging.h"
8 #include "components/keyed_service/core/refcounted_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 RefcountedBrowserStateKeyedServiceFactory::SetTestingFactory(
14 web::BrowserState
* context
,
15 TestingFactoryFunction testing_factory
) {
16 RefcountedKeyedServiceFactory::SetTestingFactory(
18 reinterpret_cast<RefcountedKeyedServiceFactory::TestingFactoryFunction
>(
22 scoped_refptr
<RefcountedKeyedService
>
23 RefcountedBrowserStateKeyedServiceFactory::SetTestingFactoryAndUse(
24 web::BrowserState
* context
,
25 TestingFactoryFunction testing_factory
) {
26 return RefcountedKeyedServiceFactory::SetTestingFactoryAndUse(
28 reinterpret_cast<RefcountedKeyedServiceFactory::TestingFactoryFunction
>(
32 RefcountedBrowserStateKeyedServiceFactory::
33 RefcountedBrowserStateKeyedServiceFactory(
35 /*BrowserState*/DependencyManager
* manager
)
36 : RefcountedKeyedServiceFactory(name
, manager
) {
39 RefcountedBrowserStateKeyedServiceFactory::
40 ~RefcountedBrowserStateKeyedServiceFactory() {
43 scoped_refptr
<RefcountedKeyedService
>
44 RefcountedBrowserStateKeyedServiceFactory::GetServiceForBrowserState(
45 web::BrowserState
* context
,
47 return RefcountedKeyedServiceFactory::GetServiceForContext(context
, create
);
51 RefcountedBrowserStateKeyedServiceFactory::GetBrowserStateToUse(
52 web::BrowserState
* context
) const {
53 DCHECK(CalledOnValidThread());
56 AssertContextWasntDestroyed(context
);
59 // Safe default for Incognito mode: no service.
60 if (context
->IsOffTheRecord())
66 bool RefcountedBrowserStateKeyedServiceFactory::
67 ServiceIsCreatedWithBrowserState() const {
68 return KeyedServiceBaseFactory::ServiceIsCreatedWithContext();
71 bool RefcountedBrowserStateKeyedServiceFactory::ServiceIsNULLWhileTesting()
73 return KeyedServiceBaseFactory::ServiceIsNULLWhileTesting();
76 void RefcountedBrowserStateKeyedServiceFactory::BrowserStateShutdown(
77 web::BrowserState
* context
) {
78 RefcountedKeyedServiceFactory::ContextShutdown(context
);
81 void RefcountedBrowserStateKeyedServiceFactory::BrowserStateDestroyed(
82 web::BrowserState
* context
) {
83 RefcountedKeyedServiceFactory::ContextDestroyed(context
);
86 scoped_refptr
<RefcountedKeyedService
>
87 RefcountedBrowserStateKeyedServiceFactory::BuildServiceInstanceFor(
88 base::SupportsUserData
* context
) const {
89 return BuildServiceInstanceFor(static_cast<web::BrowserState
*>(context
));
92 bool RefcountedBrowserStateKeyedServiceFactory::IsOffTheRecord(
93 base::SupportsUserData
* context
) const {
94 return static_cast<web::BrowserState
*>(context
)->IsOffTheRecord();
98 base::SupportsUserData
*
99 RefcountedBrowserStateKeyedServiceFactory::GetTypedContext(
100 base::SupportsUserData
* context
) const {
102 BrowserStateContextConverter
* context_converter
=
103 BrowserStateContextConverter::GetInstance();
104 if (context_converter
) {
105 context
= context_converter
->GetBrowserStateForContext(context
);
112 base::SupportsUserData
*
113 RefcountedBrowserStateKeyedServiceFactory::GetContextForDependencyManager(
114 base::SupportsUserData
* context
) const {
116 BrowserStateContextConverter
* context_converter
=
117 BrowserStateContextConverter::GetInstance();
118 if (context_converter
) {
119 context
= context_converter
->GetBrowserContextForContext(context
);
125 #endif // defined(OS_IOS)
127 base::SupportsUserData
*
128 RefcountedBrowserStateKeyedServiceFactory::GetContextToUse(
129 base::SupportsUserData
* context
) const {
130 return GetBrowserStateToUse(static_cast<web::BrowserState
*>(context
));
133 bool RefcountedBrowserStateKeyedServiceFactory::ServiceIsCreatedWithContext()
135 return ServiceIsCreatedWithBrowserState();
138 void RefcountedBrowserStateKeyedServiceFactory::ContextShutdown(
139 base::SupportsUserData
* context
) {
140 BrowserStateShutdown(static_cast<web::BrowserState
*>(context
));
143 void RefcountedBrowserStateKeyedServiceFactory::ContextDestroyed(
144 base::SupportsUserData
* context
) {
145 BrowserStateDestroyed(static_cast<web::BrowserState
*>(context
));
148 void RefcountedBrowserStateKeyedServiceFactory::RegisterPrefs(
149 user_prefs::PrefRegistrySyncable
* registry
) {
150 RegisterBrowserStatePrefs(registry
);