Chromium Code Reviews| 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/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
| 15 #include "base/sequenced_task_runner.h" | 15 #include "base/sequenced_task_runner.h" |
| 16 #include "build/build_config.h" | 16 #include "build/build_config.h" |
| 17 #include "chrome/common/chrome_constants.h" | 17 #include "chrome/common/chrome_constants.h" |
| 18 #include "components/pref_registry/pref_registry_syncable.h" | 18 #include "components/pref_registry/pref_registry_syncable.h" |
| 19 #include "components/prefs/json_pref_store.h" | 19 #include "components/prefs/json_pref_store.h" |
| 20 #include "components/prefs/persistent_pref_store.h" | 20 #include "components/prefs/persistent_pref_store.h" |
| 21 #include "components/prefs/pref_registry_simple.h" | 21 #include "components/prefs/pref_registry_simple.h" |
| 22 #include "components/user_prefs/tracked/pref_hash_store_impl.h" | 22 #include "components/user_prefs/tracked/pref_hash_store_impl.h" |
| 23 #include "components/user_prefs/tracked/segregated_pref_store.h" | 23 #include "components/user_prefs/tracked/segregated_pref_store.h" |
| 24 #include "components/user_prefs/tracked/tracked_preferences_migration.h" | 24 #include "components/user_prefs/tracked/tracked_preferences_migration.h" |
| 25 | 25 |
| 26 #if defined(OS_WIN) | |
| 27 #include "chrome/installer/util/browser_distribution.h" | |
| 28 #include "components/user_prefs/tracked/registry_hash_store_contents_win.h" | |
| 29 #endif | |
| 30 | |
| 26 namespace { | 31 namespace { |
| 27 | 32 |
| 28 void RemoveValueSilently(const base::WeakPtr<JsonPrefStore> pref_store, | 33 void RemoveValueSilently(const base::WeakPtr<JsonPrefStore> pref_store, |
| 29 const std::string& key) { | 34 const std::string& key) { |
| 30 if (pref_store) { | 35 if (pref_store) { |
| 31 pref_store->RemoveValueSilently( | 36 pref_store->RemoveValueSilently( |
| 32 key, WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 37 key, WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 33 } | 38 } |
| 34 } | 39 } |
| 35 | 40 |
| 41 #if defined(OS_WIN) | |
| 42 // Whether we are in testing mode; can be enabled via | |
| 43 // UseTestingPreferenceValidationRegistryPath(). Forces a different registry key | |
| 44 // to be used for storing preference validation MACs. | |
| 45 base::string16 g_preference_validation_registry_path_for_testing; | |
|
gab
2016/08/08 04:37:44
This will result in an undesired static initialize
proberge
2016/08/31 17:30:15
Done.
| |
| 46 #endif // OS_WIN | |
| 47 | |
| 36 } // namespace | 48 } // namespace |
| 37 | 49 |
| 50 namespace chrome_prefs { | |
| 51 | |
| 52 void SetPreferenceValidationRegistryPathForTesting(base::string16 path) { | |
| 53 #if defined(OS_WIN) | |
|
gab
2016/08/08 04:37:44
Also DCHECK(!path.empty()) maybe?
proberge
2016/08/31 17:30:15
Done.
| |
| 54 g_preference_validation_registry_path_for_testing = path; | |
| 55 #endif // OS_WIN | |
| 56 } | |
| 57 | |
| 58 } // namespace chrome_prefs | |
| 59 | |
| 38 // Preference tracking and protection is not required on platforms where other | 60 // Preference tracking and protection is not required on platforms where other |
| 39 // apps do not have access to chrome's persistent storage. | 61 // apps do not have access to chrome's persistent storage. |
| 40 const bool ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking = | 62 const bool ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking = |
| 41 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) | 63 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) |
| 42 false; | 64 false; |
| 43 #else | 65 #else |
| 44 true; | 66 true; |
| 45 #endif | 67 #endif |
| 46 | 68 |
| 47 ProfilePrefStoreManager::ProfilePrefStoreManager( | 69 ProfilePrefStoreManager::ProfilePrefStoreManager( |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after 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 GetExternalVerificationPrefHashStore(), |
| 113 reporting_ids_count_, false)); | 135 GetExternalVerificationPrefHashStoreContents(), |
| 136 unprotected_configuration, base::Closure(), | |
| 137 validation_delegate, reporting_ids_count_, false)); | |
| 114 std::unique_ptr<PrefHashFilter> protected_pref_hash_filter(new PrefHashFilter( | 138 std::unique_ptr<PrefHashFilter> protected_pref_hash_filter(new PrefHashFilter( |
| 115 GetPrefHashStore(true), protected_configuration, on_reset_on_load, | 139 GetPrefHashStore(true), GetExternalVerificationPrefHashStore(), |
| 116 validation_delegate, reporting_ids_count_, true)); | 140 GetExternalVerificationPrefHashStoreContents(), protected_configuration, |
| 141 on_reset_on_load, validation_delegate, reporting_ids_count_, true)); | |
| 117 | 142 |
| 118 PrefHashFilter* raw_unprotected_pref_hash_filter = | 143 PrefHashFilter* raw_unprotected_pref_hash_filter = |
| 119 unprotected_pref_hash_filter.get(); | 144 unprotected_pref_hash_filter.get(); |
| 120 PrefHashFilter* raw_protected_pref_hash_filter = | 145 PrefHashFilter* raw_protected_pref_hash_filter = |
| 121 protected_pref_hash_filter.get(); | 146 protected_pref_hash_filter.get(); |
| 122 | 147 |
| 123 scoped_refptr<JsonPrefStore> unprotected_pref_store(new JsonPrefStore( | 148 scoped_refptr<JsonPrefStore> unprotected_pref_store(new JsonPrefStore( |
| 124 profile_path_.Append(chrome::kPreferencesFilename), io_task_runner.get(), | 149 profile_path_.Append(chrome::kPreferencesFilename), io_task_runner.get(), |
| 125 std::move(unprotected_pref_hash_filter))); | 150 std::move(unprotected_pref_hash_filter))); |
| 126 // TODO(gab): Remove kDeprecatedProtectedPreferencesFilename as an alternate | 151 // TODO(gab): Remove kDeprecatedProtectedPreferencesFilename as an alternate |
| 127 // file in M40+. | 152 // file in M40+. |
| 128 scoped_refptr<JsonPrefStore> protected_pref_store(new JsonPrefStore( | 153 scoped_refptr<JsonPrefStore> protected_pref_store(new JsonPrefStore( |
| 129 profile_path_.Append(chrome::kSecurePreferencesFilename), | 154 profile_path_.Append(chrome::kSecurePreferencesFilename), |
| 130 profile_path_.Append(chrome::kProtectedPreferencesFilenameDeprecated), | 155 profile_path_.Append(chrome::kProtectedPreferencesFilenameDeprecated), |
| 131 io_task_runner.get(), std::move(protected_pref_hash_filter))); | 156 io_task_runner.get(), std::move(protected_pref_hash_filter))); |
| 132 | 157 |
| 133 SetupTrackedPreferencesMigration( | 158 SetupTrackedPreferencesMigration( |
| 134 unprotected_pref_names, protected_pref_names, | 159 unprotected_pref_names, protected_pref_names, |
| 135 base::Bind(&RemoveValueSilently, unprotected_pref_store->AsWeakPtr()), | 160 base::Bind(&RemoveValueSilently, unprotected_pref_store->AsWeakPtr()), |
| 136 base::Bind(&RemoveValueSilently, protected_pref_store->AsWeakPtr()), | 161 base::Bind(&RemoveValueSilently, protected_pref_store->AsWeakPtr()), |
| 137 base::Bind(&JsonPrefStore::RegisterOnNextSuccessfulWriteCallback, | 162 base::Bind(&JsonPrefStore::RegisterOnNextSuccessfulWriteReply, |
|
gab
2016/08/08 04:37:44
I'd like us to break down this CL as much as possi
proberge
2016/08/31 17:30:15
Acknowledged.
| |
| 138 unprotected_pref_store->AsWeakPtr()), | 163 unprotected_pref_store->AsWeakPtr()), |
| 139 base::Bind(&JsonPrefStore::RegisterOnNextSuccessfulWriteCallback, | 164 base::Bind(&JsonPrefStore::RegisterOnNextSuccessfulWriteReply, |
| 140 protected_pref_store->AsWeakPtr()), | 165 protected_pref_store->AsWeakPtr()), |
| 141 GetPrefHashStore(false), GetPrefHashStore(true), | 166 GetPrefHashStore(false), GetPrefHashStore(true), |
| 142 raw_unprotected_pref_hash_filter, raw_protected_pref_hash_filter); | 167 raw_unprotected_pref_hash_filter, raw_protected_pref_hash_filter); |
| 143 | 168 |
| 144 return new SegregatedPrefStore(unprotected_pref_store, protected_pref_store, | 169 return new SegregatedPrefStore(unprotected_pref_store, protected_pref_store, |
| 145 protected_pref_names); | 170 protected_pref_names); |
| 146 } | 171 } |
| 147 | 172 |
| 148 bool ProfilePrefStoreManager::InitializePrefsFromMasterPrefs( | 173 bool ProfilePrefStoreManager::InitializePrefsFromMasterPrefs( |
| 149 const base::DictionaryValue& master_prefs) { | 174 const base::DictionaryValue& master_prefs) { |
| 150 // Create the profile directory if it doesn't exist yet (very possible on | 175 // Create the profile directory if it doesn't exist yet (very possible on |
| 151 // first run). | 176 // first run). |
| 152 if (!base::CreateDirectory(profile_path_)) | 177 if (!base::CreateDirectory(profile_path_)) |
| 153 return false; | 178 return false; |
| 154 | 179 |
| 155 const base::DictionaryValue* to_serialize = &master_prefs; | 180 const base::DictionaryValue* to_serialize = &master_prefs; |
| 156 std::unique_ptr<base::DictionaryValue> copy; | 181 std::unique_ptr<base::DictionaryValue> copy; |
| 157 | 182 |
| 158 if (kPlatformSupportsPreferenceTracking) { | 183 if (kPlatformSupportsPreferenceTracking) { |
| 159 copy.reset(master_prefs.DeepCopy()); | 184 copy.reset(master_prefs.DeepCopy()); |
| 160 to_serialize = copy.get(); | 185 to_serialize = copy.get(); |
| 161 PrefHashFilter(GetPrefHashStore(false), | 186 PrefHashFilter( |
| 162 tracking_configuration_, | 187 GetPrefHashStore(false), GetExternalVerificationPrefHashStore(), |
| 163 base::Closure(), | 188 GetExternalVerificationPrefHashStoreContents(), tracking_configuration_, |
| 164 NULL, | 189 base::Closure(), NULL, reporting_ids_count_, false) |
| 165 reporting_ids_count_, | 190 .Initialize(copy.get()); |
| 166 false).Initialize(copy.get()); | |
| 167 } | 191 } |
| 168 | 192 |
| 169 // This will write out to a single combined file which will be immediately | 193 // This will write out to a single combined file which will be immediately |
| 170 // migrated to two files on load. | 194 // migrated to two files on load. |
| 171 JSONFileValueSerializer serializer( | 195 JSONFileValueSerializer serializer( |
| 172 profile_path_.Append(chrome::kPreferencesFilename)); | 196 profile_path_.Append(chrome::kPreferencesFilename)); |
| 173 | 197 |
| 174 // Call Serialize (which does IO) on the main thread, which would _normally_ | 198 // 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 | 199 // be verboten. In this case however, we require this IO to synchronously |
| 176 // complete before Chrome can start (as master preferences seed the Local | 200 // complete before Chrome can start (as master preferences seed the Local |
| 177 // State and Preferences files). This won't trip ThreadIORestrictions as they | 201 // State and Preferences files). This won't trip ThreadIORestrictions as they |
| 178 // won't have kicked in yet on the main thread. | 202 // won't have kicked in yet on the main thread. |
| 179 bool success = serializer.Serialize(*to_serialize); | 203 bool success = serializer.Serialize(*to_serialize); |
| 180 | 204 |
| 181 UMA_HISTOGRAM_BOOLEAN("Settings.InitializedFromMasterPrefs", success); | 205 UMA_HISTOGRAM_BOOLEAN("Settings.InitializedFromMasterPrefs", success); |
| 182 return success; | 206 return success; |
| 183 } | 207 } |
| 184 | 208 |
| 185 std::unique_ptr<PrefHashStore> ProfilePrefStoreManager::GetPrefHashStore( | 209 std::unique_ptr<PrefHashStore> ProfilePrefStoreManager::GetPrefHashStore( |
| 186 bool use_super_mac) { | 210 bool use_super_mac) { |
| 187 DCHECK(kPlatformSupportsPreferenceTracking); | 211 DCHECK(kPlatformSupportsPreferenceTracking); |
| 188 | 212 |
| 189 return std::unique_ptr<PrefHashStore>( | 213 return std::unique_ptr<PrefHashStore>( |
| 190 new PrefHashStoreImpl(seed_, device_id_, use_super_mac)); | 214 new PrefHashStoreImpl(seed_, device_id_, use_super_mac)); |
| 191 } | 215 } |
| 216 | |
| 217 std::unique_ptr<PrefHashStore> | |
| 218 ProfilePrefStoreManager::GetExternalVerificationPrefHashStore() { | |
| 219 DCHECK(kPlatformSupportsPreferenceTracking); | |
| 220 #if defined(OS_WIN) | |
| 221 return std::unique_ptr<PrefHashStore>( | |
| 222 new PrefHashStoreImpl("ChromiumRegistryHashStoreValidationSeed", | |
|
gab
2016/08/08 04:37:44
Use Chrome instead of Chromium I'd say (this is th
proberge
2016/08/31 17:30:15
Done.
| |
| 223 device_id_, false /* use_super_mac */)); | |
| 224 #else | |
| 225 return nullptr; | |
| 226 #endif | |
| 227 } | |
| 228 | |
| 229 std::unique_ptr<HashStoreContents> | |
| 230 ProfilePrefStoreManager::GetExternalVerificationPrefHashStoreContents() { | |
| 231 DCHECK(kPlatformSupportsPreferenceTracking); | |
| 232 #if defined(OS_WIN) | |
| 233 if (g_preference_validation_registry_path_for_testing.size() > 0) { | |
|
gab
2016/08/08 04:37:44
!empty() instead of size() > 0
(but actually now
proberge
2016/08/31 17:30:15
Done.
| |
| 234 return std::unique_ptr<HashStoreContents>(new RegistryHashStoreContentsWin( | |
| 235 g_preference_validation_registry_path_for_testing, | |
| 236 profile_path_.BaseName().LossyDisplayName())); | |
| 237 } | |
| 238 | |
| 239 return std::unique_ptr<HashStoreContents>(new RegistryHashStoreContentsWin( | |
| 240 BrowserDistribution::GetDistribution()->GetRegistryPath(), | |
| 241 profile_path_.BaseName().LossyDisplayName())); | |
| 242 #else | |
| 243 return nullptr; | |
| 244 #endif | |
| 245 } | |
| OLD | NEW |