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" | |
10 #include "base/callback.h" | |
11 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
12 #include "base/json/json_file_value_serializer.h" | 10 #include "base/json/json_file_value_serializer.h" |
13 #include "base/logging.h" | 11 #include "base/logging.h" |
14 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
15 #include "base/metrics/histogram_macros.h" | 13 #include "base/metrics/histogram_macros.h" |
16 #include "base/sequenced_task_runner.h" | 14 #include "base/sequenced_task_runner.h" |
17 #include "build/build_config.h" | 15 #include "build/build_config.h" |
18 #include "chrome/browser/prefs/browser_prefs.h" | 16 #include "chrome/browser/prefs/browser_prefs.h" |
19 #include "chrome/common/chrome_constants.h" | 17 #include "chrome/common/chrome_constants.h" |
20 #include "chrome/common/chrome_features.h" | 18 #include "chrome/common/chrome_features.h" |
21 #include "components/pref_registry/pref_registry_syncable.h" | 19 #include "components/pref_registry/pref_registry_syncable.h" |
22 #include "components/prefs/json_pref_store.h" | 20 #include "components/prefs/json_pref_store.h" |
23 #include "components/prefs/persistent_pref_store.h" | 21 #include "components/prefs/persistent_pref_store.h" |
24 #include "components/prefs/pref_registry_simple.h" | 22 #include "components/prefs/pref_registry_simple.h" |
25 #include "services/preferences/public/cpp/persistent_pref_store_client.h" | 23 #include "services/preferences/public/cpp/persistent_pref_store_client.h" |
26 #include "services/preferences/public/interfaces/preferences.mojom.h" | 24 #include "services/preferences/public/interfaces/preferences.mojom.h" |
27 #include "services/preferences/tracked/pref_hash_filter.h" | 25 #include "services/preferences/tracked/pref_hash_filter.h" |
28 #include "services/preferences/tracked/pref_hash_store_impl.h" | 26 #include "services/preferences/tracked/tracked_persistent_pref_store_factory.h" |
29 #include "services/preferences/tracked/segregated_pref_store.h" | |
30 #include "services/preferences/tracked/tracked_preferences_migration.h" | |
31 #include "services/service_manager/public/cpp/connector.h" | 27 #include "services/service_manager/public/cpp/connector.h" |
32 | 28 |
33 #if defined(OS_WIN) | 29 #if defined(OS_WIN) |
34 #include "chrome/install_static/install_util.h" | 30 #include "chrome/install_static/install_util.h" |
35 #include "services/preferences/tracked/registry_hash_store_contents_win.h" | |
36 #endif | 31 #endif |
37 | 32 |
38 namespace { | 33 namespace { |
39 | 34 |
40 using EnforcementLevel = | |
41 prefs::mojom::TrackedPreferenceMetadata::EnforcementLevel; | |
42 | |
43 void RemoveValueSilently(const base::WeakPtr<JsonPrefStore> pref_store, | |
44 const std::string& key) { | |
45 if (pref_store) { | |
46 pref_store->RemoveValueSilently( | |
47 key, WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | |
48 } | |
49 } | |
50 | |
51 #if defined(OS_WIN) | 35 #if defined(OS_WIN) |
52 // Forces a different registry key to be used for storing preference validation | 36 // Forces a different registry key to be used for storing preference validation |
53 // MACs. See |SetPreferenceValidationRegistryPathForTesting|. | 37 // MACs. See |SetPreferenceValidationRegistryPathForTesting|. |
54 const base::string16* g_preference_validation_registry_path_for_testing = | 38 const base::string16* g_preference_validation_registry_path_for_testing = |
55 nullptr; | 39 nullptr; |
56 #endif // OS_WIN | 40 #endif // OS_WIN |
57 | 41 |
58 } // namespace | 42 } // namespace |
59 | 43 |
60 // Preference tracking and protection is not required on platforms where other | 44 // Preference tracking and protection is not required on platforms where other |
61 // apps do not have access to chrome's persistent storage. | 45 // apps do not have access to chrome's persistent storage. |
62 const bool ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking = | 46 const bool ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking = |
63 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) | 47 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) |
64 false; | 48 false; |
65 #else | 49 #else |
66 true; | 50 true; |
67 #endif | 51 #endif |
68 | 52 |
69 ProfilePrefStoreManager::ProfilePrefStoreManager( | 53 ProfilePrefStoreManager::ProfilePrefStoreManager( |
70 const base::FilePath& profile_path, | 54 const base::FilePath& profile_path, |
71 std::vector<prefs::mojom::TrackedPreferenceMetadataPtr> | |
72 tracking_configuration, | |
73 size_t reporting_ids_count, | |
74 const std::string& seed, | 55 const std::string& seed, |
75 const std::string& legacy_device_id, | 56 const std::string& legacy_device_id) |
76 PrefService* local_state) | |
77 : profile_path_(profile_path), | 57 : profile_path_(profile_path), |
78 tracking_configuration_(std::move(tracking_configuration)), | |
79 reporting_ids_count_(reporting_ids_count), | |
80 seed_(seed), | 58 seed_(seed), |
81 legacy_device_id_(legacy_device_id), | 59 legacy_device_id_(legacy_device_id) {} |
82 local_state_(local_state) {} | |
83 | 60 |
84 ProfilePrefStoreManager::~ProfilePrefStoreManager() {} | 61 ProfilePrefStoreManager::~ProfilePrefStoreManager() {} |
85 | 62 |
86 // static | 63 // static |
87 void ProfilePrefStoreManager::RegisterProfilePrefs( | 64 void ProfilePrefStoreManager::RegisterProfilePrefs( |
88 user_prefs::PrefRegistrySyncable* registry) { | 65 user_prefs::PrefRegistrySyncable* registry) { |
89 PrefHashFilter::RegisterProfilePrefs(registry); | 66 PrefHashFilter::RegisterProfilePrefs(registry); |
90 } | 67 } |
91 | 68 |
92 // static | 69 // static |
93 base::Time ProfilePrefStoreManager::GetResetTime(PrefService* pref_service) { | 70 base::Time ProfilePrefStoreManager::GetResetTime(PrefService* pref_service) { |
94 return PrefHashFilter::GetResetTime(pref_service); | 71 return PrefHashFilter::GetResetTime(pref_service); |
95 } | 72 } |
96 | 73 |
97 // static | 74 // static |
98 void ProfilePrefStoreManager::ClearResetTime(PrefService* pref_service) { | 75 void ProfilePrefStoreManager::ClearResetTime(PrefService* pref_service) { |
99 PrefHashFilter::ClearResetTime(pref_service); | 76 PrefHashFilter::ClearResetTime(pref_service); |
100 } | 77 } |
101 | 78 |
102 #if defined(OS_WIN) | 79 #if defined(OS_WIN) |
103 // static | 80 // static |
104 void ProfilePrefStoreManager::SetPreferenceValidationRegistryPathForTesting( | 81 void ProfilePrefStoreManager::SetPreferenceValidationRegistryPathForTesting( |
105 const base::string16* path) { | 82 const base::string16* path) { |
106 DCHECK(!path->empty()); | 83 DCHECK(!path->empty()); |
107 g_preference_validation_registry_path_for_testing = path; | 84 g_preference_validation_registry_path_for_testing = path; |
108 } | 85 } |
109 #endif // OS_WIN | 86 #endif // OS_WIN |
110 | 87 |
111 PersistentPrefStore* ProfilePrefStoreManager::CreateProfilePrefStore( | 88 PersistentPrefStore* ProfilePrefStoreManager::CreateProfilePrefStore( |
112 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner, | 89 std::vector<prefs::mojom::TrackedPreferenceMetadataPtr> |
113 const base::Closure& on_reset_on_load, | 90 tracking_configuration, |
114 prefs::mojom::TrackedPreferenceValidationDelegate* validation_delegate, | 91 size_t reporting_ids_count, |
| 92 base::SequencedWorkerPool* worker_pool, |
| 93 prefs::mojom::ResetOnLoadObserverPtr reset_on_load_observer, |
| 94 prefs::mojom::TrackedPreferenceValidationDelegatePtr validation_delegate, |
115 service_manager::Connector* connector, | 95 service_manager::Connector* connector, |
116 scoped_refptr<PrefRegistry> pref_registry) { | 96 scoped_refptr<PrefRegistry> pref_registry) { |
117 if (features::PrefServiceEnabled()) { | 97 if (features::PrefServiceEnabled()) { |
118 ConfigurePrefService(on_reset_on_load, connector); | 98 ConfigurePrefService(std::move(tracking_configuration), reporting_ids_count, |
| 99 std::move(reset_on_load_observer), |
| 100 std::move(validation_delegate), connector); |
119 prefs::mojom::PrefStoreConnectorPtr pref_connector; | 101 prefs::mojom::PrefStoreConnectorPtr pref_connector; |
120 connector->BindInterface(prefs::mojom::kServiceName, &pref_connector); | 102 connector->BindInterface(prefs::mojom::kServiceName, &pref_connector); |
121 auto in_process_types_set = chrome::InProcessPrefStores(); | 103 auto in_process_types_set = chrome::InProcessPrefStores(); |
122 std::vector<PrefValueStore::PrefStoreType> in_process_types( | 104 std::vector<PrefValueStore::PrefStoreType> in_process_types( |
123 in_process_types_set.begin(), in_process_types_set.end()); | 105 in_process_types_set.begin(), in_process_types_set.end()); |
124 return new prefs::PersistentPrefStoreClient(std::move(pref_connector), | 106 return new prefs::PersistentPrefStoreClient(std::move(pref_connector), |
125 std::move(pref_registry), | 107 std::move(pref_registry), |
126 std::move(in_process_types)); | 108 std::move(in_process_types)); |
127 } | 109 } |
128 if (!kPlatformSupportsPreferenceTracking) { | 110 if (!kPlatformSupportsPreferenceTracking) { |
129 return new JsonPrefStore(profile_path_.Append(chrome::kPreferencesFilename), | 111 return new JsonPrefStore( |
130 io_task_runner.get(), | 112 profile_path_.Append(chrome::kPreferencesFilename), |
131 std::unique_ptr<PrefFilter>()); | 113 JsonPrefStore::GetTaskRunnerForFile(profile_path_, worker_pool), |
| 114 nullptr); |
132 } | 115 } |
133 | 116 return CreateTrackedPersistentPrefStore( |
134 std::vector<prefs::mojom::TrackedPreferenceMetadataPtr> | 117 CreateTrackedPrefStoreConfiguration( |
135 unprotected_configuration; | 118 std::move(tracking_configuration), reporting_ids_count, |
136 std::vector<prefs::mojom::TrackedPreferenceMetadataPtr> | 119 std::move(reset_on_load_observer), std::move(validation_delegate)), |
137 protected_configuration; | 120 worker_pool); |
138 std::set<std::string> protected_pref_names; | |
139 std::set<std::string> unprotected_pref_names; | |
140 for (auto& metadata : tracking_configuration_) { | |
141 if (metadata->enforcement_level > EnforcementLevel::NO_ENFORCEMENT) { | |
142 protected_pref_names.insert(metadata->name); | |
143 protected_configuration.push_back(std::move(metadata)); | |
144 } else { | |
145 unprotected_pref_names.insert(metadata->name); | |
146 unprotected_configuration.push_back(std::move(metadata)); | |
147 } | |
148 } | |
149 tracking_configuration_.clear(); | |
150 | |
151 std::unique_ptr<PrefHashFilter> unprotected_pref_hash_filter( | |
152 new PrefHashFilter(GetPrefHashStore(false), | |
153 GetExternalVerificationPrefHashStorePair(), | |
154 unprotected_configuration, base::Closure(), | |
155 validation_delegate, reporting_ids_count_, false)); | |
156 std::unique_ptr<PrefHashFilter> protected_pref_hash_filter(new PrefHashFilter( | |
157 GetPrefHashStore(true), GetExternalVerificationPrefHashStorePair(), | |
158 protected_configuration, on_reset_on_load, validation_delegate, | |
159 reporting_ids_count_, true)); | |
160 | |
161 PrefHashFilter* raw_unprotected_pref_hash_filter = | |
162 unprotected_pref_hash_filter.get(); | |
163 PrefHashFilter* raw_protected_pref_hash_filter = | |
164 protected_pref_hash_filter.get(); | |
165 | |
166 scoped_refptr<JsonPrefStore> unprotected_pref_store(new JsonPrefStore( | |
167 profile_path_.Append(chrome::kPreferencesFilename), io_task_runner.get(), | |
168 std::move(unprotected_pref_hash_filter))); | |
169 scoped_refptr<JsonPrefStore> protected_pref_store(new JsonPrefStore( | |
170 profile_path_.Append(chrome::kSecurePreferencesFilename), | |
171 io_task_runner.get(), std::move(protected_pref_hash_filter))); | |
172 | |
173 SetupTrackedPreferencesMigration( | |
174 unprotected_pref_names, protected_pref_names, | |
175 base::Bind(&RemoveValueSilently, unprotected_pref_store->AsWeakPtr()), | |
176 base::Bind(&RemoveValueSilently, protected_pref_store->AsWeakPtr()), | |
177 base::Bind(&JsonPrefStore::RegisterOnNextSuccessfulWriteReply, | |
178 unprotected_pref_store->AsWeakPtr()), | |
179 base::Bind(&JsonPrefStore::RegisterOnNextSuccessfulWriteReply, | |
180 protected_pref_store->AsWeakPtr()), | |
181 GetPrefHashStore(false), GetPrefHashStore(true), | |
182 raw_unprotected_pref_hash_filter, raw_protected_pref_hash_filter); | |
183 | |
184 return new SegregatedPrefStore(unprotected_pref_store, protected_pref_store, | |
185 protected_pref_names); | |
186 } | 121 } |
187 | 122 |
188 bool ProfilePrefStoreManager::InitializePrefsFromMasterPrefs( | 123 bool ProfilePrefStoreManager::InitializePrefsFromMasterPrefs( |
| 124 std::vector<prefs::mojom::TrackedPreferenceMetadataPtr> |
| 125 tracking_configuration, |
| 126 size_t reporting_ids_count, |
189 std::unique_ptr<base::DictionaryValue> master_prefs) { | 127 std::unique_ptr<base::DictionaryValue> master_prefs) { |
190 // Create the profile directory if it doesn't exist yet (very possible on | 128 // Create the profile directory if it doesn't exist yet (very possible on |
191 // first run). | 129 // first run). |
192 if (!base::CreateDirectory(profile_path_)) | 130 if (!base::CreateDirectory(profile_path_)) |
193 return false; | 131 return false; |
194 | 132 |
195 if (kPlatformSupportsPreferenceTracking) { | 133 if (kPlatformSupportsPreferenceTracking) { |
196 PrefHashFilter(GetPrefHashStore(false), | 134 InitializeMasterPrefsTracking( |
197 GetExternalVerificationPrefHashStorePair(), | 135 CreateTrackedPrefStoreConfiguration(std::move(tracking_configuration), |
198 tracking_configuration_, base::Closure(), NULL, | 136 reporting_ids_count, {}, nullptr), |
199 reporting_ids_count_, false) | 137 master_prefs.get()); |
200 .Initialize(master_prefs.get()); | |
201 } | 138 } |
202 | 139 |
203 // This will write out to a single combined file which will be immediately | 140 // This will write out to a single combined file which will be immediately |
204 // migrated to two files on load. | 141 // migrated to two files on load. |
205 JSONFileValueSerializer serializer( | 142 JSONFileValueSerializer serializer( |
206 profile_path_.Append(chrome::kPreferencesFilename)); | 143 profile_path_.Append(chrome::kPreferencesFilename)); |
207 | 144 |
208 // Call Serialize (which does IO) on the main thread, which would _normally_ | 145 // Call Serialize (which does IO) on the main thread, which would _normally_ |
209 // be verboten. In this case however, we require this IO to synchronously | 146 // be verboten. In this case however, we require this IO to synchronously |
210 // complete before Chrome can start (as master preferences seed the Local | 147 // complete before Chrome can start (as master preferences seed the Local |
211 // State and Preferences files). This won't trip ThreadIORestrictions as they | 148 // State and Preferences files). This won't trip ThreadIORestrictions as they |
212 // won't have kicked in yet on the main thread. | 149 // won't have kicked in yet on the main thread. |
213 bool success = serializer.Serialize(*master_prefs); | 150 bool success = serializer.Serialize(*master_prefs); |
214 | 151 |
215 UMA_HISTOGRAM_BOOLEAN("Settings.InitializedFromMasterPrefs", success); | 152 UMA_HISTOGRAM_BOOLEAN("Settings.InitializedFromMasterPrefs", success); |
216 return success; | 153 return success; |
217 } | 154 } |
218 | 155 |
219 std::unique_ptr<PrefHashStore> ProfilePrefStoreManager::GetPrefHashStore( | |
220 bool use_super_mac) { | |
221 DCHECK(kPlatformSupportsPreferenceTracking); | |
222 | |
223 return std::unique_ptr<PrefHashStore>( | |
224 new PrefHashStoreImpl(seed_, legacy_device_id_, use_super_mac)); | |
225 } | |
226 | |
227 std::pair<std::unique_ptr<PrefHashStore>, std::unique_ptr<HashStoreContents>> | |
228 ProfilePrefStoreManager::GetExternalVerificationPrefHashStorePair() { | |
229 DCHECK(kPlatformSupportsPreferenceTracking); | |
230 #if defined(OS_WIN) | |
231 return std::make_pair( | |
232 base::MakeUnique<PrefHashStoreImpl>( | |
233 "ChromeRegistryHashStoreValidationSeed", legacy_device_id_, | |
234 false /* use_super_mac */), | |
235 g_preference_validation_registry_path_for_testing | |
236 ? base::MakeUnique<RegistryHashStoreContentsWin>( | |
237 *g_preference_validation_registry_path_for_testing, | |
238 profile_path_.BaseName().LossyDisplayName()) | |
239 : base::MakeUnique<RegistryHashStoreContentsWin>( | |
240 install_static::GetRegistryPath(), | |
241 profile_path_.BaseName().LossyDisplayName())); | |
242 #else | |
243 return std::make_pair(nullptr, nullptr); | |
244 #endif | |
245 } | |
246 | |
247 void ProfilePrefStoreManager::ConfigurePrefService( | 156 void ProfilePrefStoreManager::ConfigurePrefService( |
248 const base::Closure& on_reset_on_load, | 157 std::vector<prefs::mojom::TrackedPreferenceMetadataPtr> |
| 158 tracking_configuration, |
| 159 size_t reporting_ids_count, |
| 160 prefs::mojom::ResetOnLoadObserverPtr reset_on_load_observer, |
| 161 prefs::mojom::TrackedPreferenceValidationDelegatePtr validation_delegate, |
249 service_manager::Connector* connector) { | 162 service_manager::Connector* connector) { |
250 auto config = prefs::mojom::PersistentPrefStoreConfiguration::New(); | 163 auto config = prefs::mojom::PersistentPrefStoreConfiguration::New(); |
251 config->set_simple_configuration( | 164 if (!kPlatformSupportsPreferenceTracking) { |
252 prefs::mojom::SimplePersistentPrefStoreConfiguration::New( | 165 config->set_simple_configuration( |
253 profile_path_.Append(chrome::kPreferencesFilename))); | 166 prefs::mojom::SimplePersistentPrefStoreConfiguration::New( |
| 167 profile_path_.Append(chrome::kPreferencesFilename))); |
| 168 } else { |
| 169 config->set_tracked_configuration(CreateTrackedPrefStoreConfiguration( |
| 170 std::move(tracking_configuration), reporting_ids_count, |
| 171 std::move(reset_on_load_observer), std::move(validation_delegate))); |
| 172 } |
254 prefs::mojom::PrefServiceControlPtr control; | 173 prefs::mojom::PrefServiceControlPtr control; |
255 connector->BindInterface(prefs::mojom::kServiceName, &control); | 174 connector->BindInterface(prefs::mojom::kServiceName, &control); |
256 control->Init(std::move(config)); | 175 control->Init(std::move(config)); |
257 } | 176 } |
| 177 |
| 178 prefs::mojom::TrackedPersistentPrefStoreConfigurationPtr |
| 179 ProfilePrefStoreManager::CreateTrackedPrefStoreConfiguration( |
| 180 std::vector<prefs::mojom::TrackedPreferenceMetadataPtr> |
| 181 tracking_configuration, |
| 182 size_t reporting_ids_count, |
| 183 prefs::mojom::ResetOnLoadObserverPtr reset_on_load_observer, |
| 184 prefs::mojom::TrackedPreferenceValidationDelegatePtr validation_delegate) { |
| 185 return prefs::mojom::TrackedPersistentPrefStoreConfiguration::New( |
| 186 profile_path_.Append(chrome::kPreferencesFilename), |
| 187 profile_path_.Append(chrome::kSecurePreferencesFilename), |
| 188 std::move(tracking_configuration), reporting_ids_count, seed_, |
| 189 legacy_device_id_, "ChromeRegistryHashStoreValidationSeed", |
| 190 #if defined(OS_WIN) |
| 191 g_preference_validation_registry_path_for_testing |
| 192 ? *g_preference_validation_registry_path_for_testing |
| 193 : install_static::GetRegistryPath(), |
| 194 #else |
| 195 base::string16(), |
| 196 #endif |
| 197 std::move(validation_delegate), std::move(reset_on_load_observer)); |
| 198 } |
OLD | NEW |