1 /*
2 * Copyright (c) 2025 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 "adapter/ohos/entrance/ace_container.h"
17
18 #include <ani.h>
19
20 #include "adapter/ohos/entrance/ace_view_ohos.h"
21 #include "adapter/ohos/entrance/data_ability_helper_standard.h"
22 #include "adapter/ohos/entrance/file_asset_provider_impl.h"
23 #include "adapter/ohos/entrance/hap_asset_provider_impl.h"
24 #include "adapter/ohos/entrance/mmi_event_convertor.h"
25 #include "adapter/ohos/entrance/ui_content_impl.h"
26 #include "adapter/ohos/entrance/utils.h"
27 #include "adapter/ohos/osal/page_viewport_config_ohos.h"
28 #include "adapter/ohos/osal/resource_adapter_impl_v2.h"
29 #include "adapter/ohos/osal/system_bar_style_ohos.h"
30 #include "adapter/ohos/osal/view_data_wrap_ohos.h"
31 #include "adapter/ohos/osal/window_utils.h"
32 #include "base/i18n/localization.h"
33 #include "base/log/jank_frame_report.h"
34 #include "base/thread/background_task_executor.h"
35 #include "bridge/card_frontend/card_frontend.h"
36 #include "bridge/card_frontend/form_frontend_declarative.h"
37 #include "bridge/common/utils/engine_helper.h"
38 #include "bridge/declarative_frontend/engine/jsi/jsi_declarative_engine.h"
39 #include "bridge/js_frontend/js_frontend.h"
40 #include "bridge/arkts_frontend/arkts_frontend.h"
41 #include "core/common/ace_application_info.h"
42 #include "core/common/ace_engine.h"
43 #include "core/common/plugin_manager.h"
44 #include "core/common/resource/resource_manager.h"
45 #include "core/common/task_executor_impl.h"
46 #include "core/components_ng/base/inspector.h"
47 #include "core/components_ng/render/adapter/form_render_window.h"
48 #include "core/components_ng/render/adapter/rosen_render_context.h"
49 #include "core/components_ng/render/adapter/rosen_window.h"
50 #include "core/components_ng/token_theme/token_theme_storage.h"
51
52 namespace OHOS::Ace::Platform {
53 namespace {
54 constexpr char DECLARATIVE_ARK_ENGINE_SHARED_LIB[] = "libace_engine_declarative_ark.z.so";
GetDeclarativeSharedLibrary()55 const char* GetDeclarativeSharedLibrary()
56 {
57 return DECLARATIVE_ARK_ENGINE_SHARED_LIB;
58 }
59 } // namespace
60
InitializeStaticHybridDynamic(std::shared_ptr<OHOS::AppExecFwk::Ability> aceAbility)61 void AceContainer::InitializeStaticHybridDynamic(std::shared_ptr<OHOS::AppExecFwk::Ability> aceAbility)
62 {
63 // 1.2 initialization
64 frontend_ = MakeRefPtr<ArktsFrontend>(sharedRuntime_);
65 // 1.1 initialization
66 #ifdef NG_BUILD
67 subFrontend_ = AceType::MakeRefPtr<DeclarativeFrontendNG>();
68 auto declarativeFrontend = AceType::DynamicCast<DeclarativeFrontendNG>(subFrontend_);
69 #else
70 subFrontend_ = AceType::MakeRefPtr<DeclarativeFrontend>();
71 auto declarativeFrontend = AceType::DynamicCast<DeclarativeFrontend>(subFrontend_);
72 #endif
73 auto& loader = Framework::JsEngineLoader::GetDeclarative(GetDeclarativeSharedLibrary());
74 RefPtr<Framework::JsEngine> jsEngine;
75 if (GetSettings().usingSharedRuntime) {
76 jsEngine = loader.CreateJsEngineUsingSharedRuntime(
77 instanceId_, nullptr); // replace 1.1 preload runtime when initialize
78 } else {
79 jsEngine = loader.CreateJsEngine(instanceId_);
80 }
81
82 jsEngine->AddExtraNativeObject("ability", aceAbility.get());
83 auto pageUrlCheckFunc = [id = instanceId_](
84 const std::string& url, const std::function<void()>& callback,
85 const std::function<void(int32_t, const std::string&)>& silentInstallErrorCallBack) {
86 ContainerScope scope(id);
87 auto container = Container::Current();
88 CHECK_NULL_VOID(container);
89 auto pageUrlChecker = container->GetPageUrlChecker();
90 CHECK_NULL_VOID(pageUrlChecker);
91 pageUrlChecker->LoadPageUrl(url, callback, silentInstallErrorCallBack);
92 };
93 jsEngine->SetPageUrlCheckFunc(std::move(pageUrlCheckFunc));
94
95 EngineHelper::AddEngine(instanceId_, jsEngine);
96 declarativeFrontend->SetJsEngine(jsEngine);
97 declarativeFrontend->SetPageProfile(pageProfile_);
98 declarativeFrontend->SetNeedDebugBreakPoint(AceApplicationInfo::GetInstance().IsNeedDebugBreakPoint());
99 declarativeFrontend->SetDebugVersion(AceApplicationInfo::GetInstance().IsDebugVersion());
100 }
101
InitializeDynamicHybridStatic(std::shared_ptr<OHOS::AppExecFwk::Ability> aceAbility)102 void AceContainer::InitializeDynamicHybridStatic(std::shared_ptr<OHOS::AppExecFwk::Ability> aceAbility)
103 {
104 // 1.2 initialization
105 if (!ArktsFrontend::preloadArkTSRuntime) {
106 LOGE("AceContainer::InitializeDynamicHybridStatic: ArktsFrontend::preloadArkTSRuntime is null, preload Fail");
107 }
108 subFrontend_ = MakeRefPtr<ArktsFrontend>(ArktsFrontend::preloadArkTSRuntime);
109 // 1.1 initialization
110 if (isFormRender_) {
111 #ifdef FORM_SUPPORTED
112 LOGI("Init Form Frontend");
113 frontend_ = AceType::MakeRefPtr<FormFrontendDeclarative>();
114 auto cardFrontend = AceType::DynamicCast<FormFrontendDeclarative>(frontend_);
115 auto& loader = Framework::JsEngineLoader::GetDeclarative(GetDeclarativeSharedLibrary());
116 RefPtr<Framework::JsEngine> jsEngine;
117 if (GetSettings().usingSharedRuntime) {
118 jsEngine = loader.CreateJsEngineUsingSharedRuntime(instanceId_, sharedRuntime_);
119 } else {
120 jsEngine = loader.CreateJsEngine(instanceId_);
121 }
122 jsEngine->AddExtraNativeObject("ability", aceAbility.get());
123
124 EngineHelper::AddEngine(instanceId_, jsEngine);
125 cardFrontend->SetJsEngine(jsEngine);
126 cardFrontend->SetPageProfile(pageProfile_);
127 cardFrontend->SetNeedDebugBreakPoint(AceApplicationInfo::GetInstance().IsNeedDebugBreakPoint());
128 cardFrontend->SetDebugVersion(AceApplicationInfo::GetInstance().IsDebugVersion());
129 // Card front
130 cardFrontend->SetRunningCardId(0); // ArkTsCard : nodeId, Host->FMS->FRS->innersdk
131 cardFrontend->SetIsFormRender(true);
132 #endif
133 } else if (!isSubContainer_) {
134 #ifdef NG_BUILD
135 frontend_ = AceType::MakeRefPtr<DeclarativeFrontendNG>();
136 auto declarativeFrontend = AceType::DynamicCast<DeclarativeFrontendNG>(frontend_);
137 #else
138 frontend_ = AceType::MakeRefPtr<DeclarativeFrontend>();
139 auto declarativeFrontend = AceType::DynamicCast<DeclarativeFrontend>(frontend_);
140 #endif
141
142 if (!IsDialogContainer()) {
143 auto& loader = Framework::JsEngineLoader::GetDeclarative(GetDeclarativeSharedLibrary());
144 RefPtr<Framework::JsEngine> jsEngine;
145 if (GetSettings().usingSharedRuntime) {
146 jsEngine = loader.CreateJsEngineUsingSharedRuntime(instanceId_, sharedRuntime_);
147 } else {
148 jsEngine = loader.CreateJsEngine(instanceId_);
149 }
150
151 jsEngine->AddExtraNativeObject("ability", aceAbility.get());
152 auto pageUrlCheckFunc = [id = instanceId_](
153 const std::string& url, const std::function<void()>& callback,
154 const std::function<void(int32_t, const std::string&)>& silentInstallErrorCallBack) {
155 ContainerScope scope(id);
156 auto container = Container::Current();
157 CHECK_NULL_VOID(container);
158 auto pageUrlChecker = container->GetPageUrlChecker();
159 CHECK_NULL_VOID(pageUrlChecker);
160 pageUrlChecker->LoadPageUrl(url, callback, silentInstallErrorCallBack);
161 };
162 jsEngine->SetPageUrlCheckFunc(std::move(pageUrlCheckFunc));
163 EngineHelper::AddEngine(instanceId_, jsEngine);
164
165 declarativeFrontend->SetJsEngine(jsEngine);
166 declarativeFrontend->SetPageProfile(pageProfile_);
167 declarativeFrontend->SetNeedDebugBreakPoint(AceApplicationInfo::GetInstance().IsNeedDebugBreakPoint());
168 declarativeFrontend->SetDebugVersion(AceApplicationInfo::GetInstance().IsDebugVersion());
169 }
170 } else {
171 frontend_ = OHOS::Ace::Platform::AceContainer::GetContainer(parentId_)->GetFrontend();
172 return;
173 }
174 }
175
176 } // namespace OHOS::Ace::Platform