OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/prefs/profile_pref_store_manager.h" | 5 #include "chrome/browser/prefs/profile_pref_store_manager.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
12 #include "base/json/json_file_value_serializer.h" | 12 #include "base/json/json_file_value_serializer.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/memory/ptr_util.h" |
14 #include "base/metrics/histogram_macros.h" | 15 #include "base/metrics/histogram_macros.h" |
15 #include "base/sequenced_task_runner.h" | 16 #include "base/sequenced_task_runner.h" |
16 #include "build/build_config.h" | 17 #include "build/build_config.h" |
17 #include "chrome/common/chrome_constants.h" | 18 #include "chrome/common/chrome_constants.h" |
18 #include "components/pref_registry/pref_registry_syncable.h" | 19 #include "components/pref_registry/pref_registry_syncable.h" |
19 #include "components/prefs/json_pref_store.h" | 20 #include "components/prefs/json_pref_store.h" |
20 #include "components/prefs/persistent_pref_store.h" | 21 #include "components/prefs/persistent_pref_store.h" |
21 #include "components/prefs/pref_registry_simple.h" | 22 #include "components/prefs/pref_registry_simple.h" |
22 #include "components/user_prefs/tracked/pref_hash_store_impl.h" | 23 #include "components/user_prefs/tracked/pref_hash_store_impl.h" |
23 #include "components/user_prefs/tracked/segregated_pref_store.h" | 24 #include "components/user_prefs/tracked/segregated_pref_store.h" |
24 #include "components/user_prefs/tracked/tracked_preferences_migration.h" | 25 #include "components/user_prefs/tracked/tracked_preferences_migration.h" |
25 | 26 |
| 27 #if defined(OS_WIN) |
| 28 #include "chrome/installer/util/browser_distribution.h" |
| 29 #include "components/user_prefs/tracked/registry_hash_store_contents_win.h" |
| 30 #endif |
| 31 |
26 namespace { | 32 namespace { |
27 | 33 |
28 void RemoveValueSilently(const base::WeakPtr<JsonPrefStore> pref_store, | 34 void RemoveValueSilently(const base::WeakPtr<JsonPrefStore> pref_store, |
29 const std::string& key) { | 35 const std::string& key) { |
30 if (pref_store) { | 36 if (pref_store) { |
31 pref_store->RemoveValueSilently( | 37 pref_store->RemoveValueSilently( |
32 key, WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 38 key, WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
33 } | 39 } |
34 } | 40 } |
35 | 41 |
| 42 #if defined(OS_WIN) |
| 43 // Forces a different registry key to be used for storing preference validation |
| 44 // MACs. See |SetPreferenceValidationRegistryPathForTesting|. |
| 45 const base::string16* g_preference_validation_registry_path_for_testing = |
| 46 nullptr; |
| 47 #endif // OS_WIN |
| 48 |
36 } // namespace | 49 } // namespace |
37 | 50 |
38 // Preference tracking and protection is not required on platforms where other | 51 // Preference tracking and protection is not required on platforms where other |
39 // apps do not have access to chrome's persistent storage. | 52 // apps do not have access to chrome's persistent storage. |
40 const bool ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking = | 53 const bool ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking = |
41 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) | 54 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) |
42 false; | 55 false; |
43 #else | 56 #else |
44 true; | 57 true; |
45 #endif | 58 #endif |
(...skipping 24 matching lines...) Expand all Loading... |
70 // static | 83 // static |
71 base::Time ProfilePrefStoreManager::GetResetTime(PrefService* pref_service) { | 84 base::Time ProfilePrefStoreManager::GetResetTime(PrefService* pref_service) { |
72 return PrefHashFilter::GetResetTime(pref_service); | 85 return PrefHashFilter::GetResetTime(pref_service); |
73 } | 86 } |
74 | 87 |
75 // static | 88 // static |
76 void ProfilePrefStoreManager::ClearResetTime(PrefService* pref_service) { | 89 void ProfilePrefStoreManager::ClearResetTime(PrefService* pref_service) { |
77 PrefHashFilter::ClearResetTime(pref_service); | 90 PrefHashFilter::ClearResetTime(pref_service); |
78 } | 91 } |
79 | 92 |
| 93 #if defined(OS_WIN) |
| 94 // static |
| 95 void ProfilePrefStoreManager::SetPreferenceValidationRegistryPathForTesting( |
| 96 const base::string16* path) { |
| 97 DCHECK(!path->empty()); |
| 98 g_preference_validation_registry_path_for_testing = path; |
| 99 } |
| 100 #endif // OS_WIN |
| 101 |
80 PersistentPrefStore* ProfilePrefStoreManager::CreateProfilePrefStore( | 102 PersistentPrefStore* ProfilePrefStoreManager::CreateProfilePrefStore( |
81 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner, | 103 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner, |
82 const base::Closure& on_reset_on_load, | 104 const base::Closure& on_reset_on_load, |
83 TrackedPreferenceValidationDelegate* validation_delegate) { | 105 TrackedPreferenceValidationDelegate* validation_delegate) { |
84 std::unique_ptr<PrefFilter> pref_filter; | 106 std::unique_ptr<PrefFilter> pref_filter; |
85 if (!kPlatformSupportsPreferenceTracking) { | 107 if (!kPlatformSupportsPreferenceTracking) { |
86 return new JsonPrefStore(profile_path_.Append(chrome::kPreferencesFilename), | 108 return new JsonPrefStore(profile_path_.Append(chrome::kPreferencesFilename), |
87 io_task_runner.get(), | 109 io_task_runner.get(), |
88 std::unique_ptr<PrefFilter>()); | 110 std::unique_ptr<PrefFilter>()); |
89 } | 111 } |
(...skipping 11 matching lines...) Expand all Loading... |
101 if (it->enforcement_level > PrefHashFilter::NO_ENFORCEMENT) { | 123 if (it->enforcement_level > PrefHashFilter::NO_ENFORCEMENT) { |
102 protected_configuration.push_back(*it); | 124 protected_configuration.push_back(*it); |
103 protected_pref_names.insert(it->name); | 125 protected_pref_names.insert(it->name); |
104 } else { | 126 } else { |
105 unprotected_configuration.push_back(*it); | 127 unprotected_configuration.push_back(*it); |
106 unprotected_pref_names.insert(it->name); | 128 unprotected_pref_names.insert(it->name); |
107 } | 129 } |
108 } | 130 } |
109 | 131 |
110 std::unique_ptr<PrefHashFilter> unprotected_pref_hash_filter( | 132 std::unique_ptr<PrefHashFilter> unprotected_pref_hash_filter( |
111 new PrefHashFilter(GetPrefHashStore(false), unprotected_configuration, | 133 new PrefHashFilter(GetPrefHashStore(false), |
112 base::Closure(), validation_delegate, | 134 GetExternalVerificationPrefHashStorePair(), |
113 reporting_ids_count_, false)); | 135 unprotected_configuration, base::Closure(), |
| 136 validation_delegate, reporting_ids_count_, false)); |
114 std::unique_ptr<PrefHashFilter> protected_pref_hash_filter(new PrefHashFilter( | 137 std::unique_ptr<PrefHashFilter> protected_pref_hash_filter(new PrefHashFilter( |
115 GetPrefHashStore(true), protected_configuration, on_reset_on_load, | 138 GetPrefHashStore(true), GetExternalVerificationPrefHashStorePair(), |
116 validation_delegate, reporting_ids_count_, true)); | 139 protected_configuration, on_reset_on_load, validation_delegate, |
| 140 reporting_ids_count_, true)); |
117 | 141 |
118 PrefHashFilter* raw_unprotected_pref_hash_filter = | 142 PrefHashFilter* raw_unprotected_pref_hash_filter = |
119 unprotected_pref_hash_filter.get(); | 143 unprotected_pref_hash_filter.get(); |
120 PrefHashFilter* raw_protected_pref_hash_filter = | 144 PrefHashFilter* raw_protected_pref_hash_filter = |
121 protected_pref_hash_filter.get(); | 145 protected_pref_hash_filter.get(); |
122 | 146 |
123 scoped_refptr<JsonPrefStore> unprotected_pref_store(new JsonPrefStore( | 147 scoped_refptr<JsonPrefStore> unprotected_pref_store(new JsonPrefStore( |
124 profile_path_.Append(chrome::kPreferencesFilename), io_task_runner.get(), | 148 profile_path_.Append(chrome::kPreferencesFilename), io_task_runner.get(), |
125 std::move(unprotected_pref_hash_filter))); | 149 std::move(unprotected_pref_hash_filter))); |
126 // TODO(gab): Remove kDeprecatedProtectedPreferencesFilename as an alternate | 150 // TODO(gab): Remove kDeprecatedProtectedPreferencesFilename as an alternate |
(...skipping 25 matching lines...) Expand all Loading... |
152 if (!base::CreateDirectory(profile_path_)) | 176 if (!base::CreateDirectory(profile_path_)) |
153 return false; | 177 return false; |
154 | 178 |
155 const base::DictionaryValue* to_serialize = &master_prefs; | 179 const base::DictionaryValue* to_serialize = &master_prefs; |
156 std::unique_ptr<base::DictionaryValue> copy; | 180 std::unique_ptr<base::DictionaryValue> copy; |
157 | 181 |
158 if (kPlatformSupportsPreferenceTracking) { | 182 if (kPlatformSupportsPreferenceTracking) { |
159 copy.reset(master_prefs.DeepCopy()); | 183 copy.reset(master_prefs.DeepCopy()); |
160 to_serialize = copy.get(); | 184 to_serialize = copy.get(); |
161 PrefHashFilter(GetPrefHashStore(false), | 185 PrefHashFilter(GetPrefHashStore(false), |
162 tracking_configuration_, | 186 GetExternalVerificationPrefHashStorePair(), |
163 base::Closure(), | 187 tracking_configuration_, base::Closure(), NULL, |
164 NULL, | 188 reporting_ids_count_, false) |
165 reporting_ids_count_, | 189 .Initialize(copy.get()); |
166 false).Initialize(copy.get()); | |
167 } | 190 } |
168 | 191 |
169 // This will write out to a single combined file which will be immediately | 192 // This will write out to a single combined file which will be immediately |
170 // migrated to two files on load. | 193 // migrated to two files on load. |
171 JSONFileValueSerializer serializer( | 194 JSONFileValueSerializer serializer( |
172 profile_path_.Append(chrome::kPreferencesFilename)); | 195 profile_path_.Append(chrome::kPreferencesFilename)); |
173 | 196 |
174 // Call Serialize (which does IO) on the main thread, which would _normally_ | 197 // Call Serialize (which does IO) on the main thread, which would _normally_ |
175 // be verboten. In this case however, we require this IO to synchronously | 198 // be verboten. In this case however, we require this IO to synchronously |
176 // complete before Chrome can start (as master preferences seed the Local | 199 // complete before Chrome can start (as master preferences seed the Local |
177 // State and Preferences files). This won't trip ThreadIORestrictions as they | 200 // State and Preferences files). This won't trip ThreadIORestrictions as they |
178 // won't have kicked in yet on the main thread. | 201 // won't have kicked in yet on the main thread. |
179 bool success = serializer.Serialize(*to_serialize); | 202 bool success = serializer.Serialize(*to_serialize); |
180 | 203 |
181 UMA_HISTOGRAM_BOOLEAN("Settings.InitializedFromMasterPrefs", success); | 204 UMA_HISTOGRAM_BOOLEAN("Settings.InitializedFromMasterPrefs", success); |
182 return success; | 205 return success; |
183 } | 206 } |
184 | 207 |
185 std::unique_ptr<PrefHashStore> ProfilePrefStoreManager::GetPrefHashStore( | 208 std::unique_ptr<PrefHashStore> ProfilePrefStoreManager::GetPrefHashStore( |
186 bool use_super_mac) { | 209 bool use_super_mac) { |
187 DCHECK(kPlatformSupportsPreferenceTracking); | 210 DCHECK(kPlatformSupportsPreferenceTracking); |
188 | 211 |
189 return std::unique_ptr<PrefHashStore>( | 212 return std::unique_ptr<PrefHashStore>( |
190 new PrefHashStoreImpl(seed_, device_id_, use_super_mac)); | 213 new PrefHashStoreImpl(seed_, device_id_, use_super_mac)); |
191 } | 214 } |
| 215 |
| 216 std::pair<std::unique_ptr<PrefHashStore>, std::unique_ptr<HashStoreContents>> |
| 217 ProfilePrefStoreManager::GetExternalVerificationPrefHashStorePair() { |
| 218 DCHECK(kPlatformSupportsPreferenceTracking); |
| 219 #if defined(OS_WIN) |
| 220 return std::make_pair( |
| 221 base::MakeUnique<PrefHashStoreImpl>( |
| 222 "ChromeRegistryHashStoreValidationSeed", device_id_, |
| 223 false /* use_super_mac */), |
| 224 g_preference_validation_registry_path_for_testing |
| 225 ? base::MakeUnique<RegistryHashStoreContentsWin>( |
| 226 *g_preference_validation_registry_path_for_testing, |
| 227 profile_path_.BaseName().LossyDisplayName()) |
| 228 : base::MakeUnique<RegistryHashStoreContentsWin>( |
| 229 BrowserDistribution::GetDistribution()->GetRegistryPath(), |
| 230 profile_path_.BaseName().LossyDisplayName())); |
| 231 #else |
| 232 return std::make_pair(nullptr, nullptr); |
| 233 #endif |
| 234 } |
OLD | NEW |