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 "chrome/common/chrome_features.h" |
19 #include "components/pref_registry/pref_registry_syncable.h" | 20 #include "components/pref_registry/pref_registry_syncable.h" |
20 #include "components/prefs/json_pref_store.h" | 21 #include "components/prefs/json_pref_store.h" |
21 #include "components/prefs/persistent_pref_store.h" | 22 #include "components/prefs/persistent_pref_store.h" |
22 #include "components/prefs/pref_registry_simple.h" | 23 #include "components/prefs/pref_registry_simple.h" |
23 #include "components/user_prefs/tracked/pref_hash_store_impl.h" | 24 #include "components/user_prefs/tracked/pref_hash_store_impl.h" |
24 #include "components/user_prefs/tracked/segregated_pref_store.h" | 25 #include "components/user_prefs/tracked/segregated_pref_store.h" |
25 #include "components/user_prefs/tracked/tracked_preferences_migration.h" | 26 #include "components/user_prefs/tracked/tracked_preferences_migration.h" |
| 27 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 28 #include "services/preferences/public/cpp/persistent_pref_store_client.h" |
26 #include "services/preferences/public/cpp/tracked_persistent_pref_store_factory.
h" | 29 #include "services/preferences/public/cpp/tracked_persistent_pref_store_factory.
h" |
| 30 #include "services/preferences/public/interfaces/preferences.mojom.h" |
27 #include "services/preferences/public/interfaces/preferences_configuration.mojom
.h" | 31 #include "services/preferences/public/interfaces/preferences_configuration.mojom
.h" |
| 32 #include "services/preferences/public/interfaces/tracked_preference_validation_d
elegate.mojom.h" |
| 33 #include "services/service_manager/public/cpp/connector.h" |
28 | 34 |
29 #if defined(OS_WIN) | 35 #if defined(OS_WIN) |
30 #include "chrome/install_static/install_util.h" | 36 #include "chrome/install_static/install_util.h" |
31 #include "components/user_prefs/tracked/registry_hash_store_contents_win.h" | 37 #include "components/user_prefs/tracked/registry_hash_store_contents_win.h" |
32 #endif | 38 #endif |
33 | 39 |
34 namespace { | 40 namespace { |
35 | 41 |
36 #if defined(OS_WIN) | 42 #if defined(OS_WIN) |
37 // Forces a different registry key to be used for storing preference validation | 43 // Forces a different registry key to be used for storing preference validation |
38 // MACs. See |SetPreferenceValidationRegistryPathForTesting|. | 44 // MACs. See |SetPreferenceValidationRegistryPathForTesting|. |
39 const base::string16* g_preference_validation_registry_path_for_testing = | 45 const base::string16* g_preference_validation_registry_path_for_testing = |
40 nullptr; | 46 nullptr; |
41 #endif // OS_WIN | 47 #endif // OS_WIN |
42 | 48 |
| 49 class ResetOnLoadObserverImpl : public prefs::mojom::ResetOnLoadObserver { |
| 50 public: |
| 51 explicit ResetOnLoadObserverImpl(const base::Closure& on_reset_on_load) |
| 52 : on_reset_on_load_(on_reset_on_load) { |
| 53 DCHECK(on_reset_on_load_); |
| 54 } |
| 55 |
| 56 void OnResetOnLoad() override { on_reset_on_load_.Run(); } |
| 57 |
| 58 private: |
| 59 base::Closure on_reset_on_load_; |
| 60 |
| 61 DISALLOW_COPY_AND_ASSIGN(ResetOnLoadObserverImpl); |
| 62 }; |
| 63 |
| 64 std::vector<prefs::mojom::TrackedPreferenceMetadataPtr> |
| 65 WrapTrackedPreferenceMetadata( |
| 66 const std::vector<PrefHashFilter::TrackedPreferenceMetadata>& input) { |
| 67 std::vector<prefs::mojom::TrackedPreferenceMetadataPtr> output; |
| 68 for (const auto& metadata : input) { |
| 69 output.emplace_back(base::in_place, metadata.reporting_id, metadata.name, |
| 70 metadata.enforcement_level, metadata.strategy, |
| 71 metadata.value_type); |
| 72 } |
| 73 return output; |
| 74 } |
| 75 |
43 } // namespace | 76 } // namespace |
44 | 77 |
45 // Preference tracking and protection is not required on platforms where other | 78 // Preference tracking and protection is not required on platforms where other |
46 // apps do not have access to chrome's persistent storage. | 79 // apps do not have access to chrome's persistent storage. |
47 const bool ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking = | 80 const bool ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking = |
48 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) | 81 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) |
49 false; | 82 false; |
50 #else | 83 #else |
51 true; | 84 true; |
52 #endif | 85 #endif |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 void ProfilePrefStoreManager::SetPreferenceValidationRegistryPathForTesting( | 122 void ProfilePrefStoreManager::SetPreferenceValidationRegistryPathForTesting( |
90 const base::string16* path) { | 123 const base::string16* path) { |
91 DCHECK(!path->empty()); | 124 DCHECK(!path->empty()); |
92 g_preference_validation_registry_path_for_testing = path; | 125 g_preference_validation_registry_path_for_testing = path; |
93 } | 126 } |
94 #endif // OS_WIN | 127 #endif // OS_WIN |
95 | 128 |
96 PersistentPrefStore* ProfilePrefStoreManager::CreateProfilePrefStore( | 129 PersistentPrefStore* ProfilePrefStoreManager::CreateProfilePrefStore( |
97 scoped_refptr<base::SequencedTaskRunner> io_task_runner, | 130 scoped_refptr<base::SequencedTaskRunner> io_task_runner, |
98 const base::Closure& on_reset_on_load, | 131 const base::Closure& on_reset_on_load, |
99 prefs::mojom::TrackedPreferenceValidationDelegate* validation_delegate) { | 132 std::unique_ptr<prefs::mojom::TrackedPreferenceValidationDelegate>* |
| 133 validation_delegate, |
| 134 service_manager::Connector* connector) { |
| 135 DCHECK(validation_delegate); |
| 136 if (base::FeatureList::IsEnabled(features::kPrefService)) { |
| 137 ConfigurePrefService(on_reset_on_load, std::move(*validation_delegate), |
| 138 connector); |
| 139 prefs::mojom::PersistentPrefStoreConnectorPtr pref_connector; |
| 140 connector->BindInterface(prefs::mojom::kPrefStoreServiceName, |
| 141 &pref_connector); |
| 142 return new prefs::PersistentPrefStoreClient(std::move(pref_connector)); |
| 143 } |
100 if (!kPlatformSupportsPreferenceTracking) { | 144 if (!kPlatformSupportsPreferenceTracking) { |
101 return new JsonPrefStore(profile_path_.Append(chrome::kPreferencesFilename), | 145 return new JsonPrefStore(profile_path_.Append(chrome::kPreferencesFilename), |
102 io_task_runner, std::unique_ptr<PrefFilter>()); | 146 io_task_runner, std::unique_ptr<PrefFilter>()); |
103 } | 147 } |
104 return prefs::CreateTrackedPersistentPrefStore( | 148 return prefs::CreateTrackedPersistentPrefStore( |
105 std::move(io_task_runner), | 149 std::move(io_task_runner), |
106 profile_path_.Append(chrome::kPreferencesFilename), | 150 profile_path_.Append(chrome::kPreferencesFilename), |
107 profile_path_.Append(chrome::kSecurePreferencesFilename), | 151 profile_path_.Append(chrome::kSecurePreferencesFilename), |
108 tracking_configuration_, reporting_ids_count_, seed_, legacy_device_id_, | 152 tracking_configuration_, reporting_ids_count_, seed_, legacy_device_id_, |
109 #if defined(OS_WIN) | 153 #if defined(OS_WIN) |
110 g_preference_validation_registry_path_for_testing | 154 g_preference_validation_registry_path_for_testing |
111 ? *g_preference_validation_registry_path_for_testing | 155 ? *g_preference_validation_registry_path_for_testing |
112 : install_static::GetRegistryPath(), | 156 : install_static::GetRegistryPath(), |
113 #else | 157 #else |
114 base::string16(), | 158 base::string16(), |
115 #endif | 159 #endif |
116 validation_delegate, on_reset_on_load); | 160 validation_delegate->get(), on_reset_on_load); |
117 } | 161 } |
118 | 162 |
119 bool ProfilePrefStoreManager::InitializePrefsFromMasterPrefs( | 163 bool ProfilePrefStoreManager::InitializePrefsFromMasterPrefs( |
120 std::unique_ptr<base::DictionaryValue> master_prefs) { | 164 std::unique_ptr<base::DictionaryValue> master_prefs) { |
121 // Create the profile directory if it doesn't exist yet (very possible on | 165 // Create the profile directory if it doesn't exist yet (very possible on |
122 // first run). | 166 // first run). |
123 if (!base::CreateDirectory(profile_path_)) | 167 if (!base::CreateDirectory(profile_path_)) |
124 return false; | 168 return false; |
125 | 169 |
126 if (kPlatformSupportsPreferenceTracking) { | 170 if (kPlatformSupportsPreferenceTracking) { |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 ? base::MakeUnique<RegistryHashStoreContentsWin>( | 210 ? base::MakeUnique<RegistryHashStoreContentsWin>( |
167 *g_preference_validation_registry_path_for_testing, | 211 *g_preference_validation_registry_path_for_testing, |
168 profile_path_.BaseName().LossyDisplayName()) | 212 profile_path_.BaseName().LossyDisplayName()) |
169 : base::MakeUnique<RegistryHashStoreContentsWin>( | 213 : base::MakeUnique<RegistryHashStoreContentsWin>( |
170 install_static::GetRegistryPath(), | 214 install_static::GetRegistryPath(), |
171 profile_path_.BaseName().LossyDisplayName())); | 215 profile_path_.BaseName().LossyDisplayName())); |
172 #else | 216 #else |
173 return std::make_pair(nullptr, nullptr); | 217 return std::make_pair(nullptr, nullptr); |
174 #endif | 218 #endif |
175 } | 219 } |
| 220 |
| 221 void ProfilePrefStoreManager::ConfigurePrefService( |
| 222 const base::Closure& on_reset_on_load, |
| 223 std::unique_ptr<prefs::mojom::TrackedPreferenceValidationDelegate> |
| 224 validation_delegate, |
| 225 service_manager::Connector* connector) { |
| 226 auto config = prefs::mojom::PersistentPrefStoreConfiguration::New(); |
| 227 if (!kPlatformSupportsPreferenceTracking) { |
| 228 config->set_simple_configuration( |
| 229 prefs::mojom::SimplePersistentPrefStoreConfiguration::New( |
| 230 profile_path_.Append(chrome::kPreferencesFilename))); |
| 231 } else { |
| 232 prefs::mojom::TrackedPreferenceValidationDelegatePtr |
| 233 validation_delegate_ptr; |
| 234 if (validation_delegate) { |
| 235 mojo::MakeStrongBinding(std::move(validation_delegate), |
| 236 mojo::MakeRequest(&validation_delegate_ptr)); |
| 237 } |
| 238 prefs::mojom::ResetOnLoadObserverPtr reset_on_load_observer; |
| 239 if (on_reset_on_load) { |
| 240 mojo::MakeStrongBinding( |
| 241 base::MakeUnique<ResetOnLoadObserverImpl>(on_reset_on_load), |
| 242 mojo::MakeRequest(&reset_on_load_observer)); |
| 243 } |
| 244 config->set_tracked_configuration( |
| 245 prefs::mojom::TrackedPersistentPrefStoreConfiguration::New( |
| 246 profile_path_.Append(chrome::kPreferencesFilename), |
| 247 profile_path_.Append(chrome::kSecurePreferencesFilename), |
| 248 WrapTrackedPreferenceMetadata(tracking_configuration_), |
| 249 reporting_ids_count_, seed_, legacy_device_id_, |
| 250 #if defined(OS_WIN) |
| 251 g_preference_validation_registry_path_for_testing |
| 252 ? *g_preference_validation_registry_path_for_testing |
| 253 : install_static::GetRegistryPath(), |
| 254 #else |
| 255 base::string16(), |
| 256 #endif |
| 257 std::move(validation_delegate_ptr), |
| 258 std::move(reset_on_load_observer))); |
| 259 } |
| 260 prefs::mojom::PrefServiceControlPtr control; |
| 261 connector->BindInterface(prefs::mojom::kPrefStoreServiceName, &control); |
| 262 control->Init(std::move(config)); |
| 263 } |
OLD | NEW |