Fix emulator build error
[platform/framework/web/chromium-efl.git] / components / prefs / pref_value_store.cc
1 // Copyright 2012 The Chromium Authors
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/prefs/pref_value_store.h"
6
7 #include <stddef.h>
8 #include <string>
9
10 #include "base/logging.h"
11 #include "base/strings/string_piece.h"
12 #include "components/prefs/pref_notifier.h"
13 #include "components/prefs/pref_observer.h"
14
15 PrefValueStore::PrefStoreKeeper::PrefStoreKeeper()
16     : pref_value_store_(nullptr), type_(PrefValueStore::INVALID_STORE) {}
17
18 PrefValueStore::PrefStoreKeeper::~PrefStoreKeeper() {
19   if (pref_store_) {
20     pref_store_->RemoveObserver(this);
21     pref_store_ = nullptr;
22   }
23   pref_value_store_ = nullptr;
24 }
25
26 void PrefValueStore::PrefStoreKeeper::Initialize(
27     PrefValueStore* store,
28     PrefStore* pref_store,
29     PrefValueStore::PrefStoreType type) {
30   if (pref_store_) {
31     pref_store_->RemoveObserver(this);
32     DCHECK(!pref_store_->HasObservers());
33   }
34   type_ = type;
35   pref_value_store_ = store;
36   pref_store_ = pref_store;
37   if (pref_store_)
38     pref_store_->AddObserver(this);
39 }
40
41 void PrefValueStore::PrefStoreKeeper::OnPrefValueChanged(
42     const std::string& key) {
43   pref_value_store_->OnPrefValueChanged(type_, key);
44 }
45
46 void PrefValueStore::PrefStoreKeeper::OnInitializationCompleted(
47     bool succeeded) {
48   pref_value_store_->OnInitializationCompleted(type_, succeeded);
49 }
50
51 PrefValueStore::PrefValueStore(PrefStore* managed_prefs,
52                                PrefStore* supervised_user_prefs,
53                                PrefStore* extension_prefs,
54                                PrefStore* standalone_browser_prefs,
55                                PrefStore* command_line_prefs,
56                                PrefStore* user_prefs,
57                                PrefStore* recommended_prefs,
58                                PrefStore* default_prefs,
59                                PrefNotifier* pref_notifier)
60     : pref_notifier_(pref_notifier), initialization_failed_(false) {
61   InitPrefStore(MANAGED_STORE, managed_prefs);
62   InitPrefStore(SUPERVISED_USER_STORE, supervised_user_prefs);
63   InitPrefStore(EXTENSION_STORE, extension_prefs);
64   InitPrefStore(STANDALONE_BROWSER_STORE, standalone_browser_prefs);
65   InitPrefStore(COMMAND_LINE_STORE, command_line_prefs);
66   InitPrefStore(USER_STORE, user_prefs);
67   InitPrefStore(RECOMMENDED_STORE, recommended_prefs);
68   InitPrefStore(DEFAULT_STORE, default_prefs);
69
70   CheckInitializationCompleted();
71 }
72
73 PrefValueStore::~PrefValueStore() {}
74
75 std::unique_ptr<PrefValueStore> PrefValueStore::CloneAndSpecialize(
76     PrefStore* managed_prefs,
77     PrefStore* supervised_user_prefs,
78     PrefStore* extension_prefs,
79     PrefStore* standalone_browser_prefs,
80     PrefStore* command_line_prefs,
81     PrefStore* user_prefs,
82     PrefStore* recommended_prefs,
83     PrefStore* default_prefs,
84     PrefNotifier* pref_notifier) {
85   DCHECK(pref_notifier);
86   if (!managed_prefs)
87     managed_prefs = GetPrefStore(MANAGED_STORE);
88   if (!supervised_user_prefs)
89     supervised_user_prefs = GetPrefStore(SUPERVISED_USER_STORE);
90   if (!extension_prefs)
91     extension_prefs = GetPrefStore(EXTENSION_STORE);
92   if (!standalone_browser_prefs)
93     standalone_browser_prefs = GetPrefStore(STANDALONE_BROWSER_STORE);
94   if (!command_line_prefs)
95     command_line_prefs = GetPrefStore(COMMAND_LINE_STORE);
96   if (!user_prefs)
97     user_prefs = GetPrefStore(USER_STORE);
98   if (!recommended_prefs)
99     recommended_prefs = GetPrefStore(RECOMMENDED_STORE);
100   if (!default_prefs)
101     default_prefs = GetPrefStore(DEFAULT_STORE);
102
103   return std::make_unique<PrefValueStore>(
104       managed_prefs, supervised_user_prefs, extension_prefs,
105       standalone_browser_prefs, command_line_prefs, user_prefs,
106       recommended_prefs, default_prefs, pref_notifier);
107 }
108
109 PrefValueStore::PrefStoreType PrefValueStore::ControllingPrefStoreForPref(
110     const std::string& name) const {
111   for (size_t i = 0; i <= PREF_STORE_TYPE_MAX; ++i) {
112     if (PrefValueInStore(name, static_cast<PrefStoreType>(i))) {
113       return static_cast<PrefStoreType>(i);
114     }
115   }
116   return INVALID_STORE;
117 }
118
119 bool PrefValueStore::GetValue(base::StringPiece name,
120                               base::Value::Type type,
121                               const base::Value** out_value) const {
122   // Check the |PrefStore|s in order of their priority from highest to lowest,
123   // looking for the first preference value with the given |name| and |type|.
124   for (size_t i = 0; i <= PREF_STORE_TYPE_MAX; ++i) {
125     if (GetValueFromStoreWithType(name, type, static_cast<PrefStoreType>(i),
126                                   out_value))
127       return true;
128   }
129   return false;
130 }
131
132 bool PrefValueStore::GetRecommendedValue(const std::string& name,
133                                          base::Value::Type type,
134                                          const base::Value** out_value) const {
135   return GetValueFromStoreWithType(name, type, RECOMMENDED_STORE, out_value);
136 }
137
138 void PrefValueStore::NotifyPrefChanged(
139     const std::string& path,
140     PrefValueStore::PrefStoreType new_store) {
141   DCHECK(new_store != INVALID_STORE);
142   // A notification is sent when the pref value in any store changes. If this
143   // store is currently being overridden by a higher-priority store, the
144   // effective value of the pref will not have changed.
145   pref_notifier_->OnPreferenceChanged(path);
146 }
147
148 bool PrefValueStore::PrefValueInManagedStore(const std::string& name) const {
149   return PrefValueInStore(name, MANAGED_STORE);
150 }
151
152 bool PrefValueStore::PrefValueInSupervisedStore(const std::string& name) const {
153   return PrefValueInStore(name, SUPERVISED_USER_STORE);
154 }
155
156 bool PrefValueStore::PrefValueInExtensionStore(const std::string& name) const {
157   return PrefValueInStore(name, EXTENSION_STORE);
158 }
159
160 bool PrefValueStore::PrefValueInUserStore(const std::string& name) const {
161   return PrefValueInStore(name, USER_STORE);
162 }
163
164 bool PrefValueStore::PrefValueFromExtensionStore(
165     const std::string& name) const {
166   return ControllingPrefStoreForPref(name) == EXTENSION_STORE;
167 }
168
169 bool PrefValueStore::PrefValueFromUserStore(const std::string& name) const {
170   return ControllingPrefStoreForPref(name) == USER_STORE;
171 }
172
173 bool PrefValueStore::PrefValueFromRecommendedStore(
174     const std::string& name) const {
175   return ControllingPrefStoreForPref(name) == RECOMMENDED_STORE;
176 }
177
178 bool PrefValueStore::PrefValueFromStandaloneBrowserStore(
179     const std::string& name) const {
180   return ControllingPrefStoreForPref(name) == STANDALONE_BROWSER_STORE;
181 }
182
183 bool PrefValueStore::PrefValueFromDefaultStore(const std::string& name) const {
184   return ControllingPrefStoreForPref(name) == DEFAULT_STORE;
185 }
186
187 bool PrefValueStore::PrefValueUserModifiable(const std::string& name) const {
188   PrefStoreType effective_store = ControllingPrefStoreForPref(name);
189   return effective_store >= USER_STORE ||
190          effective_store == INVALID_STORE;
191 }
192
193 bool PrefValueStore::PrefValueExtensionModifiable(
194     const std::string& name) const {
195   PrefStoreType effective_store = ControllingPrefStoreForPref(name);
196   return effective_store >= EXTENSION_STORE ||
197          effective_store == INVALID_STORE;
198 }
199
200 bool PrefValueStore::PrefValueStandaloneBrowserModifiable(
201     const std::string& name) const {
202   PrefStoreType effective_store = ControllingPrefStoreForPref(name);
203   return effective_store >= STANDALONE_BROWSER_STORE ||
204          effective_store == INVALID_STORE;
205 }
206
207 void PrefValueStore::UpdateCommandLinePrefStore(PrefStore* command_line_prefs) {
208   InitPrefStore(COMMAND_LINE_STORE, command_line_prefs);
209 }
210
211 bool PrefValueStore::IsInitializationComplete() const {
212   for (size_t i = 0; i <= PREF_STORE_TYPE_MAX; ++i) {
213     const PrefStore* pref_store = GetPrefStore(static_cast<PrefStoreType>(i));
214     if (pref_store && !pref_store->IsInitializationComplete()) {
215       return false;
216     }
217   }
218   return true;
219 }
220
221 bool PrefValueStore::PrefValueInStore(
222     const std::string& name,
223     PrefValueStore::PrefStoreType store) const {
224   // Declare a temp Value* and call GetValueFromStore,
225   // ignoring the output value.
226   const base::Value* tmp_value = nullptr;
227   return GetValueFromStore(name, store, &tmp_value);
228 }
229
230 bool PrefValueStore::PrefValueInStoreRange(
231     const std::string& name,
232     PrefValueStore::PrefStoreType first_checked_store,
233     PrefValueStore::PrefStoreType last_checked_store) const {
234   if (first_checked_store > last_checked_store) {
235     NOTREACHED();
236     return false;
237   }
238
239   for (size_t i = first_checked_store;
240        i <= static_cast<size_t>(last_checked_store); ++i) {
241     if (PrefValueInStore(name, static_cast<PrefStoreType>(i)))
242       return true;
243   }
244   return false;
245 }
246
247 bool PrefValueStore::GetValueFromStore(base::StringPiece name,
248                                        PrefValueStore::PrefStoreType store_type,
249                                        const base::Value** out_value) const {
250   // Only return true if we find a value and it is the correct type, so stale
251   // values with the incorrect type will be ignored.
252   const PrefStore* store = GetPrefStore(static_cast<PrefStoreType>(store_type));
253   if (store && store->GetValue(name, out_value))
254     return true;
255
256   // No valid value found for the given preference name: set the return value
257   // to false.
258   *out_value = nullptr;
259   return false;
260 }
261
262 bool PrefValueStore::GetValueFromStoreWithType(
263     base::StringPiece name,
264     base::Value::Type type,
265     PrefStoreType store,
266     const base::Value** out_value) const {
267   if (GetValueFromStore(name, store, out_value)) {
268     if ((*out_value)->type() == type)
269       return true;
270
271     LOG(WARNING) << "Expected type for " << name << " is " << type
272                  << " but got " << (*out_value)->type() << " in store "
273                  << store;
274   }
275
276   *out_value = nullptr;
277   return false;
278 }
279
280 void PrefValueStore::OnPrefValueChanged(PrefValueStore::PrefStoreType type,
281                                         const std::string& key) {
282   NotifyPrefChanged(key, type);
283 }
284
285 void PrefValueStore::OnInitializationCompleted(
286     PrefValueStore::PrefStoreType type, bool succeeded) {
287   if (initialization_failed_)
288     return;
289   if (!succeeded) {
290     initialization_failed_ = true;
291     pref_notifier_->OnInitializationCompleted(false);
292     return;
293   }
294   CheckInitializationCompleted();
295 }
296
297 void PrefValueStore::InitPrefStore(PrefValueStore::PrefStoreType type,
298                                    PrefStore* pref_store) {
299   pref_stores_[type].Initialize(this, pref_store, type);
300 }
301
302 void PrefValueStore::CheckInitializationCompleted() {
303   if (initialization_failed_)
304     return;
305   for (size_t i = 0; i <= PREF_STORE_TYPE_MAX; ++i) {
306     scoped_refptr<PrefStore> store =
307         GetPrefStore(static_cast<PrefStoreType>(i));
308     if (store.get() && !store->IsInitializationComplete())
309       return;
310   }
311   pref_notifier_->OnInitializationCompleted(true);
312 }