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 "ans_subscriber_stub.h"
17 
18 #include "ans_const_define.h"
19 #include "ans_inner_errors.h"
20 #include "ans_log_wrapper.h"
21 #include "message_option.h"
22 #include "message_parcel.h"
23 #include "parcel.h"
24 
25 namespace OHOS {
26 namespace Notification {
AnsSubscriberStub()27 AnsSubscriberStub::AnsSubscriberStub() {}
28 
~AnsSubscriberStub()29 AnsSubscriberStub::~AnsSubscriberStub() {}
30 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & flags)31 int32_t AnsSubscriberStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
32     MessageOption &flags)
33 {
34     std::u16string descriptor = AnsSubscriberStub::GetDescriptor();
35     std::u16string remoteDescriptor = data.ReadInterfaceToken();
36     if (descriptor != remoteDescriptor) {
37         ANS_LOGW("[OnRemoteRequest] fail: invalid interface token!");
38         return OBJECT_NULL;
39     }
40     ErrCode result = NO_ERROR;
41     switch (code) {
42         case static_cast<uint32_t>(NotificationInterfaceCode::ON_CONNECTED): {
43             result = HandleOnConnected(data, reply);
44             break;
45         }
46         case static_cast<uint32_t>(NotificationInterfaceCode::ON_DISCONNECTED): {
47             result = HandleOnDisconnected(data, reply);
48             break;
49         }
50         case static_cast<uint32_t>(NotificationInterfaceCode::ON_CONSUMED_MAP): {
51             result = HandleOnConsumedMap(data, reply);
52             break;
53         }
54         case static_cast<uint32_t>(NotificationInterfaceCode::ON_CONSUMED_LIST_MAP): {
55             result = HandleOnConsumedListMap(data, reply);
56             break;
57         }
58         case static_cast<uint32_t>(NotificationInterfaceCode::ON_CANCELED_MAP): {
59             result = HandleOnCanceledMap(data, reply);
60             break;
61         }
62         case static_cast<uint32_t>(NotificationInterfaceCode::ON_CANCELED_LIST_MAP): {
63             result = HandleOnCanceledListMap(data, reply);
64             break;
65         }
66         case static_cast<uint32_t>(NotificationInterfaceCode::ON_UPDATED): {
67             result = HandleOnUpdated(data, reply);
68             break;
69         }
70         case static_cast<uint32_t>(NotificationInterfaceCode::ON_DND_DATE_CHANGED): {
71             result = HandleOnDoNotDisturbDateChange(data, reply);
72             break;
73         }
74         case static_cast<uint32_t>(NotificationInterfaceCode::ON_ENABLED_NOTIFICATION_CHANGED): {
75             result = HandleOnEnabledNotificationChanged(data, reply);
76             break;
77         }
78         case static_cast<uint32_t>(NotificationInterfaceCode::ON_BADGE_CHANGED): {
79             result = HandleOnBadgeChanged(data, reply);
80             break;
81         }
82         case static_cast<uint32_t>(NotificationInterfaceCode::ON_BADGE_ENABLED_CHANGED): {
83             result = HandleOnBadgeEnabledChanged(data, reply);
84             break;
85         }
86         default: {
87             ANS_LOGE("[OnRemoteRequest] fail: unknown code!");
88             return IPCObjectStub::OnRemoteRequest(code, data, reply, flags);
89         }
90     }
91     return result;
92 }
93 
HandleOnConnected(MessageParcel & data,MessageParcel & reply)94 ErrCode AnsSubscriberStub::HandleOnConnected(MessageParcel &data, MessageParcel &reply)
95 {
96     OnConnected();
97     return ERR_OK;
98 }
99 
HandleOnDisconnected(MessageParcel & data,MessageParcel & reply)100 ErrCode AnsSubscriberStub::HandleOnDisconnected(MessageParcel &data, MessageParcel &reply)
101 {
102     OnDisconnected();
103     return ERR_OK;
104 }
105 
HandleOnConsumedMap(MessageParcel & data,MessageParcel & reply)106 ErrCode AnsSubscriberStub::HandleOnConsumedMap(MessageParcel &data, MessageParcel &reply)
107 {
108     sptr<Notification> notification = data.ReadParcelable<Notification>();
109     if (!notification) {
110         ANS_LOGW("[HandleOnConsumedMap] fail: notification ReadParcelable failed");
111         return ERR_ANS_PARCELABLE_FAILED;
112     }
113 
114     bool existMap = false;
115     if (!data.ReadBool(existMap)) {
116         ANS_LOGW("[HandleOnConsumedMap] fail: read existMap failed");
117         return ERR_ANS_PARCELABLE_FAILED;
118     }
119 
120     sptr<NotificationSortingMap> notificationMap = nullptr;
121     if (existMap) {
122         notificationMap = data.ReadParcelable<NotificationSortingMap>();
123         if (notificationMap == nullptr) {
124             ANS_LOGW("[HandleOnConsumedMap] fail: read NotificationSortingMap failed");
125             return ERR_ANS_PARCELABLE_FAILED;
126         }
127     }
128 
129     OnConsumed(notification, notificationMap);
130     return ERR_OK;
131 }
132 
HandleOnConsumedListMap(MessageParcel & data,MessageParcel & reply)133 ErrCode AnsSubscriberStub::HandleOnConsumedListMap(MessageParcel &data, MessageParcel &reply)
134 {
135     ANS_LOGD("Start handle notifications in consumed list.");
136 
137     std::vector<sptr<Notification>> notifications;
138     if (!ReadParcelableVector(notifications, data)) {
139         ANS_LOGE("read notifications failed");
140         return ERR_ANS_PARCELABLE_FAILED;
141     }
142 
143     bool existMap = false;
144     if (!data.ReadBool(existMap)) {
145         ANS_LOGE("read existMap failed");
146         return ERR_ANS_PARCELABLE_FAILED;
147     }
148 
149     sptr<NotificationSortingMap> notificationMap = nullptr;
150     if (existMap) {
151         notificationMap = data.ReadParcelable<NotificationSortingMap>();
152         if (notificationMap == nullptr) {
153             ANS_LOGE("read NotificationSortingMap failed");
154             return ERR_ANS_PARCELABLE_FAILED;
155         }
156     }
157 
158     OnConsumedList(notifications, notificationMap);
159     return ERR_OK;
160 }
161 
HandleOnCanceledMap(MessageParcel & data,MessageParcel & reply)162 ErrCode AnsSubscriberStub::HandleOnCanceledMap(MessageParcel &data, MessageParcel &reply)
163 {
164     sptr<Notification> notification = data.ReadParcelable<Notification>();
165     if (!notification) {
166         ANS_LOGW("[HandleOnCanceledMap] fail: notification ReadParcelable failed");
167         return ERR_ANS_PARCELABLE_FAILED;
168     }
169 
170     bool existMap = false;
171     if (!data.ReadBool(existMap)) {
172         ANS_LOGW("[HandleOnCanceledMap] fail: read existMap failed");
173         return ERR_ANS_PARCELABLE_FAILED;
174     }
175 
176     sptr<NotificationSortingMap> notificationMap = nullptr;
177     if (existMap) {
178         notificationMap = data.ReadParcelable<NotificationSortingMap>();
179         if (notificationMap == nullptr) {
180             ANS_LOGW("[HandleOnCanceledMap] fail: read NotificationSortingMap failed");
181             return ERR_ANS_PARCELABLE_FAILED;
182         }
183     }
184 
185     int32_t reason = 0;
186     if (!data.ReadInt32(reason)) {
187         ANS_LOGW("[HandleOnCanceledMap] fail: read reason failed");
188         return ERR_ANS_PARCELABLE_FAILED;
189     }
190 
191     OnCanceled(notification, notificationMap, reason);
192     return ERR_OK;
193 }
194 
195 
HandleOnCanceledListMap(MessageParcel & data,MessageParcel & reply)196 ErrCode AnsSubscriberStub::HandleOnCanceledListMap(MessageParcel &data, MessageParcel &reply)
197 {
198     std::vector<sptr<Notification>> notifications;
199     if (!ReadParcelableVector(notifications, data)) {
200         ANS_LOGE("read notifications failed");
201         return ERR_ANS_PARCELABLE_FAILED;
202     }
203 
204     bool existMap = false;
205     if (!data.ReadBool(existMap)) {
206         ANS_LOGE("read existMap failed");
207         return ERR_ANS_PARCELABLE_FAILED;
208     }
209 
210     sptr<NotificationSortingMap> notificationMap = nullptr;
211     if (existMap) {
212         notificationMap = data.ReadParcelable<NotificationSortingMap>();
213         if (notificationMap == nullptr) {
214             ANS_LOGE("read NotificationSortingMap failed");
215             return ERR_ANS_PARCELABLE_FAILED;
216         }
217     }
218 
219     int32_t reason = 0;
220     if (!data.ReadInt32(reason)) {
221         ANS_LOGE("read reason failed");
222         return ERR_ANS_PARCELABLE_FAILED;
223     }
224 
225     OnCanceledList(notifications, notificationMap, reason);
226     return ERR_OK;
227 }
228 
229 
230 template<typename T>
ReadParcelableVector(std::vector<sptr<T>> & parcelableInfos,MessageParcel & data)231 bool AnsSubscriberStub::ReadParcelableVector(std::vector<sptr<T>> &parcelableInfos, MessageParcel &data)
232 {
233     int32_t infoSize = 0;
234     if (!data.ReadInt32(infoSize)) {
235         ANS_LOGE("read Parcelable size failed.");
236         return false;
237     }
238 
239     parcelableInfos.clear();
240     infoSize = (infoSize < MAX_PARCELABLE_VECTOR_NUM) ? infoSize : MAX_PARCELABLE_VECTOR_NUM;
241     for (int32_t index = 0; index < infoSize; index++) {
242         sptr<T> info = data.ReadStrongParcelable<T>();
243         if (info == nullptr) {
244             ANS_LOGE("read Parcelable infos failed.");
245             return false;
246         }
247         parcelableInfos.emplace_back(info);
248     }
249 
250     return true;
251 }
252 
HandleOnUpdated(MessageParcel & data,MessageParcel & reply)253 ErrCode AnsSubscriberStub::HandleOnUpdated(MessageParcel &data, MessageParcel &reply)
254 {
255     sptr<NotificationSortingMap> notificationMap = data.ReadParcelable<NotificationSortingMap>();
256     if (!notificationMap) {
257         ANS_LOGW("[HandleOnUpdated] fail: notificationMap ReadParcelable failed");
258         return ERR_ANS_PARCELABLE_FAILED;
259     }
260 
261     OnUpdated(notificationMap);
262     return ERR_OK;
263 }
264 
HandleOnDoNotDisturbDateChange(MessageParcel & data,MessageParcel & reply)265 ErrCode AnsSubscriberStub::HandleOnDoNotDisturbDateChange(MessageParcel &data, MessageParcel &reply)
266 {
267     sptr<NotificationDoNotDisturbDate> date = data.ReadParcelable<NotificationDoNotDisturbDate>();
268     if (!date) {
269         ANS_LOGW("[HandleOnDoNotDisturbDateChange] fail: date ReadParcelable failed");
270         return ERR_ANS_PARCELABLE_FAILED;
271     }
272     OnDoNotDisturbDateChange(date);
273     return ERR_OK;
274 }
275 
HandleOnEnabledNotificationChanged(MessageParcel & data,MessageParcel & reply)276 ErrCode AnsSubscriberStub::HandleOnEnabledNotificationChanged(MessageParcel &data, MessageParcel &reply)
277 {
278     sptr<EnabledNotificationCallbackData> callbackData = data.ReadParcelable<EnabledNotificationCallbackData>();
279     if (!callbackData) {
280         ANS_LOGW("[HandleOnEnabledNotificationChanged] fail: callbackData ReadParcelable failed");
281         return ERR_ANS_PARCELABLE_FAILED;
282     }
283     OnEnabledNotificationChanged(callbackData);
284     return ERR_OK;
285 }
286 
HandleOnBadgeChanged(MessageParcel & data,MessageParcel & reply)287 ErrCode AnsSubscriberStub::HandleOnBadgeChanged(MessageParcel &data, MessageParcel &reply)
288 {
289     sptr<BadgeNumberCallbackData> callbackData = data.ReadParcelable<BadgeNumberCallbackData>();
290     if (!callbackData) {
291         ANS_LOGW("[HandleOnBadgeChanged] fail: callbackData ReadParcelable failed");
292         return ERR_ANS_PARCELABLE_FAILED;
293     }
294     OnBadgeChanged(callbackData);
295     return ERR_OK;
296 }
297 
HandleOnBadgeEnabledChanged(MessageParcel & data,MessageParcel & reply)298 ErrCode AnsSubscriberStub::HandleOnBadgeEnabledChanged(MessageParcel &data, MessageParcel &reply)
299 {
300     sptr<EnabledNotificationCallbackData> callbackData = data.ReadParcelable<EnabledNotificationCallbackData>();
301     if (callbackData == nullptr) {
302         ANS_LOGE("Read callback data failed.");
303         return ERR_ANS_PARCELABLE_FAILED;
304     }
305     OnBadgeEnabledChanged(callbackData);
306     return ERR_OK;
307 }
308 
OnConnected()309 void AnsSubscriberStub::OnConnected() {}
310 
OnDisconnected()311 void AnsSubscriberStub::OnDisconnected() {}
312 
OnConsumed(const sptr<Notification> & notification,const sptr<NotificationSortingMap> & notificationMap)313 void AnsSubscriberStub::OnConsumed(
314     const sptr<Notification> &notification, const sptr<NotificationSortingMap> &notificationMap)
315 {}
316 
OnConsumedList(const std::vector<sptr<Notification>> & notifications,const sptr<NotificationSortingMap> & notificationMap)317 void AnsSubscriberStub::OnConsumedList(const std::vector<sptr<Notification>> &notifications,
318     const sptr<NotificationSortingMap> &notificationMap)
319 {}
320 
OnCanceled(const sptr<Notification> & notification,const sptr<NotificationSortingMap> & notificationMap,int32_t deleteReason)321 void AnsSubscriberStub::OnCanceled(
322     const sptr<Notification> &notification, const sptr<NotificationSortingMap> &notificationMap, int32_t deleteReason)
323 {}
324 
OnCanceledList(const std::vector<sptr<Notification>> & notifications,const sptr<NotificationSortingMap> & notificationMap,int32_t deleteReason)325 void AnsSubscriberStub::OnCanceledList(const std::vector<sptr<Notification>> &notifications,
326     const sptr<NotificationSortingMap> &notificationMap, int32_t deleteReason)
327 {}
328 
OnUpdated(const sptr<NotificationSortingMap> & notificationMap)329 void AnsSubscriberStub::OnUpdated(const sptr<NotificationSortingMap> &notificationMap) {}
330 
OnDoNotDisturbDateChange(const sptr<NotificationDoNotDisturbDate> & date)331 void AnsSubscriberStub::OnDoNotDisturbDateChange(const sptr<NotificationDoNotDisturbDate> &date) {}
332 
OnEnabledNotificationChanged(const sptr<EnabledNotificationCallbackData> & callbackData)333 void AnsSubscriberStub::OnEnabledNotificationChanged(const sptr<EnabledNotificationCallbackData> &callbackData) {}
334 
OnBadgeChanged(const sptr<BadgeNumberCallbackData> & badgeData)335 void AnsSubscriberStub::OnBadgeChanged(const sptr<BadgeNumberCallbackData> &badgeData) {}
336 
OnBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> & callbackData)337 void AnsSubscriberStub::OnBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> &callbackData) {}
338 } // namespace Notification
339 } // namespace OHOS
340