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_MODULE_USAGE_DATA_STORAGE_H 17 #define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_MODULE_USAGE_DATA_STORAGE_H 18 19 #include <map> 20 #include <mutex> 21 22 #include "module_usage_record.h" 23 24 #include "bundle_constants.h" 25 #include "distributed_kv_data_manager.h" 26 #include "inner_bundle_info.h" 27 28 namespace OHOS { 29 namespace AppExecFwk { 30 class DataMgr; 31 32 class ModuleUsageRecordStorage : public std::enable_shared_from_this<ModuleUsageRecordStorage> { 33 public: 34 ModuleUsageRecordStorage(); 35 virtual ~ModuleUsageRecordStorage(); 36 37 // add&update 38 bool AddOrUpdateRecord(ModuleUsageRecord& data, const std::string& deviceId, int32_t userId); 39 bool DeleteRecordByUserId(int32_t userId); 40 bool DeleteUsageRecord(const InnerBundleInfo& data, int32_t userId); 41 bool MarkUsageRecordRemoved(const InnerBundleInfo &data, int32_t userId); 42 bool QueryRecordByNum(int32_t maxNum, std::vector<ModuleUsageRecord>& records, int32_t userId); 43 void SetDataMgr(const std::weak_ptr<DataMgr>& dataMgr) const; 44 void OnKvStoreDeath(); 45 void RegisterKvStoreDeathListener(); 46 private: 47 48 bool DeleteRecordByKeys(const std::string& bundleName, std::vector<DistributedKv::Key>& keys); 49 bool QueryRecordByCondition(DistributedKv::DataQuery& query, std::vector<DistributedKv::Entry>& records); 50 void AbilityRecordToKey(const std::string& userId, const std::string& deviceId, 51 const std::string& bundleName, const std::string& moduleName, std::string& key) const; 52 void InnerBundleInfoToKeys(const InnerBundleInfo& data, int32_t userId, 53 std::vector<DistributedKv::Key>& keys) const; 54 void FillDataStorageKeys(const std::string& userId, const std::string& bundleName, 55 const std::string& moduleName, std::vector<DistributedKv::Key>& keys) const; 56 bool ParseKey(const std::string& key, ModuleUsageRecord& record) const; 57 void UpdateUsageRecord(const std::string& jsonString, ModuleUsageRecord& data); 58 void SaveEntries(const std::vector<DistributedKv::Entry>& allEntries, 59 std::vector<ModuleUsageRecord>& records) const; 60 void TryTwice(const std::function<DistributedKv::Status()>& func) const; 61 bool CheckKvStore(); 62 DistributedKv::Status GetKvStore(); 63 bool ResetKvStore(); 64 65 private: 66 const DistributedKv::AppId appId_ { Constants::APP_ID }; 67 const DistributedKv::StoreId storeId_ { Constants::ABILITY_USAGE_STORE_ID }; 68 DistributedKv::DistributedKvDataManager dataManager_; 69 std::shared_ptr<DistributedKv::SingleKvStore> kvStorePtr_; 70 mutable std::mutex kvStorePtrMutex_; 71 enum { 72 DATABASE_KEY_INDEX_USER_ID = 0, 73 DATABASE_KEY_INDEX_DEVICE_ID, 74 DATABASE_KEY_INDEX_BUNDLE_NAME, 75 DATABASE_KEY_INDEX_MODULE_NAME, 76 DATABASE_KEY_INDEX_MAX_LENGTH, 77 }; 78 }; 79 } // namespace AppExecFwk 80 } // namespace OHOS 81 #endif // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_MODULE_USAGE_DATA_STORAGE_H