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