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