• 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 "subscribe_manager.h"
17 
18 #include "ipc_object_proxy.h"
19 #include "ipc_skeleton.h"
20 
21 #include "device_profile_errors.h"
22 #include "device_profile_log.h"
23 #include "device_profile_storage_manager.h"
24 #include "dp_radar_helper.h"
25 #include "profile_change_handler.h"
26 #include "profile_event_handler_factory.h"
27 #include "subscribe_info_checker.h"
28 #include "subscriber_death_recipient.h"
29 
30 namespace OHOS {
31 namespace DeviceProfile {
32 namespace {
33 const std::string TAG = "SubscribeManager";
34 
35 constexpr int32_t MAX_SUBSCRIBS_PER_UID = 100;
36 }
37 
38 IMPLEMENT_SINGLE_INSTANCE(SubscribeManager);
39 
Init()40 bool SubscribeManager::Init()
41 {
42     subscriberDeathRecipient_ = sptr<IRemoteObject::DeathRecipient>(
43         new (std::nothrow) SubscriberDeathRecipient);
44     if (subscriberDeathRecipient_ == nullptr) {
45         HILOGE("null death recipient");
46         return false;
47     }
48     HILOGI("init succeeded");
49     return true;
50 }
51 
SubscribeProfileEvents(const std::list<SubscribeInfo> & subscribeInfos,const sptr<IRemoteObject> & profileEventNotifier,std::list<ProfileEvent> & failedEvents)52 int32_t SubscribeManager::SubscribeProfileEvents(const std::list<SubscribeInfo>& subscribeInfos,
53     const sptr<IRemoteObject>& profileEventNotifier,
54     std::list<ProfileEvent>& failedEvents)
55 {
56     HILOGI("called");
57     if (!SubscribeInfoChecker::GetInstance().Check(subscribeInfos)) {
58         HILOGE("SubscribeInfo check failed");
59         return ERR_DP_INVALID_PARAMS;
60     }
61 
62     int32_t callingUid = IPCSkeleton::GetCallingUid();
63     HILOGI("uid %{public}d subscribe", callingUid);
64     std::lock_guard<std::mutex> autoLock(handlerLock_);
65     ProfileEvents subProfileEvents;
66     std::shared_ptr<ProfileEventHandler> handler;
67     for (const auto& subscribeInfo : subscribeInfos) {
68         auto profileEvent = subscribeInfo.profileEvent;
69         auto iter = handlersMap_.find(profileEvent);
70         if (iter == handlersMap_.end()) {
71             handler = ProfileEventHandlerFactory::GetInstance().GetHandler(profileEvent);
72             if (handler == nullptr || !handler->Init()) {
73                 failedEvents.emplace_back(profileEvent);
74                 HILOGW("get or init handler for event:%{public}d failed", profileEvent);
75                 continue;
76             }
77             handlersMap_.emplace(profileEvent, handler);
78         } else {
79             handler = iter->second;
80             if (handler == nullptr || !handler->Init()) {
81                 failedEvents.emplace_back(profileEvent);
82                 HILOGW("get or init handler for event:%{public}d failed", profileEvent);
83                 continue;
84             }
85         }
86         if (handler->Subscribe(subscribeInfo, profileEventNotifier) != ERR_OK) {
87             HILOGE("subscribe event:%{public}d failed", profileEvent);
88             failedEvents.emplace_back(profileEvent);
89         } else {
90             subProfileEvents.set(static_cast<uint32_t>(profileEvent));
91         }
92     }
93     if (!subProfileEvents.none()) {
94         TryAddNotifierLocked(profileEventNotifier, subProfileEvents);
95     }
96     struct RadarInfo info = {
97         .stageRes = failedEvents.empty() ?
98             static_cast<int32_t>(StageRes::STAGE_IDLE) : static_cast<int32_t>(StageRes::STAGE_FAIL),
99         .errCode = ERR_DP_SUBSCRIBE_FAILED,
100     };
101     if (!DpRadarHelper::GetInstance().ReportSubscribeData(info)) {
102         HILOGE("ReportSubscribeData failed");
103     }
104     return failedEvents.empty() ? ERR_OK : ERR_DP_SUBSCRIBE_FAILED;
105 }
106 
UnsubscribeProfileEvents(const std::list<ProfileEvent> & profileEvents,const sptr<IRemoteObject> & profileEventNotifier,std::list<ProfileEvent> & failedEvents)107 int32_t SubscribeManager::UnsubscribeProfileEvents(const std::list<ProfileEvent>& profileEvents,
108     const sptr<IRemoteObject>& profileEventNotifier,
109     std::list<ProfileEvent>& failedEvents)
110 {
111     HILOGI("called");
112     std::lock_guard<std::mutex> autoLock(handlerLock_);
113     ProfileEvents unsubProfileEvents;
114     for (auto profileEvent : profileEvents) {
115         auto iter = handlersMap_.find(profileEvent);
116         if (iter == handlersMap_.end()) {
117             HILOGW("not find event:%{public}u", static_cast<uint32_t>(profileEvent));
118             continue;
119         }
120         auto handler = iter->second;
121         if (handler == nullptr || handler->Unsubscribe(profileEventNotifier) != ERR_OK) {
122             HILOGE("unsubscribe event:%{public}d failed", profileEvent);
123             failedEvents.emplace_back(profileEvent);
124         } else {
125             unsubProfileEvents.set(static_cast<uint32_t>(profileEvent));
126         }
127     }
128     if (!unsubProfileEvents.none()) {
129         TryRemoveNotiferLocked(profileEventNotifier, unsubProfileEvents);
130     }
131     struct RadarInfo info = {
132         .funcName = "UnsubscribeProfileEvents",
133         .stageRes = failedEvents.empty() ?
134             static_cast<int32_t>(StageRes::STAGE_SUCC) : static_cast<int32_t>(StageRes::STAGE_FAIL),
135         .toCallPkg = kvNAME,
136         .errCode = ERR_DP_UNSUBSCRIBE_FAILED,
137     };
138     if (!DpRadarHelper::GetInstance().ReportUnsbscribeData(info)) {
139         HILOGE("ReportUnsbscribeData failed");
140     }
141     return failedEvents.empty() ? ERR_OK : ERR_DP_UNSUBSCRIBE_FAILED;
142 }
143 
TryAddNotifierLocked(const sptr<IRemoteObject> & profileEventNotifier,const ProfileEvents & subProfileEvents)144 void SubscribeManager::TryAddNotifierLocked(const sptr<IRemoteObject>& profileEventNotifier,
145     const ProfileEvents& subProfileEvents)
146 {
147     if (profileEventNotifier == nullptr) {
148         HILOGE("profileEventNotifier is nullptr!");
149         return;
150     }
151     auto iter = notifiersMap_.find(profileEventNotifier);
152     if (iter == notifiersMap_.end()) {
153         int32_t callingUid = IPCSkeleton::GetCallingUid();
154         NotifierInfo notifierInfo = { callingUid, subProfileEvents};
155         notifiersMap_.emplace(profileEventNotifier, std::move(notifierInfo));
156         profileEventNotifier->AddDeathRecipient(subscriberDeathRecipient_);
157         HILOGI("notifiers size = %{public}zu", notifiersMap_.size());
158         IncSubsOfUidLocked(callingUid);
159     } else {
160         iter->second.profileEvents |= subProfileEvents;
161     }
162 }
163 
TryRemoveNotiferLocked(const sptr<IRemoteObject> & profileEventNotifier,const ProfileEvents & unsubProfileEvents)164 void SubscribeManager::TryRemoveNotiferLocked(const sptr<IRemoteObject>& profileEventNotifier,
165     const ProfileEvents& unsubProfileEvents)
166 {
167     if (profileEventNotifier == nullptr) {
168         HILOGE("profileEventNotifier is nullptr!");
169         return;
170     }
171     auto iter = notifiersMap_.find(profileEventNotifier);
172     if (iter == notifiersMap_.end()) {
173         return;
174     }
175     auto& subProfileEvents = iter->second.profileEvents;
176     subProfileEvents &= ~unsubProfileEvents;
177     if (subProfileEvents.none()) {
178         int32_t callingUid = iter->second.callingUid;
179         profileEventNotifier->RemoveDeathRecipient(subscriberDeathRecipient_);
180         notifiersMap_.erase(iter);
181         HILOGI("notifiers size = %{public}zu", notifiersMap_.size());
182         DecSubsOfUidLocked(callingUid);
183         return;
184     }
185 }
186 
IncSubsOfUidLocked(int32_t uid)187 void SubscribeManager::IncSubsOfUidLocked(int32_t uid)
188 {
189     if (uidSubsMap_.find(uid) != uidSubsMap_.end()) {
190        ++uidSubsMap_[uid];
191     } else {
192         uidSubsMap_[uid] = 1;
193     }
194     HILOGI("uid %{public}d has %{public}u subscription(s)", uid, uidSubsMap_[uid]);
195 }
196 
DecSubsOfUidLocked(int32_t uid)197 void SubscribeManager::DecSubsOfUidLocked(int32_t uid)
198 {
199     auto iter = uidSubsMap_.find(uid);
200     if (iter != uidSubsMap_.end()) {
201         auto& numSubs = iter->second;
202         if (--numSubs == 0) {
203             uidSubsMap_.erase(iter);
204         }
205         HILOGI("uid %{public}d subscription(s) is %{public}u", uid, numSubs);
206     }
207 }
208 
CheckSubsOfUid(int32_t uid)209 bool SubscribeManager::CheckSubsOfUid(int32_t uid)
210 {
211     std::lock_guard<std::mutex> autoLock(handlerLock_);
212     auto iter = uidSubsMap_.find(uid);
213     if (iter == uidSubsMap_.end()) {
214         return true;
215     }
216     if (iter->second >= MAX_SUBSCRIBS_PER_UID) {
217         return false;
218     }
219     return true;
220 }
221 
OnSubscriberDied(const sptr<IRemoteObject> & profileEventNotifier)222 void SubscribeManager::OnSubscriberDied(const sptr<IRemoteObject>& profileEventNotifier)
223 {
224     HILOGI("called");
225     DeviceProfileStorageManager::GetInstance().NotifySubscriberDied(profileEventNotifier);
226     std::lock_guard<std::mutex> autoLock(handlerLock_);
227     auto iter = notifiersMap_.find(profileEventNotifier);
228     if (iter != notifiersMap_.end()) {
229         DecSubsOfUidLocked(iter->second.callingUid);
230         notifiersMap_.erase(iter);
231     }
232     for (const auto& [_, handler] : handlersMap_) {
233         handler->OnSubscriberDied(profileEventNotifier);
234     }
235 }
236 } // namespace DeviceProfile
237 } // namespace OHOS
238