• 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 
38 enum class RouterMode {
39     STANDARD = 0,
40     SINGLE,
41 };
42 
43 enum class RouterAction {
44     DEFAULT = 0,
45     PUSH,
46     REPLACE,
47     BACK,
48     CLEAR,
49 };
50 
51 struct RouterPageInfo {
52     std::string url;
53     RouterMode routerMode = RouterMode::STANDARD;
54     std::string path;
55 };
56 
57 struct RouterTask {
58     RouterAction action = RouterAction::PUSH;
59     RouterPageInfo routerPageInfo;
60     std::string params;
61 };
62 
63 class PageRouterManager : public AceType {
64     DECLARE_ACE_TYPE(PageRouterManager, AceType)
65 public:
66     PageRouterManager() = default;
67     ~PageRouterManager() override = default;
68 
69     void RunPage(const std::string& url, const std::string& params);
70 
71     void RunCard(const std::string& url, const std::string& params, int64_t cardId);
72 
SetManifestParser(const RefPtr<Framework::ManifestParser> & manifestParser)73     void SetManifestParser(const RefPtr<Framework::ManifestParser>& manifestParser)
74     {
75         manifestParser_ = manifestParser;
76     }
77 
SetLoadJsCallback(LoadPageCallback && callback)78     void SetLoadJsCallback(LoadPageCallback&& callback)
79     {
80         loadJs_ = std::move(callback);
81     }
82 
SetLoadCardCallback(const LoadCardCallback & callback)83     void SetLoadCardCallback(const LoadCardCallback& callback)
84     {
85         loadCard_ = callback;
86     }
87 
88     void EnableAlertBeforeBackPage(const std::string& message, std::function<void(int32_t)>&& callback);
89 
90     void ClearAlertCallback(const RefPtr<PageInfo>& pageInfo);
91 
92     void DisableAlertBeforeBackPage();
93 
94     // router operation
95     void Push(const RouterPageInfo& target, const std::string& params, RouterMode mode = RouterMode::STANDARD);
96     void PushWithCallback(const RouterPageInfo& target, const std::string& params,
97         const std::function<void(const std::string&, int32_t)>& errorCallback = nullptr,
98         RouterMode mode = RouterMode::STANDARD);
99     bool Pop();
100     void Replace(const RouterPageInfo& target, const std::string& params, RouterMode mode = RouterMode::STANDARD);
101     void ReplaceWithCallback(const RouterPageInfo& target, const std::string& params,
102         const std::function<void(const std::string&, int32_t)>& errorCallback = nullptr,
103         RouterMode mode = RouterMode::STANDARD);
104     void BackWithTarget(const RouterPageInfo& target, const std::string& params);
105     void Clear();
106     int32_t GetStackSize() const;
107 
108     void GetState(int32_t& index, std::string& name, std::string& path);
109 
110     std::string GetParams() const;
111 
GetCurrentPageNode()112     RefPtr<FrameNode> GetCurrentPageNode() const
113     {
114         if (pageRouterStack_.empty()) {
115             LOGE("fail to get current page node due to page is null");
116             return nullptr;
117         }
118         return pageRouterStack_.back().Upgrade();
119     }
120 
121     std::string GetCurrentPageUrl();
122 
123     // Get the currently running JS page information in NG structure.
124     RefPtr<Framework::RevSourceMap> GetCurrentPageSourceMap(const RefPtr<AssetManager>& assetManager);
125 
SetIsCard()126     void SetIsCard()
127     {
128         isCardRouter_ = true;
129     }
130 
131 private:
132     class RouterOptScope {
133     public:
RouterOptScope(PageRouterManager * manager)134         explicit RouterOptScope(PageRouterManager* manager) : manager_(manager)
135         {
136             manager_->inRouterOpt_ = true;
137         }
~RouterOptScope()138         ~RouterOptScope()
139         {
140             manager_->inRouterOpt_ = false;
141         }
142 
143     private:
144         PageRouterManager* manager_ = nullptr;
145     };
146 
147     // page id manage
148     int32_t GenerateNextPageId();
149 
150     std::pair<int32_t, RefPtr<FrameNode>> FindPageInStack(const std::string& url);
151 
152     void PushOhmUrl(const RouterPageInfo& target, const std::string& params, RouterMode mode,
153         const std::function<void(const std::string&, int32_t)>& errorCallback = nullptr);
154     void StartPush(const RouterPageInfo& target, const std::string& params, RouterMode mode = RouterMode::STANDARD,
155         const std::function<void(const std::string&, int32_t)>& errorCallback = nullptr);
156     void StartBack(const RouterPageInfo& target, const std::string& params, bool enableAlert = false);
157     bool StartPop();
158     void StartReplace(const RouterPageInfo& target, const std::string& params, RouterMode mode = RouterMode::STANDARD,
159         const std::function<void(const std::string&, int32_t)>& errorCallback = nullptr);
160     void ReplaceOhmUrl(const RouterPageInfo& target, const std::string& params, RouterMode mode,
161         const std::function<void(const std::string&, int32_t)>& errorCallback = nullptr);
162     void BackCheckAlert(const RouterPageInfo& target, const std::string& params);
163     void StartClean();
164     void CleanPageOverlay();
165 
166     // page operations
167     void LoadPage(int32_t pageId, const RouterPageInfo& target, const std::string& params, bool isRestore = false,
168         bool needHideLast = true, bool needTransition = true,
169         const std::function<void(const std::string&, int32_t)>& errorCallback = nullptr);
170     void MovePageToFront(int32_t index, const RefPtr<FrameNode>& pageNode, const std::string& params, bool needHideLast,
171         bool forceShowCurrent = false, bool needTransition = true);
172     void PopPage(const std::string& params, bool needShowNext, bool needTransition);
173     void PopPageToIndex(int32_t index, const std::string& params, bool needShowNext, bool needTransition);
174 
175     static bool OnPageReady(const RefPtr<FrameNode>& pageNode, bool needHideLast, bool needTransition,
176         bool isCardRouter = false, int64_t cardId = 0);
177     static bool OnPopPage(bool needShowNext, bool needTransition);
178     static bool OnPopPageToIndex(int32_t index, bool needShowNext, bool needTransition);
179     static bool OnCleanPageStack();
180 
181     void LoadCard(int32_t pageId, const RouterPageInfo& target, const std::string& params, int64_t cardId,
182         bool isRestore = false, bool needHideLast = true);
183 
184     RefPtr<Framework::ManifestParser> manifestParser_;
185 
186     bool inRouterOpt_ = false;
187     LoadPageCallback loadJs_;
188     LoadCardCallback loadCard_;
189     bool isCardRouter_ = false;
190     int32_t pageId_ = 0;
191     std::list<WeakPtr<FrameNode>> pageRouterStack_;
192     RouterPageInfo ngBackUri_ = { "" };
193     std::string backParam_;
194 
195     ACE_DISALLOW_COPY_AND_MOVE(PageRouterManager);
196 };
197 
198 } // namespace OHOS::Ace::NG
199 
200 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_PAGE_ROUTER_MANAGER_H
201