• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "frameworks/bridge/declarative_frontend/ng/frontend_delegate_declarative_ng.h"
17 
18 #include "base/i18n/localization.h"
19 #include "base/log/ace_trace.h"
20 #include "base/log/event_report.h"
21 #include "base/resource/ace_res_config.h"
22 #include "base/thread/background_task_executor.h"
23 #include "base/utils/measure_util.h"
24 #include "base/utils/utils.h"
25 #include "core/common/ace_application_info.h"
26 #include "core/common/container.h"
27 #include "core/common/thread_checker.h"
28 #include "core/components_ng/pattern/stage/page_pattern.h"
29 #include "frameworks/bridge/common/utils/utils.h"
30 
31 namespace OHOS::Ace::Framework {
32 
33 namespace {
34 
35 const char MANIFEST_JSON[] = "manifest.json";
36 
37 } // namespace
38 
AttachPipelineContext(const RefPtr<PipelineBase> & context)39 void FrontendDelegateDeclarativeNG::AttachPipelineContext(const RefPtr<PipelineBase>& context)
40 {
41     pipelineContextHolder_.Attach(context);
42 }
43 
RunPage(const std::string & url,const std::string & params,const std::string & profile)44 void FrontendDelegateDeclarativeNG::RunPage(
45     const std::string& url, const std::string& params, const std::string& profile)
46 {
47     ACE_SCOPED_TRACE("FrontendDelegateDeclarativeNG::RunPage");
48 
49     LOGI("FrontendDelegateDeclarativeNG RunPage url=%{public}s", url.c_str());
50     std::string jsonContent;
51     if (GetAssetContent(MANIFEST_JSON, jsonContent)) {
52         manifestParser_->Parse(jsonContent);
53         manifestParser_->Printer();
54     } else if (!profile.empty() && GetAssetContent(profile, jsonContent)) {
55         LOGI("Parse profile %{public}s", profile.c_str());
56         manifestParser_->Parse(jsonContent);
57     } else {
58         LOGE("RunPage parse manifest.json failed");
59     }
60     std::string mainPagePath;
61     if (!url.empty()) {
62         mainPagePath = manifestParser_->GetRouter()->GetPagePath(url);
63     } else {
64         mainPagePath = manifestParser_->GetRouter()->GetEntry();
65     }
66     taskExecutor_->PostTask(
67         [manifestParser = manifestParser_, delegate = Claim(this),
68             weakPtr = WeakPtr<NG::PageRouterManager>(pageRouterManager_), url, params]() {
69             auto pageRouterManager = weakPtr.Upgrade();
70             CHECK_NULL_VOID(pageRouterManager);
71             pageRouterManager->SetManifestParser(manifestParser);
72             pageRouterManager->RunPage(url, params);
73             auto pipeline = delegate->GetPipelineContext();
74             // TODO: get platform version from context, and should stored in AceApplicationInfo.
75             if (manifestParser->GetMinPlatformVersion() > 0) {
76                 pipeline->SetMinPlatformVersion(manifestParser->GetMinPlatformVersion());
77             }
78         },
79         TaskExecutor::TaskType::JS);
80 }
81 
Push(const std::string & uri,const std::string & params)82 void FrontendDelegateDeclarativeNG::Push(const std::string& uri, const std::string& params)
83 {
84     CHECK_NULL_VOID(pageRouterManager_);
85     pageRouterManager_->Push({ uri }, params);
86     OnMediaQueryUpdate();
87 }
88 
PushWithMode(const std::string & uri,const std::string & params,uint32_t routerMode)89 void FrontendDelegateDeclarativeNG::PushWithMode(const std::string& uri, const std::string& params, uint32_t routerMode)
90 {
91     CHECK_NULL_VOID(pageRouterManager_);
92     // TODO: router mode support
93     pageRouterManager_->Push({ uri }, params);
94 }
95 
PushWithCallback(const std::string & uri,const std::string & params,const std::function<void (const std::string &,int32_t)> & errorCallback,uint32_t routerMode)96 void FrontendDelegateDeclarativeNG::PushWithCallback(const std::string& uri, const std::string& params,
97     const std::function<void(const std::string&, int32_t)>& errorCallback, uint32_t routerMode)
98 {
99     CHECK_NULL_VOID(pageRouterManager_);
100     pageRouterManager_->PushWithCallback({ uri }, params, errorCallback);
101     OnMediaQueryUpdate();
102 }
103 
Replace(const std::string & uri,const std::string & params)104 void FrontendDelegateDeclarativeNG::Replace(const std::string& uri, const std::string& params)
105 {
106     CHECK_NULL_VOID(pageRouterManager_);
107     pageRouterManager_->Replace({ uri }, params);
108 }
109 
ReplaceWithMode(const std::string & uri,const std::string & params,uint32_t routerMode)110 void FrontendDelegateDeclarativeNG::ReplaceWithMode(
111     const std::string& uri, const std::string& params, uint32_t routerMode)
112 {
113     CHECK_NULL_VOID(pageRouterManager_);
114     // TODO: router mode support
115     pageRouterManager_->Replace({ uri }, params);
116 }
117 
ReplaceWithCallback(const std::string & uri,const std::string & params,const std::function<void (const std::string &,int32_t)> & errorCallback,uint32_t routerMode)118 void FrontendDelegateDeclarativeNG::ReplaceWithCallback(const std::string& uri, const std::string& params,
119     const std::function<void(const std::string&, int32_t)>& errorCallback, uint32_t routerMode)
120 {
121     CHECK_NULL_VOID(pageRouterManager_);
122     pageRouterManager_->ReplaceWithCallback({ uri }, params, errorCallback);
123     OnMediaQueryUpdate();
124 }
125 
Back(const std::string & uri,const std::string & params)126 void FrontendDelegateDeclarativeNG::Back(const std::string& uri, const std::string& params)
127 {
128     CHECK_NULL_VOID(pageRouterManager_);
129     pageRouterManager_->BackWithTarget({ uri }, params);
130 }
131 
Clear()132 void FrontendDelegateDeclarativeNG::Clear()
133 {
134     CHECK_NULL_VOID(pageRouterManager_);
135     pageRouterManager_->Clear();
136 }
137 
GetStackSize() const138 int32_t FrontendDelegateDeclarativeNG::GetStackSize() const
139 {
140     CHECK_NULL_RETURN(pageRouterManager_, 0);
141     return pageRouterManager_->GetStackSize();
142 }
143 
GetState(int32_t & index,std::string & name,std::string & path)144 void FrontendDelegateDeclarativeNG::GetState(int32_t& index, std::string& name, std::string& path)
145 {
146     CHECK_NULL_VOID(pageRouterManager_);
147     pageRouterManager_->GetState(index, name, path);
148 }
149 
GetParams()150 std::string FrontendDelegateDeclarativeNG::GetParams()
151 {
152     CHECK_NULL_RETURN(pageRouterManager_, "");
153     return pageRouterManager_->GetParams();
154 }
155 
NavigatePage(uint8_t type,const PageTarget & target,const std::string & params)156 void FrontendDelegateDeclarativeNG::NavigatePage(uint8_t type, const PageTarget& target, const std::string& params)
157 {
158     CHECK_NULL_VOID(pageRouterManager_);
159 }
160 
GetPage(int32_t pageId) const161 RefPtr<JsAcePage> FrontendDelegateDeclarativeNG::GetPage(int32_t pageId) const
162 {
163     return nullptr;
164 }
165 
PostJsTask(std::function<void ()> && task)166 void FrontendDelegateDeclarativeNG::PostJsTask(std::function<void()>&& task)
167 {
168     taskExecutor_->PostTask(task, TaskExecutor::TaskType::JS);
169 }
170 
GetAppID() const171 const std::string& FrontendDelegateDeclarativeNG::GetAppID() const
172 {
173     return manifestParser_->GetAppInfo()->GetAppID();
174 }
175 
GetAppName() const176 const std::string& FrontendDelegateDeclarativeNG::GetAppName() const
177 {
178     return manifestParser_->GetAppInfo()->GetAppName();
179 }
180 
GetVersionName() const181 const std::string& FrontendDelegateDeclarativeNG::GetVersionName() const
182 {
183     return manifestParser_->GetAppInfo()->GetVersionName();
184 }
185 
GetVersionCode() const186 int32_t FrontendDelegateDeclarativeNG::GetVersionCode() const
187 {
188     return manifestParser_->GetAppInfo()->GetVersionCode();
189 }
190 
PostSyncTaskToPage(std::function<void ()> && task)191 void FrontendDelegateDeclarativeNG::PostSyncTaskToPage(std::function<void()>&& task)
192 {
193     pipelineContextHolder_.Get(); // Wait until Pipeline Context is attached.
194     taskExecutor_->PostSyncTask(task, TaskExecutor::TaskType::UI);
195 }
196 
GetAssetContent(const std::string & url,std::string & content)197 bool FrontendDelegateDeclarativeNG::GetAssetContent(const std::string& url, std::string& content)
198 {
199     return GetAssetContentImpl(assetManager_, url, content);
200 }
201 
GetAssetContent(const std::string & url,std::vector<uint8_t> & content)202 bool FrontendDelegateDeclarativeNG::GetAssetContent(const std::string& url, std::vector<uint8_t>& content)
203 {
204     return GetAssetContentImpl(assetManager_, url, content);
205 }
206 
GetAssetPath(const std::string & url)207 std::string FrontendDelegateDeclarativeNG::GetAssetPath(const std::string& url)
208 {
209     return GetAssetPathImpl(assetManager_, url);
210 }
211 
ChangeLocale(const std::string & language,const std::string & countryOrRegion)212 void FrontendDelegateDeclarativeNG::ChangeLocale(const std::string& language, const std::string& countryOrRegion)
213 {
214     LOGD("JSFrontend ChangeLocale");
215     taskExecutor_->PostTask(
216         [language, countryOrRegion]() { AceApplicationInfo::GetInstance().ChangeLocale(language, countryOrRegion); },
217         TaskExecutor::TaskType::PLATFORM);
218 }
219 
RegisterFont(const std::string & familyName,const std::string & familySrc)220 void FrontendDelegateDeclarativeNG::RegisterFont(const std::string& familyName, const std::string& familySrc)
221 {
222     pipelineContextHolder_.Get()->RegisterFont(familyName, familySrc);
223 }
224 
MeasureText(const MeasureContext & context)225 double FrontendDelegateDeclarativeNG::MeasureText(const MeasureContext& context)
226 {
227     return MeasureUtil::MeasureText(context);
228 }
229 
MeasureTextSize(const MeasureContext & context)230 Size FrontendDelegateDeclarativeNG::MeasureTextSize(const MeasureContext& context)
231 {
232     return MeasureUtil::MeasureTextSize(context);
233 }
234 
GetAnimationJsTask()235 SingleTaskExecutor FrontendDelegateDeclarativeNG::GetAnimationJsTask()
236 {
237     return SingleTaskExecutor::Make(taskExecutor_, TaskExecutor::TaskType::JS);
238 }
239 
GetUiTask()240 SingleTaskExecutor FrontendDelegateDeclarativeNG::GetUiTask()
241 {
242     return SingleTaskExecutor::Make(taskExecutor_, TaskExecutor::TaskType::UI);
243 }
244 
GetPipelineContext()245 RefPtr<PipelineBase> FrontendDelegateDeclarativeNG::GetPipelineContext()
246 {
247     return pipelineContextHolder_.Get();
248 }
249 
OnPageBackPress()250 bool FrontendDelegateDeclarativeNG::OnPageBackPress()
251 {
252     CHECK_NULL_RETURN(pageRouterManager_, false);
253     auto pageNode = pageRouterManager_->GetCurrentPageNode();
254     CHECK_NULL_RETURN(pageNode, false);
255     auto pagePattern = pageNode->GetPattern<NG::PagePattern>();
256     CHECK_NULL_RETURN(pagePattern, false);
257     if (pagePattern->OnBackPressed()) {
258         return true;
259     }
260     return pageRouterManager_->Pop();
261 }
262 
OnPageShow()263 void FrontendDelegateDeclarativeNG::OnPageShow()
264 {
265     CHECK_NULL_VOID(pageRouterManager_);
266     auto pageNode = pageRouterManager_->GetCurrentPageNode();
267     CHECK_NULL_VOID(pageNode);
268     auto pagePattern = pageNode->GetPattern<NG::PagePattern>();
269     CHECK_NULL_VOID(pagePattern);
270     pagePattern->OnShow();
271 }
272 
OnPageHide()273 void FrontendDelegateDeclarativeNG::OnPageHide()
274 {
275     CHECK_NULL_VOID(pageRouterManager_);
276     auto pageNode = pageRouterManager_->GetCurrentPageNode();
277     CHECK_NULL_VOID(pageNode);
278     auto pagePattern = pageNode->GetPattern<NG::PagePattern>();
279     CHECK_NULL_VOID(pagePattern);
280     pagePattern->OnHide();
281 }
282 
283 } // namespace OHOS::Ace::Framework
284