0

Avoid CHECK against prefixed keys in WrapWithPrefixPrefStore

Currently, calling any getter/setter in WrapWithPrefixPrefStore with a
key prefixed with "the" prefix (the one that WrapWithPrefixPrefStore
works on), leads to a CHECK failure. However, WrapWithPrefixPrefStore
should not disallow having a pref with the prefix as a key, and it
should be the responsibility of the owner to ensure the prefix is not
used as a pref, if needed.

Bug: 336776819
Change-Id: I3c94ffa34696f3c1a5e804a7bc96a3b469907bc3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5563686
Auto-Submit: Ankush Singh <ankushkush@google.com>
Reviewed-by: Dominic Battré <battre@chromium.org>
Commit-Queue: Ankush Singh <ankushkush@google.com>
Cr-Commit-Position: refs/heads/main@{#1312780}
This commit is contained in:
Ankush Singh
2024-06-10 15:16:03 +00:00
committed by Chromium LUCI CQ
parent 35520b576c
commit cad9372bba
3 changed files with 19 additions and 1 deletions

@ -155,7 +155,6 @@ void WrapWithPrefixPrefStore::OnInitializationCompleted(bool succeeded) {
std::string WrapWithPrefixPrefStore::AddDottedPrefix(
std::string_view path) const {
CHECK(!HasDottedPrefix(path));
return base::StrCat({dotted_prefix_, path});
}

@ -31,6 +31,8 @@
//
// This can be used to merge separate pref stores into one single storage under
// separate dictionary items.
//
// NOTE: Users are responsible for ensuring the prefix is not an existing pref.
class COMPONENTS_PREFS_EXPORT WrapWithPrefixPrefStore
: public PersistentPrefStore,
public PrefStore::Observer {

@ -394,6 +394,23 @@ TEST_F(WrapWithPrefixPrefStoreTest, HasReadErrorDelegateWithNullDelegate) {
EXPECT_TRUE(store().HasReadErrorDelegate());
}
TEST_F(WrapWithPrefixPrefStoreTest, GetValueForPrefixedKeyIfNonExisting) {
target_store().SetString(kPrefixedTestPref, "value");
EXPECT_FALSE(store().GetValue(kPrefixedTestPref, nullptr));
}
TEST_F(WrapWithPrefixPrefStoreTest, GetValueForExistingIfExisting) {
target_store().SetString("prefixed.prefixed.test.pref", "value");
EXPECT_TRUE(ValueInStoreIs(store(), kPrefixedTestPref, "value"));
}
TEST_F(WrapWithPrefixPrefStoreTest, SetValueForPrefixedKey) {
EXPECT_CALL(observer_, OnPrefValueChanged(kPrefixedTestPref));
store().SetValue(kPrefixedTestPref, base::Value("value"), /*flags=*/0);
EXPECT_TRUE(ValueInStoreIs(store(), kPrefixedTestPref, "value"));
}
using WrapWithPrefixPrefStoreDeathTest = WrapWithPrefixPrefStoreTest;
TEST_F(WrapWithPrefixPrefStoreDeathTest,