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