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 #include "bridge/cj_frontend/frontend/cj_page_router_abstract.h"
17
18 #include "bridge/cj_frontend/frontend/cj_frontend_abstract.h"
19
20 namespace OHOS::Ace {
21
GenerateNextPageId()22 int32_t CJPageRouterAbstract::GenerateNextPageId()
23 {
24 return ++nextPageId_;
25 }
26
PopWithExitCheck()27 bool CJPageRouterAbstract::PopWithExitCheck()
28 {
29 if (GetStackSize() == 1) {
30 if (AllowPopLastPage()) {
31 return false;
32 }
33 }
34 return Pop();
35 }
36
RunPage(const std::string & url,const std::string & params)37 void CJPageRouterAbstract::RunPage(const std::string& url, const std::string& params)
38 {
39 LOGI("router.Push pagePath = %{private}s", url.c_str());
40 RouterPageInfo info { url };
41 ProcessGuard guard(this);
42 LoadPage(GenerateNextPageId(), info, params);
43 }
44
Push(const RouterPageInfo & target,const std::string & params,RouterMode mode)45 void CJPageRouterAbstract::Push(const RouterPageInfo& target, const std::string& params, RouterMode mode)
46 {
47 if (inRouterOpt_) {
48 LOGI("in router opt, post push router task");
49 auto frontend = frontend_.Upgrade();
50 CHECK_NULL_VOID(frontend);
51 auto context = frontend->pipelineContextHolder_.Get();
52 CHECK_NULL_VOID(context);
53 context->PostAsyncEvent(
54 [weak = WeakClaim(this), target, params, mode]() {
55 auto router = weak.Upgrade();
56 CHECK_NULL_VOID(router);
57 router->Push(target, params, mode);
58 },
59 "CJPageRouterAbstract::Push");
60 return;
61 }
62 StartPush(target, params, mode);
63 }
64
Pop()65 bool CJPageRouterAbstract::Pop()
66 {
67 if (inRouterOpt_) {
68 LOGE("in router opt, post Pop router task failed");
69 return false;
70 }
71
72 return StartPop();
73 }
74
Replace(const RouterPageInfo & target,const std::string & params,RouterMode mode)75 void CJPageRouterAbstract::Replace(const RouterPageInfo& target, const std::string& params, RouterMode mode)
76 {
77 if (inRouterOpt_) {
78 LOGI("in router opt, post replace router task");
79 auto frontend = frontend_.Upgrade();
80 CHECK_NULL_VOID(frontend);
81 auto context = frontend->pipelineContextHolder_.Get();
82 CHECK_NULL_VOID(context);
83 context->PostAsyncEvent(
84 [weak = WeakClaim(this), target, params, mode]() {
85 auto router = weak.Upgrade();
86 CHECK_NULL_VOID(router);
87 router->Replace(target, params, mode);
88 },
89 "CJPageRouterAbstract::Replace");
90 return;
91 }
92 StartReplace(target, params, mode);
93 }
94
BackWithTarget(const RouterPageInfo & target,const std::string & params)95 void CJPageRouterAbstract::BackWithTarget(const RouterPageInfo& target, const std::string& params)
96 {
97 LOGD("router.Back path = %{private}s", target.url.c_str());
98 if (inRouterOpt_) {
99 LOGI("in router opt, post back router task");
100 auto frontend = frontend_.Upgrade();
101 CHECK_NULL_VOID(frontend);
102 auto context = frontend->pipelineContextHolder_.Get();
103 CHECK_NULL_VOID(context);
104 context->PostAsyncEvent(
105 [weak = WeakClaim(this), target, params]() {
106 auto router = weak.Upgrade();
107 CHECK_NULL_VOID(router);
108 router->BackWithTarget(target, params);
109 },
110 "CJPageRouterAbstract::BackWithTarget");
111 return;
112 }
113 BackCheckAlert(target, params);
114 }
115
Clear()116 void CJPageRouterAbstract::Clear()
117 {
118 if (inRouterOpt_) {
119 LOGI("in router opt, post clear router task");
120 auto frontend = frontend_.Upgrade();
121 CHECK_NULL_VOID(frontend);
122 auto context = frontend->pipelineContextHolder_.Get();
123 CHECK_NULL_VOID(context);
124 context->PostAsyncEvent(
125 [weak = WeakClaim(this)]() {
126 auto router = weak.Upgrade();
127 CHECK_NULL_VOID(router);
128 router->Clear();
129 },
130 "CJPageRouterAbstract::Clear");
131 return;
132 }
133 StartClean();
134 }
135
136 } // namespace OHOS::Ace