waffles | d2d9a33 | 2016-04-09 01:59:57 | [diff] [blame] | 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 | |
| 5 | #ifndef COMPONENTS_UPDATE_CLIENT_PERSISTED_DATA_H_ |
| 6 | #define COMPONENTS_UPDATE_CLIENT_PERSISTED_DATA_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include "base/macros.h" |
| 12 | #include "base/threading/thread_checker.h" |
| 13 | #include "base/values.h" |
| 14 | |
| 15 | class PrefRegistrySimple; |
| 16 | class PrefService; |
| 17 | |
Sorin Jianu | b925867 | 2020-02-19 16:52:17 | [diff] [blame] | 18 | namespace base { |
| 19 | class Version; |
| 20 | } // namespace base |
| 21 | |
waffles | d2d9a33 | 2016-04-09 01:59:57 | [diff] [blame] | 22 | namespace update_client { |
| 23 | |
Minh X. Nguyen | be9d876 | 2017-09-11 20:57:40 | [diff] [blame] | 24 | class ActivityDataService; |
| 25 | |
waffles | d2d9a33 | 2016-04-09 01:59:57 | [diff] [blame] | 26 | // A PersistedData is a wrapper layer around a PrefService, designed to maintain |
| 27 | // update data that outlives the browser process and isn't exposed outside of |
| 28 | // update_client. |
| 29 | // |
| 30 | // The public methods of this class should be called only on the thread that |
| 31 | // initializes it - which also has to match the thread the PrefService has been |
| 32 | // initialized on. |
| 33 | class PersistedData { |
| 34 | public: |
Minh X. Nguyen | be9d876 | 2017-09-11 20:57:40 | [diff] [blame] | 35 | // Constructs a provider using the specified |pref_service| and |
| 36 | // |activity_data_service|. |
waffles | d2d9a33 | 2016-04-09 01:59:57 | [diff] [blame] | 37 | // The associated preferences are assumed to already be registered. |
Minh X. Nguyen | be9d876 | 2017-09-11 20:57:40 | [diff] [blame] | 38 | // The |pref_service| and |activity_data_service| must outlive the entire |
| 39 | // update_client. |
| 40 | PersistedData(PrefService* pref_service, |
| 41 | ActivityDataService* activity_data_service); |
waffles | d2d9a33 | 2016-04-09 01:59:57 | [diff] [blame] | 42 | |
| 43 | ~PersistedData(); |
| 44 | |
| 45 | // Returns the DateLastRollCall (the server-localized calendar date number the |
| 46 | // |id| was last checked by this client on) for the specified |id|. |
| 47 | // -2 indicates that there is no recorded date number for the |id|. |
| 48 | int GetDateLastRollCall(const std::string& id) const; |
| 49 | |
Minh X. Nguyen | be9d876 | 2017-09-11 20:57:40 | [diff] [blame] | 50 | // Returns the DateLastActive (the server-localized calendar date number the |
| 51 | // |id| was last active by this client on) for the specified |id|. |
| 52 | // -1 indicates that there is no recorded date for the |id| (i.e. this is the |
| 53 | // first time the |id| is active). |
| 54 | // -2 indicates that the |id| has an unknown value of last active date. |
| 55 | int GetDateLastActive(const std::string& id) const; |
| 56 | |
waffles | e7dff73 | 2016-04-15 23:51:49 | [diff] [blame] | 57 | // Returns the PingFreshness (a random token that is written into the profile |
| 58 | // data whenever the DateLastRollCall it is modified) for the specified |id|. |
| 59 | // "" indicates that there is no recorded freshness value for the |id|. |
| 60 | std::string GetPingFreshness(const std::string& id) const; |
| 61 | |
waffles | d2d9a33 | 2016-04-09 01:59:57 | [diff] [blame] | 62 | // Records the DateLastRollCall for the specified |ids|. |datenum| must be a |
| 63 | // non-negative integer: calls with a negative |datenum| are simply ignored. |
| 64 | // Calls to SetDateLastRollCall that occur prior to the persisted data store |
waffles | e7dff73 | 2016-04-15 23:51:49 | [diff] [blame] | 65 | // has been fully initialized are ignored. Also sets the PingFreshness. |
| 66 | void SetDateLastRollCall(const std::vector<std::string>& ids, int datenum); |
waffles | d2d9a33 | 2016-04-09 01:59:57 | [diff] [blame] | 67 | |
Minh X. Nguyen | be9d876 | 2017-09-11 20:57:40 | [diff] [blame] | 68 | // Records the DateLastActive for the specified |ids|. |datenum| must be a |
| 69 | // non-negative integer: calls with a negative |datenum| are simply ignored. |
| 70 | // Calls to SetDateLastActive that occur prior to the persisted data store |
| 71 | // has been fully initialized or the active bit of the |ids| are not set |
| 72 | // are ignored. |
| 73 | // This function also clears the active bits of the specified |ids| if they |
| 74 | // are set. |
| 75 | void SetDateLastActive(const std::vector<std::string>& ids, int datenum); |
| 76 | |
waffles | d2d9a33 | 2016-04-09 01:59:57 | [diff] [blame] | 77 | // This is called only via update_client's RegisterUpdateClientPreferences. |
| 78 | static void RegisterPrefs(PrefRegistrySimple* registry); |
| 79 | |
waffles | f43eb2fd3 | 2016-08-23 19:15:29 | [diff] [blame] | 80 | // These functions return cohort data for the specified |id|. "Cohort" |
| 81 | // indicates the membership of the client in any release channels components |
| 82 | // have set up in a machine-readable format, while "CohortName" does so in a |
| 83 | // human-readable form. "CohortHint" indicates the client's channel selection |
| 84 | // preference. |
| 85 | std::string GetCohort(const std::string& id) const; |
| 86 | std::string GetCohortHint(const std::string& id) const; |
| 87 | std::string GetCohortName(const std::string& id) const; |
| 88 | |
| 89 | // These functions set cohort data for the specified |id|. |
| 90 | void SetCohort(const std::string& id, const std::string& cohort); |
| 91 | void SetCohortHint(const std::string& id, const std::string& cohort_hint); |
| 92 | void SetCohortName(const std::string& id, const std::string& cohort_name); |
| 93 | |
Minh X. Nguyen | be9d876 | 2017-09-11 20:57:40 | [diff] [blame] | 94 | // Returns true if the active bit of the specified |id| is set. |
| 95 | bool GetActiveBit(const std::string& id) const; |
| 96 | |
| 97 | // The following two functions returns the number of days since the last |
| 98 | // time the client checked for update/was active. |
| 99 | // -1 indicates that this is the first time the client reports |
| 100 | // an update check/active for the specified |id|. |
| 101 | // -2 indicates that the client has no information about the |
| 102 | // update check/last active of the specified |id|. |
| 103 | int GetDaysSinceLastRollCall(const std::string& id) const; |
| 104 | int GetDaysSinceLastActive(const std::string& id) const; |
| 105 | |
Sorin Jianu | 2d0c54a | 2020-02-27 21:35:52 | [diff] [blame] | 106 | // These functions access |pv| data for the specified |id|. Returns an empty |
| 107 | // version, if the version is not found. |
Sorin Jianu | b925867 | 2020-02-19 16:52:17 | [diff] [blame] | 108 | base::Version GetProductVersion(const std::string& id) const; |
| 109 | void SetProductVersion(const std::string& id, const base::Version& pv); |
| 110 | |
| 111 | // These functions access the fingerprint for the specified |id|. |
| 112 | std::string GetFingerprint(const std::string& id) const; |
| 113 | void SetFingerprint(const std::string& id, const std::string& fingerprint); |
| 114 | |
waffles | d2d9a33 | 2016-04-09 01:59:57 | [diff] [blame] | 115 | private: |
waffles | f43eb2fd3 | 2016-08-23 19:15:29 | [diff] [blame] | 116 | int GetInt(const std::string& id, const std::string& key, int fallback) const; |
| 117 | std::string GetString(const std::string& id, const std::string& key) const; |
| 118 | void SetString(const std::string& id, |
| 119 | const std::string& key, |
| 120 | const std::string& value); |
| 121 | |
waffles | d2d9a33 | 2016-04-09 01:59:57 | [diff] [blame] | 122 | base::ThreadChecker thread_checker_; |
| 123 | PrefService* pref_service_; |
Minh X. Nguyen | be9d876 | 2017-09-11 20:57:40 | [diff] [blame] | 124 | ActivityDataService* activity_data_service_; |
waffles | d2d9a33 | 2016-04-09 01:59:57 | [diff] [blame] | 125 | |
| 126 | DISALLOW_COPY_AND_ASSIGN(PersistedData); |
| 127 | }; |
| 128 | |
| 129 | } // namespace update_client |
| 130 | |
| 131 | #endif // COMPONENTS_UPDATE_CLIENT_PERSISTED_DATA_H_ |