1 /* 2 * Copyright (c) 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_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 <refbase.h> 24 #include <vector> 25 26 #include "base/memory/referenced.h" 27 #include "base/want/want_wrap.h" 28 #include "core/common/container.h" 29 #include "core/common/dynamic_component_renderer.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/event/mouse_event.h" 34 #include "core/event/touch_event.h" 35 36 namespace OHOS::Accessibility { 37 class AccessibilityElementInfo; 38 class AccessibilityEventInfo; 39 } // namespace OHOS::Accessibility 40 41 namespace OHOS::MMI { 42 class KeyEvent; 43 class PointerEvent; 44 } // namespace OHOS::MMI 45 46 namespace OHOS::Ace { 47 class ModalUIExtensionProxy; 48 } // namespace OHOS::Ace 49 50 namespace OHOS::Rosen { 51 class AvoidArea; 52 } // namespace OHOS::Rosen 53 54 namespace OHOS::Ace::NG { 55 class UIExtensionProxy; 56 class UIExtensionPattern : public Pattern { 57 DECLARE_ACE_TYPE(UIExtensionPattern, Pattern); 58 59 public: 60 explicit UIExtensionPattern( 61 bool isTransferringCaller = false, bool isModal = false, bool isAsyncModalBinding = false); 62 ~UIExtensionPattern() override; 63 64 RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override; 65 FocusPattern GetFocusPattern() const override; 66 67 void UpdateWant(const RefPtr<OHOS::Ace::WantWrap>& wantWrap); 68 void UpdateWant(const AAFwk::Want& want); 69 70 void OnWindowShow() override; 71 void OnWindowHide() override; 72 void OnVisibleChange(bool visible) override; 73 void OnAreaChangedInner() override; 74 bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override; 75 76 // for DynamicComponent 77 void InitializeDynamicComponent( 78 const std::string& hapPath, const std::string& abcPath, const std::string& entryPoint, void* runtime); 79 bool OnDirtyLayoutWrapperSwapForDynamicComponent(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config); 80 OnSizeChanged(int32_t width,int32_t height)81 void OnSizeChanged(int32_t width, int32_t height) 82 { 83 if (onSizeChanged_) { 84 onSizeChanged_(width, height); 85 } 86 } 87 SetOnSizeChangedCallback(std::function<void (int32_t,int32_t)> && callback)88 void SetOnSizeChangedCallback(std::function<void(int32_t, int32_t)>&& callback) 89 { 90 onSizeChanged_ = std::move(callback); 91 } 92 93 void OnConnect(); 94 void OnDisconnect(); 95 void OnExtensionDied(); 96 void HandleDragEvent(const PointerEvent& info) override; 97 98 void SetModalOnDestroy(const std::function<void()>&& callback); 99 void FireModalOnDestroy(); 100 void SetModalOnRemoteReadyCallback( 101 const std::function<void(const std::shared_ptr<ModalUIExtensionProxy>&)>&& callback); 102 void SetOnRemoteReadyCallback(const std::function<void(const RefPtr<UIExtensionProxy>&)>&& callback); 103 void FireOnRemoteReadyCallback(); 104 void SetOnReleaseCallback(const std::function<void(int32_t)>&& callback); 105 void FireOnReleaseCallback(int32_t releaseCode); 106 void SetOnResultCallback(const std::function<void(int32_t, const AAFwk::Want&)>&& callback); 107 void FireOnResultCallback(int32_t code, const AAFwk::Want& want); 108 void SetOnReceiveCallback(const std::function<void(const AAFwk::WantParams&)>&& callback); 109 void FireOnReceiveCallback(const AAFwk::WantParams& params); 110 void SetOnErrorCallback( 111 const std::function<void(int32_t code, const std::string& name, const std::string& message)>&& callback); 112 void FireOnErrorCallback(int32_t code, const std::string& name, const std::string& message); 113 void SetSyncCallbacks(const std::list<std::function<void(const RefPtr<UIExtensionProxy>&)>>&& callbackList); 114 void FireSyncCallbacks(); 115 void SetAsyncCallbacks(const std::list<std::function<void(const RefPtr<UIExtensionProxy>&)>>&& callbackList); 116 void FireAsyncCallbacks(); 117 void SetBindModalCallback(const std::function<void()>&& callback); 118 void FireBindModalCallback(); 119 120 void NotifyForeground(); 121 void NotifyBackground(); 122 void NotifyDestroy(); 123 int32_t GetInstanceId(); 124 int32_t GetSessionId(); 125 int32_t GetNodeId(); 126 int32_t GetUiExtensionId() override; 127 int64_t WrapExtensionAbilityId(int64_t extensionOffset, int64_t abilityId) override; 128 void DispatchOriginAvoidArea(const Rosen::AvoidArea& avoidArea, uint32_t type); 129 130 virtual void SearchExtensionElementInfoByAccessibilityId(int64_t elementId, int32_t mode, int64_t baseParent, 131 std::list<Accessibility::AccessibilityElementInfo>& output) override; 132 virtual void SearchElementInfosByText(int64_t elementId, const std::string& text, int64_t baseParent, 133 std::list<Accessibility::AccessibilityElementInfo>& output) override; 134 virtual void FindFocusedElementInfo(int64_t elementId, int32_t focusType, int64_t baseParent, 135 Accessibility::AccessibilityElementInfo& output) override; 136 virtual void FocusMoveSearch(int64_t elementId, int32_t direction, int64_t baseParent, 137 Accessibility::AccessibilityElementInfo& output) override; 138 virtual bool TransferExecuteAction(int64_t elementId, const std::map<std::string, std::string>& actionArguments, 139 int32_t action, int64_t offset) override; 140 void OnAccessibilityEvent(const Accessibility::AccessibilityEventInfo& info, int64_t uiExtensionOffset); 141 142 private: 143 enum class ReleaseCode { 144 DESTROY_NORMAL = 0, 145 CONNECT_BROKEN, 146 }; 147 148 enum class AbilityState { 149 NONE = 0, 150 FOREGROUND, 151 BACKGROUND, 152 DESTRUCTION, 153 }; 154 155 struct ErrorMsg { 156 int32_t code = 0; 157 std::string name; 158 std::string message; 159 }; 160 161 enum class ComponentType { DYNAMIC, UI_EXTENSION }; 162 163 void OnAttachToFrameNode() override; 164 void OnDetachFromFrameNode(FrameNode* frameNode) override; 165 void OnLanguageConfigurationUpdate() override; 166 void OnColorConfigurationUpdate() override; 167 void OnModifyDone() override; 168 169 void InitKeyEvent(const RefPtr<FocusHub>& focusHub); 170 void InitTouchEvent(const RefPtr<GestureEventHub>& gestureHub); 171 void InitMouseEvent(const RefPtr<InputEventHub>& inputHub); 172 void InitHoverEvent(const RefPtr<InputEventHub>& inputHub); 173 bool HandleKeyEvent(const KeyEvent& event); 174 void HandleFocusEvent(); 175 void HandleBlurEvent(); 176 void HandleTouchEvent(const TouchEventInfo& info); 177 void HandleMouseEvent(const MouseInfo& info); 178 void HandleHoverEvent(bool isHover); 179 void DispatchKeyEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent); 180 bool DispatchKeyEventSync(const std::shared_ptr<MMI::KeyEvent>& keyEvent); 181 void DispatchFocusActiveEvent(bool isFocusActive); 182 void DispatchFocusState(bool focusState); 183 void DispatchPointerEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent); 184 void DispatchDisplayArea(bool isForce = false); 185 186 void RegisterVisibleAreaChange(); 187 void UpdateTextFieldManager(const Offset& offset, float height); 188 bool IsCurrentFocus() const; 189 190 RefPtr<TouchEventImpl> touchEvent_; 191 RefPtr<InputEvent> mouseEvent_; 192 RefPtr<InputEvent> hoverEvent_; 193 std::shared_ptr<MMI::PointerEvent> lastPointerEvent_ = nullptr; 194 195 std::function<void()> onModalDestroy_; 196 std::function<void(const std::shared_ptr<ModalUIExtensionProxy>&)> onModalRemoteReadyCallback_; 197 std::function<void(const RefPtr<UIExtensionProxy>&)> onRemoteReadyCallback_; 198 std::function<void(int32_t)> onReleaseCallback_; 199 std::function<void(int32_t, const AAFwk::Want&)> onResultCallback_; 200 std::function<void(const AAFwk::WantParams&)> onReceiveCallback_; 201 std::function<void(int32_t code, const std::string& name, const std::string& message)> onErrorCallback_; 202 std::list<std::function<void(const RefPtr<UIExtensionProxy>&)>> onSyncOnCallbackList_; 203 std::list<std::function<void(const RefPtr<UIExtensionProxy>&)>> onAsyncOnCallbackList_; 204 std::function<void()> bindModalCallback_; 205 206 RefPtr<FrameNode> contentNode_; 207 RefPtr<SessionWrapper> sessionWrapper_; 208 ErrorMsg lastError_; 209 int32_t instanceId_ = Container::CurrentId(); 210 AbilityState state_ = AbilityState::NONE; 211 bool isTransferringCaller_ = false; 212 bool isVisible_ = true; 213 bool isModal_ = false; 214 bool isAsyncModalBinding_ = false; 215 int32_t uiExtensionId_ = 0; 216 int32_t callbackId_ = 0; 217 RectF displayArea_; 218 219 // for DynamicComponent 220 ComponentType componentType_ = ComponentType::UI_EXTENSION; 221 RefPtr<DynamicComponentRenderer> dynamicComponentRenderer_; 222 std::function<void(int32_t, int32_t)> onSizeChanged_; 223 224 ACE_DISALLOW_COPY_AND_MOVE(UIExtensionPattern); 225 }; 226 } // namespace OHOS::Ace::NG 227 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_UI_EXTENSION_UI_EXTENSION_PATTERN_H 228