1 /*
2  * Copyright (c) 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 #define LOG_TAG "UpgradeManager"
16 
17 #include "upgrade_manager.h"
18 
19 #include <thread>
20 #include "account_delegate.h"
21 #include "device_manager_adapter.h"
22 #include "log_print.h"
23 #include "metadata/meta_data_manager.h"
24 #include "utils/anonymous.h"
25 #include "utils/constant.h"
26 #include "app_id_mapping/app_id_mapping_config_manager.h"
27 
28 namespace OHOS::DistributedData {
29 using namespace OHOS::DistributedKv;
30 using DmAdapter = DistributedData::DeviceManagerAdapter;
GetInstance()31 UpgradeManager &UpgradeManager::GetInstance()
32 {
33     static UpgradeManager instance;
34     return instance;
35 }
36 
Init(std::shared_ptr<ExecutorPool> executors)37 void UpgradeManager::Init(std::shared_ptr<ExecutorPool> executors)
38 {
39     if (executors_) {
40         return;
41     }
42     executors_ = std::move(executors);
43     executors_->Execute(GetTask());
44 }
45 
GetTask()46 ExecutorPool::Task UpgradeManager::GetTask()
47 {
48     return [this] {
49         auto succ = InitLocalCapability();
50         if (succ) {
51             return;
52         }
53         executors_->Schedule(std::chrono::milliseconds(RETRY_INTERVAL), GetTask());
54     };
55 }
56 
GetCapability(const std::string &deviceId, bool &status)57 CapMetaData UpgradeManager::GetCapability(const std::string &deviceId, bool &status)
58 {
59     status = true;
60     auto index = capabilities_.Find(deviceId);
61     if (index.first) {
62         return index.second;
63     }
64     ZLOGI("load capability from meta");
65     CapMetaData capMetaData;
66     auto dbKey = CapMetaRow::GetKeyFor(deviceId);
67     ZLOGD("cap key:%{public}s", Anonymous::Change(std::string(dbKey.begin(), dbKey.end())).c_str());
68     status = MetaDataManager::GetInstance().LoadMeta(std::string(dbKey.begin(), dbKey.end()), capMetaData);
69     if (status) {
70         capabilities_.Insert(deviceId, capMetaData);
71     }
72     ZLOGI("device:%{public}s, version:%{public}d, insert:%{public}d", Anonymous::Change(deviceId).c_str(),
73         capMetaData.version, status);
74     return capMetaData;
75 }
76 
InitLocalCapability()77 bool UpgradeManager::InitLocalCapability()
78 {
79     auto localDeviceId = DmAdapter::GetInstance().GetLocalDevice().uuid;
80     CapMetaData capMetaData;
81     capMetaData.version = CapMetaData::CURRENT_VERSION;
82     auto dbKey = CapMetaRow::GetKeyFor(localDeviceId);
83     bool status = MetaDataManager::GetInstance().SaveMeta({ dbKey.begin(), dbKey.end() }, capMetaData);
84     if (status) {
85         capabilities_.Insert(localDeviceId, capMetaData);
86     }
87     ZLOGI("put capability meta data ret %{public}d", status);
88     return status;
89 }
90 
GetIdentifierParams(std::vector<std::string> &devices, const std::vector<std::string> &uuids, int32_t authType)91 void UpgradeManager::GetIdentifierParams(std::vector<std::string> &devices,
92     const std::vector<std::string> &uuids, int32_t authType)
93 {
94     for (const auto &devId : uuids) {
95         if (DmAdapter::GetInstance().IsOHOSType(devId)) {
96             continue;
97         }
98         if (DmAdapter::GetInstance().GetAuthType(devId) != authType) {
99             continue;
100         }
101         devices.push_back(devId);
102     }
103 }
104 
SetCompatibleIdentifyByType(DistributedDB::KvStoreNbDelegate *storeDelegate, const KvStoreTuple &tuple)105 void UpgradeManager::SetCompatibleIdentifyByType(DistributedDB::KvStoreNbDelegate *storeDelegate,
106     const KvStoreTuple &tuple)
107 {
108     if (storeDelegate == nullptr) {
109         ZLOGE("null store delegate");
110         return;
111     }
112     auto uuids = DmAdapter::ToUUID(DmAdapter::GetInstance().GetRemoteDevices());
113     if (uuids.empty()) {
114         ZLOGI("no remote devs");
115         return;
116     }
117 
118     std::vector<std::string> sameAccountDevs {};
119     std::vector<std::string> defaultAccountDevs {};
120     GetIdentifierParams(sameAccountDevs, uuids, IDENTICAL_ACCOUNT);
121     GetIdentifierParams(defaultAccountDevs, uuids, NO_ACCOUNT);
122     if (!sameAccountDevs.empty()) {
123         auto convertedIds = AppIdMappingConfigManager::GetInstance().Convert(tuple.appId, tuple.userId);
124         auto identifier =
125             DistributedDB::KvStoreDelegateManager::GetKvStoreIdentifier(convertedIds.second,
126             convertedIds.first, tuple.storeId);
127         ZLOGI("same account store:%{public}s, user:%{public}s, device:%{public}.10s, appId:%{public}s",
128             Anonymous::Change(tuple.storeId).c_str(), Anonymous::Change(convertedIds.second).c_str(),
129             DistributedData::Serializable::Marshall(sameAccountDevs).c_str(), convertedIds.first.c_str());
130         storeDelegate->SetEqualIdentifier(identifier, sameAccountDevs);
131     }
132     if (!defaultAccountDevs.empty()) {
133         auto convertedIds = AppIdMappingConfigManager::GetInstance().Convert(tuple.appId, defaultAccountId);
134         auto identifier =
135             DistributedDB::KvStoreDelegateManager::GetKvStoreIdentifier(convertedIds.second,
136             convertedIds.first, tuple.storeId);
137         ZLOGI("no account identifier, store:%{public}s, device:%{public}.10s, appId:%{public}s",
138             Anonymous::Change(tuple.storeId).c_str(),
139             DistributedData::Serializable::Marshall(defaultAccountDevs).c_str(), convertedIds.first.c_str());
140         storeDelegate->SetEqualIdentifier(identifier, defaultAccountDevs);
141     }
142 }
143 } // namespace OHOS::DistributedData
144