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_stub.h"
17 #include "common_event_publish_info.h"
18 #include "event_log_wrapper.h"
19 #include "string_ex.h"
20 #include "ces_inner_error_code.h"
21 #include "ces_xcollie.h"
22 
23 namespace OHOS {
24 namespace EventFwk {
25 using namespace OHOS::Notification;
26 namespace {
27 constexpr int32_t VECTOR_MAX_SIZE = 1000;
28 }
CommonEventStub()29 CommonEventStub::CommonEventStub()
30 {}
31 
~CommonEventStub()32 CommonEventStub::~CommonEventStub()
33 {}
34 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)35 int CommonEventStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
36 {
37     if (data.ReadInterfaceToken() != GetDescriptor()) {
38         EVENT_LOGE("local descriptor is not equal to remote");
39         return ERR_TRANSACTION_FAILED;
40     }
41 
42     switch (code) {
43         case static_cast<uint32_t>(CommonEventInterfaceCode::CES_PUBLISH_COMMON_EVENT): {
44             std::unique_ptr<CommonEventData> event(data.ReadParcelable<CommonEventData>());
45             std::unique_ptr<CommonEventPublishInfo> publishinfo(data.ReadParcelable<CommonEventPublishInfo>());
46             sptr<IRemoteObject> commonEventListener = nullptr;
47             bool hasLastSubscriber = data.ReadBool();
48             if (hasLastSubscriber) {
49                 commonEventListener = data.ReadRemoteObject();
50             }
51             int32_t userId = data.ReadInt32();
52             if (!event) {
53                 EVENT_LOGE("Failed to ReadParcelable<CommonEventData>");
54                 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
55             }
56             if (!publishinfo) {
57                 EVENT_LOGE("Failed to ReadParcelable<CommonEventPublishInfo>");
58                 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
59             }
60             int32_t ret = PublishCommonEvent(*event, *publishinfo, commonEventListener, userId);
61             if (!reply.WriteInt32(ret)) {
62                 EVENT_LOGE("Failed to write reply ");
63                 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
64             }
65             break;
66         }
67         case static_cast<uint32_t>(CommonEventInterfaceCode::CES_PUBLISH_COMMON_EVENT2): {
68             std::unique_ptr<CommonEventData> event(data.ReadParcelable<CommonEventData>());
69             std::unique_ptr<CommonEventPublishInfo> publishinfo(data.ReadParcelable<CommonEventPublishInfo>());
70             sptr<IRemoteObject> commonEventListener = nullptr;
71             bool hasLastSubscriber = data.ReadBool();
72             if (hasLastSubscriber) {
73                 commonEventListener = data.ReadRemoteObject();
74             }
75             int32_t uid = data.ReadInt32();
76             int32_t callerToken = data.ReadInt32();
77             int32_t userId = data.ReadInt32();
78             if (!event) {
79                 EVENT_LOGE("Failed to ReadParcelable<CommonEventData>");
80                 return ERR_INVALID_VALUE;
81             }
82             if (!publishinfo) {
83                 EVENT_LOGE("Failed to ReadParcelable<CommonEventPublishInfo>");
84                 return ERR_INVALID_VALUE;
85             }
86             bool ret = PublishCommonEvent(*event, *publishinfo, commonEventListener, uid, callerToken, userId);
87             if (!reply.WriteBool(ret)) {
88                 EVENT_LOGE("Failed to write reply ");
89                 return ERR_INVALID_VALUE;
90             }
91             break;
92         }
93         case static_cast<uint32_t>(CommonEventInterfaceCode::CES_SUBSCRIBE_COMMON_EVENT): {
94             CesXCollie cesXCollie("ces::SubscribeCommonEvent");
95             std::unique_ptr<CommonEventSubscribeInfo> subscribeInfo(data.ReadParcelable<CommonEventSubscribeInfo>());
96             if (!subscribeInfo) {
97                 EVENT_LOGE("Failed to ReadParcelable<CommonEventSubscribeInfo>");
98                 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
99             }
100             bool hasSubscriber = data.ReadBool();
101             if (!hasSubscriber) {
102                 EVENT_LOGE("no valid commonEventListener!");
103                 return ERR_INVALID_VALUE;
104             }
105             sptr<IRemoteObject> commonEventListener = data.ReadRemoteObject();
106             if (commonEventListener == nullptr) {
107                 EVENT_LOGE("Error to ReadParcelable<IRemoteObject>");
108                 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
109             }
110             int32_t instanceKey = data.ReadInt32();
111             int32_t ret = SubscribeCommonEvent(*subscribeInfo, commonEventListener, instanceKey);
112             if (!reply.WriteInt32(ret)) {
113                 EVENT_LOGE("Failed to write reply");
114                 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
115             }
116             break;
117         }
118         case static_cast<uint32_t>(CommonEventInterfaceCode::CES_UNSUBSCRIBE_COMMON_EVENT): {
119             CesXCollie cesXCollie("ces::UnsubscribeCommonEvent");
120             bool hasSubscriber = data.ReadBool();
121             if (!hasSubscriber) {
122                 EVENT_LOGE("no valid commonEventListener!");
123                 return ERR_INVALID_VALUE;
124             }
125             sptr<IRemoteObject> commonEventListener = data.ReadRemoteObject();
126             if (commonEventListener == nullptr) {
127                 EVENT_LOGE("Failed to ReadParcelable<IRemoteObject>");
128                 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
129             }
130             int32_t ret = UnsubscribeCommonEvent(commonEventListener);
131             if (!reply.WriteInt32(ret)) {
132                 EVENT_LOGE("Failed to write reply");
133                 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
134             }
135             break;
136         }
137         case static_cast<uint32_t>(CommonEventInterfaceCode::CES_GET_STICKY_COMMON_EVENT): {
138             std::string event = Str16ToStr8(data.ReadString16());
139             AAFwk::Want want;
140             CommonEventData eventData(want, 0, "");
141 
142             bool ret = GetStickyCommonEvent(event, eventData);
143             if (!reply.WriteBool(ret)) {
144                 EVENT_LOGE("Failed to write reply ret!");
145                 return ERR_INVALID_VALUE;
146             }
147             if (!reply.WriteParcelable(&eventData)) {
148                 EVENT_LOGE("Failed to write reply eventData!");
149                 return ERR_INVALID_VALUE;
150             }
151             break;
152         }
153         case static_cast<uint32_t>(CommonEventInterfaceCode::CES_DUMP_STATE): {
154             std::vector<std::string> result;
155             uint8_t dumpType = data.ReadUint8();
156             std::string event = Str16ToStr8(data.ReadString16());
157             int32_t userId = data.ReadInt32();
158             DumpState(dumpType, event, userId, result);
159             reply.WriteInt32(result.size());
160             for (auto stack : result) {
161                 reply.WriteString16(Str8ToStr16(stack));
162             }
163             break;
164         }
165         case static_cast<uint32_t>(CommonEventInterfaceCode::CES_FINISH_RECEIVER): {
166             bool hasPorxy = data.ReadBool();
167             if (!hasPorxy) {
168                 EVENT_LOGE("no valid proxy!");
169                 return ERR_INVALID_VALUE;
170             }
171             sptr<IRemoteObject> proxy = data.ReadRemoteObject();
172             if (proxy == nullptr) {
173                 EVENT_LOGE("Failed to ReadRemoteObject");
174                 return ERR_INVALID_VALUE;
175             }
176             int32_t receiverCode = data.ReadInt32();
177             std::string receiverData = Str16ToStr8(data.ReadString16());
178             bool abortEvent = data.ReadBool();
179             bool ret = FinishReceiver(proxy, receiverCode, receiverData, abortEvent);
180             if (!reply.WriteBool(ret)) {
181                 EVENT_LOGE("Failed to write reply");
182                 return ERR_INVALID_VALUE;
183             }
184             break;
185         }
186         case static_cast<uint32_t>(CommonEventInterfaceCode::CES_FREEZE): {
187             int32_t uid = data.ReadInt32();
188             bool ret = Freeze(uid);
189             if (!reply.WriteBool(ret)) {
190                 EVENT_LOGE("Failed to write reply");
191                 return ERR_INVALID_VALUE;
192             }
193             break;
194         }
195         case static_cast<uint32_t>(CommonEventInterfaceCode::CES_UNFREEZE): {
196             int32_t uid = data.ReadInt32();
197             bool ret = Unfreeze(uid);
198             if (!reply.WriteBool(ret)) {
199                 EVENT_LOGE("Failed to write reply");
200                 return ERR_INVALID_VALUE;
201             }
202             break;
203         }
204         case static_cast<uint32_t>(CommonEventInterfaceCode::CES_UNFREEZE_ALL): {
205             bool ret = UnfreezeAll();
206             if (!reply.WriteBool(ret)) {
207                 EVENT_LOGE("Failed to write reply");
208                 return ERR_INVALID_VALUE;
209             }
210             break;
211         }
212         case static_cast<uint32_t>(CommonEventInterfaceCode::CES_REMOVE_STICKY_COMMON_EVENT): {
213             std::string event = Str16ToStr8(data.ReadString16());
214             int32_t ret = RemoveStickyCommonEvent(event);
215             if (!reply.WriteInt32(ret)) {
216                 EVENT_LOGE("Failed to write reply");
217                 return ERR_INVALID_VALUE;
218             }
219             break;
220         }
221         case static_cast<uint32_t>(CommonEventInterfaceCode::CES_SET_STATIC_SUBSCRIBER_STATE): {
222             bool enable = data.ReadBool();
223             int32_t ret = SetStaticSubscriberState(enable);
224             if (!reply.WriteInt32(ret)) {
225                 EVENT_LOGE("Failed to write reply");
226                 return ERR_INVALID_VALUE;
227             }
228             break;
229         }
230         case static_cast<uint32_t>(CommonEventInterfaceCode::CES_SET_STATIC_SUBSCRIBER_EVENTS_STATE): {
231             std::vector<std::string> events;
232             data.ReadStringVector(&events);
233             if (events.size() > VECTOR_MAX_SIZE) {
234                 EVENT_LOGE("Events size exceeds the max size.");
235                 return ERR_INVALID_VALUE;
236             }
237             bool enable = data.ReadBool();
238             int32_t ret = SetStaticSubscriberState(events, enable);
239             if (!reply.WriteInt32(ret)) {
240                 EVENT_LOGE("Failed to write reply.");
241                 return ERR_INVALID_VALUE;
242             }
243             break;
244         }
245         case static_cast<uint32_t>(CommonEventInterfaceCode::CES_SET_FREEZE_STATUS): {
246             std::set<int> pidList;
247             int size = data.ReadInt32();
248             if (size > VECTOR_MAX_SIZE) {
249                 EVENT_LOGE("PidList size exceeds the max size.");
250                 return ERR_INVALID_VALUE;
251             }
252             for (int i = 0; i < size; i++) {
253                 pidList.insert(data.ReadInt32());
254             }
255             bool isFreeze = data.ReadBool();
256             bool ret = SetFreezeStatus(pidList, isFreeze);
257             if (!reply.WriteBool(ret)) {
258                 EVENT_LOGE("Failed to write reply");
259                 return ERR_INVALID_VALUE;
260             }
261             break;
262         }
263         default:
264             EVENT_LOGW("unknown, code = %{public}u, flags= %{public}u", code, option.GetFlags());
265             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
266     }
267 
268     return NO_ERROR;
269 }
270 
PublishCommonEvent(const CommonEventData & event,const CommonEventPublishInfo & publishinfo,const sptr<IRemoteObject> & commonEventListener,const int32_t & userId)271 int32_t CommonEventStub::PublishCommonEvent(const CommonEventData &event, const CommonEventPublishInfo &publishinfo,
272     const sptr<IRemoteObject> &commonEventListener, const int32_t &userId)
273 {
274     EVENT_LOGD("called");
275 
276     return ERR_OK;
277 }
278 
PublishCommonEvent(const CommonEventData & event,const CommonEventPublishInfo & publishinfo,const sptr<IRemoteObject> & commonEventListener,const uid_t & uid,const int32_t & callerToken,const int32_t & userId)279 bool CommonEventStub::PublishCommonEvent(const CommonEventData &event, const CommonEventPublishInfo &publishinfo,
280     const sptr<IRemoteObject> &commonEventListener, const uid_t &uid, const int32_t &callerToken,
281     const int32_t &userId)
282 {
283     EVENT_LOGD("called");
284 
285     return true;
286 }
287 
SubscribeCommonEvent(const CommonEventSubscribeInfo & subscribeInfo,const sptr<IRemoteObject> & commonEventListener,const int32_t instanceKey)288 int32_t CommonEventStub::SubscribeCommonEvent(const CommonEventSubscribeInfo &subscribeInfo,
289     const sptr<IRemoteObject> &commonEventListener, const int32_t instanceKey)
290 {
291     EVENT_LOGD("called");
292 
293     return ERR_OK;
294 }
295 
UnsubscribeCommonEvent(const sptr<IRemoteObject> & commonEventListener)296 int32_t CommonEventStub::UnsubscribeCommonEvent(const sptr<IRemoteObject> &commonEventListener)
297 {
298     EVENT_LOGD("called");
299 
300     return true;
301 }
302 
GetStickyCommonEvent(const std::string & event,CommonEventData & eventData)303 bool CommonEventStub::GetStickyCommonEvent(const std::string &event, CommonEventData &eventData)
304 {
305     EVENT_LOGD("called");
306 
307     return true;
308 }
309 
DumpState(const uint8_t & dumpType,const std::string & event,const int32_t & userId,std::vector<std::string> & state)310 bool CommonEventStub::DumpState(const uint8_t &dumpType, const std::string &event, const int32_t &userId,
311     std::vector<std::string> &state)
312 {
313     EVENT_LOGD("called");
314 
315     return true;
316 }
317 
FinishReceiver(const sptr<IRemoteObject> & proxy,const int32_t & code,const std::string & receiverData,const bool & abortEvent)318 bool CommonEventStub::FinishReceiver(const sptr<IRemoteObject> &proxy, const int32_t &code,
319     const std::string &receiverData, const bool &abortEvent)
320 {
321     EVENT_LOGD("called");
322 
323     return true;
324 }
325 
Freeze(const uid_t & uid)326 bool CommonEventStub::Freeze(const uid_t &uid)
327 {
328     EVENT_LOGD("called");
329 
330     return true;
331 }
332 
Unfreeze(const uid_t & uid)333 bool CommonEventStub::Unfreeze(const uid_t &uid)
334 {
335     EVENT_LOGD("called");
336 
337     return true;
338 }
339 
UnfreezeAll()340 bool CommonEventStub::UnfreezeAll()
341 {
342     EVENT_LOGD("called");
343 
344     return true;
345 }
346 
RemoveStickyCommonEvent(const std::string & event)347 int32_t CommonEventStub::RemoveStickyCommonEvent(const std::string &event)
348 {
349     EVENT_LOGD("called");
350 
351     return ERR_OK;
352 }
353 
SetStaticSubscriberState(bool enable)354 int32_t CommonEventStub::SetStaticSubscriberState(bool enable)
355 {
356     EVENT_LOGD("called");
357 
358     return ERR_OK;
359 }
360 
SetStaticSubscriberState(const std::vector<std::string> & events,bool enable)361 int32_t CommonEventStub::SetStaticSubscriberState(const std::vector<std::string> &events, bool enable)
362 {
363     EVENT_LOGD("Called.");
364     return ERR_OK;
365 }
366 
SetFreezeStatus(std::set<int> pidList,bool isFreeze)367 bool CommonEventStub::SetFreezeStatus(std::set<int> pidList, bool isFreeze)
368 {
369     EVENT_LOGD("Called.");
370     return ERR_OK;
371 }
372 }  // namespace EventFwk
373 }  // namespace OHOS