Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(265)

Unified Diff: services/preferences/public/cpp/scoped_pref_update.h

Issue 2812863002: Pref service: Add a ScopedDictionaryPrefUpdate to track value changes. (Closed)
Patch Set: rebase Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: services/preferences/public/cpp/scoped_pref_update.h
diff --git a/services/preferences/public/cpp/scoped_pref_update.h b/services/preferences/public/cpp/scoped_pref_update.h
new file mode 100644
index 0000000000000000000000000000000000000000..9c2dd0c6d949e78e093cc86628014a2336ded45a
--- /dev/null
+++ b/services/preferences/public/cpp/scoped_pref_update.h
@@ -0,0 +1,197 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef SERVICES_PREFERENCES_PUBLIC_CPP_SCOPED_PREF_UPDATE_H_
+#define SERVICES_PREFERENCES_PUBLIC_CPP_SCOPED_PREF_UPDATE_H_
+
+#include <memory>
+#include <set>
+#include <string>
+#include <vector>
+
+#include "base/strings/string16.h"
+#include "base/strings/string_piece.h"
+#include "base/values.h"
+
+class PrefService;
+
+namespace prefs {
+class DictionaryValueUpdate;
+
+// An update to a dictionary value pref.
+class ScopedDictionaryPrefUpdate {
+ public:
+ ScopedDictionaryPrefUpdate(PrefService* service, base::StringPiece path);
+
+ // Notifies if necessary.
+ virtual ~ScopedDictionaryPrefUpdate();
+
+ virtual DictionaryValueUpdate* Get();
+
+ DictionaryValueUpdate* operator->();
+
+ private:
+ friend class DictionaryValueUpdate;
+ // If |value_| is not null, triggers a notification of PrefObservers and
+ // resets |value_|.
+ void Notify();
+
+ void RecordPath(const std::vector<std::string>& path);
+ DictionaryValueUpdate* Insert(
+ std::unique_ptr<DictionaryValueUpdate> dictionary_value_update);
+
+ // Weak pointer.
+ PrefService* service_;
+ // Path of the preference being updated.
+ std::string path_;
+ // Cache of value from user pref store (set between Get() and Notify() calls).
+ DictionaryValueUpdate* value_;
+
+ std::set<std::vector<std::string>> updated_paths_;
+
+ std::vector<std::unique_ptr<DictionaryValueUpdate>> updates_;
+
+ DISALLOW_COPY_AND_ASSIGN(ScopedDictionaryPrefUpdate);
+};
+
+// A wrapper around base::DictionaryValue that reports changes to its contents
+// to a ScopedDictionaryPrefUpdate.
+class DictionaryValueUpdate {
+ public:
+ ~DictionaryValueUpdate();
+ bool HasKey(base::StringPiece key) const;
+
+ // Returns the number of Values in this dictionary.
+ size_t size() const;
+
+ // Returns whether the dictionary is empty.
+ bool empty() const;
+
+ // Clears any current contents of this dictionary.
+ void Clear();
+
+ // Sets the Value associated with the given path starting from this object.
+ // A path has the form "<key>" or "<key>.<key>.[...]", where "." indexes
+ // into the next DictionaryValue down. Obviously, "." can't be used
+ // within a key, but there are no other restrictions on keys.
+ // If the key at any step of the way doesn't exist, or exists but isn't
+ // a DictionaryValue, a new DictionaryValue will be created and attached
+ // to the path in that location. |in_value| must be non-null.
+ void Set(base::StringPiece path, std::unique_ptr<base::Value> in_value);
+
+ // Convenience forms of Set(). These methods will replace any existing
+ // value at that path, even if it has a different type.
+ void SetBoolean(base::StringPiece path, bool in_value);
+ void SetInteger(base::StringPiece path, int in_value);
+ void SetDouble(base::StringPiece path, double in_value);
+ void SetString(base::StringPiece path, base::StringPiece in_value);
+ void SetString(base::StringPiece path, const base::string16& in_value);
+ DictionaryValueUpdate* SetDictionary(
+ base::StringPiece path,
+ std::unique_ptr<base::DictionaryValue> in_value);
+
+ // Like Set(), but without special treatment of '.'. This allows e.g. URLs to
+ // be used as paths.
+ void SetWithoutPathExpansion(base::StringPiece key,
+ std::unique_ptr<base::Value> in_value);
+
+ // Convenience forms of SetWithoutPathExpansion().
+ void SetBooleanWithoutPathExpansion(base::StringPiece path, bool in_value);
+ void SetIntegerWithoutPathExpansion(base::StringPiece path, int in_value);
+ void SetDoubleWithoutPathExpansion(base::StringPiece path, double in_value);
+ void SetStringWithoutPathExpansion(base::StringPiece path,
+ base::StringPiece in_value);
+ void SetStringWithoutPathExpansion(base::StringPiece path,
+ const base::string16& in_value);
+ DictionaryValueUpdate* SetDictionaryWithoutPathExpansion(
+ base::StringPiece path,
+ std::unique_ptr<base::DictionaryValue> in_value);
+
+ // These are convenience forms of Get(). The value will be retrieved
+ // and the return value will be true if the path is valid and the value at
+ // the end of the path can be returned in the form specified.
+ // |out_value| is optional and will only be set if non-NULL.
+ bool GetBoolean(base::StringPiece path, bool* out_value) const;
+ bool GetInteger(base::StringPiece path, int* out_value) const;
+ // Values of both type Type::INTEGER and Type::DOUBLE can be obtained as
+ // doubles.
+ bool GetDouble(base::StringPiece path, double* out_value) const;
+ bool GetString(base::StringPiece path, std::string* out_value) const;
+ bool GetString(base::StringPiece path, base::string16* out_value) const;
+ bool GetBinary(base::StringPiece path, const base::Value** out_value) const;
+ bool GetDictionary(base::StringPiece path,
+ const base::DictionaryValue** out_value) const;
+ bool GetDictionary(base::StringPiece path, DictionaryValueUpdate** out_value);
+ bool GetList(base::StringPiece path, const base::ListValue** out_value) const;
+ bool GetList(base::StringPiece path, base::ListValue** out_value);
+
+ // Like Get(), but without special treatment of '.'. This allows e.g. URLs to
+ // be used as paths.
+ bool GetBooleanWithoutPathExpansion(base::StringPiece key,
+ bool* out_value) const;
+ bool GetIntegerWithoutPathExpansion(base::StringPiece key,
+ int* out_value) const;
+ bool GetDoubleWithoutPathExpansion(base::StringPiece key,
+ double* out_value) const;
+ bool GetStringWithoutPathExpansion(base::StringPiece key,
+ std::string* out_value) const;
+ bool GetStringWithoutPathExpansion(base::StringPiece key,
+ base::string16* out_value) const;
+ bool GetDictionaryWithoutPathExpansion(
+ base::StringPiece key,
+ const base::DictionaryValue** out_value) const;
+ bool GetDictionaryWithoutPathExpansion(base::StringPiece key,
+ DictionaryValueUpdate** out_value);
+ bool GetListWithoutPathExpansion(base::StringPiece key,
+ const base::ListValue** out_value) const;
+ bool GetListWithoutPathExpansion(base::StringPiece key,
+ base::ListValue** out_value);
+
+ // Removes the Value with the specified path from this dictionary (or one
+ // of its child dictionaries, if the path is more than just a local key).
+ // If |out_value| is non-NULL, the removed Value will be passed out via
+ // |out_value|. If |out_value| is NULL, the removed value will be deleted.
+ // This method returns true if |path| is a valid path; otherwise it will
+ // return false and the DictionaryValue object will be unchanged.
+ bool Remove(base::StringPiece path, std::unique_ptr<base::Value>* out_value);
+
+ // Like Remove(), but without special treatment of '.'. This allows e.g. URLs
+ // to be used as paths.
+ bool RemoveWithoutPathExpansion(base::StringPiece key,
+ std::unique_ptr<base::Value>* out_value);
+
+ // Removes a path, clearing out all dictionaries on |path| that remain empty
+ // after removing the value at |path|.
+ bool RemovePath(base::StringPiece path,
+ std::unique_ptr<base::Value>* out_value);
+
+ base::DictionaryValue* AsDictionary();
+ const base::DictionaryValue* AsConstDictionary() const;
+
+ private:
+ friend class ScopedDictionaryPrefUpdate;
+
+ DictionaryValueUpdate(ScopedDictionaryPrefUpdate* update,
+ base::DictionaryValue* value,
+ std::vector<std::string> path);
+
+ void RecordPath(base::StringPiece path);
+ void RecordSplitPath(const std::vector<base::StringPiece>& path);
+ void RecordKey(base::StringPiece key);
+
+ std::vector<base::StringPiece> SplitPath(base::StringPiece path);
+ std::vector<std::string> ConcatPath(const std::vector<std::string>& base_path,
+ base::StringPiece path);
+ std::vector<std::string> ConcatPath(
+ const std::vector<std::string>& base_path,
+ const std::vector<base::StringPiece>& path);
+
+ ScopedDictionaryPrefUpdate* update_;
+ base::DictionaryValue* value_;
+ const std::vector<std::string> path_;
+};
+
+} // namespace prefs
+
+#endif // SERVICES_PREFERENCES_PUBLIC_CPP_SCOPED_PREF_UPDATE_H_
« no previous file with comments | « services/preferences/public/cpp/pref_store_impl.cc ('k') | services/preferences/public/cpp/scoped_pref_update.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698