persisted_data.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_UPDATE_CLIENT_PERSISTED_DATA_H_
  5. #define COMPONENTS_UPDATE_CLIENT_PERSISTED_DATA_H_
  6. #include <set>
  7. #include <string>
  8. #include <vector>
  9. #include "base/callback_forward.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/sequence_checker.h"
  12. #include "base/values.h"
  13. class PrefRegistrySimple;
  14. class PrefService;
  15. namespace base {
  16. class Version;
  17. } // namespace base
  18. namespace update_client {
  19. class ActivityDataService;
  20. // A PersistedData is a wrapper layer around a PrefService, designed to maintain
  21. // update data that outlives the browser process and isn't exposed outside of
  22. // update_client.
  23. //
  24. // The public methods of this class should be called only on the thread that
  25. // initializes it - which also has to match the thread the PrefService has been
  26. // initialized on.
  27. class PersistedData {
  28. public:
  29. // Constructs a provider using the specified |pref_service| and
  30. // |activity_data_service|.
  31. // The associated preferences are assumed to already be registered.
  32. // The |pref_service| and |activity_data_service| must outlive the entire
  33. // update_client.
  34. PersistedData(PrefService* pref_service,
  35. ActivityDataService* activity_data_service);
  36. PersistedData(const PersistedData&) = delete;
  37. PersistedData& operator=(const PersistedData&) = delete;
  38. ~PersistedData();
  39. // Returns the DateLastRollCall (the server-localized calendar date number the
  40. // |id| was last checked by this client on) for the specified |id|.
  41. // -2 indicates that there is no recorded date number for the |id|.
  42. int GetDateLastRollCall(const std::string& id) const;
  43. // Returns the DateLastActive (the server-localized calendar date number the
  44. // |id| was last active by this client on) for the specified |id|.
  45. // -1 indicates that there is no recorded date for the |id| (i.e. this is the
  46. // first time the |id| is active).
  47. // -2 indicates that the |id| has an unknown value of last active date.
  48. int GetDateLastActive(const std::string& id) const;
  49. // Returns the PingFreshness (a random token that is written into the profile
  50. // data whenever the DateLastRollCall it is modified) for the specified |id|.
  51. // "" indicates that there is no recorded freshness value for the |id|.
  52. std::string GetPingFreshness(const std::string& id) const;
  53. // Records the DateLastRollcall for the specified `ids`. Also records
  54. // DateLastActive, if the ids have active bits currently set, and then clears
  55. // those bits. Rotates PingFreshness. Then, calls `callback` on the calling
  56. // sequence. Calls with a negative `datenum` or that occur prior to the
  57. // initialization of the persisted data store will simply post the callback
  58. // immediately.
  59. void SetDateLastData(const std::vector<std::string>& ids,
  60. int datenum,
  61. base::OnceClosure callback);
  62. // This is called only via update_client's RegisterUpdateClientPreferences.
  63. static void RegisterPrefs(PrefRegistrySimple* registry);
  64. // Returns the install date for the specified |id|.
  65. // "InstallDate" refers to the initial date that the given |id| was first
  66. // installed on the machine. Date information is returned by the server. If
  67. // "InstallDate" is not known, -2 is returned.
  68. int GetInstallDate(const std::string& id) const;
  69. // These functions return cohort data for the specified |id|. "Cohort"
  70. // indicates the membership of the client in any release channels components
  71. // have set up in a machine-readable format, while "CohortName" does so in a
  72. // human-readable form. "CohortHint" indicates the client's channel selection
  73. // preference.
  74. std::string GetCohort(const std::string& id) const;
  75. std::string GetCohortHint(const std::string& id) const;
  76. std::string GetCohortName(const std::string& id) const;
  77. // These functions set cohort data for the specified |id|.
  78. void SetCohort(const std::string& id, const std::string& cohort);
  79. void SetCohortHint(const std::string& id, const std::string& cohort_hint);
  80. void SetCohortName(const std::string& id, const std::string& cohort_name);
  81. // Calls `callback` with the subset of `ids` that are active. The callback
  82. // is called on the calling sequence.
  83. void GetActiveBits(
  84. const std::vector<std::string>& ids,
  85. base::OnceCallback<void(const std::set<std::string>&)> callback) const;
  86. // The following two functions returns the number of days since the last
  87. // time the client checked for update/was active.
  88. // -1 indicates that this is the first time the client reports
  89. // an update check/active for the specified |id|.
  90. // -2 indicates that the client has no information about the
  91. // update check/last active of the specified |id|.
  92. int GetDaysSinceLastRollCall(const std::string& id) const;
  93. int GetDaysSinceLastActive(const std::string& id) const;
  94. // These functions access |pv| data for the specified |id|. Returns an empty
  95. // version, if the version is not found.
  96. base::Version GetProductVersion(const std::string& id) const;
  97. void SetProductVersion(const std::string& id, const base::Version& pv);
  98. // These functions access the fingerprint for the specified |id|.
  99. std::string GetFingerprint(const std::string& id) const;
  100. void SetFingerprint(const std::string& id, const std::string& fingerprint);
  101. private:
  102. // Returns nullptr if the app key does not exist.
  103. const base::Value* GetAppKey(const std::string& id) const;
  104. // Returns an existing or newly created app key under a root pref.
  105. base::Value* GetOrCreateAppKey(const std::string& id, base::Value* root);
  106. // Returns fallback if the key does not exist.
  107. int GetInt(const std::string& id, const std::string& key, int fallback) const;
  108. // Returns the empty string if the key does not exist.
  109. std::string GetString(const std::string& id, const std::string& key) const;
  110. void SetString(const std::string& id,
  111. const std::string& key,
  112. const std::string& value);
  113. void SetDateLastDataHelper(const std::vector<std::string>& ids,
  114. int datenum,
  115. base::OnceClosure callback,
  116. const std::set<std::string>& active_ids);
  117. SEQUENCE_CHECKER(sequence_checker_);
  118. raw_ptr<PrefService> pref_service_;
  119. raw_ptr<ActivityDataService> activity_data_service_;
  120. };
  121. } // namespace update_client
  122. #endif // COMPONENTS_UPDATE_CLIENT_PERSISTED_DATA_H_