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.
4
5 #include "components/keyed_service/content/browser_context_keyed_service_factory.h"
6
7 #include <utility>
8
9 #include "base/bind.h"
10 #include "base/check_op.h"
11 #include "base/memory/ptr_util.h"
12 #include "components/keyed_service/content/browser_context_dependency_manager.h"
13 #include "components/keyed_service/core/keyed_service.h"
14 #include "components/pref_registry/pref_registry_syncable.h"
15 #include "content/public/browser/browser_context.h"
16
SetTestingFactory(content::BrowserContext * context,TestingFactory testing_factory)17 void BrowserContextKeyedServiceFactory::SetTestingFactory(
18 content::BrowserContext* context,
19 TestingFactory testing_factory) {
20 KeyedServiceFactory::TestingFactory wrapped_factory;
21 if (testing_factory) {
22 wrapped_factory = base::BindRepeating(
23 [](const TestingFactory& testing_factory, void* context) {
24 return testing_factory.Run(
25 static_cast<content::BrowserContext*>(context));
26 },
27 std::move(testing_factory));
28 }
29 KeyedServiceFactory::SetTestingFactory(context, std::move(wrapped_factory));
30 }
31
SetTestingFactoryAndUse(content::BrowserContext * context,TestingFactory testing_factory)32 KeyedService* BrowserContextKeyedServiceFactory::SetTestingFactoryAndUse(
33 content::BrowserContext* context,
34 TestingFactory testing_factory) {
35 DCHECK(testing_factory);
36 return KeyedServiceFactory::SetTestingFactoryAndUse(
37 context,
38 base::BindRepeating(
39 [](const TestingFactory& testing_factory, void* context) {
40 return testing_factory.Run(
41 static_cast<content::BrowserContext*>(context));
42 },
43 std::move(testing_factory)));
44 }
45
BrowserContextKeyedServiceFactory(const char * name,BrowserContextDependencyManager * manager)46 BrowserContextKeyedServiceFactory::BrowserContextKeyedServiceFactory(
47 const char* name,
48 BrowserContextDependencyManager* manager)
49 : KeyedServiceFactory(name, manager, BROWSER_CONTEXT) {}
50
~BrowserContextKeyedServiceFactory()51 BrowserContextKeyedServiceFactory::~BrowserContextKeyedServiceFactory() {
52 }
53
GetServiceForBrowserContext(content::BrowserContext * context,bool create)54 KeyedService* BrowserContextKeyedServiceFactory::GetServiceForBrowserContext(
55 content::BrowserContext* context,
56 bool create) {
57 return KeyedServiceFactory::GetServiceForContext(context, create);
58 }
59
60 content::BrowserContext*
GetBrowserContextToUse(content::BrowserContext * context) const61 BrowserContextKeyedServiceFactory::GetBrowserContextToUse(
62 content::BrowserContext* context) const {
63 // Safe default for Incognito mode: no service.
64 if (context->IsOffTheRecord())
65 return nullptr;
66
67 return context;
68 }
69
ServiceIsCreatedWithBrowserContext() const70 bool BrowserContextKeyedServiceFactory::ServiceIsCreatedWithBrowserContext()
71 const {
72 return KeyedServiceBaseFactory::ServiceIsCreatedWithContext();
73 }
74
ServiceIsNULLWhileTesting() const75 bool BrowserContextKeyedServiceFactory::ServiceIsNULLWhileTesting() const {
76 return KeyedServiceBaseFactory::ServiceIsNULLWhileTesting();
77 }
78
BrowserContextShutdown(content::BrowserContext * context)79 void BrowserContextKeyedServiceFactory::BrowserContextShutdown(
80 content::BrowserContext* context) {
81 KeyedServiceFactory::ContextShutdown(context);
82 }
83
BrowserContextDestroyed(content::BrowserContext * context)84 void BrowserContextKeyedServiceFactory::BrowserContextDestroyed(
85 content::BrowserContext* context) {
86 KeyedServiceFactory::ContextDestroyed(context);
87 }
88
89 std::unique_ptr<KeyedService>
BuildServiceInstanceFor(void * context) const90 BrowserContextKeyedServiceFactory::BuildServiceInstanceFor(
91 void* context) const {
92 // TODO(isherman): The wrapped BuildServiceInstanceFor() should return a
93 // scoped_ptr as well.
94 return base::WrapUnique(
95 BuildServiceInstanceFor(static_cast<content::BrowserContext*>(context)));
96 }
97
IsOffTheRecord(void * context) const98 bool BrowserContextKeyedServiceFactory::IsOffTheRecord(void* context) const {
99 return static_cast<content::BrowserContext*>(context)->IsOffTheRecord();
100 }
101
GetContextToUse(void * context) const102 void* BrowserContextKeyedServiceFactory::GetContextToUse(void* context) const {
103 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
104 AssertContextWasntDestroyed(context);
105 return GetBrowserContextToUse(static_cast<content::BrowserContext*>(context));
106 }
107
ServiceIsCreatedWithContext() const108 bool BrowserContextKeyedServiceFactory::ServiceIsCreatedWithContext() const {
109 return ServiceIsCreatedWithBrowserContext();
110 }
111
ContextShutdown(void * context)112 void BrowserContextKeyedServiceFactory::ContextShutdown(void* context) {
113 BrowserContextShutdown(static_cast<content::BrowserContext*>(context));
114 }
115
ContextDestroyed(void * context)116 void BrowserContextKeyedServiceFactory::ContextDestroyed(void* context) {
117 BrowserContextDestroyed(static_cast<content::BrowserContext*>(context));
118 }
119
RegisterPrefs(user_prefs::PrefRegistrySyncable * registry)120 void BrowserContextKeyedServiceFactory::RegisterPrefs(
121 user_prefs::PrefRegistrySyncable* registry) {
122 RegisterProfilePrefs(registry);
123 }
124
CreateServiceNow(void * context)125 void BrowserContextKeyedServiceFactory::CreateServiceNow(void* context) {
126 KeyedServiceFactory::GetServiceForContext(context, true);
127 }
128