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 "pre_install_bundle_info.h" 24 25 namespace OHOS { 26 namespace AppExecFwk { 27 class PreInstallDataStorage : public std::enable_shared_from_this<PreInstallDataStorage> { 28 public: 29 PreInstallDataStorage(); 30 ~PreInstallDataStorage(); 31 /** 32 * @brief Save the preInstall bundle data corresponding to the device Id of the bundle name to KvStore. 33 * @param deviceId Indicates this device Id corresponding to the bundle name. 34 * @param preInstallBundleInfo Indicates the PreInstallBundleInfo object to be save. 35 * @return Returns true if the data is successfully saved; returns false otherwise. 36 */ 37 bool SavePreInstallStorageBundleInfo( 38 const std::string &deviceId, const PreInstallBundleInfo &preInstallBundleInfo); 39 /** 40 * @brief Delete the bundle data corresponding to the device Id of the bundle name to KvStore. 41 * @param deviceId Indicates this device Id corresponding to the bundle name. 42 * @param preInstallBundleInfo Indicates the PreInstallBundleInfo object to be Delete. 43 * @return Returns true if the data is successfully deleted; returns false otherwise. 44 */ 45 bool GetPreInstallStorageBundleInfo( 46 const std::string &deviceId, PreInstallBundleInfo &preInstallBundleInfo); 47 /** 48 * @brief Obtains the PreInstallBundleInfo objects provided by bundleName. 49 * @param deviceId Indicates the bundle name of the application. 50 * @param preInstallBundleInfos Indicates information about the PreInstallBundleInfo. 51 * @return Returns true if this function is successfully called; returns false otherwise. 52 */ 53 bool LoadAllPreInstallBundleInfos(std::vector<PreInstallBundleInfo> &preInstallBundleInfos); 54 /** 55 * @brief Delete the bundle data corresponding to the device Id of the bundle name to KvStore. 56 * @param deviceId Indicates this device Id corresponding to the bundle name. 57 * @param innerBundleInfo Indicates the InnerBundleInfo object to be Delete. 58 * @return Returns true if the data is successfully deleted; returns false otherwise. 59 */ 60 bool DeletePreInstallStorageBundleInfo( 61 const std::string &deviceId, const PreInstallBundleInfo &preInstallBundleInfo); 62 63 private: 64 void TryTwice(const std::function<DistributedKv::Status()>& func) const; 65 bool CheckKvStore(); 66 DistributedKv::Status GetKvStore(); 67 bool ResetKvStore(); 68 DistributedKv::Status GetEntries(std::vector<DistributedKv::Entry> &allEntries) const; 69 void SaveEntries(const std::vector<DistributedKv::Entry> &allEntries, 70 std::vector<PreInstallBundleInfo> &preInstallBundleInfos); 71 72 private: 73 const DistributedKv::AppId appId_ { Constants::APP_ID }; 74 const DistributedKv::StoreId storeId_ { Constants::PRE_INSTALL_DATA_STORE_ID }; 75 DistributedKv::DistributedKvDataManager dataManager_; 76 std::shared_ptr<DistributedKv::SingleKvStore> kvStorePtr_; 77 mutable std::mutex kvStorePtrMutex_; 78 }; 79 } // namespace AppExecFwk 80 } // namespace OHOS 81 #endif // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_PRE_INSTALL_DATA_STORAGE_H 82