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 
29 namespace OHOS {
30 template<typename T>
31 class sptr;
32 namespace Rosen {
33 class AvoidArea;
34 enum class WSError;
35 class OccupiedAreaChangeInfo;
36 } // namespace Rosen
37 } // namespace OHOS
38 
39 namespace OHOS::Ace::NG {
40 namespace {
41 constexpr int32_t UI_EXTENSION_UNKNOW_ID = 0;
42 constexpr int32_t UI_EXTENSION_ID_FIRST_MAX = 10;
43 constexpr int32_t UI_EXTENSION_ID_OTHER_MAX = 9;
44 constexpr int64_t UI_EXTENSION_OFFSET_MAX = 100000000000;
45 constexpr int64_t UI_EXTENSION_OFFSET_MIN = 10000000000;
46 constexpr int32_t UI_EXTENSION_ID_FACTOR = 10;
47 constexpr int32_t UI_EXTENSION_LEVEL_MAX = 3;
48 constexpr int32_t UI_EXTENSION_ROOT_ID = -1;
49 }; // namespace
50 
51 class UIExtensionPattern;
52 class SecurityUIExtensionPattern;
53 class ACE_FORCE_EXPORT UIExtensionIdUtility : public Singleton<UIExtensionIdUtility> {
54     DECLARE_SINGLETON(UIExtensionIdUtility);
55 public:
56     int32_t ApplyExtensionId();
57     void RecycleExtensionId(int32_t id);
58 
59 private:
60     std::bitset<UI_EXTENSION_ID_FIRST_MAX> idPool_;
61     std::mutex poolMutex_;
62 };
63 
64 class UIExtensionManager : public AceType {
65     DECLARE_ACE_TYPE(UIExtensionManager, AceType);
66 
67 public:
68     UIExtensionManager() = default;
69     ~UIExtensionManager() override = default;
70 
71     void RegisterUIExtensionInFocus(
72         const WeakPtr<UIExtensionPattern>& uiExtensionFocused, const WeakPtr<SessionWrapper>& sessionWrapper);
73     bool OnBackPressed();
74     const RefPtr<FrameNode> GetFocusUiExtensionNode();
75     bool IsWrapExtensionAbilityId(int64_t elementId);
76     bool SendAccessibilityEventInfo(const Accessibility::AccessibilityEventInfo& eventInfo, int64_t uiExtensionOffset,
77         const RefPtr<PipelineBase>& pipeline);
78     std::pair<int64_t, int64_t> UnWrapExtensionAbilityId(int64_t extensionOffset, int64_t elementId);
79     int32_t ApplyExtensionId();
80     void RecycleExtensionId(int32_t id);
81     void RegisterSecurityUIExtensionInFocus(
82         const WeakPtr<SecurityUIExtensionPattern>& uiExtensionFocused,
83         const WeakPtr<SessionWrapper>& sessionWrapper);
84 
85     /**
86      * @brief Create a UIExtensionComponent object on the page and save it in the UIExtension management object
87      *
88      * @param uiExtension The UIExtensionComponent pattern object
89      */
90     void AddAliveUIExtension(int32_t nodeId, const WeakPtr<UIExtensionPattern>& uiExtension);
91 
92     /**
93      * @brief Clear the UIExtensionComponent to be destroyed
94      *
95      * @param nodeId The UIExtensionComponent Id
96      */
97     void RemoveDestroyedUIExtension(int32_t nodeId);
98 
99     /**
100      * @brief Check whether UIExtensionComponent Node is showing placeholder
101      *
102      * @param nodeId The UIExtensionComponent Id
103      */
104     bool IsShowPlaceholder(int32_t nodeId);
105 
106     /**
107      * @brief Transfer the original avoid area and avoid area type to the UIExtensionAbility
108      *
109      * @param avoidArea The original avoid area
110      * @param type The original aovid areatype
111      */
112     void TransferOriginAvoidArea(const Rosen::AvoidArea& avoidArea, uint32_t type);
113 
114     bool NotifyOccupiedAreaChangeInfo(const sptr<Rosen::OccupiedAreaChangeInfo>& info);
115 
116     bool HandleUnfocusedModalUecBackPressed();
117 
118     bool IsLastModalUec(const RefPtr<FrameNode>& frameNode);
119 
120     void NotifySizeChangeReason(
121         WindowSizeChangeReason type, const std::shared_ptr<Rosen::RSTransaction>& rsTransaction);
122 
123     void AddAliveUIExtension(int32_t nodeId, const WeakPtr<SecurityUIExtensionPattern>& uiExtension);
124 
125     void DumpUIExt();
126 
127 private:
128     WeakPtr<UIExtensionPattern> uiExtensionFocused_;
129     WeakPtr<SecurityUIExtensionPattern> securityUiExtensionFocused_;
130     WeakPtr<SessionWrapper> sessionWrapper_;
131     std::mutex aliveUIExtensionMutex_;
132     std::map<int32_t, OHOS::Ace::WeakPtr<UIExtensionPattern>> aliveUIExtensions_;
133     std::map<int32_t, OHOS::Ace::WeakPtr<SecurityUIExtensionPattern>> aliveSecurityUIExtensions_;
134 };
135 } // namespace OHOS::Ace::NG
136 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_UI_EXTENSION_UI_EXTENSION_MANAGER_H
137