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_MANAGER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_UI_EXTENSION_UI_EXTENSION_MANAGER_H 18 19 #include <bitset> 20 #include <memory> 21 #include <mutex> 22 23 #include "base/memory/ace_type.h" 24 #include "base/memory/referenced.h" 25 #include "base/want/want_wrap.h" 26 #include "core/common/container.h" 27 #include "core/components_ng/pattern/ui_extension/session_wrapper.h" 28 #include "interfaces/inner_api/ace/viewport_config.h" 29 30 namespace OHOS { 31 template<typename T> 32 class sptr; 33 namespace Rosen { 34 class AvoidArea; 35 enum class WSError; 36 class OccupiedAreaChangeInfo; 37 enum class WindowMode : uint32_t; 38 } // namespace Rosen 39 } // namespace OHOS 40 41 namespace OHOS::Ace::NG { 42 class UIExtensionPattern; 43 namespace { 44 constexpr int32_t UI_EXTENSION_UNKNOW_ID = 0; 45 constexpr int32_t UI_EXTENSION_ID_FIRST_MAX = 10; 46 constexpr int32_t UI_EXTENSION_ID_OTHER_MAX = 9; 47 constexpr int64_t UI_EXTENSION_OFFSET_MAX = 100000000000; 48 constexpr int64_t UI_EXTENSION_OFFSET_MIN = 10000000000; 49 constexpr int32_t UI_EXTENSION_ID_FACTOR = 10; 50 constexpr int32_t UI_EXTENSION_LEVEL_MAX = 3; 51 constexpr int32_t UI_EXTENSION_ROOT_ID = -1; 52 using UIExtBusinessDataSendCallback = std::function<std::optional<AAFwk::Want>(WeakPtr<FrameNode>)>; 53 using UIExtBusinessDataConsumeCallback = std::function<int32_t(const AAFwk::Want&)>; 54 using UIExtBusinessDataConsumeReplyCallback = std::function<int32_t(const AAFwk::Want&, std::optional<AAFwk::Want>&)>; 55 56 using UIExtBusinessSendToHostReplyFunc = std::function<bool(uint32_t, const AAFwk::Want&, AAFwk::Want&)>; 57 using UIExtBusinessSendToHostFunc = std::function<bool(uint32_t, const AAFwk::Want&, BusinessDataSendType)>; 58 59 }; // namespace 60 61 class SecurityUIExtensionPattern; 62 class ACE_FORCE_EXPORT UIExtensionIdUtility : public Singleton<UIExtensionIdUtility> { 63 DECLARE_SINGLETON(UIExtensionIdUtility); 64 public: 65 int32_t ApplyExtensionId(); 66 void RecycleExtensionId(int32_t id); 67 68 private: 69 std::bitset<UI_EXTENSION_ID_FIRST_MAX> idPool_; 70 std::mutex poolMutex_; 71 }; 72 73 class UIExtensionManager : public AceType { 74 DECLARE_ACE_TYPE(UIExtensionManager, AceType); 75 76 public: 77 UIExtensionManager() = default; 78 ~UIExtensionManager() override = default; 79 80 void RegisterUIExtensionInFocus( 81 const WeakPtr<UIExtensionPattern>& uiExtensionFocused, const WeakPtr<SessionWrapper>& sessionWrapper); 82 bool OnBackPressed(); 83 const RefPtr<FrameNode> GetFocusUiExtensionNode(); 84 bool IsWrapExtensionAbilityId(int64_t elementId); 85 bool SendAccessibilityEventInfo(const Accessibility::AccessibilityEventInfo& eventInfo, int64_t uiExtensionOffset, 86 const RefPtr<PipelineBase>& pipeline); 87 std::pair<int64_t, int64_t> UnWrapExtensionAbilityId(int64_t extensionOffset, int64_t elementId); 88 int32_t ApplyExtensionId(); 89 void RecycleExtensionId(int32_t id); 90 void RegisterSecurityUIExtensionInFocus( 91 const WeakPtr<SecurityUIExtensionPattern>& uiExtensionFocused, 92 const WeakPtr<SessionWrapper>& sessionWrapper); 93 94 /** 95 * @brief Create a UIExtensionComponent object on the page and save it in the UIExtension management object 96 * 97 * @param uiExtension The UIExtensionComponent pattern object 98 */ 99 void AddAliveUIExtension(int32_t nodeId, const WeakPtr<UIExtensionPattern>& uiExtension); 100 101 /** 102 * @brief Clear the UIExtensionComponent to be destroyed 103 * 104 * @param nodeId The UIExtensionComponent Id 105 */ 106 void RemoveDestroyedUIExtension(int32_t nodeId); 107 108 /** 109 * @brief Check whether UIExtensionComponent Node is showing placeholder 110 * 111 * @param nodeId The UIExtensionComponent Id 112 */ 113 bool IsShowPlaceholder(int32_t nodeId); 114 115 /** 116 * @brief Transfer the original avoid area and avoid area type to the UIExtensionAbility 117 * 118 * @param avoidArea The original avoid area 119 * @param type The original aovid areatype 120 */ 121 void TransferOriginAvoidArea(const Rosen::AvoidArea& avoidArea, uint32_t type); 122 123 bool NotifyOccupiedAreaChangeInfo(const sptr<Rosen::OccupiedAreaChangeInfo>& info); 124 125 bool HandleUnfocusedModalUecBackPressed(); 126 127 bool IsLastModalUec(const RefPtr<FrameNode>& frameNode); 128 129 void NotifySizeChangeReason( 130 WindowSizeChangeReason type, const std::shared_ptr<Rosen::RSTransaction>& rsTransaction); 131 132 void AddAliveUIExtension(int32_t nodeId, const WeakPtr<SecurityUIExtensionPattern>& uiExtension); 133 134 void UpdateSessionViewportConfig(const ViewportConfig& config); 135 136 void DumpUIExt(); 137 138 // host send data to provider 139 void RegisterBusinessDataSendCallback( 140 UIContentBusinessCode code, BusinessDataSendType type, const UIExtBusinessDataSendCallback& callback, 141 RSSubsystemId subSystemId = RSSubsystemId::ARKUI_UIEXT); 142 void UnRegisterBusinessDataSendCallback(UIContentBusinessCode code); 143 bool TriggerBusinessDataSend(UIContentBusinessCode code); 144 145 // provider consume data 146 void RegisterBusinessDataConsumeReplyCallback( 147 UIContentBusinessCode code, const UIExtBusinessDataConsumeReplyCallback& callback); 148 void UnRegisterBusinessDataConsumeReplyCallback(UIContentBusinessCode code); 149 void DispatchBusinessDataConsumeReply( 150 UIContentBusinessCode code, const AAFwk::Want& data, std::optional<AAFwk::Want>& reply); 151 152 // provider send data to host, called by framework 153 void RegisterBusinessDataConsumeCallback( 154 UIContentBusinessCode code, const UIExtBusinessDataConsumeCallback& callback); 155 void UnRegisterBusinessDataConsumeCallback(UIContentBusinessCode code); 156 void DispatchBusinessDataConsume(UIContentBusinessCode code, const AAFwk::Want& data); 157 158 // register provider send data to host, called by framework 159 void RegisterBusinessSendToHostReply(const UIExtBusinessSendToHostReplyFunc& func); 160 // register provider send data to host, called by framework 161 void RegisterBusinessSendToHost(const UIExtBusinessSendToHostFunc& func); 162 163 // provider send to host, consumer is in ui_extension_pattern 164 bool SendBusinessToHost(UIContentBusinessCode code, const AAFwk::Want& data, BusinessDataSendType type); 165 // provider send to host sync reply, only used in uiextension init, not allowd used by other 166 bool SendBusinessToHostSyncReply(UIContentBusinessCode code, const AAFwk::Want& data, AAFwk::Want& reply); 167 168 void NotifyWindowMode(Rosen::WindowMode mode); 169 170 void SendPageModeToProvider(const int32_t nodeId, const std::string& pageMode); 171 void SendPageModeRequestToHost(const RefPtr<PipelineContext>& pipeline); 172 void TransferAccessibilityRectInfo(); 173 void UpdateWMSUIExtProperty(UIContentBusinessCode code, const AAFwk::Want& data, RSSubsystemId subSystemId); SetInstanceId(int32_t id)174 void SetInstanceId(int32_t id) 175 { 176 instanceId_ = id; 177 } SetPipelineContext(const WeakPtr<PipelineContext> & pipeline)178 void SetPipelineContext(const WeakPtr<PipelineContext>& pipeline) 179 { 180 pipeline_ = pipeline; 181 } 182 void NotifyUECProviderIfNeedded(); 183 184 private: 185 void RegisterListenerIfNeeded(); 186 void UnregisterListenerIfNeeded(); 187 bool UIExtBusinessDataSendValid(); 188 WeakPtr<UIExtensionPattern> uiExtensionFocused_; 189 WeakPtr<SecurityUIExtensionPattern> securityUiExtensionFocused_; 190 WeakPtr<SessionWrapper> sessionWrapper_; 191 std::mutex aliveUIExtensionMutex_; 192 std::map<int32_t, OHOS::Ace::WeakPtr<UIExtensionPattern>> aliveUIExtensions_; 193 std::map<int32_t, OHOS::Ace::WeakPtr<SecurityUIExtensionPattern>> aliveSecurityUIExtensions_; 194 std::map<UIContentBusinessCode, 195 std::tuple<BusinessDataSendType, UIExtBusinessDataSendCallback, RSSubsystemId>> businessDataSendCallbacks_; 196 std::map<UIContentBusinessCode, UIExtBusinessDataConsumeCallback> businessDataConsumeCallbacks_; 197 std::map<UIContentBusinessCode, UIExtBusinessDataConsumeReplyCallback> businessDataConsumeReplyCallbacks_; 198 UIExtBusinessSendToHostReplyFunc businessSendToHostReplyFunc_; 199 UIExtBusinessSendToHostFunc businessSendToHostFunc_; 200 201 int32_t instanceId_ = -1; 202 WeakPtr<PipelineContext> pipeline_; 203 bool hasRegisterListener_ = false; 204 int32_t containerModalListenerId_ = -1; 205 }; 206 } // namespace OHOS::Ace::NG 207 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_UI_EXTENSION_UI_EXTENSION_MANAGER_H 208