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_macros.h" | 14 #include "base/metrics/histogram_macros.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 = nullptr; | |
| 46 #endif // OS_WIN | |
| 47 | |
| 36 } // namespace | 48 } // namespace |
| 37 | 49 |
| 38 // Preference tracking and protection is not required on platforms where other | 50 // Preference tracking and protection is not required on platforms where other |
| 39 // apps do not have access to chrome's persistent storage. | 51 // apps do not have access to chrome's persistent storage. |
| 40 const bool ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking = | 52 const bool ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking = |
| 41 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) | 53 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) |
| 42 false; | 54 false; |
| 43 #else | 55 #else |
| 44 true; | 56 true; |
| 45 #endif | 57 #endif |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 70 // static | 82 // static |
| 71 base::Time ProfilePrefStoreManager::GetResetTime(PrefService* pref_service) { | 83 base::Time ProfilePrefStoreManager::GetResetTime(PrefService* pref_service) { |
| 72 return PrefHashFilter::GetResetTime(pref_service); | 84 return PrefHashFilter::GetResetTime(pref_service); |
| 73 } | 85 } |
| 74 | 86 |
| 75 // static | 87 // static |
| 76 void ProfilePrefStoreManager::ClearResetTime(PrefService* pref_service) { | 88 void ProfilePrefStoreManager::ClearResetTime(PrefService* pref_service) { |
| 77 PrefHashFilter::ClearResetTime(pref_service); | 89 PrefHashFilter::ClearResetTime(pref_service); |
| 78 } | 90 } |
| 79 | 91 |
| 92 #if defined(OS_WIN) | |
| 93 // static | |
| 94 void ProfilePrefStoreManager::SetPreferenceValidationRegistryPathForTesting( | |
| 95 const base::string16& path) { | |
| 96 DCHECK(!path.empty()); | |
| 97 g_preference_validation_registry_path_for_testing = new base::string16(path); | |
| 98 } | |
| 99 #endif // OS_WIN | |
| 100 | |
| 80 PersistentPrefStore* ProfilePrefStoreManager::CreateProfilePrefStore( | 101 PersistentPrefStore* ProfilePrefStoreManager::CreateProfilePrefStore( |
| 81 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner, | 102 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner, |
| 82 const base::Closure& on_reset_on_load, | 103 const base::Closure& on_reset_on_load, |
| 83 TrackedPreferenceValidationDelegate* validation_delegate) { | 104 TrackedPreferenceValidationDelegate* validation_delegate) { |
| 84 std::unique_ptr<PrefFilter> pref_filter; | 105 std::unique_ptr<PrefFilter> pref_filter; |
| 85 if (!kPlatformSupportsPreferenceTracking) { | 106 if (!kPlatformSupportsPreferenceTracking) { |
| 86 return new JsonPrefStore(profile_path_.Append(chrome::kPreferencesFilename), | 107 return new JsonPrefStore(profile_path_.Append(chrome::kPreferencesFilename), |
| 87 io_task_runner.get(), | 108 io_task_runner.get(), |
| 88 std::unique_ptr<PrefFilter>()); | 109 std::unique_ptr<PrefFilter>()); |
| 89 } | 110 } |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 101 if (it->enforcement_level > PrefHashFilter::NO_ENFORCEMENT) { | 122 if (it->enforcement_level > PrefHashFilter::NO_ENFORCEMENT) { |
| 102 protected_configuration.push_back(*it); | 123 protected_configuration.push_back(*it); |
| 103 protected_pref_names.insert(it->name); | 124 protected_pref_names.insert(it->name); |
| 104 } else { | 125 } else { |
| 105 unprotected_configuration.push_back(*it); | 126 unprotected_configuration.push_back(*it); |
| 106 unprotected_pref_names.insert(it->name); | 127 unprotected_pref_names.insert(it->name); |
| 107 } | 128 } |
| 108 } | 129 } |
| 109 | 130 |
| 110 std::unique_ptr<PrefHashFilter> unprotected_pref_hash_filter( | 131 std::unique_ptr<PrefHashFilter> unprotected_pref_hash_filter( |
| 111 new PrefHashFilter(GetPrefHashStore(false), unprotected_configuration, | 132 new PrefHashFilter(GetPrefHashStore(false), |
| 112 base::Closure(), validation_delegate, | 133 GetExternalVerificationPrefHashStorePair(), |
| 113 reporting_ids_count_, false)); | 134 unprotected_configuration, base::Closure(), |
| 135 validation_delegate, reporting_ids_count_, false)); | |
| 114 std::unique_ptr<PrefHashFilter> protected_pref_hash_filter(new PrefHashFilter( | 136 std::unique_ptr<PrefHashFilter> protected_pref_hash_filter(new PrefHashFilter( |
| 115 GetPrefHashStore(true), protected_configuration, on_reset_on_load, | 137 GetPrefHashStore(true), GetExternalVerificationPrefHashStorePair(), |
| 116 validation_delegate, reporting_ids_count_, true)); | 138 protected_configuration, on_reset_on_load, validation_delegate, |
| 139 reporting_ids_count_, true)); | |
| 117 | 140 |
| 118 PrefHashFilter* raw_unprotected_pref_hash_filter = | 141 PrefHashFilter* raw_unprotected_pref_hash_filter = |
| 119 unprotected_pref_hash_filter.get(); | 142 unprotected_pref_hash_filter.get(); |
| 120 PrefHashFilter* raw_protected_pref_hash_filter = | 143 PrefHashFilter* raw_protected_pref_hash_filter = |
| 121 protected_pref_hash_filter.get(); | 144 protected_pref_hash_filter.get(); |
| 122 | 145 |
| 123 scoped_refptr<JsonPrefStore> unprotected_pref_store(new JsonPrefStore( | 146 scoped_refptr<JsonPrefStore> unprotected_pref_store(new JsonPrefStore( |
| 124 profile_path_.Append(chrome::kPreferencesFilename), io_task_runner.get(), | 147 profile_path_.Append(chrome::kPreferencesFilename), io_task_runner.get(), |
| 125 std::move(unprotected_pref_hash_filter))); | 148 std::move(unprotected_pref_hash_filter))); |
| 126 // TODO(gab): Remove kDeprecatedProtectedPreferencesFilename as an alternate | 149 // TODO(gab): Remove kDeprecatedProtectedPreferencesFilename as an alternate |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 152 if (!base::CreateDirectory(profile_path_)) | 175 if (!base::CreateDirectory(profile_path_)) |
| 153 return false; | 176 return false; |
| 154 | 177 |
| 155 const base::DictionaryValue* to_serialize = &master_prefs; | 178 const base::DictionaryValue* to_serialize = &master_prefs; |
| 156 std::unique_ptr<base::DictionaryValue> copy; | 179 std::unique_ptr<base::DictionaryValue> copy; |
| 157 | 180 |
| 158 if (kPlatformSupportsPreferenceTracking) { | 181 if (kPlatformSupportsPreferenceTracking) { |
| 159 copy.reset(master_prefs.DeepCopy()); | 182 copy.reset(master_prefs.DeepCopy()); |
| 160 to_serialize = copy.get(); | 183 to_serialize = copy.get(); |
| 161 PrefHashFilter(GetPrefHashStore(false), | 184 PrefHashFilter(GetPrefHashStore(false), |
| 162 tracking_configuration_, | 185 GetExternalVerificationPrefHashStorePair(), |
| 163 base::Closure(), | 186 tracking_configuration_, base::Closure(), NULL, |
| 164 NULL, | 187 reporting_ids_count_, false) |
| 165 reporting_ids_count_, | 188 .Initialize(copy.get()); |
| 166 false).Initialize(copy.get()); | |
| 167 } | 189 } |
| 168 | 190 |
| 169 // This will write out to a single combined file which will be immediately | 191 // This will write out to a single combined file which will be immediately |
| 170 // migrated to two files on load. | 192 // migrated to two files on load. |
| 171 JSONFileValueSerializer serializer( | 193 JSONFileValueSerializer serializer( |
| 172 profile_path_.Append(chrome::kPreferencesFilename)); | 194 profile_path_.Append(chrome::kPreferencesFilename)); |
| 173 | 195 |
| 174 // Call Serialize (which does IO) on the main thread, which would _normally_ | 196 // 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 | 197 // be verboten. In this case however, we require this IO to synchronously |
| 176 // complete before Chrome can start (as master preferences seed the Local | 198 // complete before Chrome can start (as master preferences seed the Local |
| 177 // State and Preferences files). This won't trip ThreadIORestrictions as they | 199 // State and Preferences files). This won't trip ThreadIORestrictions as they |
| 178 // won't have kicked in yet on the main thread. | 200 // won't have kicked in yet on the main thread. |
| 179 bool success = serializer.Serialize(*to_serialize); | 201 bool success = serializer.Serialize(*to_serialize); |
| 180 | 202 |
| 181 UMA_HISTOGRAM_BOOLEAN("Settings.InitializedFromMasterPrefs", success); | 203 UMA_HISTOGRAM_BOOLEAN("Settings.InitializedFromMasterPrefs", success); |
| 182 return success; | 204 return success; |
| 183 } | 205 } |
| 184 | 206 |
| 185 std::unique_ptr<PrefHashStore> ProfilePrefStoreManager::GetPrefHashStore( | 207 std::unique_ptr<PrefHashStore> ProfilePrefStoreManager::GetPrefHashStore( |
| 186 bool use_super_mac) { | 208 bool use_super_mac) { |
| 187 DCHECK(kPlatformSupportsPreferenceTracking); | 209 DCHECK(kPlatformSupportsPreferenceTracking); |
| 188 | 210 |
| 189 return std::unique_ptr<PrefHashStore>( | 211 return std::unique_ptr<PrefHashStore>( |
| 190 new PrefHashStoreImpl(seed_, device_id_, use_super_mac)); | 212 new PrefHashStoreImpl(seed_, device_id_, use_super_mac)); |
| 191 } | 213 } |
| 214 | |
| 215 std::pair<std::unique_ptr<PrefHashStore>, std::unique_ptr<HashStoreContents>>* | |
|
gab
2016/09/16 19:47:31
Returning raw pointer is almost never correct (i.e
proberge
2016/09/20 21:35:44
Done.
| |
| 216 ProfilePrefStoreManager::GetExternalVerificationPrefHashStorePair() { | |
| 217 DCHECK(kPlatformSupportsPreferenceTracking); | |
| 218 #if defined(OS_WIN) | |
| 219 return new std::pair<std::unique_ptr<PrefHashStore>, | |
| 220 std::unique_ptr<HashStoreContents>>(std::make_pair( | |
|
gab
2016/09/16 19:47:32
Once you return by value I think you can just "ret
proberge
2016/09/20 21:35:44
Done.
| |
| 221 std::unique_ptr<PrefHashStore>( | |
| 222 new PrefHashStoreImpl("ChromeRegistryHashStoreValidationSeed", | |
|
gab
2016/09/16 19:47:31
s/std::unique_ptr<Foo>(new FooImpl(...))/base::Mak
proberge
2016/09/20 21:35:44
Done.
| |
| 223 device_id_, false /* use_super_mac */)), | |
| 224 g_preference_validation_registry_path_for_testing != nullptr | |
| 225 ? std::unique_ptr<HashStoreContents>(new RegistryHashStoreContentsWin( | |
| 226 *g_preference_validation_registry_path_for_testing, | |
| 227 profile_path_.BaseName().LossyDisplayName())) | |
| 228 : std::unique_ptr<HashStoreContents>(new RegistryHashStoreContentsWin( | |
| 229 BrowserDistribution::GetDistribution()->GetRegistryPath(), | |
| 230 profile_path_.BaseName().LossyDisplayName())))); | |
| 231 #else | |
| 232 return nullptr; | |
| 233 #endif | |
| 234 } | |
| OLD | NEW |