diff --git a/appspawn.gni b/appspawn.gni index cc2fd8e38fe921a917a83760cc2bc2f92d6a5841..fae9d0778b627c90eec3a44ae77887c7ce8dc0f6 100644 --- a/appspawn.gni +++ b/appspawn.gni @@ -36,6 +36,7 @@ declare_args() { appspawn_support_code_signature = true appspawn_allow_internet_permission = false appspawn_custom_sandbox = false + appspawn_arkweb_preload = true } if (!defined(global_parts_info) || diff --git a/bundle.json b/bundle.json index 8deb812fb4fe4db4a302cbec67b4bc67c7595c60..a60534157d258505ef4bd8af76effb22c5e8689c 100644 --- a/bundle.json +++ b/bundle.json @@ -28,7 +28,8 @@ "appspawn_support_prefork", "appspawn_support_code_signature", "appspawn_allow_internet_permission", - "appspawn_custom_sandbox" + "appspawn_custom_sandbox", + "appspawn_arkweb_preload" ], "rom": "296KB", "ram": "13125KB", diff --git a/modules/nweb_adapter/BUILD.gn b/modules/nweb_adapter/BUILD.gn index acd15abe223a366520e29f0fe8fac4c159b09e25..1b73d080ccb7c0ac22c79aa51bed66e11db854a9 100644 --- a/modules/nweb_adapter/BUILD.gn +++ b/modules/nweb_adapter/BUILD.gn @@ -31,6 +31,9 @@ ohos_shared_library("appspawn_nweb") { if (target_cpu == "x86_64") { defines += [ "webview_x86_64" ] } + if (appspawn_arkweb_preload) { + defines += [ "arkweb_preload" ] + } external_deps = [ "c_utils:utils", diff --git a/modules/nweb_adapter/nwebspawn_adapter.cpp b/modules/nweb_adapter/nwebspawn_adapter.cpp index 6d0ae2730e1c37f3b222eb38d6a901f029eb14de..a0e16f7d377a0f5355669209cb2c4dbf3441f05d 100644 --- a/modules/nweb_adapter/nwebspawn_adapter.cpp +++ b/modules/nweb_adapter/nwebspawn_adapter.cpp @@ -29,16 +29,17 @@ #include "appspawn_hook.h" #include "appspawn_manager.h" +#include "parameter.h" #ifdef WITH_SECCOMP #include "seccomp_policy.h" #endif namespace { -#if defined(webview_arm64) +#ifdef webview_arm64 const std::string ARK_WEB_CORE_HAP_LIB_PATH = "/data/storage/el1/bundle/arkwebcore/libs/arm64"; -#elif defined(webview_x86_64) +#elif webview_x86_64 const std::string ARK_WEB_CORE_HAP_LIB_PATH = "/data/storage/el1/bundle/arkwebcore/libs/x86_64"; #else @@ -49,6 +50,23 @@ namespace { const std::string ARK_WEB_RENDER_LIB_NAME = "libarkweb_render.so"; const std::string WEB_ENGINE_LIB_NAME = "libweb_engine.so"; const std::string WEB_RENDER_LIB_NAME = "libnweb_render.so"; + const std::string ARK_WEB_ENGINE_LIB_PRELOAD_PATH = + "/data/app/el1/bundle/public/com.huawei.hmos.arkwebcore/libs/arm64"; + const std::string OHOS_ADPT_GLUE_SRC_LIB_PATH = + "/system/lib64/libohos_adapter_glue_source.z.so"; + +#ifdef arkweb_preload + const std::string ARKWEB_BUNDLE_NAME = "com.ohos.arkwebcore"; +#else + const std::string ARKWEB_BUNDLE_NAME = "com.huawei.hmos.arkwebcore"; +#endif + +typedef enum { + PRELOAD_NO = 0, // 不预加载 + PRELOAD_PARTIAL = 1, // 只预加载libohos_adapter_glue_source.z.so + PRELOAD_FULL = 2 // 预加载libohos_adapter_glue_source.z.so和libarkweb_engine.so +} RenderPreLoadMode; + } // namespace static bool SetSeccompPolicyForRenderer(void *nwebRenderHandle) @@ -148,6 +166,68 @@ APPSPAWN_STATIC int RunChildProcessor(AppSpawnContent *content, AppSpawnClient * return 0; } +static std::string GetOhosAdptGlueSrcLibPath() +{ +#ifdef webview_arm64 + const std::string ARK_WEB_CORE_HAP_LIB_PATH = + "/system/lib64/libohos_adapter_glue_source.z.so"; +#else + const std::string ARK_WEB_CORE_HAP_LIB_PATH = + "/system/lib/libohos_adapter_glue_source.z.so"; +#endif + return ARK_WEB_CORE_HAP_LIB_PATH; +} + +static std::string GetArkWebEngineLibPath() +{ +#ifdef webview_arm64 + const std::string ARK_WEB_CORE_HAP_LIB_PATH = + "/data/app/el1/bundle/public/" + ARKWEB_BUNDLE_NAME + "/libs/arm64"; +#elif webview_x86_64 + const std::string ARK_WEB_CORE_HAP_LIB_PATH = + "/data/app/el1/bundle/public/" + ARKWEB_BUNDLE_NAME + "/libs/x86_64"; +#else + const std::string ARK_WEB_CORE_HAP_LIB_PATH = + "/data/app/el1/bundle/public/" + ARKWEB_BUNDLE_NAME + "/libs/arm"; +#endif + return ARK_WEB_CORE_HAP_LIB_PATH; +} + +static void PreLoadArkWebEngineLib() +{ + Dl_namespace dlns; + Dl_namespace ndkns; + dlns_init(&dlns, "nweb_ns"); + const std::string arkWebEngineLibPath = GetArkWebEngineLibPath(); + dlns_create(&dlns, arkWebEngineLibPath.c_str()); + dlns_get("ndk", &ndkns); + dlns_inherit(&dlns, &ndkns, "allow_all_shared_libs"); + void *webEngineHandle = dlopen_ns(&dlns, ARK_WEB_ENGINE_LIB_NAME.c_str(), RTLD_NOW | RTLD_GLOBAL); + if (!webEngineHandle) { + APPSPAWN_LOGE("Fail to dlopen libarkweb_engine.so, errno: %{public}d", errno); + } +} + +static void PreLoadOHOSAdptGlueSrcLib() +{ + const std::string ohosAdptGlueSrcLibPath = GetOhosAdptGlueSrcLibPath(); + void *ohosAdptGlueSrcHandle = dlopen(ohosAdptGlueSrcLibPath.c_str(), RTLD_NOW | RTLD_GLOBAL); + if (!ohosAdptGlueSrcHandle) { + APPSPAWN_LOGE("Fail to dlopen libohos_adapter_glue_source.z.so, errno: %{public}d", errno); + } +} + +#ifndef arkweb_preload +static int GetSysParamPreLoadMode() +{ + const int BUFFER_LEN = 8; + char preLoadMode[BUFFER_LEN] = {0}; + GetParameter("const.startup.nwebspawn.preloadMode", "0", preLoadMode, BUFFER_LEN); + int ret = std::atoi(preLoadMode); + return ret; +} +#endif + static int PreLoadNwebSpawn(AppSpawnMgr *content) { APPSPAWN_LOGI("PreLoadNwebSpawn %{public}d", IsNWebSpawnMode(content)); @@ -156,6 +236,22 @@ static int PreLoadNwebSpawn(AppSpawnMgr *content) } // register RegChildLooper(&content->content, RunChildProcessor); + + // preload render lib +#ifdef arkweb_preload + int preloadMode = RenderPreLoadMode::PRELOAD_FULL; +#else + int preloadMode = GetSysParamPreLoadMode(); +#endif + APPSPAWN_LOGI("NwebSpawn preload render lib mode: %{public}d", preloadMode); + if (preloadMode == PRELOAD_PARTIAL) { + PreLoadOHOSAdptGlueSrcLib(); + } + if (preloadMode == PRELOAD_FULL) { + PreLoadArkWebEngineLib(); + PreLoadOHOSAdptGlueSrcLib(); + } + return 0; } diff --git a/nwebspawn.cfg b/nwebspawn.cfg index 7b41723fe3647ff34b049b82bdbaa0af5cfa437a..e6cc4dfbdfe4a7837fe8a224a01fdb55ee0b859c 100644 --- a/nwebspawn.cfg +++ b/nwebspawn.cfg @@ -1,4 +1,11 @@ { + "jobs" : [{ + "name" : "param:nwebspawnservicestart", + "condition" : "bootevent.boot.completed=true", + "cmds" : [ + "start nwebspawn" + ] + }], "services" : [{ "name" : "nwebspawn", "path" : ["/system/bin/nwebspawn", @@ -21,7 +28,7 @@ "option" : [ ] }], - "start-mode" : "boot", + "start-mode" : "condition", "sandbox" : 0, "secon" : "u:r:nwebspawn:s0" }