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 #include "interfaces/inner_api/ace/constants.h" 32 33 namespace OHOS::Ace::NG { 34 35 using LoadPageCallback = std::function<bool(const std::string&, 36 const std::function<void(const std::string&, int32_t)>&)>; 37 using LoadPageByBufferCallback = std::function<bool( 38 const std::shared_ptr<std::vector<uint8_t>>& content, const std::function<void(const std::string&, int32_t)>&)>; 39 using LoadCardCallback = std::function<bool(const std::string&, int64_t cardId, const std::string&)>; 40 using LoadNamedRouterCallback = std::function<bool(const std::string&, bool isTriggeredByJs)>; 41 using UpdateRootComponentCallback = std::function<bool()>; 42 #if defined(PREVIEW) 43 using IsComponentPreviewCallback = std::function<bool()>; 44 #endif 45 46 enum class RouterMode { 47 STANDARD = 0, 48 SINGLE, 49 }; 50 51 struct RouterPageInfo { 52 std::string url; 53 std::string params; 54 RouterMode routerMode = RouterMode::STANDARD; 55 std::function<void(const std::string&, int32_t)> errorCallback; 56 std::string path; 57 bool isNamedRouterMode = false; 58 std::shared_ptr<std::vector<uint8_t>> content; 59 }; 60 61 class PageRouterManager : public AceType { 62 DECLARE_ACE_TYPE(PageRouterManager, AceType) 63 public: 64 PageRouterManager() = default; 65 ~PageRouterManager() override = default; 66 67 void RunPage(const std::string& url, const std::string& params); 68 void RunPage(const std::shared_ptr<std::vector<uint8_t>>& content, const std::string& params); 69 void RunPageByNamedRouter(const std::string& name, const std::string& params); 70 UIContentErrorCode RunCard( 71 const std::string& url, const std::string& params, int64_t cardId, const std::string& entryPoint = ""); 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 SetLoadJsByBufferCallback(LoadPageByBufferCallback && callback)83 void SetLoadJsByBufferCallback(LoadPageByBufferCallback&& callback) 84 { 85 loadJsByBuffer_ = std::move(callback); 86 } 87 SetLoadCardCallback(const LoadCardCallback & callback)88 void SetLoadCardCallback(const LoadCardCallback& callback) 89 { 90 loadCard_ = callback; 91 } 92 SetLoadNamedRouterCallback(LoadNamedRouterCallback && callback)93 void SetLoadNamedRouterCallback(LoadNamedRouterCallback&& callback) 94 { 95 loadNamedRouter_ = callback; 96 } 97 SetUpdateRootComponentCallback(UpdateRootComponentCallback && callback)98 void SetUpdateRootComponentCallback(UpdateRootComponentCallback&& callback) 99 { 100 updateRootComponent_ = callback; 101 } 102 #if defined(PREVIEW) SetIsComponentPreview(IsComponentPreviewCallback && callback)103 void SetIsComponentPreview(IsComponentPreviewCallback&& callback) 104 { 105 isComponentPreview_ = callback; 106 } 107 #endif 108 109 void EnableAlertBeforeBackPage(const std::string& message, std::function<void(int32_t)>&& callback); 110 111 void DisableAlertBeforeBackPage(); 112 113 // router operation 114 void Push(const RouterPageInfo& target); 115 void PushNamedRoute(const RouterPageInfo& target); 116 bool Pop(); 117 void Replace(const RouterPageInfo& target); 118 void ReplaceNamedRoute(const RouterPageInfo& target); 119 void BackWithTarget(const RouterPageInfo& target); 120 void Clear(); 121 int32_t GetStackSize() const; 122 123 void GetState(int32_t& index, std::string& name, std::string& path); 124 125 std::string GetParams() const; 126 127 int32_t GetIndexByUrl(const std::string& url) const; 128 GetCurrentPageNode()129 RefPtr<FrameNode> GetCurrentPageNode() const 130 { 131 if (pageRouterStack_.empty()) { 132 return nullptr; 133 } 134 return pageRouterStack_.back().Upgrade(); 135 } 136 137 std::string GetCurrentPageUrl(); 138 139 // Get the currently running JS page information in NG structure. 140 RefPtr<Framework::RevSourceMap> GetCurrentPageSourceMap(const RefPtr<AssetManager>& assetManager); 141 SetIsCard()142 void SetIsCard() 143 { 144 isCardRouter_ = true; 145 } 146 147 void FlushFrontend(); 148 149 std::unique_ptr<JsonValue> GetStackInfo(); 150 151 std::pair<std::string, UIContentErrorCode> RestoreRouterStack(std::unique_ptr<JsonValue> stackInfo); 152 153 private: 154 class RouterOptScope { 155 public: RouterOptScope(PageRouterManager * manager)156 explicit RouterOptScope(PageRouterManager* manager) : manager_(manager) 157 { 158 manager_->inRouterOpt_ = true; 159 } ~RouterOptScope()160 ~RouterOptScope() 161 { 162 manager_->inRouterOpt_ = false; 163 } 164 165 private: 166 PageRouterManager* manager_ = nullptr; 167 }; 168 169 // page id manage 170 int32_t GenerateNextPageId(); 171 172 std::pair<int32_t, RefPtr<FrameNode>> FindPageInStack(const std::string& url, bool needIgnoreBegin = false); 173 174 void LoadOhmUrl(const RouterPageInfo& target); 175 void PushOhmUrl(const RouterPageInfo& target); 176 void ReplaceOhmUrl(const RouterPageInfo& target); 177 void StartPush(const RouterPageInfo& target); 178 void StartReplace(const RouterPageInfo& target); 179 void StartBack(const RouterPageInfo& target); 180 bool StartPop(); 181 void StartRestore(const RouterPageInfo& target); 182 void BackCheckAlert(const RouterPageInfo& target); 183 void StartClean(); 184 void CleanPageOverlay(); 185 186 // page operations 187 void LoadPage(int32_t pageId, const RouterPageInfo& target, bool needHideLast = true, bool needTransition = true); 188 void MovePageToFront(int32_t index, const RefPtr<FrameNode>& pageNode, const RouterPageInfo& target, 189 bool needHideLast, bool forceShowCurrent = false, bool needTransition = true); 190 void PopPage(const std::string& params, bool needShowNext, bool needTransition); 191 void PopPageToIndex(int32_t index, const std::string& params, bool needShowNext, bool needTransition); 192 193 static bool OnPageReady(const RefPtr<FrameNode>& pageNode, bool needHideLast, bool needTransition, 194 bool isCardRouter = false, int64_t cardId = 0); 195 static bool OnPopPage(bool needShowNext, bool needTransition); 196 static bool OnPopPageToIndex(int32_t index, bool needShowNext, bool needTransition); 197 static bool OnCleanPageStack(); 198 199 UIContentErrorCode LoadCard(int32_t pageId, const RouterPageInfo& target, const std::string& params, int64_t cardId, 200 bool isRestore = false, bool needHideLast = true, const std::string& entryPoint = ""); 201 202 RefPtr<Framework::ManifestParser> manifestParser_; 203 204 bool inRouterOpt_ = false; 205 LoadPageCallback loadJs_; 206 LoadPageByBufferCallback loadJsByBuffer_; 207 LoadCardCallback loadCard_; 208 LoadNamedRouterCallback loadNamedRouter_; 209 UpdateRootComponentCallback updateRootComponent_; 210 bool isCardRouter_ = false; 211 int32_t pageId_ = 0; 212 std::list<WeakPtr<FrameNode>> pageRouterStack_; 213 std::list<std::string> restorePageStack_; 214 RouterPageInfo ngBackTarget_; 215 #if defined(PREVIEW) 216 IsComponentPreviewCallback isComponentPreview_; 217 #endif 218 219 ACE_DISALLOW_COPY_AND_MOVE(PageRouterManager); 220 }; 221 222 } // namespace OHOS::Ace::NG 223 224 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_PAGE_ROUTER_MANAGER_H 225