[email protected] | 9eec53fe | 2013-10-30 20:21:17 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
[email protected] | dd8fa1a99 | 2010-04-05 23:56:16 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
brettw | f00b9b4 | 2016-02-01 22:11:38 | [diff] [blame] | 5 | #include "components/prefs/scoped_user_pref_update.h" |
[email protected] | dd8fa1a99 | 2010-04-05 23:56:16 | [diff] [blame] | 6 | |
[email protected] | 26418b7 | 2011-03-30 14:07:39 | [diff] [blame] | 7 | #include "base/logging.h" |
brettw | f00b9b4 | 2016-02-01 22:11:38 | [diff] [blame] | 8 | #include "components/prefs/pref_notifier.h" |
9 | #include "components/prefs/pref_service.h" | ||||
[email protected] | ee91666a | 2010-09-21 02:40:00 | [diff] [blame] | 10 | |
[email protected] | 26418b7 | 2011-03-30 14:07:39 | [diff] [blame] | 11 | namespace subtle { |
12 | |||||
13 | ScopedUserPrefUpdateBase::ScopedUserPrefUpdateBase(PrefService* service, | ||||
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 14 | const std::string& path) |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 15 | : service_(service), path_(path), value_(nullptr) { |
gab | 6e1fb535 | 2017-05-31 18:27:12 | [diff] [blame] | 16 | DCHECK_CALLED_ON_VALID_SEQUENCE(service_->sequence_checker_); |
[email protected] | 86bb4e65 | 2014-07-10 17:09:51 | [diff] [blame] | 17 | } |
[email protected] | 26418b7 | 2011-03-30 14:07:39 | [diff] [blame] | 18 | |
19 | ScopedUserPrefUpdateBase::~ScopedUserPrefUpdateBase() { | ||||
gab | 6e1fb535 | 2017-05-31 18:27:12 | [diff] [blame] | 20 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
[email protected] | 26418b7 | 2011-03-30 14:07:39 | [diff] [blame] | 21 | Notify(); |
22 | } | ||||
23 | |||||
[email protected] | a644f1e2 | 2013-12-24 15:31:21 | [diff] [blame] | 24 | base::Value* ScopedUserPrefUpdateBase::GetValueOfType(base::Value::Type type) { |
gab | 6e1fb535 | 2017-05-31 18:27:12 | [diff] [blame] | 25 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
[email protected] | 26418b7 | 2011-03-30 14:07:39 | [diff] [blame] | 26 | if (!value_) |
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 27 | value_ = service_->GetMutableUserPref(path_, type); |
jdoerrie | 2261b1fb | 2019-05-08 08:31:56 | [diff] [blame] | 28 | |
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] | 26418b7 | 2011-03-30 14:07:39 | [diff] [blame] | 34 | return value_; |
35 | } | ||||
36 | |||||
37 | void ScopedUserPrefUpdateBase::Notify() { | ||||
38 | if (value_) { | ||||
39 | service_->ReportUserPrefChanged(path_); | ||||
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 40 | value_ = nullptr; |
[email protected] | 26418b7 | 2011-03-30 14:07:39 | [diff] [blame] | 41 | } |
42 | } | ||||
43 | |||||
44 | } // namespace subtle |