• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "subscribe_profile_manager.h"
17 
18 #include "distributed_device_profile_errors.h"
19 #include "dp_radar_helper.h"
20 #include "profile_utils.h"
21 #include "profile_cache.h"
22 
23 namespace OHOS {
24 namespace DistributedDeviceProfile {
25 IMPLEMENT_SINGLE_INSTANCE(SubscribeProfileManager);
26 namespace {
27     const std::string TAG = "SubscribeProfileManager";
28 }
29 
Init()30 int32_t SubscribeProfileManager::Init()
31 {
32     HILOGI("call!");
33     {
34         std::lock_guard<std::mutex> lockGuard(funcsMutex_);
35         funcsMap_[ProfileType::DEVICE_PROFILE * ChangeType::ADD] =
36             &SubscribeProfileManager::NotifyDeviceProfileAdd;
37         funcsMap_[ProfileType::DEVICE_PROFILE * ChangeType::UPDATE] =
38             &SubscribeProfileManager::NotifyDeviceProfileUpdate;
39         funcsMap_[ProfileType::DEVICE_PROFILE * ChangeType::DELETE] =
40             &SubscribeProfileManager::NotifyDeviceProfileDelete;
41         funcsMap_[ProfileType::SERVICE_PROFILE * ChangeType::ADD] =
42             &SubscribeProfileManager::NotifyServiceProfileAdd;
43         funcsMap_[ProfileType::SERVICE_PROFILE * ChangeType::UPDATE] =
44             &SubscribeProfileManager::NotifyServiceProfileUpdate;
45         funcsMap_[ProfileType::SERVICE_PROFILE * ChangeType::DELETE] =
46             &SubscribeProfileManager::NotifyServiceProfileDelete;
47         funcsMap_[ProfileType::CHAR_PROFILE * ChangeType::ADD] =
48             &SubscribeProfileManager::NotifyCharProfileAdd;
49         funcsMap_[ProfileType::CHAR_PROFILE * ChangeType::UPDATE] =
50             &SubscribeProfileManager::NotifyCharProfileUpdate;
51         funcsMap_[ProfileType::CHAR_PROFILE * ChangeType::DELETE] =
52             &SubscribeProfileManager::NotifyCharProfileDelete;
53     }
54     return DP_SUCCESS;
55 }
56 
UnInit()57 int32_t SubscribeProfileManager::UnInit()
58 {
59     HILOGI("call!");
60     {
61         std::lock_guard<std::mutex> lockGuard(subscribeMutex_);
62         subscribeInfoMap_.clear();
63     }
64     {
65         std::lock_guard<std::mutex> lockGuard(funcsMutex_);
66         funcsMap_.clear();
67     }
68     return DP_SUCCESS;
69 }
70 
NotifyProfileChange(ProfileType profileType,ChangeType changeType,const std::string & dbKey,const std::string & dbValue)71 int32_t SubscribeProfileManager::NotifyProfileChange(ProfileType profileType, ChangeType changeType,
72     const std::string& dbKey, const std::string& dbValue)
73 {
74     int32_t code = static_cast<int32_t>(profileType) * static_cast<int32_t>(changeType);
75     DpRadarHelper::GetInstance().ReportNotifyProfileChange(code);
76     switch (code) {
77         case ProfileType::DEVICE_PROFILE * ChangeType::ADD:
78             return SubscribeProfileManager::NotifyDeviceProfileAdd(dbKey, dbValue);
79         case ProfileType::DEVICE_PROFILE * ChangeType::UPDATE:
80             return SubscribeProfileManager::NotifyDeviceProfileUpdate(dbKey, dbValue);
81         case ProfileType::DEVICE_PROFILE * ChangeType::DELETE:
82             return SubscribeProfileManager::NotifyDeviceProfileDelete(dbKey, dbValue);
83         case ProfileType::SERVICE_PROFILE * ChangeType::ADD:
84             return SubscribeProfileManager::NotifyServiceProfileAdd(dbKey, dbValue);
85         case ProfileType::SERVICE_PROFILE * ChangeType::UPDATE:
86             return SubscribeProfileManager::NotifyServiceProfileUpdate(dbKey, dbValue);
87         case ProfileType::SERVICE_PROFILE * ChangeType::DELETE:
88             return SubscribeProfileManager::NotifyServiceProfileDelete(dbKey, dbValue);
89         case ProfileType::CHAR_PROFILE * ChangeType::ADD:
90             return SubscribeProfileManager::NotifyCharProfileAdd(dbKey, dbValue);
91         case ProfileType::CHAR_PROFILE * ChangeType::UPDATE:
92             return SubscribeProfileManager::NotifyCharProfileUpdate(dbKey, dbValue);
93         case ProfileType::CHAR_PROFILE * ChangeType::DELETE:
94             return SubscribeProfileManager::NotifyCharProfileDelete(dbKey, dbValue);
95         default:
96             HILOGE("Params is invalid!, code = %{public}u", code);
97             return DP_INVALID_PARAMS;
98     }
99 }
100 
NotifyTrustDeviceProfileAdd(const TrustDeviceProfile & trustDeviceProfile)101 int32_t SubscribeProfileManager::NotifyTrustDeviceProfileAdd(const TrustDeviceProfile& trustDeviceProfile)
102 {
103     auto subscriberInfos = GetSubscribeInfos(SUBSCRIBE_TRUST_DEVICE_PROFILE);
104     if (subscriberInfos.empty()) {
105         return DP_SUCCESS;
106     }
107     HILOGI("%{public}s!", trustDeviceProfile.dump().c_str());
108     for (const auto& subscriberInfo : subscriberInfos) {
109         sptr<IProfileChangeListener> listenerProxy = iface_cast<IProfileChangeListener>(subscriberInfo.GetListener());
110         if (listenerProxy == nullptr) {
111             HILOGE("Cast to IProfileChangeListener failed!");
112             continue;
113         }
114         if (subscriberInfo.GetProfileChangeTypes().count(ProfileChangeType::TRUST_DEVICE_PROFILE_ADD) != 0) {
115             listenerProxy->OnTrustDeviceProfileAdd(trustDeviceProfile);
116         }
117     }
118     return DP_SUCCESS;
119 }
120 
NotifyTrustDeviceProfileUpdate(const TrustDeviceProfile & oldDeviceProfile,const TrustDeviceProfile & newDeviceProfile)121 int32_t SubscribeProfileManager::NotifyTrustDeviceProfileUpdate(const TrustDeviceProfile& oldDeviceProfile,
122     const TrustDeviceProfile& newDeviceProfile)
123 {
124     auto subscriberInfos = GetSubscribeInfos(SUBSCRIBE_TRUST_DEVICE_PROFILE);
125     if (subscriberInfos.empty()) {
126         return DP_SUCCESS;
127     }
128     HILOGI("%{public}s!", newDeviceProfile.dump().c_str());
129     for (const auto& subscriberInfo : subscriberInfos) {
130         sptr<IProfileChangeListener> listenerProxy = iface_cast<IProfileChangeListener>(subscriberInfo.GetListener());
131         if (listenerProxy == nullptr) {
132             HILOGE("Cast to IProfileChangeListener failed!");
133             continue;
134         }
135         if (subscriberInfo.GetProfileChangeTypes().count(ProfileChangeType::TRUST_DEVICE_PROFILE_UPDATE) != 0) {
136             listenerProxy->OnTrustDeviceProfileUpdate(oldDeviceProfile, newDeviceProfile);
137         }
138     }
139     return DP_SUCCESS;
140 }
141 
NotifyTrustDeviceProfileDelete(const TrustDeviceProfile & trustDeviceProfile)142 int32_t SubscribeProfileManager::NotifyTrustDeviceProfileDelete(const TrustDeviceProfile& trustDeviceProfile)
143 {
144     auto subscriberInfos = GetSubscribeInfos(SUBSCRIBE_TRUST_DEVICE_PROFILE);
145     if (subscriberInfos.empty()) {
146         return DP_SUCCESS;
147     }
148     HILOGI("%{public}s!", trustDeviceProfile.dump().c_str());
149     for (const auto& subscriberInfo : subscriberInfos) {
150         sptr<IProfileChangeListener> listenerProxy = iface_cast<IProfileChangeListener>(subscriberInfo.GetListener());
151         if (listenerProxy == nullptr) {
152             HILOGE("Cast to IProfileChangeListener failed!");
153             continue;
154         }
155         if (subscriberInfo.GetProfileChangeTypes().count(ProfileChangeType::TRUST_DEVICE_PROFILE_DELETE) != 0) {
156             listenerProxy->OnTrustDeviceProfileDelete(trustDeviceProfile);
157         }
158     }
159     return DP_SUCCESS;
160 }
161 
NotifyTrustDeviceProfileActive(const TrustDeviceProfile & trustDeviceProfile)162 int32_t SubscribeProfileManager::NotifyTrustDeviceProfileActive(const TrustDeviceProfile& trustDeviceProfile)
163 {
164     auto subscriberInfos = GetSubscribeInfos(SUBSCRIBE_TRUST_DEVICE_PROFILE);
165     if (subscriberInfos.empty()) {
166         return DP_SUCCESS;
167     }
168     HILOGI("%{public}s!", trustDeviceProfile.dump().c_str());
169     for (const auto& subscriberInfo : subscriberInfos) {
170         sptr<IProfileChangeListener> listenerProxy = iface_cast<IProfileChangeListener>(subscriberInfo.GetListener());
171         if (listenerProxy == nullptr) {
172             HILOGE("Cast to IProfileChangeListener failed!");
173             continue;
174         }
175         if (subscriberInfo.GetProfileChangeTypes().count(ProfileChangeType::TRUST_DEVICE_PROFILE_ACTIVE) != 0) {
176             listenerProxy->OnTrustDeviceProfileActive(trustDeviceProfile);
177         }
178     }
179     return DP_SUCCESS;
180 }
181 
NotifyTrustDeviceProfileInactive(const TrustDeviceProfile & trustDeviceProfile)182 int32_t SubscribeProfileManager::NotifyTrustDeviceProfileInactive(const TrustDeviceProfile& trustDeviceProfile)
183 {
184     auto subscriberInfos = GetSubscribeInfos(SUBSCRIBE_TRUST_DEVICE_PROFILE);
185     if (subscriberInfos.empty()) {
186         return DP_SUCCESS;
187     }
188     HILOGI("%{public}s!", trustDeviceProfile.dump().c_str());
189     for (const auto& subscriberInfo : subscriberInfos) {
190         sptr<IProfileChangeListener> listenerProxy = iface_cast<IProfileChangeListener>(subscriberInfo.GetListener());
191         if (listenerProxy == nullptr) {
192             HILOGE("Cast to IProfileChangeListener failed!");
193             continue;
194         }
195         if (subscriberInfo.GetProfileChangeTypes().count(ProfileChangeType::TRUST_DEVICE_PROFILE_INACTIVE) != 0) {
196             listenerProxy->OnTrustDeviceProfileInactive(trustDeviceProfile);
197         }
198     }
199     return DP_SUCCESS;
200 }
201 
SubscribeDeviceProfile(const SubscribeInfo & subscribeInfo)202 int32_t SubscribeProfileManager::SubscribeDeviceProfile(const SubscribeInfo& subscribeInfo)
203 {
204     HILOGI("saId: %{public}d!, subscribeKey: %{public}s", subscribeInfo.GetSaId(),
205         ProfileUtils::GetDbKeyAnonyString(subscribeInfo.GetSubscribeKey()).c_str());
206     {
207         std::lock_guard<std::mutex> lock(subscribeMutex_);
208         if (subscribeInfoMap_.size() > MAX_LISTENER_SIZE) {
209             HILOGE("SubscribeInfoMap size is invalid!size: %{public}zu!", subscribeInfoMap_.size());
210             return DP_EXCEED_MAX_SIZE_FAIL;
211         }
212         if (subscribeInfoMap_[subscribeInfo.GetSubscribeKey()].size() > MAX_LISTENER_SIZE) {
213             HILOGE("SubscribeInfoMap size is invalid!size: %{public}zu!",
214                 subscribeInfoMap_[subscribeInfo.GetSubscribeKey()].size());
215             return DP_EXCEED_MAX_SIZE_FAIL;
216         }
217         if (subscribeInfoMap_[subscribeInfo.GetSubscribeKey()].find(subscribeInfo) !=
218             subscribeInfoMap_[subscribeInfo.GetSubscribeKey()].end()) {
219             HILOGI("this sa subscribeInfo is exist, saId : %{public}d", subscribeInfo.GetSaId());
220             subscribeInfoMap_[subscribeInfo.GetSubscribeKey()].erase(subscribeInfo);
221         }
222         subscribeInfoMap_[subscribeInfo.GetSubscribeKey()].emplace(subscribeInfo);
223     }
224     return DP_SUCCESS;
225 }
226 
SubscribeDeviceProfile(std::map<std::string,SubscribeInfo> subscribeInfos)227 int32_t SubscribeProfileManager::SubscribeDeviceProfile(std::map<std::string, SubscribeInfo> subscribeInfos)
228 {
229     HILOGD("call!");
230     for (auto item : subscribeInfos) {
231         SubscribeDeviceProfile(item.second);
232     }
233     return DP_SUCCESS;
234 }
235 
UnSubscribeDeviceProfile(const SubscribeInfo & subscribeInfo)236 int32_t SubscribeProfileManager::UnSubscribeDeviceProfile(const SubscribeInfo& subscribeInfo)
237 {
238     HILOGI("saId: %{public}d!, subscribeKey: %{public}s", subscribeInfo.GetSaId(),
239         ProfileUtils::GetDbKeyAnonyString(subscribeInfo.GetSubscribeKey()).c_str());
240     {
241         std::lock_guard<std::mutex> lock(subscribeMutex_);
242         if (subscribeInfoMap_.find(subscribeInfo.GetSubscribeKey()) != subscribeInfoMap_.end()) {
243             subscribeInfoMap_[subscribeInfo.GetSubscribeKey()].erase(subscribeInfo);
244             if (subscribeInfoMap_[subscribeInfo.GetSubscribeKey()].empty()) {
245                 subscribeInfoMap_.erase(subscribeInfo.GetSubscribeKey());
246             }
247         }
248     }
249     return DP_SUCCESS;
250 }
251 
NotifyDeviceProfileAdd(const std::string & dbKey,const std::string & dbValue)252 int32_t SubscribeProfileManager::NotifyDeviceProfileAdd(const std::string& dbKey, const std::string& dbValue)
253 {
254     std::map<std::string, std::string> values;
255     values[dbKey] = dbValue;
256     DeviceProfile deviceProfile;
257     deviceProfile.SetDeviceId(ProfileUtils::GetDeviceIdByDBKey(dbKey));
258     deviceProfile.SetUserId(ProfileUtils::GetUserIdFromDbKey(dbKey));
259     deviceProfile.SetIsMultiUser(deviceProfile.GetUserId() != DEFAULT_USER_ID);
260     ProfileUtils::EntriesToDeviceProfile(values, deviceProfile);
261     auto subscriberInfos = GetSubscribeInfos(DBKeyToSubcribeKey(dbKey));
262     if (subscriberInfos.empty()) {
263         return DP_SUCCESS;
264     }
265     HILOGI("%{public}s!", deviceProfile.AnnoymizeDump().c_str());
266     for (const auto& subscriberInfo : subscriberInfos) {
267         sptr<IProfileChangeListener> listenerProxy = iface_cast<IProfileChangeListener>(subscriberInfo.GetListener());
268         if (listenerProxy == nullptr) {
269             HILOGE("Cast to IProfileChangeListener failed!");
270             continue;
271         }
272         if (subscriberInfo.GetProfileChangeTypes().count(ProfileChangeType::DEVICE_PROFILE_ADD) != 0) {
273             listenerProxy->OnDeviceProfileAdd(deviceProfile);
274         }
275     }
276     return DP_SUCCESS;
277 }
278 
NotifyDeviceProfileUpdate(const std::string & dbKey,const std::string & dbValue)279 int32_t SubscribeProfileManager::NotifyDeviceProfileUpdate(const std::string& dbKey, const std::string& dbValue)
280 {
281     std::map<std::string, std::string> values;
282     values[dbKey] = dbValue;
283     DeviceProfile newDeviceProfile;
284     newDeviceProfile.SetDeviceId(ProfileUtils::GetDeviceIdByDBKey(dbKey));
285     newDeviceProfile.SetUserId(ProfileUtils::GetUserIdFromDbKey(dbKey));
286     newDeviceProfile.SetIsMultiUser(newDeviceProfile.GetUserId() != DEFAULT_USER_ID);
287     ProfileUtils::EntriesToDeviceProfile(values, newDeviceProfile);
288     DeviceProfile oldDeviceProfile;
289     ProfileCache::GetInstance().GetDeviceProfile(ProfileUtils::GetDeviceIdByDBKey(dbKey), oldDeviceProfile);
290     auto subscriberInfos = GetSubscribeInfos(DBKeyToSubcribeKey(dbKey));
291     if (subscriberInfos.empty()) {
292         return DP_SUCCESS;
293     }
294     HILOGI("%{public}s!", newDeviceProfile.AnnoymizeDump().c_str());
295     for (const auto& subscriberInfo : subscriberInfos) {
296         sptr<IProfileChangeListener> listenerProxy = iface_cast<IProfileChangeListener>(subscriberInfo.GetListener());
297         if (listenerProxy == nullptr) {
298             HILOGE("Cast to IProfileChangeListener failed!");
299             continue;
300         }
301         if (subscriberInfo.GetProfileChangeTypes().count(ProfileChangeType::DEVICE_PROFILE_UPDATE) != 0) {
302             listenerProxy->OnDeviceProfileUpdate(oldDeviceProfile, newDeviceProfile);
303         }
304     }
305     return DP_SUCCESS;
306 }
307 
NotifyDeviceProfileDelete(const std::string & dbKey,const std::string & dbValue)308 int32_t SubscribeProfileManager::NotifyDeviceProfileDelete(const std::string& dbKey, const std::string& dbValue)
309 {
310     std::map<std::string, std::string> values;
311     values[dbKey] = dbValue;
312     DeviceProfile deviceProfile;
313     deviceProfile.SetDeviceId(ProfileUtils::GetDeviceIdByDBKey(dbKey));
314     deviceProfile.SetUserId(ProfileUtils::GetUserIdFromDbKey(dbKey));
315     deviceProfile.SetIsMultiUser(deviceProfile.GetUserId() != DEFAULT_USER_ID);
316     ProfileUtils::EntriesToDeviceProfile(values, deviceProfile);
317     auto subscriberInfos = GetSubscribeInfos(DBKeyToSubcribeKey(dbKey));
318     if (subscriberInfos.empty()) {
319         return DP_SUCCESS;
320     }
321     HILOGI("%{public}s!", deviceProfile.AnnoymizeDump().c_str());
322     for (const auto& subscriberInfo : subscriberInfos) {
323         sptr<IProfileChangeListener> listenerProxy = iface_cast<IProfileChangeListener>(subscriberInfo.GetListener());
324         if (listenerProxy == nullptr) {
325             HILOGE("Cast to IProfileChangeListener failed!");
326             continue;
327         }
328         if (subscriberInfo.GetProfileChangeTypes().count(ProfileChangeType::DEVICE_PROFILE_DELETE) != 0) {
329             listenerProxy->OnDeviceProfileDelete(deviceProfile);
330         }
331     }
332     return DP_SUCCESS;
333 }
334 
NotifyServiceProfileAdd(const std::string & dbKey,const std::string & dbValue)335 int32_t SubscribeProfileManager::NotifyServiceProfileAdd(const std::string& dbKey, const std::string& dbValue)
336 {
337     std::map<std::string, std::string> values;
338     values[dbKey] = dbValue;
339     ServiceProfile serviceProfile;
340     serviceProfile.SetDeviceId(ProfileUtils::GetDeviceIdByDBKey(dbKey));
341     serviceProfile.SetServiceName(ProfileUtils::GetNonOhSuffixServiceNameByDBKey(dbKey));
342     serviceProfile.SetUserId(ProfileUtils::GetUserIdFromDbKey(dbKey));
343     serviceProfile.SetIsMultiUser(serviceProfile.GetUserId() != DEFAULT_USER_ID);
344     ProfileUtils::EntriesToServiceProfile(values, serviceProfile);
345     auto subscriberInfos = GetSubscribeInfos(DBKeyToSubcribeKey(dbKey));
346     if (subscriberInfos.empty()) {
347         return DP_SUCCESS;
348     }
349     HILOGI("%{public}s!", serviceProfile.dump().c_str());
350     for (const auto& subscriberInfo : subscriberInfos) {
351         sptr<IProfileChangeListener> listenerProxy = iface_cast<IProfileChangeListener>(subscriberInfo.GetListener());
352         if (listenerProxy == nullptr) {
353             HILOGE("Cast to IProfileChangeListener failed!");
354             continue;
355         }
356         if (subscriberInfo.GetProfileChangeTypes().count(ProfileChangeType::SERVICE_PROFILE_ADD) != 0) {
357             listenerProxy->OnServiceProfileAdd(serviceProfile);
358         }
359     }
360     return DP_SUCCESS;
361 }
362 
NotifyServiceProfileUpdate(const std::string & dbKey,const std::string & dbValue)363 int32_t SubscribeProfileManager::NotifyServiceProfileUpdate(const std::string& dbKey, const std::string& dbValue)
364 {
365     std::map<std::string, std::string> values;
366     values[dbKey] = dbValue;
367     ServiceProfile newServiceProfile;
368     newServiceProfile.SetDeviceId(ProfileUtils::GetDeviceIdByDBKey(dbKey));
369     newServiceProfile.SetServiceName(ProfileUtils::GetNonOhSuffixServiceNameByDBKey(dbKey));
370     newServiceProfile.SetUserId(ProfileUtils::GetUserIdFromDbKey(dbKey));
371     newServiceProfile.SetIsMultiUser(newServiceProfile.GetUserId() != DEFAULT_USER_ID);
372     ProfileUtils::EntriesToServiceProfile(values, newServiceProfile);
373     auto subscriberInfos = GetSubscribeInfos(DBKeyToSubcribeKey(dbKey));
374     if (subscriberInfos.empty()) {
375         return DP_SUCCESS;
376     }
377     HILOGI("%{public}s!", newServiceProfile.dump().c_str());
378     ServiceProfile oldServiceProfile;
379     ProfileCache::GetInstance().GetServiceProfile(ProfileUtils::GetDeviceIdByDBKey(dbKey),
380         ProfileUtils::GetNonOhSuffixServiceNameByDBKey(dbKey), oldServiceProfile);
381     for (const auto& subscriberInfo : subscriberInfos) {
382         sptr<IProfileChangeListener> listenerProxy = iface_cast<IProfileChangeListener>(subscriberInfo.GetListener());
383         if (listenerProxy == nullptr) {
384             HILOGE("Cast to IProfileChangeListener failed!");
385             continue;
386         }
387         if (subscriberInfo.GetProfileChangeTypes().count(ProfileChangeType::SERVICE_PROFILE_UPDATE) != 0) {
388             listenerProxy->OnServiceProfileUpdate(oldServiceProfile, newServiceProfile);
389         }
390     }
391     return DP_SUCCESS;
392 }
393 
NotifyServiceProfileDelete(const std::string & dbKey,const std::string & dbValue)394 int32_t SubscribeProfileManager::NotifyServiceProfileDelete(const std::string& dbKey, const std::string& dbValue)
395 {
396     std::map<std::string, std::string> values;
397     values[dbKey] = dbValue;
398     ServiceProfile serviceProfile;
399     serviceProfile.SetDeviceId(ProfileUtils::GetDeviceIdByDBKey(dbKey));
400     serviceProfile.SetServiceName(ProfileUtils::GetNonOhSuffixServiceNameByDBKey(dbKey));
401     serviceProfile.SetUserId(ProfileUtils::GetUserIdFromDbKey(dbKey));
402     serviceProfile.SetIsMultiUser(serviceProfile.GetUserId() != DEFAULT_USER_ID);
403     ProfileUtils::EntriesToServiceProfile(values, serviceProfile);
404     auto subscriberInfos = GetSubscribeInfos(DBKeyToSubcribeKey(dbKey));
405     if (subscriberInfos.empty()) {
406         return DP_SUCCESS;
407     }
408     HILOGI("%{public}s!", serviceProfile.dump().c_str());
409     for (const auto& subscriberInfo : subscriberInfos) {
410         sptr<IProfileChangeListener> listenerProxy = iface_cast<IProfileChangeListener>(subscriberInfo.GetListener());
411         if (listenerProxy == nullptr) {
412             HILOGE("Cast to IProfileChangeListener failed!");
413             continue;
414         }
415         if (subscriberInfo.GetProfileChangeTypes().count(ProfileChangeType::SERVICE_PROFILE_DELETE) != 0) {
416             listenerProxy->OnServiceProfileDelete(serviceProfile);
417         }
418     }
419     return DP_SUCCESS;
420 }
421 
NotifyCharProfileAdd(const std::string & dbKey,const std::string & dbValue)422 int32_t SubscribeProfileManager::NotifyCharProfileAdd(const std::string& dbKey, const std::string& dbValue)
423 {
424     std::map<std::string, std::string> values;
425     values[dbKey] = dbValue;
426     CharacteristicProfile charProfile;
427     charProfile.SetDeviceId(ProfileUtils::GetDeviceIdByDBKey(dbKey));
428     charProfile.SetServiceName(ProfileUtils::GetNonOhSuffixServiceNameByDBKey(dbKey));
429     charProfile.SetCharacteristicKey(ProfileUtils::GetCharKeyByDBKey(dbKey));
430     if (charProfile.GetCharacteristicKey() != SWITCH_STATUS) {
431         charProfile.SetUserId(ProfileUtils::GetUserIdFromDbKey(dbKey));
432         charProfile.SetIsMultiUser(charProfile.GetUserId() != DEFAULT_USER_ID);
433     }
434     ProfileUtils::EntriesToCharProfile(values, charProfile);
435     auto subscriberInfos = GetSubscribeInfos(DBKeyToSubcribeKey(dbKey));
436     if (subscriberInfos.empty()) {
437         return DP_SUCCESS;
438     }
439     HILOGI("%{public}s!", charProfile.dump().c_str());
440     for (const auto& subscriberInfo : subscriberInfos) {
441         sptr<IProfileChangeListener> listenerProxy = iface_cast<IProfileChangeListener>(subscriberInfo.GetListener());
442         if (listenerProxy == nullptr) {
443             HILOGE("Cast to IProfileChangeListener failed!");
444             continue;
445         }
446         if (subscriberInfo.GetProfileChangeTypes().count(ProfileChangeType::CHAR_PROFILE_ADD) != 0) {
447             listenerProxy->OnCharacteristicProfileAdd(charProfile);
448         }
449     }
450     return DP_SUCCESS;
451 }
452 
NotifyCharProfileUpdate(const std::string & dbKey,const std::string & dbValue)453 int32_t SubscribeProfileManager::NotifyCharProfileUpdate(const std::string& dbKey, const std::string& dbValue)
454 {
455     std::map<std::string, std::string> values;
456     values[dbKey] = dbValue;
457     CharacteristicProfile newCharProfile;
458     newCharProfile.SetDeviceId(ProfileUtils::GetDeviceIdByDBKey(dbKey));
459     newCharProfile.SetServiceName(ProfileUtils::GetNonOhSuffixServiceNameByDBKey(dbKey));
460     newCharProfile.SetCharacteristicKey(ProfileUtils::GetCharKeyByDBKey(dbKey));
461     if (newCharProfile.GetCharacteristicKey() != SWITCH_STATUS) {
462         newCharProfile.SetUserId(ProfileUtils::GetUserIdFromDbKey(dbKey));
463         newCharProfile.SetIsMultiUser(newCharProfile.GetUserId() != DEFAULT_USER_ID);
464     }
465     ProfileUtils::EntriesToCharProfile(values, newCharProfile);
466     auto subscriberInfos = GetSubscribeInfos(DBKeyToSubcribeKey(dbKey));
467     if (subscriberInfos.empty()) {
468         return DP_SUCCESS;
469     }
470     HILOGI("%{public}s!", newCharProfile.dump().c_str());
471     CharacteristicProfile oldCharProfile;
472     ProfileCache::GetInstance().GetCharacteristicProfile(ProfileUtils::GetDeviceIdByDBKey(dbKey),
473         ProfileUtils::GetNonOhSuffixServiceNameByDBKey(dbKey), ProfileUtils::GetCharKeyByDBKey(dbKey), oldCharProfile);
474     for (const auto& subscriberInfo : subscriberInfos) {
475         sptr<IProfileChangeListener> listenerProxy = iface_cast<IProfileChangeListener>(subscriberInfo.GetListener());
476         if (listenerProxy == nullptr) {
477             HILOGE("Cast to IProfileChangeListener failed!");
478             continue;
479         }
480         if (subscriberInfo.GetProfileChangeTypes().count(ProfileChangeType::CHAR_PROFILE_UPDATE) != 0) {
481             listenerProxy->OnCharacteristicProfileUpdate(oldCharProfile, newCharProfile);
482         }
483     }
484     return DP_SUCCESS;
485 }
486 
NotifyCharProfileDelete(const std::string & dbKey,const std::string & dbValue)487 int32_t SubscribeProfileManager::NotifyCharProfileDelete(const std::string& dbKey, const std::string& dbValue)
488 {
489     std::map<std::string, std::string> values;
490     values[dbKey] = dbValue;
491     CharacteristicProfile charProfile;
492     charProfile.SetDeviceId(ProfileUtils::GetDeviceIdByDBKey(dbKey));
493     charProfile.SetServiceName(ProfileUtils::GetNonOhSuffixServiceNameByDBKey(dbKey));
494     charProfile.SetCharacteristicKey(ProfileUtils::GetCharKeyByDBKey(dbKey));
495     if (charProfile.GetCharacteristicKey() != SWITCH_STATUS) {
496         charProfile.SetUserId(ProfileUtils::GetUserIdFromDbKey(dbKey));
497         charProfile.SetIsMultiUser(charProfile.GetUserId() != DEFAULT_USER_ID);
498     }
499     ProfileUtils::EntriesToCharProfile(values, charProfile);
500     auto subscriberInfos = GetSubscribeInfos(DBKeyToSubcribeKey(dbKey));
501     if (subscriberInfos.empty()) {
502         return DP_SUCCESS;
503     }
504     HILOGI("%{public}s!", charProfile.dump().c_str());
505     for (const auto& subscriberInfo : subscriberInfos) {
506         sptr<IProfileChangeListener> listenerProxy = iface_cast<IProfileChangeListener>(subscriberInfo.GetListener());
507         if (listenerProxy == nullptr) {
508             HILOGE("Cast to IProfileChangeListener failed!");
509             continue;
510         }
511         if (subscriberInfo.GetProfileChangeTypes().count(ProfileChangeType::CHAR_PROFILE_DELETE) != 0) {
512             listenerProxy->OnCharacteristicProfileDelete(charProfile);
513         }
514     }
515     return DP_SUCCESS;
516 }
GetSubscribeInfos(const std::string & dbKey)517 std::unordered_set<SubscribeInfo, SubscribeHash, SubscribeCompare> SubscribeProfileManager::GetSubscribeInfos(
518     const std::string& dbKey)
519 {
520     {
521         std::lock_guard<std::mutex> lock(subscribeMutex_);
522         if (subscribeInfoMap_.find(dbKey) == subscribeInfoMap_.end()) {
523             HILOGD("This dbKey is not subscribed, dbKey: %{public}s", ProfileUtils::GetDbKeyAnonyString(dbKey).c_str());
524             return {};
525         }
526         return subscribeInfoMap_[dbKey];
527     }
528 }
529 
DBKeyToSubcribeKey(const std::string & dbkey)530 std::string SubscribeProfileManager::DBKeyToSubcribeKey(const std::string& dbkey)
531 {
532     std::string subscribeKey = dbkey;
533     std::vector<std::string> res;
534     if (ProfileUtils::SplitString(subscribeKey, SEPARATOR, res) != DP_SUCCESS) {
535         return subscribeKey;
536     }
537     if (res.size() > NUM_2) {
538         res[NUM_2] = ProfileUtils::CheckAndRemoveOhSuffix(res[NUM_2]);
539         subscribeKey = ProfileUtils::JoinString(res, SEPARATOR);
540     }
541     return subscribeKey;
542 }
543 } // namespace DistributedDeviceProfile
544 } // namespace OHOS
545