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 "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 namespace {
27 constexpr int32_t VECTOR_MAX_SIZE = 1000;
28 }
CommonEventProxy(const sptr<IRemoteObject> & object)29 CommonEventProxy::CommonEventProxy(const sptr<IRemoteObject> &object) : IRemoteProxy<ICommonEvent>(object)
30 {
31 EVENT_LOGD("CommonEventProxy instance created");
32 }
33
~CommonEventProxy()34 CommonEventProxy::~CommonEventProxy()
35 {
36 EVENT_LOGD("CommonEventProxy instance destroyed");
37 }
38
PublishCommonEvent(const CommonEventData & event,const CommonEventPublishInfo & publishInfo,const sptr<IRemoteObject> & commonEventListener,const int32_t & userId)39 int32_t CommonEventProxy::PublishCommonEvent(const CommonEventData &event, const CommonEventPublishInfo &publishInfo,
40 const sptr<IRemoteObject> &commonEventListener, const int32_t &userId)
41 {
42 EVENT_LOGD("start");
43
44 MessageParcel data;
45 MessageParcel reply;
46
47 if (!data.WriteInterfaceToken(GetDescriptor())) {
48 EVENT_LOGE("Failed to write InterfaceToken");
49 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
50 }
51
52 if (!data.WriteParcelable(&event)) {
53 EVENT_LOGE("Failed to write parcelable event");
54 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
55 }
56
57 if (!data.WriteParcelable(&publishInfo)) {
58 EVENT_LOGE("Failed to write parcelable publishInfo");
59 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
60 }
61
62 if (commonEventListener) {
63 if (!data.WriteBool(true)) {
64 EVENT_LOGE("Failed to write parcelable hasLastSubscrbier");
65 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
66 }
67 if (!data.WriteRemoteObject(commonEventListener)) {
68 EVENT_LOGE("Failed to write parcelable commonEventListener");
69 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
70 }
71 } else {
72 if (!data.WriteBool(false)) {
73 EVENT_LOGE("Failed to write parcelable hasLastSubscrbier");
74 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
75 }
76 }
77
78 if (!data.WriteInt32(userId)) {
79 EVENT_LOGE("Failed to write parcelable userId");
80 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
81 }
82
83 bool ret = SendRequest(CommonEventInterfaceCode::CES_PUBLISH_COMMON_EVENT, data, reply);
84 if (!ret) {
85 EVENT_LOGE("Failed to send request");
86 return ERR_NOTIFICATION_SEND_ERROR;
87 }
88
89 EVENT_LOGD("end");
90 return reply.ReadInt32();
91 }
92
PublishCommonEvent(const CommonEventData & event,const CommonEventPublishInfo & publishInfo,const sptr<IRemoteObject> & commonEventListener,const uid_t & uid,const int32_t & callerToken,const int32_t & userId)93 bool CommonEventProxy::PublishCommonEvent(const CommonEventData &event, const CommonEventPublishInfo &publishInfo,
94 const sptr<IRemoteObject> &commonEventListener, const uid_t &uid, const int32_t &callerToken,
95 const int32_t &userId)
96 {
97 EVENT_LOGD("start");
98
99 MessageParcel data;
100 MessageParcel reply;
101
102 if (!data.WriteInterfaceToken(GetDescriptor())) {
103 EVENT_LOGE("Failed to write InterfaceToken");
104 return false;
105 }
106
107 if (!data.WriteParcelable(&event)) {
108 EVENT_LOGE("Failed to write parcelable event");
109 return false;
110 }
111
112 if (!data.WriteParcelable(&publishInfo)) {
113 EVENT_LOGE("Failed to write parcelable publishInfo");
114 return false;
115 }
116
117 if (commonEventListener) {
118 if (!data.WriteBool(true)) {
119 EVENT_LOGE("Failed to write parcelable hasLastSubscriber");
120 return false;
121 }
122 if (!data.WriteRemoteObject(commonEventListener)) {
123 EVENT_LOGE("Failed to write parcelable commonEventListener");
124 return false;
125 }
126 } else {
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 false;
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,const int32_t instanceKey)157 int32_t CommonEventProxy::SubscribeCommonEvent(const CommonEventSubscribeInfo &subscribeInfo,
158 const sptr<IRemoteObject> &commonEventListener, const int32_t instanceKey)
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("error 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("error to write parcelable hasSubscriber");
178 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
179 }
180 if (!data.WriteRemoteObject(commonEventListener)) {
181 EVENT_LOGE("error 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("error to write parcelable hasSubscriber");
188 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
189 }
190 }
191
192 if (!data.WriteInt32(instanceKey)) {
193 EVENT_LOGE("Failed to write parcelable instanceKey");
194 return false;
195 }
196
197 bool ret = SendRequest(CommonEventInterfaceCode::CES_SUBSCRIBE_COMMON_EVENT, data, reply);
198 if (!ret) {
199 EVENT_LOGE("Failed to send request");
200 return ERR_NOTIFICATION_SEND_ERROR;
201 }
202
203 EVENT_LOGD("end");
204 return reply.ReadInt32();
205 }
206
UnsubscribeCommonEvent(const sptr<IRemoteObject> & commonEventListener)207 int32_t CommonEventProxy::UnsubscribeCommonEvent(const sptr<IRemoteObject> &commonEventListener)
208 {
209 EVENT_LOGD("start");
210
211 MessageParcel data;
212 MessageParcel reply;
213
214 if (!data.WriteInterfaceToken(GetDescriptor())) {
215 EVENT_LOGE("Failed to write InterfaceToken");
216 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
217 }
218
219 if (commonEventListener != nullptr) {
220 if (!data.WriteBool(true)) {
221 EVENT_LOGE("Failed to write parcelable hasSubscriber");
222 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
223 }
224 if (!data.WriteRemoteObject(commonEventListener)) {
225 EVENT_LOGE("Failed to write parcelable commonEventListener");
226 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
227 }
228 } else {
229 EVENT_LOGW("invalid commonEventListener");
230 if (!data.WriteBool(false)) {
231 EVENT_LOGE("Failed to write parcelable hasSubscriber");
232 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
233 }
234 }
235
236 bool ret = SendRequest(CommonEventInterfaceCode::CES_UNSUBSCRIBE_COMMON_EVENT, data, reply);
237 if (!ret) {
238 EVENT_LOGE("Failed to send request");
239 return ERR_NOTIFICATION_SEND_ERROR;
240 }
241
242 EVENT_LOGD("end");
243 return reply.ReadInt32();
244 }
245
UnsubscribeCommonEventSync(const sptr<IRemoteObject> & commonEventListener)246 int32_t CommonEventProxy::UnsubscribeCommonEventSync(const sptr<IRemoteObject> &commonEventListener)
247 {
248 EVENT_LOGD("start");
249
250 MessageParcel data;
251 MessageParcel reply;
252
253 if (!data.WriteInterfaceToken(GetDescriptor())) {
254 EVENT_LOGE("Failed to write InterfaceToken");
255 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
256 }
257
258 if (commonEventListener != nullptr) {
259 if (!data.WriteBool(true)) {
260 EVENT_LOGE("Failed to write parcelable hasSubscriber");
261 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
262 }
263 if (!data.WriteRemoteObject(commonEventListener)) {
264 EVENT_LOGE("Failed to write parcelable commonEventListener");
265 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
266 }
267 } else {
268 EVENT_LOGW("invalid commonEventListener");
269 if (!data.WriteBool(false)) {
270 EVENT_LOGE("Failed to write parcelable hasSubscriber");
271 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
272 }
273 }
274
275 bool ret = SendRequest(CommonEventInterfaceCode::CES_UNSUBSCRIBE_COMMON_EVENT_SYNC, data, reply);
276 if (!ret) {
277 EVENT_LOGE("Failed to send request");
278 return ERR_NOTIFICATION_SEND_ERROR;
279 }
280
281 EVENT_LOGD("end");
282 return reply.ReadInt32();
283 }
284
GetStickyCommonEvent(const std::string & event,CommonEventData & eventData)285 bool CommonEventProxy::GetStickyCommonEvent(const std::string &event, CommonEventData &eventData)
286 {
287 EVENT_LOGD("start");
288
289 MessageParcel data;
290 MessageParcel reply;
291
292 if (!data.WriteInterfaceToken(GetDescriptor())) {
293 EVENT_LOGE("Failed to write InterfaceToken");
294 return false;
295 }
296
297 if (!data.WriteString16(Str8ToStr16(event))) {
298 EVENT_LOGE("Failed to write string event");
299 return false;
300 }
301
302 bool ret = SendRequest(CommonEventInterfaceCode::CES_GET_STICKY_COMMON_EVENT, data, reply);
303 if (ret) {
304 ret = reply.ReadBool();
305 if (ret) {
306 std::unique_ptr<CommonEventData> eventDataPtr(reply.ReadParcelable<CommonEventData>());
307 eventData = *eventDataPtr;
308 }
309 }
310
311 EVENT_LOGD("end");
312 return ret;
313 }
314
DumpState(const uint8_t & dumpType,const std::string & event,const int32_t & userId,std::vector<std::string> & state)315 bool CommonEventProxy::DumpState(const uint8_t &dumpType, const std::string &event, const int32_t &userId,
316 std::vector<std::string> &state)
317 {
318 EVENT_LOGD("start");
319
320 MessageParcel data;
321 MessageParcel reply;
322
323 if (!data.WriteInterfaceToken(GetDescriptor())) {
324 EVENT_LOGE("Failed to write InterfaceToken");
325 return false;
326 }
327
328 if (!data.WriteUint8(dumpType)) {
329 EVENT_LOGE("Failed to write parcelable dumpType");
330 return false;
331 }
332
333 if (!data.WriteString16(Str8ToStr16(event))) {
334 EVENT_LOGE("Failed to write string event");
335 return false;
336 }
337
338 if (!data.WriteInt32(userId)) {
339 EVENT_LOGE("Failed to write parcelable userId");
340 return false;
341 }
342
343 bool ret = SendRequest(CommonEventInterfaceCode::CES_DUMP_STATE, data, reply);
344 if (ret) {
345 int32_t stackNum = reply.ReadInt32();
346 stackNum = stackNum > MAX_HISTORY_SIZE ? MAX_HISTORY_SIZE : stackNum;
347 for (int32_t i = 0; i < stackNum; i++) {
348 std::string stack = Str16ToStr8(reply.ReadString16());
349 state.emplace_back(stack);
350 }
351 }
352
353 EVENT_LOGD("end");
354 return ret;
355 }
356
FinishReceiver(const sptr<IRemoteObject> & proxy,const int32_t & code,const std::string & receiverData,const bool & abortEvent)357 bool CommonEventProxy::FinishReceiver(
358 const sptr<IRemoteObject> &proxy, const int32_t &code, const std::string &receiverData, const bool &abortEvent)
359 {
360 EVENT_LOGD("start");
361
362 MessageParcel data;
363 MessageParcel reply;
364
365 if (!data.WriteInterfaceToken(GetDescriptor())) {
366 EVENT_LOGE("Failed to write InterfaceToken");
367 return false;
368 }
369
370 if (proxy != nullptr) {
371 if (!data.WriteBool(true)) {
372 EVENT_LOGE("Failed to write parcelable hasproxy");
373 return false;
374 }
375 if (!data.WriteRemoteObject(proxy)) {
376 EVENT_LOGE("Failed to write parcelable proxy");
377 return false;
378 }
379 } else {
380 EVENT_LOGW("invalid proxy");
381 if (!data.WriteBool(false)) {
382 EVENT_LOGE("Failed to write parcelable hasproxy");
383 return false;
384 }
385 }
386
387 if (!data.WriteInt32(code)) {
388 EVENT_LOGE("Failed to write int code");
389 return false;
390 }
391 if (!data.WriteString16(Str8ToStr16(receiverData))) {
392 EVENT_LOGE("Failed to write string receiverData");
393 return false;
394 }
395 if (!data.WriteBool(abortEvent)) {
396 EVENT_LOGE("Failed to write bool abortEvent");
397 return false;
398 }
399
400 bool ret = SendRequest(CommonEventInterfaceCode::CES_FINISH_RECEIVER, data, reply);
401 if (ret) {
402 ret = reply.ReadBool();
403 }
404
405 EVENT_LOGD("end");
406 return ret;
407 }
408
Freeze(const uid_t & uid)409 bool CommonEventProxy::Freeze(const uid_t &uid)
410 {
411 EVENT_LOGD("Freeze start");
412
413 MessageParcel data;
414 MessageParcel reply;
415
416 if (!data.WriteInterfaceToken(GetDescriptor())) {
417 EVENT_LOGE("Error to write InterfaceToken");
418 return false;
419 }
420
421 if (!data.WriteInt32(uid)) {
422 EVENT_LOGE("Error to write int uid");
423 return false;
424 }
425
426 bool ret = SendRequest(CommonEventInterfaceCode::CES_FREEZE, data, reply);
427 if (ret) {
428 ret = reply.ReadBool();
429 }
430
431 EVENT_LOGD("end");
432 return ret;
433 }
434
Unfreeze(const uid_t & uid)435 bool CommonEventProxy::Unfreeze(const uid_t &uid)
436 {
437 EVENT_LOGD("start");
438
439 MessageParcel data;
440 MessageParcel reply;
441
442 if (!data.WriteInterfaceToken(GetDescriptor())) {
443 EVENT_LOGE("Failed to write InterfaceToken");
444 return false;
445 }
446
447 if (!data.WriteInt32(uid)) {
448 EVENT_LOGE("Failed to write int uid");
449 return false;
450 }
451
452 bool ret = SendRequest(CommonEventInterfaceCode::CES_UNFREEZE, data, reply);
453 if (ret) {
454 ret = reply.ReadBool();
455 }
456
457 EVENT_LOGD("end");
458 return ret;
459 }
460
UnfreezeAll()461 bool CommonEventProxy::UnfreezeAll()
462 {
463 EVENT_LOGD("start");
464
465 MessageParcel data;
466 MessageParcel reply;
467
468 if (!data.WriteInterfaceToken(GetDescriptor())) {
469 EVENT_LOGE("Failed to write InterfaceToken");
470 return false;
471 }
472
473 bool ret = SendRequest(CommonEventInterfaceCode::CES_UNFREEZE_ALL, data, reply);
474 if (ret) {
475 ret = reply.ReadBool();
476 }
477
478 EVENT_LOGD("end");
479 return ret;
480 }
481
RemoveStickyCommonEvent(const std::string & event)482 int32_t CommonEventProxy::RemoveStickyCommonEvent(const std::string &event)
483 {
484 EVENT_LOGD("start");
485
486 MessageParcel data;
487 if (!data.WriteInterfaceToken(GetDescriptor())) {
488 EVENT_LOGE("Failed to write InterfaceToken");
489 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
490 }
491
492 if (!data.WriteString16(Str8ToStr16(event))) {
493 EVENT_LOGE("Failed to write string event");
494 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
495 }
496
497 MessageParcel reply;
498 bool ret = SendRequest(CommonEventInterfaceCode::CES_REMOVE_STICKY_COMMON_EVENT, data, reply);
499 if (!ret) {
500 return ERR_NOTIFICATION_SEND_ERROR;
501 }
502
503 EVENT_LOGD("end");
504 return reply.ReadInt32();
505 }
506
SetStaticSubscriberState(bool enable)507 int32_t CommonEventProxy::SetStaticSubscriberState(bool enable)
508 {
509 EVENT_LOGD("start");
510
511 MessageParcel data;
512 if (!data.WriteInterfaceToken(GetDescriptor())) {
513 EVENT_LOGE("Failed to write InterfaceToken");
514 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
515 }
516
517 if (!data.WriteBool(enable)) {
518 EVENT_LOGE("Failed to write bool enable");
519 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
520 }
521
522 MessageParcel reply;
523 bool ret = SendRequest(CommonEventInterfaceCode::CES_SET_STATIC_SUBSCRIBER_STATE, data, reply);
524 if (!ret) {
525 return ERR_NOTIFICATION_SEND_ERROR;
526 }
527
528 EVENT_LOGD("end");
529 return reply.ReadInt32();
530 }
531
SetStaticSubscriberState(const std::vector<std::string> & events,bool enable)532 int32_t CommonEventProxy::SetStaticSubscriberState(const std::vector<std::string> &events, bool enable)
533 {
534 EVENT_LOGD("Called.");
535 MessageParcel data;
536 if (!data.WriteInterfaceToken(GetDescriptor())) {
537 EVENT_LOGE("Failed to write interface token.");
538 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
539 }
540
541 if (events.size() > VECTOR_MAX_SIZE) {
542 EVENT_LOGE("Events size exceeds the max size.");
543 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
544 }
545
546 if (!data.WriteStringVector(events)) {
547 EVENT_LOGE("Failed to write event.");
548 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
549 }
550
551 if (!data.WriteBool(enable)) {
552 EVENT_LOGE("Failed to write enable.");
553 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
554 }
555
556 MessageParcel reply;
557 bool ret = SendRequest(CommonEventInterfaceCode::CES_SET_STATIC_SUBSCRIBER_EVENTS_STATE, data, reply);
558 if (!ret) {
559 return ERR_NOTIFICATION_SEND_ERROR;
560 }
561
562 return reply.ReadInt32();
563 }
564
SetFreezeStatus(std::set<int> pidList,bool isFreeze)565 bool CommonEventProxy::SetFreezeStatus(std::set<int> pidList, bool isFreeze)
566 {
567 EVENT_LOGD("start");
568
569 MessageParcel data;
570 MessageParcel reply;
571
572 if (!data.WriteInterfaceToken(GetDescriptor())) {
573 EVENT_LOGE("Failed to write InterfaceToken");
574 return false;
575 }
576
577 if (pidList.size() > VECTOR_MAX_SIZE) {
578 EVENT_LOGE("PidList size exceeds the max size.");
579 return false;
580 }
581
582 if (!data.WriteInt32(pidList.size())) {
583 EVENT_LOGE("Failed to write int pidList size");
584 return false;
585 }
586
587 for (auto it = pidList.begin(); it != pidList.end(); it++) {
588 if (!data.WriteInt32(*it)) {
589 EVENT_LOGE("Failed to write int pidList");
590 return false;
591 }
592 }
593
594 if (!data.WriteBool(isFreeze)) {
595 EVENT_LOGE("Failed to write isFreeze");
596 return false;
597 }
598
599 bool ret = SendRequest(CommonEventInterfaceCode::CES_SET_FREEZE_STATUS, data, reply);
600 if (ret) {
601 ret = reply.ReadBool();
602 }
603
604 EVENT_LOGD("end");
605 return ret;
606 }
607
SendRequest(CommonEventInterfaceCode code,MessageParcel & data,MessageParcel & reply)608 bool CommonEventProxy::SendRequest(CommonEventInterfaceCode code, MessageParcel &data, MessageParcel &reply)
609 {
610 EVENT_LOGD("start");
611
612 sptr<IRemoteObject> remote = Remote();
613 if (remote == nullptr) {
614 EVENT_LOGE("Remote is NULL, %{public}d", code);
615 return false;
616 }
617
618 MessageOption option(MessageOption::TF_SYNC);
619 int32_t result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
620 if (result != OHOS::NO_ERROR) {
621 EVENT_LOGE("Failed to SendRequest %{public}d, error code: %{public}d", code, result);
622 return false;
623 }
624
625 EVENT_LOGD("end");
626 return true;
627 }
628 } // namespace EventFwk
629 } // namespace OHOS