0

Use std::string_view in ScopedPrefUpdate.

As a drive-by, perform a few small clean-ups.

Bug: 349741884
Change-Id: I9831a134fcebfbab1f65a8b4711673e2c83c5d94
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5743293
Reviewed-by: Dominic Battré <battre@chromium.org>
Commit-Queue: Jan Keitel <jkeitel@google.com>
Cr-Commit-Position: refs/heads/main@{#1333438}
This commit is contained in:
Jan Keitel
2024-07-26 09:09:08 +00:00
committed by Chromium LUCI CQ
parent b8edbc0533
commit 45e4054ce7
2 changed files with 19 additions and 14 deletions

@ -4,6 +4,9 @@
#include "components/prefs/scoped_user_pref_update.h"
#include <string_view>
#include "base/check_deref.h"
#include "base/check_op.h"
#include "components/prefs/pref_notifier.h"
#include "components/prefs/pref_service.h"
@ -17,8 +20,8 @@
namespace subtle {
ScopedUserPrefUpdateBase::ScopedUserPrefUpdateBase(PrefService* service,
const std::string& path)
: service_(service), path_(path), value_(nullptr) {
std::string_view path)
: service_(CHECK_DEREF(service)), path_(path) {
DCHECK_CALLED_ON_VALID_SEQUENCE(service_->sequence_checker_);
}

@ -9,8 +9,10 @@
#define COMPONENTS_PREFS_SCOPED_USER_PREF_UPDATE_H_
#include <string>
#include <string_view>
#include "base/memory/raw_ptr.h"
#include "base/memory/raw_ref.h"
#include "base/sequence_checker.h"
#include "base/values.h"
#include "components/prefs/pref_service.h"
@ -32,25 +34,25 @@ class COMPONENTS_PREFS_EXPORT ScopedUserPrefUpdateBase {
ScopedUserPrefUpdateBase& operator=(const ScopedUserPrefUpdateBase&) = delete;
protected:
ScopedUserPrefUpdateBase(PrefService* service, const std::string& path);
ScopedUserPrefUpdateBase(PrefService* service, std::string_view path);
// Calls Notify().
~ScopedUserPrefUpdateBase();
virtual ~ScopedUserPrefUpdateBase();
// Sets |value_| to |service_|->GetMutableUserPref and returns it.
// Sets `value_` to `service_`->GetMutableUserPref and returns it.
base::Value* GetValueOfType(base::Value::Type type);
private:
// If |value_| is not null, triggers a notification of PrefObservers and
// resets |value_|.
// If `value_` is not null, triggers a notification of PrefObservers and
// resets `value_`.
void Notify();
// Weak pointer.
raw_ptr<PrefService> service_;
const raw_ref<PrefService> service_;
// Path of the preference being updated.
std::string path_;
const std::string path_;
// Cache of value from user pref store (set between Get() and Notify() calls).
raw_ptr<base::Value> value_;
raw_ptr<base::Value> value_ = nullptr;
SEQUENCE_CHECKER(sequence_checker_);
};
@ -67,14 +69,14 @@ class COMPONENTS_PREFS_EXPORT ScopedDictPrefUpdate
public:
// The underlying dictionary must not be removed from `service` during
// the lifetime of the created ScopedDictPrefUpdate.
ScopedDictPrefUpdate(PrefService* service, const std::string& path)
ScopedDictPrefUpdate(PrefService* service, std::string_view path)
: ScopedUserPrefUpdateBase(service, path) {}
ScopedDictPrefUpdate(const ScopedDictPrefUpdate&) = delete;
ScopedDictPrefUpdate& operator=(const ScopedDictPrefUpdate&) = delete;
// Triggers an update notification if Get() was called.
virtual ~ScopedDictPrefUpdate() = default;
~ScopedDictPrefUpdate() override = default;
// Returns a mutable `base::Value::Dict` instance that
// - is already in the user pref store, or
@ -102,14 +104,14 @@ class COMPONENTS_PREFS_EXPORT ScopedListPrefUpdate
public:
// The underlying list must not be removed from `service` during
// the lifetime of the created ScopedListPrefUpdate.
ScopedListPrefUpdate(PrefService* service, const std::string& path)
ScopedListPrefUpdate(PrefService* service, std::string_view path)
: ScopedUserPrefUpdateBase(service, path) {}
ScopedListPrefUpdate(const ScopedListPrefUpdate&) = delete;
ScopedListPrefUpdate& operator=(const ScopedListPrefUpdate&) = delete;
// Triggers an update notification if Get() was called.
virtual ~ScopedListPrefUpdate() = default;
~ScopedListPrefUpdate() override = default;
// Returns a mutable `base::Value::List` instance that
// - is already in the user pref store, or