1 /* 2 * Copyright (c) 2021-2023 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 #ifndef OHOS_FORM_FWK_FORM_CACHE_MGR_H 16 #define OHOS_FORM_FWK_FORM_CACHE_MGR_H 17 18 #include <map> 19 #include <mutex> 20 #include <singleton.h> 21 #include <string> 22 23 #include "form_provider_data.h" 24 #include "form_rdb_data_mgr.h" 25 26 namespace OHOS { 27 namespace AppExecFwk { 28 enum class CacheState { 29 DEFAULT = 0, 30 REBOOT = 1, 31 }; 32 struct FormCache { 33 std::string formId; 34 std::string dataCache; 35 std::string imgCache; 36 CacheState cacheState = CacheState::DEFAULT; 37 }; 38 /** 39 * @class FormCacheMgr 40 * Form cache data manager. 41 */ 42 class FormCacheMgr final : public DelayedRefSingleton<FormCacheMgr> { 43 DECLARE_DELAYED_REF_SINGLETON(FormCacheMgr) 44 public: 45 DISALLOW_COPY_AND_MOVE(FormCacheMgr); 46 47 void Start(); 48 49 bool AddData(int64_t formId, const FormProviderData &formProviderData); 50 51 bool DeleteData(int64_t formId); 52 53 bool GetData(int64_t formId, std::string &data, 54 std::map<std::string, std::pair<sptr<FormAshmem>, int32_t>> &imageDataMap) const; 55 56 bool NeedAcquireProviderData(int64_t formId) const; 57 private: 58 void CreateFormCacheTable(); 59 bool GetDataCacheFromDb(int64_t formId, FormCache &formCache) const; 60 bool SaveDataCacheToDb(int64_t formId, const FormCache &formCache); 61 bool DeleteDataCacheInDb(int64_t formId); 62 bool GetImgCacheFromDb(int64_t rowId, std::vector<uint8_t> &blob, int32_t &size) const; 63 bool SaveImgCacheToDb(const std::vector<uint8_t> &value, int32_t size, int64_t &rowId); 64 bool DeleteImgCacheInDb(const std::string &rowId); 65 66 bool AddCacheData(const FormProviderData &formProviderData, FormCache &formCache); 67 bool AddImgData(const FormProviderData &formProviderData, FormCache &formCache); 68 bool AddImgDataToDb( 69 const FormProviderData &formProviderData, nlohmann::json &imgDataJson); 70 bool GetImageDataFromAshmem( 71 const std::string& picName, const sptr<Ashmem> &ashmem, int32_t len, std::vector<uint8_t> &value); 72 bool InnerGetImageData(const FormCache &formCache, 73 std::map<std::string, std::pair<sptr<FormAshmem>, int32_t>> &imageDataMap) const; 74 void ResetCacheStateAfterReboot(); 75 76 mutable std::mutex cacheMutex_; 77 }; 78 } // namespace AppExecFwk 79 } // namespace OHOS 80 81 #endif // OHOS_FORM_FWK_FORM_CACHE_MGR_H 82