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/js_frontend/js_ace_page.h"
23 #include "bridge/cj_frontend/frontend/cj_page_router_abstract.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) override;
38     std::string GetParams() const override;
39     std::string GetCurrentPageUrl() override;
GetLoadingPage()40     RefPtr<JsAcePage> GetLoadingPage()
41     {
42         return loadingPage_;
43     }
FlushReload()44     void FlushReload() override
45     {
46         LOGE("not support in old pipeline");
47     }
48 
49 protected:
50     void StartPush(const RouterPageInfo& target, const std::string& params, RouterMode mode) override;
51     bool StartPop() override;
52     void StartReplace(const RouterPageInfo& target, const std::string& params, RouterMode mode) override;
53     void BackCheckAlert(const RouterPageInfo& target, const std::string& params) override;
54     void StartClean() override;
55     void LoadPage(int32_t pageId, const RouterPageInfo& target, const std::string& params, bool isRestore = false,
56         bool needHideLast = true, bool needTransition = true) override;
57     void PopPage(const std::string& params, bool needShowNext, bool needTransition) override;
58     void PopPageToIndex(int32_t index, const std::string& params, bool needShowNext, bool needTransition) override;
59 
60     void FlushPage(const RefPtr<JsAcePage>& page, const std::string& url);
61     void OnPageReady(const RefPtr<JsAcePage>& page, const std::string& url, bool needHideLast);
62     void OnPageUpdate(const RefPtr<JsAcePage>& page, bool directExecute);
63     bool OnPopPage(bool needShowNext, bool needTransition);
64     void OnPopSuccess();
65     void SetCurrentPage();
66     bool OnPopPageToIndex(int32_t index, bool needShowNext, bool needTransition);
67     bool OnCleanPageStack();
68 
69 private:
70     void PushLoadingPage(const RefPtr<JsAcePage>& page);
GetTopPage()71     RefPtr<JsAcePage> GetTopPage() const
72     {
73         if (pageRouterStack_.empty()) {
74             LOGE("fail to get current page node due to page is null");
75             return nullptr;
76         }
77         return pageRouterStack_.back();
78     }
79 
80     void PostTask(std::function<void()> callback, bool isUI = true);
81     void OnPrePageChange(const RefPtr<JsAcePage>&);
82 
83     BaseId::IdType lastTransitionListener_ = 0;
84     std::list<RefPtr<JsAcePage>> pageRouterStack_;
85     RefPtr<JsAcePage> loadingPage_;
86 
87     ACE_DISALLOW_COPY_AND_MOVE(CJPageRouter);
88 };
89 }
90 
91 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_CJ_FRONTEND_CJ_PAGE_ROUTER_H
92