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 return "";
46 }
47
RunPage(const std::string & url,const std::string & params)48 UIContentErrorCode FormFrontendDeclarative::RunPage(const std::string& url, const std::string& params)
49 {
50 return RunDynamicPage(url, params, "");
51 }
52
RunDynamicPage(const std::string & url,const std::string & params,const std::string & entryPoint)53 UIContentErrorCode FormFrontendDeclarative::RunDynamicPage(
54 const std::string& url, const std::string& params, const std::string& entryPoint)
55 {
56 TAG_LOGI(AceLogTag::ACE_FORM, "FormFrontendDeclarative run page url = %{public}s, entryPoint = %{public}s",
57 url.c_str(), entryPoint.c_str());
58 std::string urlPath = GetFormSrcPath(url, FILE_TYPE_BIN);
59 if (urlPath.empty()) {
60 return UIContentErrorCode::NULL_URL;
61 }
62 if (delegate_) {
63 auto container = Container::Current();
64 if (!container) {
65 return UIContentErrorCode::NULL_POINTER;
66 }
67 container->SetCardFrontend(AceType::WeakClaim(this), cardId_);
68 auto delegate = AceType::DynamicCast<Framework::FormFrontendDelegateDeclarative>(delegate_);
69 if (delegate) {
70 return delegate->RunCard(urlPath, params, "", cardId_, entryPoint);
71 }
72 }
73
74 return UIContentErrorCode::NULL_POINTER;
75 }
76
UpdateData(const std::string & dataList)77 void FormFrontendDeclarative::UpdateData(const std::string& dataList)
78 {
79 CHECK_NULL_VOID(taskExecutor_);
80 // eTSCard UI == Main JS/UI/PLATFORM
81 taskExecutor_->PostTask(
82 [weak = AceType::WeakClaim(this), dataList] {
83 auto frontend = weak.Upgrade();
84 if (frontend) {
85 frontend->UpdatePageData(dataList);
86 }
87 },
88 TaskExecutor::TaskType::UI, "ArkUIFormFrontendUpdatePageData", PriorityType::HIGH);
89 }
90
UpdatePageData(const std::string & dataList)91 void FormFrontendDeclarative::UpdatePageData(const std::string& dataList)
92 {
93 CHECK_RUN_ON(UI); // eTSCard UI == Main JS/UI/PLATFORM
94 auto delegate = GetDelegate();
95 if (!delegate) {
96 return;
97 }
98 delegate->UpdatePageData(dataList);
99 }
100
SetColorMode(ColorMode colorMode)101 void FormFrontendDeclarative::SetColorMode(ColorMode colorMode) {}
102
OnSurfaceChanged(int32_t width,int32_t height)103 void FormFrontendDeclarative::OnSurfaceChanged(int32_t width, int32_t height)
104 {
105 TAG_LOGI(AceLogTag::ACE_FORM, "FormFrontendDeclarative OnSurfaceChanged entry");
106 auto jsEngine = GetJsEngine();
107 auto delegate = GetDelegate();
108 if (!jsEngine || !delegate) {
109 TAG_LOGE(AceLogTag::ACE_FORM, "FormFrontendDeclarative OnSurfaceChanged fail");
110 return;
111 }
112 auto mediaQuery = delegate->GetMediaQueryInfoInstance();
113 if (!mediaQuery) {
114 TAG_LOGE(AceLogTag::ACE_FORM, "FormFrontendDeclarative OnSurfaceChanged, mediaQuery is null");
115 return;
116 }
117 mediaQuery->EnsureListenerIdValid();
118 const auto& listenerId = mediaQuery->GetListenerId();
119 auto json = JsonUtil::Create(true);
120 jsEngine->MediaQueryCallback(listenerId, json->ToString());
121 }
122
HandleSurfaceChanged(int32_t width,int32_t height)123 void FormFrontendDeclarative::HandleSurfaceChanged(int32_t width, int32_t height)
124 {
125 CHECK_RUN_ON(JS);
126 OnMediaFeatureUpdate();
127 }
128
OnMediaFeatureUpdate()129 void FormFrontendDeclarative::OnMediaFeatureUpdate()
130 {
131 CHECK_RUN_ON(JS);
132 }
133
SetErrorEventHandler(std::function<void (const std::string &,const std::string &)> && errorCallback)134 void FormFrontendDeclarative::SetErrorEventHandler(
135 std::function<void(const std::string&, const std::string&)>&& errorCallback)
136 {
137 auto jsEngine = GetJsEngine();
138 if (!jsEngine) {
139 return;
140 }
141
142 return jsEngine->SetErrorEventHandler(std::move(errorCallback));
143 }
144 } // namespace OHOS::Ace
145