• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2021-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 <memory>
17 
18 #include "notification_config_parse.h"
19 
20 #include "ans_log_wrapper.h"
21 #ifdef ENABLE_ANS_ADDITIONAL_CONTROL
22 #include "notification_extension_wrapper.h"
23 #endif
24 #include "notification_slot.h"
25 #include "file_utils.h"
26 
27 namespace OHOS {
28 namespace Notification {
NotificationConfigParse()29 NotificationConfigParse::NotificationConfigParse()
30 {
31     if (!FileUtils::GetJsonByFilePath(NOTIFICAITON_CONFIG_FILE, notificationConfigJsons_)) {
32         ANS_LOGE("Failed to get notification config file, fileName: %{public}s.", NOTIFICAITON_CONFIG_FILE);
33     }
34     defaultCurrentSlotReminder_ = {
35         {NotificationConstant::SlotType::SOCIAL_COMMUNICATION, 0b111111},
36         {NotificationConstant::SlotType::SERVICE_REMINDER, 0b111111},
37         {NotificationConstant::SlotType::CONTENT_INFORMATION, 0b000000},
38         {NotificationConstant::SlotType::OTHER, 0b000000},
39         {NotificationConstant::SlotType::LIVE_VIEW, 0b111011},
40         {NotificationConstant::SlotType::CUSTOMER_SERVICE, 0b110001},
41         {NotificationConstant::SlotType::EMERGENCY_INFORMATION, 0b111111}
42     };
43 }
44 
GetAppPrivileges(const std::string & bundleName) const45 std::shared_ptr<NotificationAppPrivileges> NotificationConfigParse::GetAppPrivileges(
46     const std::string &bundleName) const
47 {
48     nlohmann::json root;
49     std::string JsonPoint = "/";
50     JsonPoint.append(APP_PRIVILEGES);
51     if (!GetConfigJson(JsonPoint, root)) {
52         ANS_LOGE("Failed to get JsonPoint CCM config file.");
53         return nullptr;
54     }
55     if (!root.contains(APP_PRIVILEGES)) {
56         ANS_LOGE("not found jsonKey appPrivileges");
57         return nullptr;
58     }
59     nlohmann::json affects = root[APP_PRIVILEGES];
60     if (affects.is_null() || affects.empty()) {
61         ANS_LOGE("GetCcmPrivileges failed as invalid ccmPrivileges json.");
62         return nullptr;
63     }
64     for (auto &affect : affects.items()) {
65         if (affect.key() == bundleName) {
66             return std::make_shared<NotificationAppPrivileges>(affect.value());
67         }
68     }
69     return nullptr;
70 }
71 
GetConfigJson(const std::string & keyCheck,nlohmann::json & configJson) const72 bool NotificationConfigParse::GetConfigJson(const std::string &keyCheck, nlohmann::json &configJson) const
73 {
74     if (notificationConfigJsons_.size() <= 0) {
75         ANS_LOGE("Failed to get config json cause empty notificationConfigJsons.");
76         return false;
77     }
78     bool ret = false;
79     std::for_each(notificationConfigJsons_.rbegin(), notificationConfigJsons_.rend(),
80         [&keyCheck, &configJson, &ret](const nlohmann::json &json) {
81         if (keyCheck.find("/") == std::string::npos && json.contains(keyCheck)) {
82             configJson = json;
83             ret = true;
84         }
85 
86         if (keyCheck.find("/") != std::string::npos) {
87             nlohmann::json::json_pointer keyCheckPoint(keyCheck);
88             if (json.contains(keyCheckPoint)) {
89                 configJson = json;
90                 ret = true;
91             }
92         }
93     });
94     if (!ret) {
95         ANS_LOGE("Cannot find keyCheck: %{public}s in notificationConfigJsons.", keyCheck.c_str());
96     }
97     return ret;
98 }
99 
GetCurrentSlotReminder(std::map<NotificationConstant::SlotType,std::shared_ptr<NotificationFlags>> & currentSlotReminder) const100 bool NotificationConfigParse::GetCurrentSlotReminder(
101     std::map<NotificationConstant::SlotType, std::shared_ptr<NotificationFlags>> &currentSlotReminder) const
102 {
103     nlohmann::json root;
104     std::string slotJsonPoint = "/";
105     slotJsonPoint.append(CFG_KEY_NOTIFICATION_SERVICE);
106     slotJsonPoint.append("/");
107     slotJsonPoint.append(CFG_KEY_SLOT_TYPE_REMINDER);
108     if (!GetConfigJson(slotJsonPoint, root)) {
109         return false;
110     }
111 
112     if (root.find(CFG_KEY_NOTIFICATION_SERVICE) == root.end()) {
113         ANS_LOGE("GetCurrentSlotReminder failed as can not find notificationService.");
114         return false;
115     }
116     nlohmann::json currentDeviceRemindJson = root[CFG_KEY_NOTIFICATION_SERVICE][CFG_KEY_SLOT_TYPE_REMINDER];
117     if (currentDeviceRemindJson.is_null() || !currentDeviceRemindJson.is_array() || currentDeviceRemindJson.empty()) {
118         ANS_LOGE("GetCurrentSlotReminder failed as invalid currentDeviceReminder json.");
119         return false;
120     }
121     for (auto &reminderFilterSlot : currentDeviceRemindJson) {
122         NotificationConstant::SlotType slotType;
123         if (reminderFilterSlot.find(CFG_KEY_NAME) == reminderFilterSlot.end() ||
124             reminderFilterSlot[CFG_KEY_NAME].is_null() ||
125             !reminderFilterSlot[CFG_KEY_NAME].is_string() ||
126             !NotificationSlot::GetSlotTypeByString(reminderFilterSlot[CFG_KEY_NAME].get<std::string>(), slotType)) {
127             continue;
128         }
129 
130         std::shared_ptr<NotificationFlags> reminderFlags;
131         if (reminderFilterSlot.find(CFG_KEY_REMINDER_FLAGS) == reminderFilterSlot.end() ||
132             reminderFilterSlot[CFG_KEY_REMINDER_FLAGS].is_null() ||
133             !reminderFilterSlot[CFG_KEY_REMINDER_FLAGS].is_string() ||
134             !NotificationFlags::GetReminderFlagsByString(
135                 reminderFilterSlot[CFG_KEY_REMINDER_FLAGS].get<std::string>(), reminderFlags)) {
136             continue;
137         }
138         currentSlotReminder[slotType] = reminderFlags;
139     }
140     if (currentSlotReminder.size() <= 0) {
141         ANS_LOGE("GetCurrentSlotReminder failed as invalid currentSlotReminder size.");
142         return false;
143     }
144     return true;
145 }
146 
GetConfigSlotReminderModeByType(NotificationConstant::SlotType slotType)147 uint32_t NotificationConfigParse::GetConfigSlotReminderModeByType(NotificationConstant::SlotType slotType)
148 {
149     static std::map<NotificationConstant::SlotType, std::shared_ptr<NotificationFlags>> configSlotsReminder;
150     {
151         std::lock_guard<ffrt::mutex> lock(slotReminderMutex_);
152         if (configSlotsReminder.empty()) {
153             GetCurrentSlotReminder(configSlotsReminder);
154         }
155     }
156     auto iter = configSlotsReminder.find(slotType);
157     if (iter != configSlotsReminder.end()) {
158         return iter->second->GetReminderFlags();
159     }
160 
161     auto defaultIter = defaultCurrentSlotReminder_.find(slotType);
162     if (defaultIter != defaultCurrentSlotReminder_.end()) {
163         return defaultIter->second;
164     }
165 
166     return 0;
167 }
168 
IsLiveViewEnabled(const std::string bundleName) const169 bool NotificationConfigParse::IsLiveViewEnabled(const std::string bundleName) const
170 {
171     std::shared_ptr<NotificationAppPrivileges> appPrivileges = GetAppPrivileges(bundleName);
172     if (appPrivileges == nullptr) {
173         return false;
174     }
175     return appPrivileges->IsLiveViewEnabled();
176 }
177 
IsReminderEnabled(const std::string & bundleName) const178 bool NotificationConfigParse::IsReminderEnabled(const std::string& bundleName) const
179 {
180     std::shared_ptr<NotificationAppPrivileges> appPrivileges = GetAppPrivileges(bundleName);
181     if (appPrivileges == nullptr) {
182         return false;
183     }
184     return appPrivileges->IsReminderEnabled();
185 }
186 
IsDistributedReplyEnabled(const std::string & bundleName) const187 bool NotificationConfigParse::IsDistributedReplyEnabled(const std::string& bundleName) const
188 {
189     std::shared_ptr<NotificationAppPrivileges> appPrivileges = GetAppPrivileges(bundleName);
190     if (appPrivileges == nullptr) {
191         return false;
192     }
193     return appPrivileges->IsDistributedReplyEnabled();
194 }
195 
IsBannerEnabled(const std::string bundleName) const196 bool NotificationConfigParse::IsBannerEnabled(const std::string bundleName) const
197 {
198     std::shared_ptr<NotificationAppPrivileges> appPrivileges = GetAppPrivileges(bundleName);
199     if (appPrivileges != nullptr && appPrivileges->IsBannerEnabled()) {
200         return true;
201     }
202 #ifdef ENABLE_ANS_ADDITIONAL_CONTROL
203     int32_t ctrlResult = EXTENTION_WRAPPER->BannerControl(bundleName);
204     return (ctrlResult == ERR_OK) ? true : false;
205 #else
206     return false;
207 #endif
208 }
209 
GetFlowCtrlConfigFromCCM(FlowControlThreshold & threshold)210 void NotificationConfigParse::GetFlowCtrlConfigFromCCM(FlowControlThreshold &threshold)
211 {
212     nlohmann::json root;
213     std::string JsonPoint = "/";
214     JsonPoint.append(CFG_KEY_NOTIFICATION_SERVICE);
215     if (!GetConfigJson(JsonPoint, root)) {
216         ANS_LOGE("Failed to get JsonPoint CCM config file");
217         return;
218     }
219     if (!root.contains(CFG_KEY_NOTIFICATION_SERVICE)) {
220         ANS_LOGE("GetFlowCtrlConfigFromCCM not found jsonKey");
221         return;
222     }
223     nlohmann::json affects = root[CFG_KEY_NOTIFICATION_SERVICE];
224     if (affects.is_null() || affects.empty()) {
225         ANS_LOGE("GetFlowCtrlConfigFromCCM failed as invalid ccmFlowCtrlConfig json");
226         return;
227     }
228     if (affects.contains(CFG_KEY_MAX_CREATE_NUM_PERSECOND)) {
229         threshold.maxCreateNumPerSecond = affects[CFG_KEY_MAX_CREATE_NUM_PERSECOND];
230     }
231 
232     if (affects.contains(CFG_KEY_MAX_UPDATE_NUM_PERSECOND)) {
233         threshold.maxUpdateNumPerSecond = affects[CFG_KEY_MAX_UPDATE_NUM_PERSECOND];
234     }
235 
236     if (affects.contains(CFG_KEY_MAX_CREATE_NUM_PERSECOND_PERAPP)) {
237         threshold.maxCreateNumPerSecondPerApp = affects[CFG_KEY_MAX_CREATE_NUM_PERSECOND_PERAPP];
238     }
239 
240     if (affects.contains(CFG_KEY_MAX_UPDATE_NUM_PERSECOND_PERAPP)) {
241         threshold.maxUpdateNumPerSecondPerApp = affects[CFG_KEY_MAX_UPDATE_NUM_PERSECOND_PERAPP];
242     }
243 
244     ANS_LOGI("GetFlowCtrlConfigFromCCM success");
245 }
246 
GetSmartReminderEnableList(std::vector<std::string> & deviceTypes)247 bool NotificationConfigParse::GetSmartReminderEnableList(std::vector<std::string>& deviceTypes)
248 {
249     nlohmann::json root;
250     std::string jsonPoint = "/";
251     jsonPoint.append(CFG_KEY_NOTIFICATION_SERVICE);
252     jsonPoint.append("/");
253     jsonPoint.append(CFG_KEY_SMART_REMINDER_ENABLE_LIST);
254     if (!GetConfigJson(jsonPoint, root)) {
255         ANS_LOGE("get configJson fail");
256         return false;
257     }
258 
259     if (root.find(CFG_KEY_NOTIFICATION_SERVICE) == root.end()) {
260         ANS_LOGE("find notificationService fail");
261         return false;
262     }
263 
264     nlohmann::json smartReminderEnableList = root[CFG_KEY_NOTIFICATION_SERVICE][CFG_KEY_SMART_REMINDER_ENABLE_LIST];
265     if (smartReminderEnableList.is_null() || !smartReminderEnableList.is_array() || smartReminderEnableList.empty()) {
266         ANS_LOGE("smartReminderEnableList is invalid");
267         return false;
268     }
269     std::lock_guard<ffrt::mutex> lock(mutex_);
270     deviceTypes = smartReminderEnableList.get<std::vector<std::string>>();
271     return true;
272 }
273 
GetMirrorNotificationEnabledStatus(std::vector<std::string> & deviceTypes)274 bool NotificationConfigParse::GetMirrorNotificationEnabledStatus(std::vector<std::string>& deviceTypes)
275 {
276     nlohmann::json root;
277     std::string jsonPoint = "/";
278     jsonPoint.append(CFG_KEY_NOTIFICATION_SERVICE);
279     jsonPoint.append("/");
280     jsonPoint.append(CFG_KEY_MIRROR_NOTIFICAITON_ENABLED_STATUS);
281     if (!GetConfigJson(jsonPoint, root)) {
282         ANS_LOGE("get configJson fail");
283         return false;
284     }
285 
286     if (root.find(CFG_KEY_NOTIFICATION_SERVICE) == root.end()) {
287         ANS_LOGE("find notificationService fail");
288         return false;
289     }
290 
291     nlohmann::json mirrorNotificationEnabledStatus =
292         root[CFG_KEY_NOTIFICATION_SERVICE][CFG_KEY_MIRROR_NOTIFICAITON_ENABLED_STATUS];
293     if (mirrorNotificationEnabledStatus.is_null() || !mirrorNotificationEnabledStatus.is_array() ||
294         mirrorNotificationEnabledStatus.empty()) {
295         ANS_LOGE("Invalid mirror Status");
296         return false;
297     }
298     std::lock_guard<ffrt::mutex> lock(mutex_);
299     deviceTypes = mirrorNotificationEnabledStatus.get<std::vector<std::string>>();
300     return true;
301 }
302 
GetAppAndDeviceRelationMap(std::map<std::string,std::string> & relationMap)303 bool NotificationConfigParse::GetAppAndDeviceRelationMap(std::map<std::string, std::string>& relationMap)
304 {
305     nlohmann::json root;
306     std::string jsonPoint = "/";
307     jsonPoint.append(CFG_KEY_NOTIFICATION_SERVICE);
308     jsonPoint.append("/");
309     jsonPoint.append(CFG_KEY_APP_AND_DEVICE_RELATION_MAP);
310     if (!GetConfigJson(jsonPoint, root)) {
311         ANS_LOGE("get configJson fail");
312         return false;
313     }
314 
315     if (root.find(CFG_KEY_NOTIFICATION_SERVICE) == root.end()) {
316         ANS_LOGE("find notificationService fail");
317         return false;
318     }
319 
320     nlohmann::json appAndDeviceRelationMap = root[CFG_KEY_NOTIFICATION_SERVICE][CFG_KEY_APP_AND_DEVICE_RELATION_MAP];
321     if (appAndDeviceRelationMap.is_null() || appAndDeviceRelationMap.empty()) {
322         ANS_LOGE("appAndDeviceRelationMap is invalid");
323         return false;
324     }
325     std::lock_guard<ffrt::mutex> lock(mutex_);
326     for (auto& appAndDeviceRelation : appAndDeviceRelationMap.items()) {
327         relationMap[appAndDeviceRelation.key()] = appAndDeviceRelation.value();
328     }
329     return true;
330 }
331 
GetCollaborativeDeleteType() const332 std::unordered_set<std::string> NotificationConfigParse::GetCollaborativeDeleteType() const
333 {
334     nlohmann::json root;
335     std::string JsonPoint = "/";
336     JsonPoint.append(CFG_KEY_NOTIFICATION_SERVICE);
337     JsonPoint.append("/");
338     JsonPoint.append(CFG_KEY_COLLABORATIVE_DELETE_TYPES);
339     if (!GetConfigJson(JsonPoint, root)) {
340         ANS_LOGE("GetConfigJson faild");
341         return std::unordered_set<std::string>();
342     }
343     if (root.find(CFG_KEY_NOTIFICATION_SERVICE) == root.end()) {
344         ANS_LOGE("appPrivileges null");
345         return std::unordered_set<std::string>();
346     }
347 
348     nlohmann::json collaborativeDeleteTypes = root[CFG_KEY_NOTIFICATION_SERVICE][CFG_KEY_COLLABORATIVE_DELETE_TYPES];
349     if (collaborativeDeleteTypes.empty() && !collaborativeDeleteTypes.is_array()) {
350         ANS_LOGE("collaborativeDeleteTypes null or no array");
351         return std::unordered_set<std::string>();
352     }
353     std::unordered_set<std::string> collaborativeDeleteTypeSet;
354     for (const auto &item : collaborativeDeleteTypes) {
355         if (item.is_string()) {
356             collaborativeDeleteTypeSet.insert(item.get<std::string>());
357         }
358     }
359 
360     return collaborativeDeleteTypeSet;
361 }
362 
GetFilterUidAndBundleName(const std::string & key)363 bool NotificationConfigParse::GetFilterUidAndBundleName(const std::string &key)
364 {
365     nlohmann::json root;
366     std::string jsonPoint = "/";
367     jsonPoint.append(CFG_KEY_NOTIFICATION_SERVICE).append("/").append(COLLABORATION_FILTER).append("/").append(key);
368     if (!GetConfigJson(jsonPoint, root)) {
369         ANS_LOGE("Failed to get jsonPoint CCM config file.");
370         return false;
371     }
372 
373     if (!root.contains(CFG_KEY_NOTIFICATION_SERVICE) ||
374         !root[CFG_KEY_NOTIFICATION_SERVICE].contains(COLLABORATION_FILTER)) {
375         ANS_LOGE("Not found jsonKey collaborationFilter.");
376         return false;
377     }
378 
379     nlohmann::json collaborationFilter = root[CFG_KEY_NOTIFICATION_SERVICE][COLLABORATION_FILTER];
380     if (collaborationFilter.is_null() || collaborationFilter.empty()) {
381         ANS_LOGE("GetCollaborationFilter failed as invalid ccmCollaborationFilter json.");
382         return false;
383     }
384     if (collaborationFilter.contains(key) && collaborationFilter[key].is_array()) {
385         for (const auto& item : collaborationFilter[key]) {
386             if (item.is_number_integer()) {
387                 uidList_.push_back(item.get<int32_t>());
388             }
389             if (item.is_string()) {
390                 bundleNameList_.push_back(item.get<std::string>());
391             }
392         }
393         return true;
394     }
395     return false;
396 }
397 
GetCollaborationFilter()398 void NotificationConfigParse::GetCollaborationFilter()
399 {
400     if (!GetFilterUidAndBundleName(COLLABORATION_FILTER_KEY_UID)) {
401         ANS_LOGW("Failed to get filterUid.");
402     }
403     if (!GetFilterUidAndBundleName(COLLABORATION_FILTER_KEY_NAME)) {
404         ANS_LOGW("Failed to get filterBundleName.");
405     }
406 }
407 
IsInCollaborationFilter(const std::string & bundleName,int32_t uid) const408 bool NotificationConfigParse::IsInCollaborationFilter(const std::string& bundleName, int32_t uid) const
409 {
410     if (uidList_.empty() && bundleNameList_.empty()) {
411         ANS_LOGE("empty uidList or bundleNameList");
412         return false;
413     }
414 
415     if (std::find(uidList_.begin(), uidList_.end(), uid) != uidList_.end()) {
416         ANS_LOGI("Uid <%{public}d> in CollaborationFilter.", uid);
417         return true;
418     }
419 
420     if (std::find(bundleNameList_.begin(), bundleNameList_.end(), bundleName) != bundleNameList_.end()) {
421         ANS_LOGI("BundleName <%{public}s> in CollaborationFilter.", bundleName.c_str());
422         return true;
423     }
424 
425     ANS_LOGE("Uid <%{public}d> and BundleName <%{public}s> not in CollaborationFilter.", uid, bundleName.c_str());
426     return false;
427 }
428 
GetStartAbilityTimeout()429 uint32_t NotificationConfigParse::GetStartAbilityTimeout()
430 {
431     nlohmann::json root;
432     std::string JsonPoint = "/";
433     JsonPoint.append(CFG_KEY_NOTIFICATION_SERVICE);
434     if (!GetConfigJson(JsonPoint, root)) {
435         ANS_LOGE("Failed to get JsonPoint CCM config file");
436         return 0;
437     }
438     if (!root.contains(CFG_KEY_NOTIFICATION_SERVICE)) {
439         ANS_LOGE("GetStartAbilityTimeout not found jsonKey");
440         return 0;
441     }
442     nlohmann::json affects = root[CFG_KEY_NOTIFICATION_SERVICE];
443     if (affects.is_null() || affects.empty()) {
444         ANS_LOGE("GetStartAbilityTimeout failed as invalid ccmFlowCtrlConfig json");
445         return 0;
446     }
447     if (affects.contains(CFG_KEY_START_ABILITY_TIMEOUT)) {
448         return affects[CFG_KEY_START_ABILITY_TIMEOUT];
449     }
450 
451     return 0;
452 }
453 
GetReportTrustListConfig()454 void NotificationConfigParse::GetReportTrustListConfig()
455 {
456     nlohmann::json root;
457     std::string reportJsonPoint = "/";
458     reportJsonPoint.append(CFG_KEY_NOTIFICATION_SERVICE);
459     reportJsonPoint.append("/");
460     reportJsonPoint.append(CFG_KEY_DFX_NORMAL_EVENT);
461     if (!GetConfigJson(reportJsonPoint, root)) {
462         return;
463     }
464     if (root.find(CFG_KEY_NOTIFICATION_SERVICE) == root.end()) {
465         ANS_LOGE("Failed to get JsonPoint CCM config file");
466         return;
467     }
468 
469     nlohmann::json reportTrustList = root[CFG_KEY_NOTIFICATION_SERVICE][CFG_KEY_DFX_NORMAL_EVENT];
470     if (reportTrustList.is_null() || reportTrustList.empty() || !reportTrustList.is_array()) {
471         ANS_LOGE("GetReportTrustListConfig failed as invalid dfx_normal_events json.");
472         return;
473     }
474     for (auto &reportTrust : reportTrustList) {
475         reporteTrustSet_.emplace(reportTrust);
476     }
477     return;
478 }
479 
IsReportTrustList(const std::string & bundleName) const480 bool NotificationConfigParse::IsReportTrustList(const std::string& bundleName) const
481 {
482     return reporteTrustSet_.count(bundleName);
483 }
484 
GetCollaborativeDeleteTypeByDevice(std::map<std::string,std::map<std::string,std::unordered_set<std::string>>> & resultMap) const485 bool NotificationConfigParse::GetCollaborativeDeleteTypeByDevice(std::map<std::string,
486     std::map<std::string, std::unordered_set<std::string>>>& resultMap) const
487 {
488     nlohmann::json root;
489     std::string JsonPoint = "/";
490     JsonPoint.append(CFG_KEY_NOTIFICATION_SERVICE);
491     JsonPoint.append("/");
492     JsonPoint.append(CFG_KEY_COLLABORATIVE_DELETE_TYPES_DEVICES);
493     if (!GetConfigJson(JsonPoint, root)) {
494         ANS_LOGE("GetConfigJson faild");
495         return false;
496     }
497 
498     if (root.find(CFG_KEY_NOTIFICATION_SERVICE) == root.end()) {
499         ANS_LOGE("appPrivileges null");
500         return false;
501     }
502 
503     nlohmann::json collaborativeDeleteTypes =
504         root[CFG_KEY_NOTIFICATION_SERVICE][CFG_KEY_COLLABORATIVE_DELETE_TYPES_DEVICES];
505 
506     if (collaborativeDeleteTypes.is_null() || collaborativeDeleteTypes.empty() ||
507         !collaborativeDeleteTypes.is_array()) {
508         ANS_LOGE("get collaborativeDeleteTypes failed.");
509         return false;
510     }
511     std::map<std::string, std::map<std::string, std::unordered_set<std::string>>> tempMap;
512     if (!ParseCollaborativeDeleteTypesDevices(tempMap, collaborativeDeleteTypes)) {
513         return false;
514     }
515     resultMap = tempMap;
516     return true;
517 }
518 
ParseCollaborativeDeleteTypesDevices(std::map<std::string,std::map<std::string,std::unordered_set<std::string>>> & resultMap,nlohmann::json & collaborativeDeleteTypes) const519 bool NotificationConfigParse::ParseCollaborativeDeleteTypesDevices(std::map<std::string,
520     std::map<std::string, std::unordered_set<std::string>>>& resultMap,
521     nlohmann::json& collaborativeDeleteTypes) const
522 {
523     for (const auto& device : collaborativeDeleteTypes) {
524         if (!device.contains(LOCAL_DEVICE_TYPE) || !device[LOCAL_DEVICE_TYPE].is_string()) {
525             continue;
526         }
527         std::string localDeviceType = device[LOCAL_DEVICE_TYPE];
528 
529         if (!device.contains(PEER_DELETE_FILTER_DEVICE) || !device[PEER_DELETE_FILTER_DEVICE].is_array()) {
530             continue;
531         }
532 
533         std::map<std::string, std::unordered_set<std::string>> peerDeviceTypeMap;
534         ParseDeviceSlotType(device, peerDeviceTypeMap);
535         resultMap[localDeviceType] = peerDeviceTypeMap;
536     }
537     return true;
538 }
539 
ParseDeviceSlotType(const nlohmann::json & device,std::map<std::string,std::unordered_set<std::string>> & peerDeviceTypeMap) const540 bool NotificationConfigParse::ParseDeviceSlotType(const nlohmann::json& device,
541     std::map<std::string, std::unordered_set<std::string>>& peerDeviceTypeMap) const
542 {
543     for (const auto& peerDevice : device[PEER_DELETE_FILTER_DEVICE]) {
544         if (!peerDevice.contains(PEER_DEVICE_TYPE) || !peerDevice[PEER_DEVICE_TYPE].is_string()) {
545             continue;
546         }
547         std::string peerDeviceType = peerDevice[PEER_DEVICE_TYPE];
548 
549         if (!peerDevice.contains(DELETE_SLOT_TYPE) || !peerDevice[DELETE_SLOT_TYPE].is_array()) {
550             continue;
551         }
552 
553         std::unordered_set<std::string> deleteSlotTypes;
554         for (const auto& slotType : peerDevice[DELETE_SLOT_TYPE]) {
555             if (slotType.is_string()) {
556                 deleteSlotTypes.insert(slotType);
557             }
558         }
559 
560         peerDeviceTypeMap[peerDeviceType] = std::move(deleteSlotTypes);
561     }
562     return true;
563 }
564 } // namespace Notification
565 } // namespace OHOS
566