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