| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/prefs/chrome_pref_service_builder.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/file_path.h" | |
| 9 #include "base/file_util.h" | |
| 10 #include "base/metrics/histogram.h" | |
| 11 #include "base/prefs/default_pref_store.h" | |
| 12 #include "base/prefs/json_pref_store.h" | |
| 13 #include "chrome/browser/policy/configuration_policy_pref_store.h" | |
| 14 #include "chrome/browser/prefs/command_line_pref_store.h" | |
| 15 #include "chrome/browser/prefs/pref_model_associator.h" | |
| 16 #include "chrome/browser/prefs/pref_notifier_impl.h" | |
| 17 #include "chrome/browser/prefs/pref_service.h" | |
| 18 #include "chrome/browser/prefs/pref_value_store.h" | |
| 19 #include "chrome/browser/profiles/profile.h" | |
| 20 #include "chrome/browser/ui/profile_error_dialog.h" | |
| 21 #include "content/public/browser/browser_context.h" | |
| 22 #include "content/public/browser/browser_thread.h" | |
| 23 #include "grit/chromium_strings.h" | |
| 24 #include "grit/generated_resources.h" | |
| 25 #include "ui/base/l10n/l10n_util.h" | |
| 26 | |
| 27 using content::BrowserContext; | |
| 28 using content::BrowserThread; | |
| 29 | |
| 30 namespace { | |
| 31 | |
| 32 // Forwards a notification after a PostMessage so that we can wait for the | |
| 33 // MessageLoop to run. | |
| 34 void NotifyReadError(int message_id) { | |
| 35 ShowProfileErrorDialog(message_id); | |
| 36 } | |
| 37 | |
| 38 // Shows notifications which correspond to PersistentPrefStore's reading errors. | |
| 39 void HandleReadError(PersistentPrefStore::PrefReadError error) { | |
| 40 if (error != PersistentPrefStore::PREF_READ_ERROR_NONE) { | |
| 41 // Failing to load prefs on startup is a bad thing(TM). See bug 38352 for | |
| 42 // an example problem that this can cause. | |
| 43 // Do some diagnosis and try to avoid losing data. | |
| 44 int message_id = 0; | |
| 45 if (error <= PersistentPrefStore::PREF_READ_ERROR_JSON_TYPE) { | |
| 46 message_id = IDS_PREFERENCES_CORRUPT_ERROR; | |
| 47 } else if (error != PersistentPrefStore::PREF_READ_ERROR_NO_FILE) { | |
| 48 message_id = IDS_PREFERENCES_UNREADABLE_ERROR; | |
| 49 } | |
| 50 | |
| 51 if (message_id) { | |
| 52 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 53 base::Bind(&NotifyReadError, message_id)); | |
| 54 } | |
| 55 UMA_HISTOGRAM_ENUMERATION("PrefService.ReadError", error, | |
| 56 PersistentPrefStore::PREF_READ_ERROR_MAX_ENUM); | |
| 57 } | |
| 58 } | |
| 59 | |
| 60 } // namespace | |
| 61 | |
| 62 // TODO(joi): Find a better home for this. | |
| 63 PrefServiceBase* PrefServiceBase::FromBrowserContext(BrowserContext* context) { | |
| 64 return static_cast<Profile*>(context)->GetPrefs(); | |
| 65 } | |
| 66 | |
| 67 ChromePrefServiceBuilder::ChromePrefServiceBuilder() { | |
| 68 ResetDefaultState(); | |
| 69 } | |
| 70 | |
| 71 ChromePrefServiceBuilder::~ChromePrefServiceBuilder() { | |
| 72 } | |
| 73 | |
| 74 PrefService* ChromePrefServiceBuilder::CreateChromePrefs( | |
| 75 const FilePath& pref_filename, | |
| 76 base::SequencedTaskRunner* pref_io_task_runner, | |
| 77 policy::PolicyService* policy_service, | |
| 78 PrefStore* extension_prefs, | |
| 79 bool async) { | |
| 80 #if defined(OS_LINUX) | |
| 81 // We'd like to see what fraction of our users have the preferences | |
| 82 // stored on a network file system, as we've had no end of troubles | |
| 83 // with NFS/AFS. | |
| 84 // TODO(evanm): remove this once we've collected state. | |
| 85 file_util::FileSystemType fstype; | |
| 86 if (file_util::GetFileSystemType(pref_filename.DirName(), &fstype)) { | |
| 87 UMA_HISTOGRAM_ENUMERATION("PrefService.FileSystemType", | |
| 88 static_cast<int>(fstype), | |
| 89 file_util::FILE_SYSTEM_TYPE_COUNT); | |
| 90 } | |
| 91 #endif | |
| 92 | |
| 93 #if defined(ENABLE_CONFIGURATION_POLICY) | |
| 94 using policy::ConfigurationPolicyPrefStore; | |
| 95 WithManagedPrefs( | |
| 96 ConfigurationPolicyPrefStore::CreateMandatoryPolicyPrefStore( | |
| 97 policy_service)); | |
| 98 WithRecommendedPrefs( | |
| 99 ConfigurationPolicyPrefStore::CreateRecommendedPolicyPrefStore( | |
| 100 policy_service)); | |
| 101 #endif // ENABLE_CONFIGURATION_POLICY | |
| 102 | |
| 103 WithAsync(async); | |
| 104 WithCommandLinePrefs( | |
| 105 new CommandLinePrefStore(CommandLine::ForCurrentProcess())); | |
| 106 WithUserPrefs(new JsonPrefStore(pref_filename, pref_io_task_runner)); | |
| 107 | |
| 108 PrefService* pref_service = Create(); | |
| 109 ResetDefaultState(); | |
| 110 return pref_service; | |
| 111 } | |
| 112 | |
| 113 void ChromePrefServiceBuilder::ResetDefaultState() { | |
| 114 WithLocalizedStringMethod(base::Bind(&l10n_util::GetStringUTF8)); | |
| 115 WithReadErrorCallback(base::Bind(&HandleReadError)); | |
| 116 WithSyncAssociator(new PrefModelAssociator()); | |
| 117 } | |
| OLD | NEW |