1 /*
2 * Copyright (c) 2025 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/form_host_task_mgr.h"
17
18 #include "form_host_interface.h"
19 #include "form_mgr_errors.h"
20 #include "fms_log_wrapper.h"
21 #include "form_host/form_host_queue.h"
22 #include "data_center/form_data_mgr.h"
23 #include "form_provider/form_supply_callback.h"
24
25 namespace OHOS {
26 namespace AppExecFwk {
FormHostTaskMgr()27 FormHostTaskMgr::FormHostTaskMgr() {}
28
~FormHostTaskMgr()29 FormHostTaskMgr::~FormHostTaskMgr() {}
30
31 /**
32 * @brief Post form data to form host(task) when acquire form.
33 * @param formId The Id of the form.
34 * @param callingUid Calling uid.
35 * @param info Form configure info.
36 * @param wantParams WantParams of the request.
37 * @param remoteObject Form provider proxy object.
38 */
PostAcquireTaskToHost(const int64_t formId,const FormRecord & record,const sptr<IRemoteObject> & remoteObject)39 void FormHostTaskMgr::PostAcquireTaskToHost(const int64_t formId, const FormRecord &record,
40 const sptr<IRemoteObject> &remoteObject)
41 {
42 HILOG_DEBUG("call");
43
44 auto acquireTaskToHostFunc = [formId, record, remoteObject]() {
45 FormHostTaskMgr::GetInstance().AcquireTaskToHost(formId, record, remoteObject);
46 };
47 FormHostQueue::GetInstance().ScheduleTask(FORM_TASK_DELAY_TIME, acquireTaskToHostFunc);
48 }
49
50 /**
51 * @brief Post form data to form host(task) when update form.
52 * @param formId The Id of the form.
53 * @param callingUid Calling uid.
54 * @param info Form configure info.
55 * @param wantParams WantParams of the request.
56 * @param remoteObject Form provider proxy object.
57 */
PostUpdateTaskToHost(const int64_t formId,const FormRecord & record,const sptr<IRemoteObject> & remoteObject)58 void FormHostTaskMgr::PostUpdateTaskToHost(const int64_t formId, const FormRecord &record,
59 const sptr<IRemoteObject> &remoteObject)
60 {
61 HILOG_DEBUG("call");
62
63 auto updateTaskToHostFunc = [formId, record, remoteObject]() {
64 FormHostTaskMgr::GetInstance().UpdateTaskToHost(formId, record, remoteObject);
65 };
66 FormHostQueue::GetInstance().ScheduleTask(FORM_TASK_DELAY_TIME, updateTaskToHostFunc);
67 }
68
69 /**
70 * @brief Post uninstall message to form host(task).
71 * @param formIds The Id list of the forms.
72 * @param remoteObject Form provider proxy object.
73 */
PostUninstallTaskToHost(const std::vector<int64_t> & formIds,const sptr<IRemoteObject> & remoteObject)74 void FormHostTaskMgr::PostUninstallTaskToHost(const std::vector<int64_t> &formIds,
75 const sptr<IRemoteObject> &remoteObject)
76 {
77 HILOG_DEBUG("call");
78
79 auto uninstallFunc = [formIds, remoteObject]() {
80 FormHostTaskMgr::GetInstance().FormUninstall(formIds, remoteObject);
81 };
82 FormHostQueue::GetInstance().ScheduleTask(FORM_TASK_DELAY_TIME, uninstallFunc);
83 }
84
85 /**
86 * @brief Post acquire form state message to form host(task).
87 * @param state The form state.
88 * @param want The want of onAcquireFormState.
89 * @param remoteObject Form provider proxy object.
90 */
PostAcquireStateTaskToHost(AppExecFwk::FormState state,const AAFwk::Want & want,const sptr<IRemoteObject> & remoteObject)91 void FormHostTaskMgr::PostAcquireStateTaskToHost(AppExecFwk::FormState state, const AAFwk::Want &want,
92 const sptr<IRemoteObject> &remoteObject)
93 {
94 HILOG_DEBUG("call");
95
96 auto acquireStateFunc = [state, want, remoteObject]() {
97 FormHostTaskMgr::GetInstance().AcquireStateBack(state, want, remoteObject);
98 };
99 FormHostQueue::GetInstance().ScheduleTask(FORM_TASK_DELAY_TIME, acquireStateFunc);
100 }
101
102 /**
103 * @brief Post acquire form data message to form host(task).
104 * @param wantParams Indicates the data information acquired by the form.
105 * @param requestCode Indicates the requested id.
106 * @param remoteObject Form provider proxy object.
107 */
PostAcquireDataTaskToHost(const AAFwk::WantParams & wantParams,int64_t requestCode,const sptr<IRemoteObject> & remoteObject)108 void FormHostTaskMgr::PostAcquireDataTaskToHost(const AAFwk::WantParams &wantParams,
109 int64_t requestCode, const sptr<IRemoteObject> &remoteObject)
110 {
111 HILOG_DEBUG("call");
112
113 auto acquireTaskToHostFunc = [wantParams, requestCode, remoteObject]() {
114 FormHostTaskMgr::GetInstance().AcquireFormDataBack(wantParams, requestCode, remoteObject);
115 };
116 FormHostQueue::GetInstance().ScheduleTask(FORM_TASK_DELAY_TIME, acquireTaskToHostFunc);
117 }
118
119 /**
120 * @brief Post enable form task.
121 * @param formIds formIds The Id list of the forms.
122 * @param enable True or false.
123 * @param remoteObject Form host proxy object.
124 */
PostEnableFormsTaskToHost(const std::vector<int64_t> & formIds,const bool enable,const sptr<IRemoteObject> & remoteObject)125 void FormHostTaskMgr::PostEnableFormsTaskToHost(const std::vector<int64_t> &formIds, const bool enable,
126 const sptr<IRemoteObject> &remoteObject)
127 {
128 HILOG_DEBUG("call");
129
130 auto enableFormsTaskToHostFunc = [formIds, enable, remoteObject]() {
131 FormHostTaskMgr::GetInstance().EnableFormsTaskToHost(formIds, enable, remoteObject);
132 };
133 FormHostQueue::GetInstance().ScheduleTask(FORM_TASK_DELAY_TIME, enableFormsTaskToHostFunc);
134 }
135
136 /**
137 * @brief Post lock form task.
138 * @param formIds formIds The Id list of the forms.
139 * @param lock True or false.
140 * @param remoteObject Form host proxy object.
141 */
PostLockFormsTaskToHost(const std::vector<int64_t> & formIds,const bool lock,const sptr<IRemoteObject> & remoteObject)142 void FormHostTaskMgr::PostLockFormsTaskToHost(const std::vector<int64_t> &formIds, const bool lock,
143 const sptr<IRemoteObject> &remoteObject)
144 {
145 HILOG_DEBUG("call");
146
147 auto lockFormsTaskToHostFunc = [formIds, lock, remoteObject]() {
148 FormHostTaskMgr::GetInstance().LockFormsTaskToHost(formIds, lock, remoteObject);
149 };
150 FormHostQueue::GetInstance().ScheduleTask(FORM_TASK_DELAY_TIME, lockFormsTaskToHostFunc);
151 }
152
153 /**
154 * @brief Post re-add form task to form host when FormRenderService is died.
155 * @param remoteObject Form host proxy object.
156 */
PostFrsDiedTaskToHost(const sptr<IRemoteObject> & remoteObject)157 void FormHostTaskMgr::PostFrsDiedTaskToHost(const sptr<IRemoteObject> &remoteObject)
158 {
159 HILOG_DEBUG("call");
160
161 auto task = [remoteObject]() {
162 FormHostTaskMgr::GetInstance().FrsDiedTaskToHost(remoteObject);
163 };
164 FormHostQueue::GetInstance().ScheduleTask(FORM_FRS_DIED_TASK_DELAY_TIME, task);
165 }
166
167 /**
168 * @brief Post re-add form task to form host when FormRenderService is died.
169 * @param formId The Id of the form.
170 * @param errorCode Indicates error-code of the form.
171 */
PostConnectFRSFailedTaskToHost(int64_t formId,int32_t errorCode)172 void FormHostTaskMgr::PostConnectFRSFailedTaskToHost(int64_t formId, int32_t errorCode)
173 {
174 auto task = [formId, errorCode]() {
175 FormHostTaskMgr::GetInstance().ConnectFRSFailedTaskToHost(formId, errorCode);
176 };
177 const std::pair<int64_t, int64_t> eventMsg = std::make_pair((int64_t)TaskType::RENDER_FORM, formId);
178 FormHostQueue::GetInstance().CancelDelayTask(eventMsg);
179 FormHostQueue::GetInstance().ScheduleDelayTask(eventMsg, FORM_FRS_DIED_TASK_DELAY_TIME, task);
180 }
181
182 /**
183 * @brief Handle form host died(task).
184 * @param remoteHost Form host proxy object.
185 */
PostHostDiedTask(const sptr<IRemoteObject> & remoteHost)186 void FormHostTaskMgr::PostHostDiedTask(const sptr<IRemoteObject> &remoteHost)
187 {
188 HILOG_DEBUG("call");
189
190 auto postTaskFunc = [remoteHost]() {
191 FormHostTaskMgr::GetInstance().HostDied(remoteHost);
192 };
193 FormHostQueue::GetInstance().ScheduleTask(FORM_TASK_DELAY_TIME, postTaskFunc);
194 }
195
196 /**
197 * @brief Post form data to form host when acquire form.
198 * @param formId The Id of the form.
199 * @param callingUid Calling uid.
200 * @param info Form configure info.
201 * @param wantParams WantParams of the request.
202 * @param remoteObject Form provider proxy object.
203 */
AcquireTaskToHost(const int64_t formId,const FormRecord & record,const sptr<IRemoteObject> & remoteObject)204 void FormHostTaskMgr::AcquireTaskToHost(const int64_t formId, const FormRecord &record,
205 const sptr<IRemoteObject> &remoteObject)
206 {
207 HILOG_INFO("formId:%{public}" PRId64, formId);
208
209 sptr<IFormHost> remoteFormHost = iface_cast<IFormHost>(remoteObject);
210 if (remoteFormHost == nullptr) {
211 HILOG_ERROR("get formHostProxy failed");
212 return;
213 }
214
215 FormJsInfo formJsInfo;
216 FormDataMgr::GetInstance().CreateFormJsInfo(formId, record, formJsInfo);
217 HILOG_DEBUG("FormTaskMgr remoteFormHost OnAcquired");
218 remoteFormHost->OnAcquired(formJsInfo, nullptr);
219 }
220
221 /**
222 * @brief Post form data to form host when update form.
223 * @param formId The Id of the form.
224 * @param callingUid Calling uid.
225 * @param info Form configure info.
226 * @param wantParams WantParams of the request.
227 * @param remoteObject Form provider proxy object.
228 */
UpdateTaskToHost(const int64_t formId,const FormRecord & record,const sptr<IRemoteObject> & remoteObject)229 void FormHostTaskMgr::UpdateTaskToHost(const int64_t formId, const FormRecord &record,
230 const sptr<IRemoteObject> &remoteObject)
231 {
232 HILOG_DEBUG("start");
233
234 sptr<IFormHost> remoteFormHost = iface_cast<IFormHost>(remoteObject);
235 if (remoteFormHost == nullptr) {
236 HILOG_ERROR("get formHostProxy failed");
237 return;
238 }
239
240 FormJsInfo formJsInfo;
241 FormDataMgr::GetInstance().CreateFormJsInfo(formId, record, formJsInfo);
242 remoteFormHost->OnUpdate(formJsInfo);
243 HILOG_DEBUG("end");
244 }
245
246 /**
247 * @brief Handle uninstall message.
248 * @param formIds The Id list of the forms.
249 * @param remoteObject Form provider proxy object.
250 */
FormUninstall(const std::vector<int64_t> & formIds,const sptr<IRemoteObject> & remoteObject)251 void FormHostTaskMgr::FormUninstall(const std::vector<int64_t> &formIds,
252 const sptr<IRemoteObject> &remoteObject)
253 {
254 HILOG_DEBUG("start");
255
256 sptr<IFormHost> remoteFormHost = iface_cast<IFormHost>(remoteObject);
257 if (remoteFormHost == nullptr) {
258 HILOG_ERROR("get formHostProxy failed");
259 return;
260 }
261
262 remoteFormHost->OnUninstall(formIds);
263 HILOG_DEBUG("end");
264 }
265
266 /**
267 * @brief Handle acquire state.
268 * @param state the form state.
269 * @param want The want of onAcquireFormState.
270 * @param remoteObject Form provider proxy object.
271 */
AcquireStateBack(AppExecFwk::FormState state,const AAFwk::Want & want,const sptr<IRemoteObject> & remoteObject)272 void FormHostTaskMgr::AcquireStateBack(AppExecFwk::FormState state, const AAFwk::Want &want,
273 const sptr<IRemoteObject> &remoteObject)
274 {
275 HILOG_DEBUG("start");
276
277 sptr<IFormHost> remoteFormHost = iface_cast<IFormHost>(remoteObject);
278 if (remoteFormHost == nullptr) {
279 HILOG_ERROR("get formHostProxy failed");
280 return;
281 }
282
283 remoteFormHost->OnAcquireState(state, want);
284 HILOG_DEBUG("end");
285 }
286
287 /**
288 * @brief Handle acquire data.
289 * @param wantParams Indicates the data information acquired by the form.
290 * @param requestCode Indicates the requested id.
291 * @param remoteObject Form provider proxy object.
292 */
AcquireFormDataBack(const AAFwk::WantParams & wantParams,int64_t requestCode,const sptr<IRemoteObject> & remoteObject)293 void FormHostTaskMgr::AcquireFormDataBack(const AAFwk::WantParams &wantParams,
294 int64_t requestCode, const sptr<IRemoteObject> &remoteObject)
295 {
296 HILOG_DEBUG("start");
297
298 sptr<IFormHost> remoteFormHost = iface_cast<IFormHost>(remoteObject);
299 if (remoteFormHost == nullptr) {
300 HILOG_ERROR("get formHostProxy failed");
301 return;
302 }
303
304 remoteFormHost->OnAcquireDataResponse(wantParams, requestCode);
305 HILOG_DEBUG("end");
306 }
307
EnableFormsTaskToHost(const std::vector<int64_t> & formIds,const bool enable,const sptr<IRemoteObject> & remoteObject)308 void FormHostTaskMgr::EnableFormsTaskToHost(const std::vector<int64_t> &formIds, const bool enable,
309 const sptr<IRemoteObject> &remoteObject)
310 {
311 HILOG_DEBUG("start");
312
313 sptr<IFormHost> remoteFormHost = iface_cast<IFormHost>(remoteObject);
314 if (remoteFormHost == nullptr) {
315 HILOG_ERROR("get formHostProxy failed");
316 return;
317 }
318
319 remoteFormHost->OnEnableForm(formIds, enable);
320 HILOG_DEBUG("end");
321 }
322
LockFormsTaskToHost(const std::vector<int64_t> & formIds,const bool lock,const sptr<IRemoteObject> & remoteObject)323 void FormHostTaskMgr::LockFormsTaskToHost(const std::vector<int64_t> &formIds, const bool lock,
324 const sptr<IRemoteObject> &remoteObject)
325 {
326 HILOG_DEBUG("start");
327
328 HILOG_INFO("LockFormsTaskToHost start");
329 sptr<IFormHost> remoteFormHost = iface_cast<IFormHost>(remoteObject);
330 if (remoteFormHost == nullptr) {
331 HILOG_ERROR("get formHostProxy failed");
332 return;
333 }
334
335 remoteFormHost->OnLockForm(formIds, lock);
336 HILOG_DEBUG("end");
337 }
338
FrsDiedTaskToHost(const sptr<IRemoteObject> & remoteObject)339 void FormHostTaskMgr::FrsDiedTaskToHost(const sptr<IRemoteObject> &remoteObject)
340 {
341 HILOG_DEBUG("start");
342
343 sptr<IFormHost> remoteFormHost = iface_cast<IFormHost>(remoteObject);
344 if (remoteFormHost == nullptr) {
345 HILOG_ERROR("get formHostProxy failed");
346 return;
347 }
348
349 remoteFormHost->OnError(ERR_APPEXECFWK_FORM_RENDER_SERVICE_DIED, "FormRenderService is dead.");
350 HILOG_DEBUG("end");
351 }
352
ConnectFRSFailedTaskToHost(int64_t formId,int32_t errorCode)353 void FormHostTaskMgr::ConnectFRSFailedTaskToHost(int64_t formId, int32_t errorCode)
354 {
355 HILOG_WARN("formId:%{public}" PRId64 ", errorCode:%{public}d", formId, errorCode);
356 std::vector<int64_t> formIds{formId};
357
358 std::vector<sptr<IRemoteObject>> formHostObjs;
359 FormDataMgr::GetInstance().GetFormHostRemoteObj(formId, formHostObjs);
360 for (const auto &host : formHostObjs) {
361 auto hostClient = iface_cast<IFormHost>(host);
362 if (hostClient == nullptr) {
363 HILOG_ERROR("null hostClient");
364 continue;
365 }
366 hostClient->OnError(errorCode, "Connect FormRenderService failed", formIds);
367 }
368 }
369
HostDied(const sptr<IRemoteObject> & remoteHost)370 void FormHostTaskMgr::HostDied(const sptr<IRemoteObject> &remoteHost)
371 {
372 HILOG_INFO("remote client died event");
373
374 if (remoteHost == nullptr) {
375 HILOG_ERROR("remote client died, invalid param");
376 return;
377 }
378 FormDataMgr::GetInstance().HandleHostDied(remoteHost);
379 FormSupplyCallback::GetInstance()->HandleHostDied(remoteHost);
380 }
381 } // namespace AppExecFwk
382 } // namespace OHOS