• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_BRIDGE_DECLARATIVE_FRONTEND_PAGE_ROUTER_MANAGER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_PAGE_ROUTER_MANAGER_H
18 
19 #include <cstdint>
20 #include <list>
21 #include <string>
22 #include <utility>
23 
24 #include "base/memory/ace_type.h"
25 #include "base/memory/referenced.h"
26 #include "base/thread/task_executor.h"
27 #include "base/utils/noncopyable.h"
28 #include "bridge/declarative_frontend/ng/entry_page_info.h"
29 #include "core/components_ng/base/frame_node.h"
30 #include "frameworks/bridge/common/manifest/manifest_parser.h"
31 
32 namespace OHOS::Ace::NG {
33 
34 using LoadPageCallback = std::function<bool(const std::string&,
35     const std::function<void(const std::string&, int32_t)>&)>;
36 using LoadCardCallback = std::function<bool(const std::string&, int64_t cardId)>;
37 using LoadNamedRouterCallback = std::function<bool(const std::string&, bool isTriggeredByJs)>;
38 
39 enum class RouterMode {
40     STANDARD = 0,
41     SINGLE,
42 };
43 
44 struct RouterPageInfo {
45     std::string url;
46     std::string params;
47     RouterMode routerMode = RouterMode::STANDARD;
48     std::function<void(const std::string&, int32_t)> errorCallback;
49     std::string path;
50     bool isNamedRouterMode = false;
51 };
52 
53 class PageRouterManager : public AceType {
54     DECLARE_ACE_TYPE(PageRouterManager, AceType)
55 public:
56     PageRouterManager() = default;
57     ~PageRouterManager() override = default;
58 
59     void RunPage(const std::string& url, const std::string& params);
60     void RunCard(const std::string& url, const std::string& params, int64_t cardId);
61 
SetManifestParser(const RefPtr<Framework::ManifestParser> & manifestParser)62     void SetManifestParser(const RefPtr<Framework::ManifestParser>& manifestParser)
63     {
64         manifestParser_ = manifestParser;
65     }
66 
SetLoadJsCallback(LoadPageCallback && callback)67     void SetLoadJsCallback(LoadPageCallback&& callback)
68     {
69         loadJs_ = std::move(callback);
70     }
71 
SetLoadCardCallback(const LoadCardCallback & callback)72     void SetLoadCardCallback(const LoadCardCallback& callback)
73     {
74         loadCard_ = callback;
75     }
76 
SetLoadNamedRouterCallback(LoadNamedRouterCallback && callback)77     void SetLoadNamedRouterCallback(LoadNamedRouterCallback&& callback)
78     {
79         loadNamedRouter_ = callback;
80     }
81 
82     void EnableAlertBeforeBackPage(const std::string& message, std::function<void(int32_t)>&& callback);
83 
84     void DisableAlertBeforeBackPage();
85 
86     // router operation
87     void Push(const RouterPageInfo& target);
88     void PushNamedRoute(const RouterPageInfo& target);
89     bool Pop();
90     void Replace(const RouterPageInfo& target);
91     void ReplaceNamedRoute(const RouterPageInfo& target);
92     void BackWithTarget(const RouterPageInfo& target);
93     void Clear();
94     int32_t GetStackSize() const;
95 
96     void GetState(int32_t& index, std::string& name, std::string& path);
97 
98     std::string GetParams() const;
99 
GetCurrentPageNode()100     RefPtr<FrameNode> GetCurrentPageNode() const
101     {
102         if (pageRouterStack_.empty()) {
103             LOGE("fail to get current page node due to page is null");
104             return nullptr;
105         }
106         return pageRouterStack_.back().Upgrade();
107     }
108 
109     std::string GetCurrentPageUrl();
110 
111     // Get the currently running JS page information in NG structure.
112     RefPtr<Framework::RevSourceMap> GetCurrentPageSourceMap(const RefPtr<AssetManager>& assetManager);
113 
SetIsCard()114     void SetIsCard()
115     {
116         isCardRouter_ = true;
117     }
118 
119     void FlushFrontend();
120 
121     std::unique_ptr<JsonValue> GetStackInfo();
122 
123     std::string RestoreRouterStack(std::unique_ptr<JsonValue> stackInfo);
124 
125 private:
126     class RouterOptScope {
127     public:
RouterOptScope(PageRouterManager * manager)128         explicit RouterOptScope(PageRouterManager* manager) : manager_(manager)
129         {
130             manager_->inRouterOpt_ = true;
131         }
~RouterOptScope()132         ~RouterOptScope()
133         {
134             manager_->inRouterOpt_ = false;
135         }
136 
137     private:
138         PageRouterManager* manager_ = nullptr;
139     };
140 
141     // page id manage
142     int32_t GenerateNextPageId();
143 
144     std::pair<int32_t, RefPtr<FrameNode>> FindPageInStack(const std::string& url, bool needIgnoreBegin = false);
145 
146     void LoadOhmUrl(const RouterPageInfo& target);
147     void PushOhmUrl(const RouterPageInfo& target);
148     void ReplaceOhmUrl(const RouterPageInfo& target);
149     void StartPush(const RouterPageInfo& target);
150     void StartReplace(const RouterPageInfo& target);
151     void StartBack(const RouterPageInfo& target);
152     bool StartPop();
153     void StartRestore(const RouterPageInfo& target);
154     void BackCheckAlert(const RouterPageInfo& target);
155     void StartClean();
156     void CleanPageOverlay();
157 
158     // page operations
159     void LoadPage(int32_t pageId, const RouterPageInfo& target, bool needHideLast = true, bool needTransition = true);
160     void MovePageToFront(int32_t index, const RefPtr<FrameNode>& pageNode, const RouterPageInfo& target,
161         bool needHideLast, bool forceShowCurrent = false, bool needTransition = true);
162     void PopPage(const std::string& params, bool needShowNext, bool needTransition);
163     void PopPageToIndex(int32_t index, const std::string& params, bool needShowNext, bool needTransition);
164 
165     static bool OnPageReady(const RefPtr<FrameNode>& pageNode, bool needHideLast, bool needTransition,
166         bool isCardRouter = false, int64_t cardId = 0);
167     static bool OnPopPage(bool needShowNext, bool needTransition);
168     static bool OnPopPageToIndex(int32_t index, bool needShowNext, bool needTransition);
169     static bool OnCleanPageStack();
170 
171     void LoadCard(int32_t pageId, const RouterPageInfo& target, const std::string& params, int64_t cardId,
172         bool isRestore = false, bool needHideLast = true);
173 
174     RefPtr<Framework::ManifestParser> manifestParser_;
175 
176     bool inRouterOpt_ = false;
177     LoadPageCallback loadJs_;
178     LoadCardCallback loadCard_;
179     LoadNamedRouterCallback loadNamedRouter_;
180     bool isCardRouter_ = false;
181     int32_t pageId_ = 0;
182     std::list<WeakPtr<FrameNode>> pageRouterStack_;
183     std::list<std::string> restorePageStack_;
184     RouterPageInfo ngBackTarget_;
185 
186     ACE_DISALLOW_COPY_AND_MOVE(PageRouterManager);
187 };
188 
189 } // namespace OHOS::Ace::NG
190 
191 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_PAGE_ROUTER_MANAGER_H
192