• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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 {
29     interfaces_.emplace(NotificationInterfaceCode::ON_CONNECTED,
30         std::bind(&AnsSubscriberStub::HandleOnConnected, this, std::placeholders::_1, std::placeholders::_2));
31     interfaces_.emplace(NotificationInterfaceCode::ON_DISCONNECTED,
32         std::bind(&AnsSubscriberStub::HandleOnDisconnected, this, std::placeholders::_1, std::placeholders::_2));
33     interfaces_.emplace(NotificationInterfaceCode::ON_CONSUMED_MAP,
34         std::bind(&AnsSubscriberStub::HandleOnConsumedMap, this, std::placeholders::_1, std::placeholders::_2));
35     interfaces_.emplace(NotificationInterfaceCode::ON_CANCELED_MAP,
36         std::bind(&AnsSubscriberStub::HandleOnCanceledMap, this, std::placeholders::_1, std::placeholders::_2));
37     interfaces_.emplace(NotificationInterfaceCode::ON_CANCELED_LIST_MAP,
38         std::bind(&AnsSubscriberStub::HandleOnCanceledListMap, this, std::placeholders::_1, std::placeholders::_2));
39     interfaces_.emplace(NotificationInterfaceCode::ON_UPDATED,
40         std::bind(&AnsSubscriberStub::HandleOnUpdated, this, std::placeholders::_1, std::placeholders::_2));
41     interfaces_.emplace(NotificationInterfaceCode::ON_DND_DATE_CHANGED,
42         std::bind(
43             &AnsSubscriberStub::HandleOnDoNotDisturbDateChange, this, std::placeholders::_1, std::placeholders::_2));
44     interfaces_.emplace(NotificationInterfaceCode::ON_ENABLED_NOTIFICATION_CHANGED,
45         std::bind(&AnsSubscriberStub::HandleOnEnabledNotificationChanged, this, std::placeholders::_1,
46             std::placeholders::_2));
47     interfaces_.emplace(NotificationInterfaceCode::ON_BADGE_CHANGED,
48         std::bind(&AnsSubscriberStub::HandleOnBadgeChanged, this, std::placeholders::_1, std::placeholders::_2));
49 }
50 
~AnsSubscriberStub()51 AnsSubscriberStub::~AnsSubscriberStub()
52 {}
53 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & flags)54 int32_t AnsSubscriberStub::OnRemoteRequest(
55     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &flags)
56 {
57     std::u16string descriptor = AnsSubscriberStub::GetDescriptor();
58     std::u16string remoteDescriptor = data.ReadInterfaceToken();
59     if (descriptor != remoteDescriptor) {
60         ANS_LOGW("[OnRemoteRequest] fail: invalid interface token!");
61         return OBJECT_NULL;
62     }
63 
64     auto it = interfaces_.find(static_cast<NotificationInterfaceCode>(code));
65     if (it == interfaces_.end()) {
66         ANS_LOGW("[OnRemoteRequest] fail: unknown code!");
67         return IPCObjectStub::OnRemoteRequest(code, data, reply, flags);
68     }
69 
70     auto fun = it->second;
71     if (fun == nullptr) {
72         ANS_LOGW("[OnRemoteRequest] fail: not find function!");
73         return IPCObjectStub::OnRemoteRequest(code, data, reply, flags);
74     }
75 
76     fun(data, reply);
77     return NO_ERROR;
78 }
79 
HandleOnConnected(MessageParcel & data,MessageParcel & reply)80 ErrCode AnsSubscriberStub::HandleOnConnected(MessageParcel &data, MessageParcel &reply)
81 {
82     OnConnected();
83     return ERR_OK;
84 }
85 
HandleOnDisconnected(MessageParcel & data,MessageParcel & reply)86 ErrCode AnsSubscriberStub::HandleOnDisconnected(MessageParcel &data, MessageParcel &reply)
87 {
88     OnDisconnected();
89     return ERR_OK;
90 }
91 
HandleOnConsumedMap(MessageParcel & data,MessageParcel & reply)92 ErrCode AnsSubscriberStub::HandleOnConsumedMap(MessageParcel &data, MessageParcel &reply)
93 {
94     sptr<Notification> notification = data.ReadParcelable<Notification>();
95     if (!notification) {
96         ANS_LOGW("[HandleOnConsumedMap] fail: notification ReadParcelable failed");
97         return ERR_ANS_PARCELABLE_FAILED;
98     }
99 
100     bool existMap = false;
101     if (!data.ReadBool(existMap)) {
102         ANS_LOGW("[HandleOnConsumedMap] fail: read existMap failed");
103         return ERR_ANS_PARCELABLE_FAILED;
104     }
105 
106     sptr<NotificationSortingMap> notificationMap = nullptr;
107     if (existMap) {
108         notificationMap = data.ReadParcelable<NotificationSortingMap>();
109         if (notificationMap == nullptr) {
110             ANS_LOGW("[HandleOnConsumedMap] fail: read NotificationSortingMap failed");
111             return ERR_ANS_PARCELABLE_FAILED;
112         }
113     }
114 
115     OnConsumed(notification, notificationMap);
116     return ERR_OK;
117 }
118 
HandleOnCanceledMap(MessageParcel & data,MessageParcel & reply)119 ErrCode AnsSubscriberStub::HandleOnCanceledMap(MessageParcel &data, MessageParcel &reply)
120 {
121     sptr<Notification> notification = data.ReadParcelable<Notification>();
122     if (!notification) {
123         ANS_LOGW("[HandleOnCanceledMap] fail: notification ReadParcelable failed");
124         return ERR_ANS_PARCELABLE_FAILED;
125     }
126 
127     bool existMap = false;
128     if (!data.ReadBool(existMap)) {
129         ANS_LOGW("[HandleOnCanceledMap] fail: read existMap failed");
130         return ERR_ANS_PARCELABLE_FAILED;
131     }
132 
133     sptr<NotificationSortingMap> notificationMap = nullptr;
134     if (existMap) {
135         notificationMap = data.ReadParcelable<NotificationSortingMap>();
136         if (notificationMap == nullptr) {
137             ANS_LOGW("[HandleOnCanceledMap] fail: read NotificationSortingMap failed");
138             return ERR_ANS_PARCELABLE_FAILED;
139         }
140     }
141 
142     int32_t reason = 0;
143     if (!data.ReadInt32(reason)) {
144         ANS_LOGW("[HandleOnCanceledMap] fail: read reason failed");
145         return ERR_ANS_PARCELABLE_FAILED;
146     }
147 
148     OnCanceled(notification, notificationMap, reason);
149     return ERR_OK;
150 }
151 
152 
HandleOnCanceledListMap(MessageParcel & data,MessageParcel & reply)153 ErrCode AnsSubscriberStub::HandleOnCanceledListMap(MessageParcel &data, MessageParcel &reply)
154 {
155     std::vector<sptr<Notification>> notifications;
156     if (!ReadParcelableVector(notifications, data)) {
157         ANS_LOGE("read notifications failed");
158         return ERR_ANS_PARCELABLE_FAILED;
159     }
160 
161     bool existMap = false;
162     if (!data.ReadBool(existMap)) {
163         ANS_LOGE("read existMap failed");
164         return ERR_ANS_PARCELABLE_FAILED;
165     }
166 
167     sptr<NotificationSortingMap> notificationMap = nullptr;
168     if (existMap) {
169         notificationMap = data.ReadParcelable<NotificationSortingMap>();
170         if (notificationMap == nullptr) {
171             ANS_LOGE("read NotificationSortingMap failed");
172             return ERR_ANS_PARCELABLE_FAILED;
173         }
174     }
175 
176     int32_t reason = 0;
177     if (!data.ReadInt32(reason)) {
178         ANS_LOGE("read reason failed");
179         return ERR_ANS_PARCELABLE_FAILED;
180     }
181 
182     OnCanceledList(notifications, notificationMap, reason);
183     return ERR_OK;
184 }
185 
186 
187 template<typename T>
ReadParcelableVector(std::vector<sptr<T>> & parcelableInfos,MessageParcel & data)188 bool AnsSubscriberStub::ReadParcelableVector(std::vector<sptr<T>> &parcelableInfos, MessageParcel &data)
189 {
190     int32_t infoSize = 0;
191     if (!data.ReadInt32(infoSize)) {
192         ANS_LOGE("read Parcelable size failed.");
193         return false;
194     }
195 
196     parcelableInfos.clear();
197     infoSize = (infoSize < MAX_PARCELABLE_VECTOR_NUM) ? infoSize : MAX_PARCELABLE_VECTOR_NUM;
198     for (int32_t index = 0; index < infoSize; index++) {
199         sptr<T> info = data.ReadStrongParcelable<T>();
200         if (info == nullptr) {
201             ANS_LOGE("read Parcelable infos failed.");
202             return false;
203         }
204         parcelableInfos.emplace_back(info);
205     }
206 
207     return true;
208 }
209 
HandleOnUpdated(MessageParcel & data,MessageParcel & reply)210 ErrCode AnsSubscriberStub::HandleOnUpdated(MessageParcel &data, MessageParcel &reply)
211 {
212     sptr<NotificationSortingMap> notificationMap = data.ReadParcelable<NotificationSortingMap>();
213     if (!notificationMap) {
214         ANS_LOGW("[HandleOnUpdated] fail: notificationMap ReadParcelable failed");
215         return ERR_ANS_PARCELABLE_FAILED;
216     }
217 
218     OnUpdated(notificationMap);
219     return ERR_OK;
220 }
221 
HandleOnDoNotDisturbDateChange(MessageParcel & data,MessageParcel & reply)222 ErrCode AnsSubscriberStub::HandleOnDoNotDisturbDateChange(MessageParcel &data, MessageParcel &reply)
223 {
224     sptr<NotificationDoNotDisturbDate> date = data.ReadParcelable<NotificationDoNotDisturbDate>();
225     if (!date) {
226         ANS_LOGW("[HandleOnDoNotDisturbDateChange] fail: date ReadParcelable failed");
227         return ERR_ANS_PARCELABLE_FAILED;
228     }
229     OnDoNotDisturbDateChange(date);
230     return ERR_OK;
231 }
232 
HandleOnEnabledNotificationChanged(MessageParcel & data,MessageParcel & reply)233 ErrCode AnsSubscriberStub::HandleOnEnabledNotificationChanged(MessageParcel &data, MessageParcel &reply)
234 {
235     sptr<EnabledNotificationCallbackData> callbackData = data.ReadParcelable<EnabledNotificationCallbackData>();
236     if (!callbackData) {
237         ANS_LOGW("[HandleOnEnabledNotificationChanged] fail: callbackData ReadParcelable failed");
238         return ERR_ANS_PARCELABLE_FAILED;
239     }
240     OnEnabledNotificationChanged(callbackData);
241     return ERR_OK;
242 }
243 
HandleOnBadgeChanged(MessageParcel & data,MessageParcel & reply)244 ErrCode AnsSubscriberStub::HandleOnBadgeChanged(MessageParcel &data, MessageParcel &reply)
245 {
246     sptr<BadgeNumberCallbackData> callbackData = data.ReadParcelable<BadgeNumberCallbackData>();
247     if (!callbackData) {
248         ANS_LOGW("[HandleOnBadgeChanged] fail: callbackData ReadParcelable failed");
249         return ERR_ANS_PARCELABLE_FAILED;
250     }
251     OnBadgeChanged(callbackData);
252     return ERR_OK;
253 }
254 
OnConnected()255 void AnsSubscriberStub::OnConnected()
256 {}
257 
OnDisconnected()258 void AnsSubscriberStub::OnDisconnected()
259 {}
260 
OnConsumed(const sptr<Notification> & notification,const sptr<NotificationSortingMap> & notificationMap)261 void AnsSubscriberStub::OnConsumed(
262     const sptr<Notification> &notification, const sptr<NotificationSortingMap> &notificationMap)
263 {}
264 
OnCanceled(const sptr<Notification> & notification,const sptr<NotificationSortingMap> & notificationMap,int32_t deleteReason)265 void AnsSubscriberStub::OnCanceled(
266     const sptr<Notification> &notification, const sptr<NotificationSortingMap> &notificationMap, int32_t deleteReason)
267 {}
268 
OnCanceledList(const std::vector<sptr<Notification>> & notifications,const sptr<NotificationSortingMap> & notificationMap,int32_t deleteReason)269 void AnsSubscriberStub::OnCanceledList(const std::vector<sptr<Notification>> &notifications,
270     const sptr<NotificationSortingMap> &notificationMap, int32_t deleteReason)
271 {}
272 
OnUpdated(const sptr<NotificationSortingMap> & notificationMap)273 void AnsSubscriberStub::OnUpdated(const sptr<NotificationSortingMap> &notificationMap)
274 {}
275 
OnDoNotDisturbDateChange(const sptr<NotificationDoNotDisturbDate> & date)276 void AnsSubscriberStub::OnDoNotDisturbDateChange(const sptr<NotificationDoNotDisturbDate> &date)
277 {}
278 
OnEnabledNotificationChanged(const sptr<EnabledNotificationCallbackData> & callbackData)279 void AnsSubscriberStub::OnEnabledNotificationChanged(const sptr<EnabledNotificationCallbackData> &callbackData)
280 {}
281 
OnBadgeChanged(const sptr<BadgeNumberCallbackData> & badgeData)282 void AnsSubscriberStub::OnBadgeChanged(const sptr<BadgeNumberCallbackData> &badgeData)
283 {}
284 }  // namespace Notification
285 }  // namespace OHOS
286