1 /*
2 * Copyright (c) 2021-2023 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 "common_event_proxy.h"
17 #include "common_event_constant.h"
18 #include "event_log_wrapper.h"
19 #include "string_ex.h"
20 #include "ces_inner_error_code.h"
21
22 namespace OHOS {
23 namespace EventFwk {
24 using namespace OHOS::AppExecFwk;
25 using namespace OHOS::Notification;
26
CommonEventProxy(const sptr<IRemoteObject> & object)27 CommonEventProxy::CommonEventProxy(const sptr<IRemoteObject> &object) : IRemoteProxy<ICommonEvent>(object)
28 {
29 EVENT_LOGD("CommonEventProxy instance created");
30 }
31
~CommonEventProxy()32 CommonEventProxy::~CommonEventProxy()
33 {
34 EVENT_LOGD("CommonEventProxy instance destroyed");
35 }
36
PublishCommonEvent(const CommonEventData & event,const CommonEventPublishInfo & publishInfo,const sptr<IRemoteObject> & commonEventListener,const int32_t & userId)37 int32_t CommonEventProxy::PublishCommonEvent(const CommonEventData &event, const CommonEventPublishInfo &publishInfo,
38 const sptr<IRemoteObject> &commonEventListener, const int32_t &userId)
39 {
40 EVENT_LOGD("start");
41
42 MessageParcel data;
43 MessageParcel reply;
44
45 if (!data.WriteInterfaceToken(GetDescriptor())) {
46 EVENT_LOGE("Failed to write InterfaceToken");
47 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
48 }
49
50 if (!data.WriteParcelable(&event)) {
51 EVENT_LOGE("Failed to write parcelable event");
52 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
53 }
54
55 if (!data.WriteParcelable(&publishInfo)) {
56 EVENT_LOGE("Failed to write parcelable publishInfo");
57 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
58 }
59
60 if (commonEventListener) {
61 if (!data.WriteBool(true)) {
62 EVENT_LOGE("Failed to write parcelable hasLastSubscrbier");
63 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
64 }
65 if (!data.WriteRemoteObject(commonEventListener)) {
66 EVENT_LOGE("Failed to write parcelable commonEventListener");
67 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
68 }
69 } else {
70 EVENT_LOGW("invalid commonEventListener");
71 if (!data.WriteBool(false)) {
72 EVENT_LOGE("Failed to write parcelable hasLastSubscrbier");
73 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
74 }
75 }
76
77 if (!data.WriteInt32(userId)) {
78 EVENT_LOGE("Failed to write parcelable userId");
79 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
80 }
81
82 bool ret = SendRequest(CommonEventInterfaceCode::CES_PUBLISH_COMMON_EVENT, data, reply);
83 if (!ret) {
84 EVENT_LOGE("Failed to send request");
85 return ERR_NOTIFICATION_SEND_ERROR;
86 }
87
88 EVENT_LOGD("end");
89 return reply.ReadInt32();
90 }
91
PublishCommonEvent(const CommonEventData & event,const CommonEventPublishInfo & publishInfo,const sptr<IRemoteObject> & commonEventListener,const uid_t & uid,const int32_t & callerToken,const int32_t & userId)92 bool CommonEventProxy::PublishCommonEvent(const CommonEventData &event, const CommonEventPublishInfo &publishInfo,
93 const sptr<IRemoteObject> &commonEventListener, const uid_t &uid, const int32_t &callerToken,
94 const int32_t &userId)
95 {
96 EVENT_LOGD("start");
97
98 MessageParcel data;
99 MessageParcel reply;
100
101 if (!data.WriteInterfaceToken(GetDescriptor())) {
102 EVENT_LOGE("Failed to write InterfaceToken");
103 return false;
104 }
105
106 if (!data.WriteParcelable(&event)) {
107 EVENT_LOGE("Failed to write parcelable event");
108 return false;
109 }
110
111 if (!data.WriteParcelable(&publishInfo)) {
112 EVENT_LOGE("Failed to write parcelable publishInfo");
113 return false;
114 }
115
116 if (commonEventListener) {
117 if (!data.WriteBool(true)) {
118 EVENT_LOGE("Failed to write parcelable hasLastSubscriber");
119 return false;
120 }
121 if (!data.WriteRemoteObject(commonEventListener)) {
122 EVENT_LOGE("Failed to write parcelable commonEventListener");
123 return false;
124 }
125 } else {
126 EVENT_LOGW("invalid commonEventListener");
127 if (!data.WriteBool(false)) {
128 EVENT_LOGE("Failed to write parcelable hasLastSubscriber");
129 return false;
130 }
131 }
132
133 if (!data.WriteInt32(uid)) {
134 EVENT_LOGE("Failed to write int uid");
135 return false;
136 }
137
138 if (!data.WriteInt32(callerToken)) {
139 EVENT_LOGE("Failed to write parcelable callerToken");
140 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
141 }
142
143 if (!data.WriteInt32(userId)) {
144 EVENT_LOGE("Failed to write parcelable userId");
145 return false;
146 }
147
148 bool ret = SendRequest(CommonEventInterfaceCode::CES_PUBLISH_COMMON_EVENT2, data, reply);
149 if (ret) {
150 ret = reply.ReadBool();
151 }
152
153 EVENT_LOGD("end");
154 return ret;
155 }
156
SubscribeCommonEvent(const CommonEventSubscribeInfo & subscribeInfo,const sptr<IRemoteObject> & commonEventListener)157 int32_t CommonEventProxy::SubscribeCommonEvent(
158 const CommonEventSubscribeInfo &subscribeInfo, const sptr<IRemoteObject> &commonEventListener)
159 {
160 EVENT_LOGD("start");
161
162 MessageParcel data;
163 MessageParcel reply;
164
165 if (!data.WriteInterfaceToken(GetDescriptor())) {
166 EVENT_LOGE("Failed to write InterfaceToken");
167 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
168 }
169
170 if (!data.WriteParcelable(&subscribeInfo)) {
171 EVENT_LOGE("Failed to write parcelable subscribeInfo");
172 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
173 }
174
175 if (commonEventListener != nullptr) {
176 if (!data.WriteBool(true)) {
177 EVENT_LOGE("Failed to write parcelable hasSubscriber");
178 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
179 }
180 if (!data.WriteRemoteObject(commonEventListener)) {
181 EVENT_LOGE("Failed to write parcelable commonEventListener");
182 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
183 }
184 } else {
185 EVENT_LOGW("invalid commonEventListener");
186 if (!data.WriteBool(false)) {
187 EVENT_LOGE("Failed to write parcelable hasSubscriber");
188 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
189 }
190 }
191
192 bool ret = SendRequest(CommonEventInterfaceCode::CES_SUBSCRIBE_COMMON_EVENT, data, reply);
193 if (!ret) {
194 EVENT_LOGE("Failed to send request");
195 return ERR_NOTIFICATION_SEND_ERROR;
196 }
197
198 EVENT_LOGD("end");
199 return reply.ReadInt32();
200 }
201
UnsubscribeCommonEvent(const sptr<IRemoteObject> & commonEventListener)202 int32_t CommonEventProxy::UnsubscribeCommonEvent(const sptr<IRemoteObject> &commonEventListener)
203 {
204 EVENT_LOGD("start");
205
206 MessageParcel data;
207 MessageParcel reply;
208
209 if (!data.WriteInterfaceToken(GetDescriptor())) {
210 EVENT_LOGE("Failed to write InterfaceToken");
211 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
212 }
213
214 if (commonEventListener != nullptr) {
215 if (!data.WriteBool(true)) {
216 EVENT_LOGE("Failed to write parcelable hasSubscriber");
217 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
218 }
219 if (!data.WriteRemoteObject(commonEventListener)) {
220 EVENT_LOGE("Failed to write parcelable commonEventListener");
221 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
222 }
223 } else {
224 EVENT_LOGW("invalid commonEventListener");
225 if (!data.WriteBool(false)) {
226 EVENT_LOGE("Failed to write parcelable hasSubscriber");
227 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
228 }
229 }
230
231 bool ret = SendRequest(CommonEventInterfaceCode::CES_UNSUBSCRIBE_COMMON_EVENT, data, reply);
232 if (!ret) {
233 EVENT_LOGE("Failed to send request");
234 return ERR_NOTIFICATION_SEND_ERROR;
235 }
236
237 EVENT_LOGD("end");
238 return reply.ReadInt32();
239 }
240
GetStickyCommonEvent(const std::string & event,CommonEventData & eventData)241 bool CommonEventProxy::GetStickyCommonEvent(const std::string &event, CommonEventData &eventData)
242 {
243 EVENT_LOGD("start");
244
245 MessageParcel data;
246 MessageParcel reply;
247
248 if (!data.WriteInterfaceToken(GetDescriptor())) {
249 EVENT_LOGE("Failed to write InterfaceToken");
250 return false;
251 }
252
253 if (!data.WriteString16(Str8ToStr16(event))) {
254 EVENT_LOGE("Failed to write string event");
255 return false;
256 }
257
258 bool ret = SendRequest(CommonEventInterfaceCode::CES_GET_STICKY_COMMON_EVENT, data, reply);
259 if (ret) {
260 ret = reply.ReadBool();
261 if (ret) {
262 std::unique_ptr<CommonEventData> eventDataPtr(reply.ReadParcelable<CommonEventData>());
263 eventData = *eventDataPtr;
264 }
265 }
266
267 EVENT_LOGD("end");
268 return ret;
269 }
270
DumpState(const uint8_t & dumpType,const std::string & event,const int32_t & userId,std::vector<std::string> & state)271 bool CommonEventProxy::DumpState(const uint8_t &dumpType, const std::string &event, const int32_t &userId,
272 std::vector<std::string> &state)
273 {
274 EVENT_LOGD("start");
275
276 MessageParcel data;
277 MessageParcel reply;
278
279 if (!data.WriteInterfaceToken(GetDescriptor())) {
280 EVENT_LOGE("Failed to write InterfaceToken");
281 return false;
282 }
283
284 if (!data.WriteUint8(dumpType)) {
285 EVENT_LOGE("Failed to write parcelable dumpType");
286 return false;
287 }
288
289 if (!data.WriteString16(Str8ToStr16(event))) {
290 EVENT_LOGE("Failed to write string event");
291 return false;
292 }
293
294 if (!data.WriteInt32(userId)) {
295 EVENT_LOGE("Failed to write parcelable userId");
296 return false;
297 }
298
299 bool ret = SendRequest(CommonEventInterfaceCode::CES_DUMP_STATE, data, reply);
300 if (ret) {
301 int32_t stackNum = reply.ReadInt32();
302 stackNum = stackNum > MAX_HISTORY_SIZE ? MAX_HISTORY_SIZE : stackNum;
303 for (int32_t i = 0; i < stackNum; i++) {
304 std::string stack = Str16ToStr8(reply.ReadString16());
305 state.emplace_back(stack);
306 }
307 }
308
309 EVENT_LOGD("end");
310 return ret;
311 }
312
FinishReceiver(const sptr<IRemoteObject> & proxy,const int32_t & code,const std::string & receiverData,const bool & abortEvent)313 bool CommonEventProxy::FinishReceiver(
314 const sptr<IRemoteObject> &proxy, const int32_t &code, const std::string &receiverData, const bool &abortEvent)
315 {
316 EVENT_LOGD("start");
317
318 MessageParcel data;
319 MessageParcel reply;
320
321 if (!data.WriteInterfaceToken(GetDescriptor())) {
322 EVENT_LOGE("Failed to write InterfaceToken");
323 return false;
324 }
325
326 if (proxy != nullptr) {
327 if (!data.WriteBool(true)) {
328 EVENT_LOGE("Failed to write parcelable hasproxy");
329 return false;
330 }
331 if (!data.WriteRemoteObject(proxy)) {
332 EVENT_LOGE("Failed to write parcelable proxy");
333 return false;
334 }
335 } else {
336 EVENT_LOGW("invalid proxy");
337 if (!data.WriteBool(false)) {
338 EVENT_LOGE("Failed to write parcelable hasproxy");
339 return false;
340 }
341 }
342
343 if (!data.WriteInt32(code)) {
344 EVENT_LOGE("Failed to write int code");
345 return false;
346 }
347 if (!data.WriteString16(Str8ToStr16(receiverData))) {
348 EVENT_LOGE("Failed to write string receiverData");
349 return false;
350 }
351 if (!data.WriteBool(abortEvent)) {
352 EVENT_LOGE("Failed to write bool abortEvent");
353 return false;
354 }
355
356 bool ret = SendRequest(CommonEventInterfaceCode::CES_FINISH_RECEIVER, data, reply);
357 if (ret) {
358 ret = reply.ReadBool();
359 }
360
361 EVENT_LOGD("end");
362 return ret;
363 }
364
Freeze(const uid_t & uid)365 bool CommonEventProxy::Freeze(const uid_t &uid)
366 {
367 EVENT_LOGD("start");
368
369 MessageParcel data;
370 MessageParcel reply;
371
372 if (!data.WriteInterfaceToken(GetDescriptor())) {
373 EVENT_LOGE("Failed to write InterfaceToken");
374 return false;
375 }
376
377 if (!data.WriteInt32(uid)) {
378 EVENT_LOGE("Failed to write int uid");
379 return false;
380 }
381
382 bool ret = SendRequest(CommonEventInterfaceCode::CES_FREEZE, data, reply);
383 if (ret) {
384 ret = reply.ReadBool();
385 }
386
387 EVENT_LOGD("end");
388 return ret;
389 }
390
Unfreeze(const uid_t & uid)391 bool CommonEventProxy::Unfreeze(const uid_t &uid)
392 {
393 EVENT_LOGD("start");
394
395 MessageParcel data;
396 MessageParcel reply;
397
398 if (!data.WriteInterfaceToken(GetDescriptor())) {
399 EVENT_LOGE("Failed to write InterfaceToken");
400 return false;
401 }
402
403 if (!data.WriteInt32(uid)) {
404 EVENT_LOGE("Failed to write int uid");
405 return false;
406 }
407
408 bool ret = SendRequest(CommonEventInterfaceCode::CES_UNFREEZE, data, reply);
409 if (ret) {
410 ret = reply.ReadBool();
411 }
412
413 EVENT_LOGD("end");
414 return ret;
415 }
416
UnfreezeAll()417 bool CommonEventProxy::UnfreezeAll()
418 {
419 EVENT_LOGD("start");
420
421 MessageParcel data;
422 MessageParcel reply;
423
424 if (!data.WriteInterfaceToken(GetDescriptor())) {
425 EVENT_LOGE("Failed to write InterfaceToken");
426 return false;
427 }
428
429 bool ret = SendRequest(CommonEventInterfaceCode::CES_UNFREEZE_ALL, data, reply);
430 if (ret) {
431 ret = reply.ReadBool();
432 }
433
434 EVENT_LOGD("end");
435 return ret;
436 }
437
RemoveStickyCommonEvent(const std::string & event)438 int32_t CommonEventProxy::RemoveStickyCommonEvent(const std::string &event)
439 {
440 EVENT_LOGD("start");
441
442 MessageParcel data;
443 if (!data.WriteInterfaceToken(GetDescriptor())) {
444 EVENT_LOGE("Failed to write InterfaceToken");
445 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
446 }
447
448 if (!data.WriteString16(Str8ToStr16(event))) {
449 EVENT_LOGE("Failed to write string event");
450 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
451 }
452
453 MessageParcel reply;
454 bool ret = SendRequest(CommonEventInterfaceCode::CES_REMOVE_STICKY_COMMON_EVENT, data, reply);
455 if (!ret) {
456 return ERR_NOTIFICATION_SEND_ERROR;
457 }
458
459 EVENT_LOGD("end");
460 return reply.ReadInt32();
461 }
462
SetStaticSubscriberState(bool enable)463 int32_t CommonEventProxy::SetStaticSubscriberState(bool enable)
464 {
465 EVENT_LOGD("start");
466
467 MessageParcel data;
468 if (!data.WriteInterfaceToken(GetDescriptor())) {
469 EVENT_LOGE("Failed to write InterfaceToken");
470 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
471 }
472
473 if (!data.WriteBool(enable)) {
474 EVENT_LOGE("Failed to write bool enable");
475 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
476 }
477
478 MessageParcel reply;
479 bool ret = SendRequest(CommonEventInterfaceCode::CES_SET_STATIC_SUBSCRIBER_STATE, data, reply);
480 if (!ret) {
481 return ERR_NOTIFICATION_SEND_ERROR;
482 }
483
484 EVENT_LOGD("end");
485 return reply.ReadInt32();
486 }
487
SendRequest(CommonEventInterfaceCode code,MessageParcel & data,MessageParcel & reply)488 bool CommonEventProxy::SendRequest(CommonEventInterfaceCode code, MessageParcel &data, MessageParcel &reply)
489 {
490 EVENT_LOGD("start");
491
492 sptr<IRemoteObject> remote = Remote();
493 if (remote == nullptr) {
494 EVENT_LOGE("Remote is NULL, %{public}d", code);
495 return false;
496 }
497
498 MessageOption option(MessageOption::TF_SYNC);
499 int32_t result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
500 if (result != OHOS::NO_ERROR) {
501 EVENT_LOGE("Failed to SendRequest %{public}d, error code: %{public}d", code, result);
502 return false;
503 }
504
505 EVENT_LOGD("end");
506 return true;
507 }
508 } // namespace EventFwk
509 } // namespace OHOS