• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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>(ICommonEvent::Message::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>(ICommonEvent::Message::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>(ICommonEvent::Message::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>(ICommonEvent::Message::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>(ICommonEvent::Message::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>(ICommonEvent::Message::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>(ICommonEvent::Message::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>(ICommonEvent::Message::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>(ICommonEvent::Message::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>(ICommonEvent::Message::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         default:
205             EVENT_LOGW("unknown, code = %{public}u, flags= %{public}u", code, option.GetFlags());
206             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
207     }
208 
209     return NO_ERROR;
210 }
211 
PublishCommonEvent(const CommonEventData & event,const CommonEventPublishInfo & publishinfo,const sptr<IRemoteObject> & commonEventListener,const int32_t & userId)212 int32_t CommonEventStub::PublishCommonEvent(const CommonEventData &event, const CommonEventPublishInfo &publishinfo,
213     const sptr<IRemoteObject> &commonEventListener, const int32_t &userId)
214 {
215     EVENT_LOGD("called");
216 
217     return ERR_OK;
218 }
219 
PublishCommonEvent(const CommonEventData & event,const CommonEventPublishInfo & publishinfo,const sptr<IRemoteObject> & commonEventListener,const uid_t & uid,const int32_t & callerToken,const int32_t & userId)220 bool CommonEventStub::PublishCommonEvent(const CommonEventData &event, const CommonEventPublishInfo &publishinfo,
221     const sptr<IRemoteObject> &commonEventListener, const uid_t &uid, const int32_t &callerToken,
222     const int32_t &userId)
223 {
224     EVENT_LOGD("called");
225 
226     return true;
227 }
228 
SubscribeCommonEvent(const CommonEventSubscribeInfo & subscribeInfo,const sptr<IRemoteObject> & commonEventListener)229 int32_t CommonEventStub::SubscribeCommonEvent(
230     const CommonEventSubscribeInfo &subscribeInfo, const sptr<IRemoteObject> &commonEventListener)
231 {
232     EVENT_LOGD("called");
233 
234     return ERR_OK;
235 }
236 
UnsubscribeCommonEvent(const sptr<IRemoteObject> & commonEventListener)237 int32_t CommonEventStub::UnsubscribeCommonEvent(const sptr<IRemoteObject> &commonEventListener)
238 {
239     EVENT_LOGD("called");
240 
241     return true;
242 }
243 
GetStickyCommonEvent(const std::string & event,CommonEventData & eventData)244 bool CommonEventStub::GetStickyCommonEvent(const std::string &event, CommonEventData &eventData)
245 {
246     EVENT_LOGD("called");
247 
248     return true;
249 }
250 
DumpState(const uint8_t & dumpType,const std::string & event,const int32_t & userId,std::vector<std::string> & state)251 bool CommonEventStub::DumpState(const uint8_t &dumpType, const std::string &event, const int32_t &userId,
252     std::vector<std::string> &state)
253 {
254     EVENT_LOGD("called");
255 
256     return true;
257 }
258 
FinishReceiver(const sptr<IRemoteObject> & proxy,const int32_t & code,const std::string & receiverData,const bool & abortEvent)259 bool CommonEventStub::FinishReceiver(const sptr<IRemoteObject> &proxy, const int32_t &code,
260     const std::string &receiverData, const bool &abortEvent)
261 {
262     EVENT_LOGD("called");
263 
264     return true;
265 }
266 
Freeze(const uid_t & uid)267 bool CommonEventStub::Freeze(const uid_t &uid)
268 {
269     EVENT_LOGD("called");
270 
271     return true;
272 }
273 
Unfreeze(const uid_t & uid)274 bool CommonEventStub::Unfreeze(const uid_t &uid)
275 {
276     EVENT_LOGD("called");
277 
278     return true;
279 }
280 
UnfreezeAll()281 bool CommonEventStub::UnfreezeAll()
282 {
283     EVENT_LOGD("called");
284 
285     return true;
286 }
287 }  // namespace EventFwk
288 }  // namespace OHOS