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/pref_service_syncable.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/file_path.h" |
| 9 #include "base/prefs/default_pref_store.h" |
| 10 #include "base/prefs/overlay_user_pref_store.h" |
| 11 #include "base/string_number_conversions.h" |
| 12 #include "base/value_conversions.h" |
| 13 #include "chrome/browser/prefs/pref_model_associator.h" |
| 14 #include "chrome/browser/prefs/pref_notifier_impl.h" |
| 15 #include "chrome/browser/prefs/pref_service_observer.h" |
| 16 #include "chrome/browser/prefs/pref_value_store.h" |
| 17 #include "chrome/browser/ui/prefs/prefs_tab_helper.h" |
| 18 #include "ui/base/l10n/l10n_util.h" |
| 19 |
| 20 namespace { |
| 21 |
| 22 // A helper function for RegisterLocalized*Pref that creates a Value* |
| 23 // based on a localized resource. Because we control the values in a |
| 24 // locale dll, this should always return a Value of the appropriate |
| 25 // type. |
| 26 Value* CreateLocaleDefaultValue(base::Value::Type type, |
| 27 int message_id) { |
| 28 const std::string& resource_string = l10n_util::GetStringUTF8(message_id); |
| 29 DCHECK(!resource_string.empty()); |
| 30 switch (type) { |
| 31 case Value::TYPE_BOOLEAN: { |
| 32 if ("true" == resource_string) |
| 33 return Value::CreateBooleanValue(true); |
| 34 if ("false" == resource_string) |
| 35 return Value::CreateBooleanValue(false); |
| 36 break; |
| 37 } |
| 38 |
| 39 case Value::TYPE_INTEGER: { |
| 40 int val; |
| 41 base::StringToInt(resource_string, &val); |
| 42 return Value::CreateIntegerValue(val); |
| 43 } |
| 44 |
| 45 case Value::TYPE_DOUBLE: { |
| 46 double val; |
| 47 base::StringToDouble(resource_string, &val); |
| 48 return Value::CreateDoubleValue(val); |
| 49 } |
| 50 |
| 51 case Value::TYPE_STRING: { |
| 52 return Value::CreateStringValue(resource_string); |
| 53 } |
| 54 |
| 55 default: { |
| 56 NOTREACHED() << |
| 57 "list and dictionary types cannot have default locale values"; |
| 58 } |
| 59 } |
| 60 NOTREACHED(); |
| 61 return Value::CreateNullValue(); |
| 62 } |
| 63 |
| 64 } // namespace |
| 65 |
| 66 PrefServiceSyncable::~PrefServiceSyncable() {} |
| 67 |
| 68 PrefServiceSyncable* PrefServiceSyncable::CreateIncognitoPrefService( |
| 69 PrefStore* incognito_extension_prefs) { |
| 70 pref_service_forked_ = true; |
| 71 PrefNotifierImpl* pref_notifier = new PrefNotifierImpl(); |
| 72 OverlayUserPrefStore* incognito_pref_store = |
| 73 new OverlayUserPrefStore(user_pref_store_.get()); |
| 74 PrefsTabHelper::InitIncognitoUserPrefStore(incognito_pref_store); |
| 75 PrefServiceSyncable* incognito_service = new PrefServiceSyncable(); |
| 76 incognito_service->Initialize( |
| 77 pref_notifier, |
| 78 pref_value_store_->CloneAndSpecialize( |
| 79 NULL, // managed |
| 80 incognito_extension_prefs, |
| 81 NULL, // command_line_prefs |
| 82 incognito_pref_store, |
| 83 NULL, // recommended |
| 84 default_store_.get(), |
| 85 pref_notifier), |
| 86 incognito_pref_store, |
| 87 default_store_.get(), |
| 88 read_error_callback_, |
| 89 false); |
| 90 return incognito_service; |
| 91 } |
| 92 |
| 93 bool PrefServiceSyncable::IsSyncing() { |
| 94 return pref_sync_associator_.models_associated(); |
| 95 } |
| 96 |
| 97 void PrefServiceSyncable::AddObserver(PrefServiceObserver* observer) { |
| 98 observer_list_.AddObserver(observer); |
| 99 } |
| 100 |
| 101 void PrefServiceSyncable::RemoveObserver(PrefServiceObserver* observer) { |
| 102 observer_list_.RemoveObserver(observer); |
| 103 } |
| 104 |
| 105 void PrefServiceSyncable::UnregisterPreference(const char* path) { |
| 106 PrefService::UnregisterPreference(path); |
| 107 if (pref_sync_associator_.IsPrefRegistered(path)) { |
| 108 pref_sync_associator_.UnregisterPref(path); |
| 109 } |
| 110 } |
| 111 |
| 112 void PrefServiceSyncable::RegisterBooleanPref(const char* path, |
| 113 bool default_value, |
| 114 PrefSyncStatus sync_status) { |
| 115 RegisterSyncablePreference(path, |
| 116 Value::CreateBooleanValue(default_value), |
| 117 sync_status); |
| 118 } |
| 119 |
| 120 void PrefServiceSyncable::RegisterIntegerPref(const char* path, |
| 121 int default_value, |
| 122 PrefSyncStatus sync_status) { |
| 123 RegisterSyncablePreference(path, |
| 124 Value::CreateIntegerValue(default_value), |
| 125 sync_status); |
| 126 } |
| 127 |
| 128 void PrefServiceSyncable::RegisterDoublePref(const char* path, |
| 129 double default_value, |
| 130 PrefSyncStatus sync_status) { |
| 131 RegisterSyncablePreference(path, |
| 132 Value::CreateDoubleValue(default_value), |
| 133 sync_status); |
| 134 } |
| 135 |
| 136 void PrefServiceSyncable::RegisterStringPref(const char* path, |
| 137 const std::string& default_value, |
| 138 PrefSyncStatus sync_status) { |
| 139 RegisterSyncablePreference(path, |
| 140 Value::CreateStringValue(default_value), |
| 141 sync_status); |
| 142 } |
| 143 |
| 144 void PrefServiceSyncable::RegisterFilePathPref(const char* path, |
| 145 const FilePath& default_value, |
| 146 PrefSyncStatus sync_status) { |
| 147 RegisterSyncablePreference(path, |
| 148 Value::CreateStringValue(default_value.value()), |
| 149 sync_status); |
| 150 } |
| 151 |
| 152 void PrefServiceSyncable::RegisterListPref(const char* path, |
| 153 PrefSyncStatus sync_status) { |
| 154 RegisterSyncablePreference(path, new ListValue(), sync_status); |
| 155 } |
| 156 |
| 157 void PrefServiceSyncable::RegisterListPref(const char* path, |
| 158 ListValue* default_value, |
| 159 PrefSyncStatus sync_status) { |
| 160 RegisterSyncablePreference(path, default_value, sync_status); |
| 161 } |
| 162 |
| 163 void PrefServiceSyncable::RegisterDictionaryPref(const char* path, |
| 164 PrefSyncStatus sync_status) { |
| 165 RegisterSyncablePreference(path, new DictionaryValue(), sync_status); |
| 166 } |
| 167 |
| 168 void PrefServiceSyncable::RegisterDictionaryPref(const char* path, |
| 169 DictionaryValue* default_value, |
| 170 PrefSyncStatus sync_status) { |
| 171 RegisterSyncablePreference(path, default_value, sync_status); |
| 172 } |
| 173 |
| 174 void PrefServiceSyncable::RegisterLocalizedBooleanPref( |
| 175 const char* path, |
| 176 int locale_default_message_id, |
| 177 PrefSyncStatus sync_status) { |
| 178 RegisterSyncablePreference( |
| 179 path, |
| 180 CreateLocaleDefaultValue(Value::TYPE_BOOLEAN, locale_default_message_id), |
| 181 sync_status); |
| 182 } |
| 183 |
| 184 void PrefServiceSyncable::RegisterLocalizedIntegerPref( |
| 185 const char* path, |
| 186 int locale_default_message_id, |
| 187 PrefSyncStatus sync_status) { |
| 188 RegisterSyncablePreference( |
| 189 path, |
| 190 CreateLocaleDefaultValue(Value::TYPE_INTEGER, locale_default_message_id), |
| 191 sync_status); |
| 192 } |
| 193 |
| 194 void PrefServiceSyncable::RegisterLocalizedDoublePref( |
| 195 const char* path, |
| 196 int locale_default_message_id, |
| 197 PrefSyncStatus sync_status) { |
| 198 RegisterSyncablePreference( |
| 199 path, |
| 200 CreateLocaleDefaultValue(Value::TYPE_DOUBLE, locale_default_message_id), |
| 201 sync_status); |
| 202 } |
| 203 |
| 204 void PrefServiceSyncable::RegisterLocalizedStringPref( |
| 205 const char* path, |
| 206 int locale_default_message_id, |
| 207 PrefSyncStatus sync_status) { |
| 208 RegisterSyncablePreference( |
| 209 path, |
| 210 CreateLocaleDefaultValue(Value::TYPE_STRING, locale_default_message_id), |
| 211 sync_status); |
| 212 } |
| 213 |
| 214 void PrefServiceSyncable::RegisterInt64Pref( |
| 215 const char* path, |
| 216 int64 default_value, |
| 217 PrefSyncStatus sync_status) { |
| 218 RegisterSyncablePreference( |
| 219 path, |
| 220 Value::CreateStringValue(base::Int64ToString(default_value)), |
| 221 sync_status); |
| 222 } |
| 223 |
| 224 void PrefServiceSyncable::RegisterUint64Pref( |
| 225 const char* path, |
| 226 uint64 default_value, |
| 227 PrefSyncStatus sync_status) { |
| 228 RegisterSyncablePreference( |
| 229 path, |
| 230 Value::CreateStringValue(base::Uint64ToString(default_value)), |
| 231 sync_status); |
| 232 } |
| 233 |
| 234 syncer::SyncableService* PrefServiceSyncable::GetSyncableService() { |
| 235 return &pref_sync_associator_; |
| 236 } |
| 237 |
| 238 void PrefServiceSyncable::UpdateCommandLinePrefStore( |
| 239 PrefStore* cmd_line_store) { |
| 240 // If |pref_service_forked_| is true, then this PrefService and the forked |
| 241 // copies will be out of sync. |
| 242 DCHECK(!pref_service_forked_); |
| 243 PrefService::UpdateCommandLinePrefStore(cmd_line_store); |
| 244 } |
| 245 |
| 246 PrefServiceSyncable::PrefServiceSyncable() { |
| 247 pref_sync_associator_.SetPrefService(this); |
| 248 } |
| 249 |
| 250 void PrefServiceSyncable::Initialize( |
| 251 PrefNotifierImpl* pref_notifier, |
| 252 PrefValueStore* pref_value_store, |
| 253 PersistentPrefStore* user_prefs, |
| 254 DefaultPrefStore* default_store, |
| 255 base::Callback<void(PersistentPrefStore::PrefReadError)> |
| 256 read_error_callback, |
| 257 bool async) { |
| 258 PrefService::Initialize(pref_notifier, |
| 259 pref_value_store, |
| 260 user_prefs, |
| 261 default_store, |
| 262 read_error_callback, |
| 263 async); |
| 264 |
| 265 pref_value_store->set_sync_associator(&pref_sync_associator_); |
| 266 } |
| 267 |
| 268 void PrefServiceSyncable::OnIsSyncingChanged() { |
| 269 FOR_EACH_OBSERVER(PrefServiceObserver, observer_list_, OnIsSyncingChanged()); |
| 270 } |
| 271 |
| 272 void PrefServiceSyncable::RegisterSyncablePreference( |
| 273 const char* path, Value* default_value, PrefSyncStatus sync_status) { |
| 274 PrefService::RegisterPreference(path, default_value); |
| 275 // Register with sync if necessary. |
| 276 if (sync_status == SYNCABLE_PREF) |
| 277 pref_sync_associator_.RegisterPref(path); |
| 278 } |
OLD | NEW |