1 /*
2 * Copyright (c) 2021-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 "adapter/preview/entrance/ace_ability.h"
17 #include "adapter/preview/entrance/ace_view_preview.h"
18
19 #ifdef INIT_ICU_DATA_PATH
20 #include "unicode/putil.h"
21 #endif
22
23 #include <ui/rs_surface_node.h>
24 #include <ui/rs_ui_director.h>
25
26 #include "include/core/SkFontMgr.h"
27 #include "previewer/include/window.h"
28
29 #include "adapter/ohos/entrance/ace_new_pipe_judgement.h"
30 #include "adapter/preview/entrance/ace_application_info.h"
31 #include "adapter/preview/entrance/ace_container.h"
32 #include "adapter/preview/entrance/event_dispatcher.h"
33 #include "adapter/preview/entrance/rs_dir_asset_provider.h"
34 #include "adapter/preview/external/ability/stage/stage_context.h"
35 #include "adapter/preview/inspector/inspector_client.h"
36 #include "core/common/resource/resource_configuration.h"
37 #include "frameworks/base/utils/utils.h"
38 #include "frameworks/bridge/common/utils/utils.h"
39 #include "frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_context_module.h"
40 #include "frameworks/bridge/js_frontend/js_frontend.h"
41 #include "frameworks/core/common/ace_engine.h"
42 #ifdef COMPONENT_TEST_ENABLED
43 #include "frameworks/component_test/test_config.h"
44 #endif // COMPONENT_TEST_ENABLED
45
46 namespace OHOS::Ace::Platform {
47 namespace {
48
49 constexpr char ASSET_PATH_SHARE[] = "share";
50 #ifdef WINDOWS_PLATFORM
51 constexpr char DELIMITER[] = "\\";
52 constexpr char ASSET_PATH_SHARE_STAGE[] = "resources\\base\\profile";
53 #else
54 constexpr char DELIMITER[] = "/";
55 constexpr char ASSET_PATH_SHARE_STAGE[] = "resources/base/profile";
56 #endif
57
SetFontMgrConfig(const std::string & containerSdkPath)58 void SetFontMgrConfig(const std::string& containerSdkPath)
59 {
60 // To check if use ohos or container fonts.
61 std::string runtimeOS = "OHOS_Container";
62 std::string containerFontBasePath = containerSdkPath + DELIMITER + "resources" + DELIMITER + "fonts" + DELIMITER;
63 RSDirAssetProvider dirAsset(containerFontBasePath);
64 std::vector<std::string> fileList;
65 dirAsset.GetAssetList("", fileList);
66 if (containerSdkPath.empty() || fileList.empty()) {
67 runtimeOS = "OHOS";
68 containerFontBasePath = "";
69 }
70 SkFontMgr::SetFontMgrConfig(runtimeOS, containerFontBasePath);
71 }
72
GetCustomAssetPath(std::string assetPath)73 std::string GetCustomAssetPath(std::string assetPath)
74 {
75 if (assetPath.empty()) {
76 LOGW("AssetPath is null.");
77 return std::string();
78 }
79 std::string customAssetPath;
80 if (OHOS::Ace::Framework::EndWith(assetPath, DELIMITER)) {
81 assetPath = assetPath.substr(0, assetPath.size() - 1);
82 }
83 customAssetPath = assetPath.substr(0, assetPath.find_last_of(DELIMITER) + 1);
84 return customAssetPath;
85 }
86
DumpAceRunArgs(const AceRunArgs & runArgs)87 inline void DumpAceRunArgs(const AceRunArgs& runArgs)
88 {
89 #ifdef ACE_DEBUG
90 LOGI("runArgs.pageProfile: %{private}s", runArgs.pageProfile.c_str());
91 LOGI("runArgs.asset: %{private}s", runArgs.assetPath.c_str());
92 LOGI("runArgs.systemResources: %{private}s", runArgs.systemResourcesPath.c_str());
93 LOGI("runArgs.appResources: %{private}s", runArgs.appResourcesPath.c_str());
94 LOGI("runArgs.themeId: %{private}u", runArgs.themeId);
95 LOGI("runArgs.deviceConfig.orientation: %{private}d", static_cast<int>(runArgs.deviceConfig.orientation));
96 LOGI("runArgs.deviceConfig.density: %{private}lf", runArgs.deviceConfig.density);
97 LOGI("runArgs.deviceConfig.deviceType: %{private}d", static_cast<int>(runArgs.deviceConfig.deviceType));
98 LOGI("runArgs.deviceConfig.fontRatio: %{private}lf", runArgs.deviceConfig.fontRatio);
99 LOGI("runArgs.deviceConfig.colorMode: %{private}d", static_cast<int>(runArgs.deviceConfig.colorMode));
100 LOGI("runArgs.url: %{private}s", runArgs.url.c_str());
101 LOGI("runArgs.windowTitle: %{private}s", runArgs.windowTitle.c_str());
102 LOGI("runArgs.isRound: %{private}s", runArgs.isRound ? "true" : "false");
103 LOGI("runArgs.viewWidth: %{private}d", runArgs.viewWidth);
104 LOGI("runArgs.viewHeight: %{private}d", runArgs.viewHeight);
105 LOGI("runArgs.deviceWidth: %{private}d", runArgs.deviceWidth);
106 LOGI("runArgs.deviceHeight: %{private}d", runArgs.deviceHeight);
107 #endif
108 }
109
110 } // namespace
111
ConvertAvoidArea(const OHOS::Rosen::AvoidArea & avoidArea)112 NG::SafeAreaInsets ConvertAvoidArea(const OHOS::Rosen::AvoidArea& avoidArea)
113 {
114 return NG::SafeAreaInsets({ avoidArea.leftRect_.posX_, avoidArea.leftRect_.posX_ + avoidArea.leftRect_.width_ },
115 { avoidArea.topRect_.posY_, avoidArea.topRect_.posY_ + avoidArea.topRect_.height_ },
116 { avoidArea.rightRect_.posX_, avoidArea.rightRect_.posX_ + avoidArea.rightRect_.width_ },
117 { avoidArea.bottomRect_.posY_, avoidArea.bottomRect_.posY_ + avoidArea.bottomRect_.height_ });
118 }
119
GetViewSafeAreaByType(OHOS::Rosen::AvoidAreaType type,sptr<OHOS::Rosen::Window> rsWindow)120 NG::SafeAreaInsets GetViewSafeAreaByType(OHOS::Rosen::AvoidAreaType type, sptr<OHOS::Rosen::Window> rsWindow)
121 {
122 CHECK_NULL_RETURN(rsWindow, {});
123 Rosen::AvoidArea avoidArea;
124 Rosen::WMError ret = rsWindow->GetAvoidAreaByType(type, avoidArea);
125 if (ret == Rosen::WMError::WM_OK) {
126 auto safeAreaInsets = ConvertAvoidArea(avoidArea);
127 LOGI("SafeArea get success, area type is:%{public}d insets area is:%{public}s", static_cast<int32_t>(type),
128 safeAreaInsets.ToString().c_str());
129 return safeAreaInsets;
130 }
131 return {};
132 }
133
134 class IIgnoreViewSafeAreaListenerPreview : public OHOS::Rosen::IIgnoreViewSafeAreaListener {
135 public:
IIgnoreViewSafeAreaListenerPreview(int32_t instanceId)136 explicit IIgnoreViewSafeAreaListenerPreview(int32_t instanceId) : instanceId_(instanceId) {}
137 ~IIgnoreViewSafeAreaListenerPreview() = default;
138
SetIgnoreViewSafeArea(bool ignoreViewSafeArea)139 void SetIgnoreViewSafeArea(bool ignoreViewSafeArea)
140 {
141 LOGD("[instanceId_:%{public}d]: SetIgnoreViewSafeArea:%{public}u", instanceId_, ignoreViewSafeArea);
142 auto container = AceEngine::Get().GetContainer(instanceId_);
143 CHECK_NULL_VOID(container);
144 auto pipelineContext = container->GetPipelineContext();
145 auto taskExecutor = container->GetTaskExecutor();
146 CHECK_NULL_VOID(taskExecutor);
147 taskExecutor->PostSyncTask(
148 [&pipelineContext, container, ignoreSafeArea = ignoreViewSafeArea]() {
149 pipelineContext->SetIgnoreViewSafeArea(ignoreSafeArea);
150 },
151 TaskExecutor::TaskType::UI, "ArkUISetIgnoreViewSafeArea");
152 }
153
154 private:
155 int32_t instanceId_ = -1;
156 };
157
158 class AvoidAreaChangedListenerPreview : public OHOS::Rosen::IAvoidAreaChangedListener {
159 public:
AvoidAreaChangedListenerPreview(int32_t instanceId)160 explicit AvoidAreaChangedListenerPreview(int32_t instanceId) : instanceId_(instanceId) {}
161 ~AvoidAreaChangedListenerPreview() = default;
162
OnAvoidAreaChanged(const OHOS::Rosen::AvoidArea avoidArea,OHOS::Rosen::AvoidAreaType type)163 void OnAvoidAreaChanged(const OHOS::Rosen::AvoidArea avoidArea, OHOS::Rosen::AvoidAreaType type)
164 {
165 LOGD("Avoid area changed, type:%{public}d, topRect: avoidArea:x:%{public}d, y:%{public}d, "
166 "width:%{public}d, height:%{public}d; bottomRect: avoidArea:x:%{public}d, y:%{public}d, "
167 "width:%{public}d, height:%{public}d",
168 type, avoidArea.topRect_.posX_, avoidArea.topRect_.posY_, (int32_t)avoidArea.topRect_.width_,
169 (int32_t)avoidArea.topRect_.height_, avoidArea.bottomRect_.posX_, avoidArea.bottomRect_.posY_,
170 (int32_t)avoidArea.bottomRect_.width_, (int32_t)avoidArea.bottomRect_.height_);
171 auto container = Platform::AceContainer::GetContainerInstance(instanceId_);
172 CHECK_NULL_VOID(container);
173 auto pipeline = container->GetPipelineContext();
174 CHECK_NULL_VOID(pipeline);
175 auto taskExecutor = container->GetTaskExecutor();
176 CHECK_NULL_VOID(taskExecutor);
177 if (type == Rosen::AvoidAreaType::TYPE_SYSTEM) {
178 systemSafeArea_ = ConvertAvoidArea(avoidArea);
179 } else if (type == Rosen::AvoidAreaType::TYPE_NAVIGATION_INDICATOR) {
180 navigationBar_ = ConvertAvoidArea(avoidArea);
181 } else if (type == Rosen::AvoidAreaType::TYPE_CUTOUT) {
182 cutoutSafeArea_ = ConvertAvoidArea(avoidArea);
183 }
184 auto safeArea = systemSafeArea_;
185 auto navSafeArea = navigationBar_;
186 auto cutoutSafeArea = cutoutSafeArea_;
187 taskExecutor->PostTask(
188 [pipeline, safeArea, navSafeArea, cutoutSafeArea, type, avoidArea] {
189 if (type == Rosen::AvoidAreaType::TYPE_SYSTEM) {
190 pipeline->UpdateSystemSafeArea(safeArea);
191 } else if (type == Rosen::AvoidAreaType::TYPE_NAVIGATION_INDICATOR) {
192 pipeline->UpdateNavSafeArea(navSafeArea);
193 } else if (type == Rosen::AvoidAreaType::TYPE_CUTOUT) {
194 pipeline->UpdateCutoutSafeArea(cutoutSafeArea);
195 }
196 // for ui extension component
197 pipeline->UpdateOriginAvoidArea(avoidArea, static_cast<uint32_t>(type));
198 },
199 TaskExecutor::TaskType::UI, "ArkUIUpdateOriginAvoidArea");
200 }
201
202 private:
203 NG::SafeAreaInsets systemSafeArea_;
204 NG::SafeAreaInsets navigationBar_;
205 NG::SafeAreaInsets cutoutSafeArea_;
206 int32_t instanceId_ = -1;
207 };
208
AceAbility(const AceRunArgs & runArgs)209 AceAbility::AceAbility(const AceRunArgs& runArgs) : runArgs_(runArgs)
210 {
211 static std::once_flag onceFlag;
212 std::call_once(onceFlag, []() {
213 Container::UpdateCurrent(ACE_INSTANCE_ID);
214 });
215 SystemProperties::SetExtSurfaceEnabled(!runArgs.containerSdkPath.empty());
216 SystemProperties::InitDeviceInfo(runArgs.deviceWidth, runArgs.deviceHeight,
217 runArgs.deviceConfig.orientation == DeviceOrientation::PORTRAIT ? 0 : 1, runArgs.deviceConfig.density,
218 runArgs.isRound);
219 SystemProperties::InitDeviceType(runArgs.deviceConfig.deviceType);
220 InitializeAppInfo();
221 if (runArgs_.aceVersion == AceVersion::ACE_1_0) {
222 if (runArgs_.formsEnabled) {
223 LOGI("CreateContainer with JS_CARD frontend");
224 AceContainer::CreateContainer(ACE_INSTANCE_ID, FrontendType::JS_CARD, false);
225 } else {
226 LOGI("CreateContainer with JS frontend");
227 AceContainer::CreateContainer(ACE_INSTANCE_ID, FrontendType::JS, false);
228 }
229 } else if (runArgs_.aceVersion == AceVersion::ACE_2_0) {
230 if (runArgs_.formsEnabled) {
231 LOGI("CreateContainer with ETS_CARD frontend");
232 AceContainer::CreateContainer(ACE_INSTANCE_ID, FrontendType::ETS_CARD, useNewPipeline_);
233 } else {
234 LOGI("CreateContainer with JSDECLARATIVE frontend");
235 AceContainer::CreateContainer(ACE_INSTANCE_ID, FrontendType::DECLARATIVE_JS, useNewPipeline_);
236 }
237 } else {
238 LOGW("UnKnown frontend type");
239 }
240 AceContainer::SetComponentModeFlag(runArgs.isComponentMode);
241 SetConfigChanges(runArgs.configChanges);
242 auto container = AceContainer::GetContainerInstance(ACE_INSTANCE_ID);
243 CHECK_NULL_VOID(container);
244 container->SetColorMode(runArgs.deviceConfig.colorMode);
245 container->SetContainerSdkPath(runArgs.containerSdkPath);
246 container->SetInstallationFree(installationFree_);
247 container->SetLabelId(labelId_);
248 auto config = container->GetResourceConfiguration();
249 config.SetDeviceType(SystemProperties::GetDeviceType());
250 config.SetOrientation(SystemProperties::GetDeviceOrientation());
251 config.SetDensity(SystemProperties::GetResolution());
252 config.SetColorMode(container->GetColorMode());
253 config.SetFontRatio(runArgs.deviceConfig.fontRatio);
254 container->SetResourceConfiguration(config);
255 container->SetBundleName(bundleName_);
256 container->SetModuleName(moduleName_);
257 container->SetApiTargetVersion(AceApplicationInfo::GetInstance().GetApiTargetVersion());
258 InitializeClipboard();
259 }
260
~AceAbility()261 AceAbility::~AceAbility()
262 {
263 AceContainer::DestroyContainer(ACE_INSTANCE_ID);
264 }
265
SetMockModuleList(const std::map<std::string,std::string> & mockJsonInfo)266 void AceAbility::SetMockModuleList(const std::map<std::string, std::string>& mockJsonInfo)
267 {
268 // only support the stage model
269 if (runArgs_.projectModel == ProjectModel::STAGE) {
270 auto container = AceContainer::GetContainerInstance(ACE_INSTANCE_ID);
271 CHECK_NULL_VOID(container);
272 container->SetMockModuleList(mockJsonInfo);
273 }
274 }
275
CreateInstance(AceRunArgs & runArgs)276 std::unique_ptr<AceAbility> AceAbility::CreateInstance(AceRunArgs& runArgs)
277 {
278 DumpAceRunArgs(runArgs);
279 LOGI("Start create AceAbility instance");
280 #ifdef INIT_ICU_DATA_PATH
281 std::string icuPath = ".";
282 u_setDataDirectory(icuPath.c_str());
283 #endif
284 AceApplicationInfo::GetInstance().SetLocale(runArgs.language, runArgs.region, runArgs.script, "");
285 Ace::Framework::JsiContextModule::GetInstance()->IsPreview();
286 SetFontMgrConfig(runArgs.containerSdkPath);
287 EventDispatcher::GetInstance().Initialize();
288 auto aceAbility = std::make_unique<AceAbility>(runArgs);
289 return aceAbility;
290 }
291
InitEnv()292 void AceAbility::InitEnv()
293 {
294 std::vector<std::string> paths;
295 paths.push_back(runArgs_.assetPath);
296 std::string appResourcesPath(runArgs_.appResourcesPath);
297 if (!OHOS::Ace::Framework::EndWith(appResourcesPath, DELIMITER)) {
298 appResourcesPath.append(DELIMITER);
299 }
300 if (runArgs_.projectModel == ProjectModel::STAGE) {
301 // eTS Card
302 if (runArgs_.aceVersion == AceVersion::ACE_2_0 && runArgs_.formsEnabled) {
303 paths.push_back(runArgs_.assetPath + DELIMITER + "ets");
304 }
305 paths.push_back(appResourcesPath);
306 paths.push_back(appResourcesPath + ASSET_PATH_SHARE_STAGE);
307 } else {
308 paths.push_back(GetCustomAssetPath(runArgs_.assetPath) + ASSET_PATH_SHARE);
309 }
310 if (!runArgs_.containerSdkPath.empty()) {
311 paths.push_back(runArgs_.containerSdkPath);
312 }
313 AceContainer::AddAssetPath(ACE_INSTANCE_ID, "", paths);
314 auto container = AceContainer::GetContainerInstance(ACE_INSTANCE_ID);
315 CHECK_NULL_VOID(container);
316 if (runArgs_.projectModel == ProjectModel::STAGE) {
317 auto pkgcontextinfo = Referenced::MakeRefPtr<StagePkgContextInfo>();
318 pkgcontextinfo->SetPkgNameList(runArgs_.packageNameList);
319 pkgcontextinfo->SetPkgContextInfoAndAliasMap(runArgs_.pkgContextInfoJsonStringMap);
320 container->SetPkgContextInfo(pkgcontextinfo);
321 if (runArgs_.formsEnabled) {
322 container->SetStageCardConfig(runArgs_.pageProfile, runArgs_.url);
323 } else {
324 container->SetPageProfile((runArgs_.pageProfile.empty() ? "" : runArgs_.pageProfile + ".json"));
325 }
326 }
327 AceContainer::SetResourcesPathAndThemeStyle(ACE_INSTANCE_ID, runArgs_.systemResourcesPath,
328 runArgs_.containerSdkPath, runArgs_.appResourcesPath, runArgs_.themeId, runArgs_.deviceConfig.colorMode);
329
330 auto view = AceViewPreview::CreateView(ACE_INSTANCE_ID);
331 auto window = GetWindow();
332 avoidAreaChangedListener_ = new AvoidAreaChangedListenerPreview(ACE_INSTANCE_ID);
333 window->RegisterAvoidAreaChangeListener(avoidAreaChangedListener_);
334 ignoreViewSafeAreaListener_ = new IIgnoreViewSafeAreaListenerPreview(ACE_INSTANCE_ID);
335 window->RegisterIgnoreViewSafeAreaListener(ignoreViewSafeAreaListener_);
336 UIEnvCallback callback = [window, id = ACE_INSTANCE_ID](const OHOS::Ace::RefPtr<PipelineContext>& context) mutable {
337 CHECK_NULL_VOID(context);
338 CHECK_NULL_VOID(window);
339 auto director = OHOS::Rosen::RSUIDirector::Create();
340 CHECK_NULL_VOID(director);
341 director->SetRSSurfaceNode(window->GetSurfaceNode());
342 auto container = AceContainer::GetContainerInstance(id);
343 CHECK_NULL_VOID(container);
344 auto func = [taskExecutor = container->GetTaskExecutor(), id](
345 const std::function<void()>& task, uint32_t delay) {
346 CHECK_NULL_VOID(taskExecutor);
347 ContainerScope scope(id);
348 taskExecutor->PostDelayedTask(
349 task, TaskExecutor::TaskType::UI, delay, "ArkUIRenderServiceTask", PriorityType::HIGH);
350 };
351 director->SetUITaskRunner(func, id);
352 director->Init();
353 context->SetRSUIDirector(director);
354 };
355
356 if (runArgs_.aceVersion == AceVersion::ACE_2_0) {
357 AceContainer::SetView(
358 view, window, runArgs_.deviceConfig.density, runArgs_.deviceWidth, runArgs_.deviceHeight, callback);
359 AceContainer::RunPage(ACE_INSTANCE_ID, runArgs_.url, "");
360 } else {
361 AceContainer::RunPage(ACE_INSTANCE_ID, runArgs_.url, "");
362 AceContainer::SetView(
363 view, window, runArgs_.deviceConfig.density, runArgs_.deviceWidth, runArgs_.deviceHeight, callback);
364 }
365 // Drive the native engine with the platform thread.
366 container->RunNativeEngineLoop();
367 auto pipelineContext = container->GetPipelineContext();
368 if (pipelineContext) {
369 LOGI("Set MinPlatformVersion to %{public}d", compatibleVersion_);
370 pipelineContext->SetMinPlatformVersion(compatibleVersion_);
371 pipelineContext->SetDisplayWindowRectInfo(
372 Rect(Offset(0.0f, 0.0f), Size(runArgs_.deviceWidth, runArgs_.deviceHeight)));
373 }
374 container->InitializeAppConfig(runArgs_.assetPath, bundleName_, moduleName_, compileMode_);
375 pipelineContext->UpdateSystemSafeArea(GetViewSafeAreaByType(Rosen::AvoidAreaType::TYPE_SYSTEM, window));
376 pipelineContext->UpdateCutoutSafeArea(GetViewSafeAreaByType(Rosen::AvoidAreaType::TYPE_CUTOUT, window));
377 pipelineContext->UpdateNavSafeArea(GetViewSafeAreaByType(Rosen::AvoidAreaType::TYPE_NAVIGATION_INDICATOR, window));
378 AceContainer::AddRouterChangeCallback(ACE_INSTANCE_ID, runArgs_.onRouterChange);
379 OHOS::Ace::Framework::InspectorClient::GetInstance().RegisterFastPreviewErrorCallback(runArgs_.onError);
380 // Should make it possible to update surface changes by using viewWidth and viewHeight.
381 view->NotifySurfaceChanged(runArgs_.deviceWidth, runArgs_.deviceHeight);
382 view->NotifyDensityChanged(runArgs_.deviceConfig.density);
383 }
384
InitializeClipboard() const385 void AceAbility::InitializeClipboard() const
386 {
387 ClipboardProxy::GetInstance()->SetDelegate(std::make_unique<Platform::ClipboardProxyImpl>());
388 }
389
OnBackPressed() const390 void AceAbility::OnBackPressed() const
391 {
392 LOGI("Process Back Pressed Event");
393 EventDispatcher::GetInstance().DispatchBackPressedEvent();
394 }
395
OnInputEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent) const396 bool AceAbility::OnInputEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent) const
397 {
398 return EventDispatcher::GetInstance().DispatchTouchEvent(pointerEvent);
399 }
400
OnInputEvent(const std::shared_ptr<MMI::KeyEvent> & keyEvent) const401 bool AceAbility::OnInputEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent) const
402 {
403 return EventDispatcher::GetInstance().DispatchKeyEvent(keyEvent);
404 }
405
OnInputEvent(const std::shared_ptr<MMI::AxisEvent> & axisEvent) const406 bool AceAbility::OnInputEvent(const std::shared_ptr<MMI::AxisEvent>& axisEvent) const
407 {
408 return false;
409 }
410
OnInputMethodEvent(const unsigned int codePoint) const411 bool AceAbility::OnInputMethodEvent(const unsigned int codePoint) const
412 {
413 return EventDispatcher::GetInstance().DispatchInputMethodEvent(codePoint);
414 }
415
InitializeAppInfo()416 void AceAbility::InitializeAppInfo()
417 {
418 RefPtr<Context> context =
419 Context::CreateContext(runArgs_.projectModel == ProjectModel::STAGE, runArgs_.appResourcesPath);
420 if (runArgs_.projectModel == ProjectModel::STAGE) {
421 auto stageContext = AceType::DynamicCast<StageContext>(context);
422 CHECK_NULL_VOID(stageContext);
423 auto appInfo = stageContext->GetAppInfo();
424 CHECK_NULL_VOID(appInfo);
425 auto hapModuleInfo = stageContext->GetHapModuleInfo();
426 CHECK_NULL_VOID(hapModuleInfo);
427 bundleName_ = appInfo->GetBundleName();
428 moduleName_ = hapModuleInfo->GetModuleName();
429 compileMode_ = hapModuleInfo->GetCompileMode();
430 compatibleVersion_ = appInfo->GetMinAPIVersion();
431 auto targetVersion = appInfo->GetTargetAPIVersion();
432 AceApplicationInfo::GetInstance().SetApiTargetVersion(static_cast<int32_t>(targetVersion));
433 auto releaseType = appInfo->GetApiReleaseType();
434 bool enablePartialUpdate = hapModuleInfo->GetPartialUpdateFlag();
435 // only app should have menubar, card don't need
436 if (!runArgs_.formsEnabled) {
437 installationFree_ = appInfo->IsInstallationFree();
438 labelId_ = hapModuleInfo->GetLabelId();
439 }
440 useNewPipeline_ = AceNewPipeJudgement::QueryAceNewPipeEnabledStage(
441 "", compatibleVersion_, targetVersion, releaseType, !enablePartialUpdate);
442 } else {
443 auto faContext = AceType::DynamicCast<FaContext>(context);
444 CHECK_NULL_VOID(faContext);
445 auto appInfo = faContext->GetAppInfo();
446 CHECK_NULL_VOID(appInfo);
447 auto hapModuleInfo = faContext->GetHapModuleInfo();
448 CHECK_NULL_VOID(hapModuleInfo);
449 bundleName_ = appInfo->GetBundleName();
450 compatibleVersion_ = appInfo->GetMinAPIVersion();
451 auto targetVersion = appInfo->GetTargetAPIVersion();
452 auto releaseType = appInfo->GetApiReleaseType();
453 useNewPipeline_ =
454 AceNewPipeJudgement::QueryAceNewPipeEnabledFA("", compatibleVersion_, targetVersion, releaseType);
455 }
456 #ifdef COMPONENT_TEST_ENABLED
457 if (runArgs_.isComponentTestMode && runArgs_.componentTestConfig != "") {
458 ComponentTest::ParseComponentTestConfig(runArgs_.componentTestConfig);
459 }
460 #endif // COMPONENT_TEST_ENABLED
461 }
462
SetConfigChanges(const std::string & configChanges)463 void AceAbility::SetConfigChanges(const std::string& configChanges)
464 {
465 if (configChanges == "") {
466 return;
467 }
468 std::vector<std::string> configChangesSplitter;
469 OHOS::Ace::StringUtils::StringSplitter(configChanges, ',', configChangesSplitter);
470 for (const auto& singleConfig : configChangesSplitter) {
471 if (singleConfig == "locale") {
472 configChanges_.watchLocale = true;
473 continue;
474 } else if (singleConfig == "layout") {
475 configChanges_.watchLayout = true;
476 continue;
477 } else if (singleConfig == "fontSize") {
478 configChanges_.watchFontSize = true;
479 continue;
480 } else if (singleConfig == "orientation") {
481 configChanges_.watchOrientation = true;
482 continue;
483 } else if (singleConfig == "density") {
484 configChanges_.watchDensity = true;
485 continue;
486 } else {
487 LOGW("unsupported config %{public}s", singleConfig.c_str());
488 }
489 }
490 }
491
OnConfigurationChanged(const DeviceConfig & newConfig)492 void AceAbility::OnConfigurationChanged(const DeviceConfig& newConfig)
493 {
494 if (newConfig.colorMode == runArgs_.deviceConfig.colorMode) {
495 return;
496 }
497 int32_t width = runArgs_.deviceWidth;
498 int32_t height = runArgs_.deviceHeight;
499 SurfaceChanged(runArgs_.deviceConfig.orientation, runArgs_.deviceConfig.density, width, height);
500 auto container = AceContainer::GetContainerInstance(ACE_INSTANCE_ID);
501 if (!container) {
502 LOGW("container is null, change configuration failed.");
503 return;
504 }
505 container->UpdateDeviceConfig(newConfig);
506 runArgs_.deviceConfig.colorMode = newConfig.colorMode;
507 if (container->GetType() == FrontendType::DECLARATIVE_JS) {
508 container->NativeOnConfigurationUpdated(ACE_INSTANCE_ID);
509 }
510 }
511
SurfaceChanged(const DeviceOrientation & orientation,const double & resolution,int32_t & width,int32_t & height,WindowSizeChangeReason type)512 void AceAbility::SurfaceChanged(
513 const DeviceOrientation& orientation, const double& resolution, int32_t& width, int32_t& height,
514 WindowSizeChangeReason type)
515 {
516 auto container = AceContainer::GetContainerInstance(ACE_INSTANCE_ID);
517 CHECK_NULL_VOID(container);
518 auto viewPtr = AceType::DynamicCast<AceViewPreview>(container->GetAceView());
519 CHECK_NULL_VOID(viewPtr);
520 // Need to change the window resolution and then change the rendering resolution. Otherwise, the image may not adapt
521 // to the new window after the window is modified.
522 auto context = container->GetPipelineContext();
523 CHECK_NULL_VOID(context);
524 context->SetDisplayWindowRectInfo(Rect(Offset(0, 0), Size(width, height)));
525 SystemProperties::InitDeviceInfo(
526 width, height, orientation == DeviceOrientation::PORTRAIT ? 0 : 1, resolution, runArgs_.isRound);
527 DeviceConfig deviceConfig = runArgs_.deviceConfig;
528 deviceConfig.orientation = orientation;
529 deviceConfig.density = resolution;
530 container->UpdateDeviceConfig(deviceConfig);
531 viewPtr->NotifyDensityChanged(resolution);
532 viewPtr->NotifySurfaceChanged(width, height, type);
533 if ((orientation != runArgs_.deviceConfig.orientation && configChanges_.watchOrientation) ||
534 (resolution != runArgs_.deviceConfig.density && configChanges_.watchDensity) ||
535 ((width != runArgs_.deviceWidth || height != runArgs_.deviceHeight) && configChanges_.watchLayout)) {
536 container->NativeOnConfigurationUpdated(ACE_INSTANCE_ID);
537 }
538 if (orientation != runArgs_.deviceConfig.orientation || resolution != runArgs_.deviceConfig.density) {
539 container->NotifyConfigurationChange(false, ConfigurationChange({ false, false, true }));
540 }
541 runArgs_.deviceConfig.orientation = orientation;
542 runArgs_.deviceConfig.density = resolution;
543 runArgs_.deviceWidth = width;
544 runArgs_.deviceHeight = height;
545 }
546
ReplacePage(const std::string & url,const std::string & params)547 void AceAbility::ReplacePage(const std::string& url, const std::string& params)
548 {
549 auto container = AceContainer::GetContainerInstance(ACE_INSTANCE_ID);
550 CHECK_NULL_VOID(container);
551 container->GetFrontend()->ReplacePage(url, params);
552 }
553
LoadDocument(const std::string & url,const std::string & componentName,SystemParams & systemParams)554 void AceAbility::LoadDocument(const std::string& url, const std::string& componentName, SystemParams& systemParams)
555 {
556 LOGI("Component Preview start:%{public}s, ", componentName.c_str());
557 AceApplicationInfo::GetInstance().ChangeLocale(systemParams.language, systemParams.region);
558 runArgs_.isRound = systemParams.isRound;
559 SurfaceChanged(systemParams.orientation, systemParams.density, systemParams.deviceWidth, systemParams.deviceHeight);
560 DeviceConfig deviceConfig = {
561 .orientation = systemParams.orientation,
562 .density = systemParams.density,
563 .deviceType = systemParams.deviceType,
564 .colorMode = systemParams.colorMode,
565 };
566 OnConfigurationChanged(deviceConfig);
567 auto container = AceContainer::GetContainerInstance(ACE_INSTANCE_ID);
568 CHECK_NULL_VOID(container);
569 container->LoadDocument(url, componentName);
570 LOGI("Component Preview end");
571 }
572
GetJSONTree()573 std::string AceAbility::GetJSONTree()
574 {
575 LOGI("Inspector start");
576 std::string jsonTreeStr;
577 auto container = AceContainer::GetContainerInstance(ACE_INSTANCE_ID);
578 CHECK_NULL_RETURN(container, "");
579 auto taskExecutor = container->GetTaskExecutor();
580 CHECK_NULL_RETURN(taskExecutor, "");
581 taskExecutor->PostSyncTask(
582 [&jsonTreeStr] { OHOS::Ace::Framework::InspectorClient::GetInstance().AssembleJSONTreeStr(jsonTreeStr); },
583 TaskExecutor::TaskType::UI, "ArkUIGetJsonTreeStr");
584 LOGI("Inspector end");
585 return jsonTreeStr;
586 }
587
GetDefaultJSONTree()588 std::string AceAbility::GetDefaultJSONTree()
589 {
590 std::string defaultJsonTreeStr;
591 auto container = AceContainer::GetContainerInstance(ACE_INSTANCE_ID);
592 CHECK_NULL_RETURN(container, "");
593 auto taskExecutor = container->GetTaskExecutor();
594 CHECK_NULL_RETURN(taskExecutor, "");
595 taskExecutor->PostSyncTask(
596 [&defaultJsonTreeStr] {
597 OHOS::Ace::Framework::InspectorClient::GetInstance().AssembleDefaultJSONTreeStr(defaultJsonTreeStr);
598 },
599 TaskExecutor::TaskType::UI, "ArkUIGetDefaultJsonTreeStr");
600 return defaultJsonTreeStr;
601 }
602
OperateComponent(const std::string & attrsJson)603 bool AceAbility::OperateComponent(const std::string& attrsJson)
604 {
605 LOGI("Fast Preview start");
606 auto root = JsonUtil::ParseJsonString(attrsJson);
607 if (!root || !root->IsValid()) {
608 LOGE("Fast Preview failed: the attrsJson is illegal json format");
609 return false;
610 }
611
612 auto container = AceContainer::GetContainerInstance(ACE_INSTANCE_ID);
613 if (!container) {
614 LOGE("Fast Preview failed: container is null");
615 return false;
616 }
617 auto taskExecutor = container->GetTaskExecutor();
618 if (!taskExecutor) {
619 LOGE("Fast Preview failed: taskExecutor is null");
620 return false;
621 }
622 taskExecutor->PostTask(
623 [attrsJson, instanceId = ACE_INSTANCE_ID] {
624 ContainerScope scope(instanceId);
625 bool result = OHOS::Ace::Framework::InspectorClient::GetInstance().OperateComponent(attrsJson);
626 if (!result) {
627 OHOS::Ace::Framework::InspectorClient::GetInstance().CallFastPreviewErrorCallback(attrsJson);
628 }
629 },
630 TaskExecutor::TaskType::UI, "ArkUIOperateComponent");
631 LOGI("Fast Preview end");
632 return true;
633 }
634
SetWindow(sptr<OHOS::Rosen::Window> rsWindow)635 void AceAbility::SetWindow(sptr<OHOS::Rosen::Window> rsWindow)
636 {
637 rsWindow_ = rsWindow;
638 }
639
GetWindow()640 sptr<OHOS::Rosen::Window> AceAbility::GetWindow()
641 {
642 return rsWindow_;
643 }
644
645 } // namespace OHOS::Ace::Platform
646