Fix emulator build error
[platform/framework/web/chromium-efl.git] / components / prefs / pref_service.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_service.h"
6
7 #include <algorithm>
8 #include <map>
9 #include <string>
10 #include <utility>
11
12 #include "base/check_op.h"
13 #include "base/debug/alias.h"
14 #include "base/debug/dump_without_crashing.h"
15 #include "base/files/file_path.h"
16 #include "base/functional/bind.h"
17 #include "base/json/values_util.h"
18 #include "base/location.h"
19 #include "base/logging.h"
20 #include "base/memory/ptr_util.h"
21 #include "base/metrics/histogram.h"
22 #include "base/notreached.h"
23 #include "base/strings/string_number_conversions.h"
24 #include "base/strings/string_piece.h"
25 #include "base/strings/string_util.h"
26 #include "base/task/sequenced_task_runner.h"
27 #include "base/values.h"
28 #include "build/chromeos_buildflags.h"
29 #include "components/prefs/default_pref_store.h"
30 #include "components/prefs/json_pref_store.h"
31 #include "components/prefs/pref_notifier_impl.h"
32 #include "components/prefs/pref_registry.h"
33
34 #if BUILDFLAG(IS_CHROMEOS_ASH)
35 #include "components/prefs/value_map_pref_store.h"
36 #endif
37
38 #if BUILDFLAG(IS_ANDROID)
39 #include "components/prefs/android/pref_service_android.h"
40 #endif
41
42 namespace {
43
44
45 }  // namespace
46
47 PrefService::PersistentPrefStoreLoadingObserver::
48     PersistentPrefStoreLoadingObserver(PrefService* pref_service)
49     : pref_service_(pref_service) {
50   DCHECK(pref_service_);
51 }
52
53 void PrefService::PersistentPrefStoreLoadingObserver::OnInitializationCompleted(
54     bool succeeded) {
55   pref_service_->CheckPrefsLoaded();
56 }
57
58 PrefService::PrefService(
59     std::unique_ptr<PrefNotifierImpl> pref_notifier,
60     std::unique_ptr<PrefValueStore> pref_value_store,
61     scoped_refptr<PersistentPrefStore> user_prefs,
62     scoped_refptr<PersistentPrefStore> standalone_browser_prefs,
63     scoped_refptr<PrefRegistry> pref_registry,
64     base::RepeatingCallback<void(PersistentPrefStore::PrefReadError)>
65         read_error_callback,
66     bool async)
67     : pref_notifier_(std::move(pref_notifier)),
68       pref_value_store_(std::move(pref_value_store)),
69       user_pref_store_(std::move(user_prefs)),
70       standalone_browser_pref_store_(std::move(standalone_browser_prefs)),
71       read_error_callback_(std::move(read_error_callback)),
72       pref_registry_(std::move(pref_registry)),
73       pref_store_observer_(
74           std::make_unique<PrefService::PersistentPrefStoreLoadingObserver>(
75               this)) {
76   pref_notifier_->SetPrefService(this);
77
78   DCHECK(pref_registry_);
79   DCHECK(pref_value_store_);
80
81   InitFromStorage(async);
82 }
83
84 PrefService::~PrefService() {
85   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
86
87   // Remove observers. This could be necessary if this service is destroyed
88   // before the prefs are fully loaded.
89   user_pref_store_->RemoveObserver(pref_store_observer_.get());
90   if (standalone_browser_pref_store_) {
91     standalone_browser_pref_store_->RemoveObserver(pref_store_observer_.get());
92   }
93
94   // TODO(crbug.com/942491, 946668, 945772) The following code collects
95   // augments stack dumps created by ~PrefNotifierImpl() with information
96   // whether the profile owning the PrefService is an incognito profile.
97   // Delete this, once the bugs are closed.
98   const bool is_incognito_profile = user_pref_store_->IsInMemoryPrefStore();
99   base::debug::Alias(&is_incognito_profile);
100   // Export value of is_incognito_profile to a string so that `grep`
101   // is a sufficient tool to analyze crashdumps.
102   char is_incognito_profile_string[32];
103   strncpy(is_incognito_profile_string,
104           is_incognito_profile ? "is_incognito: yes" : "is_incognito: no",
105           sizeof(is_incognito_profile_string));
106   base::debug::Alias(&is_incognito_profile_string);
107 }
108
109 void PrefService::InitFromStorage(bool async) {
110   if (!async) {
111     if (!user_pref_store_->IsInitializationComplete()) {
112       user_pref_store_->ReadPrefs();
113     }
114     if (standalone_browser_pref_store_ &&
115         !standalone_browser_pref_store_->IsInitializationComplete()) {
116       standalone_browser_pref_store_->ReadPrefs();
117     }
118     CheckPrefsLoaded();
119     return;
120   }
121
122   CheckPrefsLoaded();
123
124   if (!user_pref_store_->IsInitializationComplete()) {
125     user_pref_store_->AddObserver(pref_store_observer_.get());
126     user_pref_store_->ReadPrefsAsync(nullptr);
127   }
128
129   if (standalone_browser_pref_store_ &&
130       !standalone_browser_pref_store_->IsInitializationComplete()) {
131     standalone_browser_pref_store_->AddObserver(pref_store_observer_.get());
132     standalone_browser_pref_store_->ReadPrefsAsync(nullptr);
133   }
134 }
135
136 void PrefService::CheckPrefsLoaded() {
137   if (!(user_pref_store_->IsInitializationComplete() &&
138         (!standalone_browser_pref_store_ ||
139          standalone_browser_pref_store_->IsInitializationComplete()))) {
140     // Not done initializing both prefstores.
141     return;
142   }
143
144   user_pref_store_->RemoveObserver(pref_store_observer_.get());
145   if (standalone_browser_pref_store_) {
146     standalone_browser_pref_store_->RemoveObserver(pref_store_observer_.get());
147   }
148
149   // Both prefstores are initialized, get the read errors.
150   PersistentPrefStore::PrefReadError user_store_error =
151       user_pref_store_->GetReadError();
152   if (!standalone_browser_pref_store_) {
153     read_error_callback_.Run(user_store_error);
154     return;
155   }
156   PersistentPrefStore::PrefReadError standalone_browser_store_error =
157       standalone_browser_pref_store_->GetReadError();
158
159   // If both stores have the same error (or no error), run the callback with
160   // either one. This avoids double-reporting (either way prefs weren't
161   // successfully fully loaded)
162   if (user_store_error == standalone_browser_store_error) {
163     read_error_callback_.Run(user_store_error);
164   } else if (user_store_error == PersistentPrefStore::PREF_READ_ERROR_NONE ||
165              user_store_error == PersistentPrefStore::PREF_READ_ERROR_NO_FILE) {
166     // Prefer to report the standalone_browser_pref_store error if the
167     // user_pref_store error is not significant.
168     read_error_callback_.Run(standalone_browser_store_error);
169   } else {
170     // Either the user_pref_store error is significant, or
171     // both stores failed to load but for different reasons.
172     // The user_store error is more significant in essentially all cases,
173     // so prefer to report that.
174     read_error_callback_.Run(user_store_error);
175   }
176 }
177
178 void PrefService::CommitPendingWrite(
179     base::OnceClosure reply_callback,
180     base::OnceClosure synchronous_done_callback) {
181   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
182   user_pref_store_->CommitPendingWrite(std::move(reply_callback),
183                                        std::move(synchronous_done_callback));
184 }
185
186 void PrefService::SchedulePendingLossyWrites() {
187   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
188   user_pref_store_->SchedulePendingLossyWrites();
189 }
190
191 bool PrefService::GetBoolean(base::StringPiece path) const {
192   return GetValue(path).GetBool();
193 }
194
195 int PrefService::GetInteger(base::StringPiece path) const {
196   return GetValue(path).GetInt();
197 }
198
199 double PrefService::GetDouble(base::StringPiece path) const {
200   return GetValue(path).GetDouble();
201 }
202
203 const std::string& PrefService::GetString(base::StringPiece path) const {
204   return GetValue(path).GetString();
205 }
206
207 base::FilePath PrefService::GetFilePath(base::StringPiece path) const {
208   const base::Value& value = GetValue(path);
209   absl::optional<base::FilePath> result = base::ValueToFilePath(value);
210   DCHECK(result);
211   return *result;
212 }
213
214 bool PrefService::HasPrefPath(const std::string& path) const {
215   const Preference* pref = FindPreference(path);
216   return pref && !pref->IsDefaultValue();
217 }
218
219 void PrefService::IteratePreferenceValues(
220     base::RepeatingCallback<void(const std::string& key,
221                                  const base::Value& value)> callback) const {
222   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
223   for (const auto& it : *pref_registry_)
224     callback.Run(it.first, *GetPreferenceValue(it.first));
225 }
226
227 base::Value::Dict PrefService::GetPreferenceValues(
228     IncludeDefaults include_defaults) const {
229   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
230
231   base::Value::Dict out;
232   for (const auto& it : *pref_registry_) {
233     if (include_defaults == INCLUDE_DEFAULTS) {
234       out.SetByDottedPath(it.first, GetPreferenceValue(it.first)->Clone());
235     } else {
236       const Preference* pref = FindPreference(it.first);
237       if (pref->IsDefaultValue()) {
238         continue;
239       }
240       out.SetByDottedPath(it.first, pref->GetValue()->Clone());
241     }
242   }
243   return out;
244 }
245
246 std::vector<PrefService::PreferenceValueAndStore>
247 PrefService::GetPreferencesValueAndStore() const {
248   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
249
250   std::vector<PreferenceValueAndStore> result;
251   for (const auto& it : *pref_registry_) {
252     auto* preference = FindPreference(it.first);
253     CHECK(preference);
254     PreferenceValueAndStore pref_data{
255         it.first, preference->GetValue()->Clone(),
256         pref_value_store_->ControllingPrefStoreForPref(it.first)};
257     result.emplace_back(std::move(pref_data));
258   }
259   return result;
260 }
261
262 const PrefService::Preference* PrefService::FindPreference(
263     const std::string& pref_name) const {
264   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
265   auto it = prefs_map_.find(pref_name);
266   if (it != prefs_map_.end())
267     return &(it->second);
268   const base::Value* default_value = nullptr;
269   if (!pref_registry_->defaults()->GetValue(pref_name, &default_value))
270     return nullptr;
271   it = prefs_map_
272            .insert(std::make_pair(
273                pref_name, Preference(this, pref_name, default_value->type())))
274            .first;
275   return &(it->second);
276 }
277
278 bool PrefService::ReadOnly() const {
279   return user_pref_store_->ReadOnly();
280 }
281
282 PrefService::PrefInitializationStatus PrefService::GetInitializationStatus()
283     const {
284   if (!user_pref_store_->IsInitializationComplete())
285     return INITIALIZATION_STATUS_WAITING;
286
287   switch (user_pref_store_->GetReadError()) {
288     case PersistentPrefStore::PREF_READ_ERROR_NONE:
289       return INITIALIZATION_STATUS_SUCCESS;
290     case PersistentPrefStore::PREF_READ_ERROR_NO_FILE:
291       return INITIALIZATION_STATUS_CREATED_NEW_PREF_STORE;
292     default:
293       return INITIALIZATION_STATUS_ERROR;
294   }
295 }
296
297 PrefService::PrefInitializationStatus
298 PrefService::GetAllPrefStoresInitializationStatus() const {
299   if (!pref_value_store_->IsInitializationComplete())
300     return INITIALIZATION_STATUS_WAITING;
301
302   return GetInitializationStatus();
303 }
304
305 bool PrefService::IsManagedPreference(const std::string& pref_name) const {
306   const Preference* pref = FindPreference(pref_name);
307   return pref && pref->IsManaged();
308 }
309
310 bool PrefService::IsPreferenceManagedByCustodian(
311     const std::string& pref_name) const {
312   const Preference* pref = FindPreference(pref_name);
313   return pref && pref->IsManagedByCustodian();
314 }
315
316 bool PrefService::IsUserModifiablePreference(
317     const std::string& pref_name) const {
318   const Preference* pref = FindPreference(pref_name);
319   return pref && pref->IsUserModifiable();
320 }
321
322 const base::Value& PrefService::GetValue(base::StringPiece path) const {
323   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
324   return *GetPreferenceValueChecked(path);
325 }
326
327 const base::Value::Dict& PrefService::GetDict(base::StringPiece path) const {
328   const base::Value& value = GetValue(path);
329   return value.GetDict();
330 }
331
332 const base::Value::List& PrefService::GetList(base::StringPiece path) const {
333   const base::Value& value = GetValue(path);
334   return value.GetList();
335 }
336
337 const base::Value* PrefService::GetUserPrefValue(
338     const std::string& path) const {
339   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
340
341   const Preference* pref = FindPreference(path);
342   if (!pref) {
343     NOTREACHED() << "Trying to get an unregistered pref: " << path;
344     return nullptr;
345   }
346
347   // Look for an existing preference in the user store. If it doesn't
348   // exist, return NULL.
349   base::Value* value = nullptr;
350   if (!user_pref_store_->GetMutableValue(path, &value))
351     return nullptr;
352
353   if (value->type() != pref->GetType()) {
354     NOTREACHED() << "Pref value type doesn't match registered type.";
355     return nullptr;
356   }
357
358   return value;
359 }
360
361 void PrefService::SetDefaultPrefValue(const std::string& path,
362                                       base::Value value) {
363   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
364   pref_registry_->SetDefaultPrefValue(path, std::move(value));
365 }
366
367 const base::Value* PrefService::GetDefaultPrefValue(
368     const std::string& path) const {
369   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
370   // Lookup the preference in the default store.
371   const base::Value* value = nullptr;
372   bool has_value = pref_registry_->defaults()->GetValue(path, &value);
373   DCHECK(has_value) << "Default value missing for pref: " << path;
374   return value;
375 }
376
377 void PrefService::AddPrefObserver(const std::string& path, PrefObserver* obs) {
378   pref_notifier_->AddPrefObserver(path, obs);
379 }
380
381 void PrefService::RemovePrefObserver(const std::string& path,
382                                      PrefObserver* obs) {
383   pref_notifier_->RemovePrefObserver(path, obs);
384 }
385
386 void PrefService::AddPrefInitObserver(base::OnceCallback<void(bool)> obs) {
387   pref_notifier_->AddInitObserver(std::move(obs));
388 }
389
390 PrefRegistry* PrefService::DeprecatedGetPrefRegistry() {
391   return pref_registry_.get();
392 }
393
394 void PrefService::ClearPref(const std::string& path) {
395   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
396
397   const Preference* pref = FindPreference(path);
398   if (!pref) {
399     NOTREACHED() << "Trying to clear an unregistered pref: " << path;
400     return;
401   }
402   user_pref_store_->RemoveValue(path, GetWriteFlags(pref));
403 }
404
405 void PrefService::ClearPrefsWithPrefixSilently(const std::string& prefix) {
406   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
407   user_pref_store_->RemoveValuesByPrefixSilently(prefix);
408 }
409
410 void PrefService::OnStoreDeletionFromDisk() {
411   user_pref_store_->OnStoreDeletionFromDisk();
412 }
413
414 void PrefService::AddPrefObserverAllPrefs(PrefObserver* obs) {
415   pref_notifier_->AddPrefObserverAllPrefs(obs);
416 }
417
418 void PrefService::RemovePrefObserverAllPrefs(PrefObserver* obs) {
419   pref_notifier_->RemovePrefObserverAllPrefs(obs);
420 }
421
422 #if BUILDFLAG(IS_ANDROID)
423 base::android::ScopedJavaLocalRef<jobject> PrefService::GetJavaObject() {
424   if (!pref_service_android_) {
425     pref_service_android_ = std::make_unique<PrefServiceAndroid>(this);
426   }
427   return pref_service_android_->GetJavaObject();
428 }
429 #endif
430
431 void PrefService::Set(const std::string& path, const base::Value& value) {
432   SetUserPrefValue(path, value.Clone());
433 }
434
435 void PrefService::SetBoolean(const std::string& path, bool value) {
436   SetUserPrefValue(path, base::Value(value));
437 }
438
439 void PrefService::SetInteger(const std::string& path, int value) {
440   SetUserPrefValue(path, base::Value(value));
441 }
442
443 void PrefService::SetDouble(const std::string& path, double value) {
444   SetUserPrefValue(path, base::Value(value));
445 }
446
447 void PrefService::SetString(const std::string& path, base::StringPiece value) {
448   SetUserPrefValue(path, base::Value(value));
449 }
450
451 void PrefService::SetDict(const std::string& path, base::Value::Dict dict) {
452   SetUserPrefValue(path, base::Value(std::move(dict)));
453 }
454
455 void PrefService::SetList(const std::string& path, base::Value::List list) {
456   SetUserPrefValue(path, base::Value(std::move(list)));
457 }
458
459 void PrefService::SetFilePath(const std::string& path,
460                               const base::FilePath& value) {
461   SetUserPrefValue(path, base::FilePathToValue(value));
462 }
463
464 void PrefService::SetInt64(const std::string& path, int64_t value) {
465   SetUserPrefValue(path, base::Int64ToValue(value));
466 }
467
468 int64_t PrefService::GetInt64(const std::string& path) const {
469   const base::Value& value = GetValue(path);
470   absl::optional<int64_t> integer = base::ValueToInt64(value);
471   DCHECK(integer);
472   return integer.value_or(0);
473 }
474
475 void PrefService::SetUint64(const std::string& path, uint64_t value) {
476   SetUserPrefValue(path, base::Value(base::NumberToString(value)));
477 }
478
479 uint64_t PrefService::GetUint64(const std::string& path) const {
480   const base::Value& value = GetValue(path);
481   if (!value.is_string())
482     return 0;
483
484   uint64_t result;
485   base::StringToUint64(value.GetString(), &result);
486   return result;
487 }
488
489 void PrefService::SetTime(const std::string& path, base::Time value) {
490   SetUserPrefValue(path, base::TimeToValue(value));
491 }
492
493 base::Time PrefService::GetTime(const std::string& path) const {
494   const base::Value& value = GetValue(path);
495   absl::optional<base::Time> time = base::ValueToTime(value);
496   DCHECK(time);
497   return time.value_or(base::Time());
498 }
499
500 void PrefService::SetTimeDelta(const std::string& path, base::TimeDelta value) {
501   SetUserPrefValue(path, base::TimeDeltaToValue(value));
502 }
503
504 base::TimeDelta PrefService::GetTimeDelta(const std::string& path) const {
505   const base::Value& value = GetValue(path);
506   absl::optional<base::TimeDelta> time_delta = base::ValueToTimeDelta(value);
507   DCHECK(time_delta);
508   return time_delta.value_or(base::TimeDelta());
509 }
510
511 base::Value* PrefService::GetMutableUserPref(const std::string& path,
512                                              base::Value::Type type) {
513   CHECK(type == base::Value::Type::DICT || type == base::Value::Type::LIST);
514   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
515
516   const Preference* pref = FindPreference(path);
517   if (!pref) {
518     NOTREACHED() << "Trying to get an unregistered pref: " << path;
519     return nullptr;
520   }
521   if (pref->GetType() != type) {
522     NOTREACHED() << "Wrong type for GetMutableValue: " << path;
523     return nullptr;
524   }
525
526   // Look for an existing preference in the user store. Return it in case it
527   // exists and has the correct type.
528   base::Value* value = nullptr;
529   if (user_pref_store_->GetMutableValue(path, &value) &&
530       value->type() == type) {
531     return value;
532   }
533
534   // If no user preference of the correct type exists, clone default value.
535   const base::Value* default_value = nullptr;
536   pref_registry_->defaults()->GetValue(path, &default_value);
537   DCHECK_EQ(default_value->type(), type);
538   user_pref_store_->SetValueSilently(path, default_value->Clone(),
539                                      GetWriteFlags(pref));
540   user_pref_store_->GetMutableValue(path, &value);
541   return value;
542 }
543
544 void PrefService::ReportUserPrefChanged(const std::string& key) {
545   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
546   user_pref_store_->ReportValueChanged(key, GetWriteFlags(FindPreference(key)));
547 }
548
549 void PrefService::ReportUserPrefChanged(
550     const std::string& key,
551     std::set<std::vector<std::string>> path_components) {
552   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
553   user_pref_store_->ReportSubValuesChanged(key, std::move(path_components),
554                                            GetWriteFlags(FindPreference(key)));
555 }
556
557 void PrefService::SetUserPrefValue(const std::string& path,
558                                    base::Value new_value) {
559   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
560
561   const Preference* pref = FindPreference(path);
562   if (!pref) {
563     NOTREACHED() << "Trying to write an unregistered pref: " << path;
564     return;
565   }
566   if (pref->GetType() != new_value.type()) {
567     NOTREACHED() << "Trying to set pref " << path << " of type "
568                  << pref->GetType() << " to value of type " << new_value.type();
569     return;
570   }
571
572   user_pref_store_->SetValue(path, std::move(new_value), GetWriteFlags(pref));
573 }
574
575 void PrefService::UpdateCommandLinePrefStore(PrefStore* command_line_store) {
576   pref_value_store_->UpdateCommandLinePrefStore(command_line_store);
577 }
578
579 ///////////////////////////////////////////////////////////////////////////////
580 // PrefService::Preference
581
582 PrefService::Preference::Preference(const PrefService* service,
583                                     std::string name,
584                                     base::Value::Type type)
585     : name_(std::move(name)),
586       type_(type),
587       // Cache the registration flags at creation time to avoid multiple map
588       // lookups later.
589       registration_flags_(service->pref_registry_->GetRegistrationFlags(name_)),
590       pref_service_(service) {}
591
592 const base::Value* PrefService::Preference::GetValue() const {
593   return pref_service_->GetPreferenceValueChecked(name_);
594 }
595
596 const base::Value* PrefService::Preference::GetRecommendedValue() const {
597   DCHECK(pref_service_->FindPreference(name_))
598       << "Must register pref before getting its value";
599
600   const base::Value* found_value = nullptr;
601   if (pref_value_store()->GetRecommendedValue(name_, type_, &found_value)) {
602     DCHECK(found_value->type() == type_);
603     return found_value;
604   }
605
606   // The pref has no recommended value.
607   return nullptr;
608 }
609
610 bool PrefService::Preference::IsManaged() const {
611   return pref_value_store()->PrefValueInManagedStore(name_);
612 }
613
614 bool PrefService::Preference::IsManagedByCustodian() const {
615   return pref_value_store()->PrefValueInSupervisedStore(name_);
616 }
617
618 bool PrefService::Preference::IsRecommended() const {
619   return pref_value_store()->PrefValueFromRecommendedStore(name_);
620 }
621
622 bool PrefService::Preference::HasExtensionSetting() const {
623   return pref_value_store()->PrefValueInExtensionStore(name_);
624 }
625
626 bool PrefService::Preference::HasUserSetting() const {
627   return pref_value_store()->PrefValueInUserStore(name_);
628 }
629
630 bool PrefService::Preference::IsExtensionControlled() const {
631   return pref_value_store()->PrefValueFromExtensionStore(name_);
632 }
633
634 bool PrefService::Preference::IsUserControlled() const {
635   return pref_value_store()->PrefValueFromUserStore(name_);
636 }
637
638 bool PrefService::Preference::IsDefaultValue() const {
639   return pref_value_store()->PrefValueFromDefaultStore(name_);
640 }
641
642 bool PrefService::Preference::IsUserModifiable() const {
643   return pref_value_store()->PrefValueUserModifiable(name_);
644 }
645
646 bool PrefService::Preference::IsExtensionModifiable() const {
647   return pref_value_store()->PrefValueExtensionModifiable(name_);
648 }
649
650 #if BUILDFLAG(IS_CHROMEOS_ASH)
651 bool PrefService::Preference::IsStandaloneBrowserControlled() const {
652   return pref_value_store()->PrefValueFromStandaloneBrowserStore(name_);
653 }
654
655 bool PrefService::Preference::IsStandaloneBrowserModifiable() const {
656   return pref_value_store()->PrefValueStandaloneBrowserModifiable(name_);
657 }
658 #endif
659
660 const base::Value* PrefService::GetPreferenceValue(
661     base::StringPiece path) const {
662   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
663
664   const base::Value* default_value = nullptr;
665   if (!pref_registry_->defaults()->GetValue(path, &default_value))
666     return nullptr;
667
668   const base::Value* found_value = nullptr;
669   base::Value::Type default_type = default_value->type();
670   if (!pref_value_store_->GetValue(path, default_type, &found_value)) {
671     // Every registered preference has at least a default value.
672     NOTREACHED() << "no valid value found for registered pref " << path;
673     return default_value;
674   }
675
676   DCHECK_EQ(found_value->type(), default_type);
677   return found_value;
678 }
679
680 const base::Value* PrefService::GetPreferenceValueChecked(
681     base::StringPiece path) const {
682   const base::Value* value = GetPreferenceValue(path);
683   DCHECK(value) << "Trying to read an unregistered pref: " << path;
684   return value;
685 }
686
687 #if BUILDFLAG(IS_CHROMEOS_ASH)
688 void PrefService::SetStandaloneBrowserPref(const std::string& path,
689                                            const base::Value& value) {
690   if (!standalone_browser_pref_store_) {
691     LOG(WARNING) << "Failure to set value of " << path
692                  << " in standalone browser store";
693     return;
694   }
695   standalone_browser_pref_store_->SetValue(
696       path, value.Clone(), WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
697 }
698
699 void PrefService::RemoveStandaloneBrowserPref(const std::string& path) {
700   if (!standalone_browser_pref_store_) {
701     LOG(WARNING) << "Failure to remove value of " << path
702                  << " in standalone browser store";
703     return;
704   }
705   standalone_browser_pref_store_->RemoveValue(
706       path, WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
707 }
708 #endif
709
710 // static
711 uint32_t PrefService::GetWriteFlags(const PrefService::Preference* pref) {
712   uint32_t write_flags = WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS;
713
714   if (!pref) {
715     return write_flags;
716   }
717
718   if (pref->registration_flags() & PrefRegistry::LOSSY_PREF) {
719     write_flags |= WriteablePrefStore::LOSSY_PREF_WRITE_FLAG;
720   }
721   return write_flags;
722 }