• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2025 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_NG_PATTERN_UI_EXTENSION_UEC_UI_EXTENSION_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_UI_EXTENSION_UEC_UI_EXTENSION_PATTERN_H
18 
19 #include <cstdint>
20 #include <functional>
21 #include <list>
22 #include <memory>
23 #include <optional>
24 #include <refbase.h>
25 #include <vector>
26 
27 #include "base/memory/referenced.h"
28 #include "base/want/want_wrap.h"
29 #include "core/common/container.h"
30 #include "core/components_ng/event/gesture_event_hub.h"
31 #include "core/components_ng/manager/avoid_info/avoid_info_manager.h"
32 #include "core/components_ng/pattern/pattern.h"
33 #include "core/components_ng/pattern/ui_extension/accessibility_session_adapter_ui_extension.h"
34 #include "core/components_ng/pattern/ui_extension/platform_event_proxy.h"
35 #include "core/components_ng/pattern/ui_extension/session_wrapper.h"
36 #include "core/components_ng/pattern/ui_extension/ui_extension_config.h"
37 #ifdef SUPPORT_DIGITAL_CROWN
38 #include "core/event/crown_event.h"
39 #endif
40 #include "core/event/mouse_event.h"
41 #include "core/event/touch_event.h"
42 
43 #define UIEXT_LOGD(fmt, ...)                                                                                      \
44     TAG_LOGD(AceLogTag::ACE_UIEXTENSIONCOMPONENT, "[@%{public}d][ID: %{public}d] " fmt, __LINE__, uiExtensionId_, \
45         ##__VA_ARGS__)
46 #define UIEXT_LOGI(fmt, ...)                                                                                      \
47     TAG_LOGI(AceLogTag::ACE_UIEXTENSIONCOMPONENT, "[@%{public}d][ID: %{public}d] " fmt, __LINE__, uiExtensionId_, \
48         ##__VA_ARGS__)
49 #define UIEXT_LOGW(fmt, ...)                                                                                      \
50     TAG_LOGW(AceLogTag::ACE_UIEXTENSIONCOMPONENT, "[@%{public}d][ID: %{public}d] " fmt, __LINE__, uiExtensionId_, \
51         ##__VA_ARGS__)
52 #define UIEXT_LOGE(fmt, ...)                                                                                      \
53     TAG_LOGE(AceLogTag::ACE_UIEXTENSIONCOMPONENT, "[@%{public}d][ID: %{public}d] " fmt, __LINE__, uiExtensionId_, \
54         ##__VA_ARGS__)
55 #define UIEXT_LOGF(fmt, ...)                                                                                      \
56     TAG_LOGF(AceLogTag::ACE_UIEXTENSIONCOMPONENT, "[@%{public}d][ID: %{public}d] " fmt, __LINE__, uiExtensionId_, \
57         ##__VA_ARGS__)
58 
59 namespace OHOS::Accessibility {
60 class AccessibilityElementInfo;
61 class AccessibilityEventInfo;
62 } // namespace OHOS::Accessibility
63 
64 namespace OHOS::MMI {
65 class KeyEvent;
66 class PointerEvent;
67 } // namespace OHOS::MMI
68 
69 namespace OHOS::Ace {
70 class ModalUIExtensionProxy;
71 } // namespace OHOS::Ace
72 
73 namespace OHOS::Rosen {
74 class AvoidArea;
75 class RSTransaction;
76 enum class WindowMode : uint32_t;
77 } // namespace OHOS::Rosen
78 
79 namespace OHOS::Ace::NG {
80 
81 struct SessionViewportConfig {
82     bool isDensityFollowHost_ = false;
83     float density_ = 1.0f;
84     uint64_t displayId_ = 0;
85     int32_t orientation_ = 0;
86     uint32_t transform_ = 0;
87 };
88 using BusinessDataUECConsumeCallback = std::function<int32_t(const AAFwk::Want&)>;
89 using BusinessDataUECConsumeReplyCallback = std::function<int32_t(const AAFwk::Want&, std::optional<AAFwk::Want>&)>;
90 
91 class UIExtensionProxy;
92 class UIExtensionPattern : public Pattern {
93     DECLARE_ACE_TYPE(UIExtensionPattern, Pattern);
94 
95 public:
96     explicit UIExtensionPattern(bool isTransferringCaller = false, bool isModal = false,
97         bool isAsyncModalBinding = false, SessionType sessionType = SessionType::UI_EXTENSION_ABILITY);
98     ~UIExtensionPattern() override;
99 
100     void Initialize();
101     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override;
102     FocusPattern GetFocusPattern() const override;
103     RefPtr<AccessibilitySessionAdapter> GetAccessibilitySessionAdapter() override;
104 
SetPlaceholderMap(const std::map<PlaceholderType,RefPtr<NG::FrameNode>> & placeholderMap)105     void SetPlaceholderMap(const std::map<PlaceholderType, RefPtr<NG::FrameNode>>& placeholderMap)
106     {
107         placeholderMap_ = placeholderMap;
108     }
109     void UpdateWant(const RefPtr<OHOS::Ace::WantWrap>& wantWrap);
110     void UpdateWant(const AAFwk::Want& want);
111 
112     void OnWindowShow() override;
113     void OnWindowHide() override;
114     void OnWindowSizeChanged(int32_t width, int32_t height, WindowSizeChangeReason type) override;
115     void OnVisibleChange(bool visible) override;
116     void OnMountToParentDone() override;
117     void AfterMountToParent() override;
118     void OnSyncGeometryNode(const DirtySwapConfig& config) override;
119     void RegisterWindowSceneVisibleChangeCallback(const RefPtr<Pattern>& windowScenePattern);
120     void UnRegisterWindowSceneVisibleChangeCallback(int32_t nodeId);
121     void OnWindowSceneVisibleChange(bool visible);
122     void OnAttachToMainTree() override;
123     void OnDetachFromMainTree() override;
124     void OnAttachContext(PipelineContext *context) override;
125     void OnDetachContext(PipelineContext *context) override;
126 
127     void OnConnect();
128     void OnDisconnect(bool isAbnormal);
129     void HandleDragEvent(const DragPointerEvent& info) override;
130 
131     void SetModalOnDestroy(const std::function<void()>&& callback);
132     void FireModalOnDestroy();
133     void SetModalOnRemoteReadyCallback(
134         const std::function<void(const std::shared_ptr<ModalUIExtensionProxy>&)>&& callback);
135     void SetOnRemoteReadyCallback(const std::function<void(const RefPtr<UIExtensionProxy>&)>&& callback);
136     void FireOnRemoteReadyCallback();
137     void SetOnReleaseCallback(const std::function<void(int32_t)>&& callback);
138     void FireOnReleaseCallback(int32_t releaseCode);
139     void SetOnResultCallback(const std::function<void(int32_t, const AAFwk::Want&)>&& callback);
140     void FireOnResultCallback(int32_t code, const AAFwk::Want& want);
141     void SetOnTerminatedCallback(const std::function<void(int32_t, const RefPtr<WantWrap>&)>&& callback);
142     void FireOnTerminatedCallback(int32_t code, const RefPtr<WantWrap>& wantWrap);
143     void SetOnReceiveCallback(const std::function<void(const AAFwk::WantParams&)>&& callback);
144     void FireOnReceiveCallback(const AAFwk::WantParams& params);
145     void SetOnErrorCallback(
146         const std::function<void(int32_t code, const std::string& name, const std::string& message)>&& callback);
147     void FireOnErrorCallback(int32_t code, const std::string& name, const std::string& message);
148     void SetSyncCallbacks(const std::list<std::function<void(const RefPtr<UIExtensionProxy>&)>>&& callbackList);
149     void FireSyncCallbacks();
150     void SetAsyncCallbacks(const std::list<std::function<void(const RefPtr<UIExtensionProxy>&)>>&& callbackList);
151     void FireAsyncCallbacks();
152     void SetBindModalCallback(const std::function<void()>&& callback);
153     void FireBindModalCallback();
154     void SetDensityDpi(bool densityDpi);
155     bool GetDensityDpi();
156     bool IsCompatibleOldVersion();
157 
158     void NotifySizeChangeReason(
159         WindowSizeChangeReason type, const std::shared_ptr<Rosen::RSTransaction>& rsTransaction);
160     void NotifyForeground();
161     void NotifyBackground(bool isHandleError = true);
162     void NotifyDestroy();
163     int32_t GetInstanceId() const;
164     int32_t GetSessionId() const;
165     int32_t GetNodeId() const;
166     int32_t GetUiExtensionId() override;
GetSessionWrapper()167     RefPtr<SessionWrapper> GetSessionWrapper()
168     {
169         return sessionWrapper_;
170     }
171     int64_t WrapExtensionAbilityId(int64_t extensionOffset, int64_t abilityId) override;
172     void DispatchOriginAvoidArea(const Rosen::AvoidArea& avoidArea, uint32_t type);
173     void HandleVisibleAreaChange(bool visible, double ratio);
174     void SetWantWrap(const RefPtr<OHOS::Ace::WantWrap>& wantWrap);
175     RefPtr<OHOS::Ace::WantWrap> GetWantWrap();
IsShowPlaceholder()176     bool IsShowPlaceholder()
177     {
178         return (curPlaceholderType_ != PlaceholderType::NONE);
179     }
IsCanMountPlaceholder(PlaceholderType type)180     bool IsCanMountPlaceholder(PlaceholderType type)
181     {
182         return (static_cast<int32_t>(type) > static_cast<int32_t>(curPlaceholderType_));
183     }
SetCurPlaceholderType(PlaceholderType type)184     void SetCurPlaceholderType(PlaceholderType type)
185     {
186         curPlaceholderType_ = type;
187     }
188     void PostDelayRemovePlaceholder(uint32_t delay);
189     void SetEventProxyFlag(int32_t flag);
190     void ReplacePlaceholderByContent();
191     void OnExtensionEvent(UIExtCallbackEventId eventId);
192     void OnUeaAccessibilityEventAsync();
193     void OnAreaUpdated();
194     bool IsModalUec();
195     bool IsForeground();
196     void OnExtensionDetachToDisplay();
197 
198     void OnAccessibilityEvent(const Accessibility::AccessibilityEventInfo& info, int64_t uiExtensionOffset);
SetModalFlag(bool isModal)199     void SetModalFlag(bool isModal)
200     {
201         isModal_ = isModal;
202     }
203     void OnAccessibilityChildTreeRegister(uint32_t windowId, int32_t treeId, int64_t accessibilityId);
204     void OnAccessibilityChildTreeDeregister();
205     void OnSetAccessibilityChildTree(int32_t childWindowId, int32_t childTreeId);
206     void OnAccessibilityDumpChildInfo(const std::vector<std::string>& params, std::vector<std::string>& info);
SetViewportConfigChanged(bool ViewportConfigChanged)207     void SetViewportConfigChanged(bool ViewportConfigChanged)
208     {
209         viewportConfigChanged_ = ViewportConfigChanged;
210     }
IsViewportConfigChanged()211     bool IsViewportConfigChanged() const
212     {
213         return viewportConfigChanged_;
214     }
IsForeground()215     bool IsForeground() const
216     {
217         return state_ == AbilityState::FOREGROUND;
218     }
SetSessionViewportConfig(const SessionViewportConfig & config)219     void SetSessionViewportConfig(const SessionViewportConfig& config)
220     {
221         sessionViewportConfig_ = config;
222     }
GetSessionViewportConfig()223     const SessionViewportConfig& GetSessionViewportConfig() const
224     {
225         return sessionViewportConfig_;
226     }
227     void DumpInfo() override;
228     void DumpInfo(std::unique_ptr<JsonValue>& json) override;
229     void DumpOthers();
230     int32_t GetInstanceIdFromHost() const;
231     bool SendBusinessDataSyncReply(UIContentBusinessCode code, const AAFwk::Want& data, AAFwk::Want& reply,
232         RSSubsystemId subSystemId = RSSubsystemId::ARKUI_UIEXT);
233     bool SendBusinessData(UIContentBusinessCode code, const AAFwk::Want& data, BusinessDataSendType type,
234         RSSubsystemId subSystemId = RSSubsystemId::ARKUI_UIEXT);
235     void OnUIExtBusinessReceiveReply(
236         UIContentBusinessCode code, const AAFwk::Want& data, std::optional<AAFwk::Want>& reply);
237     void OnUIExtBusinessReceive(UIContentBusinessCode code, const AAFwk::Want& data);
238     void RegisterUIExtBusinessConsumeCallback(UIContentBusinessCode code, BusinessDataUECConsumeCallback callback);
239     void RegisterUIExtBusinessConsumeReplyCallback(
240         UIContentBusinessCode code, BusinessDataUECConsumeReplyCallback callback);
241     void SetOnDrawReadyCallback(const std::function<void()>&& callback);
242 
SetIsWindowModeFollowHost(bool isWindowModeFollowHost)243     void SetIsWindowModeFollowHost(bool isWindowModeFollowHost)
244     {
245         isWindowModeFollowHost_ = isWindowModeFollowHost;
246     }
GetIsWindowModeFollowHost()247     bool GetIsWindowModeFollowHost() const
248     {
249         return isWindowModeFollowHost_;
250     }
251     void NotifyHostWindowMode(Rosen::WindowMode mode);
252     void NotifyHostWindowMode();
253 
254     void TransferAccessibilityRectInfo(bool isForce = false);
255     void OnFrameNodeChanged(FrameNodeChangeInfoFlag flag) override;
256     void UpdateWMSUIExtProperty(UIContentBusinessCode code, const AAFwk::Want& data, RSSubsystemId subSystemId);
257 
GetAvoidInfo()258     const ContainerModalAvoidInfo& GetAvoidInfo() const
259     {
260         return avoidInfo_;
261     }
SetAvoidInfo(const ContainerModalAvoidInfo & info)262     void SetAvoidInfo(const ContainerModalAvoidInfo& info)
263     {
264         avoidInfo_ = info;
265     }
266 
267 protected:
268     virtual void DispatchPointerEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent);
269     virtual void DispatchKeyEvent(const KeyEvent& event);
270 
271     int32_t uiExtensionId_ = 0;
272     int32_t instanceId_ = Container::CurrentId();
273     void FireOnDrawReadyCallback();
274 private:
275     enum class AbilityState {
276         NONE = 0,
277         FOREGROUND,
278         BACKGROUND,
279         DESTRUCTION,
280     };
281 
282     struct ErrorMsg {
283         int32_t code = 0;
284         std::string name;
285         std::string message;
286     };
287 
288     const char* ToString(AbilityState state);
289     void OnAttachToFrameNode() override;
290     void OnDetachFromFrameNode(FrameNode* frameNode) override;
291     void OnLanguageConfigurationUpdate() override;
292     void OnColorConfigurationUpdate() override;
293     void OnModifyDone() override;
294     bool CheckConstraint();
295 
296     void InitKeyEventOnFocus(const RefPtr<FocusHub>& focusHub);
297     void InitKeyEventOnBlur(const RefPtr<FocusHub>& focusHub);
298     void InitKeyEventOnClearFocusState(const RefPtr<FocusHub>& focusHub);
299     void InitKeyEventOnPaintFocusState(const RefPtr<FocusHub>& focusHub);
300     void InitKeyEventOnKeyEvent(const RefPtr<FocusHub>& focusHub);
301 #ifdef SUPPORT_DIGITAL_CROWN
302     void InitCrownEvent(const RefPtr<FocusHub>& focusHub);
303     void HandleCrownEvent(const CrownEvent& event);
304 #endif
305     void InitKeyEvent(const RefPtr<FocusHub>& focusHub);
306     void InitTouchEvent(const RefPtr<GestureEventHub>& gestureHub);
307     void InitMouseEvent(const RefPtr<InputEventHub>& inputHub);
308     void InitHoverEvent(const RefPtr<InputEventHub>& inputHub);
309     void InitializeAccessibility();
310     bool HandleKeyEvent(const KeyEvent& event);
311     void HandleFocusEvent();
312     void HandleBlurEvent();
313     void HandleTouchEvent(const TouchEventInfo& info);
314     void HandleMouseEvent(const MouseInfo& info);
315     void HandleHoverEvent(bool isHover);
316     bool DispatchKeyEventSync(const KeyEvent& event);
317     void DispatchFocusActiveEvent(bool isFocusActive);
318     void DispatchFocusState(bool focusState);
319     void DispatchDisplayArea(bool isForce = false);
320     void LogoutModalUIExtension();
321     bool IsMoving();
322     void UnRegisterEvent(int32_t instanceId);
323     void UnRegisterPipelineEvent(int32_t instanceId);
324     void UnRegisterPipelineEvent(
325         const RefPtr<PipelineContext>& pipeline, FrameNode* frameNode);
326     void UnRegisterUIExtensionManagerEvent(int32_t instanceId);
327     void RegisterEvent(int32_t instanceId);
328     void RegisterPipelineEvent(int32_t instanceId);
329     void RegisterPipelineEvent(const RefPtr<PipelineContext>& pipeline);
330     void RegisterUIExtensionManagerEvent(int32_t instanceId);
331     void UpdateSessionInstanceId(int32_t instanceId);
332 
333     void RegisterVisibleAreaChange();
334     void MountPlaceholderNode(PlaceholderType type);
335     void RemovePlaceholderNode();
SetFoldStatusChanged(bool isChanged)336     void SetFoldStatusChanged(bool isChanged)
337     {
338         isFoldStatusChanged_ = isChanged;
339     }
IsFoldStatusChanged()340     bool IsFoldStatusChanged()
341     {
342         return isFoldStatusChanged_;
343     }
SetRotateStatusChanged(bool isChanged)344     void SetRotateStatusChanged(bool isChanged)
345     {
346         isRotateStatusChanged_ = isChanged;
347     }
IsRotateStatusChanged()348     bool IsRotateStatusChanged()
349     {
350         return isRotateStatusChanged_;
351     }
352     PlaceholderType GetSizeChangeReason();
353     UIExtensionUsage GetUIExtensionUsage(const AAFwk::Want& want);
354     void ReDispatchDisplayArea();
355     void ResetAccessibilityChildTreeCallback();
GetForceProcessOnKeyEventInternal()356     bool GetForceProcessOnKeyEventInternal() const
357     {
358         return forceProcessOnKeyEventInternal_;
359     }
SetForceProcessOnKeyEventInternal(bool forceProcessOnKeyEventInternal)360     void SetForceProcessOnKeyEventInternal(bool forceProcessOnKeyEventInternal)
361     {
362         forceProcessOnKeyEventInternal_ = forceProcessOnKeyEventInternal;
363     }
364     void InitBusinessDataHandleCallback();
365     void RegisterEventProxyFlagCallback();
366 
367     void RegisterGetAvoidInfoCallback();
368     void RegisterReplyPageModeCallback();
369     void UpdateFrameNodeState();
370     bool IsAncestorNodeGeometryChange(FrameNodeChangeInfoFlag flag);
371     bool IsAncestorNodeTransformChange(FrameNodeChangeInfoFlag flag);
372     AccessibilityParentRectInfo GetAccessibilityRectInfo() const;
373 
374     RefPtr<TouchEventImpl> touchEvent_;
375     RefPtr<InputEvent> mouseEvent_;
376     RefPtr<InputEvent> hoverEvent_;
377     std::shared_ptr<MMI::PointerEvent> lastPointerEvent_ = nullptr;
378     std::shared_ptr<AccessibilityChildTreeCallback> accessibilityChildTreeCallback_;
379 
380     std::function<void()> onModalDestroy_;
381     std::function<void(const std::shared_ptr<ModalUIExtensionProxy>&)> onModalRemoteReadyCallback_;
382     std::function<void(const RefPtr<UIExtensionProxy>&)> onRemoteReadyCallback_;
383     std::function<void(int32_t)> onReleaseCallback_;
384     std::function<void(int32_t, const AAFwk::Want&)> onResultCallback_;
385     std::function<void(int32_t, const RefPtr<WantWrap>&)> onTerminatedCallback_;
386     std::function<void(const AAFwk::WantParams&)> onReceiveCallback_;
387     std::function<void(int32_t code, const std::string& name, const std::string& message)> onErrorCallback_;
388     std::list<std::function<void(const RefPtr<UIExtensionProxy>&)>> onSyncOnCallbackList_;
389     std::list<std::function<void(const RefPtr<UIExtensionProxy>&)>> onAsyncOnCallbackList_;
390     std::function<void()> bindModalCallback_;
391     std::map<PlaceholderType, RefPtr<NG::FrameNode>> placeholderMap_;
392     std::function<void()> onDrawReadyCallback_;
393 
394     RefPtr<OHOS::Ace::WantWrap> curWant_;
395     RefPtr<FrameNode> contentNode_;
396     RefPtr<SessionWrapper> sessionWrapper_;
397     RefPtr<AccessibilitySessionAdapterUIExtension> accessibilitySessionAdapter_;
398     RefPtr<PlatformEventProxy> platformEventProxy_;
399     ErrorMsg lastError_;
400     AbilityState state_ = AbilityState::NONE;
401     bool isTransferringCaller_ = false;
402     bool isVisible_ = true;
403     bool isModal_ = false;
404     bool hasInitialize_ = false;
405     bool isAsyncModalBinding_ = false;
406     PlaceholderType curPlaceholderType_ = PlaceholderType::NONE;
407     bool isFoldStatusChanged_ = false;
408     bool isRotateStatusChanged_ = false;
409     bool densityDpi_ = false;
410     WeakPtr<Pattern> weakSystemWindowScene_;
411     SessionViewportConfig sessionViewportConfig_;
412     bool viewportConfigChanged_ = false;
413     bool displayAreaChanged_ = false;
414     bool isKeyAsync_ = false;
415     bool hasDetachContext_ = false;
416     // Whether to send the focus to the UIExtension
417     // No multi-threading problem due to run js thread
418     bool canFocusSendToUIExtension_ = true;
419     bool needReSendFocusToUIExtension_ = false;
420     int32_t surfacePositionCallBackId_ = -1;
421     int32_t foldDisplayCallBackId_ = -1;
422     RectF displayArea_;
423     // StartUIExtension should after mountToParent
424     bool hasMountToParent_ = false;
425     bool needReNotifyForeground_ = false;
426     bool needReDispatchDisplayArea_ = false;
427     bool curVisible_ = false;
428     SessionType sessionType_ = SessionType::UI_EXTENSION_ABILITY;
429     UIExtensionUsage usage_ = UIExtensionUsage::EMBEDDED;
430 
431     // UEC dump info
432     bool focusState_ = false;
433     uint32_t focusWindowId_ = 0;
434     uint32_t realHostWindowId_ = 0;
435     std::string want_;
436     bool forceProcessOnKeyEventInternal_ = false;
437     std::map<UIContentBusinessCode, BusinessDataUECConsumeCallback> businessDataUECConsumeCallbacks_;
438     std::map<UIContentBusinessCode, BusinessDataUECConsumeReplyCallback> businessDataUECConsumeReplyCallbacks_;
439 
440     bool isWindowModeFollowHost_ = false;
441     std::shared_ptr<AccessibilitySAObserverCallback> accessibilitySAObserverCallback_;
442 
443     ContainerModalAvoidInfo avoidInfo_;
444 
445     ACE_DISALLOW_COPY_AND_MOVE(UIExtensionPattern);
446 };
447 } // namespace OHOS::Ace::NG
448 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_UI_EXTENSION_UEC_UI_EXTENSION_PATTERN_H
449