| OLD | NEW |
| 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 "components/pref_registry/pref_registry_syncable.h" | 5 #include "components/pref_registry/pref_registry_syncable.h" |
| 6 | 6 |
| 7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
| 8 #include "base/prefs/default_pref_store.h" | 8 #include "base/prefs/default_pref_store.h" |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 | 11 |
| 12 namespace user_prefs { | 12 namespace user_prefs { |
| 13 | 13 |
| 14 PrefRegistrySyncable::PrefRegistrySyncable() { | 14 PrefRegistrySyncable::PrefRegistrySyncable() { |
| 15 } | 15 } |
| 16 | 16 |
| 17 PrefRegistrySyncable::~PrefRegistrySyncable() { | 17 PrefRegistrySyncable::~PrefRegistrySyncable() { |
| 18 } | 18 } |
| 19 | 19 |
| 20 const PrefRegistrySyncable::PrefToStatus& | |
| 21 PrefRegistrySyncable::syncable_preferences() const { | |
| 22 return syncable_preferences_; | |
| 23 } | |
| 24 | |
| 25 void PrefRegistrySyncable::SetSyncableRegistrationCallback( | 20 void PrefRegistrySyncable::SetSyncableRegistrationCallback( |
| 26 const SyncableRegistrationCallback& cb) { | 21 const SyncableRegistrationCallback& cb) { |
| 27 callback_ = cb; | 22 callback_ = cb; |
| 28 } | 23 } |
| 29 | 24 |
| 30 void PrefRegistrySyncable::RegisterBooleanPref(const char* path, | 25 void PrefRegistrySyncable::RegisterBooleanPref(const char* path, |
| 31 bool default_value, | 26 bool default_value, |
| 32 PrefSyncStatus sync_status) { | 27 uint32 flags) { |
| 33 RegisterSyncablePreference( | 28 RegisterSyncablePreference( |
| 34 path, new base::FundamentalValue(default_value), sync_status); | 29 path, new base::FundamentalValue(default_value), flags); |
| 35 } | 30 } |
| 36 | 31 |
| 37 void PrefRegistrySyncable::RegisterIntegerPref(const char* path, | 32 void PrefRegistrySyncable::RegisterIntegerPref(const char* path, |
| 38 int default_value, | 33 int default_value, |
| 39 PrefSyncStatus sync_status) { | 34 uint32 flags) { |
| 40 RegisterSyncablePreference( | 35 RegisterSyncablePreference( |
| 41 path, new base::FundamentalValue(default_value), sync_status); | 36 path, new base::FundamentalValue(default_value), flags); |
| 42 } | 37 } |
| 43 | 38 |
| 44 void PrefRegistrySyncable::RegisterDoublePref(const char* path, | 39 void PrefRegistrySyncable::RegisterDoublePref(const char* path, |
| 45 double default_value, | 40 double default_value, |
| 46 PrefSyncStatus sync_status) { | 41 uint32 flags) { |
| 47 RegisterSyncablePreference( | 42 RegisterSyncablePreference( |
| 48 path, new base::FundamentalValue(default_value), sync_status); | 43 path, new base::FundamentalValue(default_value), flags); |
| 49 } | 44 } |
| 50 | 45 |
| 51 void PrefRegistrySyncable::RegisterStringPref(const char* path, | 46 void PrefRegistrySyncable::RegisterStringPref(const char* path, |
| 52 const std::string& default_value, | 47 const std::string& default_value, |
| 53 PrefSyncStatus sync_status) { | 48 uint32 flags) { |
| 54 RegisterSyncablePreference( | 49 RegisterSyncablePreference( |
| 55 path, new base::StringValue(default_value), sync_status); | 50 path, new base::StringValue(default_value), flags); |
| 56 } | 51 } |
| 57 | 52 |
| 58 void PrefRegistrySyncable::RegisterFilePathPref( | 53 void PrefRegistrySyncable::RegisterFilePathPref( |
| 59 const char* path, | 54 const char* path, |
| 60 const base::FilePath& default_value, | 55 const base::FilePath& default_value, |
| 61 PrefSyncStatus sync_status) { | 56 uint32 flags) { |
| 62 RegisterSyncablePreference( | 57 RegisterSyncablePreference( |
| 63 path, new base::StringValue(default_value.value()), sync_status); | 58 path, new base::StringValue(default_value.value()), flags); |
| 64 } | 59 } |
| 65 | 60 |
| 66 void PrefRegistrySyncable::RegisterListPref(const char* path, | 61 void PrefRegistrySyncable::RegisterListPref(const char* path, uint32 flags) { |
| 67 PrefSyncStatus sync_status) { | 62 RegisterSyncablePreference(path, new base::ListValue(), flags); |
| 68 RegisterSyncablePreference(path, new base::ListValue(), sync_status); | |
| 69 } | 63 } |
| 70 | 64 |
| 71 void PrefRegistrySyncable::RegisterListPref(const char* path, | 65 void PrefRegistrySyncable::RegisterListPref(const char* path, |
| 72 base::ListValue* default_value, | 66 base::ListValue* default_value, |
| 73 PrefSyncStatus sync_status) { | 67 uint32 flags) { |
| 74 RegisterSyncablePreference(path, default_value, sync_status); | 68 RegisterSyncablePreference(path, default_value, flags); |
| 75 } | 69 } |
| 76 | 70 |
| 77 void PrefRegistrySyncable::RegisterDictionaryPref(const char* path, | 71 void PrefRegistrySyncable::RegisterDictionaryPref(const char* path, |
| 78 PrefSyncStatus sync_status) { | 72 uint32 flags) { |
| 79 RegisterSyncablePreference(path, new base::DictionaryValue(), sync_status); | 73 RegisterSyncablePreference(path, new base::DictionaryValue(), flags); |
| 80 } | 74 } |
| 81 | 75 |
| 82 void PrefRegistrySyncable::RegisterDictionaryPref( | 76 void PrefRegistrySyncable::RegisterDictionaryPref( |
| 83 const char* path, | 77 const char* path, |
| 84 base::DictionaryValue* default_value, | 78 base::DictionaryValue* default_value, |
| 85 PrefSyncStatus sync_status) { | 79 uint32 flags) { |
| 86 RegisterSyncablePreference(path, default_value, sync_status); | 80 RegisterSyncablePreference(path, default_value, flags); |
| 87 } | 81 } |
| 88 | 82 |
| 89 void PrefRegistrySyncable::RegisterInt64Pref( | 83 void PrefRegistrySyncable::RegisterInt64Pref( |
| 90 const char* path, | 84 const char* path, |
| 91 int64 default_value, | 85 int64 default_value, |
| 92 PrefSyncStatus sync_status) { | 86 uint32 flags) { |
| 93 RegisterSyncablePreference( | 87 RegisterSyncablePreference( |
| 94 path, | 88 path, |
| 95 new base::StringValue(base::Int64ToString(default_value)), | 89 new base::StringValue(base::Int64ToString(default_value)), |
| 96 sync_status); | 90 flags); |
| 97 } | 91 } |
| 98 | 92 |
| 99 void PrefRegistrySyncable::RegisterUint64Pref( | 93 void PrefRegistrySyncable::RegisterUint64Pref( |
| 100 const char* path, | 94 const char* path, |
| 101 uint64 default_value, | 95 uint64 default_value, |
| 102 PrefSyncStatus sync_status) { | 96 uint32 flags) { |
| 103 RegisterSyncablePreference( | 97 RegisterSyncablePreference( |
| 104 path, | 98 path, |
| 105 new base::StringValue(base::Uint64ToString(default_value)), | 99 new base::StringValue(base::Uint64ToString(default_value)), |
| 106 sync_status); | 100 flags); |
| 107 } | 101 } |
| 108 | 102 |
| 109 void PrefRegistrySyncable::RegisterSyncablePreference( | 103 void PrefRegistrySyncable::RegisterSyncablePreference( |
| 110 const char* path, | 104 const char* path, |
| 111 base::Value* default_value, | 105 base::Value* default_value, |
| 112 PrefSyncStatus sync_status) { | 106 uint32 flags) { |
| 113 PrefRegistry::RegisterPreference(path, default_value); | 107 // Tests that |flags| does not contain both SYNCABLE_PREF and |
| 108 // SYNCABLE_PRIORITY_PREF flags at the same time. |
| 109 DCHECK(!(flags & PrefRegistrySyncable::SYNCABLE_PREF) || |
| 110 !(flags & PrefRegistrySyncable::SYNCABLE_PRIORITY_PREF)); |
| 111 PrefRegistry::RegisterPreference(path, default_value, flags); |
| 114 | 112 |
| 115 if (sync_status == PrefRegistrySyncable::SYNCABLE_PREF || | 113 if (flags & PrefRegistrySyncable::SYNCABLE_PREF || |
| 116 sync_status == PrefRegistrySyncable::SYNCABLE_PRIORITY_PREF) { | 114 flags & PrefRegistrySyncable::SYNCABLE_PRIORITY_PREF) { |
| 117 syncable_preferences_[path] = sync_status; | |
| 118 | |
| 119 if (!callback_.is_null()) | 115 if (!callback_.is_null()) |
| 120 callback_.Run(path, sync_status); | 116 callback_.Run(path, flags); |
| 121 } | 117 } |
| 122 } | 118 } |
| 123 | 119 |
| 124 scoped_refptr<PrefRegistrySyncable> PrefRegistrySyncable::ForkForIncognito() { | 120 scoped_refptr<PrefRegistrySyncable> PrefRegistrySyncable::ForkForIncognito() { |
| 125 // TODO(joi): We can directly reuse the same PrefRegistry once | 121 // TODO(joi): We can directly reuse the same PrefRegistry once |
| 126 // PrefService no longer registers for callbacks on registration and | 122 // PrefService no longer registers for callbacks on registration and |
| 127 // unregistration. | 123 // unregistration. |
| 128 scoped_refptr<PrefRegistrySyncable> registry(new PrefRegistrySyncable()); | 124 scoped_refptr<PrefRegistrySyncable> registry(new PrefRegistrySyncable()); |
| 129 registry->defaults_ = defaults_; | 125 registry->defaults_ = defaults_; |
| 130 return registry; | 126 return registry; |
| 131 } | 127 } |
| 132 | 128 |
| 133 } // namespace user_prefs | 129 } // namespace user_prefs |
| OLD | NEW |