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 "form_host_stub.h"
17 
18 #include "appexecfwk_errors.h"
19 #include "app_scheduler_interface.h"
20 #include "errors.h"
21 #include "fms_log_wrapper.h"
22 #include "form_mgr_errors.h"
23 #include "ipc_skeleton.h"
24 #include "ipc_types.h"
25 #include "iremote_object.h"
26 
27 namespace OHOS {
28 namespace AppExecFwk {
FormHostStub()29 FormHostStub::FormHostStub()
30 {}
31 
~FormHostStub()32 FormHostStub::~FormHostStub()
33 {}
34 /**
35  * @brief handle remote request.
36  * @param data input param.
37  * @param reply output param.
38  * @param option message option.
39  * @return Returns ERR_OK on success, others on failure.
40  */
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)41 int FormHostStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
42 {
43     HILOG_INFO("code:%{public}u,flags:%{public}d", code, option.GetFlags());
44     std::u16string descriptor = FormHostStub::GetDescriptor();
45     std::u16string remoteDescriptor = data.ReadInterfaceToken();
46     if (descriptor != remoteDescriptor) {
47         HILOG_ERROR("localDescribe not equal to remote");
48         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
49     }
50 
51     switch (code) {
52         case static_cast<uint32_t>(IFormHost::Message::FORM_HOST_ON_ACQUIRED):
53             return HandleAcquired(data, reply);
54         case static_cast<uint32_t>(IFormHost::Message::FORM_HOST_ON_UPDATE):
55             return HandleOnUpdate(data, reply);
56         case static_cast<uint32_t>(IFormHost::Message::FORM_HOST_ON_UNINSTALL):
57             return HandleOnUninstall(data, reply);
58         case static_cast<uint32_t>(IFormHost::Message::FORM_HOST_ON_ACQUIRE_FORM_STATE):
59             return HandleOnAcquireState(data, reply);
60         case static_cast<uint32_t>(IFormHost::Message::FORM_HOST_ON_SHARE_FORM_RESPONSE):
61             return HandleOnShareFormResponse(data, reply);
62         case static_cast<uint32_t>(IFormHost::Message::FORM_HOST_ON_ERROR):
63             return HandleOnError(data, reply);
64         case static_cast<uint32_t>(IFormHost::Message::FORM_HOST_ON_ACQUIRE_FORM_DATA):
65             return HandleOnAcquireDataResponse(data, reply);
66         case static_cast<uint32_t>(IFormHost::Message::FORM_HOST_ON_RECYCLE_FORM):
67             return HandleOnRecycleForm(data, reply);
68         case static_cast<uint32_t>(IFormHost::Message::FORM_HOST_ON_ENABLE_FORM):
69             return HandleOnEnableForm(data, reply);
70         case static_cast<uint32_t>(IFormHost::Message::FORM_HOST_ON_ERROR_FORMS):
71             return HandleOnErrorForms(data, reply);
72         default:
73             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
74     }
75 }
76 /**
77  * @brief handle OnAcquired event.
78  * @param data input param.
79  * @param reply output param.
80  * @return Returns ERR_OK on success, others on failure.
81  */
HandleAcquired(MessageParcel & data,MessageParcel & reply)82 int FormHostStub::HandleAcquired(MessageParcel &data, MessageParcel &reply)
83 {
84     std::unique_ptr<FormJsInfo> formInfo(data.ReadParcelable<FormJsInfo>());
85     if (!formInfo) {
86         HILOG_ERROR("ReadParcelable<FormJsInfo> failed");
87         return ERR_APPEXECFWK_PARCEL_ERROR;
88     }
89 
90     sptr<IRemoteObject> token = nullptr;
91     if (data.ReadBool()) {
92         token = data.ReadRemoteObject();
93     }
94 
95     OnAcquired(*formInfo, token);
96     reply.WriteInt32(ERR_OK);
97     return ERR_OK;
98 }
99 /**
100  * @brief handle OnUpdate event.
101  * @param data input param.
102  * @param reply output param.
103  * @return Returns ERR_OK on success, others on failure.
104  */
HandleOnUpdate(MessageParcel & data,MessageParcel & reply)105 int FormHostStub::HandleOnUpdate(MessageParcel &data, MessageParcel &reply)
106 {
107     std::unique_ptr<FormJsInfo> formInfo(data.ReadParcelable<FormJsInfo>());
108     if (!formInfo) {
109         HILOG_ERROR("ReadParcelable<FormJsInfo> failed");
110         return ERR_APPEXECFWK_PARCEL_ERROR;
111     }
112     OnUpdate(*formInfo);
113     reply.WriteInt32(ERR_OK);
114     return ERR_OK;
115 }
116 
117 /**
118  * @brief handle OnUnInstall event.
119  * @param data input param.
120  * @param reply output param.
121  * @return Returns ERR_OK on success, others on failure.
122  */
HandleOnUninstall(MessageParcel & data,MessageParcel & reply)123 int FormHostStub::HandleOnUninstall(MessageParcel &data, MessageParcel &reply)
124 {
125     std::vector<int64_t> formIds;
126     bool ret = data.ReadInt64Vector(&formIds);
127     if (ret) {
128         OnUninstall(formIds);
129         reply.WriteInt32(ERR_OK);
130         return ERR_OK;
131     }
132     return ERR_APPEXECFWK_PARCEL_ERROR;
133 }
134 
135 /**
136  * @brief handle OnAcquireState message.
137  * @param data input param.
138  * @param reply output param.
139  * @return Returns ERR_OK on success, others on failure.
140  */
HandleOnAcquireState(MessageParcel & data,MessageParcel & reply)141 int FormHostStub::HandleOnAcquireState(MessageParcel &data, MessageParcel &reply)
142 {
143     auto state = (FormState) data.ReadInt32();
144 
145     std::unique_ptr<AAFwk::Want> want(data.ReadParcelable<AAFwk::Want>());
146     if (!want) {
147         HILOG_ERROR("ReadParcelable<Want> failed");
148         reply.WriteInt32(ERR_APPEXECFWK_PARCEL_ERROR);
149         return ERR_APPEXECFWK_PARCEL_ERROR;
150     }
151 
152     OnAcquireState(state, *want);
153     reply.WriteInt32(ERR_OK);
154     return ERR_OK;
155 }
156 
HandleOnShareFormResponse(MessageParcel & data,MessageParcel & reply)157 int32_t FormHostStub::HandleOnShareFormResponse(MessageParcel &data, MessageParcel &reply)
158 {
159     auto requestCode = data.ReadInt64();
160     auto result = data.ReadInt32();
161 
162     OnShareFormResponse(requestCode, result);
163     reply.WriteInt32(ERR_OK);
164     return ERR_OK;
165 }
166 
HandleOnError(MessageParcel & data,MessageParcel & reply)167 int32_t FormHostStub::HandleOnError(MessageParcel &data, MessageParcel &reply)
168 {
169     int32_t errorCode = data.ReadInt32();
170     std::string errorMsg = Str16ToStr8(data.ReadString16());
171 
172     OnError(errorCode, errorMsg);
173     reply.WriteInt32(ERR_OK);
174     return ERR_OK;
175 }
176 
HandleOnAcquireDataResponse(MessageParcel & data,MessageParcel & reply)177 int32_t FormHostStub::HandleOnAcquireDataResponse(MessageParcel &data, MessageParcel &reply)
178 {
179     std::shared_ptr<AAFwk::WantParams> wantParams(data.ReadParcelable<AAFwk::WantParams>());
180     if (wantParams == nullptr) {
181         HILOG_ERROR("ReadParcelable<wantParams> failed");
182         return ERR_APPEXECFWK_PARCEL_ERROR;
183     }
184 
185     auto requestCode = data.ReadInt64();
186     if (requestCode <= 0) {
187         HILOG_ERROR("fail ReadInt64<requestCode>");
188         return ERR_APPEXECFWK_PARCEL_ERROR;
189     }
190 
191     OnAcquireDataResponse(*wantParams, requestCode);
192     reply.WriteInt32(ERR_OK);
193     return ERR_OK;
194 }
195 
HandleOnRecycleForm(MessageParcel & data,MessageParcel & reply)196 int32_t FormHostStub::HandleOnRecycleForm(MessageParcel &data, MessageParcel &reply)
197 {
198     int64_t formId = data.ReadInt64();
199     OnRecycleForm(formId);
200     reply.WriteInt32(ERR_OK);
201     return ERR_OK;
202 }
203 
HandleOnEnableForm(MessageParcel & data,MessageParcel & reply)204 int32_t FormHostStub::HandleOnEnableForm(MessageParcel &data, MessageParcel &reply)
205 {
206     std::vector<int64_t> formIds;
207     bool ret = data.ReadInt64Vector(&formIds);
208     if (!ret) {
209         HILOG_ERROR("fail ReadInt64Vector<formIds>");
210         return ERR_APPEXECFWK_PARCEL_ERROR;
211     }
212     bool enable = data.ReadBool();
213     OnEnableForm(formIds, enable);
214     reply.WriteInt32(ERR_OK);
215     return ERR_OK;
216 }
217 
HandleOnErrorForms(MessageParcel & data,MessageParcel & reply)218 int32_t FormHostStub::HandleOnErrorForms(MessageParcel &data, MessageParcel &reply)
219 {
220     int32_t errorCode = data.ReadInt32();
221     std::string errorMsg = Str16ToStr8(data.ReadString16());
222     std::vector<int64_t> formIds;
223     bool ret = data.ReadInt64Vector(&formIds);
224     if (!ret) {
225         HILOG_ERROR("fail ReadInt64Vector<formIds>");
226         return ERR_APPEXECFWK_PARCEL_ERROR;
227     }
228     OnError(errorCode, errorMsg, formIds);
229     reply.WriteInt32(ERR_OK);
230     return ERR_OK;
231 }
232 
233 }  // namespace AppExecFwk
234 }  // namespace OHOS