Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(260)

Side by Side Diff: chrome/browser/prefs/chrome_pref_service_factory.cc

Issue 154593002: Refactor pref code out of ProfileImpl. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: merge up to r249119 Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/chrome_pref_service_factory.h" 5 #include "chrome/browser/prefs/chrome_pref_service_factory.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/compiler_specific.h" 8 #include "base/compiler_specific.h"
9 #include "base/debug/trace_event.h" 9 #include "base/debug/trace_event.h"
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
11 #include "base/json/json_file_value_serializer.h"
11 #include "base/metrics/field_trial.h" 12 #include "base/metrics/field_trial.h"
12 #include "base/metrics/histogram.h" 13 #include "base/metrics/histogram.h"
13 #include "base/prefs/default_pref_store.h" 14 #include "base/prefs/default_pref_store.h"
14 #include "base/prefs/json_pref_store.h" 15 #include "base/prefs/json_pref_store.h"
15 #include "base/prefs/pref_filter.h" 16 #include "base/prefs/pref_filter.h"
16 #include "base/prefs/pref_notifier_impl.h" 17 #include "base/prefs/pref_notifier_impl.h"
17 #include "base/prefs/pref_registry.h" 18 #include "base/prefs/pref_registry.h"
18 #include "base/prefs/pref_service.h" 19 #include "base/prefs/pref_service.h"
19 #include "base/prefs/pref_store.h" 20 #include "base/prefs/pref_store.h"
20 #include "base/prefs/pref_value_store.h" 21 #include "base/prefs/pref_value_store.h"
22 #include "base/threading/sequenced_worker_pool.h"
23 #include "base/time/time.h"
21 #include "chrome/browser/browser_process.h" 24 #include "chrome/browser/browser_process.h"
22 #include "chrome/browser/prefs/command_line_pref_store.h" 25 #include "chrome/browser/prefs/command_line_pref_store.h"
23 #include "chrome/browser/prefs/pref_hash_filter.h" 26 #include "chrome/browser/prefs/pref_hash_filter.h"
24 #include "chrome/browser/prefs/pref_hash_store.h" 27 #include "chrome/browser/prefs/pref_hash_store_impl.h"
25 #include "chrome/browser/prefs/pref_model_associator.h" 28 #include "chrome/browser/prefs/pref_model_associator.h"
26 #include "chrome/browser/prefs/pref_service_syncable.h" 29 #include "chrome/browser/prefs/pref_service_syncable.h"
27 #include "chrome/browser/prefs/pref_service_syncable_factory.h" 30 #include "chrome/browser/prefs/pref_service_syncable_factory.h"
31 #include "chrome/browser/profiles/file_path_verifier_win.h"
28 #include "chrome/browser/ui/profile_error_dialog.h" 32 #include "chrome/browser/ui/profile_error_dialog.h"
33 #include "chrome/common/chrome_constants.h"
29 #include "chrome/common/pref_names.h" 34 #include "chrome/common/pref_names.h"
30 #include "components/user_prefs/pref_registry_syncable.h" 35 #include "components/user_prefs/pref_registry_syncable.h"
31 #include "content/public/browser/browser_context.h" 36 #include "content/public/browser/browser_context.h"
32 #include "content/public/browser/browser_thread.h" 37 #include "content/public/browser/browser_thread.h"
33 #include "extensions/browser/pref_names.h" 38 #include "extensions/browser/pref_names.h"
39 #include "grit/browser_resources.h"
34 #include "grit/chromium_strings.h" 40 #include "grit/chromium_strings.h"
35 #include "grit/generated_resources.h" 41 #include "grit/generated_resources.h"
42 #include "ui/base/resource/resource_bundle.h"
36 43
37 #if defined(ENABLE_CONFIGURATION_POLICY) 44 #if defined(ENABLE_CONFIGURATION_POLICY)
38 #include "components/policy/core/browser/browser_policy_connector.h" 45 #include "components/policy/core/browser/browser_policy_connector.h"
39 #include "components/policy/core/browser/configuration_policy_pref_store.h" 46 #include "components/policy/core/browser/configuration_policy_pref_store.h"
40 #include "components/policy/core/common/policy_types.h" 47 #include "components/policy/core/common/policy_types.h"
41 #endif 48 #endif
42 49
43 #if defined(ENABLE_MANAGED_USERS) 50 #if defined(ENABLE_MANAGED_USERS)
44 #include "chrome/browser/managed_mode/supervised_user_pref_store.h" 51 #include "chrome/browser/managed_mode/supervised_user_pref_store.h"
45 #endif 52 #endif
46 53
54 #if defined(OS_WIN) && defined(ENABLE_RLZ)
55 #include "rlz/lib/machine_id.h"
56 #endif
57
47 using content::BrowserContext; 58 using content::BrowserContext;
48 using content::BrowserThread; 59 using content::BrowserThread;
49 60
50 namespace { 61 namespace {
51 62
52 // These preferences must be kept in sync with the TrackedPreference enum in 63 // These preferences must be kept in sync with the TrackedPreference enum in
53 // tools/metrics/histograms/histograms.xml. To add a new preference, append it 64 // tools/metrics/histograms/histograms.xml. To add a new preference, append it
54 // to the array and add a corresponding value to the histogram enum. Each 65 // to the array and add a corresponding value to the histogram enum. Each
55 // tracked preference must be given a unique reporting ID. 66 // tracked preference must be given a unique reporting ID.
56 const PrefHashFilter::TrackedPreferenceMetadata kTrackedPrefs[] = { 67 const PrefHashFilter::TrackedPreferenceMetadata kTrackedPrefs[] = {
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 PROFILE_ERROR_PREFERENCES, 200 PROFILE_ERROR_PREFERENCES,
190 message_id)); 201 message_id));
191 } 202 }
192 #else 203 #else
193 // On ChromeOS error screen with message about broken local state 204 // On ChromeOS error screen with message about broken local state
194 // will be displayed. 205 // will be displayed.
195 #endif 206 #endif
196 } 207 }
197 } 208 }
198 209
210 base::FilePath GetPrefFilePathFromProfilePath(
211 const base::FilePath& profile_path) {
212 return profile_path.Append(chrome::kPreferencesFilename);
213 }
214
215 // Returns the PrefHashStoreImpl for the profile at |profile_path|; may be NULL
216 // on some platforms.
217 scoped_ptr<PrefHashStoreImpl> GetPrefHashStoreImpl(
218 const base::FilePath& profile_path) {
219 // TODO(erikwright): Enable this on Android when race condition is sorted out.
220 #if defined(OS_ANDROID)
221 return scoped_ptr<PrefHashStoreImpl>();
222 #else
223 std::string seed = ResourceBundle::GetSharedInstance().GetRawDataResource(
224 IDR_PREF_HASH_SEED_BIN).as_string();
225 std::string device_id;
226
227 #if defined(OS_WIN) && defined(ENABLE_RLZ)
228 // This is used by
229 // chrome/browser/extensions/api/music_manager_private/device_id_win.cc
230 // but that API is private (http://crbug.com/276485) and other platforms are
231 // not available synchronously.
232 // As part of improving pref metrics on other platforms we may want to find
233 // ways to defer preference loading until the device ID can be used.
234 rlz_lib::GetMachineId(&device_id);
235 #endif
236
237 return make_scoped_ptr(new PrefHashStoreImpl(
238 profile_path.AsUTF8Unsafe(),
239 seed,
240 device_id,
241 g_browser_process->local_state()));
242 #endif
243 }
244
199 scoped_ptr<PrefHashFilter> CreatePrefHashFilter( 245 scoped_ptr<PrefHashFilter> CreatePrefHashFilter(
200 scoped_ptr<PrefHashStore> pref_hash_store) { 246 scoped_ptr<PrefHashStore> pref_hash_store) {
201 return make_scoped_ptr(new PrefHashFilter(pref_hash_store.Pass(), 247 return make_scoped_ptr(new PrefHashFilter(pref_hash_store.Pass(),
202 kTrackedPrefs, 248 kTrackedPrefs,
203 arraysize(kTrackedPrefs), 249 arraysize(kTrackedPrefs),
204 kTrackedPrefsReportingIDsCount, 250 kTrackedPrefsReportingIDsCount,
205 GetSettingsEnforcementLevel())); 251 GetSettingsEnforcementLevel()));
206 } 252 }
207 253
208 void PrepareBuilder( 254 void PrepareBuilder(
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
304 pref_io_task_runner, 350 pref_io_task_runner,
305 policy_service, 351 policy_service,
306 NULL, 352 NULL,
307 scoped_ptr<PrefHashStore>(), 353 scoped_ptr<PrefHashStore>(),
308 NULL, 354 NULL,
309 async); 355 async);
310 return factory.Create(pref_registry.get()); 356 return factory.Create(pref_registry.get());
311 } 357 }
312 358
313 scoped_ptr<PrefServiceSyncable> CreateProfilePrefs( 359 scoped_ptr<PrefServiceSyncable> CreateProfilePrefs(
314 const base::FilePath& pref_filename, 360 const base::FilePath& profile_path,
315 base::SequencedTaskRunner* pref_io_task_runner, 361 base::SequencedTaskRunner* pref_io_task_runner,
316 policy::PolicyService* policy_service, 362 policy::PolicyService* policy_service,
317 ManagedUserSettingsService* managed_user_settings, 363 ManagedUserSettingsService* managed_user_settings,
318 scoped_ptr<PrefHashStore> pref_hash_store,
319 const scoped_refptr<PrefStore>& extension_prefs, 364 const scoped_refptr<PrefStore>& extension_prefs,
320 const scoped_refptr<user_prefs::PrefRegistrySyncable>& pref_registry, 365 const scoped_refptr<user_prefs::PrefRegistrySyncable>& pref_registry,
321 bool async) { 366 bool async) {
322 TRACE_EVENT0("browser", "chrome_prefs::CreateProfilePrefs"); 367 TRACE_EVENT0("browser", "chrome_prefs::CreateProfilePrefs");
323 PrefServiceSyncableFactory factory; 368 PrefServiceSyncableFactory factory;
324 PrepareBuilder(&factory, 369 PrepareBuilder(&factory,
325 pref_filename, 370 GetPrefFilePathFromProfilePath(profile_path),
326 pref_io_task_runner, 371 pref_io_task_runner,
327 policy_service, 372 policy_service,
328 managed_user_settings, 373 managed_user_settings,
329 pref_hash_store.Pass(), 374 GetPrefHashStoreImpl(profile_path).PassAs<PrefHashStore>(),
330 extension_prefs, 375 extension_prefs,
331 async); 376 async);
332 return factory.CreateSyncable(pref_registry.get()); 377 return factory.CreateSyncable(pref_registry.get());
333 } 378 }
334 379
335 void InitializeHashStoreForPrefFile( 380 void SchedulePrefsFilePathVerification(const base::FilePath& profile_path) {
336 const base::FilePath& pref_filename, 381 #if defined(OS_WIN)
337 base::SequencedTaskRunner* pref_io_task_runner, 382 // Only do prefs file verification on Windows.
338 scoped_ptr<PrefHashStore> pref_hash_store) { 383 const int kVerifyPrefsFileDelaySeconds = 60;
339 scoped_refptr<JsonPrefStore> pref_store( 384 BrowserThread::GetBlockingPool()->PostDelayedTask(
340 new JsonPrefStore(pref_filename, 385 FROM_HERE,
341 pref_io_task_runner, 386 base::Bind(&VerifyPreferencesFile,
342 scoped_ptr<PrefFilter>())); 387 GetPrefFilePathFromProfilePath(profile_path)),
343 pref_store->AddObserver( 388 base::TimeDelta::FromSeconds(kVerifyPrefsFileDelaySeconds));
344 new InitializeHashStoreObserver(pref_store, pref_hash_store.Pass())); 389 #endif
345 pref_store->ReadPrefsAsync(NULL); 390 }
391
392 void InitializePrefHashStoreIfRequired(
393 const base::FilePath& profile_path) {
394 scoped_ptr<PrefHashStoreImpl> pref_hash_store(
395 GetPrefHashStoreImpl(profile_path));
396 if (pref_hash_store && !pref_hash_store->IsInitialized()) {
397 const base::FilePath pref_file(
398 GetPrefFilePathFromProfilePath(profile_path));
399 scoped_refptr<JsonPrefStore> pref_store(
400 new JsonPrefStore(pref_file,
401 JsonPrefStore::GetTaskRunnerForFile(
402 pref_file, BrowserThread::GetBlockingPool()),
403 scoped_ptr<PrefFilter>()));
404 pref_store->AddObserver(
405 new InitializeHashStoreObserver(
406 pref_store, pref_hash_store.PassAs<PrefHashStore>()));
407 pref_store->ReadPrefsAsync(NULL);
408 }
409 }
410
411 void ResetPrefHashStore(const base::FilePath& profile_path) {
412 GetPrefHashStoreImpl(profile_path)->Reset();
346 } 413 }
347 414
348 } // namespace chrome_prefs 415 } // namespace chrome_prefs
OLDNEW
« no previous file with comments | « chrome/browser/prefs/chrome_pref_service_factory.h ('k') | chrome/browser/prefs/pref_hash_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698