1 /*
2 * Copyright (c) 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 #include "form_render_proxy.h"
16
17 #include <utility>
18
19 #include "appexecfwk_errors.h"
20 #include "fms_log_wrapper.h"
21 #include "string_ex.h"
22
23 namespace OHOS {
24 namespace AppExecFwk {
RenderForm(const FormJsInfo & formJsInfo,const Want & want,sptr<IRemoteObject> callerToken)25 int32_t FormRenderProxy::RenderForm(const FormJsInfo &formJsInfo, const Want &want,
26 sptr<IRemoteObject> callerToken)
27 {
28 MessageParcel data;
29 MessageParcel reply;
30 MessageOption option(MessageOption::TF_SYNC);
31
32 if (!WriteInterfaceToken(data)) {
33 HILOG_ERROR("write interface token failed");
34 return ERR_APPEXECFWK_PARCEL_ERROR;
35 }
36 if (!data.WriteParcelable(&formJsInfo)) {
37 HILOG_ERROR("write formJsInfo error");
38 return ERR_APPEXECFWK_PARCEL_ERROR;
39 }
40 if (!data.WriteParcelable(&want)) {
41 HILOG_ERROR("write want error");
42 return ERR_APPEXECFWK_PARCEL_ERROR;
43 }
44
45 if (!data.WriteRemoteObject(callerToken)) {
46 HILOG_ERROR("write callerToken error");
47 return ERR_APPEXECFWK_PARCEL_ERROR;
48 }
49
50 int error = SendTransactCmd(
51 IFormRender::Message::FORM_RENDER_RENDER_FORM,
52 data,
53 reply,
54 option);
55 if (error != ERR_OK) {
56 HILOG_ERROR("error to SendRequest:%{public}d", error);
57 return error;
58 }
59 return reply.ReadInt32();
60 }
61
StopRenderingForm(const FormJsInfo & formJsInfo,const Want & want,const sptr<IRemoteObject> & callerToken)62 int32_t FormRenderProxy::StopRenderingForm(const FormJsInfo &formJsInfo, const Want &want,
63 const sptr<IRemoteObject> &callerToken)
64 {
65 MessageParcel data;
66 MessageParcel reply;
67 MessageOption option(MessageOption::TF_ASYNC);
68
69 if (!WriteInterfaceToken(data)) {
70 HILOG_ERROR("write interface token failed");
71 return ERR_APPEXECFWK_PARCEL_ERROR;
72 }
73 if (!data.WriteParcelable(&formJsInfo)) {
74 HILOG_ERROR("write formJsInfo error");
75 return ERR_APPEXECFWK_PARCEL_ERROR;
76 }
77 if (!data.WriteParcelable(&want)) {
78 HILOG_ERROR("write want failed");
79 return ERR_APPEXECFWK_PARCEL_ERROR;
80 }
81
82 if (!data.WriteRemoteObject(callerToken)) {
83 HILOG_ERROR("write callerToken failed");
84 return ERR_APPEXECFWK_PARCEL_ERROR;
85 }
86
87 int error = SendTransactCmd(
88 IFormRender::Message::FORM_RENDER_STOP_RENDERING_FORM,
89 data,
90 reply,
91 option);
92 if (error != ERR_OK) {
93 HILOG_ERROR("SendRequest:%{public}d failed", error);
94 return error;
95 }
96 return ERR_OK;
97 }
98
CleanFormHost(const sptr<IRemoteObject> & hostToken)99 int32_t FormRenderProxy::CleanFormHost(const sptr<IRemoteObject> &hostToken)
100 {
101 MessageParcel data;
102 MessageParcel reply;
103 MessageOption option(MessageOption::TF_ASYNC);
104
105 if (!WriteInterfaceToken(data)) {
106 HILOG_ERROR("write interface token failed");
107 return ERR_APPEXECFWK_PARCEL_ERROR;
108 }
109
110 if (!data.WriteRemoteObject(hostToken)) {
111 HILOG_ERROR("fail write hostToken");
112 return ERR_APPEXECFWK_PARCEL_ERROR;
113 }
114
115 int error = SendTransactCmd(
116 IFormRender::Message::FORM_RENDER_FORM_HOST_DIED,
117 data,
118 reply,
119 option);
120 if (error != ERR_OK) {
121 HILOG_ERROR("SendRequest:%{public}d failed", error);
122 return error;
123 }
124 return ERR_OK;
125 }
126
WriteInterfaceToken(MessageParcel & data)127 bool FormRenderProxy::WriteInterfaceToken(MessageParcel &data)
128 {
129 if (!data.WriteInterfaceToken(FormRenderProxy::GetDescriptor())) {
130 HILOG_ERROR("write interface token failed");
131 return false;
132 }
133 return true;
134 }
135
ReleaseRenderer(int64_t formId,const std::string & compId,const std::string & uid,const Want & want)136 int32_t FormRenderProxy::ReleaseRenderer(
137 int64_t formId, const std::string &compId, const std::string &uid, const Want &want)
138 {
139 MessageParcel data;
140 MessageParcel reply;
141 MessageOption option(MessageOption::TF_ASYNC);
142
143 if (!WriteInterfaceToken(data)) {
144 HILOG_ERROR("error to write interface token");
145 return ERR_APPEXECFWK_PARCEL_ERROR;
146 }
147 if (!data.WriteInt64(formId)) {
148 HILOG_ERROR("write formId failed");
149 return ERR_APPEXECFWK_PARCEL_ERROR;
150 }
151 if (!data.WriteString(compId)) {
152 HILOG_ERROR("write compId failed");
153 return ERR_APPEXECFWK_PARCEL_ERROR;
154 }
155 if (!data.WriteString(uid)) {
156 HILOG_ERROR("fail write uid");
157 return ERR_APPEXECFWK_PARCEL_ERROR;
158 }
159 if (!data.WriteParcelable(&want)) {
160 HILOG_ERROR("write want failed");
161 return ERR_APPEXECFWK_PARCEL_ERROR;
162 }
163 int error = SendTransactCmd(
164 IFormRender::Message::FORM_RENDER_RELEASE_RENDERER,
165 data,
166 reply,
167 option);
168 if (error != ERR_OK) {
169 HILOG_ERROR("SendRequest:%{public}d failed", error);
170 return error;
171 }
172 return ERR_OK;
173 }
174
ReloadForm(const std::vector<FormJsInfo> && formJsInfos,const Want & want)175 int32_t FormRenderProxy::ReloadForm(const std::vector<FormJsInfo> &&formJsInfos, const Want &want)
176 {
177 MessageParcel data;
178 MessageParcel reply;
179 MessageOption option(MessageOption::TF_ASYNC);
180 if (!WriteInterfaceToken(data)) {
181 HILOG_ERROR("write interface token failed");
182 return ERR_APPEXECFWK_PARCEL_ERROR;
183 }
184
185 int32_t error = WriteParcelableVector<FormJsInfo>(formJsInfos, data);
186 if (error != ERR_OK) {
187 HILOG_ERROR("fail WriteParcelableVector<FormJsInfo>");
188 return ERR_APPEXECFWK_PARCEL_ERROR;
189 }
190
191 if (!data.WriteParcelable(&want)) {
192 HILOG_ERROR("write want failed");
193 return ERR_APPEXECFWK_PARCEL_ERROR;
194 }
195
196 error = SendTransactCmd(
197 IFormRender::Message::FORM_RENDER_RELOAD_FORM,
198 data,
199 reply,
200 option);
201 if (error != ERR_OK) {
202 HILOG_ERROR("SendRequest:%{public}d failed", error);
203 return error;
204 }
205
206 return ERR_OK;
207 }
208
OnUnlock()209 int32_t FormRenderProxy::OnUnlock()
210 {
211 MessageParcel data;
212 MessageParcel reply;
213 MessageOption option(MessageOption::TF_ASYNC);
214 if (!WriteInterfaceToken(data)) {
215 HILOG_ERROR("write interface token failed");
216 return ERR_APPEXECFWK_PARCEL_ERROR;
217 }
218
219 int32_t error = SendTransactCmd(
220 IFormRender::Message::FORM_RENDER_UNLOCKED,
221 data,
222 reply,
223 option);
224 if (error != ERR_OK) {
225 HILOG_ERROR("SendRequest:%{public}d failed", error);
226 return error;
227 }
228
229 return ERR_OK;
230 }
231
SetVisibleChange(const int64_t & formId,bool isVisible,const Want & want)232 int32_t FormRenderProxy::SetVisibleChange(const int64_t &formId, bool isVisible, const Want &want)
233 {
234 MessageParcel data;
235 MessageParcel reply;
236 MessageOption option(MessageOption::TF_ASYNC);
237
238 if (!WriteInterfaceToken(data)) {
239 HILOG_ERROR("error to write interface token formId: %{public}" PRId64, formId);
240 return ERR_APPEXECFWK_PARCEL_ERROR;
241 }
242
243 if (!data.WriteInt64(formId)) {
244 HILOG_ERROR("write formId failed formId: %{public}" PRId64, formId);
245 return ERR_APPEXECFWK_PARCEL_ERROR;
246 }
247 if (!data.WriteBool(isVisible)) {
248 HILOG_ERROR("write isVisible failed formId: %{public}" PRId64, formId);
249 return ERR_APPEXECFWK_PARCEL_ERROR;
250 }
251
252 if (!data.WriteParcelable(&want)) {
253 HILOG_ERROR("write want failed formId: %{public}" PRId64, formId);
254 return ERR_APPEXECFWK_PARCEL_ERROR;
255 }
256
257 int32_t error = SendTransactCmd(
258 IFormRender::Message::FORM_SET_VISIBLE_CHANGE, data, reply, option);
259 if (error != ERR_OK) {
260 HILOG_ERROR("SendRequest:%{public}d failed formId: %{public}" PRId64, error, formId);
261 return error;
262 }
263 return ERR_OK;
264 }
265
SendTransactCmd(IFormRender::Message code,MessageParcel & data,MessageParcel & reply,MessageOption & option)266 int FormRenderProxy::SendTransactCmd(IFormRender::Message code, MessageParcel &data,
267 MessageParcel &reply, MessageOption &option)
268 {
269 sptr<IRemoteObject> remote = Remote();
270 if (!remote) {
271 HILOG_ERROR("error to get remote object, cmd:%{public}d", code);
272 return ERR_APPEXECFWK_SERVICE_NOT_CONNECTED;
273 }
274 int32_t result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
275 if (result != ERR_OK) {
276 HILOG_ERROR("error to SendRequest:%{public}d, cmd:%{public}d", result, code);
277 return result;
278 }
279 return ERR_OK;
280 }
281
282 template<typename T>
WriteParcelableVector(const std::vector<T> & parcelableVector,MessageParcel & reply)283 int32_t FormRenderProxy::WriteParcelableVector(const std::vector<T> &parcelableVector, MessageParcel &reply)
284 {
285 if (!reply.WriteInt32(parcelableVector.size())) {
286 HILOG_ERROR("write ParcelableVector size failed");
287 return ERR_APPEXECFWK_PARCEL_ERROR;
288 }
289
290 for (auto &parcelable : parcelableVector) {
291 if (!reply.WriteParcelable(&parcelable)) {
292 HILOG_ERROR("write ParcelableVector failed");
293 return ERR_APPEXECFWK_PARCEL_ERROR;
294 }
295 }
296 return ERR_OK;
297 }
298
RecycleForm(const int64_t & formId,const Want & want)299 int32_t FormRenderProxy::RecycleForm(const int64_t &formId, const Want &want)
300 {
301 MessageParcel data;
302 MessageOption option(MessageOption::TF_SYNC);
303 if (!WriteInterfaceToken(data)) {
304 HILOG_ERROR("write interface token failed");
305 return ERR_APPEXECFWK_PARCEL_ERROR;
306 }
307 if (!data.WriteInt64(formId)) {
308 HILOG_ERROR("write formId failed");
309 return ERR_APPEXECFWK_PARCEL_ERROR;
310 }
311 if (!data.WriteParcelable(&want)) {
312 HILOG_ERROR("write want failed");
313 return ERR_APPEXECFWK_PARCEL_ERROR;
314 }
315
316 MessageParcel reply;
317 int32_t error = SendTransactCmd(
318 IFormRender::Message::FORM_RECYCLE_FORM,
319 data,
320 reply,
321 option);
322 if (error != ERR_OK) {
323 HILOG_ERROR("SendRequest:%{public}d failed", error);
324 return error;
325 }
326
327 return reply.ReadInt32();
328 }
329
RecoverForm(const FormJsInfo & formJsInfo,const Want & want)330 int32_t FormRenderProxy::RecoverForm(const FormJsInfo &formJsInfo, const Want &want)
331 {
332 MessageParcel data;
333 MessageOption option(MessageOption::TF_SYNC);
334 HILOG_INFO("begin");
335 if (!WriteInterfaceToken(data)) {
336 HILOG_ERROR("write interface token failed");
337 return ERR_APPEXECFWK_PARCEL_ERROR;
338 }
339 if (!data.WriteParcelable(&formJsInfo)) {
340 HILOG_ERROR("fail write formJsInfo");
341 return ERR_APPEXECFWK_PARCEL_ERROR;
342 }
343 if (!data.WriteParcelable(&want)) {
344 HILOG_ERROR("write want failed");
345 return ERR_APPEXECFWK_PARCEL_ERROR;
346 }
347
348 MessageParcel reply;
349 int32_t error = SendTransactCmd(
350 IFormRender::Message::FORM_RECOVER_FORM,
351 data,
352 reply,
353 option);
354 if (error != ERR_OK) {
355 HILOG_ERROR("SendRequest:%{public}d failed", error);
356 return error;
357 }
358 HILOG_INFO("end");
359 return reply.ReadInt32();
360 }
361
RunCachedConfigurationUpdated()362 void FormRenderProxy::RunCachedConfigurationUpdated()
363 {
364 MessageParcel data;
365 MessageOption option(MessageOption::TF_ASYNC);
366 if (!WriteInterfaceToken(data)) {
367 HILOG_ERROR("write interface token failed");
368 return;
369 }
370 if (!Remote()) {
371 HILOG_ERROR("null remoteObj");
372 return;
373 }
374
375 MessageParcel reply;
376 int32_t error = SendTransactCmd(
377 IFormRender::Message::FORM_RUN_CACHED_CONFIG,
378 data,
379 reply,
380 option);
381 if (error != ERR_OK) {
382 HILOG_ERROR("SendRequest:%{public}d failed", error);
383 }
384 }
385
UpdateFormSize(const int64_t & formId,float width,float height,float borderWidth,const std::string & uid)386 int32_t FormRenderProxy::UpdateFormSize(const int64_t &formId, float width, float height, float borderWidth,
387 const std::string &uid)
388 {
389 MessageParcel data;
390 MessageOption option(MessageOption::TF_ASYNC);
391 if (!WriteInterfaceToken(data)) {
392 HILOG_ERROR("write interface token failed");
393 return ERR_APPEXECFWK_PARCEL_ERROR;
394 }
395 if (!data.WriteInt64(formId)) {
396 HILOG_ERROR("write formId failed");
397 return ERR_APPEXECFWK_PARCEL_ERROR;
398 }
399 if (!data.WriteFloat(width)) {
400 HILOG_ERROR("fail write width");
401 return ERR_APPEXECFWK_PARCEL_ERROR;
402 }
403 if (!data.WriteFloat(height)) {
404 HILOG_ERROR("fail write height");
405 return ERR_APPEXECFWK_PARCEL_ERROR;
406 }
407 if (!data.WriteFloat(borderWidth)) {
408 HILOG_ERROR("fail write borderWidth");
409 return ERR_APPEXECFWK_PARCEL_ERROR;
410 }
411 if (!data.WriteString(uid)) {
412 HILOG_ERROR("fail write uid");
413 return ERR_APPEXECFWK_PARCEL_ERROR;
414 }
415 MessageParcel reply;
416 int32_t error = SendTransactCmd(
417 IFormRender::Message::FORM_UPDATE_FORM_SIZE,
418 data,
419 reply,
420 option);
421 if (error != ERR_OK) {
422 HILOG_ERROR("SendRequest:%{public}d failed", error);
423 return error;
424 }
425
426 return ERR_OK;
427 }
428 } // namespace AppExecFwk
429 } // namespace OHOS
430