0

Clear standalone_browser_prefs once Lacros is disabled.

These prefs are set by Lacros via crosapi. If Lacros is disabled, the
prefs are not updatable from Ash thus they should be cleared upon
Lacros disablement.

Bug: 297826137
Change-Id: I7706848d74374a9b583bdd9c5db30a3cc9e3b355
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5562385
Commit-Queue: Yuta Hijikata <ythjkt@chromium.org>
Reviewed-by: Dominic Battré <battre@chromium.org>
Reviewed-by: Hidehiko Abe <hidehiko@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1313237}
This commit is contained in:
Yuta Hijikata
2024-06-11 06:14:02 +00:00
committed by Chromium LUCI CQ
parent f72df8b0c1
commit 12e1d9526f
4 changed files with 117 additions and 3 deletions

@ -108,6 +108,7 @@
#include "components/user_manager/user.h"
#include "components/user_manager/user_manager.h"
#include "components/user_manager/user_type.h"
#include "components/user_prefs/user_prefs.h"
#include "components/version_info/version_info.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "ui/base/l10n/l10n_util.h"
@ -899,7 +900,21 @@ void BrowserManager::ClearLacrosData() {
}));
}
// TODO(b/297826137): Remove 'standalone_browser_preferences.json'.
// Clear prefs set by Lacros and stored in
// 'standalone_browser_preferences.json' if Lacros is disabled.
const user_manager::User* user =
user_manager::UserManager::Get()->GetPrimaryUser();
if (!user) {
CHECK_IS_TEST();
return;
}
content::BrowserContext* context =
ash::BrowserContextHelper::Get()->GetBrowserContextByUser(user);
if (!context) {
CHECK_IS_TEST();
return;
}
user_prefs::UserPrefs::Get(context)->RemoveAllStandaloneBrowserPrefs();
}
void BrowserManager::OnBrowserServiceConnected(

@ -21,6 +21,7 @@
#include "base/memory/ptr_util.h"
#include "base/metrics/histogram.h"
#include "base/notreached.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/task/sequenced_task_runner.h"
@ -39,10 +40,33 @@
#include "components/prefs/android/pref_service_android.h"
#endif
namespace {
#if BUILDFLAG(IS_CHROMEOS_ASH)
namespace pref_service_util {
void GetAllDottedPaths(std::string_view prefix,
const base::Value::Dict& dict,
std::vector<std::string>& paths) {
for (const auto pair : dict) {
std::string path;
if (prefix.empty()) {
path = pair.first;
} else {
path = base::StrCat({prefix, ".", pair.first});
}
if (pair.second.is_dict()) {
GetAllDottedPaths(path, pair.second.GetDict(), paths);
} else {
paths.push_back(path);
}
}
}
} // namespace
void GetAllDottedPaths(const base::Value::Dict& dict,
std::vector<std::string>& paths) {
GetAllDottedPaths("", dict, paths);
}
} // namespace pref_service_util
#endif // BUILDFLAG(IS_CHROMEOS_ASH)
PrefService::PersistentPrefStoreLoadingObserver::
PersistentPrefStoreLoadingObserver(PrefService* pref_service)
@ -705,6 +729,22 @@ void PrefService::RemoveStandaloneBrowserPref(const std::string& path) {
standalone_browser_pref_store_->RemoveValue(
path, WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
}
void PrefService::RemoveAllStandaloneBrowserPrefs() {
if (!standalone_browser_pref_store_) {
LOG(WARNING) << "standalone_browser_pref_store_ is null";
return;
}
std::vector<std::string> paths;
pref_service_util::GetAllDottedPaths(
standalone_browser_pref_store_->GetValues(), paths);
for (const std::string& path : paths) {
standalone_browser_pref_store_->RemoveValue(
path, WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
}
}
#endif
// static

@ -59,6 +59,15 @@ class PrefMemberBase;
class ScopedUserPrefUpdateBase;
}
#if BUILDFLAG(IS_CHROMEOS_ASH)
namespace pref_service_util {
// Gets all the dotted paths from `dict`. For example if values stored are
// `{"a" : { "b" : true, "c": false }}`, then `paths` gets ["a.b", "a.c"].
void COMPONENTS_PREFS_EXPORT GetAllDottedPaths(const base::Value::Dict& dict,
std::vector<std::string>& paths);
} // namespace pref_service_util
#endif
// Base class for PrefServices. You can use the base class to read and
// interact with preferences, but not to register new preferences; for
// that see e.g. PrefRegistrySimple.
@ -403,6 +412,10 @@ class COMPONENTS_PREFS_EXPORT PrefService {
const base::Value& value);
// Clear extension-controlled prefs from Lacros in ash.
void RemoveStandaloneBrowserPref(const std::string& path);
// Clear all prefs in standalone_browser_pref_store_. Use it when rolling back
// to Ash (i.e. disabling Lacros).
void RemoveAllStandaloneBrowserPrefs();
#endif
#if BUILDFLAG(IS_ANDROID)

@ -32,6 +32,34 @@ const char kStandaloneBrowserPref[] = "standalone_browser_pref";
} // namespace
#if BUILDFLAG(IS_CHROMEOS_ASH)
TEST(PrefServiceUtilTest, GetAllDottedPaths) {
using pref_service_util::GetAllDottedPaths;
base::Value::Dict dict;
std::vector<std::string> paths;
GetAllDottedPaths(dict, paths);
// Expect paths to be [].
EXPECT_EQ(paths.size(), std::size_t(0));
dict.SetByDottedPath("one.two", base::Value(12));
GetAllDottedPaths(dict, paths);
EXPECT_THAT(paths, testing::UnorderedElementsAre("one.two"));
paths.clear();
dict.SetByDottedPath("one.three", base::Value(13));
GetAllDottedPaths(dict, paths);
EXPECT_THAT(paths, testing::UnorderedElementsAre("one.two", "one.three"));
paths.clear();
dict.SetByDottedPath("key", "value");
GetAllDottedPaths(dict, paths);
EXPECT_THAT(paths,
testing::UnorderedElementsAre("one.two", "one.three", "key"));
}
#endif
TEST(PrefServiceTest, NoObserverFire) {
TestingPrefServiceSimple prefs;
@ -537,3 +565,21 @@ TEST_F(PrefStandaloneBrowserPrefsTest, CheckStandaloneBrowserPref) {
kStandaloneBrowserPref, WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
EXPECT_EQ(base::Value(4), *(preference->GetValue()));
}
#if BUILDFLAG(IS_CHROMEOS_ASH)
TEST_F(PrefStandaloneBrowserPrefsTest, RemoveAllStandaloneBrowserPrefs) {
const char int_pref_name[] = "int.name";
const char str_pref_name[] = "str.pref.name";
pref_registry_->RegisterIntegerPref(int_pref_name, 0);
pref_registry_->RegisterStringPref(str_pref_name, "");
pref_service_->SetStandaloneBrowserPref(int_pref_name, base::Value(4));
pref_service_->SetStandaloneBrowserPref(str_pref_name, base::Value("value"));
EXPECT_EQ(base::Value(4), pref_service_->GetValue(int_pref_name));
EXPECT_EQ(base::Value("value"), pref_service_->GetValue(str_pref_name));
pref_service_->RemoveAllStandaloneBrowserPrefs();
EXPECT_EQ(base::Value(0), pref_service_->GetValue(int_pref_name));
EXPECT_EQ(base::Value(""), pref_service_->GetValue(str_pref_name));
}
#endif