summaryrefslogtreecommitdiffstats
path: root/components/user_prefs/pref_registry_syncable.cc
blob: 874f8a679d113096639d4723fb599b624efbdde6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/user_prefs/pref_registry_syncable.h"

#include "base/files/file_path.h"
#include "base/prefs/default_pref_store.h"
#include "base/string_number_conversions.h"
#include "base/values.h"
#include "ui/base/l10n/l10n_util.h"

namespace {

// A helper function for RegisterLocalized*Pref that creates a Value*
// based on a localized resource.  Because we control the values in a
// locale dll, this should always return a Value of the appropriate
// type.
base::Value* CreateLocaleDefaultValue(base::Value::Type type,
                                      int message_id) {
  const std::string resource_string = l10n_util::GetStringUTF8(message_id);
  DCHECK(!resource_string.empty());
  switch (type) {
    case Value::TYPE_BOOLEAN: {
      if ("true" == resource_string)
        return Value::CreateBooleanValue(true);
      if ("false" == resource_string)
        return Value::CreateBooleanValue(false);
      break;
    }

    case Value::TYPE_INTEGER: {
      int val;
      base::StringToInt(resource_string, &val);
      return Value::CreateIntegerValue(val);
    }

    case Value::TYPE_DOUBLE: {
      double val;
      base::StringToDouble(resource_string, &val);
      return Value::CreateDoubleValue(val);
    }

    case Value::TYPE_STRING: {
      return Value::CreateStringValue(resource_string);
    }

    default: {
      NOTREACHED() <<
          "list and dictionary types cannot have default locale values";
    }
  }
  NOTREACHED();
  return Value::CreateNullValue();
}

}  // namespace

PrefRegistrySyncable::PrefRegistrySyncable() {
}

PrefRegistrySyncable::~PrefRegistrySyncable() {
}

const std::set<std::string>&
PrefRegistrySyncable::syncable_preferences() const {
  return syncable_preferences_;
}

void PrefRegistrySyncable::SetSyncableRegistrationCallback(
    const SyncableRegistrationCallback& cb) {
  callback_ = cb;
}

void PrefRegistrySyncable::RegisterBooleanPref(const char* path,
                                               bool default_value,
                                               PrefSyncStatus sync_status) {
  RegisterSyncablePreference(path,
                             Value::CreateBooleanValue(default_value),
                             sync_status);
}

void PrefRegistrySyncable::RegisterIntegerPref(const char* path,
                                               int default_value,
                                               PrefSyncStatus sync_status) {
  RegisterSyncablePreference(path,
                             Value::CreateIntegerValue(default_value),
                             sync_status);
}

void PrefRegistrySyncable::RegisterDoublePref(const char* path,
                                              double default_value,
                                              PrefSyncStatus sync_status) {
  RegisterSyncablePreference(path,
                             Value::CreateDoubleValue(default_value),
                             sync_status);
}

void PrefRegistrySyncable::RegisterStringPref(const char* path,
                                              const std::string& default_value,
                                              PrefSyncStatus sync_status) {
  RegisterSyncablePreference(path,
                             Value::CreateStringValue(default_value),
                             sync_status);
}

void PrefRegistrySyncable::RegisterFilePathPref(
    const char* path,
    const base::FilePath& default_value,
    PrefSyncStatus sync_status) {
  RegisterSyncablePreference(path,
                             Value::CreateStringValue(default_value.value()),
                             sync_status);
}

void PrefRegistrySyncable::RegisterListPref(const char* path,
                                            PrefSyncStatus sync_status) {
  RegisterSyncablePreference(path, new ListValue(), sync_status);
}

void PrefRegistrySyncable::RegisterListPref(const char* path,
                                            ListValue* default_value,
                                            PrefSyncStatus sync_status) {
  RegisterSyncablePreference(path, default_value, sync_status);
}

void PrefRegistrySyncable::RegisterDictionaryPref(const char* path,
                                                  PrefSyncStatus sync_status) {
  RegisterSyncablePreference(path, new DictionaryValue(), sync_status);
}

void PrefRegistrySyncable::RegisterDictionaryPref(
    const char* path,
    DictionaryValue* default_value,
    PrefSyncStatus sync_status) {
  RegisterSyncablePreference(path, default_value, sync_status);
}

void PrefRegistrySyncable::RegisterLocalizedBooleanPref(
    const char* path,
    int locale_default_message_id,
    PrefSyncStatus sync_status) {
  RegisterSyncablePreference(
      path,
      CreateLocaleDefaultValue(Value::TYPE_BOOLEAN, locale_default_message_id),
      sync_status);
}

void PrefRegistrySyncable::RegisterLocalizedIntegerPref(
    const char* path,
    int locale_default_message_id,
    PrefSyncStatus sync_status) {
  RegisterSyncablePreference(
      path,
      CreateLocaleDefaultValue(Value::TYPE_INTEGER, locale_default_message_id),
      sync_status);
}

void PrefRegistrySyncable::RegisterLocalizedDoublePref(
    const char* path,
    int locale_default_message_id,
    PrefSyncStatus sync_status) {
  RegisterSyncablePreference(
      path,
      CreateLocaleDefaultValue(Value::TYPE_DOUBLE, locale_default_message_id),
      sync_status);
}

void PrefRegistrySyncable::RegisterLocalizedStringPref(
    const char* path,
    int locale_default_message_id,
    PrefSyncStatus sync_status) {
  RegisterSyncablePreference(
      path,
      CreateLocaleDefaultValue(Value::TYPE_STRING, locale_default_message_id),
      sync_status);
}

void PrefRegistrySyncable::RegisterInt64Pref(
    const char* path,
    int64 default_value,
    PrefSyncStatus sync_status) {
  RegisterSyncablePreference(
      path,
      Value::CreateStringValue(base::Int64ToString(default_value)),
      sync_status);
}

void PrefRegistrySyncable::RegisterUint64Pref(
    const char* path,
    uint64 default_value,
    PrefSyncStatus sync_status) {
  RegisterSyncablePreference(
      path,
      Value::CreateStringValue(base::Uint64ToString(default_value)),
      sync_status);
}

void PrefRegistrySyncable::RegisterSyncablePreference(
    const char* path,
    base::Value* default_value,
    PrefSyncStatus sync_status) {
  PrefRegistry::RegisterPreference(path, default_value);

  if (sync_status == SYNCABLE_PREF) {
    syncable_preferences_.insert(path);

    if (!callback_.is_null())
      callback_.Run(path);
  }
}

scoped_refptr<PrefRegistrySyncable> PrefRegistrySyncable::ForkForIncognito() {
  // TODO(joi): We can directly reuse the same PrefRegistry once
  // PrefService no longer registers for callbacks on registration and
  // unregistration.
  scoped_refptr<PrefRegistrySyncable> registry(new PrefRegistrySyncable());
  registry->defaults_ = defaults_;
  return registry;
}