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 namespace OHOS::Ace {
19 namespace {
20 const char FILE_TYPE_BIN[] = ".abc";
21 } // namespace
22
GetFormSrcPath(const std::string & uri,const std::string & suffix) const23 std::string FormFrontendDeclarative::GetFormSrcPath(const std::string& uri, const std::string& suffix) const
24 {
25 // the case uri is starts with "/" and "/" is the mainPage
26 if (uri.size() != 0) {
27 auto result = uri;
28 if (result.compare(result.size()-4, 4, ".ets") == 0) { // 4: length of '.ets'
29 result = result.substr(0, result.size()-4); // 4: length of '.ets'
30 }
31 if (result.compare(0, 1, "/") == 0) { // 1: length of '/'
32 return result.substr(1) + ".abc"; // 1: length of '/'
33 }
34 if (result.compare(0, 2, "./") == 0) { // 2: length of './'
35 return result.substr(2) + ".abc"; // 2: length of './'
36 }
37 }
38
39 return "";
40 }
41
RunPage(const std::string & url,const std::string & params)42 UIContentErrorCode FormFrontendDeclarative::RunPage(const std::string& url, const std::string& params)
43 {
44 return RunDynamicPage(url, params, "");
45 }
46
RunDynamicPage(const std::string & url,const std::string & params,const std::string & entryPoint)47 UIContentErrorCode FormFrontendDeclarative::RunDynamicPage(
48 const std::string& url, const std::string& params, const std::string& entryPoint)
49 {
50 TAG_LOGI(AceLogTag::ACE_FORM, "FormFrontendDeclarative run page url = %{public}s, entryPoint = %{public}s",
51 url.c_str(), entryPoint.c_str());
52 auto container = Container::Current();
53 if (!container) {
54 return UIContentErrorCode::NULL_POINTER;
55 }
56
57 auto uiContentType = container->GetUIContentType();
58 if (uiContentType == UIContentType::DYNAMIC_COMPONENT) {
59 return InnerRunDynamicPage(url, params, entryPoint);
60 }
61
62 return InnerRunCardPage(url, params, entryPoint);
63 }
64
InnerRunCardPage(const std::string & url,const std::string & params,const std::string & entryPoint)65 UIContentErrorCode FormFrontendDeclarative::InnerRunCardPage(
66 const std::string& url, const std::string& params, const std::string& entryPoint)
67 {
68 TAG_LOGI(AceLogTag::ACE_FORM, "InnerRunCardPage");
69 std::string urlPath = GetFormSrcPath(url, FILE_TYPE_BIN);
70 if (urlPath.empty()) {
71 return UIContentErrorCode::NULL_URL;
72 }
73 if (delegate_) {
74 auto container = Container::Current();
75 if (!container) {
76 return UIContentErrorCode::NULL_POINTER;
77 }
78 container->SetCardFrontend(AceType::WeakClaim(this), cardId_);
79 auto delegate = AceType::DynamicCast<Framework::FormFrontendDelegateDeclarative>(delegate_);
80 if (delegate) {
81 return delegate->RunCard(urlPath, params, "", cardId_, entryPoint);
82 }
83 }
84
85 return UIContentErrorCode::NULL_POINTER;
86 }
87
InnerRunDynamicPage(const std::string & url,const std::string & params,const std::string & entryPoint)88 UIContentErrorCode FormFrontendDeclarative::InnerRunDynamicPage(
89 const std::string& url, const std::string& params, const std::string& entryPoint)
90 {
91 TAG_LOGI(AceLogTag::ACE_FORM, "InnerRunDynamicPage");
92 auto container = Container::Current();
93 CHECK_NULL_RETURN(container, UIContentErrorCode::NULL_POINTER);
94 container->SetCardFrontend(AceType::WeakClaim(this), cardId_);
95 CHECK_NULL_RETURN(delegate_, UIContentErrorCode::NULL_POINTER);
96 auto delegate = AceType::DynamicCast<Framework::FormFrontendDelegateDeclarative>(delegate_);
97 CHECK_NULL_RETURN(delegate, UIContentErrorCode::NULL_POINTER);
98 auto errorCode = delegate->RunCard(url, params, "", cardId_, entryPoint);
99 if (errorCode == NO_ERRORS) {
100 auto jsAccessibility = delegate_->GetJSAccessibilityManager();
101 CHECK_NULL_RETURN(jsAccessibility, errorCode);
102 jsAccessibility->InitializeCallback();
103 }
104 return errorCode;
105 }
106
UpdateData(const std::string & dataList)107 void FormFrontendDeclarative::UpdateData(const std::string& dataList)
108 {
109 CHECK_NULL_VOID(taskExecutor_);
110 // eTSCard UI == Main JS/UI/PLATFORM
111 TAG_LOGI(AceLogTag::ACE_FORM, "UpdateData, dataList length:%{public}zu", dataList.length());
112 taskExecutor_->PostTask(
113 [weak = AceType::WeakClaim(this), dataList] {
114 auto frontend = weak.Upgrade();
115 if (frontend) {
116 frontend->UpdatePageData(dataList);
117 }
118 },
119 TaskExecutor::TaskType::UI, "ArkUIFormFrontendUpdatePageData", PriorityType::HIGH);
120 }
121
UpdatePageData(const std::string & dataList)122 void FormFrontendDeclarative::UpdatePageData(const std::string& dataList)
123 {
124 CHECK_RUN_ON(UI); // eTSCard UI == Main JS/UI/PLATFORM
125 TAG_LOGI(AceLogTag::ACE_FORM, "UpdatePageData, dataList length:%{public}zu", dataList.length());
126 auto delegate = GetDelegate();
127 if (!delegate) {
128 TAG_LOGE(AceLogTag::ACE_FORM, "UpdatePageData delegate is null");
129 return;
130 }
131 delegate->UpdatePageData(dataList);
132 }
133
SetColorMode(ColorMode colorMode)134 void FormFrontendDeclarative::SetColorMode(ColorMode colorMode) {}
135
OnSurfaceChanged(int32_t width,int32_t height)136 void FormFrontendDeclarative::OnSurfaceChanged(int32_t width, int32_t height)
137 {
138 TAG_LOGI(AceLogTag::ACE_FORM, "FormFrontendDeclarative OnSurfaceChanged entry");
139 auto jsEngine = GetJsEngine();
140 auto delegate = GetDelegate();
141 if (!jsEngine || !delegate) {
142 TAG_LOGE(AceLogTag::ACE_FORM, "FormFrontendDeclarative OnSurfaceChanged fail");
143 return;
144 }
145 auto mediaQuery = delegate->GetMediaQueryInfoInstance();
146 if (!mediaQuery) {
147 TAG_LOGE(AceLogTag::ACE_FORM, "FormFrontendDeclarative OnSurfaceChanged, mediaQuery is null");
148 return;
149 }
150 mediaQuery->EnsureListenerIdValid();
151 const auto& listenerId = mediaQuery->GetListenerId();
152 auto json = JsonUtil::Create(true);
153 jsEngine->MediaQueryCallback(listenerId, json->ToString());
154 }
155
HandleSurfaceChanged(int32_t width,int32_t height)156 void FormFrontendDeclarative::HandleSurfaceChanged(int32_t width, int32_t height)
157 {
158 CHECK_RUN_ON(JS);
159 OnMediaFeatureUpdate();
160 }
161
OnMediaFeatureUpdate()162 void FormFrontendDeclarative::OnMediaFeatureUpdate()
163 {
164 CHECK_RUN_ON(JS);
165 }
166
SetErrorEventHandler(std::function<void (const std::string &,const std::string &)> && errorCallback)167 void FormFrontendDeclarative::SetErrorEventHandler(
168 std::function<void(const std::string&, const std::string&)>&& errorCallback)
169 {
170 auto jsEngine = GetJsEngine();
171 if (!jsEngine) {
172 return;
173 }
174
175 return jsEngine->SetErrorEventHandler(std::move(errorCallback));
176 }
177 } // namespace OHOS::Ace
178