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_delegate_declarative.h"
17 
18 #include "base/log/event_report.h"
19 #include "base/utils/utils.h"
20 #include "base/utils/measure_util.h"
21 #include "core/common/thread_checker.h"
22 
23 #ifndef PREVIEW
24 #include "form_mgr.h"
25 #endif
26 
27 namespace OHOS::Ace::Framework {
~FormFrontendDelegateDeclarative()28 FormFrontendDelegateDeclarative::~FormFrontendDelegateDeclarative()
29 {
30     CHECK_RUN_ON(JS);
31     LOG_DESTROY();
32 }
33 
RunCard(const std::string & url,const std::string & params,const std::string & profile,int64_t cardId,const std::string & entryPoint)34 UIContentErrorCode FormFrontendDelegateDeclarative::RunCard(const std::string& url, const std::string& params,
35     const std::string& profile, int64_t cardId, const std::string& entryPoint)
36 {
37     ACE_SCOPED_TRACE("FormFrontendDelegateDeclarative::RunCard");
38     auto pageRouterManager = GetPageRouterManager();
39     CHECK_NULL_RETURN(pageRouterManager, UIContentErrorCode::NULL_PAGE_ROUTER);
40     pageRouterManager->SetManifestParser(GetManifestParser());
41     pageRouterManager->SetIsCard();
42     auto cardPipeline = GetPipelineContext();
43     auto taskExecutor = GetTaskExecutor();
44     CHECK_NULL_RETURN(taskExecutor, UIContentErrorCode::NULL_POINTER);
45     cardData_ = params;
46     auto container = Container::Current();
47     CHECK_NULL_RETURN(container, UIContentErrorCode::NULL_POINTER);
48     auto weakCardPipeline = WeakPtr<PipelineBase>(cardPipeline);
49     container->SetCardPipeline(weakCardPipeline, cardId);
50 #ifndef PREVIEW
51     return pageRouterManager->RunCard(url, params, cardId, entryPoint);
52 #else
53     taskExecutor->PostTask(
54         [weak = WeakClaim<NG::PageRouterManager>(RawPtr(pageRouterManager)), url, params, cardId, entryPoint]() {
55             auto pageRouterManager = weak.Upgrade();
56             CHECK_NULL_VOID(pageRouterManager);
57             pageRouterManager->RunCard(url, params, cardId, entryPoint);
58         },
59         TaskExecutor::TaskType::JS, "ArkUIFormFrontendRunCard");
60     return UIContentErrorCode::NO_ERRORS;
61 #endif
62 }
63 
FireCardEvent(const EventMarker & eventMarker,const std::string & params)64 void FormFrontendDelegateDeclarative::FireCardEvent(const EventMarker& eventMarker, const std::string& params) {}
65 
FireCardAction(const std::string & action)66 void FormFrontendDelegateDeclarative::FireCardAction(const std::string& action)
67 {
68     auto context = GetPipelineContext();
69     CHECK_NULL_VOID(context);
70     auto taskExecutor = GetTaskExecutor();
71     CHECK_NULL_VOID(taskExecutor);
72     taskExecutor->PostTask(
73         [weakCardPipeline = WeakPtr<PipelineBase>(context), action]() {
74             auto context = weakCardPipeline.Upgrade();
75             if (context) {
76                 context->OnActionEvent(action);
77             }
78         },
79         TaskExecutor::TaskType::UI, "ArkUIFormFrontendFireAction"); // eTSCard UI == Main JS/UI/PLATFORM
80 }
81 
RegisterFont(const std::string & familyName,const std::string & familySrc,const std::string & bundleName,const std::string & moduleName)82 void FormFrontendDelegateDeclarative::RegisterFont(const std::string& familyName, const std::string& familySrc,
83     const std::string& bundleName, const std::string& moduleName)
84 {
85 #ifndef PREVIEW
86     auto container = Container::Current();
87     CHECK_NULL_VOID(container);
88     std::string formBundleName = container->GetBundleName();
89     if (!OHOS::AppExecFwk::FormMgr::GetInstance().IsSystemAppForm(formBundleName)) {
90         TAG_LOGE(AceLogTag::ACE_FORM, "%{public}s is not system form", formBundleName.c_str());
91         return;
92     }
93 
94     FrontendDelegateDeclarative::RegisterFont(familyName, familySrc, bundleName, moduleName);
95 #endif
96 }
97 } // namespace OHOS::Ace::Framework
98