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
16 #include "form_renderer.h"
17
18 #include "form_renderer_hilog.h"
19
20 #include "base/utils/utils.h"
21
22 namespace OHOS {
23 namespace Ace {
24 namespace {
25 constexpr char FORM_RENDERER_ALLOW_UPDATE[] = "allowUpdate";
26 constexpr char FORM_RENDERER_DISPATCHER[] = "ohos.extra.param.key.process_on_form_renderer_dispatcher";
27 constexpr char FORM_RENDERER_PROCESS_ON_ADD_SURFACE[] = "ohos.extra.param.key.process_on_add_surface";
28 constexpr char TRANSPARENT_COLOR[] = "#00FFFFFF";
29 constexpr int32_t DOUBLE = 2;
30 constexpr int64_t DEFAULT_TIMESTAMP = -1;
31 constexpr int64_t DELAY_SET_DEFAULT_TIMESTAMP_TASK = 50; //ms
32 constexpr int64_t DELAY_RESIZE_FORM_AGAIN_TASK = 100; //ms
33 constexpr int32_t RSSURFACENODE_PROPERTIES_WIDTH_INDEX = 2;
34 constexpr int32_t RSSURFACENODE_PROPERTIES_HEIGHT_INDEX = 3;
35 } // namespace
36
37 using EventHandler = OHOS::AppExecFwk::EventHandler;
38
FormRenderer(const std::shared_ptr<OHOS::AbilityRuntime::Context> context,const std::shared_ptr<OHOS::AbilityRuntime::Runtime> runtime,std::weak_ptr<OHOS::AppExecFwk::EventHandler> eventHandler)39 FormRenderer::FormRenderer(const std::shared_ptr<OHOS::AbilityRuntime::Context> context,
40 const std::shared_ptr<OHOS::AbilityRuntime::Runtime> runtime,
41 std::weak_ptr<OHOS::AppExecFwk::EventHandler> eventHandler)
42 : context_(context), runtime_(runtime), eventHandler_(eventHandler)
43 {
44 HILOG_INFO("FormRenderer created.");
45 if (!context_ || !runtime_) {
46 return;
47 }
48 auto& nativeEngine = (static_cast<AbilityRuntime::JsRuntime&>(*runtime_.get())).GetNativeEngine();
49 uiContent_ = UIContent::Create(context_.get(), &nativeEngine, true);
50 }
51
~FormRenderer()52 FormRenderer::~FormRenderer()
53 {
54 HILOG_DEBUG("called");
55 }
56
PreInitUIContent(const OHOS::AAFwk::Want & want,const OHOS::AppExecFwk::FormJsInfo & formJsInfo)57 void FormRenderer::PreInitUIContent(const OHOS::AAFwk::Want& want, const OHOS::AppExecFwk::FormJsInfo& formJsInfo)
58 {
59 HILOG_INFO("InitUIContent width = %{public}f , height = %{public}f, borderWidth = %{public}f. \
60 formJsInfo.formData.size = %{public}zu. formJsInfo.imageDataMap.size = %{public}zu.",
61 width_, height_, borderWidth_,
62 formJsInfo.formData.size(),
63 formJsInfo.imageDataMap.size());
64 SetAllowUpdate(allowUpdate_);
65 uiContent_->SetFormWidth(width_ - borderWidth_ * DOUBLE);
66 uiContent_->SetFormHeight(height_ - borderWidth_ * DOUBLE);
67 lastBorderWidth_ = borderWidth_;
68 uiContent_->SetFontScaleFollowSystem(fontScaleFollowSystem_);
69 uiContent_->UpdateFormSharedImage(formJsInfo.imageDataMap);
70 uiContent_->UpdateFormData(formJsInfo.formData);
71 uiContent_->PreInitializeForm(nullptr, formJsInfo.formSrc, nullptr);
72 backgroundColor_ = want.GetStringParam(OHOS::AppExecFwk::Constants::PARAM_FORM_TRANSPARENCY_KEY);
73 if (!backgroundColor_.empty()) {
74 uiContent_->SetFormBackgroundColor(backgroundColor_);
75 }
76 }
77
RunFormPageInner(const OHOS::AAFwk::Want & want,const OHOS::AppExecFwk::FormJsInfo & formJsInfo)78 void FormRenderer::RunFormPageInner(const OHOS::AAFwk::Want& want, const OHOS::AppExecFwk::FormJsInfo& formJsInfo)
79 {
80 if (renderingMode_ == AppExecFwk::Constants::RenderingMode::SINGLE_COLOR) {
81 uiContent_->SetFormRenderingMode(static_cast<int8_t>(renderingMode_));
82 }
83 if (enableBlurBackground_) {
84 uiContent_->SetFormEnableBlurBackground(enableBlurBackground_);
85 }
86 uiContent_->RunFormPage();
87 backgroundColor_ = want.GetStringParam(OHOS::AppExecFwk::Constants::PARAM_FORM_TRANSPARENCY_KEY);
88 if (!backgroundColor_.empty()) {
89 uiContent_->SetFormBackgroundColor(backgroundColor_);
90 }
91
92 auto actionEventHandler = [weak = weak_from_this()](const std::string& action) {
93 auto formRenderer = weak.lock();
94 if (formRenderer) {
95 formRenderer->OnActionEvent(action);
96 }
97 };
98 uiContent_->SetActionEventHandler(actionEventHandler);
99
100 auto errorEventHandler = [weak = weak_from_this()](const std::string& code, const std::string& msg) {
101 auto formRenderer = weak.lock();
102 if (formRenderer) {
103 formRenderer->OnError(code, msg);
104 }
105 };
106 uiContent_->SetErrorEventHandler(errorEventHandler);
107
108 if (!formJsInfo.isDynamic) {
109 auto formLinkInfoUpdateHandler = [weak = weak_from_this()](const std::vector<std::string>& formLinkInfos) {
110 auto formRenderer = weak.lock();
111 if (formRenderer) {
112 formRenderer->OnFormLinkInfoUpdate(formLinkInfos);
113 }
114 };
115 uiContent_->SetFormLinkInfoUpdateHandler(formLinkInfoUpdateHandler);
116 }
117
118 auto rsSurfaceNode = uiContent_->GetFormRootNode();
119 if (rsSurfaceNode == nullptr) {
120 return;
121 }
122 HandleTimeStampAndSetBounds(rsSurfaceNode);
123 if (renderingMode_ == AppExecFwk::Constants::RenderingMode::SINGLE_COLOR || enableBlurBackground_) {
124 HILOG_INFO("InitUIContent SetFormBackgroundColor #00FFFFFF");
125 uiContent_->SetFormBackgroundColor(TRANSPARENT_COLOR);
126 }
127 HILOG_INFO("ChangeSensitiveNodes: %{public}s", obscurationMode_ ? "true" : "false");
128 uiContent_->ChangeSensitiveNodes(obscurationMode_);
129 uiContent_->Foreground();
130 }
131
InitUIContent(const OHOS::AAFwk::Want & want,const OHOS::AppExecFwk::FormJsInfo & formJsInfo)132 void FormRenderer::InitUIContent(const OHOS::AAFwk::Want& want, const OHOS::AppExecFwk::FormJsInfo& formJsInfo)
133 {
134 PreInitUIContent(want, formJsInfo);
135 RunFormPageInner(want, formJsInfo);
136 }
137
ParseWant(const OHOS::AAFwk::Want & want)138 void FormRenderer::ParseWant(const OHOS::AAFwk::Want& want)
139 {
140 allowUpdate_ = want.GetBoolParam(FORM_RENDERER_ALLOW_UPDATE, true);
141 width_ = want.GetDoubleParam(OHOS::AppExecFwk::Constants::PARAM_FORM_WIDTH_KEY, 0.0f);
142 height_ = want.GetDoubleParam(OHOS::AppExecFwk::Constants::PARAM_FORM_HEIGHT_KEY, 0.0f);
143 proxy_ = want.GetRemoteObject(FORM_RENDERER_PROCESS_ON_ADD_SURFACE);
144 renderingMode_ = (AppExecFwk::Constants::RenderingMode)want.GetIntParam(
145 OHOS::AppExecFwk::Constants::PARAM_FORM_RENDERINGMODE_KEY, 0);
146 enableBlurBackground_ = want.GetBoolParam(OHOS::AppExecFwk::Constants::PARAM_FORM_ENABLE_BLUR_BACKGROUND_KEY,
147 false);
148 borderWidth_ = want.GetFloatParam(OHOS::AppExecFwk::Constants::PARAM_FORM_BORDER_WIDTH_KEY, 0.0f);
149 fontScaleFollowSystem_ = want.GetBoolParam(OHOS::AppExecFwk::Constants::PARAM_FONT_FOLLOW_SYSTEM_KEY, true);
150 obscurationMode_ = want.GetBoolParam(OHOS::AppExecFwk::Constants::PARAM_FORM_OBSCURED_KEY, false);
151 }
152
AddForm(const OHOS::AAFwk::Want & want,const OHOS::AppExecFwk::FormJsInfo & formJsInfo)153 void FormRenderer::AddForm(const OHOS::AAFwk::Want& want, const OHOS::AppExecFwk::FormJsInfo& formJsInfo)
154 {
155 if (uiContent_ == nullptr) {
156 HILOG_ERROR("uiContent is null!");
157 return;
158 }
159 formRendererDispatcherImpl_ = new FormRendererDispatcherImpl(uiContent_, shared_from_this(), eventHandler_);
160 ParseWant(want);
161 InitUIContent(want, formJsInfo);
162 SetRenderDelegate(proxy_);
163 if (want.HasParameter(OHOS::AppExecFwk::Constants::FORM_STATUS_DATA)) {
164 std::string statusData = want.GetStringParam(OHOS::AppExecFwk::Constants::FORM_STATUS_DATA);
165 RecoverForm(statusData);
166 }
167 OnSurfaceCreate(formJsInfo, want.GetBoolParam(
168 OHOS::AppExecFwk::Constants::FORM_IS_RECOVER_FORM_TO_HANDLE_CLICK_EVENT, false));
169 }
170
PreInitAddForm(const OHOS::AAFwk::Want & want,const OHOS::AppExecFwk::FormJsInfo & formJsInfo)171 void FormRenderer::PreInitAddForm(const OHOS::AAFwk::Want& want, const OHOS::AppExecFwk::FormJsInfo& formJsInfo)
172 {
173 if (uiContent_ == nullptr) {
174 HILOG_ERROR("uiContent is null!");
175 return;
176 }
177 formRendererDispatcherImpl_ = new FormRendererDispatcherImpl(uiContent_, shared_from_this(), eventHandler_);
178 ParseWant(want);
179 PreInitUIContent(want, formJsInfo);
180 }
181
RunFormPage(const OHOS::AAFwk::Want & want,const OHOS::AppExecFwk::FormJsInfo & formJsInfo)182 void FormRenderer::RunFormPage(const OHOS::AAFwk::Want& want, const OHOS::AppExecFwk::FormJsInfo& formJsInfo)
183 {
184 if (uiContent_ == nullptr) {
185 HILOG_ERROR("uiContent is null!");
186 return;
187 }
188 ParseWant(want);
189 RunFormPageInner(want, formJsInfo);
190 SetRenderDelegate(proxy_);
191 if (want.HasParameter(OHOS::AppExecFwk::Constants::FORM_STATUS_DATA)) {
192 std::string statusData = want.GetStringParam(OHOS::AppExecFwk::Constants::FORM_STATUS_DATA);
193 RecoverForm(statusData);
194 }
195 OnSurfaceCreate(formJsInfo, want.GetBoolParam(
196 OHOS::AppExecFwk::Constants::FORM_IS_RECOVER_FORM_TO_HANDLE_CLICK_EVENT, false));
197 }
198
ReloadForm(const std::string & url)199 void FormRenderer::ReloadForm(const std::string& url)
200 {
201 if (!uiContent_) {
202 HILOG_ERROR("uiContent_ is null");
203 return;
204 }
205 uiContent_->ReloadForm(url);
206 }
207
IsAllowUpdate()208 bool FormRenderer::IsAllowUpdate()
209 {
210 if (formRendererDispatcherImpl_ == nullptr) {
211 HILOG_ERROR("formRendererDispatcherImpl is null");
212 return true;
213 }
214
215 return formRendererDispatcherImpl_->IsAllowUpdate();
216 }
217
SetAllowUpdate(bool allowUpdate)218 void FormRenderer::SetAllowUpdate(bool allowUpdate)
219 {
220 if (formRendererDispatcherImpl_ == nullptr) {
221 HILOG_ERROR("formRendererDispatcherImpl is null");
222 return;
223 }
224
225 formRendererDispatcherImpl_->SetAllowUpdate(allowUpdate);
226 }
227
UpdateForm(const OHOS::AppExecFwk::FormJsInfo & formJsInfo)228 void FormRenderer::UpdateForm(const OHOS::AppExecFwk::FormJsInfo& formJsInfo)
229 {
230 HILOG_INFO("FormRender UpdateForm start.");
231 if (!IsAllowUpdate()) {
232 HILOG_ERROR("Not allow update");
233 return;
234 }
235 if (!uiContent_) {
236 HILOG_ERROR("uiContent_ is null");
237 return;
238 }
239 uiContent_->SetFontScaleFollowSystem(fontScaleFollowSystem_);
240 uiContent_->UpdateFormSharedImage(formJsInfo.imageDataMap);
241 uiContent_->UpdateFormData(formJsInfo.formData);
242 HILOG_INFO("FormRender UpdateForm end. formJsInfo.formData.size = %{public}zu. \
243 formJsInfo.imageDataMap.size = %{public}zu.",
244 formJsInfo.formData.size(),
245 formJsInfo.imageDataMap.size());
246 }
247
RemoveFormDeathRecipient()248 void FormRenderer::RemoveFormDeathRecipient()
249 {
250 if (!formRendererDelegate_) {
251 return;
252 }
253 auto renderDelegate = formRendererDelegate_->AsObject();
254 if (renderDelegate != nullptr) {
255 renderDelegate->RemoveDeathRecipient(renderDelegateDeathRecipient_);
256 }
257 }
258
Destroy()259 void FormRenderer::Destroy()
260 {
261 HILOG_INFO("Destroy FormRenderer start.");
262 if (formRendererDelegate_ != nullptr && uiContent_ != nullptr) {
263 auto rsSurfaceNode = uiContent_->GetFormRootNode();
264 if (rsSurfaceNode != nullptr) {
265 HILOG_INFO("Form OnSurfaceRelease!");
266 formRendererDelegate_->OnSurfaceRelease(rsSurfaceNode->GetId());
267 }
268 }
269
270 RemoveFormDeathRecipient();
271 renderDelegateDeathRecipient_ = nullptr;
272 formRendererDelegate_ = nullptr;
273 formRendererDispatcherImpl_ = nullptr;
274 if (uiContent_) {
275 uiContent_->Destroy();
276 uiContent_ = nullptr;
277 }
278 context_ = nullptr;
279 runtime_ = nullptr;
280 HILOG_INFO("Destroy FormRenderer finish.");
281 }
282
UpdateFormSize(float width,float height,float borderWidth)283 void FormRenderer::UpdateFormSize(float width, float height, float borderWidth)
284 {
285 if (!uiContent_) {
286 HILOG_ERROR("uiContent_ is null");
287 return;
288 }
289 auto rsSurfaceNode = uiContent_->GetFormRootNode();
290 if (rsSurfaceNode == nullptr) {
291 HILOG_ERROR("rsSurfaceNode is nullptr.");
292 return;
293 }
294 float resizedWidth = width - borderWidth * DOUBLE;
295 float resizedHeight = height - borderWidth * DOUBLE;
296 if (!NearEqual(width, width_) || !NearEqual(height, height_) || !NearEqual(borderWidth, lastBorderWidth_)) {
297 width_ = width;
298 height_ = height;
299 borderWidth_ = borderWidth;
300 uiContent_->SetFormWidth(resizedWidth);
301 uiContent_->SetFormHeight(resizedHeight);
302 lastBorderWidth_ = borderWidth_;
303 std::shared_ptr<EventHandler> eventHandler = eventHandler_.lock();
304 rsSurfaceNode->SetBounds(borderWidth_, borderWidth_, resizedWidth, resizedHeight);
305 HILOG_INFO(
306 "UpdateFormSize after setbounds, rsSurfaceNode width: %{public}f, height: %{public}f",
307 rsSurfaceNode->GetStagingProperties().GetBounds().data_[RSSURFACENODE_PROPERTIES_WIDTH_INDEX],
308 rsSurfaceNode->GetStagingProperties().GetBounds().data_[RSSURFACENODE_PROPERTIES_HEIGHT_INDEX]);
309 if (!eventHandler) {
310 HILOG_ERROR("eventHandler is null");
311 return;
312 }
313 eventHandler->PostTask([uiContent = uiContent_, resizedWidth, resizedHeight]() {
314 if (!uiContent) {
315 HILOG_ERROR("uiContent is null");
316 return;
317 }
318 uiContent->OnFormSurfaceChange(resizedWidth, resizedHeight);
319 });
320 } else {
321 CheckWhetherNeedResizeFormAgain(borderWidth, resizedWidth, resizedHeight);
322 }
323 }
324
OnSurfaceChange(float width,float height,float borderWidth)325 void FormRenderer::OnSurfaceChange(float width, float height, float borderWidth)
326 {
327 if (!formRendererDelegate_) {
328 HILOG_ERROR("form renderer delegate is null!");
329 return;
330 }
331 HILOG_INFO("Form OnSurfaceChange!");
332 formRendererDelegate_->OnSurfaceChange(width, height, borderWidth);
333 width_ = width;
334 height_ = height;
335 borderWidth_ = borderWidth;
336 }
337
OnSurfaceCreate(const OHOS::AppExecFwk::FormJsInfo & formJsInfo,bool isRecoverFormToHandleClickEvent)338 void FormRenderer::OnSurfaceCreate(const OHOS::AppExecFwk::FormJsInfo& formJsInfo,
339 bool isRecoverFormToHandleClickEvent)
340 {
341 if (!formRendererDispatcherImpl_) {
342 HILOG_ERROR("form renderer dispatcher is null!");
343 return;
344 }
345 if (!formRendererDelegate_) {
346 HILOG_ERROR("form renderer delegate is null!");
347 return;
348 }
349 OHOS::AAFwk::Want newWant;
350 newWant.SetParam(FORM_RENDERER_DISPATCHER, formRendererDispatcherImpl_->AsObject());
351 newWant.SetParam(OHOS::AppExecFwk::Constants::FORM_IS_RECOVER_FORM_TO_HANDLE_CLICK_EVENT,
352 isRecoverFormToHandleClickEvent);
353 auto rsSurfaceNode = uiContent_->GetFormRootNode();
354 HILOG_INFO("Form OnSurfaceCreate!");
355 formRendererDelegate_->OnSurfaceCreate(rsSurfaceNode, formJsInfo, newWant);
356 }
357
OnSurfaceReuse(const OHOS::AppExecFwk::FormJsInfo & formJsInfo)358 void FormRenderer::OnSurfaceReuse(const OHOS::AppExecFwk::FormJsInfo& formJsInfo)
359 {
360 if (!formRendererDispatcherImpl_) {
361 HILOG_ERROR("form renderer dispatcher is null!");
362 return;
363 }
364 if (!formRendererDelegate_) {
365 HILOG_ERROR("form renderer delegate is null!");
366 return;
367 }
368 auto rsSurfaceNode = uiContent_->GetFormRootNode();
369 if (rsSurfaceNode == nullptr) {
370 HILOG_ERROR("form renderer rsSurfaceNode is null!");
371 return;
372 }
373 OHOS::AAFwk::Want newWant;
374 newWant.SetParam(FORM_RENDERER_DISPATCHER, formRendererDispatcherImpl_->AsObject());
375 HILOG_INFO("Form OnSurfaceReuse.");
376 formRendererDelegate_->OnSurfaceReuse(rsSurfaceNode->GetId(), formJsInfo, newWant);
377 formRendererDelegate_->OnFormLinkInfoUpdate(cachedInfos_);
378 }
379
OnSurfaceDetach()380 void FormRenderer::OnSurfaceDetach()
381 {
382 if (!formRendererDelegate_) {
383 HILOG_ERROR("form renderer delegate is null!");
384 return;
385 }
386 auto rsSurfaceNode = uiContent_->GetFormRootNode();
387 if (rsSurfaceNode == nullptr) {
388 HILOG_ERROR("form renderer rsSurfaceNode is null!");
389 return;
390 }
391 HILOG_INFO("Form OnSurfaceDetach.");
392 formRendererDelegate_->OnSurfaceDetach(rsSurfaceNode->GetId());
393 }
394
OnActionEvent(const std::string & action)395 void FormRenderer::OnActionEvent(const std::string& action)
396 {
397 if (!formRendererDelegate_) {
398 HILOG_ERROR("formRendererDelegate is null!");
399 return;
400 }
401
402 formRendererDelegate_->OnActionEvent(action);
403 }
404
OnError(const std::string & code,const std::string & msg)405 void FormRenderer::OnError(const std::string& code, const std::string& msg)
406 {
407 if (!formRendererDelegate_) {
408 HILOG_ERROR("formRendererDelegate is null!");
409 return;
410 }
411
412 formRendererDelegate_->OnError(code, msg);
413 }
414
OnFormLinkInfoUpdate(const std::vector<std::string> & formLinkInfos)415 void FormRenderer::OnFormLinkInfoUpdate(const std::vector<std::string>& formLinkInfos)
416 {
417 if (!formRendererDelegate_) {
418 HILOG_ERROR("formRendererDelegate is null!");
419 return;
420 }
421 cachedInfos_ = formLinkInfos;
422 formRendererDelegate_->OnFormLinkInfoUpdate(formLinkInfos);
423 }
424
SetRenderDelegate(const sptr<IRemoteObject> & remoteObj)425 void FormRenderer::SetRenderDelegate(const sptr<IRemoteObject>& remoteObj)
426 {
427 HILOG_INFO("Get renderRemoteObj, add death recipient.");
428 auto renderRemoteObj = iface_cast<IFormRendererDelegate>(remoteObj);
429 if (renderRemoteObj == nullptr) {
430 HILOG_ERROR("renderRemoteObj is nullptr.");
431 return;
432 }
433
434 if (!formRendererDelegate_) {
435 formRendererDelegate_ = renderRemoteObj;
436 } else {
437 RemoveFormDeathRecipient();
438 auto formRendererDelegate = renderRemoteObj;
439 bool checkFlag = true;
440 formRendererDelegate->OnCheckManagerDelegate(checkFlag);
441 if (checkFlag) {
442 formRendererDelegate_ = renderRemoteObj;
443 } else {
444 HILOG_ERROR("EventHandle - SetRenderDelegate error checkFlag is false");
445 }
446 }
447
448 if (renderDelegateDeathRecipient_ == nullptr) {
449 renderDelegateDeathRecipient_ = new FormRenderDelegateRecipient(
450 [eventHandler = eventHandler_, renderer = weak_from_this()]() {
451 auto handler = eventHandler.lock();
452 if (!handler) {
453 HILOG_ERROR("eventHandler is nullptr");
454 return;
455 }
456
457 handler->PostTask([weak = renderer]() {
458 auto formRender = weak.lock();
459 if (!formRender) {
460 HILOG_ERROR("formRender is nullptr");
461 return;
462 }
463 formRender->ResetRenderDelegate();
464 });
465 });
466 }
467 auto renderDelegate = formRendererDelegate_->AsObject();
468 if (renderDelegate == nullptr) {
469 HILOG_ERROR("renderDelegate is nullptr, can not get obj from renderRemoteObj.");
470 return;
471 }
472 renderDelegate->AddDeathRecipient(renderDelegateDeathRecipient_);
473 }
474
ResetRenderDelegate()475 void FormRenderer::ResetRenderDelegate()
476 {
477 HILOG_INFO("ResetRenderDelegate.");
478 RemoveFormDeathRecipient();
479 renderDelegateDeathRecipient_ = nullptr;
480 formRendererDelegate_ = nullptr;
481 }
482
UpdateConfiguration(const std::shared_ptr<OHOS::AppExecFwk::Configuration> & config)483 void FormRenderer::UpdateConfiguration(const std::shared_ptr<OHOS::AppExecFwk::Configuration>& config)
484 {
485 if (!uiContent_) {
486 HILOG_ERROR("uiContent_ is null");
487 return;
488 }
489
490 uiContent_->UpdateConfiguration(config);
491 }
492
OnRemoteDied(const wptr<IRemoteObject> & remote)493 void FormRenderDelegateRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
494 {
495 HILOG_ERROR("Recv FormRenderDelegate death notice");
496 if (remote == nullptr) {
497 HILOG_ERROR("weak remote is null");
498 return;
499 }
500 if (handler_) {
501 handler_();
502 }
503 }
504
AttachForm(const OHOS::AAFwk::Want & want,const OHOS::AppExecFwk::FormJsInfo & formJsInfo)505 void FormRenderer::AttachForm(const OHOS::AAFwk::Want& want, const OHOS::AppExecFwk::FormJsInfo& formJsInfo)
506 {
507 if (uiContent_ == nullptr) {
508 HILOG_ERROR("uiContent is null!");
509 return;
510 }
511 ParseWant(want);
512 OnSurfaceDetach();
513 AttachUIContent(want, formJsInfo);
514 SetRenderDelegate(proxy_);
515 OnSurfaceReuse(formJsInfo);
516 }
517
AttachUIContent(const OHOS::AAFwk::Want & want,const OHOS::AppExecFwk::FormJsInfo & formJsInfo)518 void FormRenderer::AttachUIContent(const OHOS::AAFwk::Want& want, const OHOS::AppExecFwk::FormJsInfo& formJsInfo)
519 {
520 HILOG_INFO("AttachUIContent width = %{public}f , height = %{public}f, borderWidth_ = %{public}f.",
521 width_, height_, borderWidth_);
522 SetAllowUpdate(allowUpdate_);
523 auto rsSurfaceNode = uiContent_->GetFormRootNode();
524 if (rsSurfaceNode == nullptr) {
525 HILOG_ERROR("rsSurfaceNode is nullptr.");
526 return;
527 }
528 float width = width_ - borderWidth_ * DOUBLE;
529 float height = height_ - borderWidth_ * DOUBLE;
530 if (!NearEqual(width, uiContent_->GetFormWidth()) || !NearEqual(height, uiContent_->GetFormHeight())
531 || !NearEqual(borderWidth_, lastBorderWidth_)) {
532 uiContent_->SetFormWidth(width);
533 uiContent_->SetFormHeight(height);
534 lastBorderWidth_ = borderWidth_;
535 uiContent_->OnFormSurfaceChange(width, height);
536 rsSurfaceNode->SetBounds(borderWidth_, borderWidth_, width, height);
537 HILOG_INFO(
538 "AttachUIContent after setbounds, rsSurfaceNode width: %{public}f, height: %{public}f",
539 rsSurfaceNode->GetStagingProperties().GetBounds().data_[RSSURFACENODE_PROPERTIES_WIDTH_INDEX],
540 rsSurfaceNode->GetStagingProperties().GetBounds().data_[RSSURFACENODE_PROPERTIES_HEIGHT_INDEX]);
541 }
542 auto backgroundColor = want.GetStringParam(OHOS::AppExecFwk::Constants::PARAM_FORM_TRANSPARENCY_KEY);
543 if (backgroundColor_ != backgroundColor) {
544 backgroundColor_ = backgroundColor;
545 uiContent_->SetFormBackgroundColor(backgroundColor_);
546 }
547 if (renderingMode_ == AppExecFwk::Constants::RenderingMode::SINGLE_COLOR || enableBlurBackground_) {
548 HILOG_INFO("AttachUIContent SetFormBackgroundColor #00FFFFFF");
549 uiContent_->SetFormBackgroundColor(TRANSPARENT_COLOR);
550 }
551
552 uiContent_->Foreground();
553 }
554
GetRectRelativeToWindow(AccessibilityParentRectInfo & parentRectInfo) const555 void FormRenderer::GetRectRelativeToWindow(AccessibilityParentRectInfo& parentRectInfo) const
556 {
557 if (!formRendererDelegate_) {
558 HILOG_ERROR("form renderer delegate is null!");
559 return;
560 }
561 formRendererDelegate_->OnGetRectRelativeToWindow(parentRectInfo);
562 }
563
RecycleForm(std::string & statusData)564 void FormRenderer::RecycleForm(std::string& statusData)
565 {
566 if (uiContent_ == nullptr) {
567 HILOG_ERROR("RecycleForm, uiContent_ is null!");
568 return;
569 }
570 statusData = uiContent_->RecycleForm();
571 }
572
RecoverForm(const std::string & statusData)573 void FormRenderer::RecoverForm(const std::string& statusData)
574 {
575 if (uiContent_ == nullptr) {
576 HILOG_ERROR("RecoverForm, uiContent_ is null!");
577 return;
578 }
579 uiContent_->RecoverForm(statusData);
580 }
581
SetVisibleChange(bool isVisible)582 void FormRenderer::SetVisibleChange(bool isVisible)
583 {
584 if (formRendererDispatcherImpl_ != nullptr) {
585 formRendererDispatcherImpl_->SetVisible(isVisible);
586 } else {
587 HILOG_WARN("formRendererDispatcherImpl_ is null!");
588 }
589
590 if (uiContent_ == nullptr) {
591 HILOG_ERROR("SetVisibleChange error, uiContent_ is null!");
592 return;
593 }
594 uiContent_->ProcessFormVisibleChange(isVisible);
595 }
596
HandleTimeStampAndSetBounds(std::shared_ptr<Rosen::RSSurfaceNode> rsSurfaceNode)597 void FormRenderer::HandleTimeStampAndSetBounds(std::shared_ptr<Rosen::RSSurfaceNode> rsSurfaceNode)
598 {
599 int64_t timeStamp = static_cast<int64_t>(std::chrono::duration_cast<std::chrono::milliseconds>(
600 std::chrono::system_clock::now().time_since_epoch()).count());
601 std::shared_ptr<EventHandler> eventHandler = eventHandler_.lock();
602 SetRunFormPageInnerTimeStamp(timeStamp);
603
604 if (eventHandler) {
605 auto task = [weak = weak_from_this()]() {
606 auto formRenderer = weak.lock();
607 if (formRenderer) {
608 formRenderer->SetRunFormPageInnerTimeStamp(DEFAULT_TIMESTAMP);
609 } else {
610 HILOG_WARN("HandleTimeStampAndSetBounds, formRenderer is nullptr");
611 }
612 };
613 eventHandler->PostTask(task, DELAY_SET_DEFAULT_TIMESTAMP_TASK);
614 }
615
616 rsSurfaceNode->SetBounds(round(borderWidth_), round(borderWidth_), round(width_ - borderWidth_ * DOUBLE),
617 round(height_ - borderWidth_ * DOUBLE));
618 HILOG_INFO(
619 "HandleTimeStampAndSetBounds after setbounds, rsSurfaceNode width: %{public}f, height: %{public}f",
620 rsSurfaceNode->GetStagingProperties().GetBounds().data_[RSSURFACENODE_PROPERTIES_WIDTH_INDEX],
621 rsSurfaceNode->GetStagingProperties().GetBounds().data_[RSSURFACENODE_PROPERTIES_HEIGHT_INDEX]);
622 }
623
CheckWhetherNeedResizeFormAgain(float borderWidth,float width,float height)624 void FormRenderer::CheckWhetherNeedResizeFormAgain(float borderWidth, float width, float height)
625 {
626 int64_t timeStamp = static_cast<int64_t>(std::chrono::duration_cast<std::chrono::milliseconds>(
627 std::chrono::system_clock::now().time_since_epoch()).count());
628 int64_t runFormPageInnerTimeStamp = GetRunFormPageInnerTimeStamp();
629 SetRunFormPageInnerTimeStamp(DEFAULT_TIMESTAMP);
630 if (timeStamp - runFormPageInnerTimeStamp > DELAY_SET_DEFAULT_TIMESTAMP_TASK) {
631 return;
632 }
633 std::shared_ptr<EventHandler> eventHandler = eventHandler_.lock();
634 if (eventHandler == nullptr) {
635 HILOG_ERROR("CheckWhetherNeedResizeFormAgain, eventHandler is nullptr!");
636 return;
637 }
638 auto task = [weak = weak_from_this(), borderWidth, resizedWidth = width, resizedHeight = height]() {
639 auto formRenderer = weak.lock();
640 if (formRenderer == nullptr) {
641 HILOG_WARN("CheckWhetherNeedResizeFormAgain, formRenderer is nullptr");
642 return;
643 }
644 formRenderer->ResizeFormAgain(borderWidth, resizedWidth, resizedHeight);
645 };
646 eventHandler->PostTask(task, DELAY_RESIZE_FORM_AGAIN_TASK);
647 }
648
ResizeFormAgain(float borderWidth,float width,float height)649 void FormRenderer::ResizeFormAgain(float borderWidth, float width, float height)
650 {
651 if (uiContent_ == nullptr) {
652 HILOG_ERROR("uiContent_ is null");
653 return;
654 }
655 auto rsSurfaceNode = uiContent_->GetFormRootNode();
656 if (rsSurfaceNode == nullptr) {
657 HILOG_ERROR("rsSurfaceNode is nullptr.");
658 return;
659 }
660 uiContent_->SetFormWidth(width);
661 uiContent_->SetFormHeight(height);
662 uiContent_->OnFormSurfaceChange(width, height);
663 rsSurfaceNode->SetBounds(borderWidth, borderWidth, width, height);
664 HILOG_INFO(
665 "ResizeFormAgain after setbounds, rsSurfaceNode width: %{public}f, height: %{public}f",
666 rsSurfaceNode->GetStagingProperties().GetBounds().data_[RSSURFACENODE_PROPERTIES_WIDTH_INDEX],
667 rsSurfaceNode->GetStagingProperties().GetBounds().data_[RSSURFACENODE_PROPERTIES_HEIGHT_INDEX]);
668 }
669
GetRunFormPageInnerTimeStamp()670 int64_t FormRenderer::GetRunFormPageInnerTimeStamp()
671 {
672 return runFormPageInnerTimeStamp_;
673 }
674
SetRunFormPageInnerTimeStamp(int64_t timeStamp)675 void FormRenderer::SetRunFormPageInnerTimeStamp(int64_t timeStamp)
676 {
677 runFormPageInnerTimeStamp_ = timeStamp;
678 }
679 } // namespace Ace
680 } // namespace OHOS
681