Commit 3ec74cb3 authored by Archana Simha's avatar Archana Simha Committed by Commit Bot

[Extensions] Refactor extension specific ExtensionPref with basic types.

This CL implements the getter and setter methods for extension specific
ExtensionPrefs with string, integer and boolean values.

Design Doc: http://go/extprefrefactor

Bug: 1069560
Change-Id: I238cde708b678deb18ac0b5d97641c0be8bfe14c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2300326
Commit-Queue: Archana Simha <archanasimha@chromium.org>
Reviewed-by: default avatarDevlin <rdevlin.cronin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#790573}
parent eb21540e
...@@ -1359,4 +1359,34 @@ TEST_F(ExtensionPrefsSimpleTest, ProfileExtensionPrefsMapTest) { ...@@ -1359,4 +1359,34 @@ TEST_F(ExtensionPrefsSimpleTest, ProfileExtensionPrefsMapTest) {
EXPECT_EQ(prefs.prefs()->GetPrefAsString(kTestStringPref), "foo"); EXPECT_EQ(prefs.prefs()->GetPrefAsString(kTestStringPref), "foo");
} }
TEST_F(ExtensionPrefsSimpleTest, ExtensionSpecificPrefsMapTest) {
constexpr PrefMap kTestBooleanPref = {"test.boolean", PrefType::kBool,
PrefScope::kExtensionSpecific};
constexpr PrefMap kTestIntegerPref = {"test.integer", PrefType::kInteger,
PrefScope::kExtensionSpecific};
constexpr PrefMap kTestStringPref = {"test.string", PrefType::kString,
PrefScope::kExtensionSpecific};
content::BrowserTaskEnvironment task_environment_;
TestExtensionPrefs prefs(base::ThreadTaskRunnerHandle::Get());
std::string extension_id = prefs.AddExtensionAndReturnId("1");
prefs.prefs()->SetBooleanPref(extension_id, kTestBooleanPref, true);
prefs.prefs()->SetIntegerPref(extension_id, kTestIntegerPref, 1);
prefs.prefs()->SetStringPref(extension_id, kTestStringPref, "foo");
bool bool_value = false;
EXPECT_TRUE(prefs.prefs()->ReadPrefAsBoolean(extension_id, kTestBooleanPref,
&bool_value));
EXPECT_TRUE(bool_value);
int int_value = 0;
EXPECT_TRUE(prefs.prefs()->ReadPrefAsInteger(extension_id, kTestIntegerPref,
&int_value));
EXPECT_EQ(int_value, 1);
std::string string_value;
EXPECT_TRUE(prefs.prefs()->ReadPrefAsString(extension_id, kTestStringPref,
&string_value));
EXPECT_EQ(string_value, "foo");
}
} // namespace extensions } // namespace extensions
...@@ -469,6 +469,36 @@ const base::DictionaryValue* ExtensionPrefs::GetExtensionPref( ...@@ -469,6 +469,36 @@ const base::DictionaryValue* ExtensionPrefs::GetExtensionPref(
return extension_dict; return extension_dict;
} }
void ExtensionPrefs::SetIntegerPref(const std::string& id,
const PrefMap& pref,
int value) {
UpdateExtensionPref(id, pref, std::make_unique<base::Value>(value));
}
void ExtensionPrefs::SetBooleanPref(const std::string& id,
const PrefMap& pref,
bool value) {
UpdateExtensionPref(id, pref, std::make_unique<base::Value>(value));
}
void ExtensionPrefs::SetStringPref(const std::string& id,
const PrefMap& pref,
const std::string value) {
UpdateExtensionPref(id, pref,
std::make_unique<base::Value>(std::move(value)));
}
void ExtensionPrefs::UpdateExtensionPref(
const std::string& extension_id,
const PrefMap& pref,
std::unique_ptr<base::Value> data_value) {
DCHECK_EQ(PrefScope::kExtensionSpecific, pref.scope);
DCHECK(CheckPrefType(pref.type, data_value.get()));
DCHECK(crx_file::id_util::IdIsValid(extension_id));
ScopedExtensionPrefUpdate update(prefs_, extension_id);
update->Set(pref.name, std::move(data_value));
}
void ExtensionPrefs::UpdateExtensionPref( void ExtensionPrefs::UpdateExtensionPref(
const std::string& extension_id, const std::string& extension_id,
base::StringPiece key, base::StringPiece key,
...@@ -492,6 +522,42 @@ void ExtensionPrefs::DeleteExtensionPrefs(const std::string& extension_id) { ...@@ -492,6 +522,42 @@ void ExtensionPrefs::DeleteExtensionPrefs(const std::string& extension_id) {
update->Remove(extension_id, NULL); update->Remove(extension_id, NULL);
} }
bool ExtensionPrefs::ReadPrefAsBoolean(const std::string& extension_id,
const PrefMap& pref,
bool* out_value) const {
DCHECK_EQ(pref.scope, PrefScope::kExtensionSpecific);
DCHECK_EQ(pref.type, PrefType::kBool);
const base::DictionaryValue* ext = GetExtensionPref(extension_id);
if (!ext || !ext->GetBoolean(pref.name, out_value))
return false;
return true;
}
bool ExtensionPrefs::ReadPrefAsInteger(const std::string& extension_id,
const PrefMap& pref,
int* out_value) const {
DCHECK_EQ(pref.scope, PrefScope::kExtensionSpecific);
DCHECK_EQ(pref.type, PrefType::kInteger);
const base::DictionaryValue* ext = GetExtensionPref(extension_id);
if (!ext || !ext->GetInteger(pref.name, out_value))
return false;
return true;
}
bool ExtensionPrefs::ReadPrefAsString(const std::string& extension_id,
const PrefMap& pref,
std::string* out_value) const {
DCHECK_EQ(pref.scope, PrefScope::kExtensionSpecific);
DCHECK_EQ(pref.type, PrefType::kString);
const base::DictionaryValue* ext = GetExtensionPref(extension_id);
if (!ext || !ext->GetString(pref.name, out_value))
return false;
return true;
}
bool ExtensionPrefs::ReadPrefAsBoolean(const std::string& extension_id, bool ExtensionPrefs::ReadPrefAsBoolean(const std::string& extension_id,
base::StringPiece pref_key, base::StringPiece pref_key,
bool* out_value) const { bool* out_value) const {
......
...@@ -255,12 +255,30 @@ class ExtensionPrefs : public KeyedService { ...@@ -255,12 +255,30 @@ class ExtensionPrefs : public KeyedService {
// Populates |out| with the ids of all installed extensions. // Populates |out| with the ids of all installed extensions.
void GetExtensions(ExtensionIdList* out) const; void GetExtensions(ExtensionIdList* out) const;
void SetIntegerPref(const std::string& id, const PrefMap& pref, int value);
void SetBooleanPref(const std::string& id, const PrefMap& pref, bool value);
void SetStringPref(const std::string& id,
const PrefMap& pref,
const std::string value);
void UpdateExtensionPref(const std::string& id, void UpdateExtensionPref(const std::string& id,
base::StringPiece key, base::StringPiece key,
std::unique_ptr<base::Value> value); std::unique_ptr<base::Value> value);
void DeleteExtensionPrefs(const std::string& id); void DeleteExtensionPrefs(const std::string& id);
bool ReadPrefAsBoolean(const std::string& extension_id,
const PrefMap& pref,
bool* out_value) const;
bool ReadPrefAsInteger(const std::string& extension_id,
const PrefMap& pref,
int* out_value) const;
bool ReadPrefAsString(const std::string& extension_id,
const PrefMap& pref,
std::string* out_value) const;
bool ReadPrefAsBoolean(const std::string& extension_id, bool ReadPrefAsBoolean(const std::string& extension_id,
base::StringPiece pref_key, base::StringPiece pref_key,
bool* out_value) const; bool* out_value) const;
...@@ -699,6 +717,11 @@ class ExtensionPrefs : public KeyedService { ...@@ -699,6 +717,11 @@ class ExtensionPrefs : public KeyedService {
const base::Value* GetPref(const PrefMap& pref) const; const base::Value* GetPref(const PrefMap& pref) const;
void SetPref(const PrefMap& pref, std::unique_ptr<base::Value> value); void SetPref(const PrefMap& pref, std::unique_ptr<base::Value> value);
// Updates ExtensionPrefs for a specific extension.
void UpdateExtensionPref(const std::string& id,
const PrefMap& pref,
std::unique_ptr<base::Value> value);
// Converts absolute paths in the pref to paths relative to the // Converts absolute paths in the pref to paths relative to the
// install_directory_. // install_directory_.
void MakePathsRelative(); void MakePathsRelative();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment