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_dependency_manager.h"
10 #include "components/keyed_service/ios/browser_state_helper.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 KeyedService
* BrowserStateKeyedServiceFactory::BuildServiceInstanceFor(
78 base::SupportsUserData
* context
) const {
79 return BuildServiceInstanceFor(BrowserStateFromContext(context
));
82 bool BrowserStateKeyedServiceFactory::IsOffTheRecord(
83 base::SupportsUserData
* context
) const {
84 return BrowserStateFromContext(context
)->IsOffTheRecord();
87 user_prefs::PrefRegistrySyncable
*
88 BrowserStateKeyedServiceFactory::GetAssociatedPrefRegistry(
89 base::SupportsUserData
* context
) const {
94 base::SupportsUserData
* BrowserStateKeyedServiceFactory::GetContextToUse(
95 base::SupportsUserData
* context
) const {
96 return GetBrowserStateToUse(BrowserStateFromContext(context
));
99 bool BrowserStateKeyedServiceFactory::ServiceIsCreatedWithContext() const {
100 return ServiceIsCreatedWithBrowserState();
103 void BrowserStateKeyedServiceFactory::ContextShutdown(
104 base::SupportsUserData
* context
) {
105 BrowserStateShutdown(BrowserStateFromContext(context
));
108 void BrowserStateKeyedServiceFactory::ContextDestroyed(
109 base::SupportsUserData
* context
) {
110 BrowserStateDestroyed(BrowserStateFromContext(context
));
113 void BrowserStateKeyedServiceFactory::RegisterPrefs(
114 user_prefs::PrefRegistrySyncable
* registry
) {
115 RegisterProfilePrefs(registry
);