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/memory/ptr_util.h" |
15 #include "base/metrics/histogram_macros.h" | 15 #include "base/metrics/histogram_macros.h" |
16 #include "base/sequenced_task_runner.h" | 16 #include "base/sequenced_task_runner.h" |
17 #include "build/build_config.h" | 17 #include "build/build_config.h" |
18 #include "chrome/common/chrome_constants.h" | 18 #include "chrome/common/chrome_constants.h" |
19 #include "components/pref_registry/pref_registry_syncable.h" | 19 #include "components/pref_registry/pref_registry_syncable.h" |
20 #include "components/prefs/json_pref_store.h" | 20 #include "components/prefs/json_pref_store.h" |
21 #include "components/prefs/persistent_pref_store.h" | 21 #include "components/prefs/persistent_pref_store.h" |
22 #include "components/prefs/pref_registry_simple.h" | 22 #include "components/prefs/pref_registry_simple.h" |
23 #include "components/user_prefs/tracked/pref_hash_store_impl.h" | 23 #include "components/user_prefs/tracked/pref_hash_store_impl.h" |
24 #include "components/user_prefs/tracked/segregated_pref_store.h" | 24 #include "components/user_prefs/tracked/segregated_pref_store.h" |
25 #include "components/user_prefs/tracked/tracked_preferences_migration.h" | 25 #include "components/user_prefs/tracked/tracked_preferences_migration.h" |
26 #include "services/preferences/public/cpp/tracked_persistent_pref_store_factory. h" | |
27 #include "services/preferences/public/interfaces/preferences_configuration.mojom .h" | |
26 | 28 |
27 #if defined(OS_WIN) | 29 #if defined(OS_WIN) |
28 #include "chrome/install_static/install_util.h" | 30 #include "chrome/install_static/install_util.h" |
29 #include "components/user_prefs/tracked/registry_hash_store_contents_win.h" | 31 #include "components/user_prefs/tracked/registry_hash_store_contents_win.h" |
30 #endif | 32 #endif |
31 | 33 |
32 namespace { | 34 namespace { |
33 | 35 |
34 void RemoveValueSilently(const base::WeakPtr<JsonPrefStore> pref_store, | |
35 const std::string& key) { | |
36 if (pref_store) { | |
37 pref_store->RemoveValueSilently( | |
38 key, WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | |
39 } | |
40 } | |
41 | |
42 #if defined(OS_WIN) | 36 #if defined(OS_WIN) |
43 // Forces a different registry key to be used for storing preference validation | 37 // Forces a different registry key to be used for storing preference validation |
44 // MACs. See |SetPreferenceValidationRegistryPathForTesting|. | 38 // MACs. See |SetPreferenceValidationRegistryPathForTesting|. |
45 const base::string16* g_preference_validation_registry_path_for_testing = | 39 const base::string16* g_preference_validation_registry_path_for_testing = |
46 nullptr; | 40 nullptr; |
47 #endif // OS_WIN | 41 #endif // OS_WIN |
48 | 42 |
49 } // namespace | 43 } // namespace |
50 | 44 |
51 // Preference tracking and protection is not required on platforms where other | 45 // Preference tracking and protection is not required on platforms where other |
(...skipping 11 matching lines...) Expand all Loading... | |
63 tracking_configuration, | 57 tracking_configuration, |
64 size_t reporting_ids_count, | 58 size_t reporting_ids_count, |
65 const std::string& seed, | 59 const std::string& seed, |
66 const std::string& legacy_device_id, | 60 const std::string& legacy_device_id, |
67 PrefService* local_state) | 61 PrefService* local_state) |
68 : profile_path_(profile_path), | 62 : profile_path_(profile_path), |
69 tracking_configuration_(tracking_configuration), | 63 tracking_configuration_(tracking_configuration), |
70 reporting_ids_count_(reporting_ids_count), | 64 reporting_ids_count_(reporting_ids_count), |
71 seed_(seed), | 65 seed_(seed), |
72 legacy_device_id_(legacy_device_id), | 66 legacy_device_id_(legacy_device_id), |
73 local_state_(local_state) {} | 67 local_state_(local_state) {} |
gab
2017/03/15 15:09:58
Also just realized this param is no longer used.
Sam McNally
2017/03/16 07:13:48
Done.
| |
74 | 68 |
75 ProfilePrefStoreManager::~ProfilePrefStoreManager() {} | 69 ProfilePrefStoreManager::~ProfilePrefStoreManager() {} |
76 | 70 |
77 // static | 71 // static |
78 void ProfilePrefStoreManager::RegisterProfilePrefs( | 72 void ProfilePrefStoreManager::RegisterProfilePrefs( |
79 user_prefs::PrefRegistrySyncable* registry) { | 73 user_prefs::PrefRegistrySyncable* registry) { |
80 PrefHashFilter::RegisterProfilePrefs(registry); | 74 PrefHashFilter::RegisterProfilePrefs(registry); |
81 } | 75 } |
82 | 76 |
83 // static | 77 // static |
84 base::Time ProfilePrefStoreManager::GetResetTime(PrefService* pref_service) { | 78 base::Time ProfilePrefStoreManager::GetResetTime(PrefService* pref_service) { |
85 return PrefHashFilter::GetResetTime(pref_service); | 79 return PrefHashFilter::GetResetTime(pref_service); |
86 } | 80 } |
87 | 81 |
88 // static | 82 // static |
89 void ProfilePrefStoreManager::ClearResetTime(PrefService* pref_service) { | 83 void ProfilePrefStoreManager::ClearResetTime(PrefService* pref_service) { |
90 PrefHashFilter::ClearResetTime(pref_service); | 84 PrefHashFilter::ClearResetTime(pref_service); |
91 } | 85 } |
92 | 86 |
93 #if defined(OS_WIN) | 87 #if defined(OS_WIN) |
94 // static | 88 // static |
95 void ProfilePrefStoreManager::SetPreferenceValidationRegistryPathForTesting( | 89 void ProfilePrefStoreManager::SetPreferenceValidationRegistryPathForTesting( |
96 const base::string16* path) { | 90 const base::string16* path) { |
97 DCHECK(!path->empty()); | 91 DCHECK(!path->empty()); |
98 g_preference_validation_registry_path_for_testing = path; | 92 g_preference_validation_registry_path_for_testing = path; |
99 } | 93 } |
100 #endif // OS_WIN | 94 #endif // OS_WIN |
101 | 95 |
102 PersistentPrefStore* ProfilePrefStoreManager::CreateProfilePrefStore( | 96 PersistentPrefStore* ProfilePrefStoreManager::CreateProfilePrefStore( |
103 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner, | 97 scoped_refptr<base::SequencedTaskRunner> io_task_runner, |
104 const base::Closure& on_reset_on_load, | 98 const base::Closure& on_reset_on_load, |
105 prefs::mojom::TrackedPreferenceValidationDelegate* validation_delegate) { | 99 prefs::mojom::TrackedPreferenceValidationDelegate* validation_delegate) { |
106 std::unique_ptr<PrefFilter> pref_filter; | |
107 if (!kPlatformSupportsPreferenceTracking) { | 100 if (!kPlatformSupportsPreferenceTracking) { |
108 return new JsonPrefStore(profile_path_.Append(chrome::kPreferencesFilename), | 101 return new JsonPrefStore(profile_path_.Append(chrome::kPreferencesFilename), |
109 io_task_runner.get(), | 102 io_task_runner, std::unique_ptr<PrefFilter>()); |
gab
2017/03/15 15:09:58
std::move here as well (since returning reference
Sam McNally
2017/03/16 07:13:48
Done.
| |
110 std::unique_ptr<PrefFilter>()); | |
111 } | 103 } |
112 | 104 return prefs::CreateTrackedPersistentPrefStore( |
113 std::vector<PrefHashFilter::TrackedPreferenceMetadata> | 105 std::move(io_task_runner), |
114 unprotected_configuration; | 106 profile_path_.Append(chrome::kPreferencesFilename), |
115 std::vector<PrefHashFilter::TrackedPreferenceMetadata> | |
116 protected_configuration; | |
117 std::set<std::string> protected_pref_names; | |
118 std::set<std::string> unprotected_pref_names; | |
119 for (std::vector<PrefHashFilter::TrackedPreferenceMetadata>::const_iterator | |
120 it = tracking_configuration_.begin(); | |
121 it != tracking_configuration_.end(); | |
122 ++it) { | |
123 if (it->enforcement_level > | |
124 PrefHashFilter::EnforcementLevel::NO_ENFORCEMENT) { | |
125 protected_configuration.push_back(*it); | |
126 protected_pref_names.insert(it->name); | |
127 } else { | |
128 unprotected_configuration.push_back(*it); | |
129 unprotected_pref_names.insert(it->name); | |
130 } | |
131 } | |
132 | |
133 std::unique_ptr<PrefHashFilter> unprotected_pref_hash_filter( | |
134 new PrefHashFilter(GetPrefHashStore(false), | |
135 GetExternalVerificationPrefHashStorePair(), | |
136 unprotected_configuration, base::Closure(), | |
137 validation_delegate, reporting_ids_count_, false)); | |
138 std::unique_ptr<PrefHashFilter> protected_pref_hash_filter(new PrefHashFilter( | |
139 GetPrefHashStore(true), GetExternalVerificationPrefHashStorePair(), | |
140 protected_configuration, on_reset_on_load, validation_delegate, | |
141 reporting_ids_count_, true)); | |
142 | |
143 PrefHashFilter* raw_unprotected_pref_hash_filter = | |
144 unprotected_pref_hash_filter.get(); | |
145 PrefHashFilter* raw_protected_pref_hash_filter = | |
146 protected_pref_hash_filter.get(); | |
147 | |
148 scoped_refptr<JsonPrefStore> unprotected_pref_store(new JsonPrefStore( | |
149 profile_path_.Append(chrome::kPreferencesFilename), io_task_runner.get(), | |
150 std::move(unprotected_pref_hash_filter))); | |
151 scoped_refptr<JsonPrefStore> protected_pref_store(new JsonPrefStore( | |
152 profile_path_.Append(chrome::kSecurePreferencesFilename), | 107 profile_path_.Append(chrome::kSecurePreferencesFilename), |
153 io_task_runner.get(), std::move(protected_pref_hash_filter))); | 108 tracking_configuration_, reporting_ids_count_, seed_, legacy_device_id_, |
154 | 109 #if defined(OS_WIN) |
155 SetupTrackedPreferencesMigration( | 110 g_preference_validation_registry_path_for_testing |
156 unprotected_pref_names, protected_pref_names, | 111 ? *g_preference_validation_registry_path_for_testing |
157 base::Bind(&RemoveValueSilently, unprotected_pref_store->AsWeakPtr()), | 112 : install_static::GetRegistryPath(), |
158 base::Bind(&RemoveValueSilently, protected_pref_store->AsWeakPtr()), | 113 #else |
159 base::Bind(&JsonPrefStore::RegisterOnNextSuccessfulWriteReply, | 114 base::string16(), |
160 unprotected_pref_store->AsWeakPtr()), | 115 #endif |
161 base::Bind(&JsonPrefStore::RegisterOnNextSuccessfulWriteReply, | 116 validation_delegate, on_reset_on_load); |
162 protected_pref_store->AsWeakPtr()), | |
163 GetPrefHashStore(false), GetPrefHashStore(true), | |
164 raw_unprotected_pref_hash_filter, raw_protected_pref_hash_filter); | |
165 | |
166 return new SegregatedPrefStore(unprotected_pref_store, protected_pref_store, | |
167 protected_pref_names); | |
168 } | 117 } |
169 | 118 |
170 bool ProfilePrefStoreManager::InitializePrefsFromMasterPrefs( | 119 bool ProfilePrefStoreManager::InitializePrefsFromMasterPrefs( |
171 std::unique_ptr<base::DictionaryValue> master_prefs) { | 120 std::unique_ptr<base::DictionaryValue> master_prefs) { |
172 // Create the profile directory if it doesn't exist yet (very possible on | 121 // Create the profile directory if it doesn't exist yet (very possible on |
173 // first run). | 122 // first run). |
174 if (!base::CreateDirectory(profile_path_)) | 123 if (!base::CreateDirectory(profile_path_)) |
175 return false; | 124 return false; |
176 | 125 |
177 if (kPlatformSupportsPreferenceTracking) { | 126 if (kPlatformSupportsPreferenceTracking) { |
178 PrefHashFilter(GetPrefHashStore(false), | 127 PrefHashFilter(GetPrefHashStore(), |
gab
2017/03/15 15:09:58
inline GetPrefHashStore() here as this is now the
Sam McNally
2017/03/16 07:13:48
Done.
| |
179 GetExternalVerificationPrefHashStorePair(), | 128 GetExternalVerificationPrefHashStorePair(), |
180 tracking_configuration_, base::Closure(), NULL, | 129 tracking_configuration_, base::Closure(), NULL, |
181 reporting_ids_count_, false) | 130 reporting_ids_count_, false) |
182 .Initialize(master_prefs.get()); | 131 .Initialize(master_prefs.get()); |
183 } | 132 } |
184 | 133 |
185 // This will write out to a single combined file which will be immediately | 134 // This will write out to a single combined file which will be immediately |
186 // migrated to two files on load. | 135 // migrated to two files on load. |
187 JSONFileValueSerializer serializer( | 136 JSONFileValueSerializer serializer( |
188 profile_path_.Append(chrome::kPreferencesFilename)); | 137 profile_path_.Append(chrome::kPreferencesFilename)); |
189 | 138 |
190 // Call Serialize (which does IO) on the main thread, which would _normally_ | 139 // Call Serialize (which does IO) on the main thread, which would _normally_ |
191 // be verboten. In this case however, we require this IO to synchronously | 140 // be verboten. In this case however, we require this IO to synchronously |
192 // complete before Chrome can start (as master preferences seed the Local | 141 // complete before Chrome can start (as master preferences seed the Local |
193 // State and Preferences files). This won't trip ThreadIORestrictions as they | 142 // State and Preferences files). This won't trip ThreadIORestrictions as they |
194 // won't have kicked in yet on the main thread. | 143 // won't have kicked in yet on the main thread. |
195 bool success = serializer.Serialize(*master_prefs); | 144 bool success = serializer.Serialize(*master_prefs); |
196 | 145 |
197 UMA_HISTOGRAM_BOOLEAN("Settings.InitializedFromMasterPrefs", success); | 146 UMA_HISTOGRAM_BOOLEAN("Settings.InitializedFromMasterPrefs", success); |
198 return success; | 147 return success; |
199 } | 148 } |
200 | 149 |
201 std::unique_ptr<PrefHashStore> ProfilePrefStoreManager::GetPrefHashStore( | 150 std::unique_ptr<PrefHashStore> ProfilePrefStoreManager::GetPrefHashStore() { |
202 bool use_super_mac) { | |
203 DCHECK(kPlatformSupportsPreferenceTracking); | 151 DCHECK(kPlatformSupportsPreferenceTracking); |
204 | 152 |
205 return std::unique_ptr<PrefHashStore>( | 153 return std::unique_ptr<PrefHashStore>( |
206 new PrefHashStoreImpl(seed_, legacy_device_id_, use_super_mac)); | 154 new PrefHashStoreImpl(seed_, legacy_device_id_, false)); |
207 } | 155 } |
208 | 156 |
209 std::pair<std::unique_ptr<PrefHashStore>, std::unique_ptr<HashStoreContents>> | 157 std::pair<std::unique_ptr<PrefHashStore>, std::unique_ptr<HashStoreContents>> |
210 ProfilePrefStoreManager::GetExternalVerificationPrefHashStorePair() { | 158 ProfilePrefStoreManager::GetExternalVerificationPrefHashStorePair() { |
211 DCHECK(kPlatformSupportsPreferenceTracking); | 159 DCHECK(kPlatformSupportsPreferenceTracking); |
212 #if defined(OS_WIN) | 160 #if defined(OS_WIN) |
213 return std::make_pair( | 161 return std::make_pair( |
214 base::MakeUnique<PrefHashStoreImpl>( | 162 base::MakeUnique<PrefHashStoreImpl>( |
215 "ChromeRegistryHashStoreValidationSeed", legacy_device_id_, | 163 "ChromeRegistryHashStoreValidationSeed", legacy_device_id_, |
216 false /* use_super_mac */), | 164 false /* use_super_mac */), |
217 g_preference_validation_registry_path_for_testing | 165 g_preference_validation_registry_path_for_testing |
218 ? base::MakeUnique<RegistryHashStoreContentsWin>( | 166 ? base::MakeUnique<RegistryHashStoreContentsWin>( |
219 *g_preference_validation_registry_path_for_testing, | 167 *g_preference_validation_registry_path_for_testing, |
220 profile_path_.BaseName().LossyDisplayName()) | 168 profile_path_.BaseName().LossyDisplayName()) |
221 : base::MakeUnique<RegistryHashStoreContentsWin>( | 169 : base::MakeUnique<RegistryHashStoreContentsWin>( |
222 install_static::GetRegistryPath(), | 170 install_static::GetRegistryPath(), |
223 profile_path_.BaseName().LossyDisplayName())); | 171 profile_path_.BaseName().LossyDisplayName())); |
224 #else | 172 #else |
225 return std::make_pair(nullptr, nullptr); | 173 return std::make_pair(nullptr, nullptr); |
226 #endif | 174 #endif |
227 } | 175 } |
OLD | NEW |