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_BUNDLEMGR_INCLUDE_BUNDLE_DATA_STORAGE_DATABASE_H 17 #define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_DATA_STORAGE_DATABASE_H 18 19 #include <map> 20 21 #include "distributed_kv_data_manager.h" 22 23 #include "bundle_constants.h" 24 #include "inner_bundle_info.h" 25 #include "bundle_data_storage_interface.h" 26 27 namespace OHOS { 28 namespace AppExecFwk { 29 class BundleDataStorageDatabase : 30 public IBundleDataStorage, public std::enable_shared_from_this<BundleDataStorageDatabase> { 31 public: 32 BundleDataStorageDatabase(); 33 ~BundleDataStorageDatabase(); 34 /** 35 * @brief Load all installed bundles data from KvStore to innerBundleInfos. 36 * @param infos Indicates the map to save all installed bundles. 37 * @return Returns true if the data is successfully loaded; returns false otherwise. 38 */ 39 virtual bool LoadAllData(std::map<std::string, std::map<std::string, InnerBundleInfo>> &infos); 40 /** 41 * @brief Save the bundle data corresponding to the device Id of the bundle name to KvStore. 42 * @param deviceId Indicates this device Id corresponding to the bundle name. 43 * @param innerBundleInfo Indicates the InnerBundleInfo object to be save. 44 * @return Returns true if the data is successfully saved; returns false otherwise. 45 */ 46 virtual bool SaveStorageBundleInfo(const std::string &deviceId, const InnerBundleInfo &innerBundleInfo); 47 /** 48 * @brief Delete the bundle data corresponding to the device Id of the bundle name to KvStore. 49 * @param deviceId Indicates this device Id corresponding to the bundle name. 50 * @param innerBundleInfo Indicates the InnerBundleInfo object to be Delete. 51 * @return Returns true if the data is successfully deleted; returns false otherwise. 52 */ 53 virtual bool DeleteStorageBundleInfo(const std::string &deviceId, const InnerBundleInfo &innerBundleInfo); 54 void RegisterKvStoreDeathListener(); 55 bool ResetKvStore(); 56 private: 57 void SaveEntries(const std::vector<DistributedKv::Entry> &allEntries, 58 std::map<std::string, std::map<std::string, InnerBundleInfo>> &infos); 59 DistributedKv::Status GetEntries(std::vector<DistributedKv::Entry> &allEntries) const; 60 void TryTwice(const std::function<DistributedKv::Status()> &func) const; 61 bool CheckKvStore(); 62 DistributedKv::Status GetKvStore(); 63 64 private: 65 const DistributedKv::AppId appId_ {Constants::APP_ID}; 66 const DistributedKv::StoreId storeId_ {Constants::STORE_ID}; 67 bool KeyToDeviceAndName(const std::string &key, std::string &deviceId, std::string &bundleName) const; 68 void DeviceAndNameToKey(const std::string &deviceId, const std::string &bundleName, std::string &key) const; 69 DistributedKv::DistributedKvDataManager dataManager_; 70 std::shared_ptr<DistributedKv::SingleKvStore> kvStorePtr_; 71 // std::shared_ptr<DataChangeListener> dataChangeListener_; 72 mutable std::mutex kvStorePtrMutex_; 73 }; 74 } // namespace AppExecFwk 75 } // namespace OHOS 76 #endif // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_DATA_STORAGE_DATABASE_H 77