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_render/form_render_task_mgr.h"
17
18 #include "form_render_interface.h"
19 #include "form_js_info.h"
20 #include "form_constants.h"
21 #include "fms_log_wrapper.h"
22 #include "form_render/form_render_queue.h"
23 #include "common/event/form_event_report.h"
24 #include "data_center/form_data_mgr.h"
25 #include "data_center/form_record/form_record.h"
26
27 namespace OHOS {
28 namespace AppExecFwk {
FormRenderTaskMgr()29 FormRenderTaskMgr::FormRenderTaskMgr() {}
30
~FormRenderTaskMgr()31 FormRenderTaskMgr::~FormRenderTaskMgr() {}
32
PostUpdateFormSize(const int64_t & formId,float width,float height,float borderWidth,const std::string & uid,const sptr<IRemoteObject> & remoteObject)33 void FormRenderTaskMgr::PostUpdateFormSize(const int64_t &formId, float width, float height, float borderWidth,
34 const std::string &uid, const sptr<IRemoteObject> &remoteObject)
35 {
36 HILOG_DEBUG("start");
37
38 auto updateFormSize = [formId, width, height, borderWidth, uid, remoteObject]() {
39 FormRenderTaskMgr::GetInstance().UpdateFormSize(formId, width, height, borderWidth, uid, remoteObject);
40 };
41 FormRenderQueue::GetInstance().ScheduleTask(FORM_TASK_DELAY_TIME, updateFormSize);
42 HILOG_DEBUG("end");
43 }
44
PostOnUnlock(const sptr<IRemoteObject> & remoteObject)45 void FormRenderTaskMgr::PostOnUnlock(const sptr<IRemoteObject> &remoteObject)
46 {
47 HILOG_DEBUG("call");
48
49 auto task = [remoteObject]() {
50 FormRenderTaskMgr::GetInstance().OnUnlock(remoteObject);
51 };
52 FormRenderQueue::GetInstance().ScheduleTask(FORM_TASK_DELAY_TIME, task);
53 HILOG_DEBUG("end");
54 }
55
PostSetVisibleChange(int64_t formId,bool isVisible,const sptr<IRemoteObject> & remoteObject)56 void FormRenderTaskMgr::PostSetVisibleChange(int64_t formId, bool isVisible, const sptr<IRemoteObject> &remoteObject)
57 {
58 HILOG_INFO("call");
59
60 auto task = [formId, isVisible, remoteObject]() {
61 FormRenderTaskMgr::GetInstance().SetVisibleChange(formId, isVisible, remoteObject);
62 };
63 FormRenderQueue::GetInstance().ScheduleTask(FORM_TASK_DELAY_TIME, task);
64 HILOG_INFO("start task formId: %{public}" PRId64 " isVisible: %{public}d", formId, isVisible);
65 }
66
PostReloadForm(const std::vector<FormRecord> && formRecords,const Want & want,const sptr<IRemoteObject> & remoteObject)67 void FormRenderTaskMgr::PostReloadForm(const std::vector<FormRecord> &&formRecords, const Want &want,
68 const sptr<IRemoteObject> &remoteObject)
69 {
70 HILOG_INFO("begin");
71
72 auto reloadForm = [forms = std::forward<decltype(formRecords)>(formRecords), want, remoteObject]() {
73 FormRenderTaskMgr::GetInstance().ReloadForm(std::move(forms), want, remoteObject);
74 };
75 FormRenderQueue::GetInstance().ScheduleTask(FORM_TASK_DELAY_TIME, reloadForm);
76 HILOG_INFO("end");
77 }
78
UpdateFormSize(const int64_t & formId,float width,float height,float borderWidth,const std::string & uid,const sptr<IRemoteObject> & remoteObject)79 void FormRenderTaskMgr::UpdateFormSize(const int64_t &formId, float width, float height, float borderWidth,
80 const std::string &uid, const sptr<IRemoteObject> &remoteObject)
81 {
82 HILOG_DEBUG("start");
83
84 sptr<IFormRender> remoteFormRender = iface_cast<IFormRender>(remoteObject);
85 if (remoteFormRender == nullptr) {
86 HILOG_ERROR("get formRenderProxy failed");
87 return;
88 }
89
90 int32_t error = remoteFormRender->UpdateFormSize(formId, width, height, borderWidth, uid);
91 if (error != ERR_OK) {
92 HILOG_ERROR("fail Update FormSize");
93 return;
94 }
95
96 HILOG_DEBUG("end");
97 }
98
OnUnlock(const sptr<IRemoteObject> & remoteObject)99 void FormRenderTaskMgr::OnUnlock(const sptr<IRemoteObject> &remoteObject)
100 {
101 HILOG_DEBUG("begin");
102
103 sptr<IFormRender> remoteFormRender = iface_cast<IFormRender>(remoteObject);
104 if (remoteFormRender == nullptr) {
105 HILOG_ERROR("get formRenderProxy failed");
106 return;
107 }
108 int32_t error = remoteFormRender->OnUnlock();
109 if (error != ERR_OK) {
110 HILOG_ERROR("fail");
111 return;
112 }
113 HILOG_DEBUG("end");
114 }
115
SetVisibleChange(int64_t formId,bool isVisible,const sptr<IRemoteObject> & remoteObject)116 void FormRenderTaskMgr::SetVisibleChange(int64_t formId, bool isVisible, const sptr<IRemoteObject> &remoteObject)
117 {
118 HILOG_INFO("begin");
119
120 sptr<IFormRender> remoteFormRender = iface_cast<IFormRender>(remoteObject);
121 if (remoteFormRender == nullptr) {
122 HILOG_ERROR("get formRenderProxy failed");
123 return;
124 }
125
126 FormRecord formRecord;
127 if (!FormDataMgr::GetInstance().GetFormRecord(formId, formRecord)) {
128 HILOG_ERROR("form %{public}" PRId64 " not exist", formId);
129 return;
130 }
131
132 Want want;
133 want.SetParam(Constants::FORM_SUPPLY_UID, std::to_string(formRecord.providerUserId) + formRecord.bundleName);
134
135 int32_t error = remoteFormRender->SetVisibleChange(formId, isVisible, want);
136 if (error != ERR_OK) {
137 HILOG_ERROR("fail");
138 return;
139 }
140 HILOG_INFO("end");
141 }
142
ReloadForm(const std::vector<FormRecord> && formRecords,const Want & want,const sptr<IRemoteObject> & remoteObject)143 void FormRenderTaskMgr::ReloadForm(const std::vector<FormRecord> &&formRecords, const Want &want,
144 const sptr<IRemoteObject> &remoteObject)
145 {
146 HILOG_INFO("begin");
147
148 sptr<IFormRender> remoteFormRender = iface_cast<IFormRender>(remoteObject);
149 if (remoteFormRender == nullptr) {
150 HILOG_ERROR("get formRenderProxy failed");
151 return;
152 }
153
154 std::string bundleName = want.GetStringParam(Constants::PARAM_BUNDLE_NAME_KEY);
155 if (bundleName.empty()) {
156 HILOG_ERROR("GetBundleName failed");
157 return;
158 }
159
160 std::vector<FormJsInfo> formJsInfos;
161 for (const auto &formRecord : formRecords) {
162 FormJsInfo formInfo;
163 FormDataMgr::GetInstance().CreateFormJsInfo(formRecord.formId, formRecord, formInfo);
164 formJsInfos.emplace_back(formInfo);
165 }
166
167 int32_t error = remoteFormRender->ReloadForm(std::move(formJsInfos), want);
168 if (error != ERR_OK) {
169 HILOG_ERROR("fail reload form");
170 FormEventReport::SendFormFailedEvent(FormEventName::RELOAD_FORM_FAILED,
171 0,
172 bundleName,
173 "",
174 static_cast<int32_t>(ReloadFormErrorType::RELOAD_FORM_FRS_DEAD),
175 error);
176 return;
177 }
178 HILOG_INFO("end");
179 }
180 } // namespace AppExecFwk
181 } // namespace OHOS