blob: 7de6bfbdf29865e9080c72e4c2070dae5f7903e0 [file] [log] [blame]
brettw58cd1f12016-01-30 05:56:051// 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//
5// A helper class that assists preferences in firing notifications when lists
6// or dictionaries are changed.
7
brettw066508682016-02-03 08:22:028#ifndef COMPONENTS_PREFS_SCOPED_USER_PREF_UPDATE_H_
9#define COMPONENTS_PREFS_SCOPED_USER_PREF_UPDATE_H_
brettw58cd1f12016-01-30 05:56:0510
11#include <string>
12
13#include "base/macros.h"
gab6e1fb5352017-05-31 18:27:1214#include "base/sequence_checker.h"
brettw58cd1f12016-01-30 05:56:0515#include "base/values.h"
brettwf00b9b42016-02-01 22:11:3816#include "components/prefs/pref_service.h"
Brett Wilson5c6cf262017-09-09 02:05:5417#include "components/prefs/prefs_export.h"
brettw58cd1f12016-01-30 05:56:0518
19class PrefService;
20
21namespace base {
22class DictionaryValue;
23class ListValue;
24}
25
26namespace subtle {
27
28// Base class for ScopedUserPrefUpdateTemplate that contains the parts
29// that do not depend on ScopedUserPrefUpdateTemplate's template parameter.
30//
31// We need this base class mostly for making it a friend of PrefService
32// and getting access to PrefService::GetMutableUserPref and
33// PrefService::ReportUserPrefChanged.
gab6e1fb5352017-05-31 18:27:1234class COMPONENTS_PREFS_EXPORT ScopedUserPrefUpdateBase {
brettw58cd1f12016-01-30 05:56:0535 protected:
36 ScopedUserPrefUpdateBase(PrefService* service, const std::string& path);
37
38 // Calls Notify().
39 ~ScopedUserPrefUpdateBase();
40
41 // Sets |value_| to |service_|->GetMutableUserPref and returns it.
42 base::Value* GetValueOfType(base::Value::Type type);
43
44 private:
45 // If |value_| is not null, triggers a notification of PrefObservers and
46 // resets |value_|.
47 void Notify();
48
49 // Weak pointer.
50 PrefService* service_;
51 // Path of the preference being updated.
52 std::string path_;
53 // Cache of value from user pref store (set between Get() and Notify() calls).
54 base::Value* value_;
55
gab6e1fb5352017-05-31 18:27:1256 SEQUENCE_CHECKER(sequence_checker_);
57
brettw58cd1f12016-01-30 05:56:0558 DISALLOW_COPY_AND_ASSIGN(ScopedUserPrefUpdateBase);
59};
60
61} // namespace subtle
62
63// Class to support modifications to DictionaryValues and ListValues while
64// guaranteeing that PrefObservers are notified of changed values.
65//
66// This class may only be used on the UI thread as it requires access to the
67// PrefService.
68template <typename T, base::Value::Type type_enum_value>
69class ScopedUserPrefUpdate : public subtle::ScopedUserPrefUpdateBase {
70 public:
71 ScopedUserPrefUpdate(PrefService* service, const std::string& path)
72 : ScopedUserPrefUpdateBase(service, path) {}
73
74 // Triggers an update notification if Get() was called.
75 virtual ~ScopedUserPrefUpdate() {}
76
77 // Returns a mutable |T| instance that
78 // - is already in the user pref store, or
79 // - is (silently) created and written to the user pref store if none existed
80 // before.
81 //
82 // Calling Get() implies that an update notification is necessary at
83 // destruction time.
84 //
85 // The ownership of the return value remains with the user pref store.
86 // Virtual so it can be overriden in subclasses that transform the value
87 // before returning it (for example to return a subelement of a dictionary).
88 virtual T* Get() {
89 return static_cast<T*>(GetValueOfType(type_enum_value));
90 }
91
92 T& operator*() {
93 return *Get();
94 }
95
96 T* operator->() {
97 return Get();
98 }
99
100 private:
101 DISALLOW_COPY_AND_ASSIGN(ScopedUserPrefUpdate);
102};
103
104typedef ScopedUserPrefUpdate<base::DictionaryValue,
jdoerriedc72ee942016-12-07 15:43:28105 base::Value::Type::DICTIONARY>
brettw58cd1f12016-01-30 05:56:05106 DictionaryPrefUpdate;
jdoerriedc72ee942016-12-07 15:43:28107typedef ScopedUserPrefUpdate<base::ListValue, base::Value::Type::LIST>
brettw58cd1f12016-01-30 05:56:05108 ListPrefUpdate;
109
brettw066508682016-02-03 08:22:02110#endif // COMPONENTS_PREFS_SCOPED_USER_PREF_UPDATE_H_