pref_registry_syncable.cc revision 116680a4aac90f2aa7413d9095a592090648e557
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 "components/pref_registry/pref_registry_syncable.h"
6
7#include "base/files/file_path.h"
8#include "base/prefs/default_pref_store.h"
9#include "base/strings/string_number_conversions.h"
10#include "base/values.h"
11#include "ui/base/l10n/l10n_util.h"
12
13namespace user_prefs {
14
15namespace {
16
17// A helper function for RegisterLocalized*Pref that creates a Value*
18// based on a localized resource.  Because we control the values in a
19// locale dll, this should always return a Value of the appropriate
20// type.
21base::Value* CreateLocaleDefaultValue(base::Value::Type type,
22                                      int message_id) {
23  const std::string resource_string = l10n_util::GetStringUTF8(message_id);
24  DCHECK(!resource_string.empty());
25  switch (type) {
26    case base::Value::TYPE_BOOLEAN: {
27      if ("true" == resource_string)
28        return new base::FundamentalValue(true);
29      if ("false" == resource_string)
30        return new base::FundamentalValue(false);
31      break;
32    }
33
34    case base::Value::TYPE_INTEGER: {
35      int val;
36      base::StringToInt(resource_string, &val);
37      return new base::FundamentalValue(val);
38    }
39
40    case base::Value::TYPE_DOUBLE: {
41      double val;
42      base::StringToDouble(resource_string, &val);
43      return base::Value::CreateDoubleValue(val);
44    }
45
46    case base::Value::TYPE_STRING: {
47      return base::Value::CreateStringValue(resource_string);
48    }
49
50    default: {
51      NOTREACHED() <<
52          "list and dictionary types cannot have default locale values";
53    }
54  }
55  NOTREACHED();
56  return base::Value::CreateNullValue();
57}
58
59}  // namespace
60
61PrefRegistrySyncable::PrefRegistrySyncable() {
62}
63
64PrefRegistrySyncable::~PrefRegistrySyncable() {
65}
66
67const PrefRegistrySyncable::PrefToStatus&
68PrefRegistrySyncable::syncable_preferences() const {
69  return syncable_preferences_;
70}
71
72void PrefRegistrySyncable::SetSyncableRegistrationCallback(
73    const SyncableRegistrationCallback& cb) {
74  callback_ = cb;
75}
76
77void PrefRegistrySyncable::RegisterBooleanPref(const char* path,
78                                               bool default_value,
79                                               PrefSyncStatus sync_status) {
80  RegisterSyncablePreference(
81      path, new base::FundamentalValue(default_value), sync_status);
82}
83
84void PrefRegistrySyncable::RegisterIntegerPref(const char* path,
85                                               int default_value,
86                                               PrefSyncStatus sync_status) {
87  RegisterSyncablePreference(
88      path, new base::FundamentalValue(default_value), sync_status);
89}
90
91void PrefRegistrySyncable::RegisterDoublePref(const char* path,
92                                              double default_value,
93                                              PrefSyncStatus sync_status) {
94  RegisterSyncablePreference(path,
95                             base::Value::CreateDoubleValue(default_value),
96                             sync_status);
97}
98
99void PrefRegistrySyncable::RegisterStringPref(const char* path,
100                                              const std::string& default_value,
101                                              PrefSyncStatus sync_status) {
102  RegisterSyncablePreference(path,
103                             base::Value::CreateStringValue(default_value),
104                             sync_status);
105}
106
107void PrefRegistrySyncable::RegisterFilePathPref(
108    const char* path,
109    const base::FilePath& default_value,
110    PrefSyncStatus sync_status) {
111  RegisterSyncablePreference(path,
112                             base::Value::CreateStringValue(
113                                 default_value.value()),
114                             sync_status);
115}
116
117void PrefRegistrySyncable::RegisterListPref(const char* path,
118                                            PrefSyncStatus sync_status) {
119  RegisterSyncablePreference(path, new base::ListValue(), sync_status);
120}
121
122void PrefRegistrySyncable::RegisterListPref(const char* path,
123                                            base::ListValue* default_value,
124                                            PrefSyncStatus sync_status) {
125  RegisterSyncablePreference(path, default_value, sync_status);
126}
127
128void PrefRegistrySyncable::RegisterDictionaryPref(const char* path,
129                                                  PrefSyncStatus sync_status) {
130  RegisterSyncablePreference(path, new base::DictionaryValue(), sync_status);
131}
132
133void PrefRegistrySyncable::RegisterDictionaryPref(
134    const char* path,
135    base::DictionaryValue* default_value,
136    PrefSyncStatus sync_status) {
137  RegisterSyncablePreference(path, default_value, sync_status);
138}
139
140void PrefRegistrySyncable::RegisterLocalizedBooleanPref(
141    const char* path,
142    int locale_default_message_id,
143    PrefSyncStatus sync_status) {
144  RegisterSyncablePreference(
145      path,
146      CreateLocaleDefaultValue(base::Value::TYPE_BOOLEAN,
147                               locale_default_message_id),
148      sync_status);
149}
150
151void PrefRegistrySyncable::RegisterLocalizedIntegerPref(
152    const char* path,
153    int locale_default_message_id,
154    PrefSyncStatus sync_status) {
155  RegisterSyncablePreference(
156      path,
157      CreateLocaleDefaultValue(base::Value::TYPE_INTEGER,
158                               locale_default_message_id),
159      sync_status);
160}
161
162void PrefRegistrySyncable::RegisterLocalizedDoublePref(
163    const char* path,
164    int locale_default_message_id,
165    PrefSyncStatus sync_status) {
166  RegisterSyncablePreference(
167      path,
168      CreateLocaleDefaultValue(base::Value::TYPE_DOUBLE,
169                               locale_default_message_id),
170      sync_status);
171}
172
173void PrefRegistrySyncable::RegisterLocalizedStringPref(
174    const char* path,
175    int locale_default_message_id,
176    PrefSyncStatus sync_status) {
177  RegisterSyncablePreference(
178      path,
179      CreateLocaleDefaultValue(base::Value::TYPE_STRING,
180                               locale_default_message_id),
181      sync_status);
182}
183
184void PrefRegistrySyncable::RegisterInt64Pref(
185    const char* path,
186    int64 default_value,
187    PrefSyncStatus sync_status) {
188  RegisterSyncablePreference(
189      path,
190      base::Value::CreateStringValue(base::Int64ToString(default_value)),
191      sync_status);
192}
193
194void PrefRegistrySyncable::RegisterUint64Pref(
195    const char* path,
196    uint64 default_value,
197    PrefSyncStatus sync_status) {
198  RegisterSyncablePreference(
199      path,
200      base::Value::CreateStringValue(base::Uint64ToString(default_value)),
201      sync_status);
202}
203
204void PrefRegistrySyncable::RegisterSyncablePreference(
205    const char* path,
206    base::Value* default_value,
207    PrefSyncStatus sync_status) {
208  PrefRegistry::RegisterPreference(path, default_value);
209
210  if (sync_status == PrefRegistrySyncable::SYNCABLE_PREF ||
211      sync_status == PrefRegistrySyncable::SYNCABLE_PRIORITY_PREF) {
212    syncable_preferences_[path] = sync_status;
213
214    if (!callback_.is_null())
215      callback_.Run(path, sync_status);
216  }
217}
218
219scoped_refptr<PrefRegistrySyncable> PrefRegistrySyncable::ForkForIncognito() {
220  // TODO(joi): We can directly reuse the same PrefRegistry once
221  // PrefService no longer registers for callbacks on registration and
222  // unregistration.
223  scoped_refptr<PrefRegistrySyncable> registry(new PrefRegistrySyncable());
224  registry->defaults_ = defaults_;
225  return registry;
226}
227
228}  // namespace user_prefs
229