1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// found in the LICENSE file.
490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "components/keyed_service/content/browser_context_keyed_base_factory.h"
690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/prefs/pref_service.h"
8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "components/keyed_service/content/browser_context_dependency_manager.h"
9cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/pref_registry/pref_registry_syncable.h"
1090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "components/user_prefs/user_prefs.h"
1190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "content/public/browser/browser_context.h"
1290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)BrowserContextKeyedBaseFactory::BrowserContextKeyedBaseFactory(
14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const char* name,
15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    BrowserContextDependencyManager* manager)
1690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    : dependency_manager_(manager)
1790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#ifndef NDEBUG
18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      ,
19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      service_name_(name)
2090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif
2190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles){
2290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  dependency_manager_->AddComponent(this);
2390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
2490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)BrowserContextKeyedBaseFactory::~BrowserContextKeyedBaseFactory() {
2690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  dependency_manager_->RemoveComponent(this);
2790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
2890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void BrowserContextKeyedBaseFactory::DependsOn(
3090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    BrowserContextKeyedBaseFactory* rhs) {
3190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  dependency_manager_->AddEdge(rhs, this);
3290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
3390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
34a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)void BrowserContextKeyedBaseFactory::RegisterProfilePrefsIfNecessaryForContext(
35a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const content::BrowserContext* context,
36a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    user_prefs::PrefRegistrySyncable* registry) {
37a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  std::set<const content::BrowserContext*>::iterator it =
38a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      registered_preferences_.find(context);
39a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (it == registered_preferences_.end()) {
40a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    RegisterProfilePrefs(registry);
41a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    registered_preferences_.insert(context);
42a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
43a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
44a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
4590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)content::BrowserContext* BrowserContextKeyedBaseFactory::GetBrowserContextToUse(
4690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    content::BrowserContext* context) const {
4790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(CalledOnValidThread());
4890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
4990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#ifndef NDEBUG
5090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  dependency_manager_->AssertBrowserContextWasntDestroyed(context);
5190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif
5290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
5390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Safe default for the Incognito mode: no service.
5490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (context->IsOffTheRecord())
5590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return NULL;
5690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
5790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return context;
5890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
5990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
60a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)void BrowserContextKeyedBaseFactory::RegisterUserPrefsOnBrowserContextForTest(
6190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    content::BrowserContext* context) {
6290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Safe timing for pref registration is hard. Previously, we made
6390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // BrowserContext responsible for all pref registration on every service
6490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // that used BrowserContext. Now we don't and there are timing issues.
6590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  //
6690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // With normal contexts, prefs can simply be registered at
67a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // BrowserContextDependencyManager::RegisterProfilePrefsForServices time.
6890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // With incognito contexts, we just never register since incognito contexts
6990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // share the same pref services with their parent contexts.
7090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  //
7190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // TestingBrowserContexts throw a wrench into the mix, in that some tests will
7290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // swap out the PrefService after we've registered user prefs on the original
7390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // PrefService. Test code that does this is responsible for either manually
747dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // invoking RegisterProfilePrefs() on the appropriate
7590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // BrowserContextKeyedServiceFactory associated with the prefs they need,
7690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // or they can use SetTestingFactory() and create a service (since service
77a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // creation with a factory method causes registration to happen at
78a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // TestingProfile creation time).
7990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  //
8090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Now that services are responsible for declaring their preferences, we have
8190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // to enforce a uniquenes check here because some tests create one context and
8290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // multiple services of the same type attached to that context (serially, not
8390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // parallel) and we don't want to register multiple times on the same context.
84a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // This is the purpose of RegisterProfilePrefsIfNecessary() which could be
85a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // replaced directly by RegisterProfilePrefs() if this method is ever phased
86a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // out.
87a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  PrefService* prefs = user_prefs::UserPrefs::Get(context);
88a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  user_prefs::PrefRegistrySyncable* registry =
89a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      static_cast<user_prefs::PrefRegistrySyncable*>(
90a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          prefs->DeprecatedGetPrefRegistry());
91a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  RegisterProfilePrefsIfNecessaryForContext(context, registry);
9290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
9390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
94a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool BrowserContextKeyedBaseFactory::ServiceIsCreatedWithBrowserContext()
95a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const {
9690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return false;
9790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
9890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
9990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool BrowserContextKeyedBaseFactory::ServiceIsNULLWhileTesting() const {
10090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return false;
10190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
10290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
10390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void BrowserContextKeyedBaseFactory::BrowserContextDestroyed(
10490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    content::BrowserContext* context) {
10590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // While object destruction can be customized in ways where the object is
10690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // only dereferenced, this still must run on the UI thread.
10790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(CalledOnValidThread());
10890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
10990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  registered_preferences_.erase(context);
11090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
11190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
11290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool BrowserContextKeyedBaseFactory::ArePreferencesSetOn(
11390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    content::BrowserContext* context) const {
114a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return registered_preferences_.find(context) != registered_preferences_.end();
11590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
11690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
11790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void BrowserContextKeyedBaseFactory::MarkPreferencesSetOn(
11890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    content::BrowserContext* context) {
11990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK(!ArePreferencesSetOn(context));
12090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  registered_preferences_.insert(context);
12190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
122