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