• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
28 namespace OHOS {
29 namespace AppExecFwk {
30 class FormDbCache final : public DelayedRefSingleton<FormDbCache> {
31 DECLARE_DELAYED_REF_SINGLETON(FormDbCache)
32 public:
33     DISALLOW_COPY_AND_MOVE(FormDbCache);
34 
35     /**
36      * @brief Load form data from DB to DbCache when starting.
37      */
38     void Start();
39 
40     /**
41      * @brief Get all form data from DbCache.
42      * @param formDBInfos Storage all DbCache.
43      */
44     void GetAllFormInfo(std::vector<FormDBInfo> &formDBInfos);
45 
46     /**
47      * @brief Save or update form data to DbCache and DB.
48      * @param formDBInfo Form data.
49      * @return Returns ERR_OK on success, others on failure.
50      */
51     ErrCode SaveFormInfo(const FormDBInfo &formDBInfo);
52 
53     /**
54      * @brief Delete form data in DbCache and DB with formId.
55      * @param formId form data Id.
56      * @return Returns ERR_OK on success, others on failure.
57      */
58     ErrCode DeleteFormInfo(int64_t formId);
59 
60     /**
61      * @brief Get record from DB cache with formId
62      * @param formId Form data Id
63      * @param record Form data
64      * @return Returns ERR_OK on success, others on failure.
65      */
66     ErrCode GetDBRecord(const int64_t formId, FormRecord &record) const;
67 
68     /**
69      * @brief Get record from DB cache with formId
70      * @param formId Form data Id
71      * @param record Form db data
72      * @return Returns ERR_OK on success, others on failure.
73      */
74     ErrCode GetDBRecord(const int64_t formId, FormDBInfo &record) const;
75 
76     /**
77      * @brief Use record save or update DB data and DB cache with formId
78      * @param formId Form data Id
79      * @param record Form data
80      * @return Returns ERR_OK on success, others on failure.
81      */
82     ErrCode UpdateDBRecord(const int64_t formId, const FormRecord &record) const;
83 
84     /**
85      * @brief Delete form data in DbCache and DB with formId.
86      * @param bundleName BundleName.
87      * @param userId user ID.
88      * @param removedDBForms Removed db form infos
89      * @return Returns ERR_OK on success, others on failure.
90      */
91     ErrCode DeleteFormInfoByBundleName(const std::string &bundleName, const int32_t userId,
92         std::vector<FormDBInfo> &removedDBForms);
93 
94     /**
95      * @brief Get no host db record.
96      * @param uid The caller uid.
97      * @param noHostFormDBList no host db record list.
98      * @param foundFormsMap Form Id list.
99      * @return Returns ERR_OK on success, others on failure.
100      */
101     ErrCode GetNoHostDBForms(const int uid, std::map<FormIdKey, std::set<int64_t>> &noHostFormDBList,
102         std::map<int64_t, bool> &foundFormsMap);
103 
104     /**
105      * @brief Get match count by bundleName and moduleName.
106      * @param bundleName BundleName.
107      * @param moduleName ModuleName.
108      * @return Returns match count.
109      */
110     int GetMatchCount(const std::string &bundleName, const std::string &moduleName);
111 
112     /**
113      * @brief delete forms bu userId.
114      * @param userId user ID.
115      */
116     void DeleteDBFormsByUserId(const int32_t userId);
117 
118     /**
119      * @brief handle get no host invalid DB forms.
120      * @param userId User ID.
121      * @param callingUid The UID of the proxy.
122      * @param matchedFormIds The set of the valid forms.
123      * @param noHostDBFormsMap The map of the no host forms.
124      * @param foundFormsMap The map of the found forms.
125      */
126     void GetNoHostInvalidDBForms(int32_t userId, int32_t callingUid, std::set<int64_t> &matchedFormIds,
127                                  std::map<FormIdKey, std::set<int64_t>> &noHostDBFormsMap,
128                                  std::map<int64_t, bool> &foundFormsMap);
129 
130     /**
131      * @brief handle delete no host DB forms.
132      * @param callingUid The UID of the proxy.
133      * @param noHostDBFormsMap The map of the no host forms.
134      * @param foundFormsMap The map of the found forms.
135      */
136     void BatchDeleteNoHostDBForms(int32_t callingUid, std::map<FormIdKey, std::set<int64_t>> &noHostDBFormsMap,
137                                   std::map<int64_t, bool> &foundFormsMap);
138 
139     /**
140      * @brief handle delete invalid DB forms.
141      * @param userId User ID.
142      * @param callingUid The UID of the proxy.
143      * @param matchedFormIds The set of the valid forms.
144      * @param removedFormsMap The map of the removed invalid forms.
145      * @return Returns ERR_OK on success, others on failure.
146      */
147     ErrCode DeleteInvalidDBForms(int32_t userId, int32_t callingUid, std::set<int64_t> &matchedFormIds,
148                                  std::map<int64_t, bool> &removedFormsMap);
149 
150     bool IsHostOwner(int64_t formId, int32_t hostUid);
151 private:
152     /**
153      * @brief Save or update form data to DbCache and DB.
154      * @param formDBInfo Form data.
155      * @return Returns ERR_OK on success, others on failure.(NoLock)
156      */
157     ErrCode SaveFormInfoNolock(const FormDBInfo &formDBInfo);
158 
159     mutable std::mutex formDBInfosMutex_;
160     std::vector<FormDBInfo> formDBInfos_;
161 };
162 }  // namespace AppExecFwk
163 }  // namespace OHOS
164 #endif  // OHOS_FORM_FWK_FORM_DB_CACHE_H
165