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_DATA_MGR_H 17 #define FOUNDATION_APPEXECFWK_SERVICES_FORMMGR_INCLUDE_FORM_DATA_MGR_H 18 19 #include <map> 20 #include <memory> 21 #include <mutex> 22 #include <set> 23 #include <singleton.h> 24 #include <string> 25 26 #include "form_host_record.h" 27 #include "form_id_key.h" 28 #include "form_info.h" 29 #include "form_item_info.h" 30 #include "form_js_info.h" 31 #include "form_record.h" 32 #include "iremote_object.h" 33 34 namespace OHOS { 35 namespace AppExecFwk { 36 /** 37 * @class FormDataMgr 38 * form data manager. 39 */ 40 class FormDataMgr final : public DelayedRefSingleton<FormDataMgr> { 41 DECLARE_DELAYED_REF_SINGLETON(FormDataMgr) 42 public: 43 DISALLOW_COPY_AND_MOVE(FormDataMgr); 44 /** 45 * @brief Allot form info by item info. 46 * @param formId The Id of the form. 47 * @param formInfo Form item info. 48 * @param callingUid The UID of the proxy. 49 * @return Returns form record. 50 */ 51 FormRecord AllotFormRecord(const FormItemInfo &formInfo, const int callingUid); 52 /** 53 * @brief Create form js info by form record. 54 * @param formId The Id of the form. 55 * @param record Form record. 56 * @param formInfo Js info. 57 * @return None. 58 */ 59 void CreateFormInfo(const int64_t formId, const FormRecord &record, FormJsInfo &formInfo); 60 /** 61 * @brief Delete form js info by form record. 62 * @param formId The Id of the form. 63 * @return Returns true if this function is successfully called; returns false otherwise. 64 */ 65 bool DeleteFormRecord(const int64_t formId); 66 /** 67 * @brief Clean removed forms for host. 68 * @param removedFormIds The id list of the forms. 69 */ 70 void CleanHostRemovedForms(const std::vector<int64_t> &removedFormIds); 71 /** 72 * @brief Allot form host record by caller token. 73 * @param info The form item info. 74 * @param callerToken callerToken 75 * @param formId The Id of the form. 76 * @param callingUid The UID of the proxy. 77 * @return Returns true if this function is successfully called; returns false otherwise. 78 */ 79 bool AllotFormHostRecord(const FormItemInfo &info, const sptr<IRemoteObject> &callerToken, 80 const int64_t formId, const int callingUid); 81 /** 82 * @brief Check temp form count is max. 83 * @return Returns ERR_OK if the temp form not reached; returns ERR_MAX_SYSTEM_TEMP_FORMS is reached. 84 */ 85 int CheckTempEnoughForm() const; 86 /** 87 * @brief Check form count is max. 88 * @param callingUid The UID of the proxy. 89 * @return Returns true if this function is successfully called; returns false otherwise. 90 */ 91 int CheckEnoughForm(const int callingUid) const; 92 /** 93 * @brief Delete temp form. 94 * @param formId The Id of the form. 95 * @return Returns true if this function is successfully called; returns false otherwise. 96 */ 97 bool DeleteTempForm(const int64_t formId); 98 /** 99 * @brief Check temp form is exist. 100 * @param formId The Id of the form. 101 * @return Returns true if the temp form is exist; returns false is not exist. 102 */ 103 bool ExistTempForm(const int64_t formId) const; 104 /** 105 * @brief Modify form temp flag by formId. 106 * @param formId The Id of the form. 107 * @param formTempFlg The form temp flag. 108 * @return Returns true if this function is successfully called; returns false otherwise. 109 */ 110 bool ModifyFormTempFlg(const int64_t formId, const bool formTempFlg); 111 /** 112 * @brief Add form user uid from form record. 113 * @param formId The Id of the form. 114 * @param formUserUid The form user uid. 115 * @return Returns true if this function is successfully called; returns false otherwise. 116 */ 117 bool AddFormUserUid(const int64_t formId, const int32_t formUserUid); 118 /** 119 * @brief Delete form user uid from form record. 120 * @param formId The Id of the form. 121 * @param uid calling user id. 122 * @return Returns true if this function is successfully called; returns false otherwise. 123 */ 124 bool DeleteFormUserUid(const int64_t formId, const int32_t uid); 125 /** 126 * @brief Update form record. 127 * @param formId The Id of the form. 128 * @param formRecord The form record. 129 * @return Returns true if this function is successfully called; returns false otherwise. 130 */ 131 bool UpdateFormRecord(const int64_t formId, const FormRecord &formRecord); 132 /** 133 * @brief Get form record. 134 * @param formId The Id of the form. 135 * @param formRecord The form record. 136 * @return Returns true if this function is successfully called; returns false otherwise. 137 */ 138 bool GetFormRecord(const int64_t formId, FormRecord &formRecord) const; 139 /** 140 * @brief Get form record. 141 * @param bundleName Bundle name. 142 * @param formInfos The form record list. 143 * @return Returns true if this function is successfully called; returns false otherwise. 144 */ 145 bool GetFormRecord(const std::string &bundleName, std::vector<FormRecord> &formInfos); 146 /** 147 * @brief Check form record is exist. 148 * @param formId The Id of the form. 149 * @return Returns true if the form record is exist; returns false is not exist. 150 */ 151 bool ExistFormRecord(const int64_t formId) const; 152 /** 153 * @brief Has form user uids in form record. 154 * @param formId The Id of the form. 155 * @return Returns true if this form has form user uids; returns false is not has. 156 */ 157 bool HasFormUserUids(const int64_t formId) const; 158 /** 159 * @brief Get form host record. 160 * @param formId The id of the form. 161 * @param formHostRecord The form host record. 162 * @return Returns true if this function is successfully called; returns false otherwise. 163 */ 164 bool GetFormHostRecord(const int64_t formId, FormHostRecord &formHostRecord) const; 165 /** 166 * @brief Delete form host record. 167 * @param callerToken The client stub of the form host record. 168 * @param formId The id of the form. 169 * @return Returns true if this function is successfully called; returns false otherwise. 170 */ 171 bool DeleteHostRecord(const sptr<IRemoteObject> &callerToken, const int64_t formId); 172 /** 173 * @brief Handle form host died. 174 * @param remoteHost Form host proxy object. 175 */ 176 void HandleHostDied(const sptr<IRemoteObject> &remoteHost); 177 /** 178 * @brief Refresh enable or not. 179 * @param formId The Id of the form. 180 * @return true on enbale, false on disable. 181 */ 182 bool IsEnableRefresh(int64_t formId); 183 /** 184 * @brief Check calling uid is valid. 185 * @param formUserUids The form user uids. 186 * @return Returns true if this user uid is valid; returns false otherwise. 187 */ 188 bool IsCallingUidValid(const std::vector<int32_t> &formUserUids) const; 189 /** 190 * @brief Generate udid. 191 * @return Returns true if this function is successfully called; returns false otherwise. 192 */ 193 bool GenerateUdidHash(); 194 /** 195 * @brief Generate form id. 196 * @return form id. 197 */ 198 int64_t GenerateFormId(); 199 /** 200 * @brief Get udid. 201 * @return udid. 202 */ 203 int64_t GetUdidHash() const; 204 /** 205 * @brief Set udid. 206 * @param udidHash udid. 207 */ 208 void SetUdidHash(const int64_t udidHash); 209 210 /** 211 * @brief Get the matched form host record by client stub. 212 * 213 * @param callerToken The client stub of the form host record. 214 * @param formHostRecord The form host record. 215 * @return Returns true if this function is successfully called, returns false otherwise. 216 */ 217 bool GetMatchedHostClient(const sptr<IRemoteObject> &callerToken, FormHostRecord &formHostRecord) const; 218 219 /** 220 * @brief Set needRefresh for FormRecord. 221 * @param formId The Id of the form. 222 * @param needRefresh true or false. 223 */ 224 void SetNeedRefresh(const int64_t formId, const bool needRefresh); 225 /** 226 * @brief Set isCountTimerRefresh for FormRecord. 227 * @param formId The Id of the form. 228 * @param countTimerRefresh true or false. 229 */ 230 void SetCountTimerRefresh(const int64_t formId, const bool countTimerRefresh); 231 /** 232 * @brief Get updated form info. 233 * @param record FormRecord. 234 * @param targetForms Target forms. 235 * @param updatedForm Updated form info. 236 * @return Returns true on success, false on failure. 237 */ 238 bool GetUpdatedForm(const FormRecord &record, const std::vector<FormInfo> &targetForms, FormInfo &updatedForm); 239 /** 240 * @brief Set isEnableUpdate for FormRecord. 241 * @param formId The Id of the form. 242 * @param enableUpdate true or false. 243 */ 244 void SetEnableUpdate(const int64_t formId, const bool enableUpdate); 245 /** 246 * @brief Set update info for FormRecord. 247 * @param formId The Id of the form. 248 * @param enableUpdate true or false. 249 * @param updateDuration Update duration. 250 * @param updateAtHour Update at hour. 251 * @param updateAtMin Update at minute. 252 */ 253 void SetUpdateInfo(const int64_t formId, const bool enableUpdate, const long updateDuration, 254 const int updateAtHour, const int updateAtMin); 255 /** 256 * @brief Clean removed form records. 257 * @param bundleName BundleName. 258 * @param removedForms The id list of the forms. 259 */ 260 void CleanRemovedFormRecords(const std::string &bundleName, std::set<int64_t> &removedForms); 261 /** 262 * @brief Clean removed temp form records. 263 * @param bundleName BundleName. 264 * @param removedForms The id list of the forms. 265 */ 266 void CleanRemovedTempFormRecords(const std::string &bundleName, std::set<int64_t> &removedForms); 267 /** 268 * @brief Get recreate form records. 269 * @param reCreateForms The id list of the forms. 270 */ 271 void GetReCreateFormRecordsByBundleName(const std::string &bundleName, std::set<int64_t> &reCreateForms); 272 /** 273 * @brief Set form isInited = true. 274 * @param formId The Id of the form. 275 * @param isInited isInited property 276 */ 277 void SetFormCacheInited(const int64_t formId, const bool isInited); 278 /** 279 * @brief Set versionUpgrade. 280 * @param formId The Id of the form. 281 * @param versionUpgrade true or false 282 */ 283 void SetVersionUpgrade(const int64_t formId, const bool versionUpgrade); 284 /** 285 * @brief Update form for host clients. 286 * @param formId The Id of the form. 287 * @param needRefresh true or false 288 */ 289 void UpdateHostNeedRefresh(const int64_t formId, const bool needRefresh); 290 /** 291 * @brief Update form for host clients. 292 * @param formId The Id of the form. 293 * @param formRecord The form info. 294 * @return Returns true if form update, false if other. 295 */ 296 bool UpdateHostForm(const int64_t formId, const FormRecord &formRecord); 297 /** 298 * @brief handle update form flag. 299 * @param formIDs The id of the forms. 300 * @param callerToken Caller ability token. 301 * @param flag form flag. 302 * @param refreshForms Refresh forms 303 * @return Returns ERR_OK on success, others on failure. 304 */ 305 int32_t UpdateHostFormFlag(std::vector<int64_t> formIds, const sptr<IRemoteObject> &callerToken, 306 const bool flag, std::vector<int64_t> &refreshForms); 307 /** 308 * @brief Find matched form id. 309 * @param formId The form id. 310 * @return Matched form id. 311 */ 312 int64_t FindMatchedFormId(const int64_t formId); 313 /** 314 * @brief Clear host data by uId. 315 * @param uId The caller uId. 316 */ 317 void ClearHostDataByUId(const int uId); 318 /** 319 * @brief Get no host temp forms. 320 * @param uid The caller uid. 321 * @param noHostTempFormsMap no host temp forms. 322 * @param foundFormsMap Form Id list. 323 */ 324 void GetNoHostTempForms(const int uid, std::map<FormIdKey, std::set<int64_t>> &noHostTempFormsMap, 325 std::map<int64_t, bool> &foundFormsMap); 326 327 /** 328 * @brief Update form for host clients. 329 * @param formId The Id of the form. 330 * @param formProviderInfo FormProviderInfo object 331 */ 332 void UpdateFormProviderInfo(const int64_t formId, const FormProviderInfo &formProviderInfo); 333 private: 334 /** 335 * @brief Create form record. 336 * @param formInfo The form item info. 337 * @param callingUid The UID of the proxy. 338 * @return Form record. 339 */ 340 FormRecord CreateFormRecord(const FormItemInfo &formInfo, const int callingUid) const; 341 /** 342 * @brief Create host record. 343 * @param info The form item info. 344 * @param callerToken The UID of the proxy. 345 * @param callingUid The UID of the proxy. 346 * @param record The form host record. 347 * @return Returns true if this function is successfully called; returns false otherwise. 348 */ 349 bool CreateHostRecord(const FormItemInfo &info, const sptr<IRemoteObject> &callerToken, 350 const int callingUid, FormHostRecord &record); 351 /** 352 * @brief Parse update config. 353 * @param record The form record. 354 * @param info The form item info. 355 * @return None. 356 */ 357 void ParseUpdateConfig(FormRecord &record, const FormItemInfo &info) const; 358 /** 359 * @brief Parse update interval config. 360 * @param record The form record. 361 * @param configDuration interval duration. 362 */ 363 void ParseIntervalConfig(FormRecord &record, const int configDuration) const; 364 /** 365 * @brief Parse at time config. 366 * @param record The form record. 367 * @param info form item info. 368 */ 369 void ParseAtTimerConfig(FormRecord &record, const FormItemInfo &info) const; 370 /** 371 * @brief Get the temp forms from host and delete temp form in cache. 372 * @param record The form record. 373 * @param recordTempForms Getted the temp forms. 374 */ 375 void HandleHostDiedForTempForms(const FormHostRecord &record, std::vector<int64_t> &recordTempForms); 376 /** 377 * @brief Check if two forms is same or not. 378 * @param record FormRecord. 379 * @param formInfo FormInfo. 380 * @return Returns true on success, false on failure. 381 */ 382 bool IsSameForm(const FormRecord &record, const FormInfo &formInfo); 383 /** 384 * @brief handle update form flag. 385 * @param formIDs The id of the forms. 386 * @param callerToken Caller ability token. 387 * @param flag form flag. 388 * @return Returns ERR_OK on success, others on failure. 389 */ 390 bool IsFormCached(const FormRecord record); 391 private: 392 mutable std::mutex formRecordMutex_; 393 mutable std::mutex formHostRecordMutex_; 394 mutable std::mutex formTempMutex_; 395 std::map<int64_t, FormRecord> formRecords_; 396 std::vector<FormHostRecord> clientRecords_; 397 std::vector<int64_t> tempForms_; 398 int64_t udidHash_; 399 }; 400 } // namespace AppExecFwk 401 } // namespace OHOS 402 #endif // FOUNDATION_APPEXECFWK_SERVICES_FORMMGR_INCLUDE_FORM_HOST_DATA_MGR_H 403