1 /*
2  * Copyright (c) 2024 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 DISTRIBUTED_BM_STORAGE_H
17 #define DISTRIBUTED_BM_STORAGE_H
18 
19 #include <future>
20 #include <map>
21 #include <mutex>
22 #include <shared_mutex>
23 
24 #include "bundle_info.h"
25 #include "bundle_mgr_interface.h"
26 #include "distributed_kv_data_manager.h"
27 #include "deviceManager/dms_device_info.h"
28 #include "distributed_data_change_listener.h"
29 #include "distributed_kv_data_manager.h"
30 #include "distributed_sched_continuation.h"
31 #include "mission/distributed_bundle_info.h"
32 #include "kvstore_death_recipient.h"
33 #include "os_account_manager.h"
34 
35 namespace OHOS {
36 namespace DistributedSchedule {
37 namespace {
38 // distributed bm
39 static std::string DMS_BM_APP_ID = "DistributedSchedule";
40 static std::string DISTRIBUTE_BM_STORE_ID = "dms_bundle_datas";
41 const uint16_t MAX_BUNDLEID = 65535; // size of uint16_t
42 const uint8_t MAX_CONTINUETYPEID = 255; // size of uint8_t
43 }
44 struct ContinueEventInfo {
45     std::string networkId;
46     std::string bundleName;
47     std::string moduleName;
48     std::string abilityName;
49     std::string developerId;
50 };
51 
52 class DmsBmStorage {
53 public:
54     DmsBmStorage();
55     ~DmsBmStorage();
56     static std::shared_ptr<DmsBmStorage> GetInstance();
57     OHOS::sptr<OHOS::AppExecFwk::IBundleMgr> GetBundleMgr();
58     bool SaveStorageDistributeInfo(const std::string &bundleName, bool isPackageChange = false);
59     bool DeleteStorageDistributeInfo(const std::string &bundleName);
60     bool GetStorageDistributeInfo(const std::string &networkId, const std::string &bundleName,
61         DmsBundleInfo &info);
62     bool GetDistributedBundleName(const std::string &networkId,  const uint16_t& bundleNameId,
63         std::string& bundleName);
64     bool GetDistributedBundleInfo(const std::string &networkId, const uint16_t &bundleNameId,
65         DmsBundleInfo &distributeBundleInfo);
66     bool GetBundleNameId(const std::string& bundleName, uint16_t &bundleNameId);
67     std::string GetContinueType(const std::string &networkId, std::string &bundleName, uint8_t continueTypeId);
68     std::string GetAbilityName(const std::string &networkId, std::string &bundleName, std::string &continueType);
69     bool GetContinueTypeId(const std::string &bundleName, const std::string &abilityName, uint8_t &continueTypeId);
70     bool GetContinueEventInfo(const std::string &networkId, const std::string &bundleName,
71         const std::string& continueType, ContinueEventInfo &continueEventInfo);
72     void UpdateDistributedData();
73     int32_t CloudSync();
74     void FindProvishionInfo(OHOS::sptr<OHOS::AppExecFwk::IBundleMgr> bundleMgr,
75         AppExecFwk::AppProvisionInfo appProvisionInfo, std::vector<AccountSA::OsAccountInfo> accounts,
76         int32_t result, const std::string& bundleName);
77     void DmsPutBatch(const std::vector<DmsBundleInfo> &dmsBundleInfos);
78     bool UpdatePublicRecords(const std::string &localUdid);
79 
80 private:
81     std::string DeviceAndNameToKey(const std::string &udid, const std::string &bundleName) const;
82     void TryTwice(const std::function<DistributedKv::Status()> &func) const;
83     bool CheckKvStore();
84     DistributedKv::Status GetKvStore();
85     bool DealGetBundleName(const std::string &networkId, const uint16_t& bundleNameId, std::string &bundleName);
86     uint16_t CreateBundleNameId(const std::string &bundleName, bool isPackageChange = false);
87     void AddBundleNameId(const uint16_t &bundleNameId, const std::string &bundleName);
88     void DelBundleNameId(const std::string &bundleName);
89     DmsBundleInfo ConvertToDistributedBundleInfo(const AppExecFwk::BundleInfo &bundleInfo,
90         AppExecFwk::AppProvisionInfo appProvisionInfo,
91         bool isPackageChange = false);
92     bool InnerSaveStorageDistributeInfo(const DmsBundleInfo &distributedBundleInfo, const std::string &localUdid);
93     std::map<std::string, DmsBundleInfo> GetAllOldDistributionBundleInfo(
94         const std::vector<std::string> &bundleNames);
95     void GetEntries(const std::string &networkId, const OHOS::DistributedKv::Key &allEntryKeyPrefix,
96         std::promise<OHOS::DistributedKv::Status> &resultStatusSignal,
97         std::vector<OHOS::DistributedKv::Entry> &allEntries);
98     OHOS::DistributedKv::Status GetResultSatus(std::promise<OHOS::DistributedKv::Status> &resultStatusSignal);
99     bool DelReduData(const std::string &networkId, const std::vector<OHOS::DistributedKv::Entry> &reduRiskEntries);
100     bool CheckSyncData(const std::string &networkId);
101     bool RebuildLocalData();
102     bool GetLastBundleNameId(uint16_t &bundleNameId);
103 
104 private:
105     static std::mutex mutex_;
106     static std::shared_ptr<DmsBmStorage> instance_;
107     const DistributedKv::AppId appId_ {DMS_BM_APP_ID};
108     const DistributedKv::StoreId storeId_ {DISTRIBUTE_BM_STORE_ID};
109     DistributedKv::DistributedKvDataManager dataManager_;
110     std::shared_ptr<DistributedKv::SingleKvStore> kvStorePtr_;
111     mutable std::mutex kvStorePtrMutex_;
112     OHOS::sptr<OHOS::AppExecFwk::IBundleMgr> bundleMgr_;
113     std::map<uint16_t, std::string> bundleNameIdTables_;
114     int32_t waittingTime_ = 180; // 3 s
115 };
116 }  // namespace DistributedSchedule
117 }  // namespace OHOS
118 #endif  // DISTRIBUTED_BM_STORAGE_H