同步代码

Signed-off-by: pancras <dange985@163.com>
This commit is contained in:
pancras 2024-11-15 11:09:55 +08:00
parent 9a6bc4f92d
commit 8121526eb8
11 changed files with 201 additions and 1 deletions

View File

@ -97,6 +97,10 @@
#include "nweb_helper.h"
#endif
#if defined(NWEB) && defined(NWEB_GRAPHIC)
#include "nweb_adapter_helper.h"
#endif
#ifdef IMAGE_PURGEABLE_PIXELMAP
#include "purgeable_resource_manager.h"
#endif
@ -167,6 +171,13 @@ const std::string SYSTEM_DEFAULT_FONTSIZE_SCALE = "1.0";
const int32_t TYPE_RESERVE = 1;
const int32_t TYPE_OTHERS = 2;
#if defined(NWEB) && defined(NWEB_GRAPHIC)
const std::string NWEB_SURFACE_NODE_NAME = "nwebPreloadSurface";
const std::string BLANK_URL = "about:blank";
constexpr uint32_t NWEB_SURFACE_SIZE = 1;
constexpr int32_t PRELOAD_TASK_DELAY_TIME = 2000; //millisecond
#endif
extern "C" int DFX_SetAppRunningUniqueId(const char* appRunningId, size_t len) __attribute__((weak));
} // namespace
@ -1732,7 +1743,58 @@ void MainThread::HandleLaunchApplication(const AppLaunchData &appLaunchData, con
OHOS::NWeb::NWebHelper::TryPreReadLib(isFirstStartUpWeb, app->GetAppContext()->GetBundleCodeDir());
}).detach();
#endif
#if defined(NWEB) && defined(NWEB_GRAPHIC)
if (appLaunchData.IsAllowedNWebPreload()) {
HandleNWebPreload();
}
#endif
}
#if defined(NWEB) && defined(NWEB_GRAPHIC)
void MainThread::HandleNWebPreload()
{
if (!mainHandler_) {
TAG_LOGE(AAFwkTag::APPKIT, "mainHandler is nullptr");
return;
}
auto task = [this]() {
auto ctx = AbilityRuntime::ApplicationContext::GetApplicationContext();
if (!ctx) {
TAG_LOGE(AAFwkTag::APPKIT, "get application context failed");
return;
}
NWeb::NWebHelper::Instance().SetBundlePath(ctx->GetBundleCodeDir());
if (!NWeb::NWebHelper::Instance().InitAndRun(true)) {
TAG_LOGE(AAFwkTag::APPKIT, "init NWebEngine failed");
return;
}
Rosen::RSSurfaceNodeConfig config;
config.SurfaceNodeName = NWEB_SURFACE_NODE_NAME;
preloadSurfaceNode_ = Rosen::RSSurfaceNode::Create(config, false);
auto surface = preloadSurfaceNode_->GetSurface();
if (!surface) {
TAG_LOGE(AAFwkTag::APPKIT, "preload surface is nullptr");
preloadSurfaceNode_ = nullptr;
return;
}
auto initArgs = std::make_shared<NWeb::NWebEngineInitArgsImpl>();
preloadNWeb_ = NWeb::NWebAdapterHelper::Instance().CreateNWeb(surface, initArgs,
NWEB_SURFACE_SIZE, NWEB_SURFACE_SIZE, false);
if (!preloadNWeb_) {
TAG_LOGE(AAFwkTag::APPKIT, "create preLoadNWeb failed");
return;
}
auto handler = std::make_shared<NWebPreloadHandlerImpl>();
preloadNWeb_->SetNWebHandler(handler);
preloadNWeb_->Load(BLANK_URL);
TAG_LOGI(AAFwkTag::APPKIT, "init NWeb success");
};
mainHandler_->PostIdleTask(task, "MainThread::NWEB_PRELOAD", PRELOAD_TASK_DELAY_TIME);
TAG_LOGI(AAFwkTag::APPKIT, "postIdleTask success");
}
#endif
#ifdef ABILITY_LIBRARY_LOADER
void MainThread::CalcNativeLiabraryEntries(const BundleInfo &bundleInfo, std::string &nativeLibraryPath)

View File

@ -227,6 +227,13 @@ public:
*/
virtual bool Marshalling(Parcel &parcel) const override;
/**
* @brief to solve cyclomatic of Marshalling
*
* @param outParcel Indicates the Parcel object to which the Sequenceable object will be marshaled.
*/
bool MarshallingExtend(Parcel &parcel) const;
/**
* @brief Unmarshals this Sequenceable object from a Parcel.
*
@ -246,6 +253,19 @@ public:
*/
bool isNativeStart() const;
/**
* @brief Setting if allow nweb preload.
*
* @param isAllowedNWebPreload, if allow nweb preload.
*/
void SetNWebPreload(const bool isAllowedNWebPreload);
/**
* @brief Get if allow nweb preload.
*
* @return Returns if allow nweb preload.
*/
bool IsAllowedNWebPreload() const;
private:
ApplicationInfo applicationInfo_;
Profile profile_;
@ -261,6 +281,7 @@ private:
bool isMultiThread_ = false;
bool isErrorInfoEnhance_ = false;
std::string appRunningUniqueId_;
bool isAllowedNWebPreload_ = false;
};
} // namespace AppExecFwk
} // namespace OHOS

View File

@ -96,6 +96,11 @@ bool AppLaunchData::Marshalling(Parcel &parcel) const
return false;
}
return MarshallingExtend(parcel);
}
bool AppLaunchData::MarshallingExtend(Parcel &parcel) const
{
if (!parcel.WriteBool(isMultiThread_)) {
TAG_LOGE(AAFwkTag::APPMGR, "Failed to write is multi thread flag.");
return false;
@ -106,6 +111,11 @@ bool AppLaunchData::Marshalling(Parcel &parcel) const
return false;
}
if (!parcel.WriteBool(isAllowedNWebPreload_)) {
TAG_LOGE(AAFwkTag::APPMGR, "Marshalling, Failed to write isAllowedNWebPreload.");
return false;
}
return true;
}
@ -152,6 +162,7 @@ bool AppLaunchData::ReadFromParcel(Parcel &parcel)
appRunningUniqueId_ = parcel.ReadString();
isMultiThread_ = parcel.ReadBool();
isErrorInfoEnhance_ = parcel.ReadBool();
isAllowedNWebPreload_ = parcel.ReadBool();
return true;
}
@ -242,5 +253,15 @@ bool AppLaunchData::isNativeStart() const
{
return isNativeStart_;
}
void AppLaunchData::SetNWebPreload(const bool isAllowedNWebPreload)
{
isAllowedNWebPreload_ = isAllowedNWebPreload;
}
bool AppLaunchData::IsAllowedNWebPreload() const
{
return isAllowedNWebPreload_;
}
} // namespace AppExecFwk
} // namespace OHOS

View File

@ -43,6 +43,11 @@
#include "app_jsheap_mem_info.h"
#define ABILITY_LIBRARY_LOADER
#if defined(NWEB) && defined(NWEB_GRAPHIC)
#include "nweb_preload.h"
#include "ui/rs_surface_node.h"
#endif
class Runtime;
namespace OHOS {
namespace AppExecFwk {
@ -623,6 +628,10 @@ private:
*/
void ParseAppConfigurationParams(const std::string configuration, Configuration &config);
#if defined(NWEB) && defined(NWEB_GRAPHIC)
void HandleNWebPreload();
#endif
class MainHandler : public EventHandler {
public:
MainHandler(const std::shared_ptr<EventRunner> &runner, const sptr<MainThread> &thread);
@ -659,6 +668,10 @@ private:
std::string abilityLibraryType_ = ".so";
static std::weak_ptr<OHOSApplication> applicationForDump_;
bool isDeveloperMode_ = false;
#if defined(NWEB) && defined(NWEB_GRAPHIC)
Rosen::RSSurfaceNode::SharedPtr preloadSurfaceNode_ = nullptr;
std::shared_ptr<NWeb::NWeb> preloadNWeb_ = nullptr;
#endif
#ifdef ABILITY_LIBRARY_LOADER
/**

View File

@ -0,0 +1,31 @@
/*
* Copyright (c) 2024 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.
*/
#ifndef OHOS_ABILITY_RUNTIME_NWEB_PRELOAD_H
#define OHOS_ABILITY_RUNTIME_NWEB_PRELOAD_H
#include "nweb_handler.h"
namespace OHOS {
namespace AppExecFwk {
class NWebPreloadHandlerImpl :
public OHOS::NWeb::NWebHandler {
public:
NWebPreloadHandlerImpl() = default;
~NWebPreloadHandlerImpl() = default;
};
} // AppExecFwk
} // OHOS
#endif

View File

@ -536,6 +536,15 @@ public:
*/
void LaunchApplication(const std::shared_ptr<AppRunningRecord> &appRecord);
/**
* LaunchApplicationExt, to solve the nbnc of method over 50.
*
* @param appRecord, the application record.
*
* @return
*/
void LaunchApplicationExt(const std::shared_ptr<AppRunningRecord> &appRecord);
/**
* Notice of AddAbilityStageInfo()
*
@ -1563,6 +1572,7 @@ private:
void DealMultiUserConfig(const Configuration &config, const int32_t userId);
int32_t KillProcessByPidInner(const pid_t pid, const std::string& reason,
const std::string& killReason, std::shared_ptr<AppRunningRecord> appRecord);
bool IsAllowedNWebPreload(const std::string &processName);
const std::string TASK_ON_CALLBACK_DIED = "OnCallbackDiedTask";
std::vector<AppStateCallbackWithUserId> appStateCallbacks_;
std::shared_ptr<RemoteClientManager> remoteClientManager_;
@ -1609,6 +1619,7 @@ private:
std::shared_ptr<AAFwk::TaskHandlerWrap> delayKillTaskHandler_;
std::shared_ptr<MultiUserConfigurationMgr> multiUserConfigurationMgr_;
std::unordered_set<std::string> nwebPreloadSet_ {};
};
} // namespace AppExecFwk
} // namespace OHOS

View File

@ -856,6 +856,7 @@ public:
bool HasUIAbilityLaunched();
void AddAppLifecycleEvent(const std::string &msg);
void SetNWebPreload(const bool isAllowedNWebPreload);
private:
/**
* SearchTheModuleInfoNeedToUpdated, Get an uninitialized abilityStage data.
@ -1013,6 +1014,7 @@ private:
bool isDependedOnArkWeb_ = false;
bool isUserRequestCleaning_ = false;
bool hasUIAbilityLaunched_ = false;
bool isAllowedNWebPreload_ = false;
};
} // namespace AppExecFwk

View File

@ -259,6 +259,8 @@ constexpr int32_t MAX_SPECIFIED_PROCESS_NAME_LENGTH = 255;
constexpr int32_t LEAK_WAIT = 900000;
constexpr int32_t NORMAL_WAIT = 120000;
constexpr int32_t NWEB_PRELOAD_DELAY = 3000;
int32_t GetUserIdByUid(int32_t uid)
{
return uid / BASE_USER_RANGE;
@ -332,6 +334,9 @@ void AppMgrServiceInner::Init()
if (configuration_) {
appRunningManager_->initConfig(*configuration_);
}
otherTaskHandler_->SubmitTask([pThis = shared_from_this()]() {
pThis->nwebPreloadSet_ = AAFwk::ResSchedUtil::GetInstance().GetNWebPreloadSet();
}, NWEB_PRELOAD_DELAY);
}
AppMgrServiceInner::~AppMgrServiceInner()
@ -958,7 +963,7 @@ void AppMgrServiceInner::AttachApplication(const pid_t pid, const sptr<IAppSched
appRecord->SetAppDeathRecipient(appDeathRecipient);
appRecord->SetApplicationClient(appScheduler);
if (appRecord->GetState() == ApplicationState::APP_STATE_CREATE) {
LaunchApplication(appRecord);
LaunchApplicationExt(appRecord);
}
// submit cached load ability task after scene board attach
@ -971,6 +976,19 @@ void AppMgrServiceInner::AttachApplication(const pid_t pid, const sptr<IAppSched
AAFwk::EventReport::SendAppEvent(AAFwk::EventName::APP_ATTACH, HiSysEventType::BEHAVIOR, eventInfo);
}
void AppMgrServiceInner::LaunchApplicationExt(const std::shared_ptr<AppRunningRecord> &appRecord)
{
auto isPreload = IsAllowedNWebPreload(appRecord->GetProcessName());
appRecord->SetNWebPreload(isPreload);
LaunchApplication(appRecord);
}
bool AppMgrServiceInner::IsAllowedNWebPreload(const std::string &processName)
{
// nwebPreloadSet_ only be initialized in Init(), no lock required.
return nwebPreloadSet_.count(processName);
}
void AppMgrServiceInner::NotifyAppAttachFailed(std::shared_ptr<AppRunningRecord> appRecord)
{
CHECK_POINTER_AND_RETURN_LOG(appRecord, "AppRecord null.");

View File

@ -481,6 +481,7 @@ void AppRunningRecord::LaunchApplication(const Configuration &config)
launchData.SetJITEnabled(jitEnabled_);
launchData.SetNativeStart(isNativeStart_);
launchData.SetAppRunningUniqueId(std::to_string(startTimeMillis_));
launchData.SetNWebPreload(isAllowedNWebPreload_);
TAG_LOGD(AAFwkTag::APPMGR, "%{public}s called,app is %{public}s.", __func__, GetName().c_str());
AddAppLifecycleEvent("AppRunningRecord::LaunchApplication");
@ -2511,5 +2512,10 @@ void AppRunningRecord::AddAppLifecycleEvent(const std::string &msg)
FreezeUtil::GetInstance().AddAppLifecycleEvent(prioObject->GetPid(), msg);
}
}
void AppRunningRecord::SetNWebPreload(const bool isAllowedNWebPreload)
{
isAllowedNWebPreload_ = isAllowedNWebPreload;
}
} // namespace AppExecFwk
} // namespace OHOS

View File

@ -49,6 +49,7 @@ public:
bool CheckShouldForceKillProcess(int32_t pid);
void ReportLoadingEventToRss(LoadingStage stage, int32_t pid, int32_t uid,
int64_t timeDuration = 0, int64_t abilityRecordId = -1);
std::unordered_set<std::string> GetNWebPreloadSet() const;
private:
ResSchedUtil() = default;
~ResSchedUtil() = default;

View File

@ -175,5 +175,19 @@ void ResSchedUtil::ReportLoadingEventToRss(LoadingStage stage, int32_t pid, int3
ResourceSchedule::ResSchedClient::GetInstance().ReportData(resType, type, eventParams);
#endif
}
std::unordered_set<std::string> ResSchedUtil::GetNWebPreloadSet() const
{
uint32_t resType = ResourceSchedule::ResType::SYNC_RES_TYPE_GET_NWEB_PRELOAD_SET;
nlohmann::json payload;
nlohmann::json reply;
ResourceSchedule::ResSchedClient::GetInstance().ReportSyncEvent(resType, 0, payload, reply);
if (!reply.contains("NWebPreloadSet")) {
TAG_LOGW(AAFwkTag::DEFAULT, "does not get preload process set");
return {};
}
auto jsonObj = reply["NWebPreloadSet"];
return { jsonObj.begin(), jsonObj.end() };
}
} // namespace AAFwk
} // namespace OHOS