Sorin Jianu | 2d0c54a | 2020-02-27 21:35:52 | [diff] [blame] | 1 | // Copyright 2020 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 CHROME_UPDATER_PERSISTED_DATA_H_ |
| 6 | #define CHROME_UPDATER_PERSISTED_DATA_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include "base/memory/ref_counted.h" |
| 12 | #include "base/sequence_checker.h" |
| 13 | |
| 14 | class PrefService; |
| 15 | |
| 16 | namespace base { |
Michael Chang | b15df0e2 | 2020-03-02 22:21:12 | [diff] [blame] | 17 | class FilePath; |
Joshua Pawlicki | c9616ce | 2020-08-11 17:24:48 | [diff] [blame] | 18 | class Value; |
Sorin Jianu | 2d0c54a | 2020-02-27 21:35:52 | [diff] [blame] | 19 | class Version; |
Michael Chang | b15df0e2 | 2020-03-02 22:21:12 | [diff] [blame] | 20 | } // namespace base |
Sorin Jianu | 2d0c54a | 2020-02-27 21:35:52 | [diff] [blame] | 21 | |
| 22 | namespace updater { |
| 23 | |
Michael Chang | b15df0e2 | 2020-03-02 22:21:12 | [diff] [blame] | 24 | struct RegistrationRequest; |
| 25 | |
Sorin Jianu | 2d0c54a | 2020-02-27 21:35:52 | [diff] [blame] | 26 | // PersistedData uses the PrefService to persist updater data that outlives |
| 27 | // the updater processes. |
| 28 | // |
| 29 | // This class has sequence affinity. |
| 30 | // |
| 31 | // A mechanism to remove apps or app versions from prefs is needed. |
| 32 | // TODO(sorin): crbug.com/1056450 |
Sorin Jianu | 94f92a1 | 2020-07-22 19:39:33 | [diff] [blame] | 33 | class PersistedData : public base::RefCountedThreadSafe<PersistedData> { |
Sorin Jianu | 2d0c54a | 2020-02-27 21:35:52 | [diff] [blame] | 34 | public: |
| 35 | // Constructs a provider using the specified |pref_service|. |
| 36 | // The associated preferences are assumed to already be registered. |
| 37 | // The |pref_service| must outlive the instance of this class. |
| 38 | explicit PersistedData(PrefService* pref_service); |
| 39 | PersistedData(const PersistedData&) = delete; |
| 40 | PersistedData& operator=(const PersistedData&) = delete; |
| 41 | |
| 42 | // These functions access |pv| data for the specified |id|. Returns an empty |
| 43 | // version, if the version is not found. |
| 44 | base::Version GetProductVersion(const std::string& id) const; |
| 45 | void SetProductVersion(const std::string& id, const base::Version& pv); |
| 46 | |
| 47 | // These functions access |fingerprint| data for the specified |id|. |
| 48 | std::string GetFingerprint(const std::string& id) const; |
| 49 | void SetFingerprint(const std::string& id, const std::string& fp); |
| 50 | |
Michael Chang | b15df0e2 | 2020-03-02 22:21:12 | [diff] [blame] | 51 | // These functions access the existence checker path for the specified id. |
| 52 | base::FilePath GetExistenceCheckerPath(const std::string& id) const; |
| 53 | void SetExistenceCheckerPath(const std::string& id, |
| 54 | const base::FilePath& ecp); |
| 55 | |
| 56 | // These functions access the brand code for the specified id. |
| 57 | std::string GetBrandCode(const std::string& id) const; |
| 58 | void SetBrandCode(const std::string& id, const std::string& bc); |
| 59 | |
| 60 | // These functions access the tag for the specified id. |
| 61 | std::string GetTag(const std::string& id) const; |
| 62 | void SetTag(const std::string& id, const std::string& tag); |
| 63 | |
| 64 | // This function sets everything in the registration request object into the |
| 65 | // persistent data store. |
| 66 | void RegisterApp(const RegistrationRequest& rq); |
| 67 | |
Michael Chang | 1b930091 | 2020-09-10 17:36:28 | [diff] [blame^] | 68 | // This function removes a registered application from the persistent store. |
| 69 | bool RemoveApp(const std::string& id); |
| 70 | |
Sorin Jianu | 2d0c54a | 2020-02-27 21:35:52 | [diff] [blame] | 71 | // Returns the app ids of the applications registered in prefs, if the |
| 72 | // application has a valid version. |
| 73 | std::vector<std::string> GetAppIds() const; |
| 74 | |
| 75 | private: |
Sorin Jianu | 94f92a1 | 2020-07-22 19:39:33 | [diff] [blame] | 76 | friend class base::RefCountedThreadSafe<PersistedData>; |
Sorin Jianu | 2d0c54a | 2020-02-27 21:35:52 | [diff] [blame] | 77 | ~PersistedData(); |
| 78 | |
Joshua Pawlicki | c9616ce | 2020-08-11 17:24:48 | [diff] [blame] | 79 | // Returns nullptr if the app key does not exist. |
| 80 | const base::Value* GetAppKey(const std::string& id) const; |
| 81 | |
| 82 | // Returns an existing or newly created app key under a root pref. |
| 83 | base::Value* GetOrCreateAppKey(const std::string& id, base::Value* root); |
| 84 | |
Sorin Jianu | 2d0c54a | 2020-02-27 21:35:52 | [diff] [blame] | 85 | std::string GetString(const std::string& id, const std::string& key) const; |
| 86 | void SetString(const std::string& id, |
| 87 | const std::string& key, |
| 88 | const std::string& value); |
Sorin Jianu | 2d0c54a | 2020-02-27 21:35:52 | [diff] [blame] | 89 | SEQUENCE_CHECKER(sequence_checker_); |
| 90 | |
| 91 | PrefService* pref_service_ = nullptr; // Not owned by this class. |
| 92 | }; |
| 93 | |
| 94 | } // namespace updater |
| 95 | |
| 96 | #endif // CHROME_UPDATER_PERSISTED_DATA_H_ |