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_H 17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_CJ_FRONTEND_CJ_PAGE_ROUTER_H 18 19 #include <mutex> 20 21 #include "base/utils/base_id.h" 22 #include "bridge/cj_frontend/frontend/cj_page_router_abstract.h" 23 #include "bridge/js_frontend/js_ace_page.h" 24 25 namespace OHOS::Ace::Framework { 26 class CJPageRouter final : public CJPageRouterAbstract { DECLARE_ACE_TYPE(CJPageRouter,CJPageRouterAbstract)27 DECLARE_ACE_TYPE(CJPageRouter, CJPageRouterAbstract) 28 public: 29 explicit CJPageRouter(WeakPtr<CJFrontendAbstract> frontend) : CJPageRouterAbstract(std::move(frontend)) {} 30 31 void OnShowCurrent() override; 32 void OnHideCurrent() override; 33 bool AllowPopLastPage() override; 34 35 void EnableAlertBeforeBackPage(const std::string& message, std::function<void(int32_t)> callback) override; 36 int32_t GetStackSize() const override; 37 void GetState(int32_t& index, std::string& name, std::string& path, std::string& params) override; 38 void GetStateByIndex(int32_t& index, std::string& name, std::string& path, std::string& params) override; 39 std::vector<CJPageRouterAbstract::RouterState> GetStateByUrl(const std::string& url) override; 40 void StartPushPageWithCallback(const RouterPageInfo& target, const std::string& params) override; 41 void StartReplacePageWithCallback(const RouterPageInfo& target, const std::string& params) override; 42 void BackCheckAlertIndex(int32_t index, const std::string& params) override; 43 void DisableAlertBeforeBackPage() override; 44 std::string GetParams() const override; 45 std::string GetCurrentPageUrl() override; GetLoadingPage()46 RefPtr<JsAcePage> GetLoadingPage() 47 { 48 return loadingPage_; 49 } FlushReload()50 void FlushReload() override 51 { 52 LOGE("not support in old pipeline"); 53 } 54 55 protected: 56 void StartPush(const RouterPageInfo& target, const std::string& params, RouterMode mode) override; 57 bool StartPop() override; 58 void StartReplace(const RouterPageInfo& target, const std::string& params, RouterMode mode) override; 59 void BackCheckAlert(const RouterPageInfo& target, const std::string& params) override; 60 void StartClean() override; 61 void LoadPage(int32_t pageId, const RouterPageInfo& target, const std::string& params, bool isRestore = false, 62 bool needHideLast = true, bool needTransition = true) override; 63 void PopPage(const std::string& params, bool needShowNext, bool needTransition) override; 64 void PopPageToIndex(int32_t index, const std::string& params, bool needShowNext, bool needTransition) override; 65 66 void FlushPage(const RefPtr<JsAcePage>& page, const std::string& url); 67 void OnPageReady(const RefPtr<JsAcePage>& page, const std::string& url, bool needHideLast); 68 void OnPageUpdate(const RefPtr<JsAcePage>& page, bool directExecute); 69 bool OnPopPage(bool needShowNext, bool needTransition); 70 void OnPopSuccess(); 71 void SetCurrentPage(); 72 bool OnPopPageToIndex(int32_t index, bool needShowNext, bool needTransition); 73 bool OnCleanPageStack(); 74 75 private: 76 void PushLoadingPage(const RefPtr<JsAcePage>& page); GetTopPage()77 RefPtr<JsAcePage> GetTopPage() const 78 { 79 if (pageRouterStack_.empty()) { 80 LOGE("fail to get current page node due to page is null"); 81 return nullptr; 82 } 83 return pageRouterStack_.back(); 84 } 85 86 void PostTask(std::function<void()> callback, bool isUI = true); 87 void OnPrePageChange(const RefPtr<JsAcePage>&); 88 89 BaseId::IdType lastTransitionListener_ = 0; 90 std::list<RefPtr<JsAcePage>> pageRouterStack_; 91 RefPtr<JsAcePage> loadingPage_; 92 93 ACE_DISALLOW_COPY_AND_MOVE(CJPageRouter); 94 }; 95 } // namespace OHOS::Ace::Framework 96 97 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_CJ_FRONTEND_CJ_PAGE_ROUTER_H 98