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