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_BRIDGE_CJ_FRONTEND_CJ_PAGE_ROUTER_ABSTRACT_H 17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_CJ_FRONTEND_CJ_PAGE_ROUTER_ABSTRACT_H 18 19 #include "base/memory/ace_type.h" 20 #include "bridge/common/manifest/manifest_parser.h" 21 22 namespace OHOS::Ace { 23 24 namespace NG { 25 class FrameNode; 26 } 27 28 class CJFrontendAbstract; 29 30 extern "C" ACE_EXPORT void* OHOSAceFrameworkGetCJFrontendLoader(); 31 32 constexpr int32_t MAX_ROUTER_STACK_SIZE = 32; 33 class CJPageRouterAbstract : public AceType { 34 DECLARE_ACE_TYPE(CJPageRouterAbstract, AceType); 35 public: 36 enum class RouterMode { 37 STANDARD = 0, 38 SINGLE, 39 }; 40 41 enum class RouterAction { 42 DEFAULT = 0, 43 PUSH, 44 REPLACE, 45 BACK, 46 CLEAR, 47 }; 48 49 struct RouterPageInfo { 50 std::string url; 51 RouterMode routerMode = RouterMode::STANDARD; 52 std::string path; 53 std::function<void(int32_t)> callback; 54 }; 55 56 struct RouterTask { 57 RouterAction action = RouterAction::PUSH; 58 RouterPageInfo routerPageInfo; 59 std::string params; 60 }; 61 62 struct RouterState { 63 int32_t index; 64 char* name; 65 char* path; 66 char* params; 67 }; 68 69 struct RouterStateList { 70 RouterState* array; 71 int64_t size; 72 void (*free)(int64_t, RouterState*); 73 }; 74 RouterStateListFree(int64_t size,RouterState * src)75 static void RouterStateListFree(int64_t size, RouterState* src) 76 { 77 if (!src) { 78 return; 79 } 80 for (int64_t i = 0; i < size; i++) { 81 if (src[i].name) { 82 delete[] src[i].name; 83 } 84 if (src[i].path) { 85 delete[] src[i].path; 86 } 87 if (src[i].params) { 88 delete[] src[i].params; 89 } 90 } 91 delete[] src; 92 } 93 94 virtual void OnShowCurrent() = 0; 95 virtual void OnHideCurrent() = 0; 96 virtual std::string GetParams() const = 0; 97 virtual int32_t GetStackSize() const = 0; 98 virtual std::string GetCurrentPageUrl() = 0; 99 virtual void EnableAlertBeforeBackPage(const std::string& message, std::function<void(int32_t)> callback) = 0; 100 virtual void DisableAlertBeforeBackPage() = 0; 101 virtual bool AllowPopLastPage() = 0; 102 virtual RefPtr<NG::FrameNode> GetCurrentPageNode(); 103 virtual bool PopWithExitCheck(); 104 virtual void StartPushPageWithCallback(const RouterPageInfo& target, const std::string& params) = 0; 105 virtual void StartReplacePageWithCallback(const RouterPageInfo& target, const std::string& params) = 0; 106 virtual void BackCheckAlertIndex(int32_t index, const std::string& params) = 0; 107 virtual void GetState(int32_t& index, std::string& name, std::string& path, std::string& params) = 0; 108 virtual void GetStateByIndex(int32_t& index, std::string& name, std::string& path, std::string& params) = 0; 109 virtual std::vector<RouterState> GetStateByUrl(const std::string& url) = 0; 110 void RunPage(const std::string& url, const std::string& params); 111 void Push(const RouterPageInfo& target, const std::string& params, RouterMode = RouterMode::STANDARD); 112 bool Pop(); 113 void Replace(const RouterPageInfo& target, const std::string& params, RouterMode = RouterMode::STANDARD); 114 void BackWithTarget(const RouterPageInfo& target, const std::string& params); 115 void Clear(); 116 virtual void FlushReload() = 0; SetManifestParser(const RefPtr<Framework::ManifestParser> & manifestParser)117 void SetManifestParser(const RefPtr<Framework::ManifestParser>& manifestParser) 118 { 119 manifestParser_ = manifestParser; 120 } 121 GetNativeViews()122 std::list<int64_t> GetNativeViews() 123 { 124 return viewStack_; 125 } 126 AddView(int64_t viewId)127 void AddView(int64_t viewId) 128 { 129 viewStack_.emplace_back(viewId); 130 } 131 void PushPageWithCallback(const RouterPageInfo& target, const std::string& params); 132 void ReplacePageWithCallback(const RouterPageInfo& target, const std::string& params); 133 void BackWithIndex(int32_t index, const std::string& params); 134 135 protected: CJPageRouterAbstract(WeakPtr<CJFrontendAbstract> frontend)136 explicit CJPageRouterAbstract(WeakPtr<CJFrontendAbstract> frontend) : frontend_(std::move(frontend)) {} 137 virtual void StartPush( 138 const RouterPageInfo& target, const std::string& params, RouterMode mode = RouterMode::STANDARD) = 0; 139 virtual bool StartPop() = 0; 140 virtual void StartReplace( 141 const RouterPageInfo& target, const std::string& params, RouterMode mode = RouterMode::STANDARD) = 0; 142 virtual void BackCheckAlert(const RouterPageInfo& target, const std::string& params) = 0; 143 virtual void StartClean() = 0; 144 145 // page operations 146 virtual void LoadPage(int32_t pageId, const RouterPageInfo& target, const std::string& params, 147 bool isRestore = false, bool needHideLast = true, bool needTransition = true) = 0; 148 virtual void PopPage(const std::string& params, bool needShowNext, bool needTransition) = 0; 149 virtual void PopPageToIndex(int32_t index, const std::string& params, bool needShowNext, bool needTransition) = 0; 150 151 protected: 152 class ProcessGuard { 153 public: ProcessGuard(CJPageRouterAbstract * router)154 explicit ProcessGuard(CJPageRouterAbstract* router) : router_(router) 155 { 156 router_->inRouterOpt_ = true; 157 } ~ProcessGuard()158 ~ProcessGuard() 159 { 160 router_->inRouterOpt_ = false; 161 router_ = nullptr; 162 } 163 164 private: 165 CJPageRouterAbstract* router_ = nullptr; 166 }; 167 168 int32_t GenerateNextPageId(); 169 170 WeakPtr<CJFrontendAbstract> frontend_; 171 RefPtr<Framework::ManifestParser> manifestParser_; 172 bool inRouterOpt_ = false; 173 RouterPageInfo ngBackUri_ = { "" }; 174 std::string backParam_; 175 std::list<int64_t> viewStack_; 176 int32_t ngBackIndex_ = 0; 177 178 private: 179 int32_t nextPageId_ = 0; 180 181 ACE_DISALLOW_COPY_AND_MOVE(CJPageRouterAbstract); 182 }; 183 184 } // namespace OHOS::Ace 185 186 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_CJ_FRONTEND_CJ_PAGE_ROUTER_ABSTRACT_H 187