blob: 73fd92d62aeab2fa4094d57043138ad77082dd24 [file] [log] [blame]
wafflesd2d9a332016-04-09 01:59:571// 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
15class PrefRegistrySimple;
16class PrefService;
17
Sorin Jianub9258672020-02-19 16:52:1718namespace base {
19class Version;
20} // namespace base
21
wafflesd2d9a332016-04-09 01:59:5722namespace update_client {
23
Minh X. Nguyenbe9d8762017-09-11 20:57:4024class ActivityDataService;
25
wafflesd2d9a332016-04-09 01:59:5726// 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.
33class PersistedData {
34 public:
Minh X. Nguyenbe9d8762017-09-11 20:57:4035 // Constructs a provider using the specified |pref_service| and
36 // |activity_data_service|.
wafflesd2d9a332016-04-09 01:59:5737 // The associated preferences are assumed to already be registered.
Minh X. Nguyenbe9d8762017-09-11 20:57:4038 // The |pref_service| and |activity_data_service| must outlive the entire
39 // update_client.
40 PersistedData(PrefService* pref_service,
41 ActivityDataService* activity_data_service);
wafflesd2d9a332016-04-09 01:59:5742
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. Nguyenbe9d8762017-09-11 20:57:4050 // 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
wafflese7dff732016-04-15 23:51:4957 // 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
wafflesd2d9a332016-04-09 01:59:5762 // 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
wafflese7dff732016-04-15 23:51:4965 // has been fully initialized are ignored. Also sets the PingFreshness.
66 void SetDateLastRollCall(const std::vector<std::string>& ids, int datenum);
wafflesd2d9a332016-04-09 01:59:5767
Minh X. Nguyenbe9d8762017-09-11 20:57:4068 // 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
wafflesd2d9a332016-04-09 01:59:5777 // This is called only via update_client's RegisterUpdateClientPreferences.
78 static void RegisterPrefs(PrefRegistrySimple* registry);
79
wafflesf43eb2fd32016-08-23 19:15:2980 // 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. Nguyenbe9d8762017-09-11 20:57:4094 // 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 Jianu2d0c54a2020-02-27 21:35:52106 // These functions access |pv| data for the specified |id|. Returns an empty
107 // version, if the version is not found.
Sorin Jianub9258672020-02-19 16:52:17108 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
wafflesd2d9a332016-04-09 01:59:57115 private:
wafflesf43eb2fd32016-08-23 19:15:29116 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
wafflesd2d9a332016-04-09 01:59:57122 base::ThreadChecker thread_checker_;
123 PrefService* pref_service_;
Minh X. Nguyenbe9d8762017-09-11 20:57:40124 ActivityDataService* activity_data_service_;
wafflesd2d9a332016-04-09 01:59:57125
126 DISALLOW_COPY_AND_ASSIGN(PersistedData);
127};
128
129} // namespace update_client
130
131#endif // COMPONENTS_UPDATE_CLIENT_PERSISTED_DATA_H_