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