1 /*
2  * Copyright (c) 2023-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_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_UI_EXTENSION_UI_EXTENSION_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_UI_EXTENSION_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/pattern/pattern.h"
32 #include "core/components_ng/pattern/ui_extension/session_wrapper.h"
33 #include "core/components_ng/pattern/ui_extension/ui_extension_config.h"
34 #include "core/components_ng/pattern/ui_extension/accessibility_session_adapter_ui_extension.h"
35 #include "core/event/mouse_event.h"
36 #include "core/event/touch_event.h"
37 
38 #define UIEXT_LOGD(fmt, ...)                                                                                      \
39     TAG_LOGD(AceLogTag::ACE_UIEXTENSIONCOMPONENT, "[@%{public}d][ID: %{public}d] " fmt, __LINE__, uiExtensionId_, \
40         ##__VA_ARGS__)
41 #define UIEXT_LOGI(fmt, ...)                                                                                      \
42     TAG_LOGI(AceLogTag::ACE_UIEXTENSIONCOMPONENT, "[@%{public}d][ID: %{public}d] " fmt, __LINE__, uiExtensionId_, \
43         ##__VA_ARGS__)
44 #define UIEXT_LOGW(fmt, ...)                                                                                      \
45     TAG_LOGW(AceLogTag::ACE_UIEXTENSIONCOMPONENT, "[@%{public}d][ID: %{public}d] " fmt, __LINE__, uiExtensionId_, \
46         ##__VA_ARGS__)
47 #define UIEXT_LOGE(fmt, ...)                                                                                      \
48     TAG_LOGE(AceLogTag::ACE_UIEXTENSIONCOMPONENT, "[@%{public}d][ID: %{public}d] " fmt, __LINE__, uiExtensionId_, \
49         ##__VA_ARGS__)
50 #define UIEXT_LOGF(fmt, ...)                                                                                      \
51     TAG_LOGF(AceLogTag::ACE_UIEXTENSIONCOMPONENT, "[@%{public}d][ID: %{public}d] " fmt, __LINE__, uiExtensionId_, \
52         ##__VA_ARGS__)
53 
54 namespace OHOS::Accessibility {
55 class AccessibilityElementInfo;
56 class AccessibilityEventInfo;
57 } // namespace OHOS::Accessibility
58 
59 namespace OHOS::MMI {
60 class KeyEvent;
61 class PointerEvent;
62 } // namespace OHOS::MMI
63 
64 namespace OHOS::Ace {
65 class ModalUIExtensionProxy;
66 } // namespace OHOS::Ace
67 
68 namespace OHOS::Rosen {
69 class AvoidArea;
70 class RSTransaction;
71 } // namespace OHOS::Rosen
72 
73 namespace OHOS::Ace::NG {
74 class UIExtensionProxy;
75 class UIExtensionPattern : public Pattern {
76     DECLARE_ACE_TYPE(UIExtensionPattern, Pattern);
77 
78 public:
79     explicit UIExtensionPattern(bool isTransferringCaller = false, bool isModal = false,
80         bool isAsyncModalBinding = false, SessionType sessionType = SessionType::UI_EXTENSION_ABILITY);
81     ~UIExtensionPattern() override;
82 
83     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override;
84     FocusPattern GetFocusPattern() const override;
85     RefPtr<AccessibilitySessionAdapter> GetAccessibilitySessionAdapter() override;
86 
SetPlaceholderMap(const std::map<PlaceholderType,RefPtr<NG::FrameNode>> & placeholderMap)87     void SetPlaceholderMap(const std::map<PlaceholderType, RefPtr<NG::FrameNode>>& placeholderMap)
88     {
89         placeholderMap_ = placeholderMap;
90     }
91     void UpdateWant(const RefPtr<OHOS::Ace::WantWrap>& wantWrap);
92     void UpdateWant(const AAFwk::Want& want);
93 
94     void OnWindowShow() override;
95     void OnWindowHide() override;
96     void OnWindowSizeChanged(int32_t width, int32_t height, WindowSizeChangeReason type) override;
97     void OnVisibleChange(bool visible) override;
98     void OnMountToParentDone() override;
99     void AfterMountToParent() override;
100     void OnSyncGeometryNode(const DirtySwapConfig& config) override;
101     void RegisterWindowSceneVisibleChangeCallback(const RefPtr<Pattern>& windowScenePattern);
102     void UnRegisterWindowSceneVisibleChangeCallback(int32_t nodeId);
103     void OnWindowSceneVisibleChange(bool visible);
104 
105     void OnConnect();
106     void OnDisconnect(bool isAbnormal);
107     void HandleDragEvent(const PointerEvent& info) override;
108 
109     void SetModalOnDestroy(const std::function<void()>&& callback);
110     void FireModalOnDestroy();
111     void SetModalOnRemoteReadyCallback(
112         const std::function<void(const std::shared_ptr<ModalUIExtensionProxy>&)>&& callback);
113     void SetOnRemoteReadyCallback(const std::function<void(const RefPtr<UIExtensionProxy>&)>&& callback);
114     void FireOnRemoteReadyCallback();
115     void SetOnReleaseCallback(const std::function<void(int32_t)>&& callback);
116     void FireOnReleaseCallback(int32_t releaseCode);
117     void SetOnResultCallback(const std::function<void(int32_t, const AAFwk::Want&)>&& callback);
118     void FireOnResultCallback(int32_t code, const AAFwk::Want& want);
119     void SetOnTerminatedCallback(const std::function<void(int32_t, const RefPtr<WantWrap>&)>&& callback);
120     void FireOnTerminatedCallback(int32_t code, const RefPtr<WantWrap>& wantWrap);
121     void SetOnReceiveCallback(const std::function<void(const AAFwk::WantParams&)>&& callback);
122     void FireOnReceiveCallback(const AAFwk::WantParams& params);
123     void SetOnErrorCallback(
124         const std::function<void(int32_t code, const std::string& name, const std::string& message)>&& callback);
125     void FireOnErrorCallback(int32_t code, const std::string& name, const std::string& message);
126     void SetSyncCallbacks(const std::list<std::function<void(const RefPtr<UIExtensionProxy>&)>>&& callbackList);
127     void FireSyncCallbacks();
128     void SetAsyncCallbacks(const std::list<std::function<void(const RefPtr<UIExtensionProxy>&)>>&& callbackList);
129     void FireAsyncCallbacks();
130     void SetBindModalCallback(const std::function<void()>&& callback);
131     void FireBindModalCallback();
132     void DispatchFollowHostDensity(bool densityDpi);
133     void OnDpiConfigurationUpdate() override;
134     void SetDensityDpi(bool densityDpi);
135     bool GetDensityDpi();
136     bool IsCompatibleOldVersion();
137 
138     void NotifySizeChangeReason(
139         WindowSizeChangeReason type, const std::shared_ptr<Rosen::RSTransaction>& rsTransaction);
140     void NotifyForeground();
141     void NotifyBackground(bool isHandleError = true);
142     void NotifyDestroy();
143     int32_t GetInstanceId();
144     int32_t GetSessionId();
145     int32_t GetNodeId();
146     int32_t GetUiExtensionId() override;
147     bool IsModalUec();
148     bool IsForeground();
149     void OnExtensionDetachToDisplay();
GetSessionWrapper()150     RefPtr<SessionWrapper> GetSessionWrapper()
151     {
152         return sessionWrapper_;
153     }
154     int64_t WrapExtensionAbilityId(int64_t extensionOffset, int64_t abilityId) override;
155     void DispatchOriginAvoidArea(const Rosen::AvoidArea& avoidArea, uint32_t type);
156     void HandleVisibleAreaChange(bool visible, double ratio);
157     void SetWantWrap(const RefPtr<OHOS::Ace::WantWrap>& wantWrap);
158     RefPtr<OHOS::Ace::WantWrap> GetWantWrap();
IsShowPlaceholder()159     bool IsShowPlaceholder()
160     {
161         return (curPlaceholderType_ != PlaceholderType::NONE);
162     }
IsCanMountPlaceholder(PlaceholderType type)163     bool IsCanMountPlaceholder(PlaceholderType type)
164     {
165         return (static_cast<int32_t>(type) > static_cast<int32_t>(curPlaceholderType_));
166     }
SetCurPlaceholderType(PlaceholderType type)167     void SetCurPlaceholderType(PlaceholderType type)
168     {
169         curPlaceholderType_ = type;
170     }
171     void PostDelayRemovePlaceholder(uint32_t delay);
172     void ReplacePlaceholderByContent();
173     void OnExtensionEvent(UIExtCallbackEventId eventId);
174     void OnUeaAccessibilityEventAsync();
175     void OnAreaUpdated();
176 
177     void OnAccessibilityEvent(const Accessibility::AccessibilityEventInfo& info, int64_t uiExtensionOffset);
SetModalFlag(bool isModal)178     void SetModalFlag(bool isModal)
179     {
180         isModal_ = isModal;
181     }
SetNeedCheckWindowSceneId(bool needCheckWindowSceneId)182     void SetNeedCheckWindowSceneId(bool needCheckWindowSceneId)
183     {
184         needCheckWindowSceneId_ = needCheckWindowSceneId;
185     }
186     void OnAccessibilityChildTreeRegister(uint32_t windowId, int32_t treeId, int64_t accessibilityId);
187     void OnAccessibilityChildTreeDeregister();
188     void OnSetAccessibilityChildTree(int32_t childWindowId, int32_t childTreeId);
189     void OnAccessibilityDumpChildInfo(const std::vector<std::string>& params, std::vector<std::string>& info);
190 
191     void DumpInfo() override;
192     void DumpInfo(std::unique_ptr<JsonValue>& json) override;
193     void DumpOthers();
194 
195 protected:
196     virtual void DispatchPointerEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent);
197     virtual void DispatchKeyEvent(const KeyEvent& event);
198 
199     int32_t uiExtensionId_ = 0;
200     int32_t instanceId_ = Container::CurrentId();
201 
202 private:
203     enum class AbilityState {
204         NONE = 0,
205         FOREGROUND,
206         BACKGROUND,
207         DESTRUCTION,
208     };
209 
210     struct ErrorMsg {
211         int32_t code = 0;
212         std::string name;
213         std::string message;
214     };
215 
216     const char* ToString(AbilityState state);
217     void OnAttachToFrameNode() override;
218     void OnDetachFromFrameNode(FrameNode* frameNode) override;
219     void OnLanguageConfigurationUpdate() override;
220     void OnColorConfigurationUpdate() override;
221     void OnModifyDone() override;
222     bool CheckConstraint();
223 
224     void InitKeyEvent(const RefPtr<FocusHub>& focusHub);
225     void InitTouchEvent(const RefPtr<GestureEventHub>& gestureHub);
226     void InitMouseEvent(const RefPtr<InputEventHub>& inputHub);
227     void InitHoverEvent(const RefPtr<InputEventHub>& inputHub);
228     void InitializeAccessibility();
229     bool HandleKeyEvent(const KeyEvent& event);
230     void HandleFocusEvent();
231     void HandleBlurEvent();
232     void HandleTouchEvent(const TouchEventInfo& info);
233     void HandleMouseEvent(const MouseInfo& info);
234     void HandleHoverEvent(bool isHover);
235     bool DispatchKeyEventSync(const KeyEvent& event);
236     void DispatchFocusActiveEvent(bool isFocusActive);
237     void DispatchFocusState(bool focusState);
238     void DispatchDisplayArea(bool isForce = false);
239     void LogoutModalUIExtension();
240 
241     void RegisterVisibleAreaChange();
242     void MountPlaceholderNode(PlaceholderType type);
243     void RemovePlaceholderNode();
SetFoldStatusChanged(bool isChanged)244     void SetFoldStatusChanged(bool isChanged)
245     {
246         isFoldStatusChanged_ = isChanged;
247     }
IsFoldStatusChanged()248     bool IsFoldStatusChanged()
249     {
250         return isFoldStatusChanged_;
251     }
SetRotateStatusChanged(bool isChanged)252     void SetRotateStatusChanged(bool isChanged)
253     {
254         isRotateStatusChanged_ = isChanged;
255     }
IsRotateStatusChanged()256     bool IsRotateStatusChanged()
257     {
258         return isRotateStatusChanged_;
259     }
260     PlaceholderType GetSizeChangeReason();
261     UIExtensionUsage GetUIExtensionUsage(const AAFwk::Want& want);
262     void ReDispatchDisplayArea();
263     int32_t GetInstanceIdFromHost();
264     void ResetAccessibilityChildTreeCallback();
265 
266     RefPtr<TouchEventImpl> touchEvent_;
267     RefPtr<InputEvent> mouseEvent_;
268     RefPtr<InputEvent> hoverEvent_;
269     std::shared_ptr<MMI::PointerEvent> lastPointerEvent_ = nullptr;
270     std::shared_ptr<AccessibilityChildTreeCallback> accessibilityChildTreeCallback_;
271 
272     std::function<void()> onModalDestroy_;
273     std::function<void(const std::shared_ptr<ModalUIExtensionProxy>&)> onModalRemoteReadyCallback_;
274     std::function<void(const RefPtr<UIExtensionProxy>&)> onRemoteReadyCallback_;
275     std::function<void(int32_t)> onReleaseCallback_;
276     std::function<void(int32_t, const AAFwk::Want&)> onResultCallback_;
277     std::function<void(int32_t, const RefPtr<WantWrap>&)> onTerminatedCallback_;
278     std::function<void(const AAFwk::WantParams&)> onReceiveCallback_;
279     std::function<void(int32_t code, const std::string& name, const std::string& message)> onErrorCallback_;
280     std::list<std::function<void(const RefPtr<UIExtensionProxy>&)>> onSyncOnCallbackList_;
281     std::list<std::function<void(const RefPtr<UIExtensionProxy>&)>> onAsyncOnCallbackList_;
282     std::function<void()> bindModalCallback_;
283     std::map<PlaceholderType, RefPtr<NG::FrameNode>> placeholderMap_;
284 
285     RefPtr<OHOS::Ace::WantWrap> curWant_;
286     RefPtr<FrameNode> contentNode_;
287     RefPtr<SessionWrapper> sessionWrapper_;
288     RefPtr<AccessibilitySessionAdapterUIExtension> accessibilitySessionAdapter_;
289     ErrorMsg lastError_;
290     AbilityState state_ = AbilityState::NONE;
291     bool isTransferringCaller_ = false;
292     bool isVisible_ = true;
293     bool isModal_ = false;
294     bool isAsyncModalBinding_ = false;
295     PlaceholderType curPlaceholderType_ = PlaceholderType::NONE;
296     bool isFoldStatusChanged_ = false;
297     bool isRotateStatusChanged_ = false;
298     bool densityDpi_ = false;
299     WeakPtr<Pattern> weakSystemWindowScene_;
300     // Whether to send the focus to the UIExtension
301     // No multi-threading problem due to run js thread
302     bool canFocusSendToUIExtension_ = true;
303     bool needReSendFocusToUIExtension_ = false;
304     int32_t surfacePositionCallBackId_ = -1;
305     int32_t foldDisplayCallBackId_ = -1;
306     RectF displayArea_;
307     bool isKeyAsync_ = false;
308     // StartUIExtension should after mountToParent
309     bool hasMountToParent_ = false;
310     bool needReNotifyForeground_ = false;
311     bool needCheckWindowSceneId_ = false;
312     bool needReDispatchDisplayArea_ = false;
313     bool curVisible_ = false;
314     SessionType sessionType_ = SessionType::UI_EXTENSION_ABILITY;
315     UIExtensionUsage usage_ = UIExtensionUsage::EMBEDDED;
316 
317     // UEC dump info
318     bool focusState_ = false;
319     uint32_t focusWindowId_ = 0;
320     uint32_t realHostWindowId_ = 0;
321     std::string want_;
322 
323     ACE_DISALLOW_COPY_AND_MOVE(UIExtensionPattern);
324 };
325 } // namespace OHOS::Ace::NG
326 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_UI_EXTENSION_UI_EXTENSION_PATTERN_H
327