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 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,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
GetStickyCommonEvent(const std::string & event,CommonEventData & eventData)246 bool CommonEventProxy::GetStickyCommonEvent(const std::string &event, CommonEventData &eventData)
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 false;
256 }
257
258 if (!data.WriteString16(Str8ToStr16(event))) {
259 EVENT_LOGE("Failed to write string event");
260 return false;
261 }
262
263 bool ret = SendRequest(CommonEventInterfaceCode::CES_GET_STICKY_COMMON_EVENT, data, reply);
264 if (ret) {
265 ret = reply.ReadBool();
266 if (ret) {
267 std::unique_ptr<CommonEventData> eventDataPtr(reply.ReadParcelable<CommonEventData>());
268 eventData = *eventDataPtr;
269 }
270 }
271
272 EVENT_LOGD("end");
273 return ret;
274 }
275
DumpState(const uint8_t & dumpType,const std::string & event,const int32_t & userId,std::vector<std::string> & state)276 bool CommonEventProxy::DumpState(const uint8_t &dumpType, const std::string &event, const int32_t &userId,
277 std::vector<std::string> &state)
278 {
279 EVENT_LOGD("start");
280
281 MessageParcel data;
282 MessageParcel reply;
283
284 if (!data.WriteInterfaceToken(GetDescriptor())) {
285 EVENT_LOGE("Failed to write InterfaceToken");
286 return false;
287 }
288
289 if (!data.WriteUint8(dumpType)) {
290 EVENT_LOGE("Failed to write parcelable dumpType");
291 return false;
292 }
293
294 if (!data.WriteString16(Str8ToStr16(event))) {
295 EVENT_LOGE("Failed to write string event");
296 return false;
297 }
298
299 if (!data.WriteInt32(userId)) {
300 EVENT_LOGE("Failed to write parcelable userId");
301 return false;
302 }
303
304 bool ret = SendRequest(CommonEventInterfaceCode::CES_DUMP_STATE, data, reply);
305 if (ret) {
306 int32_t stackNum = reply.ReadInt32();
307 stackNum = stackNum > MAX_HISTORY_SIZE ? MAX_HISTORY_SIZE : stackNum;
308 for (int32_t i = 0; i < stackNum; i++) {
309 std::string stack = Str16ToStr8(reply.ReadString16());
310 state.emplace_back(stack);
311 }
312 }
313
314 EVENT_LOGD("end");
315 return ret;
316 }
317
FinishReceiver(const sptr<IRemoteObject> & proxy,const int32_t & code,const std::string & receiverData,const bool & abortEvent)318 bool CommonEventProxy::FinishReceiver(
319 const sptr<IRemoteObject> &proxy, const int32_t &code, const std::string &receiverData, const bool &abortEvent)
320 {
321 EVENT_LOGD("start");
322
323 MessageParcel data;
324 MessageParcel reply;
325
326 if (!data.WriteInterfaceToken(GetDescriptor())) {
327 EVENT_LOGE("Failed to write InterfaceToken");
328 return false;
329 }
330
331 if (proxy != nullptr) {
332 if (!data.WriteBool(true)) {
333 EVENT_LOGE("Failed to write parcelable hasproxy");
334 return false;
335 }
336 if (!data.WriteRemoteObject(proxy)) {
337 EVENT_LOGE("Failed to write parcelable proxy");
338 return false;
339 }
340 } else {
341 EVENT_LOGW("invalid proxy");
342 if (!data.WriteBool(false)) {
343 EVENT_LOGE("Failed to write parcelable hasproxy");
344 return false;
345 }
346 }
347
348 if (!data.WriteInt32(code)) {
349 EVENT_LOGE("Failed to write int code");
350 return false;
351 }
352 if (!data.WriteString16(Str8ToStr16(receiverData))) {
353 EVENT_LOGE("Failed to write string receiverData");
354 return false;
355 }
356 if (!data.WriteBool(abortEvent)) {
357 EVENT_LOGE("Failed to write bool abortEvent");
358 return false;
359 }
360
361 bool ret = SendRequest(CommonEventInterfaceCode::CES_FINISH_RECEIVER, data, reply);
362 if (ret) {
363 ret = reply.ReadBool();
364 }
365
366 EVENT_LOGD("end");
367 return ret;
368 }
369
Freeze(const uid_t & uid)370 bool CommonEventProxy::Freeze(const uid_t &uid)
371 {
372 EVENT_LOGD("Freeze start");
373
374 MessageParcel data;
375 MessageParcel reply;
376
377 if (!data.WriteInterfaceToken(GetDescriptor())) {
378 EVENT_LOGE("Error to write InterfaceToken");
379 return false;
380 }
381
382 if (!data.WriteInt32(uid)) {
383 EVENT_LOGE("Error to write int uid");
384 return false;
385 }
386
387 bool ret = SendRequest(CommonEventInterfaceCode::CES_FREEZE, data, reply);
388 if (ret) {
389 ret = reply.ReadBool();
390 }
391
392 EVENT_LOGD("end");
393 return ret;
394 }
395
Unfreeze(const uid_t & uid)396 bool CommonEventProxy::Unfreeze(const uid_t &uid)
397 {
398 EVENT_LOGD("start");
399
400 MessageParcel data;
401 MessageParcel reply;
402
403 if (!data.WriteInterfaceToken(GetDescriptor())) {
404 EVENT_LOGE("Failed to write InterfaceToken");
405 return false;
406 }
407
408 if (!data.WriteInt32(uid)) {
409 EVENT_LOGE("Failed to write int uid");
410 return false;
411 }
412
413 bool ret = SendRequest(CommonEventInterfaceCode::CES_UNFREEZE, data, reply);
414 if (ret) {
415 ret = reply.ReadBool();
416 }
417
418 EVENT_LOGD("end");
419 return ret;
420 }
421
UnfreezeAll()422 bool CommonEventProxy::UnfreezeAll()
423 {
424 EVENT_LOGD("start");
425
426 MessageParcel data;
427 MessageParcel reply;
428
429 if (!data.WriteInterfaceToken(GetDescriptor())) {
430 EVENT_LOGE("Failed to write InterfaceToken");
431 return false;
432 }
433
434 bool ret = SendRequest(CommonEventInterfaceCode::CES_UNFREEZE_ALL, data, reply);
435 if (ret) {
436 ret = reply.ReadBool();
437 }
438
439 EVENT_LOGD("end");
440 return ret;
441 }
442
RemoveStickyCommonEvent(const std::string & event)443 int32_t CommonEventProxy::RemoveStickyCommonEvent(const std::string &event)
444 {
445 EVENT_LOGD("start");
446
447 MessageParcel data;
448 if (!data.WriteInterfaceToken(GetDescriptor())) {
449 EVENT_LOGE("Failed to write InterfaceToken");
450 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
451 }
452
453 if (!data.WriteString16(Str8ToStr16(event))) {
454 EVENT_LOGE("Failed to write string event");
455 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
456 }
457
458 MessageParcel reply;
459 bool ret = SendRequest(CommonEventInterfaceCode::CES_REMOVE_STICKY_COMMON_EVENT, data, reply);
460 if (!ret) {
461 return ERR_NOTIFICATION_SEND_ERROR;
462 }
463
464 EVENT_LOGD("end");
465 return reply.ReadInt32();
466 }
467
SetStaticSubscriberState(bool enable)468 int32_t CommonEventProxy::SetStaticSubscriberState(bool enable)
469 {
470 EVENT_LOGD("start");
471
472 MessageParcel data;
473 if (!data.WriteInterfaceToken(GetDescriptor())) {
474 EVENT_LOGE("Failed to write InterfaceToken");
475 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
476 }
477
478 if (!data.WriteBool(enable)) {
479 EVENT_LOGE("Failed to write bool enable");
480 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
481 }
482
483 MessageParcel reply;
484 bool ret = SendRequest(CommonEventInterfaceCode::CES_SET_STATIC_SUBSCRIBER_STATE, data, reply);
485 if (!ret) {
486 return ERR_NOTIFICATION_SEND_ERROR;
487 }
488
489 EVENT_LOGD("end");
490 return reply.ReadInt32();
491 }
492
SetStaticSubscriberState(const std::vector<std::string> & events,bool enable)493 int32_t CommonEventProxy::SetStaticSubscriberState(const std::vector<std::string> &events, bool enable)
494 {
495 EVENT_LOGD("Called.");
496 MessageParcel data;
497 if (!data.WriteInterfaceToken(GetDescriptor())) {
498 EVENT_LOGE("Failed to write interface token.");
499 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
500 }
501
502 if (events.size() > VECTOR_MAX_SIZE) {
503 EVENT_LOGE("Events size exceeds the max size.");
504 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
505 }
506
507 if (!data.WriteStringVector(events)) {
508 EVENT_LOGE("Failed to write event.");
509 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
510 }
511
512 if (!data.WriteBool(enable)) {
513 EVENT_LOGE("Failed to write enable.");
514 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
515 }
516
517 MessageParcel reply;
518 bool ret = SendRequest(CommonEventInterfaceCode::CES_SET_STATIC_SUBSCRIBER_EVENTS_STATE, data, reply);
519 if (!ret) {
520 return ERR_NOTIFICATION_SEND_ERROR;
521 }
522
523 return reply.ReadInt32();
524 }
525
SetFreezeStatus(std::set<int> pidList,bool isFreeze)526 bool CommonEventProxy::SetFreezeStatus(std::set<int> pidList, bool isFreeze)
527 {
528 EVENT_LOGD("start");
529
530 MessageParcel data;
531 MessageParcel reply;
532
533 if (!data.WriteInterfaceToken(GetDescriptor())) {
534 EVENT_LOGE("Failed to write InterfaceToken");
535 return false;
536 }
537
538 if (pidList.size() > VECTOR_MAX_SIZE) {
539 EVENT_LOGE("PidList size exceeds the max size.");
540 return false;
541 }
542
543 if (!data.WriteInt32(pidList.size())) {
544 EVENT_LOGE("Failed to write int pidList size");
545 return false;
546 }
547
548 for (auto it = pidList.begin(); it != pidList.end(); it++) {
549 if (!data.WriteInt32(*it)) {
550 EVENT_LOGE("Failed to write int pidList");
551 return false;
552 }
553 }
554
555 if (!data.WriteBool(isFreeze)) {
556 EVENT_LOGE("Failed to write isFreeze");
557 return false;
558 }
559
560 bool ret = SendRequest(CommonEventInterfaceCode::CES_SET_FREEZE_STATUS, data, reply);
561 if (ret) {
562 ret = reply.ReadBool();
563 }
564
565 EVENT_LOGD("end");
566 return ret;
567 }
568
SendRequest(CommonEventInterfaceCode code,MessageParcel & data,MessageParcel & reply)569 bool CommonEventProxy::SendRequest(CommonEventInterfaceCode code, MessageParcel &data, MessageParcel &reply)
570 {
571 EVENT_LOGD("start");
572
573 sptr<IRemoteObject> remote = Remote();
574 if (remote == nullptr) {
575 EVENT_LOGE("Remote is NULL, %{public}d", code);
576 return false;
577 }
578
579 MessageOption option(MessageOption::TF_SYNC);
580 int32_t result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
581 if (result != OHOS::NO_ERROR) {
582 EVENT_LOGE("Failed to SendRequest %{public}d, error code: %{public}d", code, result);
583 return false;
584 }
585
586 EVENT_LOGD("end");
587 return true;
588 }
589 } // namespace EventFwk
590 } // namespace OHOS