1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_FORM_FWK_FORM_DB_CACHE_H
17 #define OHOS_FORM_FWK_FORM_DB_CACHE_H
18 
19 #include <mutex>
20 #include <set>
21 #include <singleton.h>
22 #include <vector>
23 
24 #include "form_id_key.h"
25 #include "form_record.h"
26 #include "form_info_rdb_storage_mgr.h"
27 #ifdef THEME_MGR_ENABLE
28 #include "theme_manager_client.h"
29 #endif
30 
31 namespace OHOS {
32 namespace AppExecFwk {
33 class FormDbCache final : public DelayedRefSingleton<FormDbCache> {
34 DECLARE_DELAYED_REF_SINGLETON(FormDbCache)
35 public:
36     DISALLOW_COPY_AND_MOVE(FormDbCache);
37 
38     /**
39      * @brief Load form data from DB to DbCache when starting.
40      */
41     void Start();
42 
43     /**
44      * @brief Get all form data from DbCache.
45      * @param formDBInfos Storage all DbCache.
46      */
47     void GetAllFormInfo(std::vector<FormDBInfo> &formDBInfos);
48 
49     /**
50      * @brief Save or update form data to DbCache and DB.
51      * @param formDBInfo Form data.
52      * @return Returns ERR_OK on success, others on failure.
53      */
54     ErrCode SaveFormInfo(const FormDBInfo &formDBInfo);
55 
56     /**
57      * @brief Delete form data in DbCache and DB with formId.
58      * @param formId form data Id.
59      * @return Returns ERR_OK on success, others on failure.
60      */
61     ErrCode DeleteFormInfo(int64_t formId);
62 
63     /**
64      * @brief Get record from DB cache with formId
65      * @param formId Form data Id
66      * @param record Form data
67      * @return Returns ERR_OK on success, others on failure.
68      */
69     ErrCode GetDBRecord(const int64_t formId, FormRecord &record) const;
70 
71     /**
72      * @brief Get record from DB cache with formId
73      * @param formId Form data Id
74      * @param record Form db data
75      * @return Returns ERR_OK on success, others on failure.
76      */
77     ErrCode GetDBRecord(const int64_t formId, FormDBInfo &record) const;
78 
79     /**
80      * @brief Use record save or update DB data and DB cache with formId
81      * @param formId Form data Id
82      * @param record Form data
83      * @return Returns ERR_OK on success, others on failure.
84      */
85     ErrCode UpdateDBRecord(const int64_t formId, const FormRecord &record) const;
86 
87     /**
88      * @brief Delete form data in DbCache and DB with formId.
89      * @param bundleName BundleName.
90      * @param userId user ID.
91      * @param removedDBForms Removed db form infos
92      * @return Returns ERR_OK on success, others on failure.
93      */
94     ErrCode DeleteFormInfoByBundleName(const std::string &bundleName, const int32_t userId,
95         std::vector<FormDBInfo> &removedDBForms);
96 
97     /**
98      * @brief Get no host db record.
99      * @param uid The caller uid.
100      * @param noHostFormDBList no host db record list.
101      * @param foundFormsMap Form Id list.
102      * @return Returns ERR_OK on success, others on failure.
103      */
104     ErrCode GetNoHostDBForms(const int uid, std::map<FormIdKey, std::set<int64_t>> &noHostFormDBList,
105         std::map<int64_t, bool> &foundFormsMap);
106 
107     /**
108      * @brief Get match count by bundleName and moduleName.
109      * @param bundleName BundleName.
110      * @param moduleName ModuleName.
111      * @return Returns match count.
112      */
113     int GetMatchCount(const std::string &bundleName, const std::string &moduleName);
114 
115     /**
116      * @brief delete forms bu userId.
117      * @param userId user ID.
118      */
119     void DeleteDBFormsByUserId(const int32_t userId);
120 
121     /**
122      * @brief handle get no host invalid DB forms.
123      * @param userId User ID.
124      * @param callingUid The UID of the proxy.
125      * @param matchedFormIds The set of the valid forms.
126      * @param noHostDBFormsMap The map of the no host forms.
127      * @param foundFormsMap The map of the found forms.
128      */
129     void GetNoHostInvalidDBForms(int32_t userId, int32_t callingUid, std::set<int64_t> &matchedFormIds,
130                                  std::map<FormIdKey, std::set<int64_t>> &noHostDBFormsMap,
131                                  std::map<int64_t, bool> &foundFormsMap);
132 
133     /**
134      * @brief handle delete no host DB forms.
135      * @param callingUid The UID of the proxy.
136      * @param noHostDBFormsMap The map of the no host forms.
137      * @param foundFormsMap The map of the found forms.
138      */
139     void BatchDeleteNoHostDBForms(int32_t callingUid, std::map<FormIdKey, std::set<int64_t>> &noHostDBFormsMap,
140                                   std::map<int64_t, bool> &foundFormsMap);
141 
142     /**
143      * @brief handle delete invalid DB forms.
144      * @param userId User ID.
145      * @param callingUid The UID of the proxy.
146      * @param matchedFormIds The set of the valid forms.
147      * @param removedFormsMap The map of the removed invalid forms.
148      * @return Returns ERR_OK on success, others on failure.
149      */
150     ErrCode DeleteInvalidDBForms(int32_t userId, int32_t callingUid, std::set<int64_t> &matchedFormIds,
151                                  std::map<int64_t, bool> &removedFormsMap);
152 
153     bool IsHostOwner(int64_t formId, int32_t hostUid);
154 
155     /**
156      * @brief Update form location.
157      * @param formId The Id of the form.
158      * @param formLocation The form location.
159      * @return Returns ERR_OK on success, others on failure.
160      */
161     ErrCode UpdateFormLocation(const int64_t formId, const int32_t formLocation);
162 private:
163     /**
164      * @brief Save or update form data to DbCache and DB.
165      * @param formDBInfo Form data.
166      * @return Returns ERR_OK on success, others on failure.(NoLock)
167      */
168     ErrCode SaveFormInfoNolock(const FormDBInfo &formDBInfo);
169 
170 #ifdef THEME_MGR_ENABLE
171     /**
172      * @brief Call ThemeManager to delete form.
173      * @param removedFormsMap Indicates the map of forms to be delete.
174      */
175     void DeleteThemeForms(std::map<int64_t, bool> &removedFormsMap);
176 #endif
177     mutable std::mutex formDBInfosMutex_;
178     std::vector<FormDBInfo> formDBInfos_;
179 };
180 }  // namespace AppExecFwk
181 }  // namespace OHOS
182 #endif  // OHOS_FORM_FWK_FORM_DB_CACHE_H
183