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 FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_PRE_INSTALL_DATA_STORAGE_H 17 #define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_PRE_INSTALL_DATA_STORAGE_H 18 19 #include <mutex> 20 21 #include "bundle_constants.h" 22 #include "distributed_kv_data_manager.h" 23 #include "preinstall_data_storage_interface.h" 24 25 namespace OHOS { 26 namespace AppExecFwk { 27 class PreInstallDataStorage : 28 public IPreInstallDataStorage, public std::enable_shared_from_this<PreInstallDataStorage> { 29 public: 30 PreInstallDataStorage(); 31 ~PreInstallDataStorage(); 32 33 bool SavePreInstallStorageBundleInfo(const PreInstallBundleInfo &preInstallBundleInfo) override; 34 bool LoadAllPreInstallBundleInfos(std::vector<PreInstallBundleInfo> &preInstallBundleInfos) override; 35 bool DeletePreInstallStorageBundleInfo(const PreInstallBundleInfo &preInstallBundleInfo) override; 36 37 private: 38 void TryTwice(const std::function<DistributedKv::Status()>& func) const; 39 bool CheckKvStore(); 40 DistributedKv::Status GetKvStore(); 41 bool ResetKvStore(); 42 DistributedKv::Status GetEntries(std::vector<DistributedKv::Entry> &allEntries) const; 43 void SaveEntries(const std::vector<DistributedKv::Entry> &allEntries, 44 std::vector<PreInstallBundleInfo> &preInstallBundleInfos); 45 void UpdateDataBase(std::map<std::string, PreInstallBundleInfo>& infos); 46 void DeleteOldBundleInfo(const std::string& oldKey); 47 48 private: 49 const DistributedKv::AppId appId_ { Constants::APP_ID }; 50 const DistributedKv::StoreId storeId_ { Constants::PRE_INSTALL_DATA_STORE_ID }; 51 DistributedKv::DistributedKvDataManager dataManager_; 52 std::shared_ptr<DistributedKv::SingleKvStore> kvStorePtr_; 53 mutable std::mutex kvStorePtrMutex_; 54 }; 55 } // namespace AppExecFwk 56 } // namespace OHOS 57 #endif // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_PRE_INSTALL_DATA_STORAGE_H 58