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