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