1 /*
2  * Copyright (c) 2021-2024 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_ADAPTER_OHOS_ENTRANCE_ACE_UI_CONTENT_IMPL_H
17 #define FOUNDATION_ACE_ADAPTER_OHOS_ENTRANCE_ACE_UI_CONTENT_IMPL_H
18 
19 #include <list>
20 
21 #include "ability_info.h"
22 #include "display_manager.h"
23 #include "dm/display_manager.h"
24 #include "interfaces/inner_api/ace/arkui_rect.h"
25 #include "interfaces/inner_api/ace/ui_content.h"
26 #include "interfaces/inner_api/ace/viewport_config.h"
27 #include "interfaces/inner_api/ui_session/ui_content_stub_impl.h"
28 #include "key_event.h"
29 #include "native_engine/native_engine.h"
30 #include "native_engine/native_value.h"
31 #include "wm/window.h"
32 
33 #include "adapter/ohos/entrance/distributed_ui_manager.h"
34 #include "adapter/ohos/entrance/ace_viewport_config.h"
35 #include "base/thread/task_executor.h"
36 #include "base/view_data/view_data_wrap.h"
37 #include "core/common/asset_manager_impl.h"
38 #include "core/common/update_config_manager.h"
39 #include "core/components/common/properties/animation_option.h"
40 #include "core/components/common/properties/popup_param.h"
41 
42 namespace OHOS::Accessibility {
43 class AccessibilityElementInfo;
44 }
45 
46 namespace OHOS::Ace {
47 class ACE_FORCE_EXPORT UIContentImpl : public UIContent {
48 public:
49     UIContentImpl(OHOS::AbilityRuntime::Context* context, void* runtime);
50     UIContentImpl(OHOS::AppExecFwk::Ability* ability);
51     UIContentImpl(OHOS::AbilityRuntime::Context* context, void* runtime, bool isCard);
~UIContentImpl()52     ~UIContentImpl()
53     {
54         ProcessDestructCallbacks();
55         DestroyUIDirector();
56         DestroyCallback();
57     }
58 
59     // UI content lifeCycles
60     UIContentErrorCode Initialize(OHOS::Rosen::Window* window, const std::string& url, napi_value storage) override;
61     UIContentErrorCode Initialize(
62         OHOS::Rosen::Window* window, const std::shared_ptr<std::vector<uint8_t>>& content, napi_value storage) override;
63     UIContentErrorCode InitializeByName(
64         OHOS::Rosen::Window* window, const std::string& name, napi_value storage) override;
65     void InitializeDynamic(int32_t hostInstanceId, const std::string& hapPath, const std::string& abcPath,
66         const std::string& entryPoint, const std::vector<std::string>& registerComponents) override;
67     void Initialize(
68         OHOS::Rosen::Window* window, const std::string& url, napi_value storage, uint32_t focusWindowId) override;
69     void Foreground() override;
70     void Background() override;
71     void Focus() override;
72     void UnFocus() override;
73     void Destroy() override;
74     void OnNewWant(const OHOS::AAFwk::Want& want) override;
75 
76     // restore
77     UIContentErrorCode Restore(OHOS::Rosen::Window* window, const std::string& contentInfo,
78         napi_value storage, ContentInfoType type) override;
79     std::string GetContentInfo(ContentInfoType type) const override;
80     void DestroyUIDirector() override;
81     void SetUIContentType(UIContentType uIContentType) override;
82     void UpdateFontScale(const std::shared_ptr<OHOS::AppExecFwk::Configuration>& config);
83 
84     // UI content event process
85     bool ProcessBackPressed() override;
86     void UpdateDialogResourceConfiguration(RefPtr<Container>& container,
87         const std::shared_ptr<OHOS::AbilityRuntime::Context>& context);
88     bool ProcessPointerEvent(const std::shared_ptr<OHOS::MMI::PointerEvent>& pointerEvent) override;
89     bool ProcessPointerEventWithCallback(
90         const std::shared_ptr<OHOS::MMI::PointerEvent>& pointerEvent, const std::function<void()>& callback) override;
91     bool ProcessKeyEvent(const std::shared_ptr<OHOS::MMI::KeyEvent>& keyEvent, bool isPreIme) override;
92     bool ProcessAxisEvent(const std::shared_ptr<OHOS::MMI::AxisEvent>& axisEvent) override;
93     bool ProcessVsyncEvent(uint64_t timeStampNanos) override;
94     void UpdateConfiguration(const std::shared_ptr<OHOS::AppExecFwk::Configuration>& config) override;
95     void UpdateViewportConfig(const ViewportConfig& config, OHOS::Rosen::WindowSizeChangeReason reason,
96         const std::shared_ptr<OHOS::Rosen::RSTransaction>& rsTransaction = nullptr,
97         const std::map<OHOS::Rosen::AvoidAreaType, OHOS::Rosen::AvoidArea>& avoidAreas = {}) override;
98     void UpdateViewportConfigWithAnimation(const ViewportConfig& config, OHOS::Rosen::WindowSizeChangeReason reason,
99         AnimationOption animationOpt, const std::shared_ptr<OHOS::Rosen::RSTransaction>& rsTransaction = nullptr,
100         const std::map<OHOS::Rosen::AvoidAreaType, OHOS::Rosen::AvoidArea>& avoidAreas = {});
101     void UIExtensionUpdateViewportConfig(const ViewportConfig& config);
102     void UpdateWindowMode(OHOS::Rosen::WindowMode mode, bool hasDeco = true) override;
103     void UpdateDecorVisible(bool visible, bool hasDeco) override;
104     void HideWindowTitleButton(bool hideSplit, bool hideMaximize, bool hideMinimize, bool hideClose) override;
105     void SetIgnoreViewSafeArea(bool ignoreViewSafeArea) override;
106     void UpdateMaximizeMode(OHOS::Rosen::MaximizeMode mode) override;
107     void UpdateTitleInTargetPos(bool isShow, int32_t height) override;
108     void NotifyRotationAnimationEnd() override;
109 
110     void ChangeSensitiveNodes(bool isSensitive) override;
111 
112     // Window color
113     uint32_t GetBackgroundColor() override;
114     void SetBackgroundColor(uint32_t color) override;
115     void SetWindowContainerColor(uint32_t activeColor, uint32_t inactiveColor) override;
116 
117     bool NeedSoftKeyboard() override;
118 
119     void SetOnWindowFocused(const std::function<void()>& callback) override;
120 
121     // Current paintSize of window
122     void GetAppPaintSize(OHOS::Rosen::Rect& paintrect) override;
123 
124     // Get paintSize of window by calculating
125     void GetWindowPaintSize(OHOS::Rosen::Rect& paintrect) override;
126 
127     void DumpInfo(const std::vector<std::string>& params, std::vector<std::string>& info) override;
128 
129     // Set UIContent callback for custom window animation
130     void SetNextFrameLayoutCallback(std::function<void()>&& callback) override;
131 
132     // Set UIContent callback after layout finish
133     void SetFrameLayoutFinishCallback(std::function<void()>&& callback) override;
134 
135     // Set UIContent callback after lastest layout finish
136     void SetLastestFrameLayoutFinishCallback(std::function<void()>&& callback) override;
137 
138     // Receive memory level notification
139     void NotifyMemoryLevel(int32_t level) override;
140 
141     void SetAppWindowTitle(const std::string& title) override;
142     void SetAppWindowIcon(const std::shared_ptr<Media::PixelMap>& pixelMap) override;
143 
144     // ArkTS Form
145     void PreInitializeForm(OHOS::Rosen::Window* window, const std::string& url, napi_value storage) override;
146     void RunFormPage() override;
147     std::shared_ptr<Rosen::RSSurfaceNode> GetFormRootNode() override;
148     void UpdateFormData(const std::string& data) override;
149     void UpdateFormSharedImage(const std::map<std::string, sptr<OHOS::AppExecFwk::FormAshmem>>& imageDataMap) override;
150     void ReloadForm(const std::string& url) override;
151 
152     void SetFormWidth(float width) override
153     {
154         formWidth_ = width;
155     }
156     void SetFormHeight(float height) override
157     {
158         formHeight_ = height;
159     }
160     float GetFormWidth() override
161     {
162         return formWidth_;
163     }
164     float GetFormHeight() override
165     {
166         return formHeight_;
167     }
168 
169     void SetActionEventHandler(std::function<void(const std::string& action)>&& actionCallback) override;
170     void SetErrorEventHandler(std::function<void(const std::string&, const std::string&)>&& errorCallback) override;
171     void SetFormLinkInfoUpdateHandler(std::function<void(const std::vector<std::string>&)>&& callback) override;
172 
173     void OnFormSurfaceChange(float width, float height,
174         OHOS::Rosen::WindowSizeChangeReason type = static_cast<OHOS::Rosen::WindowSizeChangeReason>(0),
175         const std::shared_ptr<Rosen::RSTransaction>& rsTransaction = nullptr) override;
176 
177     void SetFormBackgroundColor(const std::string& color) override;
178 
179     void RegisterAccessibilityChildTree(
180         uint32_t parentWindowId, int32_t parentTreeId, int64_t parentElementId) override;
181     void SetAccessibilityGetParentRectHandler(std::function<void(int32_t&, int32_t&)>&& callback) override;
182     void SetAccessibilityGetParentRectHandler(
183         std::function<void(AccessibilityParentRectInfo&)>&& callback) override;
184     void DeregisterAccessibilityChildTree() override;
185     void AccessibilityDumpChildInfo(const std::vector<std::string>& params, std::vector<std::string>& info) override;
186 
187     void SetFontScaleFollowSystem(const bool fontScaleFollowSystem) override;
188 
189     SerializeableObjectArray DumpUITree() override
190     {
191         CHECK_NULL_RETURN(uiManager_, SerializeableObjectArray());
192         return uiManager_->DumpUITree();
193     }
194     void SubscribeUpdate(const std::function<void(int32_t, SerializeableObjectArray&)>& onUpdate) override
195     {
196         CHECK_NULL_VOID(uiManager_);
197         return uiManager_->SubscribeUpdate(onUpdate);
198     }
199     void UnSubscribeUpdate() override
200     {
201         CHECK_NULL_VOID(uiManager_);
202         uiManager_->UnSubscribeUpdate();
203     }
204     void ProcessSerializeableInputEvent(const SerializeableObjectArray& array) override
205     {
206         CHECK_NULL_VOID(uiManager_);
207         uiManager_->ProcessSerializeableInputEvent(array);
208     }
209     void RestoreUITree(const SerializeableObjectArray& array) override
210     {
211         CHECK_NULL_VOID(uiManager_);
212         uiManager_->RestoreUITree(array);
213     }
214     void UpdateUITree(const SerializeableObjectArray& array) override
215     {
216         CHECK_NULL_VOID(uiManager_);
217         uiManager_->UpdateUITree(array);
218     }
219     void SubscribeInputEventProcess(const std::function<void(SerializeableObjectArray&)>& onEvent) override
220     {
221         CHECK_NULL_VOID(uiManager_);
222         uiManager_->SubscribeInputEventProcess(onEvent);
223     }
224     void UnSubscribeInputEventProcess() override
225     {
226         CHECK_NULL_VOID(uiManager_);
227         uiManager_->UnSubscribeInputEventProcess();
228     }
229     void GetResourcePaths(std::vector<std::string>& resourcesPaths, std::string& assetRootPath,
230         std::vector<std::string>& assetBasePaths, std::string& resFolderName) override;
231     void SetResourcePaths(const std::vector<std::string>& resourcesPaths, const std::string& assetRootPath,
232         const std::vector<std::string>& assetBasePaths) override;
233 
234     napi_value GetUINapiContext() override;
235     void SetIsFocusActive(bool isFocusActive) override;
236 
237     void UpdateResource() override;
238 
239     int32_t CreateModalUIExtension(const AAFwk::Want& want,
240         const ModalUIExtensionCallbacks& callbacks, const ModalUIExtensionConfig& config) override;
241     void CloseModalUIExtension(int32_t sessionId) override;
242 
243     void SetParentToken(sptr<IRemoteObject> token) override;
244     sptr<IRemoteObject> GetParentToken() override;
245     AbilityBase::AutoFillType ViewDataToType(const AbilityBase::ViewData& viewData);
246     bool DumpViewData(AbilityBase::ViewData& viewData, AbilityBase::AutoFillType& type) override;
247     bool CheckNeedAutoSave() override;
248     bool DumpViewData(const RefPtr<NG::FrameNode>& node, RefPtr<ViewDataWrap> viewDataWrap,
249         bool skipSubAutoFillContainer = false, bool needsRecordData = false);
250 
251     void SearchElementInfoByAccessibilityId(
252         int64_t elementId, int32_t mode,
253         int64_t baseParent, std::list<Accessibility::AccessibilityElementInfo>& output) override;
254 
255     void SearchElementInfosByText(
256         int64_t elementId, const std::string& text, int64_t baseParent,
257         std::list<Accessibility::AccessibilityElementInfo>& output) override;
258 
259     void FindFocusedElementInfo(
260         int64_t elementId, int32_t focusType,
261         int64_t baseParent, Accessibility::AccessibilityElementInfo& output) override;
262 
263     void FocusMoveSearch(
264         int64_t elementId, int32_t direction,
265         int64_t baseParent, Accessibility::AccessibilityElementInfo& output) override;
266 
267     bool NotifyExecuteAction(int64_t elementId, const std::map<std::string, std::string>& actionArguments,
268         int32_t action, int64_t offset) override;
269 
270     void HandleAccessibilityHoverEvent(float pointX, float pointY, int32_t sourceType,
271         int32_t eventType, int64_t timeMs) override;
272 
273     int32_t GetInstanceId() override
274     {
275         return instanceId_;
276     }
277 
278     std::string RecycleForm() override;
279 
280     void RecoverForm(const std::string& statusData) override;
281     Shadow GetPopupShadow();
282 
283     int32_t CreateCustomPopupUIExtension(const AAFwk::Want& want,
284         const ModalUIExtensionCallbacks& callbacks, const CustomPopupUIExtensionConfig& config) override;
285     bool GetTargetNode(
286         int32_t& nodeIdLabel, RefPtr<NG::FrameNode>& targetNode, const CustomPopupUIExtensionConfig& config);
287     void DestroyCustomPopupUIExtension(int32_t nodeId) override;
288     void UpdateCustomPopupUIExtension(const CustomPopupUIExtensionConfig& config) override;
289 
290     void SetContainerModalTitleVisible(bool customTitleSettedShow, bool floatingTitleSettedShow) override;
291     void SetContainerModalTitleHeight(int32_t height) override;
292     int32_t GetContainerModalTitleHeight() override;
293     bool GetContainerModalButtonsRect(Rosen::Rect& containerModal, Rosen::Rect& buttons) override;
294     void SubscribeContainerModalButtonsRectChange(
295         std::function<void(Rosen::Rect& containerModal, Rosen::Rect& buttons)>&& callback) override;
296     void UpdateTransform(const OHOS::Rosen::Transform& transform) override;
297 
298     SerializedGesture GetFormSerializedGesture() override;
299 
300     void SetUIExtensionSubWindow(bool isUIExtensionSubWindow) override
301     {
302         isUIExtensionSubWindow_ = isUIExtensionSubWindow;
303     }
304 
305     bool IsUIExtensionSubWindow() override
306     {
307         return isUIExtensionSubWindow_;
308     }
309 
310     void SetUIExtensionAbilityProcess(bool isUIExtensionAbilityProcess) override
311     {
312         isUIExtensionAbilityProcess_ = isUIExtensionAbilityProcess;
313     }
314 
315     bool IsUIExtensionAbilityProcess() override
316     {
317         return isUIExtensionAbilityProcess_;
318     }
319 
320     void SetUIExtensionAbilityHost(bool isUIExtensionAbilityHost) override
321     {
322         isUIExtensionAbilityHost_ = isUIExtensionAbilityHost;
323     }
324 
325     bool IsUIExtensionAbilityHost() override
326     {
327         return isUIExtensionAbilityHost_;
328     }
329 
330     std::vector<Ace::RectF> GetOverlayNodePositions() const override;
331 
332     void RegisterOverlayNodePositionsUpdateCallback(
333         const std::function<void(std::vector<Ace::RectF>)>& callback) const override;
334 
335     void SetFormRenderingMode(int8_t renderMode) override;
336 
337     void SetContentNodeGrayScale(float grayscale) override;
338 
339     void PreLayout() override;
340 
341     sptr<IRemoteObject> GetRemoteObj() override
342     {
343         return instance_;
344     }
345 
346     void SetStatusBarItemColor(uint32_t color) override;
347 
348     void SetFontScaleAndWeightScale(const RefPtr<Platform::AceContainer>& container, int32_t instanceId);
349 
350     void SetForceSplitEnable(bool isForceSplit, const std::string& homePage) override;
351 
352     void UpdateDialogContainerConfig(const std::shared_ptr<OHOS::AppExecFwk::Configuration>& config);
353 
AddDestructCallback(void* key, const std::function<void()>& callback)354     void AddDestructCallback(void* key, const std::function<void()>& callback)
355     {
356         destructCallbacks_.emplace(key, callback);
357     }
358 
RemoveDestructCallback(void* key)359     void RemoveDestructCallback(void* key)
360     {
361         destructCallbacks_.erase(key);
362     }
363 
364 private:
365     UIContentErrorCode InitializeInner(
366         OHOS::Rosen::Window* window, const std::string& contentInfo, napi_value storage, bool isNamedRouter);
367     UIContentErrorCode CommonInitialize(
368         OHOS::Rosen::Window* window, const std::string& contentInfo, napi_value storage, uint32_t focusWindowId = 0);
369     UIContentErrorCode CommonInitializeForm(
370         OHOS::Rosen::Window* window, const std::string& contentInfo, napi_value storage);
371     void InitializeSubWindow(OHOS::Rosen::Window* window, bool isDialog = false);
372     void DestroyCallback() const;
373     void ProcessDestructCallbacks();
374     void SetConfiguration(const std::shared_ptr<OHOS::AppExecFwk::Configuration>& config);
375 
376     void InitializeSafeArea(const RefPtr<Platform::AceContainer>& container);
377     void InitializeDisplayAvailableRect(const RefPtr<Platform::AceContainer>& container);
378 
379     void InitDragSummaryMap(const RefPtr<Platform::AceContainer>& container);
380 
381     RefPtr<PopupParam> CreateCustomPopupParam(bool isShow, const CustomPopupUIExtensionConfig& config);
382     void OnPopupStateChange(const std::string& event, const CustomPopupUIExtensionConfig& config, int32_t nodeId);
383     void SetCustomPopupConfig(int32_t nodeId, const CustomPopupUIExtensionConfig& config, int32_t popupId);
384 
385     void AddWatchSystemParameter();
386     void StoreConfiguration(const std::shared_ptr<OHOS::AppExecFwk::Configuration>& config);
387 
388     std::weak_ptr<OHOS::AbilityRuntime::Context> context_;
389     void* runtime_ = nullptr;
390     OHOS::Rosen::Window* window_ = nullptr;
391     std::string startUrl_;
392     int32_t instanceId_ = -1;
393     OHOS::sptr<OHOS::Rosen::IWindowDragListener> dragWindowListener_ = nullptr;
394     OHOS::sptr<OHOS::Rosen::IOccupiedAreaChangeListener> occupiedAreaChangeListener_ = nullptr;
395     OHOS::sptr<OHOS::Rosen::IAvoidAreaChangedListener> avoidAreaChangedListener_ = nullptr;
396     OHOS::sptr<OHOS::Rosen::DisplayManager::IFoldStatusListener> foldStatusListener_ = nullptr;
397     OHOS::sptr<OHOS::Rosen::DisplayManager::IDisplayModeListener> foldDisplayModeListener_ = nullptr;
398     OHOS::sptr<OHOS::Rosen::DisplayManager::IAvailableAreaListener> availableAreaChangedListener_ = nullptr;
399 
400     // ITouchOutsideListener is used for touching out of hot areas of window.
401     OHOS::sptr<OHOS::Rosen::ITouchOutsideListener> touchOutsideListener_ = nullptr;
402 
403     // ArkTS Form
404     bool isFormRender_ = false;
405     bool isFormRenderInit_ = false;
406     std::string bundleName_;
407     std::string moduleName_;
408     std::string hapPath_;
409     bool isBundle_ = false;
410     float formWidth_ = 0.0;
411     float formHeight_ = 0.0;
412     std::string formData_;
413     bool fontScaleFollowSystem_ = true;
414     std::map<std::string, sptr<OHOS::AppExecFwk::FormAshmem>> formImageDataMap_;
415     std::unordered_map<int32_t, CustomPopupUIExtensionConfig> customPopupConfigMap_;
416     std::unordered_map<int32_t, int32_t> popupUIExtensionRecords_;
417     std::unique_ptr<DistributedUIManager> uiManager_;
418 
419     bool isDynamicRender_ = false;
420     int32_t hostInstanceId_ = -1;
421     UIContentType uIContentType_ = UIContentType::UNDEFINED;
422     std::shared_ptr<TaskWrapper> taskWrapper_;
423     std::vector<std::string> registerComponents_;
424 
425     sptr<IRemoteObject> parentToken_ = nullptr;
426     sptr<IRemoteObject> instance_ = new (std::nothrow) UIContentServiceStubImpl();
427     bool isUIExtensionSubWindow_ = false;
428     bool isUIExtensionAbilityProcess_ = false;
429     bool isUIExtensionAbilityHost_ = false;
430     RefPtr<UpdateConfigManager<AceViewportConfig>> viewportConfigMgr_ =
431         Referenced::MakeRefPtr<UpdateConfigManager<AceViewportConfig>>();
432     std::unordered_map<void*, std::function<void()>> destructCallbacks_;
433 
434     SingleTaskExecutor::CancelableTask updateDecorVisibleTask_;
435 };
436 
437 } // namespace OHOS::Ace
438 
439 #endif // FOUNDATION_ACE_ADAPTER_OHOS_ENTRANCE_ACE_UI_CONTENT_IMPL_H
440