1 // Copyright (c) 2012 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/browser_context_keyed_service/browser_context_keyed_base_factory.h"
7 #include "base/prefs/pref_service.h"
8 #include "components/browser_context_keyed_service/browser_context_dependency_manager.h"
9 #include "components/user_prefs/pref_registry_syncable.h"
10 #include "components/user_prefs/user_prefs.h"
11 #include "content/public/browser/browser_context.h"
13 BrowserContextKeyedBaseFactory::BrowserContextKeyedBaseFactory(
14 const char* name
, BrowserContextDependencyManager
* manager
)
15 : dependency_manager_(manager
)
20 dependency_manager_
->AddComponent(this);
23 BrowserContextKeyedBaseFactory::~BrowserContextKeyedBaseFactory() {
24 dependency_manager_
->RemoveComponent(this);
27 void BrowserContextKeyedBaseFactory::DependsOn(
28 BrowserContextKeyedBaseFactory
* rhs
) {
29 dependency_manager_
->AddEdge(rhs
, this);
32 content::BrowserContext
* BrowserContextKeyedBaseFactory::GetBrowserContextToUse(
33 content::BrowserContext
* context
) const {
34 DCHECK(CalledOnValidThread());
37 dependency_manager_
->AssertBrowserContextWasntDestroyed(context
);
40 // Safe default for the Incognito mode: no service.
41 if (context
->IsOffTheRecord())
47 void BrowserContextKeyedBaseFactory::RegisterUserPrefsOnBrowserContext(
48 content::BrowserContext
* context
) {
49 // Safe timing for pref registration is hard. Previously, we made
50 // BrowserContext responsible for all pref registration on every service
51 // that used BrowserContext. Now we don't and there are timing issues.
53 // With normal contexts, prefs can simply be registered at
54 // BrowserContextDependencyManager::CreateBrowserContextServices time.
55 // With incognito contexts, we just never register since incognito contexts
56 // share the same pref services with their parent contexts.
58 // TestingBrowserContexts throw a wrench into the mix, in that some tests will
59 // swap out the PrefService after we've registered user prefs on the original
60 // PrefService. Test code that does this is responsible for either manually
61 // invoking RegisterProfilePrefs() on the appropriate
62 // BrowserContextKeyedServiceFactory associated with the prefs they need,
63 // or they can use SetTestingFactory() and create a service (since service
64 // creation with a factory method causes registration to happen at service
67 // Now that services are responsible for declaring their preferences, we have
68 // to enforce a uniquenes check here because some tests create one context and
69 // multiple services of the same type attached to that context (serially, not
70 // parallel) and we don't want to register multiple times on the same context.
72 std::set
<content::BrowserContext
*>::iterator it
=
73 registered_preferences_
.find(context
);
74 if (it
== registered_preferences_
.end()) {
75 PrefService
* prefs
= user_prefs::UserPrefs::Get(context
);
76 user_prefs::PrefRegistrySyncable
* registry
=
77 static_cast<user_prefs::PrefRegistrySyncable
*>(
78 prefs
->DeprecatedGetPrefRegistry());
79 RegisterProfilePrefs(registry
);
80 registered_preferences_
.insert(context
);
85 BrowserContextKeyedBaseFactory::ServiceIsCreatedWithBrowserContext() const {
89 bool BrowserContextKeyedBaseFactory::ServiceIsNULLWhileTesting() const {
93 void BrowserContextKeyedBaseFactory::BrowserContextDestroyed(
94 content::BrowserContext
* context
) {
95 // While object destruction can be customized in ways where the object is
96 // only dereferenced, this still must run on the UI thread.
97 DCHECK(CalledOnValidThread());
99 registered_preferences_
.erase(context
);
102 bool BrowserContextKeyedBaseFactory::ArePreferencesSetOn(
103 content::BrowserContext
* context
) const {
104 return registered_preferences_
.find(context
) !=
105 registered_preferences_
.end();
108 void BrowserContextKeyedBaseFactory::MarkPreferencesSetOn(
109 content::BrowserContext
* context
) {
110 DCHECK(!ArePreferencesSetOn(context
));
111 registered_preferences_
.insert(context
);