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.
5 // A helper class that assists preferences in firing notifications when lists
6 // or dictionaries are changed.
8 #ifndef BASE_PREFS_SCOPED_USER_PREF_UPDATE_H_
9 #define BASE_PREFS_SCOPED_USER_PREF_UPDATE_H_
13 #include "base/basictypes.h"
14 #include "base/prefs/base_prefs_export.h"
15 #include "base/prefs/pref_service.h"
16 #include "base/threading/non_thread_safe.h"
17 #include "base/values.h"
22 class DictionaryValue;
28 // Base class for ScopedUserPrefUpdateTemplate that contains the parts
29 // that do not depend on ScopedUserPrefUpdateTemplate's template parameter.
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.
34 class BASE_PREFS_EXPORT ScopedUserPrefUpdateBase : public base::NonThreadSafe {
36 ScopedUserPrefUpdateBase(PrefService* service, const char* path);
39 ~ScopedUserPrefUpdateBase();
41 // Sets |value_| to |service_|->GetMutableUserPref and returns it.
42 base::Value* GetValueOfType(base::Value::Type type);
45 // If |value_| is not null, triggers a notification of PrefObservers and
50 PrefService* service_;
51 // Path of the preference being updated.
53 // Cache of value from user pref store (set between Get() and Notify() calls).
56 DISALLOW_COPY_AND_ASSIGN(ScopedUserPrefUpdateBase);
61 // Class to support modifications to DictionaryValues and ListValues while
62 // guaranteeing that PrefObservers are notified of changed values.
64 // This class may only be used on the UI thread as it requires access to the
66 template <typename T, base::Value::Type type_enum_value>
67 class ScopedUserPrefUpdate : public subtle::ScopedUserPrefUpdateBase {
69 ScopedUserPrefUpdate(PrefService* service, const char* path)
70 : ScopedUserPrefUpdateBase(service, path) {}
72 // Triggers an update notification if Get() was called.
73 virtual ~ScopedUserPrefUpdate() {}
75 // Returns a mutable |T| instance that
76 // - is already in the user pref store, or
77 // - is (silently) created and written to the user pref store if none existed
80 // Calling Get() implies that an update notification is necessary at
83 // The ownership of the return value remains with the user pref store.
84 // Virtual so it can be overriden in subclasses that transform the value
85 // before returning it (for example to return a subelement of a dictionary).
87 return static_cast<T*>(GetValueOfType(type_enum_value));
99 DISALLOW_COPY_AND_ASSIGN(ScopedUserPrefUpdate);
102 typedef ScopedUserPrefUpdate<base::DictionaryValue,
103 base::Value::TYPE_DICTIONARY>
104 DictionaryPrefUpdate;
105 typedef ScopedUserPrefUpdate<base::ListValue, base::Value::TYPE_LIST>
108 #endif // BASE_PREFS_SCOPED_USER_PREF_UPDATE_H_