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 "chrome/browser/profiles/profile_keyed_base_factory.h" | 5 #include "chrome/browser/profiles/profile_keyed_base_factory.h" |
6 | 6 |
7 #include "base/prefs/pref_service.h" | 7 #include "base/prefs/pref_service.h" |
8 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
9 #include "chrome/browser/profiles/profile_dependency_manager.h" | 9 #include "chrome/browser/profiles/profile_dependency_manager.h" |
10 #include "components/user_prefs/pref_registry_syncable.h" | 10 #include "components/user_prefs/pref_registry_syncable.h" |
11 #include "components/user_prefs/user_prefs.h" | |
11 | 12 |
12 ProfileKeyedBaseFactory::ProfileKeyedBaseFactory( | 13 ProfileKeyedBaseFactory::ProfileKeyedBaseFactory( |
13 const char* name, ProfileDependencyManager* manager) | 14 const char* name, ProfileDependencyManager* manager) |
14 : dependency_manager_(manager) | 15 : dependency_manager_(manager) |
15 #ifndef NDEBUG | 16 #ifndef NDEBUG |
16 , service_name_(name) | 17 , service_name_(name) |
17 #endif | 18 #endif |
18 { | 19 { |
19 dependency_manager_->AddComponent(this); | 20 dependency_manager_->AddComponent(this); |
20 } | 21 } |
21 | 22 |
22 ProfileKeyedBaseFactory::~ProfileKeyedBaseFactory() { | 23 ProfileKeyedBaseFactory::~ProfileKeyedBaseFactory() { |
23 dependency_manager_->RemoveComponent(this); | 24 dependency_manager_->RemoveComponent(this); |
24 } | 25 } |
25 | 26 |
26 void ProfileKeyedBaseFactory::DependsOn(ProfileKeyedBaseFactory* rhs) { | 27 void ProfileKeyedBaseFactory::DependsOn(ProfileKeyedBaseFactory* rhs) { |
27 dependency_manager_->AddEdge(rhs, this); | 28 dependency_manager_->AddEdge(rhs, this); |
28 } | 29 } |
29 | 30 |
30 Profile* ProfileKeyedBaseFactory::GetProfileToUse(Profile* profile) { | 31 Profile* ProfileKeyedBaseFactory::GetProfileToUse(Profile* profile) { |
31 DCHECK(CalledOnValidThread()); | 32 DCHECK(CalledOnValidThread()); |
32 | 33 |
33 #ifndef NDEBUG | 34 #ifndef NDEBUG |
34 dependency_manager_->AssertProfileWasntDestroyed(profile); | 35 dependency_manager_->AssertProfileWasntDestroyed(profile); |
35 #endif | 36 #endif |
36 | 37 |
37 // Possibly handle Incognito mode. | 38 // Possibly handle Incognito mode. |
38 if (profile->IsOffTheRecord()) { | 39 if (profile->IsOffTheRecord()) { |
39 if (ServiceRedirectedInIncognito()) { | 40 if (ServiceRedirectedInIncognito()) { |
40 profile = profile->GetOriginalProfile(); | 41 profile = profile->GetOriginalProfile(); |
Paweł Hajdan Jr.
2013/04/18 18:33:00
There is no GetOriginalProfile or equivalent in Br
Jói
2013/04/19 10:39:26
Perhaps the ProfileDependencyManager should receiv
| |
41 | 42 |
42 #ifndef NDEBUG | 43 #ifndef NDEBUG |
43 dependency_manager_->AssertProfileWasntDestroyed(profile); | 44 dependency_manager_->AssertProfileWasntDestroyed(profile); |
44 #endif | 45 #endif |
45 } else if (ServiceHasOwnInstanceInIncognito()) { | 46 } else if (ServiceHasOwnInstanceInIncognito()) { |
46 // No-op; the pointers are already set correctly. | 47 // No-op; the pointers are already set correctly. |
47 } else { | 48 } else { |
48 return NULL; | 49 return NULL; |
49 } | 50 } |
50 } | 51 } |
51 | 52 |
52 return profile; | 53 return profile; |
53 } | 54 } |
54 | 55 |
55 void ProfileKeyedBaseFactory::RegisterUserPrefsOnProfile(Profile* profile) { | 56 void ProfileKeyedBaseFactory::RegisterUserPrefsOnProfile(content::BrowserContext * profile) { |
56 // Safe timing for pref registration is hard. Previously, we made Profile | 57 // Safe timing for pref registration is hard. Previously, we made Profile |
57 // responsible for all pref registration on every service that used | 58 // responsible for all pref registration on every service that used |
58 // Profile. Now we don't and there are timing issues. | 59 // Profile. Now we don't and there are timing issues. |
59 // | 60 // |
60 // With normal profiles, prefs can simply be registered at | 61 // With normal profiles, prefs can simply be registered at |
61 // ProfileDependencyManager::CreateProfileServices time. With incognito | 62 // ProfileDependencyManager::CreateProfileServices time. With incognito |
62 // profiles, we just never register since incognito profiles share the same | 63 // profiles, we just never register since incognito profiles share the same |
63 // pref services with their parent profiles. | 64 // pref services with their parent profiles. |
64 // | 65 // |
65 // TestingProfiles throw a wrench into the mix, in that some tests will | 66 // TestingProfiles throw a wrench into the mix, in that some tests will |
66 // swap out the PrefService after we've registered user prefs on the original | 67 // swap out the PrefService after we've registered user prefs on the original |
67 // PrefService. Test code that does this is responsible for either manually | 68 // PrefService. Test code that does this is responsible for either manually |
68 // invoking RegisterUserPrefs() on the appropriate ProfileKeyedServiceFactory | 69 // invoking RegisterUserPrefs() on the appropriate ProfileKeyedServiceFactory |
69 // associated with the prefs they need, or they can use SetTestingFactory() | 70 // associated with the prefs they need, or they can use SetTestingFactory() |
70 // and create a service (since service creation with a factory method causes | 71 // and create a service (since service creation with a factory method causes |
71 // registration to happen at service creation time). | 72 // registration to happen at service creation time). |
72 // | 73 // |
73 // Now that services are responsible for declaring their preferences, we have | 74 // Now that services are responsible for declaring their preferences, we have |
74 // to enforce a uniquenes check here because some tests create one profile and | 75 // to enforce a uniquenes check here because some tests create one profile and |
75 // multiple services of the same type attached to that profile (serially, not | 76 // multiple services of the same type attached to that profile (serially, not |
76 // parallel) and we don't want to register multiple times on the same profile. | 77 // parallel) and we don't want to register multiple times on the same profile. |
77 DCHECK(!profile->IsOffTheRecord()); | 78 DCHECK(!profile->IsOffTheRecord()); |
78 | 79 |
79 std::set<Profile*>::iterator it = registered_preferences_.find(profile); | 80 std::set<content::BrowserContext*>::iterator it = registered_preferences_.find (profile); |
80 if (it == registered_preferences_.end()) { | 81 if (it == registered_preferences_.end()) { |
81 PrefService* prefs = profile->GetPrefs(); | 82 PrefService* prefs = components::UserPrefs::Get(profile); |
82 PrefRegistrySyncable* registry = static_cast<PrefRegistrySyncable*>( | 83 PrefRegistrySyncable* registry = static_cast<PrefRegistrySyncable*>( |
83 prefs->DeprecatedGetPrefRegistry()); | 84 prefs->DeprecatedGetPrefRegistry()); |
84 RegisterUserPrefs(registry); | 85 RegisterUserPrefs(registry); |
85 registered_preferences_.insert(profile); | 86 registered_preferences_.insert(profile); |
86 } | 87 } |
87 } | 88 } |
88 | 89 |
89 bool ProfileKeyedBaseFactory::ServiceRedirectedInIncognito() const { | 90 bool ProfileKeyedBaseFactory::ServiceRedirectedInIncognito() const { |
90 return false; | 91 return false; |
91 } | 92 } |
92 | 93 |
93 bool ProfileKeyedBaseFactory::ServiceHasOwnInstanceInIncognito() const { | 94 bool ProfileKeyedBaseFactory::ServiceHasOwnInstanceInIncognito() const { |
94 return false; | 95 return false; |
95 } | 96 } |
96 | 97 |
97 bool ProfileKeyedBaseFactory::ServiceIsCreatedWithProfile() const { | 98 bool ProfileKeyedBaseFactory::ServiceIsCreatedWithProfile() const { |
98 return false; | 99 return false; |
99 } | 100 } |
100 | 101 |
101 bool ProfileKeyedBaseFactory::ServiceIsNULLWhileTesting() const { | 102 bool ProfileKeyedBaseFactory::ServiceIsNULLWhileTesting() const { |
102 return false; | 103 return false; |
103 } | 104 } |
104 | 105 |
105 void ProfileKeyedBaseFactory::ProfileDestroyed(Profile* profile) { | 106 void ProfileKeyedBaseFactory::ProfileDestroyed(content::BrowserContext* profile) { |
106 // While object destruction can be customized in ways where the object is | 107 // While object destruction can be customized in ways where the object is |
107 // only dereferenced, this still must run on the UI thread. | 108 // only dereferenced, this still must run on the UI thread. |
108 DCHECK(CalledOnValidThread()); | 109 DCHECK(CalledOnValidThread()); |
109 | 110 |
110 registered_preferences_.erase(profile); | 111 registered_preferences_.erase(profile); |
111 } | 112 } |
112 | 113 |
113 bool ProfileKeyedBaseFactory::ArePreferencesSetOn(Profile* profile) const { | 114 bool ProfileKeyedBaseFactory::ArePreferencesSetOn(content::BrowserContext* profi le) const { |
114 return registered_preferences_.find(profile) != | 115 return registered_preferences_.find(profile) != |
115 registered_preferences_.end(); | 116 registered_preferences_.end(); |
116 } | 117 } |
117 | 118 |
118 void ProfileKeyedBaseFactory::MarkPreferencesSetOn(Profile* profile) { | 119 void ProfileKeyedBaseFactory::MarkPreferencesSetOn(content::BrowserContext* prof ile) { |
119 DCHECK(!ArePreferencesSetOn(profile)); | 120 DCHECK(!ArePreferencesSetOn(profile)); |
120 registered_preferences_.insert(profile); | 121 registered_preferences_.insert(profile); |
121 } | 122 } |
OLD | NEW |