Commit 08fbc499 authored by Archana Simha's avatar Archana Simha Committed by Commit Bot

[Extensions] Add methods for dictionary, time and list prefs.

Add methods for to get and set dictionary, time and list extension
specific ExtensionPrefs.

Design Doc: http://go/extprefrefactor

Bug: 1069560
Change-Id: I4afa211b9eb1a15f87797cb3cea6e20da9fe47ad
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2314992
Commit-Queue: Archana Simha <archanasimha@chromium.org>
Reviewed-by: default avatarDevlin <rdevlin.cronin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#791964}
parent 4851f5ff
......@@ -1389,6 +1389,12 @@ TEST_F(ExtensionPrefsSimpleTest, ExtensionSpecificPrefsMapTest) {
PrefScope::kExtensionSpecific};
constexpr PrefMap kTestStringPref = {"test.string", PrefType::kString,
PrefScope::kExtensionSpecific};
constexpr PrefMap kTestDictPref = {"test.dict", PrefType::kDictionary,
PrefScope::kExtensionSpecific};
constexpr PrefMap kTestListPref = {"test.list", PrefType::kList,
PrefScope::kExtensionSpecific};
constexpr PrefMap kTestTimePref = {"test.time", PrefType::kTime,
PrefScope::kExtensionSpecific};
content::BrowserTaskEnvironment task_environment_;
TestExtensionPrefs prefs(base::ThreadTaskRunnerHandle::Get());
......@@ -1397,6 +1403,15 @@ TEST_F(ExtensionPrefsSimpleTest, ExtensionSpecificPrefsMapTest) {
prefs.prefs()->SetBooleanPref(extension_id, kTestBooleanPref, true);
prefs.prefs()->SetIntegerPref(extension_id, kTestIntegerPref, 1);
prefs.prefs()->SetStringPref(extension_id, kTestStringPref, "foo");
auto dict = std::make_unique<base::DictionaryValue>();
dict->SetString("key", "val");
prefs.prefs()->SetDictionaryPref(extension_id, kTestDictPref,
std::move(dict));
auto list = base::ListValue();
list.AppendString("list_val");
prefs.prefs()->SetListPref(extension_id, kTestListPref, std::move(list));
base::Time time = base::Time::Now();
prefs.prefs()->SetTimePref(extension_id, kTestTimePref, time);
bool bool_value = false;
EXPECT_TRUE(prefs.prefs()->ReadPrefAsBoolean(extension_id, kTestBooleanPref,
......@@ -1410,6 +1425,18 @@ TEST_F(ExtensionPrefsSimpleTest, ExtensionSpecificPrefsMapTest) {
EXPECT_TRUE(prefs.prefs()->ReadPrefAsString(extension_id, kTestStringPref,
&string_value));
EXPECT_EQ(string_value, "foo");
const base::DictionaryValue* dict_val = nullptr;
prefs.prefs()->ReadPrefAsDictionary(extension_id, kTestDictPref, &dict_val);
dict_val->GetString("key", &string_value);
EXPECT_EQ(string_value, "val");
const base::ListValue* list_val = nullptr;
prefs.prefs()->ReadPrefAsList(extension_id, kTestListPref, &list_val);
EXPECT_TRUE(list_val->GetList()[0].is_string());
EXPECT_EQ(list_val->GetList()[0].GetString(), "list_val");
EXPECT_EQ(time, prefs.prefs()->ReadPrefAsTime(extension_id, kTestTimePref));
}
} // namespace extensions
......@@ -19,6 +19,7 @@
#include "base/time/clock.h"
#include "base/time/default_clock.h"
#include "base/trace_event/trace_event.h"
#include "base/util/values/values_util.h"
#include "build/build_config.h"
#include "components/crx_file/id_util.h"
#include "components/pref_registry/pref_registry_syncable.h"
......@@ -250,6 +251,8 @@ bool CheckPrefType(PrefType pref_type, const base::Value* value) {
return value->is_int();
case kDictionary:
return value->is_dict();
case kList:
return value->is_list();
}
}
......@@ -476,22 +479,51 @@ const base::DictionaryValue* ExtensionPrefs::GetExtensionPref(
void ExtensionPrefs::SetIntegerPref(const std::string& id,
const PrefMap& pref,
int value) {
DCHECK_EQ(pref.type, PrefType::kInteger);
UpdateExtensionPref(id, pref, std::make_unique<base::Value>(value));
}
void ExtensionPrefs::SetBooleanPref(const std::string& id,
const PrefMap& pref,
bool value) {
DCHECK_EQ(pref.type, PrefType::kBool);
UpdateExtensionPref(id, pref, std::make_unique<base::Value>(value));
}
void ExtensionPrefs::SetStringPref(const std::string& id,
const PrefMap& pref,
const std::string value) {
DCHECK_EQ(pref.type, PrefType::kString);
UpdateExtensionPref(id, pref,
std::make_unique<base::Value>(std::move(value)));
}
void ExtensionPrefs::SetListPref(const std::string& id,
const PrefMap& pref,
base::Value value) {
DCHECK_EQ(pref.type, PrefType::kList);
DCHECK_EQ(base::Value::Type::LIST, value.type());
UpdateExtensionPref(id, pref,
std::make_unique<base::Value>(std::move(value)));
}
void ExtensionPrefs::SetDictionaryPref(
const std::string& id,
const PrefMap& pref,
std::unique_ptr<base::DictionaryValue> value) {
DCHECK_EQ(pref.type, PrefType::kDictionary);
DCHECK_EQ(base::Value::Type::DICTIONARY, value->type());
UpdateExtensionPref(id, pref, std::move(value));
}
void ExtensionPrefs::SetTimePref(const std::string& id,
const PrefMap& pref,
const base::Time value) {
DCHECK_EQ(pref.type, PrefType::kTime);
UpdateExtensionPref(
id, pref, std::make_unique<base::Value>(::util::TimeToValue(value)));
}
void ExtensionPrefs::UpdateExtensionPref(
const std::string& extension_id,
const PrefMap& pref,
......@@ -529,8 +561,9 @@ void ExtensionPrefs::DeleteExtensionPrefs(const std::string& extension_id) {
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);
DCHECK_EQ(PrefScope::kExtensionSpecific, pref.scope);
DCHECK_EQ(PrefType::kBool, pref.type);
const base::DictionaryValue* ext = GetExtensionPref(extension_id);
if (!ext || !ext->GetBoolean(pref.name, out_value))
return false;
......@@ -541,8 +574,8 @@ bool ExtensionPrefs::ReadPrefAsBoolean(const std::string& extension_id,
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);
DCHECK_EQ(PrefScope::kExtensionSpecific, pref.scope);
DCHECK_EQ(PrefType::kInteger, pref.type);
const base::DictionaryValue* ext = GetExtensionPref(extension_id);
if (!ext || !ext->GetInteger(pref.name, out_value))
return false;
......@@ -553,8 +586,8 @@ bool ExtensionPrefs::ReadPrefAsInteger(const std::string& extension_id,
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);
DCHECK_EQ(PrefScope::kExtensionSpecific, pref.scope);
DCHECK_EQ(PrefType::kString, pref.type);
const base::DictionaryValue* ext = GetExtensionPref(extension_id);
if (!ext || !ext->GetString(pref.name, out_value))
return false;
......@@ -562,6 +595,44 @@ bool ExtensionPrefs::ReadPrefAsString(const std::string& extension_id,
return true;
}
bool ExtensionPrefs::ReadPrefAsList(const std::string& extension_id,
const PrefMap& pref,
const base::ListValue** out_value) const {
DCHECK_EQ(PrefScope::kExtensionSpecific, pref.scope);
DCHECK_EQ(PrefType::kList, pref.type);
DCHECK(out_value);
const base::DictionaryValue* ext = GetExtensionPref(extension_id);
if (!ext || !ext->GetList(pref.name, out_value))
return false;
return true;
}
bool ExtensionPrefs::ReadPrefAsDictionary(
const std::string& extension_id,
const PrefMap& pref,
const base::DictionaryValue** out_value) const {
DCHECK_EQ(PrefScope::kExtensionSpecific, pref.scope);
DCHECK_EQ(PrefType::kDictionary, pref.type);
DCHECK(out_value);
const base::DictionaryValue* ext = GetExtensionPref(extension_id);
if (!ext || !ext->GetDictionary(pref.name, out_value))
return false;
return true;
}
base::Time ExtensionPrefs::ReadPrefAsTime(const std::string& extension_id,
const PrefMap& pref) const {
DCHECK_EQ(PrefScope::kExtensionSpecific, pref.scope);
DCHECK_EQ(PrefType::kTime, pref.type);
const base::DictionaryValue* ext = GetExtensionPref(extension_id);
const base::Value* value;
if (!ext || !ext->Get(pref.name, &value))
return base::Time();
base::Optional<base::Time> time = ::util::ValueToTime(value);
DCHECK(time);
return time.value_or(base::Time());
}
bool ExtensionPrefs::ReadPrefAsBoolean(const std::string& extension_id,
base::StringPiece pref_key,
bool* out_value) const {
......
......@@ -267,6 +267,15 @@ class ExtensionPrefs : public KeyedService {
void SetStringPref(const std::string& id,
const PrefMap& pref,
const std::string value);
void SetListPref(const std::string& id,
const PrefMap& pref,
base::Value value);
void SetDictionaryPref(const std::string& id,
const PrefMap& pref,
std::unique_ptr<base::DictionaryValue> value);
void SetTimePref(const std::string& id,
const PrefMap& pref,
const base::Time value);
void UpdateExtensionPref(const std::string& id,
base::StringPiece key,
......@@ -286,6 +295,17 @@ class ExtensionPrefs : public KeyedService {
const PrefMap& pref,
std::string* out_value) const;
bool ReadPrefAsList(const std::string& extension_id,
const PrefMap& pref,
const base::ListValue** out_value) const;
bool ReadPrefAsDictionary(const std::string& extension_id,
const PrefMap& pref,
const base::DictionaryValue** out_value) const;
base::Time ReadPrefAsTime(const std::string& extension_id,
const PrefMap& pref) const;
bool ReadPrefAsBoolean(const std::string& extension_id,
base::StringPiece pref_key,
bool* out_value) const;
......
......@@ -15,7 +15,7 @@ enum PrefType {
kInteger,
// TODO(archanasimha): implement Get/SetAsX for the following PrefTypes.
kGURL,
// kList,
kList,
kDictionary,
// kExtensionIdList,
// kPermissionSet,
......
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