brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 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 | // |
| 5 | // A helper class that assists preferences in firing notifications when lists |
| 6 | // or dictionaries are changed. |
| 7 | |
brettw | 06650868 | 2016-02-03 08:22:02 | [diff] [blame] | 8 | #ifndef COMPONENTS_PREFS_SCOPED_USER_PREF_UPDATE_H_ |
| 9 | #define COMPONENTS_PREFS_SCOPED_USER_PREF_UPDATE_H_ |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 10 | |
| 11 | #include <string> |
| 12 | |
| 13 | #include "base/macros.h" |
gab | 6e1fb535 | 2017-05-31 18:27:12 | [diff] [blame] | 14 | #include "base/sequence_checker.h" |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 15 | #include "base/values.h" |
brettw | f00b9b4 | 2016-02-01 22:11:38 | [diff] [blame] | 16 | #include "components/prefs/pref_service.h" |
Brett Wilson | 5c6cf26 | 2017-09-09 02:05:54 | [diff] [blame] | 17 | #include "components/prefs/prefs_export.h" |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 18 | |
| 19 | class PrefService; |
| 20 | |
| 21 | namespace base { |
| 22 | class DictionaryValue; |
| 23 | class ListValue; |
| 24 | } |
| 25 | |
| 26 | namespace 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. |
gab | 6e1fb535 | 2017-05-31 18:27:12 | [diff] [blame] | 34 | class COMPONENTS_PREFS_EXPORT ScopedUserPrefUpdateBase { |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 35 | 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 | |
gab | 6e1fb535 | 2017-05-31 18:27:12 | [diff] [blame] | 56 | SEQUENCE_CHECKER(sequence_checker_); |
| 57 | |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 58 | 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. |
| 68 | template <typename T, base::Value::Type type_enum_value> |
| 69 | class 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 | |
| 104 | typedef ScopedUserPrefUpdate<base::DictionaryValue, |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 105 | base::Value::Type::DICTIONARY> |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 106 | DictionaryPrefUpdate; |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 107 | typedef ScopedUserPrefUpdate<base::ListValue, base::Value::Type::LIST> |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 108 | ListPrefUpdate; |
| 109 | |
brettw | 06650868 | 2016-02-03 08:22:02 | [diff] [blame] | 110 | #endif // COMPONENTS_PREFS_SCOPED_USER_PREF_UPDATE_H_ |