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 #ifndef FOUNDATION_ACE_INTERFACE_INNERKITS_FORM_RENDERER_H
17 #define FOUNDATION_ACE_INTERFACE_INNERKITS_FORM_RENDERER_H
18 
19 #include "ability_context.h"
20 #include "form_js_info.h"
21 #include "form_renderer_delegate_interface.h"
22 #include "form_renderer_dispatcher_impl.h"
23 #include "js_runtime.h"
24 #include "runtime.h"
25 #include "ui_content.h"
26 #include "event_handler.h"
27 #include "form_constants.h"
28 
29 namespace OHOS {
30 namespace AppExecFwk {
31 class Configuration;
32 class EventHandler;
33 }
34 namespace Ace {
35 /**
36  * @class FormRenderer
37  */
38 class FormRenderer : public std::enable_shared_from_this<FormRenderer> {
39 public:
40     FormRenderer(const std::shared_ptr<OHOS::AbilityRuntime::Context> context,
41         const std::shared_ptr<OHOS::AbilityRuntime::Runtime> runtime,
42         std::weak_ptr<OHOS::AppExecFwk::EventHandler> eventHandler);
43     ~FormRenderer();
44 
45     void AddForm(const OHOS::AAFwk::Want& want, const OHOS::AppExecFwk::FormJsInfo& formJsInfo);
46     void PreInitAddForm(const OHOS::AAFwk::Want& want, const OHOS::AppExecFwk::FormJsInfo& formJsInfo);
47     void RunFormPage(const OHOS::AAFwk::Want& want, const OHOS::AppExecFwk::FormJsInfo& formJsInfo);
48     void UpdateForm(const OHOS::AppExecFwk::FormJsInfo& formJsInfo);
49     void ReloadForm(const std::string& url);
50     void Destroy();
51     void ResetRenderDelegate();
52     void SetAllowUpdate(bool allowUpdate);
53     bool IsAllowUpdate();
54 
55     void OnSurfaceCreate(const OHOS::AppExecFwk::FormJsInfo& formJsInfo, bool isRecoverFormToHandleClickEvent);
56     void OnSurfaceReuse(const OHOS::AppExecFwk::FormJsInfo& formJsInfo);
57     void OnSurfaceDetach();
58     void OnActionEvent(const std::string& action);
59     void OnError(const std::string& code, const std::string& msg);
60     void OnSurfaceChange(float width, float height, float borderWidth = 0.0);
61     void OnFormLinkInfoUpdate(const std::vector<std::string>& formLinkInfos);
62     void UpdateConfiguration(const std::shared_ptr<OHOS::AppExecFwk::Configuration>& config);
63     void AttachForm(const OHOS::AAFwk::Want& want, const OHOS::AppExecFwk::FormJsInfo& formJsInfo);
64     void RecycleForm(std::string& statusData);
65     void RecoverForm(const std::string& statusData);
66     void GetRectRelativeToWindow(int32_t &top, int32_t &left) const;
67     void SetVisibleChange(bool isVisible);
68     void UpdateFormSize(float width, float height, float borderWidth);
69 
70 private:
71     void InitUIContent(const OHOS::AAFwk::Want& want, const OHOS::AppExecFwk::FormJsInfo& formJsInfo);
72     void ParseWant(const OHOS::AAFwk::Want& want);
73     void SetRenderDelegate(const sptr<IRemoteObject>& renderRemoteObj);
74     void AttachUIContent(const OHOS::AAFwk::Want& want, const OHOS::AppExecFwk::FormJsInfo& formJsInfo);
75     void PreInitUIContent(const OHOS::AAFwk::Want& want, const OHOS::AppExecFwk::FormJsInfo& formJsInfo);
76     void RunFormPageInner(const OHOS::AAFwk::Want& want, const OHOS::AppExecFwk::FormJsInfo& formJsInfo);
77 
78     bool allowUpdate_ = true;
79     bool obscurationMode_ = false;
80     float width_ = 0.0f;
81     float height_ = 0.0f;
82     float borderWidth_ = 0.0f;
83     float lastBorderWidth_ = 0.0f;
84     bool fontScaleFollowSystem_ = true;
85     std::string backgroundColor_;
86     AppExecFwk::Constants::RenderingMode renderingMode_ = AppExecFwk::Constants::RenderingMode::FULL_COLOR;
87     std::vector<std::string> cachedInfos_;
88     std::shared_ptr<OHOS::AbilityRuntime::Context> context_;
89     std::shared_ptr<OHOS::AbilityRuntime::Runtime> runtime_;
90     std::weak_ptr<OHOS::AppExecFwk::EventHandler> eventHandler_;
91     sptr<FormRendererDispatcherImpl> formRendererDispatcherImpl_;
92     sptr<IFormRendererDelegate> formRendererDelegate_;
93     std::shared_ptr<UIContent> uiContent_;
94     sptr<IRemoteObject::DeathRecipient> renderDelegateDeathRecipient_;
95     sptr<IRemoteObject> proxy_;
96 };
97 
98 /**
99  * @class FormRenderDelegateRecipient
100  * FormRenderDelegateRecipient notices IRemoteBroker died.
101  */
102 class FormRenderDelegateRecipient : public IRemoteObject::DeathRecipient {
103 public:
104     using RemoteDiedHandler = std::function<void()>;
FormRenderDelegateRecipient(RemoteDiedHandler handler)105     explicit FormRenderDelegateRecipient(RemoteDiedHandler handler) : handler_(std::move(handler)) {}
106 
107     ~FormRenderDelegateRecipient() override = default;
108 
109     void OnRemoteDied(const wptr<IRemoteObject>& remote) override;
110 
111 private:
112     RemoteDiedHandler handler_;
113 };
114 } // namespace Ace
115 } // namespace OHOS
116 #endif // FOUNDATION_ACE_INTERFACE_INNERKITS_FORM_RENDERER_H
117