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 "executor_factory.h"
23 #include "log_print.h"
24 #include "metadata/meta_data_manager.h"
25 #include "utils/anonymous.h"
26 #include "utils/constant.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()37 void UpgradeManager::Init()
38 {
39 OHOS::DistributedKv::KvStoreTask retryTask([this]() {
40 do {
41 if (InitLocalCapability()) {
42 break;
43 }
44 static constexpr int RETRY_INTERVAL = 500; // millisecond
45 std::this_thread::sleep_for(std::chrono::milliseconds(RETRY_INTERVAL));
46 } while (true);
47 });
48 ExecutorFactory::GetInstance().Execute(std::move(retryTask));
49 }
50
GetCapability(const std::string & deviceId,bool & status)51 CapMetaData UpgradeManager::GetCapability(const std::string &deviceId, bool &status)
52 {
53 status = true;
54 if (capabilityMap_.Contains(deviceId)) {
55 return capabilityMap_.Find(deviceId).second;
56 }
57 ZLOGI("load capability from meta");
58 CapMetaData capMetaData;
59 auto dbKey = CapMetaRow::GetKeyFor(deviceId);
60 ZLOGD("cap key:%{public}s", Anonymous::Change(std::string(dbKey.begin(), dbKey.end())).c_str());
61 status = MetaDataManager::GetInstance().LoadMeta(std::string(dbKey.begin(), dbKey.end()), capMetaData);
62 if (status) {
63 capabilityMap_.Insert(deviceId, capMetaData);
64 }
65 ZLOGI("device:%{public}s, version:%{public}d, insert:%{public}d", Anonymous::Change(deviceId).c_str(),
66 capMetaData.version, status);
67 return capMetaData;
68 }
69
InitLocalCapability()70 bool UpgradeManager::InitLocalCapability()
71 {
72 auto localDeviceId = DmAdapter::GetInstance().GetLocalDevice().uuid;
73 CapMetaData capMetaData;
74 capMetaData.version = CapMetaData::CURRENT_VERSION;
75 auto dbKey = CapMetaRow::GetKeyFor(localDeviceId);
76 bool status = MetaDataManager::GetInstance().SaveMeta({ dbKey.begin(), dbKey.end() }, capMetaData);
77 if (status) {
78 capabilityMap_.Insert(localDeviceId, capMetaData);
79 }
80 ZLOGI("put capability meta data ret %{public}d", status);
81 return status;
82 }
83
SetCompatibleIdentifyByType(DistributedDB::KvStoreNbDelegate * storeDelegate,const KvStoreTuple & tuple,DistributedData::AUTH_GROUP_TYPE groupType)84 void UpgradeManager::SetCompatibleIdentifyByType(DistributedDB::KvStoreNbDelegate *storeDelegate,
85 const KvStoreTuple &tuple, DistributedData::AUTH_GROUP_TYPE groupType)
86 {
87 if (storeDelegate == nullptr) {
88 ZLOGE("null store delegate");
89 return;
90 }
91 auto localDevice = DmAdapter::GetInstance().GetLocalDevice().uuid;
92 auto devices =
93 AuthDelegate::GetInstance()->GetTrustedDevicesByType(groupType, std::stoi(tuple.userId), tuple.appId);
94 auto result = std::remove_if(devices.begin(), devices.end(), [&localDevice](const std::string &device) {
95 if (localDevice == device) {
96 return true;
97 }
98 bool flag = false;
99 auto capability = DistributedData::UpgradeManager::GetInstance().GetCapability(device, flag);
100 return !flag || capability.version >= DistributedData::CapMetaData::CURRENT_VERSION;
101 });
102 devices.erase(result, devices.end());
103
104 bool isSuccess = false;
105 auto compatibleUser = UpgradeManager::GetIdentifierByType(groupType, isSuccess);
106 if (!isSuccess) {
107 ZLOGW("get identifier by type failed");
108 return;
109 }
110
111 auto syncIdentifier =
112 DistributedDB::KvStoreDelegateManager::GetKvStoreIdentifier(compatibleUser, tuple.appId, tuple.storeId);
113 ZLOGI("set compatible identifier, store:%{public}s, user:%{public}s, device:%{public}.10s", tuple.storeId.c_str(),
114 compatibleUser.c_str(), DistributedData::Serializable::Marshall(devices).c_str());
115 storeDelegate->SetEqualIdentifier(syncIdentifier, devices);
116 }
117
GetIdentifierByType(int32_t groupType,bool & isSuccess)118 std::string UpgradeManager::GetIdentifierByType(int32_t groupType, bool &isSuccess)
119 {
120 isSuccess = true;
121 if (groupType == PEER_TO_PEER_GROUP) {
122 return "default";
123 } else if (groupType == IDENTICAL_ACCOUNT_GROUP) {
124 auto accountId = AccountDelegate::GetInstance()->GetCurrentAccountId();
125 if (accountId.empty()) {
126 ZLOGE("failed to get current account id");
127 isSuccess = false;
128 return {};
129 }
130 return accountId;
131 } else {
132 ZLOGW("not supported group type:%{public}d", groupType);
133 isSuccess = false;
134 return {};
135 }
136 }
137 } // namespace OHOS::DistributedData
138