scoped_user_pref_update.cc 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/prefs/scoped_user_pref_update.h"
  5. #include "base/check_op.h"
  6. #include "components/prefs/pref_notifier.h"
  7. #include "components/prefs/pref_service.h"
  8. namespace subtle {
  9. ScopedUserPrefUpdateBase::ScopedUserPrefUpdateBase(PrefService* service,
  10. const std::string& path)
  11. : service_(service), path_(path), value_(nullptr) {
  12. DCHECK_CALLED_ON_VALID_SEQUENCE(service_->sequence_checker_);
  13. }
  14. ScopedUserPrefUpdateBase::~ScopedUserPrefUpdateBase() {
  15. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  16. Notify();
  17. }
  18. base::Value* ScopedUserPrefUpdateBase::GetValueOfType(base::Value::Type type) {
  19. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  20. if (!value_)
  21. value_ = service_->GetMutableUserPref(path_, type);
  22. // |value_| might be downcast to base::DictionaryValue or base::ListValue,
  23. // side-stepping CHECKs built into base::Value. Thus we need to be certain
  24. // that the type matches.
  25. if (value_)
  26. CHECK_EQ(value_->type(), type);
  27. return value_;
  28. }
  29. void ScopedUserPrefUpdateBase::Notify() {
  30. if (value_) {
  31. service_->ReportUserPrefChanged(path_);
  32. value_ = nullptr;
  33. }
  34. }
  35. } // namespace subtle