| 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 |
| 11 | 11 |
| 12 #include <string> | 12 #include <string> |
| 13 | 13 |
| 14 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
| 15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/threading/non_thread_safe.h" | 16 #include "base/threading/non_thread_safe.h" |
| 17 #include "base/values.h" | 17 #include "base/values.h" |
| 18 #include "chrome/browser/prefs/pref_service.h" | 18 #include "chrome/browser/prefs/pref_service.h" |
| 19 | 19 |
| 20 class PrefService; |
| 21 |
| 22 namespace base { |
| 20 class DictionaryValue; | 23 class DictionaryValue; |
| 21 class ListValue; | 24 class ListValue; |
| 22 class PrefService; | 25 } |
| 23 | 26 |
| 24 namespace subtle { | 27 namespace subtle { |
| 25 | 28 |
| 26 // Base class for ScopedUserPrefUpdateTemplate that contains the parts | 29 // Base class for ScopedUserPrefUpdateTemplate that contains the parts |
| 27 // that do not depend on ScopedUserPrefUpdateTemplate's template parameter. | 30 // that do not depend on ScopedUserPrefUpdateTemplate's template parameter. |
| 28 // | 31 // |
| 29 // We need this base class mostly for making it a friend of PrefService | 32 // We need this base class mostly for making it a friend of PrefService |
| 30 // and getting access to PrefService::GetMutableUserPref and | 33 // and getting access to PrefService::GetMutableUserPref and |
| 31 // PrefService::ReportUserPrefChanged. | 34 // PrefService::ReportUserPrefChanged. |
| 32 class ScopedUserPrefUpdateBase : public base::NonThreadSafe { | 35 class ScopedUserPrefUpdateBase : public base::NonThreadSafe { |
| 33 protected: | 36 protected: |
| 34 ScopedUserPrefUpdateBase(PrefService* service, const char* path); | 37 ScopedUserPrefUpdateBase(PrefService* service, const char* path); |
| 35 | 38 |
| 36 // Calls Notify(). | 39 // Calls Notify(). |
| 37 virtual ~ScopedUserPrefUpdateBase(); | 40 virtual ~ScopedUserPrefUpdateBase(); |
| 38 | 41 |
| 39 // Sets |value_| to |service_|->GetMutableUserPref and returns it. | 42 // Sets |value_| to |service_|->GetMutableUserPref and returns it. |
| 40 Value* Get(Value::ValueType type); | 43 base::Value* Get(Value::ValueType type); |
| 41 | 44 |
| 42 private: | 45 private: |
| 43 // If |value_| is not null, triggers a notification of PrefObservers and | 46 // If |value_| is not null, triggers a notification of PrefObservers and |
| 44 // resets |value_|. | 47 // resets |value_|. |
| 45 virtual void Notify(); | 48 virtual void Notify(); |
| 46 | 49 |
| 47 // Weak pointer. | 50 // Weak pointer. |
| 48 PrefService* service_; | 51 PrefService* service_; |
| 49 // Path of the preference being updated. | 52 // Path of the preference being updated. |
| 50 std::string path_; | 53 std::string path_; |
| 51 // 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). |
| 52 Value* value_; | 55 base::Value* value_; |
| 53 | 56 |
| 54 DISALLOW_COPY_AND_ASSIGN(ScopedUserPrefUpdateBase); | 57 DISALLOW_COPY_AND_ASSIGN(ScopedUserPrefUpdateBase); |
| 55 }; | 58 }; |
| 56 | 59 |
| 57 } // namespace subtle | 60 } // namespace subtle |
| 58 | 61 |
| 59 // Class to support modifications to DictionaryValues and ListValues while | 62 // Class to support modifications to DictionaryValues and ListValues while |
| 60 // guaranteeing that PrefObservers are notified of changed values. | 63 // guaranteeing that PrefObservers are notified of changed values. |
| 61 // | 64 // |
| 62 // 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 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 89 } | 92 } |
| 90 | 93 |
| 91 T* operator->() { | 94 T* operator->() { |
| 92 return Get(); | 95 return Get(); |
| 93 } | 96 } |
| 94 | 97 |
| 95 private: | 98 private: |
| 96 DISALLOW_COPY_AND_ASSIGN(ScopedUserPrefUpdate); | 99 DISALLOW_COPY_AND_ASSIGN(ScopedUserPrefUpdate); |
| 97 }; | 100 }; |
| 98 | 101 |
| 99 typedef ScopedUserPrefUpdate<DictionaryValue, Value::TYPE_DICTIONARY> | 102 typedef ScopedUserPrefUpdate<base::DictionaryValue, Value::TYPE_DICTIONARY> |
| 100 DictionaryPrefUpdate; | 103 DictionaryPrefUpdate; |
| 101 typedef ScopedUserPrefUpdate<ListValue, Value::TYPE_LIST> ListPrefUpdate; | 104 typedef ScopedUserPrefUpdate<base::ListValue, Value::TYPE_LIST> ListPrefUpdate; |
| 102 | 105 |
| 103 #endif // CHROME_BROWSER_PREFS_SCOPED_USER_PREF_UPDATE_H_ | 106 #endif // CHROME_BROWSER_PREFS_SCOPED_USER_PREF_UPDATE_H_ |
| OLD | NEW |