Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/update_client/persisted_data.h" | 5 #include "components/update_client/persisted_data.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/guid.h" | 10 #include "base/guid.h" |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 21 | 21 |
| 22 namespace update_client { | 22 namespace update_client { |
| 23 | 23 |
| 24 PersistedData::PersistedData(PrefService* pref_service) | 24 PersistedData::PersistedData(PrefService* pref_service) |
| 25 : pref_service_(pref_service) {} | 25 : pref_service_(pref_service) {} |
| 26 | 26 |
| 27 PersistedData::~PersistedData() { | 27 PersistedData::~PersistedData() { |
| 28 DCHECK(thread_checker_.CalledOnValidThread()); | 28 DCHECK(thread_checker_.CalledOnValidThread()); |
| 29 } | 29 } |
| 30 | 30 |
| 31 int PersistedData::GetDateLastRollCall(const std::string& id) const { | 31 int PersistedData::GetInt(const std::string& id, |
| 32 const std::string& key, | |
| 33 int fallback) const { | |
| 32 DCHECK(thread_checker_.CalledOnValidThread()); | 34 DCHECK(thread_checker_.CalledOnValidThread()); |
| 35 // We assume ids do not contain '.' characters. | |
| 36 DCHECK_EQ(std::string::npos, id.find('.')); | |
| 33 if (!pref_service_) | 37 if (!pref_service_) |
| 34 return kDateLastRollCallUnknown; | 38 return fallback; |
| 35 int dlrc = kDateLastRollCallUnknown; | 39 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.
| |
| 36 const base::DictionaryValue* dict = | 40 const base::DictionaryValue* dict = |
| 37 pref_service_->GetDictionary(kPersistedDataPreference); | 41 pref_service_->GetDictionary(kPersistedDataPreference); |
| 42 if (!dict) | |
| 43 return fallback; | |
| 44 if (!dict->GetInteger( | |
| 45 base::StringPrintf("apps.%s.%s", id.c_str(), key.c_str()), &result)) | |
| 46 return fallback; | |
| 47 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.
| |
| 48 } | |
| 49 | |
| 50 std::string PersistedData::GetString(const std::string& id, | |
| 51 const std::string& key) const { | |
| 52 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 38 // We assume ids do not contain '.' characters. | 53 // We assume ids do not contain '.' characters. |
| 39 DCHECK_EQ(std::string::npos, id.find('.')); | 54 DCHECK_EQ(std::string::npos, id.find('.')); |
| 40 if (!dict->GetInteger(base::StringPrintf("apps.%s.dlrc", id.c_str()), &dlrc)) | 55 if (!pref_service_) |
| 41 return kDateLastRollCallUnknown; | 56 return std::string(); |
| 42 return dlrc; | 57 std::string result; |
|
Sorin Jianu
2016/08/22 19:59:33
same as above.
waffles
2016/08/22 23:40:32
Done.
| |
| 58 const base::DictionaryValue* dict = | |
| 59 pref_service_->GetDictionary(kPersistedDataPreference); | |
| 60 if (!dict) | |
| 61 return std::string(); | |
| 62 if (!dict->GetString( | |
| 63 base::StringPrintf("apps.%s.%s", id.c_str(), key.c_str()), &result)) | |
| 64 return std::string(); | |
| 65 return result; | |
| 66 } | |
| 67 | |
| 68 int PersistedData::GetDateLastRollCall(const std::string& id) const { | |
| 69 return GetInt(id, "dlrc", kDateLastRollCallUnknown); | |
| 43 } | 70 } |
| 44 | 71 |
| 45 std::string PersistedData::GetPingFreshness(const std::string& id) const { | 72 std::string PersistedData::GetPingFreshness(const std::string& id) const { |
| 46 DCHECK(thread_checker_.CalledOnValidThread()); | 73 std::string result = GetString(id, "pf"); |
| 47 if (!pref_service_) | 74 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.
| |
| 48 return std::string(); | 75 return base::StringPrintf("{%s}", result.c_str()); |
| 49 std::string freshness; | 76 } |
| 50 const base::DictionaryValue* dict = | 77 return result; |
| 51 pref_service_->GetDictionary(kPersistedDataPreference); | 78 } |
| 52 // We assume ids do not contain '.' characters. | 79 |
| 53 DCHECK_EQ(std::string::npos, id.find('.')); | 80 std::string PersistedData::GetCohort(const std::string& id) const { |
| 54 if (!dict->GetString(base::StringPrintf("apps.%s.pf", id.c_str()), | 81 return GetString(id, "cohort"); |
| 55 &freshness)) | 82 } |
| 56 return std::string(); | 83 |
| 57 return base::StringPrintf("{%s}", freshness.c_str()); | 84 std::string PersistedData::GetCohortName(const std::string& id) const { |
| 85 return GetString(id, "cohortname"); | |
| 86 } | |
| 87 | |
| 88 std::string PersistedData::GetCohortHint(const std::string& id) const { | |
| 89 return GetString(id, "cohorthint"); | |
| 58 } | 90 } |
| 59 | 91 |
| 60 void PersistedData::SetDateLastRollCall(const std::vector<std::string>& ids, | 92 void PersistedData::SetDateLastRollCall(const std::vector<std::string>& ids, |
| 61 int datenum) { | 93 int datenum) { |
| 62 DCHECK(thread_checker_.CalledOnValidThread()); | 94 DCHECK(thread_checker_.CalledOnValidThread()); |
| 63 if (!pref_service_ || datenum < 0) | 95 if (!pref_service_ || datenum < 0) |
| 64 return; | 96 return; |
| 65 DictionaryPrefUpdate update(pref_service_, kPersistedDataPreference); | 97 DictionaryPrefUpdate update(pref_service_, kPersistedDataPreference); |
| 66 for (auto id : ids) { | 98 for (auto id : ids) { |
| 67 // We assume ids do not contain '.' characters. | 99 // We assume ids do not contain '.' characters. |
| 68 DCHECK_EQ(std::string::npos, id.find('.')); | 100 DCHECK_EQ(std::string::npos, id.find('.')); |
| 69 update->SetInteger(base::StringPrintf("apps.%s.dlrc", id.c_str()), datenum); | 101 update->SetInteger(base::StringPrintf("apps.%s.dlrc", id.c_str()), datenum); |
| 70 update->SetString(base::StringPrintf("apps.%s.pf", id.c_str()), | 102 update->SetString(base::StringPrintf("apps.%s.pf", id.c_str()), |
| 71 base::GenerateGUID()); | 103 base::GenerateGUID()); |
| 72 } | 104 } |
| 73 } | 105 } |
| 74 | 106 |
| 107 void PersistedData::SetString(const std::string& id, | |
| 108 const std::string& key, | |
| 109 const std::string& value) { | |
| 110 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 111 if (!pref_service_) | |
| 112 return; | |
| 113 DictionaryPrefUpdate update(pref_service_, kPersistedDataPreference); | |
| 114 update->SetString(base::StringPrintf("apps.%s.%s", id.c_str(), key.c_str()), | |
| 115 value); | |
| 116 } | |
| 117 | |
| 118 void PersistedData::SetCohort(const std::string& id, | |
| 119 const std::string& cohort) { | |
| 120 SetString(id, "cohort", cohort); | |
| 121 } | |
| 122 | |
| 123 void PersistedData::SetCohortName(const std::string& id, | |
| 124 const std::string& cohort_name) { | |
| 125 SetString(id, "cohortname", cohort_name); | |
| 126 } | |
| 127 | |
| 128 void PersistedData::SetCohortHint(const std::string& id, | |
| 129 const std::string& cohort_hint) { | |
| 130 SetString(id, "cohorthint", cohort_hint); | |
| 131 } | |
| 132 | |
| 75 void PersistedData::RegisterPrefs(PrefRegistrySimple* registry) { | 133 void PersistedData::RegisterPrefs(PrefRegistrySimple* registry) { |
| 76 registry->RegisterDictionaryPref(kPersistedDataPreference); | 134 registry->RegisterDictionaryPref(kPersistedDataPreference); |
| 77 } | 135 } |
| 78 | 136 |
| 79 } // namespace update_client | 137 } // namespace update_client |
| OLD | NEW |