| 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; | |
| 36 const base::DictionaryValue* dict = | 39 const base::DictionaryValue* dict = |
| 37 pref_service_->GetDictionary(kPersistedDataPreference); | 40 pref_service_->GetDictionary(kPersistedDataPreference); |
| 41 if (!dict) |
| 42 return fallback; |
| 43 int result = 0; |
| 44 return dict->GetInteger( |
| 45 base::StringPrintf("apps.%s.%s", id.c_str(), key.c_str()), &result) |
| 46 ? result |
| 47 : fallback; |
| 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 const base::DictionaryValue* dict = |
| 58 pref_service_->GetDictionary(kPersistedDataPreference); |
| 59 if (!dict) |
| 60 return std::string(); |
| 61 std::string result; |
| 62 return dict->GetString( |
| 63 base::StringPrintf("apps.%s.%s", id.c_str(), key.c_str()), &result) |
| 64 ? result |
| 65 : std::string(); |
| 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 return !result.empty() ? base::StringPrintf("{%s}", result.c_str()) : result; |
| 48 return std::string(); | 75 } |
| 49 std::string freshness; | 76 |
| 50 const base::DictionaryValue* dict = | 77 std::string PersistedData::GetCohort(const std::string& id) const { |
| 51 pref_service_->GetDictionary(kPersistedDataPreference); | 78 return GetString(id, "cohort"); |
| 52 // We assume ids do not contain '.' characters. | 79 } |
| 53 DCHECK_EQ(std::string::npos, id.find('.')); | 80 |
| 54 if (!dict->GetString(base::StringPrintf("apps.%s.pf", id.c_str()), | 81 std::string PersistedData::GetCohortName(const std::string& id) const { |
| 55 &freshness)) | 82 return GetString(id, "cohortname"); |
| 56 return std::string(); | 83 } |
| 57 return base::StringPrintf("{%s}", freshness.c_str()); | 84 |
| 85 std::string PersistedData::GetCohortHint(const std::string& id) const { |
| 86 return GetString(id, "cohorthint"); |
| 58 } | 87 } |
| 59 | 88 |
| 60 void PersistedData::SetDateLastRollCall(const std::vector<std::string>& ids, | 89 void PersistedData::SetDateLastRollCall(const std::vector<std::string>& ids, |
| 61 int datenum) { | 90 int datenum) { |
| 62 DCHECK(thread_checker_.CalledOnValidThread()); | 91 DCHECK(thread_checker_.CalledOnValidThread()); |
| 63 if (!pref_service_ || datenum < 0) | 92 if (!pref_service_ || datenum < 0) |
| 64 return; | 93 return; |
| 65 DictionaryPrefUpdate update(pref_service_, kPersistedDataPreference); | 94 DictionaryPrefUpdate update(pref_service_, kPersistedDataPreference); |
| 66 for (auto id : ids) { | 95 for (auto id : ids) { |
| 67 // We assume ids do not contain '.' characters. | 96 // We assume ids do not contain '.' characters. |
| 68 DCHECK_EQ(std::string::npos, id.find('.')); | 97 DCHECK_EQ(std::string::npos, id.find('.')); |
| 69 update->SetInteger(base::StringPrintf("apps.%s.dlrc", id.c_str()), datenum); | 98 update->SetInteger(base::StringPrintf("apps.%s.dlrc", id.c_str()), datenum); |
| 70 update->SetString(base::StringPrintf("apps.%s.pf", id.c_str()), | 99 update->SetString(base::StringPrintf("apps.%s.pf", id.c_str()), |
| 71 base::GenerateGUID()); | 100 base::GenerateGUID()); |
| 72 } | 101 } |
| 73 } | 102 } |
| 74 | 103 |
| 104 void PersistedData::SetString(const std::string& id, |
| 105 const std::string& key, |
| 106 const std::string& value) { |
| 107 DCHECK(thread_checker_.CalledOnValidThread()); |
| 108 if (!pref_service_) |
| 109 return; |
| 110 DictionaryPrefUpdate update(pref_service_, kPersistedDataPreference); |
| 111 update->SetString(base::StringPrintf("apps.%s.%s", id.c_str(), key.c_str()), |
| 112 value); |
| 113 } |
| 114 |
| 115 void PersistedData::SetCohort(const std::string& id, |
| 116 const std::string& cohort) { |
| 117 SetString(id, "cohort", cohort); |
| 118 } |
| 119 |
| 120 void PersistedData::SetCohortName(const std::string& id, |
| 121 const std::string& cohort_name) { |
| 122 SetString(id, "cohortname", cohort_name); |
| 123 } |
| 124 |
| 125 void PersistedData::SetCohortHint(const std::string& id, |
| 126 const std::string& cohort_hint) { |
| 127 SetString(id, "cohorthint", cohort_hint); |
| 128 } |
| 129 |
| 75 void PersistedData::RegisterPrefs(PrefRegistrySimple* registry) { | 130 void PersistedData::RegisterPrefs(PrefRegistrySimple* registry) { |
| 76 registry->RegisterDictionaryPref(kPersistedDataPreference); | 131 registry->RegisterDictionaryPref(kPersistedDataPreference); |
| 77 } | 132 } |
| 78 | 133 |
| 79 } // namespace update_client | 134 } // namespace update_client |
| OLD | NEW |