From 1f631ae680a8611cc0b53c4bfae6f381fb27894f Mon Sep 17 00:00:00 2001 From: donglin Date: Wed, 20 Dec 2023 06:18:09 +0000 Subject: [PATCH] add hms' hsp map Signed-off-by: donglin Change-Id: I4b3653d14f8f755d6fb95ac61d80fe49b48f7832 --- .../native/runtime/js_module_reader.cpp | 4 ++ frameworks/native/runtime/js_module_reader.h | 1 + frameworks/native/runtime/js_runtime.cpp | 68 +++++++++++++++++++ interfaces/inner_api/runtime/BUILD.gn | 2 + .../inner_api/runtime/include/js_runtime.h | 5 ++ 5 files changed, 80 insertions(+) diff --git a/frameworks/native/runtime/js_module_reader.cpp b/frameworks/native/runtime/js_module_reader.cpp index 76c6e36756..1913e8bf62 100755 --- a/frameworks/native/runtime/js_module_reader.cpp +++ b/frameworks/native/runtime/js_module_reader.cpp @@ -160,6 +160,10 @@ std::string JsModuleReader::GetPresetAppHapPath(const std::string& inputPath, co } std::string tmpPath = inputPath.substr(inputPath.find_first_of("/") + 1); const std::string sharedBundleName = tmpPath.substr(0, tmpPath.find_first_of("/")); + if (baseSharedBundleInfos.size() == 0) { + presetAppHapPath = "/system/app/appServiceFwk/" + sharedBundleName + "/" + moduleName + ".hsp"; + return presetAppHapPath; + } for (const auto &info : baseSharedBundleInfos) { if ((info.bundleName == sharedBundleName) && (info.moduleName == moduleName)) { presetAppHapPath = info.hapPath; diff --git a/frameworks/native/runtime/js_module_reader.h b/frameworks/native/runtime/js_module_reader.h index f2aa1d020f..082f69e5af 100755 --- a/frameworks/native/runtime/js_module_reader.h +++ b/frameworks/native/runtime/js_module_reader.h @@ -32,6 +32,7 @@ public: static constexpr char ABS_DATA_CODE_PATH[] = "/data/app/el1/bundle/public/"; static constexpr char BUNDLE[] = "bundle/"; static constexpr char MERGE_ABC_PATH[] = "ets/modules.abc"; + static constexpr char SYS_ABS_CODE_PATH[] = "/system/app/appServiceFwk/"; static constexpr char SHARED_FILE_SUFFIX[] = ".hsp"; JsModuleReader(const std::string& bundleName, const std::string& hapPath, bool isFormRender = false); ~JsModuleReader() = default; diff --git a/frameworks/native/runtime/js_runtime.cpp b/frameworks/native/runtime/js_runtime.cpp index 0531b22c45..33369e5fc7 100644 --- a/frameworks/native/runtime/js_runtime.cpp +++ b/frameworks/native/runtime/js_runtime.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -88,6 +89,13 @@ constexpr char MERGE_ABC_PATH[] = "/ets/modules.abc"; constexpr char BUNDLE_INSTALL_PATH[] = "/data/storage/el1/bundle/"; constexpr const char* PERMISSION_RUN_ANY_CODE = "ohos.permission.RUN_ANY_CODE"; +const std::string SYSTEM_KITS_CONFIG_PATH = "/system/etc/system_kits_config.json"; + +const std::string SYSTEM_KITS = "systemkits"; +const std::string NAMESPACE = "namespace"; +const std::string TARGET_OHM = "targetohm"; +const std::string SINCE_VERSION = "sinceVersion"; + static auto PermissionCheckFunc = []() { Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID(); @@ -638,6 +646,8 @@ bool JsRuntime::Initialize(const Options& options) panda::JSNApi::SetHostResolveBufferTracker( vm, JsModuleReader(options.bundleName, options.hapPath, options.isUnique)); isModular = !panda::JSNApi::IsBundle(vm); + std::vector systemKitsMap = GetSystemKitsMap(apiTargetVersion_); + panda::JSNApi::SetHmsModuleList(vm, systemKitsMap); } } @@ -1351,5 +1361,63 @@ void JsRuntime::SetDeviceDisconnectCallback(const std::function &cb) CHECK_POINTER(jsEnv_); jsEnv_->SetDeviceDisconnectCallback(cb); } + +std::vector JsRuntime::GetSystemKitsMap(uint32_t version) +{ + std::vector systemKitsMap; + nlohmann::json jsonBuf; + if (access(SYSTEM_KITS_CONFIG_PATH.c_str(), F_OK) != 0) { + return systemKitsMap; + } + + std::fstream in; + char errBuf[256]; + errBuf[0] = '\0'; + in.open(SYSTEM_KITS_CONFIG_PATH, std::ios_base::in); + if (!in.is_open()) { + strerror_r(errno, errBuf, sizeof(errBuf)); + return systemKitsMap; + } + + in.seekg(0, std::ios::end); + int64_t size = in.tellg(); + if (size <= 0) { + HILOG_ERROR("the file is an empty file"); + in.close(); + return systemKitsMap; + } + + in.seekg(0, std::ios::beg); + jsonBuf = nlohmann::json::parse(in, nullptr, false); + in.close(); + if (jsonBuf.is_discarded()) { + HILOG_ERROR("bad profile file"); + return systemKitsMap; + } + + if (!jsonBuf.contains(SYSTEM_KITS)) { + HILOG_ERROR("json config doesn't contain systemkits."); + return systemKitsMap; + } + for (auto &item : jsonBuf.at(SYSTEM_KITS).items()) { + nlohmann::json& jsonObject = item.value(); + if (!jsonObject.contains(NAMESPACE) || !jsonObject.at(NAMESPACE).is_string() || + !jsonObject.contains(TARGET_OHM) || !jsonObject.at(TARGET_OHM).is_string() || + !jsonObject.contains(SINCE_VERSION) || !jsonObject.at(SINCE_VERSION).is_number()) { + continue; + } + uint32_t sinceVersion = jsonObject.at(SINCE_VERSION).get(); + if (version >= sinceVersion) { + panda::HmsMap hmsMap = { + .originalPath = jsonObject.at(NAMESPACE).get(), + .targetPath = jsonObject.at(TARGET_OHM).get(), + .sinceVersion = sinceVersion + }; + systemKitsMap.emplace_back(hmsMap); + } + } + HILOG_DEBUG("The size of the map is %{public}zu", systemKitsMap.size()); + return systemKitsMap; +} } // namespace AbilityRuntime } // namespace OHOS diff --git a/interfaces/inner_api/runtime/BUILD.gn b/interfaces/inner_api/runtime/BUILD.gn index 5a4c3a24b3..68c442148d 100644 --- a/interfaces/inner_api/runtime/BUILD.gn +++ b/interfaces/inner_api/runtime/BUILD.gn @@ -40,6 +40,7 @@ config("runtime_public_config") { include_dirs = [ "${ability_runtime_path}/interfaces/kits/native/ability/native/ability_business_error", "include", + "//third_party/json/include", ] } @@ -85,6 +86,7 @@ ohos_shared_library("runtime") { "${ability_runtime_native_path}/ability/native:ability_business_error", "${ability_runtime_native_path}/appkit:appkit_manager_helper", "${ability_runtime_path}/js_environment/frameworks/js_environment:js_environment", + "${third_party_path}/jsoncpp:jsoncpp", "${third_party_path}/zlib:shared_libz", ] diff --git a/interfaces/inner_api/runtime/include/js_runtime.h b/interfaces/inner_api/runtime/include/js_runtime.h index 38ee111899..7a94b3c5e3 100644 --- a/interfaces/inner_api/runtime/include/js_runtime.h +++ b/interfaces/inner_api/runtime/include/js_runtime.h @@ -29,6 +29,9 @@ namespace panda::ecmascript { class EcmaVM; } // namespace panda::ecmascript +namespace panda { +struct HmsMap; +} namespace OHOS { namespace AppExecFwk { class EventHandler; @@ -163,6 +166,8 @@ private: void PostPreload(const Options& options); void LoadAotFile(const Options& options); void SetRequestAotCallback(); + + std::vector GetSystemKitsMap(uint32_t version); }; } // namespace AbilityRuntime } // namespace OHOS