blob: 27d56af893dbc039f972719d46d8eeebd14ae4c1 [file] [log] [blame]
[email protected]9eec53fe2013-10-30 20:21:171// Copyright 2013 The Chromium Authors. All rights reserved.
[email protected]dd8fa1a992010-04-05 23:56:162// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
brettwf00b9b42016-02-01 22:11:385#include "components/prefs/scoped_user_pref_update.h"
[email protected]dd8fa1a992010-04-05 23:56:166
[email protected]26418b72011-03-30 14:07:397#include "base/logging.h"
brettwf00b9b42016-02-01 22:11:388#include "components/prefs/pref_notifier.h"
9#include "components/prefs/pref_service.h"
[email protected]ee91666a2010-09-21 02:40:0010
[email protected]26418b72011-03-30 14:07:3911namespace subtle {
12
13ScopedUserPrefUpdateBase::ScopedUserPrefUpdateBase(PrefService* service,
georgesak7da6e9d2014-12-03 01:10:2914 const std::string& path)
Ivan Kotenkov75b1c3a2017-10-24 14:47:2415 : service_(service), path_(path), value_(nullptr) {
gab6e1fb5352017-05-31 18:27:1216 DCHECK_CALLED_ON_VALID_SEQUENCE(service_->sequence_checker_);
[email protected]86bb4e652014-07-10 17:09:5117}
[email protected]26418b72011-03-30 14:07:3918
19ScopedUserPrefUpdateBase::~ScopedUserPrefUpdateBase() {
gab6e1fb5352017-05-31 18:27:1220 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
[email protected]26418b72011-03-30 14:07:3921 Notify();
22}
23
[email protected]a644f1e22013-12-24 15:31:2124base::Value* ScopedUserPrefUpdateBase::GetValueOfType(base::Value::Type type) {
gab6e1fb5352017-05-31 18:27:1225 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
[email protected]26418b72011-03-30 14:07:3926 if (!value_)
georgesak7da6e9d2014-12-03 01:10:2927 value_ = service_->GetMutableUserPref(path_, type);
jdoerrie2261b1fb2019-05-08 08:31:5628
29 // |value_| might be downcast to base::DictionaryValue or base::ListValue,
30 // side-stepping CHECKs built into base::Value. Thus we need to be certain
31 // that the type matches.
32 if (value_)
33 CHECK_EQ(value_->type(), type);
[email protected]26418b72011-03-30 14:07:3934 return value_;
35}
36
37void ScopedUserPrefUpdateBase::Notify() {
38 if (value_) {
39 service_->ReportUserPrefChanged(path_);
Ivan Kotenkov75b1c3a2017-10-24 14:47:2440 value_ = nullptr;
[email protected]26418b72011-03-30 14:07:3941 }
42}
43
44} // namespace subtle