browser_context_keyed_base_factory.cc revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
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.
4
5#include "components/browser_context_keyed_service/browser_context_keyed_base_factory.h"
6
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"
12
13BrowserContextKeyedBaseFactory::BrowserContextKeyedBaseFactory(
14    const char* name, BrowserContextDependencyManager* manager)
15    : dependency_manager_(manager)
16#ifndef NDEBUG
17    , service_name_(name)
18#endif
19{
20  dependency_manager_->AddComponent(this);
21}
22
23BrowserContextKeyedBaseFactory::~BrowserContextKeyedBaseFactory() {
24  dependency_manager_->RemoveComponent(this);
25}
26
27void BrowserContextKeyedBaseFactory::DependsOn(
28    BrowserContextKeyedBaseFactory* rhs) {
29  dependency_manager_->AddEdge(rhs, this);
30}
31
32void BrowserContextKeyedBaseFactory::RegisterProfilePrefsIfNecessaryForContext(
33    const content::BrowserContext* context,
34    user_prefs::PrefRegistrySyncable* registry) {
35  std::set<const content::BrowserContext*>::iterator it =
36      registered_preferences_.find(context);
37  if (it == registered_preferences_.end()) {
38    RegisterProfilePrefs(registry);
39    registered_preferences_.insert(context);
40  }
41}
42
43content::BrowserContext* BrowserContextKeyedBaseFactory::GetBrowserContextToUse(
44    content::BrowserContext* context) const {
45  DCHECK(CalledOnValidThread());
46
47#ifndef NDEBUG
48  dependency_manager_->AssertBrowserContextWasntDestroyed(context);
49#endif
50
51  // Safe default for the Incognito mode: no service.
52  if (context->IsOffTheRecord())
53    return NULL;
54
55  return context;
56}
57
58void BrowserContextKeyedBaseFactory::RegisterUserPrefsOnBrowserContextForTest(
59    content::BrowserContext* context) {
60  // Safe timing for pref registration is hard. Previously, we made
61  // BrowserContext responsible for all pref registration on every service
62  // that used BrowserContext. Now we don't and there are timing issues.
63  //
64  // With normal contexts, prefs can simply be registered at
65  // BrowserContextDependencyManager::RegisterProfilePrefsForServices time.
66  // With incognito contexts, we just never register since incognito contexts
67  // share the same pref services with their parent contexts.
68  //
69  // TestingBrowserContexts throw a wrench into the mix, in that some tests will
70  // swap out the PrefService after we've registered user prefs on the original
71  // PrefService. Test code that does this is responsible for either manually
72  // invoking RegisterProfilePrefs() on the appropriate
73  // BrowserContextKeyedServiceFactory associated with the prefs they need,
74  // or they can use SetTestingFactory() and create a service (since service
75  // creation with a factory method causes registration to happen at
76  // TestingProfile creation time).
77  //
78  // Now that services are responsible for declaring their preferences, we have
79  // to enforce a uniquenes check here because some tests create one context and
80  // multiple services of the same type attached to that context (serially, not
81  // parallel) and we don't want to register multiple times on the same context.
82  // This is the purpose of RegisterProfilePrefsIfNecessary() which could be
83  // replaced directly by RegisterProfilePrefs() if this method is ever phased
84  // out.
85  PrefService* prefs = user_prefs::UserPrefs::Get(context);
86  user_prefs::PrefRegistrySyncable* registry =
87      static_cast<user_prefs::PrefRegistrySyncable*>(
88          prefs->DeprecatedGetPrefRegistry());
89  RegisterProfilePrefsIfNecessaryForContext(context, registry);
90}
91
92bool
93BrowserContextKeyedBaseFactory::ServiceIsCreatedWithBrowserContext() const {
94  return false;
95}
96
97bool BrowserContextKeyedBaseFactory::ServiceIsNULLWhileTesting() const {
98  return false;
99}
100
101void BrowserContextKeyedBaseFactory::BrowserContextDestroyed(
102    content::BrowserContext* context) {
103  // While object destruction can be customized in ways where the object is
104  // only dereferenced, this still must run on the UI thread.
105  DCHECK(CalledOnValidThread());
106
107  registered_preferences_.erase(context);
108}
109
110bool BrowserContextKeyedBaseFactory::ArePreferencesSetOn(
111    content::BrowserContext* context) const {
112  return registered_preferences_.find(context) !=
113      registered_preferences_.end();
114}
115
116void BrowserContextKeyedBaseFactory::MarkPreferencesSetOn(
117    content::BrowserContext* context) {
118  DCHECK(!ArePreferencesSetOn(context));
119  registered_preferences_.insert(context);
120}
121