fix page loading when dynamic import a non-named page

Signed-off-by: Far <yesiyuan2@huawei.com>
Change-Id: I1147b825b849a0ea54e5f8d445f433d8b2e458b0
This commit is contained in:
Far 2023-10-17 15:05:05 +08:00
parent ae3f51700f
commit 03b5790a17
26 changed files with 117 additions and 24 deletions

View File

@ -97,6 +97,7 @@ foreach(item, ace_platforms) {
"$ace_root/frameworks/core/components/test/mock/mock_resource_adapter.cpp",
"$ace_root/frameworks/core/components/test/unittest/mock/subwindow_mock.cpp",
"$ace_root/test/mock/adapter/mock_ace_container.cpp",
"$ace_root/test/mock/interfaces/mock_ace_forward_compatibility.cpp",
]
if (is_ohos_standard_system) {

View File

@ -256,6 +256,7 @@ void DeclarativeFrontend::InitializeFrontendDelegate(const RefPtr<TaskExecutor>&
return;
}
jsEngine->LoadJs(url, jsPage, isMainPage);
jsEngine->UpdateRootComponent();
};
const auto& setPluginMessageTransferCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](
@ -530,7 +531,16 @@ void DeclarativeFrontend::InitializeFrontendDelegate(const RefPtr<TaskExecutor>&
}
return jsEngine->LoadNamedRouterSource(namedRouter, isTriggeredByJs);
};
delegate_->InitializeRouterManager(std::move(loadPageCallback), std::move(loadNamedRouterCallback));
auto updateRootComponentCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)]() {
auto jsEngine = weakEngine.Upgrade();
if (!jsEngine) {
return false;
}
return jsEngine->UpdateRootComponent();
};
delegate_->InitializeRouterManager(std::move(loadPageCallback), std::move(loadNamedRouterCallback),
std::move(updateRootComponentCallback));
auto moduleNamecallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)](const std::string& pageName)->
std::string {
auto jsEngine = weakEngine.Upgrade();

View File

@ -888,6 +888,7 @@ napi_value JsiDeclarativeEngineInstance::GetContextValue()
}
std::unordered_map<std::string, NamedRouterProperty> JsiDeclarativeEngine::namedRouterRegisterMap_;
panda::Global<panda::ObjectRef> JsiDeclarativeEngine::obj_;
// -----------------------
// Start JsiDeclarativeEngine
@ -1221,6 +1222,12 @@ bool JsiDeclarativeEngine::ExecuteCardAbc(const std::string& fileName, int64_t c
return true;
}
bool JsiDeclarativeEngine::UpdateRootComponent()
{
Framework::UpdateRootComponent(JsiDeclarativeEngine::obj_.ToLocal());
return true;
}
void JsiDeclarativeEngine::LoadJs(const std::string& url, const RefPtr<JsAcePage>& page, bool isMainPage)
{
ACE_SCOPED_TRACE("JsiDeclarativeEngine::LoadJs url : %s", url.c_str());
@ -1513,7 +1520,7 @@ bool JsiDeclarativeEngine::LoadNamedRouterSource(const std::string& namedRoute,
shared_ptr<ArkJSRuntime> arkRuntime = std::static_pointer_cast<ArkJSRuntime>(runtime);
arkRuntime->AddRootView(rootView);
#endif
UpdateRootComponent(ret->ToObject(vm));
Framework::UpdateRootComponent(ret->ToObject(vm));
JSViewStackProcessor::JsStopGetAccessRecording();
LOGI("LoadNamedRouterSource name = %{public}s", namedRoute.c_str());
return true;

View File

@ -404,6 +404,12 @@ public:
const std::string& namedRoute, panda::Local<panda::ObjectRef> params);
bool LoadNamedRouterSource(const std::string& namedRoute, bool isTriggeredByJs) override;
std::string SearchRouterRegisterMap(const std::string& pageName) override;
bool UpdateRootComponent() override;
static void SetEntryObject(const panda::Global<panda::ObjectRef>& obj)
{
obj_ = obj;
}
private:
bool CallAppFunc(const std::string& appFuncName);
@ -441,6 +447,7 @@ private:
std::string pluginModuleName_;
static std::unordered_map<std::string, NamedRouterProperty> namedRouterRegisterMap_;
bool isFirstCallShow_ = true;
static panda::Global<panda::ObjectRef> obj_;
ACE_DISALLOW_COPY_AND_MOVE(JsiDeclarativeEngine);
};

View File

@ -221,14 +221,14 @@ panda::Local<panda::JSValueRef> JsLoadDocument(panda::JsiRuntimeCallInfo* runtim
return panda::JSValueRef::Undefined(vm);
}
panda::Local<panda::ObjectRef> obj = firstArg->ToObject(vm);
panda::Global<panda::ObjectRef> obj(vm, Local<panda::ObjectRef>(firstArg));
JsiDeclarativeEngine::SetEntryObject(obj);
#if defined(PREVIEW)
panda::Global<panda::ObjectRef> rootView(vm, obj->ToObject(vm));
auto runtime = JsiDeclarativeEngineInstance::GetCurrentRuntime();
shared_ptr<ArkJSRuntime> arkRuntime = std::static_pointer_cast<ArkJSRuntime>(runtime);
arkRuntime->AddRootView(rootView);
#endif
UpdateRootComponent(obj);
return panda::JSValueRef::Undefined(vm);
}

View File

@ -798,11 +798,13 @@ void FrontendDelegateDeclarative::GetStageSourceMap(
}
void FrontendDelegateDeclarative::InitializeRouterManager(
NG::LoadPageCallback&& loadPageCallback, NG::LoadNamedRouterCallback&& loadNamedRouterCallback)
NG::LoadPageCallback&& loadPageCallback, NG::LoadNamedRouterCallback&& loadNamedRouterCallback,
NG::UpdateRootComponentCallback&& updateRootComponentCallback)
{
pageRouterManager_ = AceType::MakeRefPtr<NG::PageRouterManager>();
pageRouterManager_->SetLoadJsCallback(std::move(loadPageCallback));
pageRouterManager_->SetLoadNamedRouterCallback(std::move(loadNamedRouterCallback));
pageRouterManager_->SetUpdateRootComponentCallback(std::move(updateRootComponentCallback));
}
// Start FrontendDelegate overrides.

View File

@ -308,7 +308,8 @@ public:
std::unordered_map<std::string, RefPtr<Framework::RevSourceMap>>& sourceMap);
void InitializeRouterManager(
NG::LoadPageCallback&& loadPageCallback, NG::LoadNamedRouterCallback&& loadNamedRouterCallback);
NG::LoadPageCallback&& loadPageCallback, NG::LoadNamedRouterCallback&& loadNamedRouterCallback,
NG::UpdateRootComponentCallback&& updateRootComponentCallback);
const RefPtr<NG::PageRouterManager>& GetPageRouterManager() const
{

View File

@ -226,8 +226,17 @@ void DeclarativeFrontendNG::InitializeDelegate(const RefPtr<TaskExecutor>& taskE
return jsEngine->LoadNamedRouterSource(namedRouter, isTriggeredByJs);
};
auto updateRootComponentCallback = [weakEngine = WeakPtr<Framework::JsEngine>(jsEngine_)]() {
auto jsEngine = weakEngine.Upgrade();
if (!jsEngine) {
return false;
}
return jsEngine->UpdateRootComponent();
};
pageRouterManager->SetLoadJsCallback(std::move(loadPageCallback));
pageRouterManager->SetLoadNamedRouterCallback(std::move(loadNamedRouterCallback));
pageRouterManager->SetUpdateRootComponentCallback(std::move(updateRootComponentCallback));
delegate_ = AceType::MakeRefPtr<Framework::FrontendDelegateDeclarativeNG>(taskExecutor);
delegate_->SetMediaQueryCallback(std::move(mediaQueryCallback));

View File

@ -916,22 +916,27 @@ void PageRouterManager::LoadPage(int32_t pageId, const RouterPageInfo& target, b
FrameNode::CreateFrameNode(V2::PAGE_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(), pagePattern);
pageNode->SetHostPageId(pageId);
pageRouterStack_.emplace_back(pageNode);
auto result = loadJs_(target.path, target.errorCallback);
if (pageNode->GetChildren().empty()) {
// try to load named route
result = loadNamedRouter_(target.url, target.isNamedRouterMode);
if (!result && target.isNamedRouterMode && target.errorCallback) {
loadJs_(target.path, target.errorCallback);
auto result = loadNamedRouter_(target.url, target.isNamedRouterMode);
if (!result) {
if (!target.isNamedRouterMode) {
result = updateRootComponent_();
} else if (target.errorCallback) {
target.errorCallback("The named route is not exist.", Framework::ERROR_CODE_NAMED_ROUTE_ERROR);
}
}
if (target.errorCallback != nullptr) {
target.errorCallback("", Framework::ERROR_CODE_NO_ERROR);
}
if (!result) {
LOGE("fail to load page file");
pageRouterStack_.pop_back();
return;
}
if (target.errorCallback != nullptr) {
target.errorCallback("", Framework::ERROR_CODE_NO_ERROR);
}
if (!OnPageReady(pageNode, needHideLast, needTransition)) {
LOGE("fail to mount page");
pageRouterStack_.pop_back();

View File

@ -35,6 +35,7 @@ using LoadPageCallback = std::function<bool(const std::string&,
const std::function<void(const std::string&, int32_t)>&)>;
using LoadCardCallback = std::function<bool(const std::string&, int64_t cardId)>;
using LoadNamedRouterCallback = std::function<bool(const std::string&, bool isTriggeredByJs)>;
using UpdateRootComponentCallback = std::function<bool()>;
enum class RouterMode {
STANDARD = 0,
@ -80,6 +81,11 @@ public:
loadNamedRouter_ = callback;
}
void SetUpdateRootComponentCallback(UpdateRootComponentCallback&& callback)
{
updateRootComponent_ = callback;
}
void EnableAlertBeforeBackPage(const std::string& message, std::function<void(int32_t)>&& callback);
void DisableAlertBeforeBackPage();
@ -178,6 +184,7 @@ private:
LoadPageCallback loadJs_;
LoadCardCallback loadCard_;
LoadNamedRouterCallback loadNamedRouter_;
UpdateRootComponentCallback updateRootComponent_;
bool isCardRouter_ = false;
int32_t pageId_ = 0;
std::list<WeakPtr<FrameNode>> pageRouterStack_;

View File

@ -130,6 +130,11 @@ public:
return "";
}
virtual bool UpdateRootComponent()
{
return false;
}
// Update running page
virtual void UpdateRunningPage(const RefPtr<JsAcePage>& page) = 0;

View File

@ -35,6 +35,7 @@
#include "core/components_ng/base/distributed_ui.h"
#include "core/components_ng/pattern/navigator/navigator_event_hub.h"
#include "core/pipeline/pipeline_base.h"
#include "interfaces/inner_api/ace/ace_forward_compatibility.h"
namespace OHOS::Ace {
@ -247,7 +248,7 @@ public:
static bool IsCurrentUseNewPipeline()
{
auto container = Current();
return container ? container->useNewPipeline_ : false;
return container ? container->useNewPipeline_ : AceForwardCompatibility::IsUseNG();
}
// SetCurrentUsePartialUpdate is called when initial render on a page

View File

@ -99,6 +99,7 @@ ohos_unittest("event_manager_test_ng") {
"$ace_root/test/mock/core/common/mock_container.cpp",
"$ace_root/test/mock/core/common/mock_flutter_window.cpp",
"$ace_root/test/mock/core/common/mock_window.cpp",
"$ace_root/test/mock/interfaces/mock_ace_forward_compatibility.cpp",
"event_manager_test_ng.cpp",
]

View File

@ -236,6 +236,7 @@ ohos_unittest("navigation_test_ng") {
"$ace_root/test/mock/core/common/mock_font_manager_ng.cpp",
"$ace_root/test/mock/core/render/mock_font_collection.cpp",
"$ace_root/test/mock/core/render/mock_paragraph.cpp",
"$ace_root/test/mock/interfaces/mock_ace_forward_compatibility.cpp",
"$ace_root/test/unittest/core/pipeline/mock_overlay_manager.cpp",
"bar_item_test_ng.cpp",
"nav_bar_test_ng.cpp",

View File

@ -55,9 +55,7 @@ void AceForwardCompatibility::Init(const std::string& bundleName, const uint32_t
isForceOldPipeline_ = true;
#endif
isNewPipeline_ = !isForceOldPipeline_
&& (apiCompatibleVersion >= ARKUI_NEW_PIPELINE_MIN_VERSION)
&& !deprecated;
isNewPipeline_ = (apiCompatibleVersion >= ARKUI_NEW_PIPELINE_MIN_VERSION) && !deprecated;
isInited_ = true;
LOGI("AceForwardCompatibility [%{public}s] force:%{public}d newpipe:%{public}d",
bundleName.c_str(), isForceOldPipeline_, isNewPipeline_);
@ -78,12 +76,17 @@ bool AceForwardCompatibility::IsForceOldPipeline()
bool AceForwardCompatibility::IsNewPipeline()
{
if (isInited_) {
return isNewPipeline_;
return isNewPipeline_ && !isForceOldPipeline_;
}
isNewAppspawn_ = !IsForceOldPipeline();
return !IsForceOldPipeline();
}
bool AceForwardCompatibility::IsUseNG()
{
return isNewPipeline_;
}
const char* AceForwardCompatibility::GetAceLibName()
{
const char* libName;
@ -98,7 +101,7 @@ const char* AceForwardCompatibility::GetAceLibName()
bool AceForwardCompatibility::PipelineChanged()
{
return isNewPipeline_ != isNewAppspawn_;
return (isNewPipeline_ && !isForceOldPipeline_) != isNewAppspawn_;
}
} // namespace Ace
} // namespace OHOS

View File

@ -29,7 +29,8 @@ public:
static void Init(const std::string& bundleName, const uint32_t apiCompatibleVersion, bool deprecated);
static bool Inited();
static bool IsForceOldPipeline();
static bool IsNewPipeline();
static bool IsNewPipeline(); // true for using libace, false for using libace_compatible
static bool IsUseNG(); // true for using newpipeline in ace engine
static bool PipelineChanged();
static const char* GetAceLibName();
private:

View File

@ -77,13 +77,14 @@ ohos_fuzztest("AceResKeyParserFuzzTest") {
# mock
#"$ace_root/frameworks/base/test/mock/mock_system_properties.cpp",
"$ace_root/test/mock/interfaces/mock_ace_forward_compatibility.cpp",
#fuzz
"$ace_root/test/fuzztest/acereskeyparser_fuzzer/acereskeyparser_fuzzer.cpp",
"$ace_root/adapter/ohos/entrance/file_asset_provider.cpp",
"$ace_root/adapter/ohos/osal/system_properties.cpp",
"$ace_root/frameworks/base/resource/ace_res_key_parser.cpp",
"$ace_root/frameworks/core/common/flutter/flutter_asset_manager.cpp",
"$ace_root/test/fuzztest/acereskeyparser_fuzzer/acereskeyparser_fuzzer.cpp",
#"$ace_root/frameworks/base/log/ace_trace.cpp",
"$ace_root/frameworks/core/components/test/unittest/mock/ace_trace_mock.cpp",

View File

@ -75,6 +75,7 @@ ohos_fuzztest("ImageApiFuzzTest") {
"$ace_root/frameworks/core/common/frontend.cpp",
"$ace_root/frameworks/core/common/thread_checker.cpp",
"$ace_root/frameworks/core/common/window.cpp",
"$ace_root/test/mock/interfaces/mock_ace_forward_compatibility.cpp",
# event
"$ace_root/frameworks/core/common/test/mock/mock_watch_dog.cpp",

View File

@ -83,6 +83,7 @@ ohos_fuzztest("ImageLoaderFuzzTest") {
"$ace_root/frameworks/core/components_ng/test/mock/image_provider/mock_skia_image_data.cpp",
"$ace_root/frameworks/core/components_ng/test/mock/render/mock_skia_image.cpp",
"$ace_root/test/mock/adapter/mock_file_uri_helper_ohos.cpp",
"$ace_root/test/mock/interfaces/mock_ace_forward_compatibility.cpp",
#fuzz
"$ace_root/adapter/ohos/entrance/file_asset_provider.cpp",

View File

@ -67,6 +67,7 @@ ohos_fuzztest("ManifestFuzzTest") {
"$ace_root/frameworks/core/common/frontend.cpp",
"$ace_root/frameworks/core/common/thread_checker.cpp",
"$ace_root/frameworks/core/common/window.cpp",
"$ace_root/test/mock/interfaces/mock_ace_forward_compatibility.cpp",
# event
"$ace_root/frameworks/core/common/test/mock/mock_watch_dog.cpp",

View File

@ -77,14 +77,15 @@ ohos_fuzztest("Res2FuzzTest") {
# mock
#"$ace_root/frameworks/base/test/mock/mock_system_properties.cpp",
"$ace_root/test/mock/interfaces/mock_ace_forward_compatibility.cpp",
#fuzz
"$ace_root/test/fuzztest/res2_fuzzer/res2_fuzzer.cpp",
"$ace_root/adapter/ohos/entrance/file_asset_provider.cpp",
"$ace_root/adapter/ohos/osal/system_properties.cpp",
"$ace_root/frameworks/base/resource/ace_res_config.cpp",
"$ace_root/frameworks/base/resource/ace_res_key_parser.cpp",
"$ace_root/frameworks/core/common/flutter/flutter_asset_manager.cpp",
"$ace_root/test/fuzztest/res2_fuzzer/res2_fuzzer.cpp",
#"$ace_root/frameworks/base/log/ace_trace.cpp",
"$ace_root/frameworks/core/components/test/unittest/mock/ace_trace_mock.cpp",

View File

@ -76,13 +76,14 @@ ohos_fuzztest("SharedImageManagerFuzzTest") {
# mock
#"$ace_root/frameworks/base/test/mock/mock_system_properties.cpp",
"$ace_root/test/mock/interfaces/mock_ace_forward_compatibility.cpp",
#fuzz
"$ace_root/test/fuzztest/sharedimagemanager_fuzzer/sharedimagemanager_fuzzer.cpp",
"$ace_root/adapter/ohos/entrance/file_asset_provider.cpp",
"$ace_root/adapter/ohos/osal/system_properties.cpp",
"$ace_root/frameworks/base/resource/shared_image_manager.cpp",
"$ace_root/frameworks/core/common/flutter/flutter_asset_manager.cpp",
"$ace_root/test/fuzztest/sharedimagemanager_fuzzer/sharedimagemanager_fuzzer.cpp",
#"$ace_root/frameworks/base/log/ace_trace.cpp",
"$ace_root/frameworks/core/components/test/unittest/mock/ace_trace_mock.cpp",

View File

@ -39,6 +39,7 @@ ohos_fuzztest("UtilEngineFuzzTest") {
"$ace_root/frameworks/bridge/js_frontend/engine/common/js_engine_loader.cpp",
"$ace_root/frameworks/core/components/test/unittest/mock/subwindow_mock.cpp",
"$ace_root/test/mock/core/common/mock_container.cpp",
"$ace_root/test/mock/interfaces/mock_ace_forward_compatibility.cpp",
"//third_party/cJSON/cJSON.c",
"utilengine_fuzzer.cpp",
]

View File

@ -0,0 +1,23 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "interfaces/inner_api/ace/ace_forward_compatibility.h"
namespace OHOS::Ace {
bool AceForwardCompatibility::IsUseNG()
{
return false;
}
} // namespace OHOS::Ace

View File

@ -424,6 +424,7 @@ source_set("ace_components_mock") {
"$ace_root/test/mock/core/common/mock_container.cpp",
"$ace_root/test/mock/core/render/mock_font_collection.cpp",
"$ace_root/test/mock/core/render/mock_paragraph.cpp",
"$ace_root/test/mock/interfaces/mock_ace_forward_compatibility.cpp",
]
deps = [ "//third_party/googletest:gmock_main" ]
configs += [ ":ace_unittest_config" ]

View File

@ -48,6 +48,7 @@ ohos_unittest("canvas_renderer_test_ng") {
"$ace_root/test/mock/adapter/mock_ace_container.cpp",
"$ace_root/test/mock/adapter/mock_mmi_event_convertor.cpp",
"$ace_root/test/mock/core/common/mock_udmf.cpp",
"$ace_root/test/mock/interfaces/mock_ace_forward_compatibility.cpp",
]
if (is_ohos_standard_system) {