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 #include "meta_info_manager.h"
17 
18 #include "anonymous_string.h"
19 #include "capability_utils.h"
20 #include "constants.h"
21 #include "dh_context.h"
22 #include "dh_utils_tool.h"
23 #include "distributed_hardware_errno.h"
24 #include "distributed_hardware_log.h"
25 #include "distributed_hardware_manager.h"
26 #include "task_executor.h"
27 #include "task_factory.h"
28 #include "task_board.h"
29 
30 namespace OHOS {
31 namespace DistributedHardware {
32 
33 #undef DH_LOG_TAG
34 #define DH_LOG_TAG "MetaInfoManager"
35 
MetaInfoManager()36 MetaInfoManager::MetaInfoManager() : dbAdapterPtr_(nullptr)
37 {
38     DHLOGI("MetaInfoManager construction!");
39 }
40 
~MetaInfoManager()41 MetaInfoManager::~MetaInfoManager()
42 {
43     DHLOGI("MetaInfoManager destruction!");
44 }
45 
GetInstance()46 std::shared_ptr<MetaInfoManager> MetaInfoManager::GetInstance()
47 {
48     static std::shared_ptr<MetaInfoManager> instance = std::make_shared<MetaInfoManager>();
49     return instance;
50 }
51 
MetaInfoManagerEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> runner,std::shared_ptr<MetaInfoManager> metaInfoMgrPtr)52 MetaInfoManager::MetaInfoManagerEventHandler::MetaInfoManagerEventHandler(
53     const std::shared_ptr<AppExecFwk::EventRunner> runner, std::shared_ptr<MetaInfoManager> metaInfoMgrPtr)
54     : AppExecFwk::EventHandler(runner), metaInfoMgrWPtr_(metaInfoMgrPtr)
55 {
56     DHLOGI("Ctor MetaInfoManagerEventHandler");
57 }
58 
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)59 void MetaInfoManager::MetaInfoManagerEventHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
60 {
61     uint32_t eventId = event->GetInnerEventId();
62     auto selfPtr = metaInfoMgrWPtr_.lock();
63     if (!selfPtr) {
64         DHLOGE("Can not get strong self ptr");
65         return;
66     }
67     switch (eventId) {
68         case EVENT_META_INFO_DB_RECOVER:
69             selfPtr->SyncRemoteMetaInfos();
70             break;
71         default:
72             DHLOGE("event is undefined, id is %{public}d", eventId);
73             break;
74     }
75 }
76 
GetEventHandler()77 std::shared_ptr<MetaInfoManager::MetaInfoManagerEventHandler> MetaInfoManager::GetEventHandler()
78 {
79     return this->eventHandler_;
80 }
81 
Init()82 int32_t MetaInfoManager::Init()
83 {
84     DHLOGI("MetaInfoManager instance init!");
85     std::lock_guard<std::mutex> lock(metaInfoMgrMutex_);
86     dbAdapterPtr_ = std::make_shared<DBAdapter>(APP_ID, GLOBAL_META_INFO, shared_from_this());
87     if (dbAdapterPtr_ == nullptr) {
88         DHLOGE("dbAdapterPtr_ is null");
89         return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL;
90     }
91     if (dbAdapterPtr_->Init(false, DistributedKv::DataType::TYPE_STATICS) != DH_FWK_SUCCESS) {
92         DHLOGE("Init dbAdapterPtr_ failed");
93         return ERR_DH_FWK_RESOURCE_INIT_DB_FAILED;
94     }
95     std::shared_ptr<AppExecFwk::EventRunner> runner = AppExecFwk::EventRunner::Create(true);
96     eventHandler_ = std::make_shared<MetaInfoManager::MetaInfoManagerEventHandler>(runner, shared_from_this());
97     DHLOGI("MetaInfoManager instance init success");
98     return DH_FWK_SUCCESS;
99 }
100 
UnInit()101 int32_t MetaInfoManager::UnInit()
102 {
103     DHLOGI("MetaInfoManager UnInit");
104     std::lock_guard<std::mutex> lock(metaInfoMgrMutex_);
105     if (dbAdapterPtr_ == nullptr) {
106         DHLOGE("dbAdapterPtr_ is null");
107         return ERR_DH_FWK_RESOURCE_UNINIT_DB_FAILED;
108     }
109     dbAdapterPtr_->UnInit();
110     dbAdapterPtr_.reset();
111     return DH_FWK_SUCCESS;
112 }
113 
AddMetaCapInfos(const std::vector<std::shared_ptr<MetaCapabilityInfo>> & metaCapInfos)114 int32_t MetaInfoManager::AddMetaCapInfos(const std::vector<std::shared_ptr<MetaCapabilityInfo>> &metaCapInfos)
115 {
116     if (metaCapInfos.empty() || metaCapInfos.size() > MAX_DB_RECORD_SIZE) {
117         DHLOGE("MetaCapInfos is empty or too large!");
118         return ERR_DH_FWK_RESOURCE_RES_DB_DATA_INVALID;
119     }
120     std::lock_guard<std::mutex> lock(metaInfoMgrMutex_);
121     if (dbAdapterPtr_ == nullptr) {
122         DHLOGE("dbAdapterPtr_ is null");
123         return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL;
124     }
125     std::vector<std::string> keys;
126     std::vector<std::string> values;
127     std::string key;
128     std::string data;
129     for (auto &metaCapInfo : metaCapInfos) {
130         if (metaCapInfo == nullptr) {
131             continue;
132         }
133         key = metaCapInfo->GetKey();
134         globalMetaInfoMap_[key] = metaCapInfo;
135         if (dbAdapterPtr_->GetDataByKey(key, data) == DH_FWK_SUCCESS && data == metaCapInfo->ToJsonString()) {
136             DHLOGI("this record is exist, Key: %{public}s", metaCapInfo->GetAnonymousKey().c_str());
137             continue;
138         }
139         DHLOGI("AddMetaCapability, Key: %{public}s", metaCapInfo->GetAnonymousKey().c_str());
140         keys.push_back(key);
141         values.push_back(metaCapInfo->ToJsonString());
142     }
143     if (keys.empty() || values.empty()) {
144         DHLOGD("Records are empty, No need add data to db!");
145         return DH_FWK_SUCCESS;
146     }
147     if (dbAdapterPtr_->PutDataBatch(keys, values) != DH_FWK_SUCCESS) {
148         DHLOGE("Fail to storage batch to kv");
149         return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL;
150     }
151     return DH_FWK_SUCCESS;
152 }
153 
SyncMetaInfoFromDB(const std::string & udidHash)154 int32_t MetaInfoManager::SyncMetaInfoFromDB(const std::string &udidHash)
155 {
156     if (!IsHashSizeValid(udidHash)) {
157         return ERR_DH_FWK_PARA_INVALID;
158     }
159     DHLOGI("Sync MetaInfo from DB, udidHash: %{public}s", GetAnonyString(udidHash).c_str());
160     std::lock_guard<std::mutex> lock(metaInfoMgrMutex_);
161     if (dbAdapterPtr_ == nullptr) {
162         DHLOGE("dbAdapterPtr_ is null");
163         return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL;
164     }
165     std::vector<std::string> dataVector;
166     if (dbAdapterPtr_->GetDataByKeyPrefix(udidHash, dataVector) != DH_FWK_SUCCESS) {
167         DHLOGE("Query Metadata from DB by udidHash failed, udidHash: %{public}s", GetAnonyString(udidHash).c_str());
168         return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL;
169     }
170     if (dataVector.empty() || dataVector.size() > MAX_DB_RECORD_SIZE) {
171         DHLOGE("dataVector size: %{public}zu is invalid, maybe empty or too large.", dataVector.size());
172         return ERR_DH_FWK_RESOURCE_RES_DB_DATA_INVALID;
173     }
174     for (const auto &data : dataVector) {
175         std::shared_ptr<MetaCapabilityInfo> metaCapInfo;
176         if (GetMetaCapByValue(data, metaCapInfo) != DH_FWK_SUCCESS) {
177             DHLOGE("Get capability ptr by value failed");
178             continue;
179         }
180         globalMetaInfoMap_[metaCapInfo->GetKey()] = metaCapInfo;
181     }
182     return DH_FWK_SUCCESS;
183 }
184 
SyncRemoteMetaInfos()185 int32_t MetaInfoManager::SyncRemoteMetaInfos()
186 {
187     DHLOGI("Sync full remote device Metainfo from DB");
188     std::lock_guard<std::mutex> lock(metaInfoMgrMutex_);
189     if (dbAdapterPtr_ == nullptr) {
190         DHLOGE("dbAdapterPtr_ is null");
191         return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL;
192     }
193     std::vector<std::string> udidHashVec;
194     DHContext::GetInstance().GetOnlineDeviceUdidHash(udidHashVec);
195     for (const auto &udidHash : udidHashVec) {
196         std::vector<std::string> dataVector;
197         if (dbAdapterPtr_->GetDataByKeyPrefix(udidHash, dataVector) != DH_FWK_SUCCESS) {
198             DHLOGE("Query the udidHash: %{public}s data from DB failed", GetAnonyString(udidHash).c_str());
199             continue;
200         }
201         if (dataVector.empty() || dataVector.size() > MAX_DB_RECORD_SIZE) {
202             DHLOGE("dataVector size: %{public}zu is invalid, maybe empty or too large.", dataVector.size());
203             continue;
204         }
205         for (const auto &data : dataVector) {
206             std::shared_ptr<MetaCapabilityInfo> metaCapInfo;
207             if (GetMetaCapByValue(data, metaCapInfo) != DH_FWK_SUCCESS) {
208                 DHLOGE("Get Metainfo ptr by value failed");
209                 continue;
210             }
211             const std::string &udidHash = metaCapInfo->GetUdidHash();
212             const std::string &localUdidHash = DHContext::GetInstance().GetDeviceInfo().udidHash;
213             if (udidHash.compare(localUdidHash) == 0) {
214                 DHLOGE("device MetaInfo not need sync from db");
215                 continue;
216             }
217             globalMetaInfoMap_[metaCapInfo->GetKey()] = metaCapInfo;
218         }
219     }
220     return DH_FWK_SUCCESS;
221 }
222 
GetDataByKeyPrefix(const std::string & keyPrefix,MetaCapInfoMap & metaCapMap)223 int32_t MetaInfoManager::GetDataByKeyPrefix(const std::string &keyPrefix, MetaCapInfoMap &metaCapMap)
224 {
225     if (!IsKeySizeValid(keyPrefix)) {
226         return ERR_DH_FWK_PARA_INVALID;
227     }
228     std::lock_guard<std::mutex> lock(metaInfoMgrMutex_);
229     if (dbAdapterPtr_ == nullptr) {
230         DHLOGE("dbAdapterPtr is null");
231         return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL;
232     }
233     std::vector<std::string> dataVector;
234     if (dbAdapterPtr_->GetDataByKeyPrefix(keyPrefix, dataVector) != DH_FWK_SUCCESS) {
235         DHLOGE("Query metaInfo from db failed, keyPrefix: %{public}s", GetAnonyString(keyPrefix).c_str());
236         return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL;
237     }
238     if (dataVector.empty() || dataVector.size() > MAX_DB_RECORD_SIZE) {
239         DHLOGE("On dataVector error, maybe empty or too large.");
240         return ERR_DH_FWK_RESOURCE_RES_DB_DATA_INVALID;
241     }
242     for (const auto &data : dataVector) {
243         std::shared_ptr<MetaCapabilityInfo> metaCapInfo;
244         if (GetMetaCapByValue(data, metaCapInfo) != DH_FWK_SUCCESS) {
245             DHLOGE("Get Metainfo ptr by value failed");
246             continue;
247         }
248         metaCapMap[metaCapInfo->GetKey()] = metaCapInfo;
249     }
250     return DH_FWK_SUCCESS;
251 }
252 
RemoveMetaInfoByKey(const std::string & key)253 int32_t MetaInfoManager::RemoveMetaInfoByKey(const std::string &key)
254 {
255     if (!IsKeySizeValid(key)) {
256         return ERR_DH_FWK_PARA_INVALID;
257     }
258     DHLOGI("Remove device metaInfo, key: %{public}s", GetAnonyString(key).c_str());
259     std::lock_guard<std::mutex> lock(metaInfoMgrMutex_);
260     if (dbAdapterPtr_ == nullptr) {
261         DHLOGE("dbAdapterPtr_ is null");
262         return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL;
263     }
264 
265     globalMetaInfoMap_.erase(key);
266     if (dbAdapterPtr_->RemoveDataByKey(key) != DH_FWK_SUCCESS) {
267         DHLOGE("Remove device metaData failed, key: %{public}s", GetAnonyString(key).c_str());
268         return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL;
269     }
270     return DH_FWK_SUCCESS;
271 }
272 
GetMetaCapInfo(const std::string & udidHash,const std::string & dhId,std::shared_ptr<MetaCapabilityInfo> & metaCapPtr)273 int32_t MetaInfoManager::GetMetaCapInfo(const std::string &udidHash,
274     const std::string &dhId, std::shared_ptr<MetaCapabilityInfo> &metaCapPtr)
275 {
276     if (!IsHashSizeValid(udidHash) || !IsIdLengthValid(dhId)) {
277         return ERR_DH_FWK_PARA_INVALID;
278     }
279     std::lock_guard<std::mutex> lock(metaInfoMgrMutex_);
280     std::string key = GetCapabilityKey(udidHash, dhId);
281     if (globalMetaInfoMap_.find(key) == globalMetaInfoMap_.end()) {
282         DHLOGE("Can not find capability In globalMetaInfoMap_: %{public}s", GetAnonyString(udidHash).c_str());
283         return ERR_DH_FWK_RESOURCE_CAPABILITY_MAP_NOT_FOUND;
284     }
285     metaCapPtr = globalMetaInfoMap_[key];
286     return DH_FWK_SUCCESS;
287 }
288 
GetMetaCapInfosByUdidHash(const std::string & udidHash,std::vector<std::shared_ptr<MetaCapabilityInfo>> & metaCapInfos)289 void MetaInfoManager::GetMetaCapInfosByUdidHash(const std::string &udidHash,
290     std::vector<std::shared_ptr<MetaCapabilityInfo>> &metaCapInfos)
291 {
292     if (!IsHashSizeValid(udidHash)) {
293         return;
294     }
295     std::lock_guard<std::mutex> lock(metaInfoMgrMutex_);
296     for (auto &metaCapInfo : globalMetaInfoMap_) {
297         if (IsCapKeyMatchDeviceId(metaCapInfo.first, udidHash)) {
298             metaCapInfos.emplace_back(metaCapInfo.second);
299         }
300     }
301 }
302 
GetMetaCapByValue(const std::string & value,std::shared_ptr<MetaCapabilityInfo> & metaCapPtr)303 int32_t MetaInfoManager::GetMetaCapByValue(const std::string &value, std::shared_ptr<MetaCapabilityInfo> &metaCapPtr)
304 {
305     if (!IsMessageLengthValid(value)) {
306         return ERR_DH_FWK_PARA_INVALID;
307     }
308     if (metaCapPtr == nullptr) {
309         metaCapPtr = std::make_shared<MetaCapabilityInfo>();
310     }
311     return metaCapPtr->FromJsonString(value);
312 }
313 
GetMetaDataByDHType(const DHType dhType,MetaCapInfoMap & metaInfoMap)314 int32_t MetaInfoManager::GetMetaDataByDHType(const DHType dhType, MetaCapInfoMap &metaInfoMap)
315 {
316     std::lock_guard<std::mutex> lock(metaInfoMgrMutex_);
317     for (const auto &metaCapInfo : globalMetaInfoMap_) {
318         if (metaCapInfo.second->GetDHType() != dhType) {
319             continue;
320         }
321         metaInfoMap[metaCapInfo.first] = metaCapInfo.second;
322     }
323     return DH_FWK_SUCCESS;
324 }
325 
SyncDataByNetworkId(const std::string & networkId)326 int32_t MetaInfoManager::SyncDataByNetworkId(const std::string &networkId)
327 {
328     if (!IsIdLengthValid(networkId)) {
329         DHLOGE("networId: %{public}s is invalid", GetAnonyString(networkId).c_str());
330         return ERR_DH_FWK_PARA_INVALID;
331     }
332     if (dbAdapterPtr_ == nullptr) {
333         DHLOGE("dbAdapterPtr is null");
334         return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL;
335     }
336     dbAdapterPtr_->SyncDataByNetworkId(networkId);
337     return DH_FWK_SUCCESS;
338 }
339 
OnChange(const DistributedKv::ChangeNotification & changeNotification)340 void MetaInfoManager::OnChange(const DistributedKv::ChangeNotification &changeNotification)
341 {
342     DHLOGI("MetaInfoManager: DB data OnChange");
343     if (!changeNotification.GetInsertEntries().empty() &&
344         changeNotification.GetInsertEntries().size() <= MAX_DB_RECORD_SIZE) {
345         DHLOGI("MetaInfoManager Handle capability data add change");
346         HandleMetaCapabilityAddChange(changeNotification.GetInsertEntries());
347     }
348     if (!changeNotification.GetUpdateEntries().empty() &&
349         changeNotification.GetUpdateEntries().size() <= MAX_DB_RECORD_SIZE) {
350         DHLOGI("MetaInfoManager Handle capability data update change");
351         HandleMetaCapabilityUpdateChange(changeNotification.GetUpdateEntries());
352     }
353     if (!changeNotification.GetDeleteEntries().empty() &&
354         changeNotification.GetDeleteEntries().size() <= MAX_DB_RECORD_SIZE) {
355         DHLOGI("MetaInfoManager Handle capability data delete change");
356         HandleMetaCapabilityDeleteChange(changeNotification.GetDeleteEntries());
357     }
358 }
359 
OnChange(const DistributedKv::DataOrigin & origin,Keys && keys)360 void MetaInfoManager::OnChange(const DistributedKv::DataOrigin &origin, Keys &&keys)
361 {
362     DHLOGI("MetaInfoManager: Cloud data OnChange.");
363     std::vector<DistributedKv::Entry> insertRecords = GetEntriesByKeys(keys[ChangeOp::OP_INSERT]);
364     if (!insertRecords.empty() && insertRecords.size() <= MAX_DB_RECORD_SIZE) {
365         DHLOGI("MetaInfoManager Handle capability data add change");
366         HandleMetaCapabilityAddChange(insertRecords);
367     }
368     std::vector<DistributedKv::Entry> updateRecords = GetEntriesByKeys(keys[ChangeOp::OP_UPDATE]);
369     if (!updateRecords.empty() && updateRecords.size() <= MAX_DB_RECORD_SIZE) {
370         DHLOGI("MetaInfoManager Handle capability data update change");
371         HandleMetaCapabilityUpdateChange(updateRecords);
372     }
373     std::vector<std::string> delKeys = keys[ChangeOp::OP_DELETE];
374     if (!delKeys.empty() && delKeys.size() <= MAX_DB_RECORD_SIZE) {
375         std::vector<DistributedKv::Entry> deleteRecords;
376         for (const auto &key : delKeys) {
377             DistributedKv::Entry entry;
378             DistributedKv::Key kvKey(key);
379             entry.key = kvKey;
380             deleteRecords.emplace_back(entry);
381         }
382         DHLOGI("MetaInfoManager Handle capability data delete change");
383         HandleMetaCapabilityDeleteChange(deleteRecords);
384     }
385 }
386 
HandleMetaCapabilityAddChange(const std::vector<DistributedKv::Entry> & insertRecords)387 void MetaInfoManager::HandleMetaCapabilityAddChange(const std::vector<DistributedKv::Entry> &insertRecords)
388 {
389     std::lock_guard<std::mutex> lock(metaInfoMgrMutex_);
390     for (const auto &item : insertRecords) {
391         const std::string value = item.value.ToString();
392         std::shared_ptr<MetaCapabilityInfo> capPtr;
393         if (GetCapabilityByValue<MetaCapabilityInfo>(value, capPtr) != DH_FWK_SUCCESS) {
394             DHLOGE("Get Meta capability by value failed");
395             continue;
396         }
397         std::string uuid = DHContext::GetInstance().GetUUIDByDeviceId(capPtr->GetDeviceId());
398         if (uuid.empty()) {
399             DHLOGE("Find uuid failed and never enable, deviceId: %{public}s",
400                 GetAnonyString(capPtr->GetDeviceId()).c_str());
401             continue;
402         }
403         std::string networkId = DHContext::GetInstance().GetNetworkIdByUUID(uuid);
404         if (networkId.empty()) {
405             DHLOGE("Find network failed and never enable, uuid: %{public}s", GetAnonyString(uuid).c_str());
406             continue;
407         }
408 
409         const auto keyString = capPtr->GetKey();
410         DHLOGI("Add MetaCapability key: %{public}s", capPtr->GetAnonymousKey().c_str());
411         globalMetaInfoMap_[keyString] = capPtr;
412         TaskParam taskParam = {
413             .networkId = networkId,
414             .uuid = uuid,
415             .dhId = capPtr->GetDHId(),
416             .dhType = capPtr->GetDHType()
417         };
418         auto task = TaskFactory::GetInstance().CreateTask(TaskType::ENABLE, taskParam, nullptr);
419         TaskExecutor::GetInstance().PushTask(task);
420     }
421 }
422 
HandleMetaCapabilityUpdateChange(const std::vector<DistributedKv::Entry> & updateRecords)423 void MetaInfoManager::HandleMetaCapabilityUpdateChange(const std::vector<DistributedKv::Entry> &updateRecords)
424 {
425     std::lock_guard<std::mutex> lock(metaInfoMgrMutex_);
426     for (const auto &item : updateRecords) {
427         const std::string value = item.value.ToString();
428         std::shared_ptr<MetaCapabilityInfo> capPtr;
429         if (GetCapabilityByValue<MetaCapabilityInfo>(value, capPtr) != DH_FWK_SUCCESS) {
430             DHLOGE("Get Meta capability by value failed");
431             continue;
432         }
433         std::string uuid = DHContext::GetInstance().GetUUIDByDeviceId(capPtr->GetDeviceId());
434         if (uuid.empty()) {
435             DHLOGE("Find uuid failed and never enable, deviceId: %{public}s",
436                 GetAnonyString(capPtr->GetDeviceId()).c_str());
437             continue;
438         }
439         std::string networkId = DHContext::GetInstance().GetNetworkIdByUUID(uuid);
440         if (networkId.empty()) {
441             DHLOGE("Find network failed and never enable, uuid: %{public}s", GetAnonyString(uuid).c_str());
442             continue;
443         }
444         std::string enabledDeviceKey = GetCapabilityKey(capPtr->GetDeviceId(), capPtr->GetDHId());
445         if (TaskBoard::GetInstance().IsEnabledDevice(enabledDeviceKey)) {
446             DHLOGI("The deviceKey: %{public}s is enabled.", GetAnonyString(enabledDeviceKey).c_str());
447             continue;
448         }
449         const auto keyString = capPtr->GetKey();
450         DHLOGI("Update MetaCapability key: %{public}s", capPtr->GetAnonymousKey().c_str());
451         globalMetaInfoMap_[keyString] = capPtr;
452         TaskParam taskParam = {
453             .networkId = networkId,
454             .uuid = uuid,
455             .dhId = capPtr->GetDHId(),
456             .dhType = capPtr->GetDHType()
457         };
458         auto task = TaskFactory::GetInstance().CreateTask(TaskType::ENABLE, taskParam, nullptr);
459         TaskExecutor::GetInstance().PushTask(task);
460     }
461 }
462 
HandleMetaCapabilityDeleteChange(const std::vector<DistributedKv::Entry> & deleteRecords)463 void MetaInfoManager::HandleMetaCapabilityDeleteChange(const std::vector<DistributedKv::Entry> &deleteRecords)
464 {
465     std::lock_guard<std::mutex> lock(metaInfoMgrMutex_);
466     for (const auto &item : deleteRecords) {
467         const std::string value = item.value.ToString();
468         std::shared_ptr<MetaCapabilityInfo> capPtr;
469         if (GetCapabilityByValue<MetaCapabilityInfo>(value, capPtr) != DH_FWK_SUCCESS) {
470             DHLOGE("Get Meta capability by value failed");
471             continue;
472         }
473         const auto keyString = capPtr->GetKey();
474         DHLOGI("Delete MetaCapability key: %{public}s", capPtr->GetAnonymousKey().c_str());
475         globalMetaInfoMap_.erase(keyString);
476     }
477 }
478 
GetEntriesByKeys(const std::vector<std::string> & keys)479 std::vector<DistributedKv::Entry> MetaInfoManager::GetEntriesByKeys(const std::vector<std::string> &keys)
480 {
481     if (!IsArrayLengthValid(keys)) {
482         return {};
483     }
484     DHLOGI("call");
485     if (keys.empty()) {
486         DHLOGE("keys empty.");
487         return {};
488     }
489     std::lock_guard<std::mutex> lock(metaInfoMgrMutex_);
490     if (dbAdapterPtr_ == nullptr) {
491         DHLOGE("dbAdapterPtr_ is null");
492         return {};
493     }
494     return dbAdapterPtr_->GetEntriesByKeys(keys);
495 }
496 }
497 }