| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | 4 // |
| 5 // A helper class that assists preferences in firing notifications when lists | 5 // A helper class that assists preferences in firing notifications when lists |
| 6 // or dictionaries are changed. | 6 // or dictionaries are changed. |
| 7 | 7 |
| 8 #ifndef CHROME_BROWSER_PREFS_SCOPED_USER_PREF_UPDATE_H_ | 8 #ifndef CHROME_BROWSER_PREFS_SCOPED_USER_PREF_UPDATE_H_ |
| 9 #define CHROME_BROWSER_PREFS_SCOPED_USER_PREF_UPDATE_H_ | 9 #define CHROME_BROWSER_PREFS_SCOPED_USER_PREF_UPDATE_H_ |
| 10 #pragma once | 10 #pragma once |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 // and getting access to PrefService::GetMutableUserPref and | 33 // and getting access to PrefService::GetMutableUserPref and |
| 34 // PrefService::ReportUserPrefChanged. | 34 // PrefService::ReportUserPrefChanged. |
| 35 class ScopedUserPrefUpdateBase : public base::NonThreadSafe { | 35 class ScopedUserPrefUpdateBase : public base::NonThreadSafe { |
| 36 protected: | 36 protected: |
| 37 ScopedUserPrefUpdateBase(PrefService* service, const char* path); | 37 ScopedUserPrefUpdateBase(PrefService* service, const char* path); |
| 38 | 38 |
| 39 // Calls Notify(). | 39 // Calls Notify(). |
| 40 virtual ~ScopedUserPrefUpdateBase(); | 40 virtual ~ScopedUserPrefUpdateBase(); |
| 41 | 41 |
| 42 // Sets |value_| to |service_|->GetMutableUserPref and returns it. | 42 // Sets |value_| to |service_|->GetMutableUserPref and returns it. |
| 43 base::Value* Get(Value::ValueType type); | 43 base::Value* Get(base::Value::Type type); |
| 44 | 44 |
| 45 private: | 45 private: |
| 46 // If |value_| is not null, triggers a notification of PrefObservers and | 46 // If |value_| is not null, triggers a notification of PrefObservers and |
| 47 // resets |value_|. | 47 // resets |value_|. |
| 48 virtual void Notify(); | 48 virtual void Notify(); |
| 49 | 49 |
| 50 // Weak pointer. | 50 // Weak pointer. |
| 51 PrefService* service_; | 51 PrefService* service_; |
| 52 // Path of the preference being updated. | 52 // Path of the preference being updated. |
| 53 std::string path_; | 53 std::string path_; |
| 54 // Cache of value from user pref store (set between Get() and Notify() calls). | 54 // Cache of value from user pref store (set between Get() and Notify() calls). |
| 55 base::Value* value_; | 55 base::Value* value_; |
| 56 | 56 |
| 57 DISALLOW_COPY_AND_ASSIGN(ScopedUserPrefUpdateBase); | 57 DISALLOW_COPY_AND_ASSIGN(ScopedUserPrefUpdateBase); |
| 58 }; | 58 }; |
| 59 | 59 |
| 60 } // namespace subtle | 60 } // namespace subtle |
| 61 | 61 |
| 62 // Class to support modifications to DictionaryValues and ListValues while | 62 // Class to support modifications to DictionaryValues and ListValues while |
| 63 // guaranteeing that PrefObservers are notified of changed values. | 63 // guaranteeing that PrefObservers are notified of changed values. |
| 64 // | 64 // |
| 65 // This class may only be used on the UI thread as it requires access to the | 65 // This class may only be used on the UI thread as it requires access to the |
| 66 // PrefService. | 66 // PrefService. |
| 67 template <typename T, Value::ValueType type_enum_value> | 67 template <typename T, base::Value::Type type_enum_value> |
| 68 class ScopedUserPrefUpdate : public subtle::ScopedUserPrefUpdateBase { | 68 class ScopedUserPrefUpdate : public subtle::ScopedUserPrefUpdateBase { |
| 69 public: | 69 public: |
| 70 ScopedUserPrefUpdate(PrefService* service, const char* path) | 70 ScopedUserPrefUpdate(PrefService* service, const char* path) |
| 71 : ScopedUserPrefUpdateBase(service, path) {} | 71 : ScopedUserPrefUpdateBase(service, path) {} |
| 72 | 72 |
| 73 // Triggers an update notification if Get() was called. | 73 // Triggers an update notification if Get() was called. |
| 74 virtual ~ScopedUserPrefUpdate() {} | 74 virtual ~ScopedUserPrefUpdate() {} |
| 75 | 75 |
| 76 // Returns a mutable |T| instance that | 76 // Returns a mutable |T| instance that |
| 77 // - is already in the user pref store, or | 77 // - is already in the user pref store, or |
| (...skipping 19 matching lines...) Expand all Loading... |
| 97 | 97 |
| 98 private: | 98 private: |
| 99 DISALLOW_COPY_AND_ASSIGN(ScopedUserPrefUpdate); | 99 DISALLOW_COPY_AND_ASSIGN(ScopedUserPrefUpdate); |
| 100 }; | 100 }; |
| 101 | 101 |
| 102 typedef ScopedUserPrefUpdate<base::DictionaryValue, Value::TYPE_DICTIONARY> | 102 typedef ScopedUserPrefUpdate<base::DictionaryValue, Value::TYPE_DICTIONARY> |
| 103 DictionaryPrefUpdate; | 103 DictionaryPrefUpdate; |
| 104 typedef ScopedUserPrefUpdate<base::ListValue, Value::TYPE_LIST> ListPrefUpdate; | 104 typedef ScopedUserPrefUpdate<base::ListValue, Value::TYPE_LIST> ListPrefUpdate; |
| 105 | 105 |
| 106 #endif // CHROME_BROWSER_PREFS_SCOPED_USER_PREF_UPDATE_H_ | 106 #endif // CHROME_BROWSER_PREFS_SCOPED_USER_PREF_UPDATE_H_ |
| OLD | NEW |