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