Commit c9616cee authored by Joshua Pawlicki's avatar Joshua Pawlicki Committed by Commit Bot

Updater: allow prefs to handle appids with ".".

There needs to be some thought devoted to how we are sharing prefs with
update_client (and how and whether we should share code as well).

This change is very limited in scope, specifically to fix issue 1092503
and unblock other changes, allowing the test app to function correctly.

Bug: 1092503
Change-Id: I0500d3ae210a2a60e2991826432417a49ce88166
Fixed: 1092503
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2348537
Commit-Queue: Joshua Pawlicki <waffles@chromium.org>
Commit-Queue: Sorin Jianu <sorin@chromium.org>
Auto-Submit: Joshua Pawlicki <waffles@chromium.org>
Reviewed-by: default avatarSorin Jianu <sorin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#796881}
parent b40dbcad
...@@ -125,31 +125,52 @@ std::vector<std::string> PersistedData::GetAppIds() const { ...@@ -125,31 +125,52 @@ std::vector<std::string> PersistedData::GetAppIds() const {
return app_ids; return app_ids;
} }
std::string PersistedData::GetString(const std::string& id, const base::Value* PersistedData::GetAppKey(const std::string& id) const {
const std::string& key) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(!base::Contains(id, '.')); // Assume the id does not contain '.'. if (!pref_service_)
return nullptr;
const base::DictionaryValue* dict = const base::DictionaryValue* dict =
pref_service_->GetDictionary(kPersistedDataPreference); pref_service_->GetDictionary(kPersistedDataPreference);
if (!dict) if (!dict)
return nullptr;
const base::Value* apps = dict->FindDictKey("apps");
if (!apps)
return nullptr;
return apps->FindDictKey(id);
}
std::string PersistedData::GetString(const std::string& id,
const std::string& key) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
const base::Value* app_key = GetAppKey(id);
if (!app_key)
return {};
const std::string* value = app_key->FindStringKey(key);
if (!value)
return {}; return {};
std::string result; return *value;
return dict->GetString( }
base::StringPrintf("apps.%s.%s", id.c_str(), key.c_str()), &result)
? result base::Value* PersistedData::GetOrCreateAppKey(const std::string& id,
: std::string(); base::Value* root) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
base::Value* apps = root->FindDictKey("apps");
if (!apps)
apps = root->SetKey("apps", base::Value(base::Value::Type::DICTIONARY));
base::Value* app = apps->FindDictKey(id);
if (!app)
app = apps->SetKey(id, base::Value(base::Value::Type::DICTIONARY));
return app;
} }
void PersistedData::SetString(const std::string& id, void PersistedData::SetString(const std::string& id,
const std::string& key, const std::string& key,
const std::string& value) { const std::string& value) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(!base::Contains(id, '.')); // Assume the id does not contain '.'. if (!pref_service_)
return;
DictionaryPrefUpdate update(pref_service_, kPersistedDataPreference); DictionaryPrefUpdate update(pref_service_, kPersistedDataPreference);
update->SetString(base::StringPrintf("apps.%s.%s", id.c_str(), key.c_str()), GetOrCreateAppKey(id, update.Get())->SetStringKey(key, value);
value);
} }
} // namespace updater } // namespace updater
...@@ -15,6 +15,7 @@ class PrefService; ...@@ -15,6 +15,7 @@ class PrefService;
namespace base { namespace base {
class FilePath; class FilePath;
class Value;
class Version; class Version;
} // namespace base } // namespace base
...@@ -72,6 +73,12 @@ class PersistedData : public base::RefCountedThreadSafe<PersistedData> { ...@@ -72,6 +73,12 @@ class PersistedData : public base::RefCountedThreadSafe<PersistedData> {
friend class base::RefCountedThreadSafe<PersistedData>; friend class base::RefCountedThreadSafe<PersistedData>;
~PersistedData(); ~PersistedData();
// Returns nullptr if the app key does not exist.
const base::Value* GetAppKey(const std::string& id) const;
// Returns an existing or newly created app key under a root pref.
base::Value* GetOrCreateAppKey(const std::string& id, base::Value* root);
std::string GetString(const std::string& id, const std::string& key) const; std::string GetString(const std::string& id, const std::string& key) const;
void SetString(const std::string& id, void SetString(const std::string& id,
const std::string& key, const std::string& key,
......
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