1 /*
2 * Copyright (c) 2023 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/card_frontend/form_frontend_declarative.h"
17
18 #include "base/log/event_report.h"
19 #include "base/utils/utils.h"
20 #include "core/common/thread_checker.h"
21 #include "frameworks/bridge/common/utils/utils.h"
22 #include "frameworks/core/pipeline_ng/pipeline_context.h"
23
24 namespace OHOS::Ace {
25 namespace {
26 const char FILE_TYPE_BIN[] = ".abc";
27 } // namespace
28
GetFormSrcPath(const std::string & uri,const std::string & suffix) const29 std::string FormFrontendDeclarative::GetFormSrcPath(const std::string& uri, const std::string& suffix) const
30 {
31 // the case uri is starts with "/" and "/" is the mainPage
32 if (uri.size() != 0) {
33 auto result = uri;
34 if (result.compare(result.size()-4, 4, ".ets") == 0) { // 4: length of '.ets'
35 result = result.substr(0, result.size()-4); // 4: length of '.ets'
36 }
37 if (result.compare(0, 1, "/") == 0) { // 1: length of '/'
38 return result.substr(1) + ".abc"; // 1: length of '/'
39 }
40 if (result.compare(0, 2, "./") == 0) { // 2: length of './'
41 return result.substr(2) + ".abc"; // 2: length of './'
42 }
43 }
44
45 LOGE("can't find this page %{private}s path", uri.c_str());
46 return "";
47 }
48
RunPage(int32_t pageId,const std::string & url,const std::string & params)49 void FormFrontendDeclarative::RunPage(int32_t pageId, const std::string& url, const std::string& params)
50 {
51 LOGI("FormFrontendDeclarative::RunPage url = %{public}s", url.c_str());
52 std::string urlPath = GetFormSrcPath(url, FILE_TYPE_BIN);
53 if (urlPath.empty()) {
54 LOGE("fail to eTS Card run page due to path url is empty");
55 EventReport::SendFormException(FormExcepType::RUN_PAGE_ERR);
56 return;
57 }
58 LOGI("FormFrontendDeclarative::RunPage urlPath = %{public}s", urlPath.c_str());
59 if (delegate_) {
60 auto container = Container::Current();
61 if (!container) {
62 LOGE("RunPage host container null");
63 EventReport::SendFormException(FormExcepType::RUN_PAGE_ERR);
64 return;
65 }
66 container->SetCardFrontend(AceType::WeakClaim(this), cardId_);
67 auto delegate = AceType::DynamicCast<Framework::FormFrontendDelegateDeclarative>(delegate_);
68 if (delegate) {
69 delegate->RunCard(urlPath, params, "", cardId_);
70 } else {
71 LOGE("FormFrontendDeclarative::RunPage delegate nullptr");
72 }
73 }
74 }
75
ClearEngineCache()76 void FormFrontendDeclarative::ClearEngineCache()
77 {
78 auto jsEngine = GetJsEngine();
79 if (!jsEngine) {
80 return;
81 }
82 jsEngine->ClearCache();
83 }
84
UpdateData(const std::string & dataList)85 void FormFrontendDeclarative::UpdateData(const std::string& dataList)
86 {
87 LOGI("FormFrontendDeclarative::UpdateData dataList = %{public}s", dataList.c_str());
88 CHECK_NULL_VOID(taskExecutor_);
89 taskExecutor_->PostTask(
90 [weak = AceType::WeakClaim(this), dataList] {
91 auto frontend = weak.Upgrade();
92 if (frontend) {
93 frontend->UpdatePageData(dataList);
94 }
95 },
96 TaskExecutor::TaskType::UI); // eTSCard UI == Main JS/UI/PLATFORM
97 }
98
UpdatePageData(const std::string & dataList)99 void FormFrontendDeclarative::UpdatePageData(const std::string& dataList)
100 {
101 CHECK_RUN_ON(UI); // eTSCard UI == Main JS/UI/PLATFORM
102 auto delegate = GetDelegate();
103 if (!delegate) {
104 LOGE("the delegate is null");
105 EventReport::SendFormException(FormExcepType::UPDATE_PAGE_ERR);
106 return;
107 }
108 delegate->UpdatePageData(dataList);
109 }
110
SetColorMode(ColorMode colorMode)111 void FormFrontendDeclarative::SetColorMode(ColorMode colorMode) {}
OnSurfaceChanged(int32_t width,int32_t height)112 void FormFrontendDeclarative::OnSurfaceChanged(int32_t width, int32_t height) {}
113
HandleSurfaceChanged(int32_t width,int32_t height)114 void FormFrontendDeclarative::HandleSurfaceChanged(int32_t width, int32_t height)
115 {
116 CHECK_RUN_ON(JS);
117 OnMediaFeatureUpdate();
118 }
119
OnMediaFeatureUpdate()120 void FormFrontendDeclarative::OnMediaFeatureUpdate()
121 {
122 CHECK_RUN_ON(JS);
123 }
124
SetErrorEventHandler(std::function<void (const std::string &,const std::string &)> && errorCallback)125 void FormFrontendDeclarative::SetErrorEventHandler(
126 std::function<void(const std::string&, const std::string&)>&& errorCallback)
127 {
128 auto jsEngine = GetJsEngine();
129 if (!jsEngine) {
130 return;
131 }
132
133 return jsEngine->SetErrorEventHandler(std::move(errorCallback));
134 }
135 } // namespace OHOS::Ace
136