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_stub.h"
16 #include "appexecfwk_errors.h"
17 #include "app_scheduler_interface.h"
18 #include "errors.h"
19 #include "fms_log_wrapper.h"
20 #include "form_mgr_errors.h"
21 #include "ipc_skeleton.h"
22 #include "ipc_types.h"
23 #include "iremote_object.h"
24 #include "xcollie/watchdog.h"
25 #include "xcollie/xcollie.h"
26 #include "xcollie/xcollie_define.h"
27
28 namespace OHOS {
29 namespace AppExecFwk {
30 namespace {
31 constexpr int32_t MAX_ALLOW_SIZE = 8 * 1024;
32 constexpr int32_t FORM_RENDER_API_TIME_OUT = 30;
33 }
34
FormRenderStub()35 FormRenderStub::FormRenderStub()
36 {}
37
~FormRenderStub()38 FormRenderStub::~FormRenderStub()
39 {}
40
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)41 int FormRenderStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
42 {
43 HILOG_DEBUG("FormRenderStub::OnReceived,code = %{public}u,flags= %{public}d", code, option.GetFlags());
44 std::u16string descriptor = FormRenderStub::GetDescriptor();
45 std::u16string remoteDescriptor = data.ReadInterfaceToken();
46 if (descriptor != remoteDescriptor) {
47 HILOG_ERROR("localDescriptor not equal to remote");
48 return ERR_APPEXECFWK_FORM_INVALID_PARAM;
49 }
50
51 switch (code) {
52 case static_cast<uint32_t>(IFormRender::Message::FORM_RENDER_RENDER_FORM):
53 return HandleRenderForm(data, reply);
54 case static_cast<uint32_t>(IFormRender::Message::FORM_RENDER_STOP_RENDERING_FORM):
55 return HandleStopRenderingForm(data, reply);
56 case static_cast<uint32_t>(IFormRender::Message::FORM_RENDER_FORM_HOST_DIED):
57 return HandleCleanFormHost(data, reply);
58 case static_cast<uint32_t>(IFormRender::Message::FORM_RENDER_RELOAD_FORM):
59 return HandleReloadForm(data, reply);
60 case static_cast<uint32_t>(IFormRender::Message::FORM_RENDER_RELEASE_RENDERER):
61 return HandleReleaseRenderer(data, reply);
62 case static_cast<uint32_t>(IFormRender::Message::FORM_RENDER_UNLOCKED):
63 return HandleOnUnlock(data, reply);
64 case static_cast<uint32_t>(IFormRender::Message::FORM_RECYCLE_FORM):
65 return HandleRecycleForm(data, reply);
66 case static_cast<uint32_t>(IFormRender::Message::FORM_RECOVER_FORM):
67 return HandleRecoverForm(data, reply);
68 case static_cast<uint32_t>(IFormRender::Message::FORM_RUN_CACHED_CONFIG):{
69 int timerId = HiviewDFX::XCollie::GetInstance().SetTimer("FRS_RunCachedConfigurationUpdated",
70 FORM_RENDER_API_TIME_OUT, nullptr, nullptr, HiviewDFX::XCOLLIE_FLAG_LOG);
71 RunCachedConfigurationUpdated();
72 HiviewDFX::XCollie::GetInstance().CancelTimer(timerId);
73 return ERR_OK;
74 }
75 case static_cast<uint32_t>(IFormRender::Message::FORM_SET_VISIBLE_CHANGE):
76 return HandleSetVisibleChange(data, reply);
77 case static_cast<uint32_t>(IFormRender::Message::FORM_UPDATE_FORM_SIZE):
78 return HandleUpdateFormSize(data, reply);
79 default:
80 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
81 }
82 }
83
HandleRenderForm(MessageParcel & data,MessageParcel & reply)84 int FormRenderStub::HandleRenderForm(MessageParcel &data, MessageParcel &reply)
85 {
86 std::unique_ptr<FormJsInfo> formJsInfo(data.ReadParcelable<FormJsInfo>());
87 if (!formJsInfo) {
88 HILOG_ERROR("error to ReadParcelable<formJsInfo>");
89 return ERR_APPEXECFWK_PARCEL_ERROR;
90 }
91 std::unique_ptr<Want> want(data.ReadParcelable<Want>());
92 if (!want) {
93 HILOG_ERROR("error to ReadParcelable<Want>");
94 return ERR_APPEXECFWK_PARCEL_ERROR;
95 }
96
97 sptr<IRemoteObject> client = data.ReadRemoteObject();
98 if (client == nullptr) {
99 HILOG_ERROR("error to get remote object");
100 return ERR_APPEXECFWK_PARCEL_ERROR;
101 }
102 int timerId = HiviewDFX::XCollie::GetInstance().SetTimer("FRS_RenderForm",
103 FORM_RENDER_API_TIME_OUT, nullptr, nullptr, HiviewDFX::XCOLLIE_FLAG_LOG);
104 int32_t result = RenderForm(*formJsInfo, *want, client);
105 HiviewDFX::XCollie::GetInstance().CancelTimer(timerId);
106 reply.WriteInt32(result);
107 return result;
108 }
109
HandleStopRenderingForm(MessageParcel & data,MessageParcel & reply)110 int FormRenderStub::HandleStopRenderingForm(MessageParcel &data, MessageParcel &reply)
111 {
112 std::unique_ptr<FormJsInfo> formJsInfo(data.ReadParcelable<FormJsInfo>());
113 if (!formJsInfo) {
114 HILOG_ERROR("ReadParcelable<formJsInfo> fail");
115 return ERR_APPEXECFWK_PARCEL_ERROR;
116 }
117 std::unique_ptr<Want> want(data.ReadParcelable<Want>());
118 if (!want) {
119 HILOG_ERROR("ReadParcelable<Want> fail");
120 return ERR_APPEXECFWK_PARCEL_ERROR;
121 }
122
123 sptr<IRemoteObject> client = data.ReadRemoteObject();
124 if (client == nullptr) {
125 HILOG_ERROR("get remote object failed");
126 return ERR_APPEXECFWK_PARCEL_ERROR;
127 }
128 int timerId = HiviewDFX::XCollie::GetInstance().SetTimer("FRS_StopRenderingForm",
129 FORM_RENDER_API_TIME_OUT, nullptr, nullptr, HiviewDFX::XCOLLIE_FLAG_LOG);
130 int32_t result = StopRenderingForm(*formJsInfo, *want, client);
131 HiviewDFX::XCollie::GetInstance().CancelTimer(timerId);
132 reply.WriteInt32(result);
133 return result;
134 }
135
HandleCleanFormHost(MessageParcel & data,MessageParcel & reply)136 int FormRenderStub::HandleCleanFormHost(MessageParcel &data, MessageParcel &reply)
137 {
138 sptr<IRemoteObject> hostToken = data.ReadRemoteObject();
139 if (hostToken == nullptr) {
140 HILOG_ERROR("null hostToken");
141 return ERR_APPEXECFWK_PARCEL_ERROR;
142 }
143 int timerId = HiviewDFX::XCollie::GetInstance().SetTimer("FRS_CleanFormHost",
144 FORM_RENDER_API_TIME_OUT, nullptr, nullptr, HiviewDFX::XCOLLIE_FLAG_LOG);
145 int32_t result = CleanFormHost(hostToken);
146 HiviewDFX::XCollie::GetInstance().CancelTimer(timerId);
147 reply.WriteInt32(result);
148 return result;
149 }
150
HandleReleaseRenderer(MessageParcel & data,MessageParcel & reply)151 int32_t FormRenderStub::HandleReleaseRenderer(MessageParcel &data, MessageParcel &reply)
152 {
153 int64_t formId = data.ReadInt64();
154 std::string compId = data.ReadString();
155 std::string uid = data.ReadString();
156 int timerId = HiviewDFX::XCollie::GetInstance().SetTimer("FRS_ReleaseRenderer",
157 FORM_RENDER_API_TIME_OUT, nullptr, nullptr, HiviewDFX::XCOLLIE_FLAG_LOG);
158 int32_t result = ReleaseRenderer(formId, compId, uid);
159 HiviewDFX::XCollie::GetInstance().CancelTimer(timerId);
160 reply.WriteInt32(result);
161 return result;
162 }
163
HandleReloadForm(MessageParcel & data,MessageParcel & reply)164 int FormRenderStub::HandleReloadForm(MessageParcel &data, MessageParcel &reply)
165 {
166 std::vector<FormJsInfo> formJsInfos;
167 int32_t result = GetParcelableInfos(data, formJsInfos);
168 if (result != ERR_OK) {
169 HILOG_ERROR("fail GetParcelableInfos<FormJsInfo>");
170 return result;
171 }
172
173 std::unique_ptr<Want> want(data.ReadParcelable<Want>());
174 if (!want) {
175 HILOG_ERROR("ReadParcelable<Want> failed");
176 return ERR_APPEXECFWK_PARCEL_ERROR;
177 }
178 int timerId = HiviewDFX::XCollie::GetInstance().SetTimer("FRS_ReloadForm",
179 FORM_RENDER_API_TIME_OUT, nullptr, nullptr, HiviewDFX::XCOLLIE_FLAG_LOG);
180 result = ReloadForm(std::move(formJsInfos), *want);
181 HiviewDFX::XCollie::GetInstance().CancelTimer(timerId);
182 reply.WriteInt32(result);
183 return result;
184 }
185
HandleOnUnlock(MessageParcel & data,MessageParcel & reply)186 int32_t FormRenderStub::HandleOnUnlock(MessageParcel &data, MessageParcel &reply)
187 {
188 int timerId = HiviewDFX::XCollie::GetInstance().SetTimer("FRS_OnUnlock",
189 FORM_RENDER_API_TIME_OUT, nullptr, nullptr, HiviewDFX::XCOLLIE_FLAG_LOG);
190 int32_t result = OnUnlock();
191 HiviewDFX::XCollie::GetInstance().CancelTimer(timerId);
192 reply.WriteInt32(result);
193 return result;
194 }
195
HandleSetVisibleChange(MessageParcel & data,MessageParcel & reply)196 int32_t FormRenderStub::HandleSetVisibleChange(MessageParcel &data, MessageParcel &reply)
197 {
198 HILOG_ERROR("begin");
199 int64_t formId = data.ReadInt64();
200 bool isVisible = data.ReadBool();
201 std::unique_ptr<Want> want(data.ReadParcelable<Want>());
202 if (!want) {
203 HILOG_ERROR("error to ReadParcelable<Want>");
204 return ERR_APPEXECFWK_PARCEL_ERROR;
205 }
206 int timerId = HiviewDFX::XCollie::GetInstance().SetTimer("FRS_SetVisibleChange",
207 FORM_RENDER_API_TIME_OUT, nullptr, nullptr, HiviewDFX::XCOLLIE_FLAG_LOG);
208 int32_t result = SetVisibleChange(formId, isVisible, *want);
209 HiviewDFX::XCollie::GetInstance().CancelTimer(timerId);
210 reply.WriteInt32(result);
211 HILOG_ERROR("end");
212 return result;
213 }
214
215 template<typename T>
GetParcelableInfos(MessageParcel & reply,std::vector<T> & parcelableInfos)216 int32_t FormRenderStub::GetParcelableInfos(MessageParcel &reply, std::vector<T> &parcelableInfos)
217 {
218 int32_t infoSize = reply.ReadInt32();
219 if (infoSize < 0 || infoSize > MAX_ALLOW_SIZE) {
220 HILOG_ERROR("invalid size:%{public}d", infoSize);
221 return ERR_APPEXECFWK_PARCEL_ERROR;
222 }
223
224 for (int32_t i = 0; i < infoSize; i++) {
225 std::unique_ptr<T> info(reply.ReadParcelable<T>());
226 if (!info) {
227 HILOG_ERROR("Read Parcelable infos error");
228 return ERR_APPEXECFWK_PARCEL_ERROR;
229 }
230 parcelableInfos.emplace_back(*info);
231 }
232 return ERR_OK;
233 }
234
HandleRecycleForm(MessageParcel & data,MessageParcel & reply)235 int32_t FormRenderStub::HandleRecycleForm(MessageParcel &data, MessageParcel &reply)
236 {
237 int64_t formId = data.ReadInt64();
238 std::unique_ptr<Want> want(data.ReadParcelable<Want>());
239 if (!want) {
240 HILOG_ERROR("error to ReadParcelable<Want>");
241 return ERR_APPEXECFWK_PARCEL_ERROR;
242 }
243 int timerId = HiviewDFX::XCollie::GetInstance().SetTimer("FRS_RecycleForm",
244 FORM_RENDER_API_TIME_OUT, nullptr, nullptr, HiviewDFX::XCOLLIE_FLAG_LOG);
245 int32_t result = RecycleForm(formId, *want);
246 HiviewDFX::XCollie::GetInstance().CancelTimer(timerId);
247 reply.WriteInt32(result);
248 return result;
249 }
250
HandleRecoverForm(MessageParcel & data,MessageParcel & reply)251 int32_t FormRenderStub::HandleRecoverForm(MessageParcel &data, MessageParcel &reply)
252 {
253 HILOG_INFO("begin");
254 std::unique_ptr<FormJsInfo> formJsInfo(data.ReadParcelable<FormJsInfo>());
255 if (!formJsInfo) {
256 HILOG_ERROR("read FormJsInfo error");
257 return ERR_APPEXECFWK_PARCEL_ERROR;
258 }
259 std::unique_ptr<Want> want(data.ReadParcelable<Want>());
260 if (!want) {
261 HILOG_ERROR("read want error");
262 return ERR_APPEXECFWK_PARCEL_ERROR;
263 }
264 int timerId = HiviewDFX::XCollie::GetInstance().SetTimer("FRS_RecoverForm",
265 FORM_RENDER_API_TIME_OUT, nullptr, nullptr, HiviewDFX::XCOLLIE_FLAG_LOG);
266 int32_t result = RecoverForm(*formJsInfo, *want);
267 HiviewDFX::XCollie::GetInstance().CancelTimer(timerId);
268 reply.WriteInt32(result);
269 HILOG_INFO("end");
270 return result;
271 }
272
HandleUpdateFormSize(MessageParcel & data,MessageParcel & reply)273 int32_t FormRenderStub::HandleUpdateFormSize(MessageParcel &data, MessageParcel &reply)
274 {
275 int64_t formId = data.ReadInt64();
276 float width = data.ReadFloat();
277 float height = data.ReadFloat();
278 float borderWidth = data.ReadFloat();
279 std::string uid = data.ReadString();
280 int timerId = HiviewDFX::XCollie::GetInstance().SetTimer("FRS_UpdateFormSize",
281 FORM_RENDER_API_TIME_OUT, nullptr, nullptr, HiviewDFX::XCOLLIE_FLAG_LOG);
282 int32_t result = UpdateFormSize(formId, width, height, borderWidth, uid);
283 HiviewDFX::XCollie::GetInstance().CancelTimer(timerId);
284 reply.WriteInt32(result);
285 return result;
286 }
287 } // namespace AppExecFwk
288 } // namespace OHOS
289