1 // Copyright 2013 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/scoped_user_pref_update.h"
6
7 #include "base/check_op.h"
8 #include "components/prefs/pref_notifier.h"
9 #include "components/prefs/pref_service.h"
10
11 // TODO(crbug.com/1419591): The following two can be removed after resolving
12 // the problem.
13 #include "base/debug/crash_logging.h"
14 #include "base/debug/dump_without_crashing.h"
15 #include "base/types/cxx23_to_underlying.h"
16
17 namespace subtle {
18
ScopedUserPrefUpdateBase(PrefService * service,const std::string & path)19 ScopedUserPrefUpdateBase::ScopedUserPrefUpdateBase(PrefService* service,
20 const std::string& path)
21 : service_(service), path_(path), value_(nullptr) {
22 DCHECK_CALLED_ON_VALID_SEQUENCE(service_->sequence_checker_);
23 }
24
~ScopedUserPrefUpdateBase()25 ScopedUserPrefUpdateBase::~ScopedUserPrefUpdateBase() {
26 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
27 Notify();
28 }
29
GetValueOfType(base::Value::Type type)30 base::Value* ScopedUserPrefUpdateBase::GetValueOfType(base::Value::Type type) {
31 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
32 if (!value_)
33 value_ = service_->GetMutableUserPref(path_, type);
34 if (!value_) {
35 // TODO(crbug.com/1419591) This is unexpected, so let's collect some data.
36 const PrefService::Preference* pref = service_->FindPreference(path_);
37 SCOPED_CRASH_KEY_NUMBER(
38 "ScopedUserPrefUpdate", "PrevServiceStatus",
39 base::to_underlying(service_->GetInitializationStatus()));
40 SCOPED_CRASH_KEY_STRING32("ScopedUserPrefUpdate", "FindPreference",
41 pref ? "Yes" : "No");
42 SCOPED_CRASH_KEY_NUMBER("ScopedUserPrefUpdate", "Type",
43 pref ? base::to_underlying(pref->GetType()) : -1);
44 base::debug::DumpWithoutCrashing();
45 }
46 return value_;
47 }
48
Notify()49 void ScopedUserPrefUpdateBase::Notify() {
50 if (value_) {
51 service_->ReportUserPrefChanged(path_);
52 value_ = nullptr;
53 }
54 }
55
56 } // namespace subtle
57
Get()58 base::Value::Dict& ScopedDictPrefUpdate::Get() {
59 return GetValueOfType(base::Value::Type::DICT)->GetDict();
60 }
61
Get()62 base::Value::List& ScopedListPrefUpdate::Get() {
63 return GetValueOfType(base::Value::Type::LIST)->GetList();
64 }
65