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
32content::BrowserContext* BrowserContextKeyedBaseFactory::GetBrowserContextToUse(
33    content::BrowserContext* context) const {
34  DCHECK(CalledOnValidThread());
35
36#ifndef NDEBUG
37  dependency_manager_->AssertBrowserContextWasntDestroyed(context);
38#endif
39
40  // Safe default for the Incognito mode: no service.
41  if (context->IsOffTheRecord())
42    return NULL;
43
44  return context;
45}
46
47void 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.
52  //
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.
57  //
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
65  // creation time).
66  //
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.
71  DCHECK(!context->IsOffTheRecord());
72
73  std::set<content::BrowserContext*>::iterator it =
74      registered_preferences_.find(context);
75  if (it == registered_preferences_.end()) {
76    PrefService* prefs = user_prefs::UserPrefs::Get(context);
77    user_prefs::PrefRegistrySyncable* registry =
78        static_cast<user_prefs::PrefRegistrySyncable*>(
79            prefs->DeprecatedGetPrefRegistry());
80    RegisterProfilePrefs(registry);
81    registered_preferences_.insert(context);
82  }
83}
84
85bool
86BrowserContextKeyedBaseFactory::ServiceIsCreatedWithBrowserContext() const {
87  return false;
88}
89
90bool BrowserContextKeyedBaseFactory::ServiceIsNULLWhileTesting() const {
91  return false;
92}
93
94void BrowserContextKeyedBaseFactory::BrowserContextDestroyed(
95    content::BrowserContext* context) {
96  // While object destruction can be customized in ways where the object is
97  // only dereferenced, this still must run on the UI thread.
98  DCHECK(CalledOnValidThread());
99
100  registered_preferences_.erase(context);
101}
102
103bool BrowserContextKeyedBaseFactory::ArePreferencesSetOn(
104    content::BrowserContext* context) const {
105  return registered_preferences_.find(context) !=
106      registered_preferences_.end();
107}
108
109void BrowserContextKeyedBaseFactory::MarkPreferencesSetOn(
110    content::BrowserContext* context) {
111  DCHECK(!ArePreferencesSetOn(context));
112  registered_preferences_.insert(context);
113}
114