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 "common/util/form_id_key.h" 25 #include "data_center/form_record/form_record.h" 26 #include "data_center/form_info/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 Get all form data size from DbCache. 51 * @return Returns form data size. 52 */ 53 int32_t GetAllFormInfoSize(); 54 55 /** 56 * @brief Get form counts from DbCache by calling user id. 57 * @param currentAccountId current account ID. 58 * @param callingUid calling user ID. 59 * @return Returns form counts. 60 */ 61 int GetFormCountsByCallingUid(const int32_t currentAccountId, const int callingUid); 62 63 /** 64 * @brief Get all form data in DbCache and DB by bundleName. 65 * @param bundleName BundleName. 66 * @param userId user ID. 67 * @param formDBInfos all db form infos 68 */ 69 void GetAllFormDBInfoByBundleName(const std::string &bundleName, const int32_t userId, 70 std::vector<FormDBInfo> &formDBInfos); 71 72 /** 73 * @brief Save or update form data to DbCache and DB. 74 * @param formDBInfo Form data. 75 * @return Returns ERR_OK on success, others on failure. 76 */ 77 ErrCode SaveFormInfo(const FormDBInfo &formDBInfo); 78 79 /** 80 * @brief Delete form data in DbCache and DB with formId. 81 * @param formId form data Id. 82 * @return Returns ERR_OK on success, others on failure. 83 */ 84 ErrCode DeleteFormInfo(int64_t formId); 85 86 /** 87 * @brief Get record from 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 GetDBRecord(const int64_t formId, FormRecord &record) const; 93 94 /** 95 * @brief Get record from DB cache with formId 96 * @param formId Form data Id 97 * @param record Form db data 98 * @return Returns ERR_OK on success, others on failure. 99 */ 100 ErrCode GetDBRecord(const int64_t formId, FormDBInfo &record) const; 101 102 /** 103 * @brief Use record save or update DB data and DB cache with formId 104 * @param formId Form data Id 105 * @param record Form data 106 * @return Returns ERR_OK on success, others on failure. 107 */ 108 ErrCode UpdateDBRecord(const int64_t formId, const FormRecord &record) const; 109 110 /** 111 * @brief Delete form data in DbCache and DB with formId. 112 * @param bundleName BundleName. 113 * @param userId user ID. 114 * @param removedDBForms Removed db form infos 115 * @return Returns ERR_OK on success, others on failure. 116 */ 117 ErrCode DeleteFormInfoByBundleName(const std::string &bundleName, const int32_t userId, 118 std::vector<FormDBInfo> &removedDBForms); 119 120 /** 121 * @brief Get no host db record. 122 * @param uid The caller uid. 123 * @param noHostFormDBList no host db record list. 124 * @param foundFormsMap Form Id list. 125 * @return Returns ERR_OK on success, others on failure. 126 */ 127 ErrCode GetNoHostDBForms(const int uid, std::map<FormIdKey, std::set<int64_t>> &noHostFormDBList, 128 std::map<int64_t, bool> &foundFormsMap); 129 130 /** 131 * @brief Get match count by bundleName and moduleName. 132 * @param bundleName BundleName. 133 * @param moduleName ModuleName. 134 * @return Returns match count. 135 */ 136 int GetMatchCount(const std::string &bundleName, const std::string &moduleName); 137 138 /** 139 * @brief delete forms bu userId. 140 * @param userId user ID. 141 */ 142 void DeleteDBFormsByUserId(const int32_t userId); 143 144 /** 145 * @brief handle get no host invalid DB forms. 146 * @param userId User ID. 147 * @param callingUid The UID of the proxy. 148 * @param matchedFormIds The set of the valid forms. 149 * @param noHostDBFormsMap The map of the no host forms. 150 * @param foundFormsMap The map of the found forms. 151 */ 152 void GetNoHostInvalidDBForms(int32_t userId, int32_t callingUid, std::set<int64_t> &matchedFormIds, 153 std::map<FormIdKey, std::set<int64_t>> &noHostDBFormsMap, 154 std::map<int64_t, bool> &foundFormsMap); 155 156 /** 157 * @brief handle delete no host DB forms. 158 * @param callingUid The UID of the proxy. 159 * @param noHostDBFormsMap The map of the no host forms. 160 * @param foundFormsMap The map of the found forms. 161 */ 162 void BatchDeleteNoHostDBForms(int32_t callingUid, std::map<FormIdKey, std::set<int64_t>> &noHostDBFormsMap, 163 std::map<int64_t, bool> &foundFormsMap); 164 165 /** 166 * @brief handle delete invalid DB forms. 167 * @param userId User ID. 168 * @param callingUid The UID of the proxy. 169 * @param matchedFormIds The set of the valid forms. 170 * @param removedFormsMap The map of the removed invalid forms. 171 * @return Returns ERR_OK on success, others on failure. 172 */ 173 ErrCode DeleteInvalidDBForms(int32_t userId, int32_t callingUid, std::set<int64_t> &matchedFormIds, 174 std::map<int64_t, bool> &removedFormsMap); 175 176 bool IsHostOwner(int64_t formId, int32_t hostUid); 177 178 /** 179 * @brief Update form location. 180 * @param formId The Id of the form. 181 * @param formLocation The form location. 182 * @return Returns ERR_OK on success, others on failure. 183 */ 184 ErrCode UpdateFormLocation(const int64_t formId, const int32_t formLocation); 185 186 /** 187 * @brief Get multi app version code of form from DB. 188 * @param bundleName Bundlename. 189 * @return Returns version code. 190 */ 191 uint32_t GetMultiAppFormVersionCode(const std::string &bundleName); 192 193 /** 194 * @brief Save or update the multi app version code of form in DB. 195 * @param bundleName Bundlename. 196 * @param versionCode version code. 197 */ 198 void UpdateMultiAppFormVersionCode(const std::string &bundleName, uint32_t versionCode); 199 private: 200 /** 201 * @brief Save or update form data to DbCache and DB. 202 * @param formDBInfo Form data. 203 * @return Returns ERR_OK on success, others on failure.(NoLock) 204 */ 205 ErrCode SaveFormInfoNolock(const FormDBInfo &formDBInfo); 206 207 #ifdef THEME_MGR_ENABLE 208 /** 209 * @brief Call ThemeManager to delete form. 210 * @param removedFormsMap Indicates the map of forms to be delete. 211 */ 212 void DeleteThemeForms(std::map<int64_t, bool> &removedFormsMap); 213 #endif 214 mutable std::mutex formDBInfosMutex_; 215 std::vector<FormDBInfo> formDBInfos_; 216 mutable std::mutex multiAppFormVersionCodeMutex_; 217 std::map<std::string, uint32_t> multiAppFormVersionCodeMap_; 218 }; 219 } // namespace AppExecFwk 220 } // namespace OHOS 221 #endif // OHOS_FORM_FWK_FORM_DB_CACHE_H 222