• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_UNSUBSCRIBE_COMMON_EVENT_SYNC): {
138             CesXCollie cesXCollie("ces::UnsubscribeCommonEvent");
139             bool hasSubscriber = data.ReadBool();
140             if (!hasSubscriber) {
141                 EVENT_LOGE("no valid commonEventListener!");
142                 return ERR_INVALID_VALUE;
143             }
144             sptr<IRemoteObject> commonEventListener = data.ReadRemoteObject();
145             if (commonEventListener == nullptr) {
146                 EVENT_LOGE("Failed to ReadParcelable<IRemoteObject>");
147                 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
148             }
149             int32_t ret = UnsubscribeCommonEventSync(commonEventListener);
150             if (!reply.WriteInt32(ret)) {
151                 EVENT_LOGE("Failed to write reply");
152                 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
153             }
154             break;
155         }
156         case static_cast<uint32_t>(CommonEventInterfaceCode::CES_GET_STICKY_COMMON_EVENT): {
157             std::string event = Str16ToStr8(data.ReadString16());
158             AAFwk::Want want;
159             CommonEventData eventData(want, 0, "");
160 
161             bool ret = GetStickyCommonEvent(event, eventData);
162             if (!reply.WriteBool(ret)) {
163                 EVENT_LOGE("Failed to write reply ret!");
164                 return ERR_INVALID_VALUE;
165             }
166             if (!reply.WriteParcelable(&eventData)) {
167                 EVENT_LOGE("Failed to write reply eventData!");
168                 return ERR_INVALID_VALUE;
169             }
170             break;
171         }
172         case static_cast<uint32_t>(CommonEventInterfaceCode::CES_DUMP_STATE): {
173             std::vector<std::string> result;
174             uint8_t dumpType = data.ReadUint8();
175             std::string event = Str16ToStr8(data.ReadString16());
176             int32_t userId = data.ReadInt32();
177             DumpState(dumpType, event, userId, result);
178             reply.WriteInt32(result.size());
179             for (auto stack : result) {
180                 reply.WriteString16(Str8ToStr16(stack));
181             }
182             break;
183         }
184         case static_cast<uint32_t>(CommonEventInterfaceCode::CES_FINISH_RECEIVER): {
185             bool hasPorxy = data.ReadBool();
186             if (!hasPorxy) {
187                 EVENT_LOGE("no valid proxy!");
188                 return ERR_INVALID_VALUE;
189             }
190             sptr<IRemoteObject> proxy = data.ReadRemoteObject();
191             if (proxy == nullptr) {
192                 EVENT_LOGE("Failed to ReadRemoteObject");
193                 return ERR_INVALID_VALUE;
194             }
195             int32_t receiverCode = data.ReadInt32();
196             std::string receiverData = Str16ToStr8(data.ReadString16());
197             bool abortEvent = data.ReadBool();
198             bool ret = FinishReceiver(proxy, receiverCode, receiverData, abortEvent);
199             if (!reply.WriteBool(ret)) {
200                 EVENT_LOGE("Failed to write reply");
201                 return ERR_INVALID_VALUE;
202             }
203             break;
204         }
205         case static_cast<uint32_t>(CommonEventInterfaceCode::CES_FREEZE): {
206             int32_t uid = data.ReadInt32();
207             bool ret = Freeze(uid);
208             if (!reply.WriteBool(ret)) {
209                 EVENT_LOGE("Failed to write reply");
210                 return ERR_INVALID_VALUE;
211             }
212             break;
213         }
214         case static_cast<uint32_t>(CommonEventInterfaceCode::CES_UNFREEZE): {
215             int32_t uid = data.ReadInt32();
216             bool ret = Unfreeze(uid);
217             if (!reply.WriteBool(ret)) {
218                 EVENT_LOGE("Failed to write reply");
219                 return ERR_INVALID_VALUE;
220             }
221             break;
222         }
223         case static_cast<uint32_t>(CommonEventInterfaceCode::CES_UNFREEZE_ALL): {
224             bool ret = UnfreezeAll();
225             if (!reply.WriteBool(ret)) {
226                 EVENT_LOGE("Failed to write reply");
227                 return ERR_INVALID_VALUE;
228             }
229             break;
230         }
231         case static_cast<uint32_t>(CommonEventInterfaceCode::CES_REMOVE_STICKY_COMMON_EVENT): {
232             std::string event = Str16ToStr8(data.ReadString16());
233             int32_t ret = RemoveStickyCommonEvent(event);
234             if (!reply.WriteInt32(ret)) {
235                 EVENT_LOGE("Failed to write reply");
236                 return ERR_INVALID_VALUE;
237             }
238             break;
239         }
240         case static_cast<uint32_t>(CommonEventInterfaceCode::CES_SET_STATIC_SUBSCRIBER_STATE): {
241             bool enable = data.ReadBool();
242             int32_t ret = SetStaticSubscriberState(enable);
243             if (!reply.WriteInt32(ret)) {
244                 EVENT_LOGE("Failed to write reply");
245                 return ERR_INVALID_VALUE;
246             }
247             break;
248         }
249         case static_cast<uint32_t>(CommonEventInterfaceCode::CES_SET_STATIC_SUBSCRIBER_EVENTS_STATE): {
250             std::vector<std::string> events;
251             data.ReadStringVector(&events);
252             if (events.size() > VECTOR_MAX_SIZE) {
253                 EVENT_LOGE("Events size exceeds the max size.");
254                 return ERR_INVALID_VALUE;
255             }
256             bool enable = data.ReadBool();
257             int32_t ret = SetStaticSubscriberState(events, enable);
258             if (!reply.WriteInt32(ret)) {
259                 EVENT_LOGE("Failed to write reply.");
260                 return ERR_INVALID_VALUE;
261             }
262             break;
263         }
264         case static_cast<uint32_t>(CommonEventInterfaceCode::CES_SET_FREEZE_STATUS): {
265             std::set<int> pidList;
266             int size = data.ReadInt32();
267             if (size > VECTOR_MAX_SIZE) {
268                 EVENT_LOGE("PidList size exceeds the max size.");
269                 return ERR_INVALID_VALUE;
270             }
271             for (int i = 0; i < size; i++) {
272                 pidList.insert(data.ReadInt32());
273             }
274             bool isFreeze = data.ReadBool();
275             bool ret = SetFreezeStatus(pidList, isFreeze);
276             if (!reply.WriteBool(ret)) {
277                 EVENT_LOGE("Failed to write reply");
278                 return ERR_INVALID_VALUE;
279             }
280             break;
281         }
282         default:
283             EVENT_LOGW("unknown, code = %{public}u, flags= %{public}u", code, option.GetFlags());
284             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
285     }
286 
287     return NO_ERROR;
288 }
289 
PublishCommonEvent(const CommonEventData & event,const CommonEventPublishInfo & publishinfo,const sptr<IRemoteObject> & commonEventListener,const int32_t & userId)290 int32_t CommonEventStub::PublishCommonEvent(const CommonEventData &event, const CommonEventPublishInfo &publishinfo,
291     const sptr<IRemoteObject> &commonEventListener, const int32_t &userId)
292 {
293     EVENT_LOGD("called");
294 
295     return ERR_OK;
296 }
297 
PublishCommonEvent(const CommonEventData & event,const CommonEventPublishInfo & publishinfo,const sptr<IRemoteObject> & commonEventListener,const uid_t & uid,const int32_t & callerToken,const int32_t & userId)298 bool CommonEventStub::PublishCommonEvent(const CommonEventData &event, const CommonEventPublishInfo &publishinfo,
299     const sptr<IRemoteObject> &commonEventListener, const uid_t &uid, const int32_t &callerToken,
300     const int32_t &userId)
301 {
302     EVENT_LOGD("called");
303 
304     return true;
305 }
306 
SubscribeCommonEvent(const CommonEventSubscribeInfo & subscribeInfo,const sptr<IRemoteObject> & commonEventListener,const int32_t instanceKey)307 int32_t CommonEventStub::SubscribeCommonEvent(const CommonEventSubscribeInfo &subscribeInfo,
308     const sptr<IRemoteObject> &commonEventListener, const int32_t instanceKey)
309 {
310     EVENT_LOGD("called");
311 
312     return ERR_OK;
313 }
314 
UnsubscribeCommonEvent(const sptr<IRemoteObject> & commonEventListener)315 int32_t CommonEventStub::UnsubscribeCommonEvent(const sptr<IRemoteObject> &commonEventListener)
316 {
317     EVENT_LOGD("called");
318 
319     return true;
320 }
321 
UnsubscribeCommonEventSync(const sptr<IRemoteObject> & commonEventListener)322 int32_t CommonEventStub::UnsubscribeCommonEventSync(const sptr<IRemoteObject> &commonEventListener)
323 {
324     EVENT_LOGD("called");
325 
326     return true;
327 }
328 
GetStickyCommonEvent(const std::string & event,CommonEventData & eventData)329 bool CommonEventStub::GetStickyCommonEvent(const std::string &event, CommonEventData &eventData)
330 {
331     EVENT_LOGD("called");
332 
333     return true;
334 }
335 
DumpState(const uint8_t & dumpType,const std::string & event,const int32_t & userId,std::vector<std::string> & state)336 bool CommonEventStub::DumpState(const uint8_t &dumpType, const std::string &event, const int32_t &userId,
337     std::vector<std::string> &state)
338 {
339     EVENT_LOGD("called");
340 
341     return true;
342 }
343 
FinishReceiver(const sptr<IRemoteObject> & proxy,const int32_t & code,const std::string & receiverData,const bool & abortEvent)344 bool CommonEventStub::FinishReceiver(const sptr<IRemoteObject> &proxy, const int32_t &code,
345     const std::string &receiverData, const bool &abortEvent)
346 {
347     EVENT_LOGD("called");
348 
349     return true;
350 }
351 
Freeze(const uid_t & uid)352 bool CommonEventStub::Freeze(const uid_t &uid)
353 {
354     EVENT_LOGD("called");
355 
356     return true;
357 }
358 
Unfreeze(const uid_t & uid)359 bool CommonEventStub::Unfreeze(const uid_t &uid)
360 {
361     EVENT_LOGD("called");
362 
363     return true;
364 }
365 
UnfreezeAll()366 bool CommonEventStub::UnfreezeAll()
367 {
368     EVENT_LOGD("called");
369 
370     return true;
371 }
372 
RemoveStickyCommonEvent(const std::string & event)373 int32_t CommonEventStub::RemoveStickyCommonEvent(const std::string &event)
374 {
375     EVENT_LOGD("called");
376 
377     return ERR_OK;
378 }
379 
SetStaticSubscriberState(bool enable)380 int32_t CommonEventStub::SetStaticSubscriberState(bool enable)
381 {
382     EVENT_LOGD("called");
383 
384     return ERR_OK;
385 }
386 
SetStaticSubscriberState(const std::vector<std::string> & events,bool enable)387 int32_t CommonEventStub::SetStaticSubscriberState(const std::vector<std::string> &events, bool enable)
388 {
389     EVENT_LOGD("Called.");
390     return ERR_OK;
391 }
392 
SetFreezeStatus(std::set<int> pidList,bool isFreeze)393 bool CommonEventStub::SetFreezeStatus(std::set<int> pidList, bool isFreeze)
394 {
395     EVENT_LOGD("Called.");
396     return ERR_OK;
397 }
398 }  // namespace EventFwk
399 }  // namespace OHOS