Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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 #ifndef SERVICES_PREFERENCES_PUBLIC_CPP_SCOPED_PREF_UPDATE_H_ | |
| 6 #define SERVICES_PREFERENCES_PUBLIC_CPP_SCOPED_PREF_UPDATE_H_ | |
| 7 | |
| 8 #include <memory> | |
| 9 #include <set> | |
| 10 #include <string> | |
| 11 #include <vector> | |
| 12 | |
| 13 #include "base/strings/string16.h" | |
| 14 #include "base/strings/string_piece.h" | |
| 15 #include "base/values.h" | |
| 16 | |
| 17 class PrefService; | |
| 18 | |
| 19 namespace prefs { | |
| 20 class DictionaryValueUpdate; | |
| 21 | |
| 22 // An update to a dictionary value pref. | |
|
tibell
2017/04/12 07:13:52
Could you include an example code snippet of how t
| |
| 23 class ScopedDictionaryPrefUpdate { | |
| 24 public: | |
| 25 ScopedDictionaryPrefUpdate(PrefService* service, base::StringPiece path); | |
| 26 | |
| 27 // Notifies if necessary. | |
| 28 virtual ~ScopedDictionaryPrefUpdate(); | |
| 29 | |
| 30 virtual DictionaryValueUpdate* Get(); | |
| 31 | |
| 32 DictionaryValueUpdate* operator->(); | |
| 33 | |
| 34 private: | |
| 35 friend class DictionaryValueUpdate; | |
| 36 // If |value_| is not null, triggers a notification of PrefObservers and | |
| 37 // resets |value_|. | |
| 38 void Notify(); | |
| 39 | |
| 40 void RecordPath(const std::vector<std::string>& path); | |
| 41 DictionaryValueUpdate* Insert( | |
| 42 std::unique_ptr<DictionaryValueUpdate> dictionary_value_update); | |
| 43 | |
| 44 // Weak pointer. | |
| 45 PrefService* service_; | |
| 46 // Path of the preference being updated. | |
| 47 std::string path_; | |
| 48 // Cache of value from user pref store (set between Get() and Notify() calls). | |
| 49 DictionaryValueUpdate* value_; | |
|
tibell
2017/04/12 07:13:52
Owned?
| |
| 50 | |
| 51 std::set<std::vector<std::string>> updated_paths_; | |
| 52 | |
| 53 std::vector<std::unique_ptr<DictionaryValueUpdate>> updates_; | |
| 54 | |
| 55 DISALLOW_COPY_AND_ASSIGN(ScopedDictionaryPrefUpdate); | |
| 56 }; | |
| 57 | |
| 58 // A wrapper around base::DictionaryValue that reports changes to its contents | |
| 59 // to a ScopedDictionaryPrefUpdate. | |
| 60 class DictionaryValueUpdate { | |
| 61 public: | |
| 62 ~DictionaryValueUpdate(); | |
| 63 bool HasKey(base::StringPiece key) const; | |
| 64 | |
| 65 // Returns the number of Values in this dictionary. | |
| 66 size_t size() const; | |
| 67 | |
| 68 // Returns whether the dictionary is empty. | |
| 69 bool empty() const; | |
| 70 | |
| 71 // Clears any current contents of this dictionary. | |
| 72 void Clear(); | |
| 73 | |
| 74 // Sets the Value associated with the given path starting from this object. | |
| 75 // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes | |
| 76 // into the next DictionaryValue down. Obviously, "." can't be used | |
| 77 // within a key, but there are no other restrictions on keys. | |
| 78 // If the key at any step of the way doesn't exist, or exists but isn't | |
| 79 // a DictionaryValue, a new DictionaryValue will be created and attached | |
| 80 // to the path in that location. |in_value| must be non-null. | |
| 81 void Set(base::StringPiece path, std::unique_ptr<base::Value> in_value); | |
| 82 | |
| 83 // Convenience forms of Set(). These methods will replace any existing | |
| 84 // value at that path, even if it has a different type. | |
| 85 void SetBoolean(base::StringPiece path, bool in_value); | |
| 86 void SetInteger(base::StringPiece path, int in_value); | |
| 87 void SetDouble(base::StringPiece path, double in_value); | |
| 88 void SetString(base::StringPiece path, base::StringPiece in_value); | |
| 89 void SetString(base::StringPiece path, const base::string16& in_value); | |
| 90 DictionaryValueUpdate* SetDictionary( | |
| 91 base::StringPiece path, | |
| 92 std::unique_ptr<base::DictionaryValue> in_value); | |
| 93 | |
| 94 // Like Set(), but without special treatment of '.'. This allows e.g. URLs to | |
| 95 // be used as paths. | |
| 96 void SetWithoutPathExpansion(base::StringPiece key, | |
| 97 std::unique_ptr<base::Value> in_value); | |
| 98 | |
| 99 // Convenience forms of SetWithoutPathExpansion(). | |
| 100 void SetBooleanWithoutPathExpansion(base::StringPiece path, bool in_value); | |
| 101 void SetIntegerWithoutPathExpansion(base::StringPiece path, int in_value); | |
| 102 void SetDoubleWithoutPathExpansion(base::StringPiece path, double in_value); | |
| 103 void SetStringWithoutPathExpansion(base::StringPiece path, | |
| 104 base::StringPiece in_value); | |
| 105 void SetStringWithoutPathExpansion(base::StringPiece path, | |
| 106 const base::string16& in_value); | |
| 107 DictionaryValueUpdate* SetDictionaryWithoutPathExpansion( | |
| 108 base::StringPiece path, | |
| 109 std::unique_ptr<base::DictionaryValue> in_value); | |
| 110 | |
| 111 // These are convenience forms of Get(). The value will be retrieved | |
| 112 // and the return value will be true if the path is valid and the value at | |
| 113 // the end of the path can be returned in the form specified. | |
| 114 // |out_value| is optional and will only be set if non-NULL. | |
| 115 bool GetBoolean(base::StringPiece path, bool* out_value) const; | |
| 116 bool GetInteger(base::StringPiece path, int* out_value) const; | |
| 117 // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as | |
| 118 // doubles. | |
| 119 bool GetDouble(base::StringPiece path, double* out_value) const; | |
| 120 bool GetString(base::StringPiece path, std::string* out_value) const; | |
| 121 bool GetString(base::StringPiece path, base::string16* out_value) const; | |
| 122 bool GetBinary(base::StringPiece path, const base::Value** out_value) const; | |
| 123 bool GetDictionary(base::StringPiece path, | |
| 124 const base::DictionaryValue** out_value) const; | |
| 125 bool GetDictionary(base::StringPiece path, DictionaryValueUpdate** out_value); | |
| 126 bool GetList(base::StringPiece path, const base::ListValue** out_value) const; | |
| 127 bool GetList(base::StringPiece path, base::ListValue** out_value); | |
| 128 | |
| 129 // Like Get(), but without special treatment of '.'. This allows e.g. URLs to | |
| 130 // be used as paths. | |
| 131 bool GetBooleanWithoutPathExpansion(base::StringPiece key, | |
| 132 bool* out_value) const; | |
| 133 bool GetIntegerWithoutPathExpansion(base::StringPiece key, | |
| 134 int* out_value) const; | |
| 135 bool GetDoubleWithoutPathExpansion(base::StringPiece key, | |
| 136 double* out_value) const; | |
| 137 bool GetStringWithoutPathExpansion(base::StringPiece key, | |
| 138 std::string* out_value) const; | |
| 139 bool GetStringWithoutPathExpansion(base::StringPiece key, | |
| 140 base::string16* out_value) const; | |
| 141 bool GetDictionaryWithoutPathExpansion( | |
| 142 base::StringPiece key, | |
| 143 const base::DictionaryValue** out_value) const; | |
| 144 bool GetDictionaryWithoutPathExpansion(base::StringPiece key, | |
| 145 DictionaryValueUpdate** out_value); | |
| 146 bool GetListWithoutPathExpansion(base::StringPiece key, | |
| 147 const base::ListValue** out_value) const; | |
| 148 bool GetListWithoutPathExpansion(base::StringPiece key, | |
| 149 base::ListValue** out_value); | |
| 150 | |
| 151 // Removes the Value with the specified path from this dictionary (or one | |
| 152 // of its child dictionaries, if the path is more than just a local key). | |
| 153 // If |out_value| is non-NULL, the removed Value will be passed out via | |
| 154 // |out_value|. If |out_value| is NULL, the removed value will be deleted. | |
| 155 // This method returns true if |path| is a valid path; otherwise it will | |
| 156 // return false and the DictionaryValue object will be unchanged. | |
| 157 bool Remove(base::StringPiece path, std::unique_ptr<base::Value>* out_value); | |
| 158 | |
| 159 // Like Remove(), but without special treatment of '.'. This allows e.g. URLs | |
| 160 // to be used as paths. | |
| 161 bool RemoveWithoutPathExpansion(base::StringPiece key, | |
| 162 std::unique_ptr<base::Value>* out_value); | |
| 163 | |
| 164 // Removes a path, clearing out all dictionaries on |path| that remain empty | |
| 165 // after removing the value at |path|. | |
| 166 bool RemovePath(base::StringPiece path, | |
| 167 std::unique_ptr<base::Value>* out_value); | |
| 168 | |
| 169 base::DictionaryValue* AsDictionary(); | |
| 170 const base::DictionaryValue* AsConstDictionary() const; | |
| 171 | |
| 172 private: | |
| 173 friend class ScopedDictionaryPrefUpdate; | |
| 174 | |
| 175 DictionaryValueUpdate(ScopedDictionaryPrefUpdate* update, | |
| 176 base::DictionaryValue* value, | |
| 177 std::vector<std::string> path); | |
| 178 | |
| 179 void RecordPath(base::StringPiece path); | |
| 180 void RecordSplitPath(const std::vector<base::StringPiece>& path); | |
| 181 void RecordKey(base::StringPiece key); | |
| 182 | |
| 183 std::vector<base::StringPiece> SplitPath(base::StringPiece path); | |
| 184 std::vector<std::string> ConcatPath(const std::vector<std::string>& base_path, | |
| 185 base::StringPiece path); | |
| 186 std::vector<std::string> ConcatPath( | |
| 187 const std::vector<std::string>& base_path, | |
| 188 const std::vector<base::StringPiece>& path); | |
| 189 | |
| 190 ScopedDictionaryPrefUpdate* update_; | |
| 191 base::DictionaryValue* value_; | |
| 192 const std::vector<std::string> path_; | |
| 193 }; | |
| 194 | |
| 195 } // namespace prefs | |
| 196 | |
| 197 #endif // SERVICES_PREFERENCES_PUBLIC_CPP_SCOPED_PREF_UPDATE_H_ | |
| OLD | NEW |