• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     taskExecutor_->PostTask(
112         [weak = AceType::WeakClaim(this), dataList] {
113             auto frontend = weak.Upgrade();
114             if (frontend) {
115                 frontend->UpdatePageData(dataList);
116             }
117         },
118         TaskExecutor::TaskType::UI, "ArkUIFormFrontendUpdatePageData", PriorityType::HIGH);
119 }
120 
UpdatePageData(const std::string & dataList)121 void FormFrontendDeclarative::UpdatePageData(const std::string& dataList)
122 {
123     CHECK_RUN_ON(UI); // eTSCard UI == Main JS/UI/PLATFORM
124     auto delegate = GetDelegate();
125     if (!delegate) {
126         return;
127     }
128     delegate->UpdatePageData(dataList);
129 }
130 
SetColorMode(ColorMode colorMode)131 void FormFrontendDeclarative::SetColorMode(ColorMode colorMode) {}
132 
OnSurfaceChanged(int32_t width,int32_t height)133 void FormFrontendDeclarative::OnSurfaceChanged(int32_t width, int32_t height)
134 {
135     TAG_LOGI(AceLogTag::ACE_FORM, "FormFrontendDeclarative OnSurfaceChanged entry");
136     auto jsEngine = GetJsEngine();
137     auto delegate = GetDelegate();
138     if (!jsEngine || !delegate) {
139         TAG_LOGE(AceLogTag::ACE_FORM, "FormFrontendDeclarative OnSurfaceChanged fail");
140         return;
141     }
142     auto mediaQuery = delegate->GetMediaQueryInfoInstance();
143     if (!mediaQuery) {
144         TAG_LOGE(AceLogTag::ACE_FORM, "FormFrontendDeclarative OnSurfaceChanged, mediaQuery is null");
145         return;
146     }
147     mediaQuery->EnsureListenerIdValid();
148     const auto& listenerId = mediaQuery->GetListenerId();
149     auto json = JsonUtil::Create(true);
150     jsEngine->MediaQueryCallback(listenerId, json->ToString());
151 }
152 
HandleSurfaceChanged(int32_t width,int32_t height)153 void FormFrontendDeclarative::HandleSurfaceChanged(int32_t width, int32_t height)
154 {
155     CHECK_RUN_ON(JS);
156     OnMediaFeatureUpdate();
157 }
158 
OnMediaFeatureUpdate()159 void FormFrontendDeclarative::OnMediaFeatureUpdate()
160 {
161     CHECK_RUN_ON(JS);
162 }
163 
SetErrorEventHandler(std::function<void (const std::string &,const std::string &)> && errorCallback)164 void FormFrontendDeclarative::SetErrorEventHandler(
165     std::function<void(const std::string&, const std::string&)>&& errorCallback)
166 {
167     auto jsEngine = GetJsEngine();
168     if (!jsEngine) {
169         return;
170     }
171 
172     return jsEngine->SetErrorEventHandler(std::move(errorCallback));
173 }
174 } // namespace OHOS::Ace
175