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_BASE_OBSERVER_HANDLER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_BASE_OBSERVER_HANDLER_H 18 19 #include <string> 20 #include <utility> 21 22 #include "base/memory/ace_type.h" 23 #include "base/memory/referenced.h" 24 #include "core/common/frontend.h" 25 #include "core/common/container.h" 26 #include "core/components_ng/pattern/stage/page_pattern.h" 27 28 namespace OHOS::Ace::NG { 29 enum class NavDestinationState { 30 ON_SHOWN = 0, 31 ON_HIDDEN = 1, 32 }; 33 34 struct NavDestinationInfo { 35 std::string navigationId; 36 std::string name; 37 NavDestinationState state; 38 NavDestinationInfoNavDestinationInfo39 NavDestinationInfo(std::string id, std::string name, NavDestinationState state) 40 : navigationId(std::move(id)), name(std::move(name)), state(state) 41 {} 42 }; 43 44 struct RouterPageInfoNG { 45 napi_value context; 46 int32_t index; 47 std::string name; 48 std::string path; 49 RouterPageState state; 50 RouterPageInfoNGRouterPageInfoNG51 RouterPageInfoNG(napi_value context, int32_t index, std::string name, std::string path, RouterPageState state) 52 : context(context), index(index), name(std::move(name)), path(std::move(path)), state(state) 53 {} 54 }; 55 56 struct AbilityContextInfo { 57 std::string name = ""; 58 std::string bundleName = ""; 59 std::string moduleName = ""; 60 IsEqualAbilityContextInfo61 bool IsEqual(AbilityContextInfo& info) 62 { 63 if (info.name != name) { 64 return false; 65 } 66 if (info.bundleName != bundleName) { 67 return false; 68 } 69 if (info.moduleName != moduleName) { 70 return false; 71 } 72 return true; 73 } 74 }; 75 76 class ACE_FORCE_EXPORT UIObserverHandler { 77 public: 78 UIObserverHandler() = default; 79 ~UIObserverHandler() = default; 80 static UIObserverHandler& GetInstance(); 81 void NotifyNavigationStateChange(const WeakPtr<AceType>& weakPattern, NavDestinationState state); 82 void NotifyRouterPageStateChange(const RefPtr<PageInfo>& pageInfo, RouterPageState state); 83 std::shared_ptr<NavDestinationInfo> GetNavigationState(const RefPtr<AceType>& node); 84 std::shared_ptr<RouterPageInfoNG> GetRouterPageState(const RefPtr<AceType>& node); 85 using NavigationHandleFunc = void (*)(const std::string&, const std::string&, NavDestinationState); 86 using RouterPageHandleFunc = void (*)( 87 AbilityContextInfo&, napi_value, int32_t, const std::string&, const std::string&, RouterPageState); 88 void SetHandleNavigationChangeFunc(NavigationHandleFunc func); 89 void SetHandleRouterPageChangeFunc(RouterPageHandleFunc func); 90 private: 91 NavigationHandleFunc navigationHandleFunc_ = nullptr; 92 RouterPageHandleFunc routerPageHandleFunc_ = nullptr; 93 napi_value GetUIContextValue(); 94 }; 95 } // namespace OHOS::Ace::NG 96 97 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_BASE_OBSERVER_HANDLER_H 98