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 #ifndef FOUNDATION_ACE_FRAMEWORKS_BRIDGE_CARD_FRONTEND_FORM_FRONTEND_DECLARATIVE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_CARD_FRONTEND_FORM_FRONTEND_DECLARATIVE_H
18 
19 #include <memory>
20 
21 #include "base/memory/ace_type.h"
22 #include "core/common/ace_page.h"
23 #include "core/common/frontend.h"
24 #include "frameworks/bridge/card_frontend/form_frontend_delegate_declarative.h"
25 #include "frameworks/bridge/card_frontend/js_card_parser.h"
26 #include "frameworks/bridge/common/accessibility/accessibility_node_manager.h"
27 #include "frameworks/bridge/common/manifest/manifest_parser.h"
28 #include "frameworks/bridge/common/utils/page_id_pool.h"
29 #include "frameworks/bridge/declarative_frontend/declarative_frontend.h"
30 #include "frameworks/bridge/declarative_frontend/ng/page_router_manager.h"
31 #include "frameworks/bridge/declarative_frontend/ng/declarative_frontend_ng.h"
32 #include "frameworks/bridge/js_frontend/frontend_delegate_impl.h"
33 
34 namespace OHOS::Ace {
35 class ACE_EXPORT FormFrontendDeclarative : public DeclarativeFrontend {
36     DECLARE_ACE_TYPE(FormFrontendDeclarative, DeclarativeFrontend);
37 public:
38     FormFrontendDeclarative() = default;
39 
40     // Card
41     void UpdateData(const std::string& dataList);
42     void HandleSurfaceChanged(int32_t width, int32_t height);
43     void UpdatePageData(const std::string& dataList);
44     void OnMediaFeatureUpdate();
45 
46     UIContentErrorCode RunPage(const std::string& url, const std::string& params) override;
47     UIContentErrorCode RunDynamicPage(
48         const std::string& url, const std::string& params, const std::string& entryPoint) override;
49 
50     void SetErrorEventHandler(
51         std::function<void(const std::string&, const std::string&)>&& errorCallback) override;
52 
53     void OnSurfaceChanged(int32_t width, int32_t height) override;
54     void SetColorMode(ColorMode colorMode) override;
55 
SetLoadCardCallBack(const WeakPtr<PipelineBase> & outSidePipelineContext)56     void SetLoadCardCallBack(const WeakPtr<PipelineBase>& outSidePipelineContext)
57     {
58         const auto& loadCallback = [outSidePipelineContext](
59                                        const std::string& url, int64_t cardId, const std::string& entryPoint) -> bool {
60             auto context = outSidePipelineContext.Upgrade();
61             if (!context) {
62                 return false;
63             }
64 
65             auto outSidefrontend = AceType::DynamicCast<DeclarativeFrontend>(context->GetFrontend());
66             if (!outSidefrontend) {
67                 return false;
68             }
69 
70             // Use the same js engine with host pipeline
71             auto jsEngine = outSidefrontend->GetJsEngine();
72             if (!jsEngine) {
73                 return false;
74             }
75             return jsEngine->LoadCard(url, cardId, entryPoint);
76         };
77 
78         auto delegate = AceType::DynamicCast<Framework::FormFrontendDelegateDeclarative>(delegate_);
79         if (delegate) {
80             delegate->SetLoadCardCallBack(loadCallback);
81         }
82     }
83 
GetDelegate()84     RefPtr<Framework::FormFrontendDelegateDeclarative> GetDelegate()
85     {
86         return AceType::DynamicCast<Framework::FormFrontendDelegateDeclarative>(delegate_);
87     }
88 
89     std::string GetFormSrcPath(const std::string& uri, const std::string& suffix) const;
90 
SetFormSrc(std::string formSrc)91     void SetFormSrc(std::string formSrc)
92     {
93         formSrc_ = formSrc;
94     }
95 
GetFormSrc()96     std::string GetFormSrc() const
97     {
98         return formSrc_;
99     }
100 
SetRunningCardId(int64_t cardId)101     void SetRunningCardId(int64_t cardId)
102     {
103         cardId_ = cardId;
104     }
105 
SetIsFormRender(bool isCardfront)106     void SetIsFormRender(bool isCardfront)
107     {
108         isFormRender_ = isCardfront;
109     }
110 
IsFormRender()111     bool IsFormRender()
112     {
113         return isFormRender_;
114     }
115 
SetTaskExecutor(RefPtr<TaskExecutor> taskExecutor)116     void SetTaskExecutor(RefPtr<TaskExecutor> taskExecutor)
117     {
118         taskExecutor_ = taskExecutor;
119     }
120 
SetModuleName(const std::string & moduleName)121     void SetModuleName(const std::string& moduleName)
122     {
123         moduleName_ = moduleName;
124     }
SetBundleName(const std::string & bundleName)125     void SetBundleName(const std::string& bundleName)
126     {
127         bundleName_ = bundleName;
128     }
GetModuleName()129     std::string GetModuleName() const
130     {
131         return moduleName_;
132     }
GetBundleName()133     std::string GetBundleName() const
134     {
135         return bundleName_;
136     }
SetIsBundle(bool isBundle)137     void SetIsBundle(bool isBundle)
138     {
139         isBundle_ = isBundle;
140     }
IsBundle()141     bool IsBundle() const
142     {
143         return isBundle_;
144     }
145 
146     ColorMode colorMode_ = ColorMode::LIGHT;
147     bool foregroundFrontend_ = false;
148     double density_ = 1.0;
149     std::string cardHapPath_;
150 
151     Framework::PipelineContextHolder holder_;
152     RefPtr<AssetManager> assetManager_;
153 
154     mutable std::once_flag onceFlag_;
155     RefPtr<TaskExecutor> taskExecutor_;
156     RefPtr<AceEventHandler> eventHandler_;
157     std::string formSrc_;
158     WindowConfig cardWindowConfig_;
159     uint64_t cardId_ = 0; // cardId != formId, cardId is the nodeId of component.
160 
161     std::string bundleName_;
162     std::string moduleName_;
163     bool isBundle_ = false;
164 };
165 } // namespace OHOS::Ace
166 
167 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_CARD_FRONTEND_FORM_FRONTEND_DECLARATIVE_H
168