Chromium Code Reviews| Index: components/update_client/persisted_data.cc |
| diff --git a/components/update_client/persisted_data.cc b/components/update_client/persisted_data.cc |
| index 673ba1e6b16254a8bb9005a2141390bd5c4f637a..9c47c3044e74a3df0ce96e81904854ea8961398e 100644 |
| --- a/components/update_client/persisted_data.cc |
| +++ b/components/update_client/persisted_data.cc |
| @@ -28,33 +28,65 @@ PersistedData::~PersistedData() { |
| DCHECK(thread_checker_.CalledOnValidThread()); |
| } |
| -int PersistedData::GetDateLastRollCall(const std::string& id) const { |
| +int PersistedData::GetInt(const std::string& id, |
| + const std::string& key, |
| + int fallback) const { |
| DCHECK(thread_checker_.CalledOnValidThread()); |
| + // We assume ids do not contain '.' characters. |
| + DCHECK_EQ(std::string::npos, id.find('.')); |
| if (!pref_service_) |
| - return kDateLastRollCallUnknown; |
| - int dlrc = kDateLastRollCallUnknown; |
| + return fallback; |
| + int result = fallback; |
|
Sorin Jianu
2016/08/22 19:59:33
we could move the declaration closer to where the
waffles
2016/08/22 23:40:32
Done.
|
| const base::DictionaryValue* dict = |
| pref_service_->GetDictionary(kPersistedDataPreference); |
| - // We assume ids do not contain '.' characters. |
| - DCHECK_EQ(std::string::npos, id.find('.')); |
| - if (!dict->GetInteger(base::StringPrintf("apps.%s.dlrc", id.c_str()), &dlrc)) |
| - return kDateLastRollCallUnknown; |
| - return dlrc; |
| + if (!dict) |
| + return fallback; |
| + if (!dict->GetInteger( |
| + base::StringPrintf("apps.%s.%s", id.c_str(), key.c_str()), &result)) |
| + return fallback; |
| + return result; |
|
Sorin Jianu
2016/08/22 19:59:33
Do you like this rewrite?
int PersistedData::Ge
waffles
2016/08/22 23:40:32
Done.
|
| } |
| -std::string PersistedData::GetPingFreshness(const std::string& id) const { |
| +std::string PersistedData::GetString(const std::string& id, |
| + const std::string& key) const { |
| DCHECK(thread_checker_.CalledOnValidThread()); |
| + // We assume ids do not contain '.' characters. |
| + DCHECK_EQ(std::string::npos, id.find('.')); |
| if (!pref_service_) |
| return std::string(); |
| - std::string freshness; |
| + std::string result; |
|
Sorin Jianu
2016/08/22 19:59:33
same as above.
waffles
2016/08/22 23:40:32
Done.
|
| const base::DictionaryValue* dict = |
| pref_service_->GetDictionary(kPersistedDataPreference); |
| - // We assume ids do not contain '.' characters. |
| - DCHECK_EQ(std::string::npos, id.find('.')); |
| - if (!dict->GetString(base::StringPrintf("apps.%s.pf", id.c_str()), |
| - &freshness)) |
| + if (!dict) |
| + return std::string(); |
| + if (!dict->GetString( |
| + base::StringPrintf("apps.%s.%s", id.c_str(), key.c_str()), &result)) |
| return std::string(); |
| - return base::StringPrintf("{%s}", freshness.c_str()); |
| + return result; |
| +} |
| + |
| +int PersistedData::GetDateLastRollCall(const std::string& id) const { |
| + return GetInt(id, "dlrc", kDateLastRollCallUnknown); |
| +} |
| + |
| +std::string PersistedData::GetPingFreshness(const std::string& id) const { |
| + std::string result = GetString(id, "pf"); |
| + if (!result.empty()) { |
|
Sorin Jianu
2016/08/22 19:59:33
{} not needed or we could use ?;
waffles
2016/08/22 23:40:32
Done.
|
| + return base::StringPrintf("{%s}", result.c_str()); |
| + } |
| + return result; |
| +} |
| + |
| +std::string PersistedData::GetCohort(const std::string& id) const { |
| + return GetString(id, "cohort"); |
| +} |
| + |
| +std::string PersistedData::GetCohortName(const std::string& id) const { |
| + return GetString(id, "cohortname"); |
| +} |
| + |
| +std::string PersistedData::GetCohortHint(const std::string& id) const { |
| + return GetString(id, "cohorthint"); |
| } |
| void PersistedData::SetDateLastRollCall(const std::vector<std::string>& ids, |
| @@ -72,6 +104,32 @@ void PersistedData::SetDateLastRollCall(const std::vector<std::string>& ids, |
| } |
| } |
| +void PersistedData::SetString(const std::string& id, |
| + const std::string& key, |
| + const std::string& value) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + if (!pref_service_) |
| + return; |
| + DictionaryPrefUpdate update(pref_service_, kPersistedDataPreference); |
| + update->SetString(base::StringPrintf("apps.%s.%s", id.c_str(), key.c_str()), |
| + value); |
| +} |
| + |
| +void PersistedData::SetCohort(const std::string& id, |
| + const std::string& cohort) { |
| + SetString(id, "cohort", cohort); |
| +} |
| + |
| +void PersistedData::SetCohortName(const std::string& id, |
| + const std::string& cohort_name) { |
| + SetString(id, "cohortname", cohort_name); |
| +} |
| + |
| +void PersistedData::SetCohortHint(const std::string& id, |
| + const std::string& cohort_hint) { |
| + SetString(id, "cohorthint", cohort_hint); |
| +} |
| + |
| void PersistedData::RegisterPrefs(PrefRegistrySimple* registry) { |
| registry->RegisterDictionaryPref(kPersistedDataPreference); |
| } |