123b3eb3cSopenharmony_ci/*
223b3eb3cSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
323b3eb3cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
423b3eb3cSopenharmony_ci * you may not use this file except in compliance with the License.
523b3eb3cSopenharmony_ci * You may obtain a copy of the License at
623b3eb3cSopenharmony_ci *
723b3eb3cSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
823b3eb3cSopenharmony_ci *
923b3eb3cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1023b3eb3cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1123b3eb3cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1223b3eb3cSopenharmony_ci * See the License for the specific language governing permissions and
1323b3eb3cSopenharmony_ci * limitations under the License.
1423b3eb3cSopenharmony_ci */
1523b3eb3cSopenharmony_ci
1623b3eb3cSopenharmony_ci#ifndef FOUNDATION_ACE_INTERFACES_OBSERVER_H
1723b3eb3cSopenharmony_ci#define FOUNDATION_ACE_INTERFACES_OBSERVER_H
1823b3eb3cSopenharmony_ci
1923b3eb3cSopenharmony_ci#include <list>
2023b3eb3cSopenharmony_ci#include <optional>
2123b3eb3cSopenharmony_ci#include <unordered_map>
2223b3eb3cSopenharmony_ci#include <string>
2323b3eb3cSopenharmony_ci
2423b3eb3cSopenharmony_ci#include "napi/native_api.h"
2523b3eb3cSopenharmony_ci#include "napi/native_common.h"
2623b3eb3cSopenharmony_ci#include "napi/native_node_api.h"
2723b3eb3cSopenharmony_ci
2823b3eb3cSopenharmony_ci#include "ui_observer_listener.h"
2923b3eb3cSopenharmony_ci
3023b3eb3cSopenharmony_ci#include "core/components_ng/base/observer_handler.h"
3123b3eb3cSopenharmony_ci
3223b3eb3cSopenharmony_cinamespace OHOS::Ace::Napi {
3323b3eb3cSopenharmony_ciclass UIObserver {
3423b3eb3cSopenharmony_cipublic:
3523b3eb3cSopenharmony_ci    static void RegisterNavigationCallback(const std::shared_ptr<UIObserverListener>& listener);
3623b3eb3cSopenharmony_ci    static void RegisterNavigationCallback(
3723b3eb3cSopenharmony_ci        std::string navigationId, const std::shared_ptr<UIObserverListener>& listener);
3823b3eb3cSopenharmony_ci    static void UnRegisterNavigationCallback(napi_value cb);
3923b3eb3cSopenharmony_ci    static void UnRegisterNavigationCallback(std::string navigationId, napi_value cb);
4023b3eb3cSopenharmony_ci    static void HandleNavigationStateChange(const NG::NavDestinationInfo& info);
4123b3eb3cSopenharmony_ci
4223b3eb3cSopenharmony_ci    static void RegisterScrollEventCallback(const std::shared_ptr<UIObserverListener>& listener);
4323b3eb3cSopenharmony_ci    static void RegisterScrollEventCallback(
4423b3eb3cSopenharmony_ci        const std::string& id, const std::shared_ptr<UIObserverListener>& listener);
4523b3eb3cSopenharmony_ci    static void UnRegisterScrollEventCallback(napi_value cb);
4623b3eb3cSopenharmony_ci    static void UnRegisterScrollEventCallback(const std::string& id, napi_value cb);
4723b3eb3cSopenharmony_ci    static void HandleScrollEventStateChange(const std::string& id, int32_t uniqueId,
4823b3eb3cSopenharmony_ci        NG::ScrollEventType eventType, float offset);
4923b3eb3cSopenharmony_ci
5023b3eb3cSopenharmony_ci    static void RegisterRouterPageCallback(
5123b3eb3cSopenharmony_ci        napi_env env, napi_value uiAbilityContext, const std::shared_ptr<UIObserverListener>& listener);
5223b3eb3cSopenharmony_ci    static void RegisterRouterPageCallback(
5323b3eb3cSopenharmony_ci        int32_t uiContextInstanceId, const std::shared_ptr<UIObserverListener>& listener);
5423b3eb3cSopenharmony_ci    static void UnRegisterRouterPageCallback(napi_env env, napi_value uiAbilityContext, napi_value callback);
5523b3eb3cSopenharmony_ci    static void UnRegisterRouterPageCallback(int32_t uiContextInstanceId, napi_value callback);
5623b3eb3cSopenharmony_ci    static void HandleRouterPageStateChange(NG::AbilityContextInfo& info, const NG::RouterPageInfoNG& pageInfo);
5723b3eb3cSopenharmony_ci
5823b3eb3cSopenharmony_ci    static void RegisterDensityCallback(
5923b3eb3cSopenharmony_ci        int32_t uiContextInstanceId, const std::shared_ptr<UIObserverListener>& listener);
6023b3eb3cSopenharmony_ci    static void UnRegisterDensityCallback(int32_t uiContextInstanceId, napi_value callback);
6123b3eb3cSopenharmony_ci    static void HandleDensityChange(NG::AbilityContextInfo& info, double density);
6223b3eb3cSopenharmony_ci    static void RegisterDrawCallback(int32_t uiContextInstanceId, const std::shared_ptr<UIObserverListener>& listener);
6323b3eb3cSopenharmony_ci    static void UnRegisterDrawCallback(int32_t uiContextInstanceId, napi_value callback);
6423b3eb3cSopenharmony_ci    static void RegisterLayoutCallback(
6523b3eb3cSopenharmony_ci        int32_t uiContextInstanceId, const std::shared_ptr<UIObserverListener>& listener);
6623b3eb3cSopenharmony_ci    static void UnRegisterLayoutCallback(int32_t uiContextInstanceId, napi_value callback);
6723b3eb3cSopenharmony_ci    static void HandDrawCommandSendChange();
6823b3eb3cSopenharmony_ci    static void HandLayoutDoneChange();
6923b3eb3cSopenharmony_ci
7023b3eb3cSopenharmony_ci    static void RegisterNavDestinationSwitchCallback(
7123b3eb3cSopenharmony_ci        napi_env env, napi_value uiAbilityContext, const std::optional<std::string>& navigationId,
7223b3eb3cSopenharmony_ci        const std::shared_ptr<UIObserverListener>& listener);
7323b3eb3cSopenharmony_ci    static void RegisterNavDestinationSwitchCallback(
7423b3eb3cSopenharmony_ci        int32_t uiContextInstanceId, const std::optional<std::string>& navigationId,
7523b3eb3cSopenharmony_ci        const std::shared_ptr<UIObserverListener>& listener);
7623b3eb3cSopenharmony_ci    static void UnRegisterNavDestinationSwitchCallback(napi_env env, napi_value uiAbilityContext,
7723b3eb3cSopenharmony_ci        const std::optional<std::string>& navigationId, napi_value callback);
7823b3eb3cSopenharmony_ci    static void UnRegisterNavDestinationSwitchCallback(int32_t uiContextInstanceId,
7923b3eb3cSopenharmony_ci        const std::optional<std::string>& navigationId, napi_value callback);
8023b3eb3cSopenharmony_ci    static void HandleNavDestinationSwitch(
8123b3eb3cSopenharmony_ci        const NG::AbilityContextInfo& info, NG::NavDestinationSwitchInfo& switchInfo);
8223b3eb3cSopenharmony_ci
8323b3eb3cSopenharmony_ci    static void RegisterWillClickCallback(
8423b3eb3cSopenharmony_ci        napi_env env, napi_value uiAbilityContext, const std::shared_ptr<UIObserverListener>& listener);
8523b3eb3cSopenharmony_ci    static void RegisterWillClickCallback(
8623b3eb3cSopenharmony_ci        int32_t uiContextInstanceId, const std::shared_ptr<UIObserverListener>& listener);
8723b3eb3cSopenharmony_ci    static void UnRegisterWillClickCallback(napi_env env, napi_value uiAbilityContext, napi_value callback);
8823b3eb3cSopenharmony_ci    static void UnRegisterWillClickCallback(int32_t uiContextInstanceId, napi_value callback);
8923b3eb3cSopenharmony_ci    static void HandleWillClick(NG::AbilityContextInfo& info, const GestureEvent& gestureEventInfo,
9023b3eb3cSopenharmony_ci        const ClickInfo& clickInfo, const RefPtr<NG::FrameNode>& frameNode);
9123b3eb3cSopenharmony_ci
9223b3eb3cSopenharmony_ci    static void RegisterDidClickCallback(
9323b3eb3cSopenharmony_ci        napi_env env, napi_value uiAbilityContext, const std::shared_ptr<UIObserverListener>& listener);
9423b3eb3cSopenharmony_ci    static void RegisterDidClickCallback(
9523b3eb3cSopenharmony_ci        int32_t uiContextInstanceId, const std::shared_ptr<UIObserverListener>& listener);
9623b3eb3cSopenharmony_ci    static void UnRegisterDidClickCallback(napi_env env, napi_value uiAbilityContext, napi_value callback);
9723b3eb3cSopenharmony_ci    static void UnRegisterDidClickCallback(int32_t uiContextInstanceId, napi_value callback);
9823b3eb3cSopenharmony_ci    static void HandleDidClick(NG::AbilityContextInfo& info, const GestureEvent& gestureEventInfo,
9923b3eb3cSopenharmony_ci        const ClickInfo& clickInfo, const RefPtr<NG::FrameNode>& frameNode);
10023b3eb3cSopenharmony_ci
10123b3eb3cSopenharmony_ci    static void RegisterTabContentStateCallback(const std::shared_ptr<UIObserverListener>& listener);
10223b3eb3cSopenharmony_ci    static void RegisterTabContentStateCallback(
10323b3eb3cSopenharmony_ci        const std::string& id, const std::shared_ptr<UIObserverListener>& listener);
10423b3eb3cSopenharmony_ci    static void UnRegisterTabContentStateCallback(napi_value cb);
10523b3eb3cSopenharmony_ci    static void UnRegisterTabContentStateCallback(const std::string& id, napi_value cb);
10623b3eb3cSopenharmony_ci    static void HandleTabContentStateChange(const NG::TabContentInfo& tabContentInfo);
10723b3eb3cSopenharmony_ci
10823b3eb3cSopenharmony_ci    static bool ParseStringFromNapi(napi_env env, napi_value val, std::string& str);
10923b3eb3cSopenharmony_ci    static bool MatchValueType(napi_env env, napi_value value, napi_valuetype targetType);
11023b3eb3cSopenharmony_ciprivate:
11123b3eb3cSopenharmony_ci    static void HandleAbilityUIContextNavDestinationSwitch(
11223b3eb3cSopenharmony_ci        const NG::AbilityContextInfo& info, NG::NavDestinationSwitchInfo& switchInfo);
11323b3eb3cSopenharmony_ci    static void HandleUIContextNavDestinationSwitch(const NG::NavDestinationSwitchInfo& switchInfo);
11423b3eb3cSopenharmony_ci    using NavIdAndListenersMap =
11523b3eb3cSopenharmony_ci        std::unordered_map<std::optional<std::string>, std::list<std::shared_ptr<UIObserverListener>>>;
11623b3eb3cSopenharmony_ci    static void HandleListenersWithEmptyNavigationId(
11723b3eb3cSopenharmony_ci        const NavIdAndListenersMap& listenersMap, const NG::NavDestinationSwitchInfo& switchInfo);
11823b3eb3cSopenharmony_ci    static void HandleListenersWithSpecifiedNavigationId(
11923b3eb3cSopenharmony_ci        const NavIdAndListenersMap& listenersMap, const NG::NavDestinationSwitchInfo& switchInfo);
12023b3eb3cSopenharmony_ci    static void GetAbilityInfos(napi_env env, napi_value abilityContext, NG::AbilityContextInfo& info);
12123b3eb3cSopenharmony_ci    static napi_env GetCurrentNapiEnv();
12223b3eb3cSopenharmony_ci
12323b3eb3cSopenharmony_ci    static std::list<std::shared_ptr<UIObserverListener>> unspecifiedNavigationListeners_;
12423b3eb3cSopenharmony_ci    static std::unordered_map<std::string, std::list<std::shared_ptr<UIObserverListener>>>
12523b3eb3cSopenharmony_ci        specifiedCNavigationListeners_;
12623b3eb3cSopenharmony_ci    static std::list<std::shared_ptr<UIObserverListener>> scrollEventListeners_;
12723b3eb3cSopenharmony_ci    static std::unordered_map<std::string, std::list<std::shared_ptr<UIObserverListener>>>
12823b3eb3cSopenharmony_ci        specifiedScrollEventListeners_;
12923b3eb3cSopenharmony_ci    static std::unordered_map<napi_ref, std::list<std::shared_ptr<UIObserverListener>>>
13023b3eb3cSopenharmony_ci        abilityContextRouterPageListeners_;
13123b3eb3cSopenharmony_ci    static std::unordered_map<int32_t, std::list<std::shared_ptr<UIObserverListener>>>
13223b3eb3cSopenharmony_ci        specifiedRouterPageListeners_;
13323b3eb3cSopenharmony_ci    static std::unordered_map<napi_ref, NG::AbilityContextInfo> infosForRouterPage_;
13423b3eb3cSopenharmony_ci    static std::unordered_map<int32_t, std::list<std::shared_ptr<UIObserverListener>>>
13523b3eb3cSopenharmony_ci        specifiedDensityListeners_;
13623b3eb3cSopenharmony_ci    static std::unordered_map<int32_t, std::list<std::shared_ptr<UIObserverListener>>> specifiedDrawListeners_;
13723b3eb3cSopenharmony_ci    static std::unordered_map<int32_t, std::list<std::shared_ptr<UIObserverListener>>> specifiedLayoutListeners_;
13823b3eb3cSopenharmony_ci    static std::unordered_map<napi_ref, std::list<std::shared_ptr<UIObserverListener>>>
13923b3eb3cSopenharmony_ci        abilityContextWillClickListeners_;
14023b3eb3cSopenharmony_ci    static std::unordered_map<int32_t, std::list<std::shared_ptr<UIObserverListener>>>
14123b3eb3cSopenharmony_ci        specifiedWillClickListeners_;
14223b3eb3cSopenharmony_ci    static std::unordered_map<napi_ref, std::list<std::shared_ptr<UIObserverListener>>>
14323b3eb3cSopenharmony_ci        abilityContextDidClickListeners_;
14423b3eb3cSopenharmony_ci    static std::unordered_map<int32_t, std::list<std::shared_ptr<UIObserverListener>>>
14523b3eb3cSopenharmony_ci        specifiedDidClickListeners_;
14623b3eb3cSopenharmony_ci    static std::list<std::shared_ptr<UIObserverListener>> tabContentStateListeners_;
14723b3eb3cSopenharmony_ci    static std::unordered_map<std::string, std::list<std::shared_ptr<UIObserverListener>>>
14823b3eb3cSopenharmony_ci        specifiedTabContentStateListeners_;
14923b3eb3cSopenharmony_ci
15023b3eb3cSopenharmony_ci    static std::unordered_map<napi_ref, NavIdAndListenersMap> abilityUIContextNavDesSwitchListeners_;
15123b3eb3cSopenharmony_ci    static std::unordered_map<int32_t, NavIdAndListenersMap> uiContextNavDesSwitchListeners_;
15223b3eb3cSopenharmony_ci    static std::unordered_map<napi_ref, NG::AbilityContextInfo> infosForNavDesSwitch_;
15323b3eb3cSopenharmony_ci    static std::unordered_map<napi_ref, NG::AbilityContextInfo> willClickInfos_;
15423b3eb3cSopenharmony_ci    static std::unordered_map<napi_ref, NG::AbilityContextInfo> didClickInfos_;
15523b3eb3cSopenharmony_ci};
15623b3eb3cSopenharmony_ci} // namespace OHOS::Ace::Napi
15723b3eb3cSopenharmony_ci#endif // FOUNDATION_ACE_INTERFACES_OBSERVER_H
158