1 /*
2  * Copyright (c) 2024 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_COMMON_PIPELINE_NG_NAVIGATION_MANAGER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_PIPELINE_NG_NAVIGATION_MANAGER_H
18 
19 #include <cstdint>
20 #include <functional>
21 #include <map>
22 
23 #include "base/json/json_util.h"
24 #include "base/memory/ace_type.h"
25 
26 namespace OHOS::Ace::NG {
27 class NavigationStack;
28 struct NavigationInfo {
29     std::string navigationId;
30     WeakPtr<NavigationStack> pathStack;
31 
32     NavigationInfo() = default;
NavigationInfoNavigationInfo33     NavigationInfo(const std::string& id, const WeakPtr<NavigationStack>& navigationStack)
34         : navigationId(std::move(id)), pathStack(navigationStack)
35     {}
36 };
37 
38 struct NavdestinationRecoveryInfo {
39     std::string name;
40     std::string param;
41     // mode of navdestination, 0 for standard page and 1 for dialog page
42     int32_t mode;
43 
NavdestinationRecoveryInfoNavdestinationRecoveryInfo44     NavdestinationRecoveryInfo(const std::string& name, const std::string& param, int32_t mode)
45         : name(std::move(name)), param(std::move(param)), mode(mode) {}
46 };
47 
48 class NavigationManager : public virtual AceType {
49     DECLARE_ACE_TYPE(NavigationManager, AceType);
50 public:
51     using DumpLogDepth = int;
52     using DumpCallback = std::function<void(DumpLogDepth)>;
53     NavigationManager() = default;
54     ~NavigationManager() = default;
55 
56     void AddNavigationDumpCallback(int32_t nodeId, int32_t depth, const DumpCallback& callback);
57     void RemoveNavigationDumpCallback(int32_t nodeId, int32_t depth);
58 
59     void OnDumpInfo();
60 
AddNavigationUpdateCallback(std::function<void ()> callback)61     void AddNavigationUpdateCallback(std::function<void()> callback)
62     {
63         updateCallbacks_.emplace_back(callback);
64     }
65 
66     void FireNavigationUpdateCallback();
67     std::shared_ptr<NavigationInfo> GetNavigationInfo(const RefPtr<AceType>& node);
68 
IsInteractive()69     bool IsInteractive() const
70     {
71         return isInteractive_;
72     }
73 
SetInteractive(int32_t frameNodeId)74     void SetInteractive(int32_t frameNodeId)
75     {
76         isInteractive_ = true;
77         interactiveAnimationId_ = frameNodeId;
78     }
79 
FinishInteractiveAnimation()80     void FinishInteractiveAnimation()
81     {
82         isInteractive_ = false;
83     }
84 
85     bool AddInteractiveAnimation(const std::function<void()>& addCallback);
86 
87     bool AddRecoverableNavigation(std::string id, RefPtr<AceType> navigationNode);
88     std::unique_ptr<JsonValue> GetNavigationJsonInfo();
89     void StorageNavigationRecoveryInfo(std::unique_ptr<JsonValue> allNavigationInfo);
90     const std::vector<NavdestinationRecoveryInfo> GetNavigationRecoveryInfo(std::string navigationId);
91 
92 private:
93     struct DumpMapKey {
94         int32_t nodeId;
95         int32_t depth;
96 
DumpMapKeyDumpMapKey97         DumpMapKey(int32_t n, int32_t d) : nodeId(n), depth(d) {}
98         bool operator< (const DumpMapKey& o) const
99         {
100             if (depth != o.depth) {
101                 return depth < o.depth;
102             }
103             return nodeId < o.nodeId;
104         }
105     };
106     std::unordered_map<std::string, WeakPtr<AceType>> recoverableNavigationMap_;
107     std::unordered_map<std::string, std::vector<NavdestinationRecoveryInfo>> navigationRecoveryInfo_;
108     std::map<DumpMapKey, DumpCallback> dumpMap_;
109     std::vector<std::function<void()>> updateCallbacks_;
110     bool isInteractive_ = false;
111     int32_t interactiveAnimationId_ = -1;
112 };
113 } // namespace OHOS::Ace::NG
114 
115 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_PIPELINE_NG_NAVIGATION_MANAGER_H
116