1 /*
2  * Copyright (c) 2021-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_FRAMEWORKS_CORE_COMPONENTS_FORM_RESOURCE_FORM_MANAGER_DELEGATE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_FORM_RESOURCE_FORM_MANAGER_DELEGATE_H
18 
19 #include <list>
20 
21 #include "interfaces/inner_api/form_render/include/form_renderer_delegate_impl.h"
22 #include "interfaces/inner_api/form_render/include/form_renderer_dispatcher_interface.h"
23 
24 #include "core/components/common/layout/constants.h"
25 #include "core/components/form/resource/form_manager_resource.h"
26 #include "core/components/form/resource/form_request_data.h"
27 #include "core/pipeline/pipeline_base.h"
28 
29 #ifdef OHOS_STANDARD_SYSTEM
30 #include "form_info.h"
31 #include "form_js_info.h"
32 #include "ui/rs_surface_node.h"
33 #include "want.h"
34 #include "want_params_wrapper.h"
35 
36 #include "core/components/form/resource/form_utils.h"
37 #endif
38 
39 namespace OHOS::Ace {
40 class FormCallbackClient;
41 class FormSurfaceCallbackClient;
42 class FormManagerDelegate : public FormManagerResource {
43     DECLARE_ACE_TYPE(FormManagerDelegate, FormManagerResource);
44 
45 public:
46     using onFormAcquiredCallbackForJava =
47         std::function<void(int64_t, const std::string&, const std::string&, const std::string&)>;
48     using OnFormUpdateCallbackForJava = std::function<void(int64_t, const std::string&)>;
49     using OnFormAcquiredCallback = std::function<void(int64_t, const std::string&, const std::string&,
50         const std::string&, const std::map<std::string, sptr<AppExecFwk::FormAshmem>>&, const AppExecFwk::FormJsInfo&,
51         const FrontendType& frontendType, const FrontendType& uiSyntax)>;
52     using OnFormUpdateCallback =
53         std::function<void(int64_t, const std::string&, const std::map<std::string, sptr<AppExecFwk::FormAshmem>>&)>;
54     using OnFormLinkInfoUpdateCallback = std::function<void(const std::vector<std::string>&)>;
55     using OnGetRectRelativeToWindowCallback = std::function<void(int32_t&, int32_t&)>;
56     using OnFormErrorCallback = std::function<void(const std::string&, const std::string&)>;
57     using OnFormUninstallCallback = std::function<void(int64_t)>;
58     using OnFormSurfaceNodeCallback = std::function<void(const std::shared_ptr<Rosen::RSSurfaceNode>&,
59         const AAFwk::Want&)>;
60     using OnFormSurfaceChangeCallback = std::function<void(float width, float height, float borderWidth)>;
61     using OnFormSurfaceDetachCallback = std::function<void()>;
62     using ActionEventHandle = std::function<void(const std::string&)>;
63     using UnTrustFormCallback = std::function<void()>;
64     using SnapshotCallback = std::function<void(const uint32_t&)>;
65     using EnableFormCallback = std::function<void(const bool enable)>;
66 
67     enum class State : char {
68         WAITINGFORSIZE,
69         CREATING,
70         CREATED,
71         CREATEFAILED,
72         RELEASED,
73     };
74 
75     enum class RecycleStatus {
76         RECYCLED,
77         RECOVERING,
78         RECOVERED,
79     };
80 
81     struct NotifySurfaceChangeFailedRecord {
82         bool isfailed = false;
83         float expectedWidth = 0.0f;
84         float expectedHeight = 0.0f;
85         float expectedBorderWidth = 0.0f;
86     };
87 
88     FormManagerDelegate() = delete;
89     ~FormManagerDelegate() override;
FormManagerDelegate(const WeakPtr<PipelineBase> & context)90     explicit FormManagerDelegate(const WeakPtr<PipelineBase>& context)
91         : FormManagerResource("formAdaptor", context), state_(State::WAITINGFORSIZE)
92     {}
93 
94 #if OHOS_STANDARD_SYSTEM
95     void AddForm(const WeakPtr<PipelineBase>& context, const RequestFormInfo& info,
96         const AppExecFwk::FormInfo& formInfo);
97 #else
98     void AddForm(const WeakPtr<PipelineBase>& context, const RequestFormInfo& info);
99 #endif
100     void ReleasePlatformResource();
101 
102     void AddFormAcquireCallback(const OnFormAcquiredCallback& callback);
103     void AddFormUpdateCallback(const OnFormUpdateCallback& callback);
104     void AddFormErrorCallback(const OnFormErrorCallback& callback);
105     void AddFormUninstallCallback(const OnFormUninstallCallback& callback);
106     void AddFormSurfaceNodeCallback(const OnFormSurfaceNodeCallback& callback);
107     void AddFormSurfaceChangeCallback(OnFormSurfaceChangeCallback&& callback);
108     void AddFormSurfaceDetachCallback(OnFormSurfaceDetachCallback&& callback);
109     void AddFormLinkInfoUpdateCallback(OnFormLinkInfoUpdateCallback&& callback);
110     void AddGetRectRelativeToWindowCallback(OnGetRectRelativeToWindowCallback && callback);
111     void AddActionEventHandle(const ActionEventHandle& callback);
112     void AddUnTrustFormCallback(const UnTrustFormCallback& callback);
113     void AddSnapshotCallback(SnapshotCallback&& callback);
114     void AddEnableFormCallback(EnableFormCallback&& callback);
115     void OnActionEventHandle(const std::string& action);
116     void SetAllowUpdate(bool allowUpdate);
117     void OnActionEvent(const std::string& action);
118     void DispatchPointerEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent,
119         SerializedGesture& serializedGesture);
120     void AddRenderDelegate();
121     void RegisterRenderDelegateEvent();
122     void OnFormError(const std::string& code, const std::string& msg);
123     void OnFormLinkInfoUpdate(const std::vector<std::string>& formLinkInfos);
124     void OnGetRectRelativeToWindow(int32_t &top, int32_t &left);
125     void ReleaseRenderer();
126     void SetObscured(bool isObscured);
127     void OnAccessibilityTransferHoverEvent(float pointX, float pointY, int32_t sourceType,
128         int32_t eventType, int64_t timeMs);
129     void OnAccessibilityChildTreeRegister(uint32_t windowId, int32_t treeId, int64_t accessibilityId);
130     void OnAccessibilityChildTreeDeregister();
131     void OnAccessibilityDumpChildInfo(const std::vector<std::string>& params, std::vector<std::string>& info);
132     bool CheckFormBundleForbidden(const std::string& bundleName);
133 #ifdef OHOS_STANDARD_SYSTEM
134     void ProcessFormUpdate(const AppExecFwk::FormJsInfo& formJsInfo);
135     void ProcessFormUninstall(const int64_t formId);
136     void OnDeathReceived();
137     void SetFormUtils(const std::shared_ptr<FormUtils>& formUtils);
138     void OnSurfaceCreate(const AppExecFwk::FormJsInfo& formInfo,
139         const std::shared_ptr<Rosen::RSSurfaceNode>& rsSurfaceNode, const AAFwk::Want& want);
140     void OnFormSurfaceChange(float width, float height, float borderWidth = 0.0);
141     void OnFormSurfaceDetach();
142     void ResetForm();
143     void ReleaseForm();
144     void NotifySurfaceChange(float width, float height, float borderWidth = 0.0);
145     static bool GetFormInfo(const std::string& bundleName, const std::string& moduleName,
146         const std::string& cardName, AppExecFwk::FormInfo& formInfo);
147     void ProcessRecycleForm();
148     void ProcessEnableForm(bool enable);
149 #endif
150     void HandleCachedClickEvents();
151 
152 private:
153     void CreatePlatformResource(const WeakPtr<PipelineBase>& context, const RequestFormInfo& info);
154     void Stop();
155     void RegisterEvent();
156     void UnregisterEvent();
157     std::string ConvertRequestInfo(const RequestFormInfo& info) const;
158 
159     void OnFormAcquired(const std::string& param);
160     void OnFormUpdate(const std::string& param);
161     void OnFormError(const std::string& param);
162     void ReAddForm();
163     void HandleUnTrustFormCallback();
164     void HandleSnapshotCallback(const uint32_t& delayTime);
165     bool ParseAction(const std::string& action, const std::string& type, AAFwk::Want& want);
166     void HandleEnableFormCallback(const bool enable);
167     void SetGestureInnerFlag();
168     void CheckWhetherSurfaceChangeFailed();
169 
170     onFormAcquiredCallbackForJava onFormAcquiredCallbackForJava_;
171     OnFormUpdateCallbackForJava onFormUpdateCallbackForJava_;
172     OnFormAcquiredCallback onFormAcquiredCallback_;
173     OnFormUpdateCallback onFormUpdateCallback_;
174     OnFormLinkInfoUpdateCallback onFormLinkInfoUpdateCallback_;
175     OnGetRectRelativeToWindowCallback onGetRectRelativeToWindowCallback_;
176     OnFormErrorCallback onFormErrorCallback_;
177     OnFormUninstallCallback onFormUninstallCallback_;
178     OnFormSurfaceNodeCallback onFormSurfaceNodeCallback_;
179     OnFormSurfaceChangeCallback onFormSurfaceChangeCallback_;
180     OnFormSurfaceDetachCallback onFormSurfaceDetachCallback_;
181     ActionEventHandle actionEventHandle_;
182     UnTrustFormCallback unTrustFormCallback_;
183     SnapshotCallback snapshotCallback_;
184     EnableFormCallback enableFormCallback_;
185 
186     State state_ { State::WAITINGFORSIZE };
187     bool isDynamic_ = true;
188     std::mutex recycleMutex_;
189     std::mutex surfaceChangeFailedRecordMutex_;
190     RecycleStatus recycleStatus_ = RecycleStatus::RECOVERED;
191     std::vector<std::shared_ptr<MMI::PointerEvent>> pointerEventCache_;
192     NotifySurfaceChangeFailedRecord notifySurfaceChangeFailedRecord_;
193 #ifdef OHOS_STANDARD_SYSTEM
194     int64_t runningCardId_ = -1;
195     std::string runningCompId_;
196     AAFwk::Want wantCache_;
197     bool hasCreated_ = false;
198     std::shared_ptr<FormCallbackClient> formCallbackClient_;
199     std::shared_ptr<FormUtils> formUtils_;
200     std::shared_ptr<FormSurfaceCallbackClient> formSurfaceCallbackClient_;
201     sptr<FormRendererDelegateImpl> renderDelegate_;
202     sptr<IFormRendererDispatcher> formRendererDispatcher_;
203     AppExecFwk::FormJsInfo formJsInfo_;
204 #endif
205 };
206 
207 } // namespace OHOS::Ace
208 
209 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_FORM_RESOURCE_FORM_MANAGER_DELEGATE_H
210