| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #ifndef CHROME_BROWSER_PREFS_PREF_VALUE_STORE_H_ | 5 #ifndef CHROME_BROWSER_PREFS_PREF_VALUE_STORE_H_ |
| 6 #define CHROME_BROWSER_PREFS_PREF_VALUE_STORE_H_ | 6 #define CHROME_BROWSER_PREFS_PREF_VALUE_STORE_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 // | 32 // |
| 33 // Unless otherwise explicitly noted, all of the methods of this class must | 33 // Unless otherwise explicitly noted, all of the methods of this class must |
| 34 // be called on the UI thread. | 34 // be called on the UI thread. |
| 35 class PrefValueStore : public base::RefCountedThreadSafe<PrefValueStore> { | 35 class PrefValueStore : public base::RefCountedThreadSafe<PrefValueStore> { |
| 36 public: | 36 public: |
| 37 // Returns a new PrefValueStore with all applicable PrefStores. The | 37 // Returns a new PrefValueStore with all applicable PrefStores. The |
| 38 // |pref_filename| points to the user preference file. The |profile| is the | 38 // |pref_filename| points to the user preference file. The |profile| is the |
| 39 // one to which these preferences apply; it may be NULL if we're dealing | 39 // one to which these preferences apply; it may be NULL if we're dealing |
| 40 // with the local state. If |pref_filename| is empty, the user PrefStore will | 40 // with the local state. If |pref_filename| is empty, the user PrefStore will |
| 41 // not be created. If |user_only| is true, no PrefStores will be created | 41 // not be created. If |user_only| is true, no PrefStores will be created |
| 42 // other than the user PrefStore (if |pref_filename| is not empty). This | 42 // other than the user and default PrefStores. This should not normally be |
| 43 // should not normally be called directly: the usual way to create a | 43 // called directly: the usual way to create a PrefValueStore is by creating a |
| 44 // PrefValueStore is by creating a PrefService. | 44 // PrefService. |
| 45 static PrefValueStore* CreatePrefValueStore(const FilePath& pref_filename, | 45 static PrefValueStore* CreatePrefValueStore(const FilePath& pref_filename, |
| 46 Profile* profile, | 46 Profile* profile, |
| 47 bool user_only); | 47 bool user_only); |
| 48 | 48 |
| 49 ~PrefValueStore(); | 49 ~PrefValueStore(); |
| 50 | 50 |
| 51 // Get the preference value for the given preference name. | 51 // Get the preference value for the given preference name. |
| 52 // Return true if a value for the given preference name was found. | 52 // Return true if a value for the given preference name was found in any of |
| 53 // the available PrefStores. Most callers should use Preference::GetValue() |
| 54 // instead of calling this method directly. |
| 53 bool GetValue(const std::string& name, Value** out_value) const; | 55 bool GetValue(const std::string& name, Value** out_value) const; |
| 54 | 56 |
| 55 // Read preference values into the three PrefStores so that they are available | 57 // Read preference values into the three PrefStores so that they are available |
| 56 // through the GetValue method. Return the first error that occurs (but | 58 // through the GetValue method. Return the first error that occurs (but |
| 57 // continue reading the remaining PrefStores). | 59 // continue reading the remaining PrefStores). |
| 58 PrefStore::PrefReadError ReadPrefs(); | 60 PrefStore::PrefReadError ReadPrefs(); |
| 59 | 61 |
| 60 // Persists prefs (to disk or elsewhere). Returns true if writing values was | 62 // Persists prefs (to disk or elsewhere). Returns true if writing values was |
| 61 // successful. In practice, only the user prefs are expected to be written | 63 // successful. In practice, only the user prefs are expected to be written |
| 62 // out. | 64 // out. |
| 63 bool WritePrefs(); | 65 bool WritePrefs(); |
| 64 | 66 |
| 65 // Calls the method ScheduleWritePrefs on the PrefStores. In practice, only | 67 // Calls the method ScheduleWritePrefs on the PrefStores. In practice, only |
| 66 // the user prefs are expected to be written out. | 68 // the user prefs are expected to be written out. |
| 67 void ScheduleWritePrefs(); | 69 void ScheduleWritePrefs(); |
| 68 | 70 |
| 69 // Returns true if the PrefValueStore contains the given preference. | 71 // Returns true if the PrefValueStore contains the given preference. |
| 70 bool HasPrefPath(const char* name) const; | 72 bool HasPrefPath(const char* name) const; |
| 71 | 73 |
| 72 // Returns true if the effective value of the preference has changed from its | 74 // Called by the PrefNotifier when the value of the preference at |path| has |
| 73 // |old_value| (which should be the effective value of the preference as | 75 // changed, been added, or been removed in one of the PrefStores. The |
| 74 // reported by GetValue() or the PrefService before the PrefStore changed it), | 76 // |new_store| is the PrefStoreType of the caller. Returns true if the |
| 75 // or if the store controlling the pref has changed. Virtual so it can be | 77 // effective value of the preference has changed, or if the store controlling |
| 76 // mocked for a unit test. | 78 // the pref has changed. Virtual so it can be mocked for a unit test. |
| 77 // TODO(pamg): If we're setting the same value as we already had, into the | |
| 78 // same store that was controlling it before, and there's also a value set in | |
| 79 // a lower-priority store, *and* we're not the highest-priority store, then | |
| 80 // this will return true when it shouldn't. Fix that if it causes problems. | |
| 81 virtual bool PrefHasChanged(const char* path, | 79 virtual bool PrefHasChanged(const char* path, |
| 82 PrefNotifier::PrefStoreType new_store, | 80 PrefNotifier::PrefStoreType new_store); |
| 83 const Value* old_value); | |
| 84 | 81 |
| 85 // Returns true if the PrefValueStore is read-only. | 82 // Returns true if the PrefValueStore is read-only. |
| 86 // Because the managed and recommended PrefStores are always read-only, the | 83 // Because the managed and recommended PrefStores are always read-only, the |
| 87 // PrefValueStore as a whole is read-only if the PrefStore containing the user | 84 // PrefValueStore as a whole is read-only if the PrefStore containing the user |
| 88 // preferences is read-only. | 85 // preferences is read-only. |
| 89 bool ReadOnly(); | 86 bool ReadOnly(); |
| 90 | 87 |
| 91 // Alters the user-defined value of a preference. Even if the preference is | 88 // Alters the user-defined value of a preference. Even if the preference is |
| 92 // managed this method allows the user-defined value of the preference to be | 89 // managed this method allows the user-defined value of the preference to be |
| 93 // set. But GetValue calls will not return this value as long as the | 90 // set. But GetValue calls will not return this value as long as the |
| 94 // preference is managed. Instead GetValue will return the managed value | 91 // preference is managed. Instead GetValue will return the managed value |
| 95 // of the preference. Note that the PrefValueStore takes the ownership of | 92 // of the preference. Note that the PrefValueStore takes the ownership of |
| 96 // the value referenced by |in_value|. It is an error to call this when no | 93 // the value referenced by |in_value|. It is an error to call this when no |
| 97 // user PrefStore has been set. | 94 // user PrefStore has been set. Returns true if the user-set value of the |
| 98 void SetUserPrefValue(const char* name, Value* in_value); | 95 // preference was newly added or changed. |
| 96 bool SetUserPrefValue(const char* name, Value* in_value); |
| 99 | 97 |
| 100 // Removes a value from the PrefValueStore. If a preference is managed | 98 // Removes a value from the user PrefStore. If a preference is managed |
| 101 // or recommended this function should have no effect. | 99 // this function should have no visible effect. Returns true if there was a |
| 102 void RemoveUserPrefValue(const char* name); | 100 // user-set value to be removed. |
| 101 bool RemoveUserPrefValue(const char* name); |
| 102 |
| 103 // Sets a value in the DefaultPrefStore, which takes ownership of the Value. |
| 104 void SetDefaultPrefValue(const char* name, Value* in_value); |
| 103 | 105 |
| 104 // These methods return true if a preference with the given name is in the | 106 // These methods return true if a preference with the given name is in the |
| 105 // indicated pref store, even if that value is currently being overridden by | 107 // indicated pref store, even if that value is currently being overridden by |
| 106 // a higher-priority source. | 108 // a higher-priority source. |
| 107 bool PrefValueInManagedStore(const char* name); | 109 bool PrefValueInManagedStore(const char* name) const; |
| 108 bool PrefValueInExtensionStore(const char* name); | 110 bool PrefValueInExtensionStore(const char* name) const; |
| 109 bool PrefValueInUserStore(const char* name); | 111 bool PrefValueInUserStore(const char* name) const; |
| 110 | 112 |
| 111 // These methods return true if a preference with the given name is actually | 113 // These methods return true if a preference with the given name is actually |
| 112 // being controlled by the indicated pref store and not being overridden by | 114 // being controlled by the indicated pref store and not being overridden by |
| 113 // a higher-priority source. | 115 // a higher-priority source. |
| 114 bool PrefValueFromExtensionStore(const char* name); | 116 bool PrefValueFromExtensionStore(const char* name) const; |
| 115 bool PrefValueFromUserStore(const char* name); | 117 bool PrefValueFromUserStore(const char* name) const; |
| 118 bool PrefValueFromDefaultStore(const char* name) const; |
| 116 | 119 |
| 117 // Check whether a Preference value is modifiable by the user, i.e. whether | 120 // Check whether a Preference value is modifiable by the user, i.e. whether |
| 118 // there is no higher-priority source controlling it. | 121 // there is no higher-priority source controlling it. |
| 119 bool PrefValueUserModifiable(const char* name); | 122 bool PrefValueUserModifiable(const char* name) const; |
| 123 |
| 124 // Returns the pref store type identifying the source that controls the |
| 125 // Preference identified by |name|. If none of the sources has a value, |
| 126 // PrefNotifier::INVALID_STORE is returned. In practice, the default PrefStore |
| 127 // should always have a value for any registered preferencem, so INVALID_STORE |
| 128 // indicates an error. |
| 129 PrefNotifier::PrefStoreType ControllingPrefStoreForPref( |
| 130 const char* name) const; |
| 120 | 131 |
| 121 // Signature of callback triggered after policy refresh. Parameter is not | 132 // Signature of callback triggered after policy refresh. Parameter is not |
| 122 // passed as reference to prevent passing along a pointer to a set whose | 133 // passed as reference to prevent passing along a pointer to a set whose |
| 123 // lifecycle is managed in another thread. | 134 // lifecycle is managed in another thread. |
| 124 typedef Callback1<std::vector<std::string> >::Type AfterRefreshCallback; | 135 typedef Callback1<std::vector<std::string> >::Type AfterRefreshCallback; |
| 125 | 136 |
| 126 // Called as a result of a notification of policy change. Triggers a | 137 // Called as a result of a notification of policy change. Triggers a |
| 127 // reload of managed preferences from policy. Caller must pass in | 138 // reload of managed preferences from policy. Caller must pass in |
| 128 // new, uninitialized managed and recommended PrefStores in | 139 // new, uninitialized managed and recommended PrefStores in |
| 129 // |managed_pref_store| and |recommended_pref_store| respectively, since | 140 // |managed_pref_store| and |recommended_pref_store| respectively, since |
| 130 // PrefValueStore doesn't know about policy-specific PrefStores. | 141 // PrefValueStore doesn't know about policy-specific PrefStores. |
| 131 // |callback| is called with the set of preferences changed by the policy | 142 // |callback| is called with the set of preferences changed by the policy |
| 132 // refresh. |callback| is called on the caller's thread as a Task | 143 // refresh. |callback| is called on the caller's thread as a Task |
| 133 // after RefreshPolicyPrefs has returned. RefreshPolicyPrefs takes ownership | 144 // after RefreshPolicyPrefs has returned. RefreshPolicyPrefs takes ownership |
| 134 // of the |callback| object. | 145 // of the |callback| object. |
| 135 void RefreshPolicyPrefs(PrefStore* managed_pref_store, | 146 void RefreshPolicyPrefs(PrefStore* managed_pref_store, |
| 136 PrefStore* recommended_pref_store, | 147 PrefStore* recommended_pref_store, |
| 137 AfterRefreshCallback* callback); | 148 AfterRefreshCallback* callback); |
| 138 | 149 |
| 139 protected: | 150 protected: |
| 140 // In decreasing order of precedence: | 151 // In decreasing order of precedence: |
| 141 // |managed_prefs| contains all managed (policy) preference values. | 152 // |managed_prefs| contains all managed (policy) preference values. |
| 142 // |extension_prefs| contains preference values set by extensions. | 153 // |extension_prefs| contains preference values set by extensions. |
| 143 // |command_line_prefs| contains preference values set by command-line | 154 // |command_line_prefs| contains preference values set by command-line |
| 144 // switches. | 155 // switches. |
| 145 // |user_prefs| contains all user-set preference values. | 156 // |user_prefs| contains all user-set preference values. |
| 146 // |recommended_prefs| contains all recommended (policy) preference values. | 157 // |recommended_prefs| contains all recommended (policy) preference values. |
| 158 // |default_prefs| contains application-default preference values. It must |
| 159 // be non-null if any preferences are to be registered. |
| 147 // | 160 // |
| 148 // This constructor should only be used internally, or by subclasses in | 161 // This constructor should only be used internally, or by subclasses in |
| 149 // testing. The usual way to create a PrefValueStore is by creating a | 162 // testing. The usual way to create a PrefValueStore is by creating a |
| 150 // PrefService. | 163 // PrefService. |
| 151 PrefValueStore(PrefStore* managed_prefs, | 164 PrefValueStore(PrefStore* managed_prefs, |
| 152 PrefStore* extension_prefs, | 165 PrefStore* extension_prefs, |
| 153 PrefStore* command_line_prefs, | 166 PrefStore* command_line_prefs, |
| 154 PrefStore* user_prefs, | 167 PrefStore* user_prefs, |
| 155 PrefStore* recommended_prefs); | 168 PrefStore* recommended_prefs, |
| 169 PrefStore* default_prefs); |
| 156 | 170 |
| 157 private: | 171 private: |
| 158 friend class PrefValueStoreTest; | 172 friend class PrefValueStoreTest; |
| 159 FRIEND_TEST_ALL_PREFIXES(PrefValueStoreTest, | 173 FRIEND_TEST_ALL_PREFIXES(PrefValueStoreTest, |
| 160 TestRefreshPolicyPrefsCompletion); | 174 TestRefreshPolicyPrefsCompletion); |
| 161 | 175 |
| 162 scoped_ptr<PrefStore> pref_stores_[PrefNotifier::PREF_STORE_TYPE_MAX + 1]; | 176 scoped_ptr<PrefStore> pref_stores_[PrefNotifier::PREF_STORE_TYPE_MAX + 1]; |
| 163 | 177 |
| 164 bool PrefValueInStore(const char* name, PrefNotifier::PrefStoreType type); | 178 bool PrefValueInStore(const char* name, |
| 165 | 179 PrefNotifier::PrefStoreType type) const; |
| 166 // Returns true if the preference |name| is found in any PrefStore starting | |
| 167 // just beyond the |boundary|, non-inclusive, and checking either | |
| 168 // higher-priority stores (if |higher_priority| is true) or lower-priority | |
| 169 // stores. | |
| 170 bool PrefValueInStoreRange(const char* name, | |
| 171 PrefNotifier::PrefStoreType boundary, | |
| 172 bool higher_priority); | |
| 173 | |
| 174 // Returns the pref store type identifying the source that controls the | |
| 175 // Preference identified by |name|. If none of the sources has a value, | |
| 176 // INVALID is returned. | |
| 177 PrefNotifier::PrefStoreType ControllingPrefStoreForPref(const char* name); | |
| 178 | 180 |
| 179 // Called during policy refresh after ReadPrefs completes on the thread | 181 // Called during policy refresh after ReadPrefs completes on the thread |
| 180 // that initiated the policy refresh. RefreshPolicyPrefsCompletion takes | 182 // that initiated the policy refresh. RefreshPolicyPrefsCompletion takes |
| 181 // ownership of the |callback| object. | 183 // ownership of the |callback| object. |
| 182 void RefreshPolicyPrefsCompletion( | 184 void RefreshPolicyPrefsCompletion( |
| 183 PrefStore* new_managed_pref_store, | 185 PrefStore* new_managed_pref_store, |
| 184 PrefStore* new_recommended_pref_store, | 186 PrefStore* new_recommended_pref_store, |
| 185 AfterRefreshCallback* callback); | 187 AfterRefreshCallback* callback); |
| 186 | 188 |
| 187 // Called during policy refresh to do the ReadPrefs on the FILE thread. | 189 // Called during policy refresh to do the ReadPrefs on the FILE thread. |
| 188 // RefreshPolicyPrefsOnFileThread takes ownership of the |callback| object. | 190 // RefreshPolicyPrefsOnFileThread takes ownership of the |callback| object. |
| 189 void RefreshPolicyPrefsOnFileThread( | 191 void RefreshPolicyPrefsOnFileThread( |
| 190 ChromeThread::ID calling_thread_id, | 192 ChromeThread::ID calling_thread_id, |
| 191 PrefStore* new_managed_pref_store, | 193 PrefStore* new_managed_pref_store, |
| 192 PrefStore* new_recommended_pref_store, | 194 PrefStore* new_recommended_pref_store, |
| 193 AfterRefreshCallback* callback); | 195 AfterRefreshCallback* callback); |
| 194 | 196 |
| 195 DISALLOW_COPY_AND_ASSIGN(PrefValueStore); | 197 DISALLOW_COPY_AND_ASSIGN(PrefValueStore); |
| 196 }; | 198 }; |
| 197 | 199 |
| 198 #endif // CHROME_BROWSER_PREFS_PREF_VALUE_STORE_H_ | 200 #endif // CHROME_BROWSER_PREFS_PREF_VALUE_STORE_H_ |
| OLD | NEW |