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_INTERFACES_OBSERVER_H 17 #define FOUNDATION_ACE_INTERFACES_OBSERVER_H 18 19 #include <list> 20 #include <optional> 21 #include <unordered_map> 22 #include <string> 23 24 #include "napi/native_api.h" 25 #include "napi/native_common.h" 26 #include "napi/native_node_api.h" 27 28 #include "ui_observer_listener.h" 29 30 #include "core/components_ng/base/observer_handler.h" 31 #include "core/components_ng/gestures/recognizers/gesture_recognizer.h" 32 33 namespace OHOS::Ace::NG { 34 class NodeRenderStatusMonitor; 35 } 36 37 namespace OHOS::Ace::Napi { 38 class UIObserver { 39 public: 40 struct NodeRenderListener { 41 int32_t id = -1; 42 std::list<std::shared_ptr<UIObserverListener>> listeners; NodeRenderListenerNodeRenderListener43 NodeRenderListener(int32_t id, std::list<std::shared_ptr<UIObserverListener>> listeners) 44 : id(id), listeners(listeners) {}; 45 }; 46 static void RegisterNavigationCallback(const std::shared_ptr<UIObserverListener>& listener); 47 static void RegisterNavigationCallback( 48 std::string navigationId, const std::shared_ptr<UIObserverListener>& listener); 49 static void RegisterNavigationCallback( 50 int32_t navigationUniqueId, const std::shared_ptr<UIObserverListener>& listener); 51 static void UnRegisterNavigationCallback(napi_value cb); 52 static void UnRegisterNavigationCallback(std::string navigationId, napi_value cb); 53 static void UnRegisterNavigationCallback(int32_t navigationUniqueId, napi_value cb); 54 static void HandleNavigationStateChange(const NG::NavDestinationInfo& info); 55 56 static void RegisterScrollEventCallback(const std::shared_ptr<UIObserverListener>& listener); 57 static void RegisterScrollEventCallback( 58 const std::string& id, const std::shared_ptr<UIObserverListener>& listener); 59 static void UnRegisterScrollEventCallback(napi_value cb); 60 static void UnRegisterScrollEventCallback(const std::string& id, napi_value cb); 61 static void HandleScrollEventStateChange(const std::string& id, int32_t uniqueId, 62 NG::ScrollEventType eventType, float offset, Ace::Axis axis); 63 64 static void RegisterRouterPageCallback( 65 napi_env env, napi_value uiAbilityContext, const std::shared_ptr<UIObserverListener>& listener); 66 static void RegisterRouterPageCallback( 67 int32_t uiContextInstanceId, const std::shared_ptr<UIObserverListener>& listener); 68 static void UnRegisterRouterPageCallback(napi_env env, napi_value uiAbilityContext, napi_value callback); 69 static void UnRegisterRouterPageCallback(int32_t uiContextInstanceId, napi_value callback); 70 static void HandleRouterPageStateChange(NG::AbilityContextInfo& info, const NG::RouterPageInfoNG& pageInfo); 71 72 static void RegisterDensityCallback( 73 int32_t uiContextInstanceId, const std::shared_ptr<UIObserverListener>& listener); 74 static void UnRegisterDensityCallback(int32_t uiContextInstanceId, napi_value callback); 75 static void HandleDensityChange(NG::AbilityContextInfo& info, double density); 76 static void RegisterDrawCallback(int32_t uiContextInstanceId, const std::shared_ptr<UIObserverListener>& listener); 77 static void UnRegisterDrawCallback(int32_t uiContextInstanceId, napi_value callback); 78 static void RegisterLayoutCallback( 79 int32_t uiContextInstanceId, const std::shared_ptr<UIObserverListener>& listener); 80 static void UnRegisterLayoutCallback(int32_t uiContextInstanceId, napi_value callback); 81 static void HandDrawCommandSendChange(); 82 static void HandLayoutDoneChange(); 83 84 static void RegisterNavDestinationSwitchCallback( 85 napi_env env, napi_value uiAbilityContext, const std::optional<std::string>& navigationId, 86 const std::shared_ptr<UIObserverListener>& listener); 87 static void RegisterNavDestinationSwitchCallback( 88 int32_t uiContextInstanceId, const std::optional<std::string>& navigationId, 89 const std::shared_ptr<UIObserverListener>& listener); 90 static void UnRegisterNavDestinationSwitchCallback(napi_env env, napi_value uiAbilityContext, 91 const std::optional<std::string>& navigationId, napi_value callback); 92 static void UnRegisterNavDestinationSwitchCallback(int32_t uiContextInstanceId, 93 const std::optional<std::string>& navigationId, napi_value callback); 94 static void HandleNavDestinationSwitch( 95 const NG::AbilityContextInfo& info, NG::NavDestinationSwitchInfo& switchInfo); 96 97 static void RegisterWillClickCallback( 98 napi_env env, napi_value uiAbilityContext, const std::shared_ptr<UIObserverListener>& listener); 99 static void RegisterWillClickCallback( 100 int32_t uiContextInstanceId, const std::shared_ptr<UIObserverListener>& listener); 101 static void UnRegisterWillClickCallback(napi_env env, napi_value uiAbilityContext, napi_value callback); 102 static void UnRegisterWillClickCallback(int32_t uiContextInstanceId, napi_value callback); 103 static void HandleWillClick(NG::AbilityContextInfo& info, const GestureEvent& gestureEventInfo, 104 const ClickInfo& clickInfo, const RefPtr<NG::FrameNode>& frameNode); 105 106 static void RegisterDidClickCallback( 107 napi_env env, napi_value uiAbilityContext, const std::shared_ptr<UIObserverListener>& listener); 108 static void RegisterDidClickCallback( 109 int32_t uiContextInstanceId, const std::shared_ptr<UIObserverListener>& listener); 110 static void UnRegisterDidClickCallback(napi_env env, napi_value uiAbilityContext, napi_value callback); 111 static void UnRegisterDidClickCallback(int32_t uiContextInstanceId, napi_value callback); 112 static void HandleDidClick(NG::AbilityContextInfo& info, const GestureEvent& gestureEventInfo, 113 const ClickInfo& clickInfo, const RefPtr<NG::FrameNode>& frameNode); 114 115 static void RegisterTabContentStateCallback(const std::shared_ptr<UIObserverListener>& listener); 116 static void RegisterTabContentStateCallback( 117 const std::string& id, const std::shared_ptr<UIObserverListener>& listener); 118 static void UnRegisterTabContentStateCallback(napi_value cb); 119 static void UnRegisterTabContentStateCallback(const std::string& id, napi_value cb); 120 static void HandleTabContentStateChange(const NG::TabContentInfo& tabContentInfo); 121 122 static void RegisterBeforePanStartCallback( 123 napi_env env, napi_value uiAbilityContext, const std::shared_ptr<UIObserverListener>& listener); 124 static void RegisterBeforePanStartCallback( 125 int32_t uiContextInstanceId, const std::shared_ptr<UIObserverListener>& listener); 126 static void UnRegisterBeforePanStartCallback(napi_env env, napi_value uiAbilityContext, napi_value callback); 127 static void UnRegisterBeforePanStartCallback(int32_t uiContextInstanceId, napi_value callback); 128 static void RegisterBeforePanEndCallback( 129 napi_env env, napi_value uiAbilityContext, const std::shared_ptr<UIObserverListener>& listener); 130 static void RegisterBeforePanEndCallback( 131 int32_t uiContextInstanceId, const std::shared_ptr<UIObserverListener>& listener); 132 static void UnRegisterBeforePanEndCallback(napi_env env, napi_value uiAbilityContext, napi_value callback); 133 static void UnRegisterBeforePanEndCallback(int32_t uiContextInstanceId, napi_value callback); 134 static void RegisterAfterPanStartCallback( 135 napi_env env, napi_value uiAbilityContext, const std::shared_ptr<UIObserverListener>& listener); 136 static void RegisterAfterPanStartCallback( 137 int32_t uiContextInstanceId, const std::shared_ptr<UIObserverListener>& listener); 138 static void UnRegisterAfterPanStartCallback(napi_env env, napi_value uiAbilityContext, napi_value callback); 139 static void UnRegisterAfterPanStartCallback(int32_t uiContextInstanceId, napi_value callback); 140 static void RegisterAfterPanEndCallback( 141 napi_env env, napi_value uiAbilityContext, const std::shared_ptr<UIObserverListener>& listener); 142 static void RegisterAfterPanEndCallback( 143 int32_t uiContextInstanceId, const std::shared_ptr<UIObserverListener>& listener); 144 static void UnRegisterAfterPanEndCallback(napi_env env, napi_value uiAbilityContext, napi_value callback); 145 static void UnRegisterAfterPanEndCallback(int32_t uiContextInstanceId, napi_value callback); 146 static void HandlePanGestureAccept(NG::AbilityContextInfo& info, const GestureEvent& gestureEventInfo, 147 const RefPtr<NG::PanRecognizer>& current, const RefPtr<NG::FrameNode>& frameNode, 148 const NG::PanGestureInfo& panGestureInfo); 149 using PanGestureListenersPair = 150 std::pair<std::unordered_map<napi_ref, std::list<std::shared_ptr<UIObserverListener>>>&, 151 std::unordered_map<int32_t, std::list<std::shared_ptr<UIObserverListener>>>&>; 152 static PanGestureListenersPair GetPanGestureListeners(const NG::PanGestureInfo& panGestureInfo); 153 154 static void RegisterNodeRenderStateChangeCallback(RefPtr<NG::FrameNode> frameNode, 155 const std::shared_ptr<UIObserverListener>& listener, const RefPtr<NG::NodeRenderStatusMonitor>& monitor); 156 static void UnRegisterNodeRenderStateChangeCallback( 157 RefPtr<NG::FrameNode> frameNode, napi_value callback, const RefPtr<NG::NodeRenderStatusMonitor>& monitor); 158 159 static bool ParseStringFromNapi(napi_env env, napi_value val, std::string& str); 160 static bool MatchValueType(napi_env env, napi_value value, napi_valuetype targetType); 161 private: 162 static void HandleAbilityUIContextNavDestinationSwitch( 163 const NG::AbilityContextInfo& info, NG::NavDestinationSwitchInfo& switchInfo); 164 static void HandleUIContextNavDestinationSwitch(const NG::NavDestinationSwitchInfo& switchInfo); 165 using NavIdAndListenersMap = 166 std::unordered_map<std::optional<std::string>, std::list<std::shared_ptr<UIObserverListener>>>; 167 static void HandleListenersWithEmptyNavigationId( 168 const NavIdAndListenersMap& listenersMap, const NG::NavDestinationSwitchInfo& switchInfo); 169 static void HandleListenersWithSpecifiedNavigationId( 170 const NavIdAndListenersMap& listenersMap, const NG::NavDestinationSwitchInfo& switchInfo); 171 static void GetAbilityInfos(napi_env env, napi_value abilityContext, NG::AbilityContextInfo& info); 172 static napi_env GetCurrentNapiEnv(); 173 174 static std::list<std::shared_ptr<UIObserverListener>> unspecifiedNavigationListeners_; 175 static std::unordered_map<std::string, std::list<std::shared_ptr<UIObserverListener>>> 176 specifiedCNavigationListeners_; 177 static std::unordered_map<int32_t, std::list<std::shared_ptr<UIObserverListener>>> 178 specifiedUniqueIdNavigationListeners_; 179 static std::list<std::shared_ptr<UIObserverListener>> scrollEventListeners_; 180 static std::unordered_map<std::string, std::list<std::shared_ptr<UIObserverListener>>> 181 specifiedScrollEventListeners_; 182 static std::unordered_map<napi_ref, std::list<std::shared_ptr<UIObserverListener>>> 183 abilityContextRouterPageListeners_; 184 static std::unordered_map<int32_t, std::list<std::shared_ptr<UIObserverListener>>> 185 specifiedRouterPageListeners_; 186 static std::unordered_map<napi_ref, NG::AbilityContextInfo> infosForRouterPage_; 187 static std::unordered_map<int32_t, std::list<std::shared_ptr<UIObserverListener>>> 188 specifiedDensityListeners_; 189 190 static std::unordered_map<napi_ref, std::list<std::shared_ptr<UIObserverListener>>> 191 abilityContextWillClickListeners_; 192 static std::unordered_map<int32_t, std::list<std::shared_ptr<UIObserverListener>>> 193 specifiedWillClickListeners_; 194 static std::unordered_map<napi_ref, std::list<std::shared_ptr<UIObserverListener>>> 195 abilityContextDidClickListeners_; 196 static std::unordered_map<int32_t, std::list<std::shared_ptr<UIObserverListener>>> 197 specifiedDidClickListeners_; 198 static std::list<std::shared_ptr<UIObserverListener>> tabContentStateListeners_; 199 static std::unordered_map<std::string, std::list<std::shared_ptr<UIObserverListener>>> 200 specifiedTabContentStateListeners_; 201 static std::unordered_map<napi_ref, std::list<std::shared_ptr<UIObserverListener>>> 202 abilityContextBeforePanStartListeners_; 203 static std::unordered_map<int32_t, std::list<std::shared_ptr<UIObserverListener>>> 204 specifiedBeforePanStartListeners_; 205 static std::unordered_map<napi_ref, std::list<std::shared_ptr<UIObserverListener>>> 206 abilityContextBeforePanEndListeners_; 207 static std::unordered_map<int32_t, std::list<std::shared_ptr<UIObserverListener>>> 208 specifiedBeforePanEndListeners_; 209 static std::unordered_map<napi_ref, std::list<std::shared_ptr<UIObserverListener>>> 210 abilityContextAfterPanStartListeners_; 211 static std::unordered_map<int32_t, std::list<std::shared_ptr<UIObserverListener>>> 212 specifiedAfterPanStartListeners_; 213 static std::unordered_map<napi_ref, std::list<std::shared_ptr<UIObserverListener>>> 214 abilityContextAfterPanEndListeners_; 215 static std::unordered_map<int32_t, std::list<std::shared_ptr<UIObserverListener>>> 216 specifiedAfterPanEndListeners_; 217 static std::unordered_map<NG::FrameNode*, std::shared_ptr<NodeRenderListener>> 218 specifiedNodeRenderStateListeners_; 219 220 static std::unordered_map<int32_t, std::list<std::shared_ptr<UIObserverListener>>> specifiedDrawListeners_; 221 static std::unordered_map<int32_t, std::list<std::shared_ptr<UIObserverListener>>> specifiedLayoutListeners_; 222 223 static std::unordered_map<napi_ref, NavIdAndListenersMap> abilityUIContextNavDesSwitchListeners_; 224 static std::unordered_map<int32_t, NavIdAndListenersMap> uiContextNavDesSwitchListeners_; 225 static std::unordered_map<napi_ref, NG::AbilityContextInfo> infosForNavDesSwitch_; 226 static std::unordered_map<napi_ref, NG::AbilityContextInfo> willClickInfos_; 227 static std::unordered_map<napi_ref, NG::AbilityContextInfo> didClickInfos_; 228 static std::unordered_map<napi_ref, NG::AbilityContextInfo> beforePanStartInfos_; 229 static std::unordered_map<napi_ref, NG::AbilityContextInfo> beforePanEndInfos_; 230 static std::unordered_map<napi_ref, NG::AbilityContextInfo> afterPanStartInfos_; 231 static std::unordered_map<napi_ref, NG::AbilityContextInfo> afterPanEndInfos_; 232 static std::unordered_map<napi_ref, NG::AbilityContextInfo> PanGestureInfos_; 233 }; 234 } // namespace OHOS::Ace::Napi 235 #endif // FOUNDATION_ACE_INTERFACES_OBSERVER_H 236